ObjectCutDialog.pm 11 KB

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