ObjectCutDialog.pm 11 KB

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