ObjectCutDialog.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # Cut an object at a Z position, keep either the top or the bottom of the object.
  2. # This dialog gets opened with the "Cut..." button above the platter.
  3. package Slic3r::GUI::Plater::ObjectCutDialog;
  4. use strict;
  5. use warnings;
  6. use utf8;
  7. use Slic3r::Geometry qw(PI X);
  8. use Wx qw(wxTheApp :dialog :id :misc :sizer wxTAB_TRAVERSAL);
  9. use Wx::Event qw(EVT_CLOSE EVT_BUTTON);
  10. use List::Util qw(max);
  11. use base 'Wx::Dialog';
  12. sub new {
  13. my ($class, $parent, %params) = @_;
  14. my $self = $class->SUPER::new($parent, -1, $params{object}->name, wxDefaultPosition, [500,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  15. $self->{model_object} = $params{model_object};
  16. $self->{new_model_objects} = [];
  17. # Mark whether the mesh cut is valid.
  18. # If not, it needs to be recalculated by _update() on wxTheApp->CallAfter() or on exit of the dialog.
  19. $self->{mesh_cut_valid} = 0;
  20. # Note whether the window was already closed, so a pending update is not executed.
  21. $self->{already_closed} = 0;
  22. # cut options
  23. $self->{cut_options} = {
  24. z => 0,
  25. keep_upper => 1,
  26. keep_lower => 1,
  27. rotate_lower => 1,
  28. # preview => 1,
  29. # Disabled live preview by default as it is not stable and/or the calculation takes too long for interactive usage.
  30. preview => 0,
  31. };
  32. my $optgroup;
  33. $optgroup = $self->{optgroup} = Slic3r::GUI::OptionsGroup->new(
  34. parent => $self,
  35. title => 'Cut',
  36. on_change => sub {
  37. my ($opt_id) = @_;
  38. # There seems to be an issue with wxWidgets 3.0.2/3.0.3, where the slider
  39. # genates tens of events for a single value change.
  40. # Only trigger the recalculation if the value changes
  41. # or a live preview was activated and the mesh cut is not valid yet.
  42. if ($self->{cut_options}{$opt_id} != $optgroup->get_value($opt_id) ||
  43. ! $self->{mesh_cut_valid} && $self->_life_preview_active()) {
  44. $self->{cut_options}{$opt_id} = $optgroup->get_value($opt_id);
  45. $self->{mesh_cut_valid} = 0;
  46. wxTheApp->CallAfter(sub {
  47. $self->_update;
  48. });
  49. }
  50. },
  51. label_width => 120,
  52. );
  53. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  54. opt_id => 'z',
  55. type => 'slider',
  56. label => 'Z',
  57. default => $self->{cut_options}{z},
  58. min => 0,
  59. max => $self->{model_object}->bounding_box->size->z,
  60. full_width => 1,
  61. ));
  62. {
  63. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  64. label => 'Keep',
  65. );
  66. $line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
  67. opt_id => 'keep_upper',
  68. type => 'bool',
  69. label => 'Upper part',
  70. default => $self->{cut_options}{keep_upper},
  71. ));
  72. $line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
  73. opt_id => 'keep_lower',
  74. type => 'bool',
  75. label => 'Lower part',
  76. default => $self->{cut_options}{keep_lower},
  77. ));
  78. $optgroup->append_line($line);
  79. }
  80. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  81. opt_id => 'rotate_lower',
  82. label => 'Rotate lower part upwards',
  83. type => 'bool',
  84. tooltip => 'If enabled, the lower part will be rotated by 180° so that the flat cut surface lies on the print bed.',
  85. default => $self->{cut_options}{rotate_lower},
  86. ));
  87. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  88. opt_id => 'preview',
  89. label => 'Show preview',
  90. type => 'bool',
  91. tooltip => 'If enabled, object will be cut in real time.',
  92. default => $self->{cut_options}{preview},
  93. ));
  94. {
  95. my $cut_button_sizer = Wx::BoxSizer->new(wxVERTICAL);
  96. $self->{btn_cut} = Wx::Button->new($self, -1, "Perform cut", wxDefaultPosition, wxDefaultSize);
  97. $cut_button_sizer->Add($self->{btn_cut}, 0, wxALIGN_RIGHT | wxALL, 10);
  98. $optgroup->append_line(Slic3r::GUI::OptionsGroup::Line->new(
  99. sizer => $cut_button_sizer,
  100. ));
  101. }
  102. # left pane with tree
  103. my $left_sizer = Wx::BoxSizer->new(wxVERTICAL);
  104. $left_sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  105. # right pane with preview canvas
  106. my $canvas;
  107. if ($Slic3r::GUI::have_OpenGL) {
  108. $canvas = $self->{canvas} = Slic3r::GUI::3DScene->new($self);
  109. Slic3r::GUI::_3DScene::load_model_object($self->{canvas}, $self->{model_object}, 0, [0]);
  110. Slic3r::GUI::_3DScene::set_auto_bed_shape($canvas);
  111. Slic3r::GUI::_3DScene::set_axes_length($canvas, 2.0 * max(@{ Slic3r::GUI::_3DScene::get_volumes_bounding_box($canvas)->size }));
  112. $canvas->SetSize([500,500]);
  113. $canvas->SetMinSize($canvas->GetSize);
  114. Slic3r::GUI::_3DScene::set_config($canvas, $self->GetParent->{config});
  115. Slic3r::GUI::_3DScene::enable_force_zoom_to_bed($canvas, 1);
  116. }
  117. $self->{sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
  118. $self->{sizer}->Add($left_sizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
  119. $self->{sizer}->Add($canvas, 1, wxEXPAND | wxALL, 0) if $canvas;
  120. $self->SetSizer($self->{sizer});
  121. $self->SetMinSize($self->GetSize);
  122. $self->{sizer}->SetSizeHints($self);
  123. EVT_BUTTON($self, $self->{btn_cut}, sub {
  124. # Recalculate the cut if the preview was not active.
  125. $self->_perform_cut() unless $self->{mesh_cut_valid};
  126. # Adjust position / orientation of the split object halves.
  127. if ($self->{new_model_objects}{lower}) {
  128. if ($self->{cut_options}{rotate_lower}) {
  129. $self->{new_model_objects}{lower}->rotate(PI, X);
  130. $self->{new_model_objects}{lower}->center_around_origin; # align to Z = 0
  131. }
  132. }
  133. if ($self->{new_model_objects}{upper}) {
  134. $self->{new_model_objects}{upper}->center_around_origin; # align to Z = 0
  135. }
  136. # Note that the window was already closed, so a pending update will not be executed.
  137. $self->{already_closed} = 1;
  138. $self->EndModal(wxID_OK);
  139. $self->{canvas}->Destroy;
  140. $self->Destroy();
  141. });
  142. EVT_CLOSE($self, sub {
  143. # Note that the window was already closed, so a pending update will not be executed.
  144. $self->{already_closed} = 1;
  145. $self->EndModal(wxID_CANCEL);
  146. $self->{canvas}->Destroy;
  147. $self->Destroy();
  148. });
  149. $self->_update;
  150. return $self;
  151. }
  152. # scale Z down to original size since we're using the transformed mesh for 3D preview
  153. # and cut dialog but ModelObject::cut() needs Z without any instance transformation
  154. sub _mesh_slice_z_pos
  155. {
  156. my ($self) = @_;
  157. return $self->{cut_options}{z} / $self->{model_object}->instances->[0]->scaling_factor;
  158. }
  159. # Only perform live preview if just a single part of the object shall survive.
  160. sub _life_preview_active
  161. {
  162. my ($self) = @_;
  163. return $self->{cut_options}{preview} && ($self->{cut_options}{keep_upper} != $self->{cut_options}{keep_lower});
  164. }
  165. # Slice the mesh, keep the top / bottom part.
  166. sub _perform_cut
  167. {
  168. my ($self) = @_;
  169. # Early exit. If the cut is valid, don't recalculate it.
  170. return if $self->{mesh_cut_valid};
  171. my $z = $self->_mesh_slice_z_pos();
  172. my ($new_model) = $self->{model_object}->cut($z);
  173. my ($upper_object, $lower_object) = @{$new_model->objects};
  174. $self->{new_model} = $new_model;
  175. $self->{new_model_objects} = {};
  176. if ($self->{cut_options}{keep_upper} && $upper_object->volumes_count > 0) {
  177. $self->{new_model_objects}{upper} = $upper_object;
  178. }
  179. if ($self->{cut_options}{keep_lower} && $lower_object->volumes_count > 0) {
  180. $self->{new_model_objects}{lower} = $lower_object;
  181. }
  182. $self->{mesh_cut_valid} = 1;
  183. }
  184. sub _update {
  185. my ($self) = @_;
  186. # Don't update if the window was already closed.
  187. # We are not sure whether the action planned by wxTheApp->CallAfter() may be triggered after the window is closed.
  188. # Probably not, but better be safe than sorry, which is espetially true on multiple platforms.
  189. return if $self->{already_closed};
  190. # Only recalculate the cut, if the live cut preview is active.
  191. my $life_preview_active = $self->_life_preview_active();
  192. $self->_perform_cut() if $life_preview_active;
  193. {
  194. # scale Z down to original size since we're using the transformed mesh for 3D preview
  195. # and cut dialog but ModelObject::cut() needs Z without any instance transformation
  196. my $z = $self->_mesh_slice_z_pos();
  197. # update canvas
  198. if ($self->{canvas}) {
  199. # get volumes to render
  200. my @objects = ();
  201. if ($life_preview_active) {
  202. push @objects, values %{$self->{new_model_objects}};
  203. } else {
  204. push @objects, $self->{model_object};
  205. }
  206. my $z_cut = $z + $self->{model_object}->bounding_box->z_min;
  207. # get section contour
  208. my @expolygons = ();
  209. foreach my $volume (@{$self->{model_object}->volumes}) {
  210. next if !$volume->mesh;
  211. next if $volume->modifier;
  212. my $expp = $volume->mesh->slice([ $z_cut ])->[0];
  213. push @expolygons, @$expp;
  214. }
  215. foreach my $expolygon (@expolygons) {
  216. $self->{model_object}->instances->[0]->transform_polygon($_)
  217. for @$expolygon;
  218. $expolygon->translate(map Slic3r::Geometry::scale($_), @{ $self->{model_object}->instances->[0]->offset });
  219. }
  220. Slic3r::GUI::_3DScene::reset_volumes($self->{canvas});
  221. Slic3r::GUI::_3DScene::load_model_object($self->{canvas}, $_, 0, [0]) for @objects;
  222. Slic3r::GUI::_3DScene::set_cutting_plane($self->{canvas}, $self->{cut_options}{z}, [@expolygons]);
  223. Slic3r::GUI::_3DScene::update_volumes_colors_by_extruder($self->{canvas});
  224. Slic3r::GUI::_3DScene::render($self->{canvas});
  225. }
  226. }
  227. # update controls
  228. {
  229. my $z = $self->{cut_options}{z};
  230. my $optgroup = $self->{optgroup};
  231. $optgroup->get_field('keep_upper')->toggle(my $have_upper = abs($z - $optgroup->get_option('z')->max) > 0.1);
  232. $optgroup->get_field('keep_lower')->toggle(my $have_lower = $z > 0.1);
  233. $optgroup->get_field('rotate_lower')->toggle($z > 0 && $self->{cut_options}{keep_lower});
  234. # Disabled live preview by default as it is not stable and/or the calculation takes too long for interactive usage.
  235. # $optgroup->get_field('preview')->toggle($self->{cut_options}{keep_upper} != $self->{cut_options}{keep_lower});
  236. # update cut button
  237. if (($self->{cut_options}{keep_upper} && $have_upper)
  238. || ($self->{cut_options}{keep_lower} && $have_lower)) {
  239. $self->{btn_cut}->Enable;
  240. } else {
  241. $self->{btn_cut}->Disable;
  242. }
  243. }
  244. }
  245. sub NewModelObjects {
  246. my ($self) = @_;
  247. return values %{ $self->{new_model_objects} };
  248. }
  249. 1;