ObjectCutDialog.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 POSIX qw(ceil);
  8. use Scalar::Util qw(looks_like_number);
  9. use Slic3r::Geometry qw(PI X Y Z);
  10. use Wx qw(wxTheApp :dialog :id :misc :sizer wxTAB_TRAVERSAL);
  11. use Wx::Event qw(EVT_CLOSE EVT_BUTTON);
  12. use base 'Wx::Dialog';
  13. sub new {
  14. my $class = shift;
  15. my ($parent, %params) = @_;
  16. my $self = $class->SUPER::new($parent, -1, $params{object}->name, wxDefaultPosition, [500,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  17. $self->{model_object_idx} = $params{model_object_idx};
  18. $self->{model_object} = $params{model_object};
  19. $self->{new_model_objects} = [];
  20. # Mark whether the mesh cut is valid.
  21. # If not, it needs to be recalculated by _update() on wxTheApp->CallAfter() or on exit of the dialog.
  22. $self->{mesh_cut_valid} = 0;
  23. # Note whether the window was already closed, so a pending update is not executed.
  24. $self->{already_closed} = 0;
  25. $self->{model_object}->transform_by_instance($self->{model_object}->get_instance(0), 1);
  26. # cut options
  27. my $size_z = $self->{model_object}->instance_bounding_box(0)->size->z;
  28. $self->{cut_options} = {
  29. axis => Z,
  30. z => $size_z/2,
  31. keep_upper => 0,
  32. keep_lower => 1,
  33. rotate_lower => 0,
  34. preview => 1,
  35. };
  36. my $optgroup;
  37. $optgroup = $self->{optgroup} = Slic3r::GUI::OptionsGroup->new(
  38. parent => $self,
  39. title => 'Cut',
  40. on_change => sub {
  41. my ($opt_id) = @_;
  42. # There seems to be an issue with wxWidgets 3.0.2/3.0.3, where the slider
  43. # genates tens of events for a single value change.
  44. # Only trigger the recalculation if the value changes
  45. # or a live preview was activated and the mesh cut is not valid yet.
  46. if ($self->{cut_options}{$opt_id} != $optgroup->get_value($opt_id) ||
  47. ! $self->{mesh_cut_valid} && $self->_life_preview_active()) {
  48. $self->{cut_options}{$opt_id} = $optgroup->get_value($opt_id);
  49. $self->{mesh_cut_valid} = 0;
  50. wxTheApp->CallAfter(sub {
  51. $self->_update;
  52. });
  53. }
  54. },
  55. label_width => 120,
  56. );
  57. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  58. opt_id => 'axis',
  59. type => 'select',
  60. label => 'Axis',
  61. labels => ['X','Y','Z'],
  62. values => [X,Y,Z],
  63. default => $self->{cut_options}{axis},
  64. ));
  65. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  66. opt_id => 'z',
  67. type => 'slider',
  68. label => 'Z',
  69. default => $self->{cut_options}{z},
  70. min => 0,
  71. max => $size_z,
  72. full_width => 1,
  73. ));
  74. {
  75. my $line = Slic3r::GUI::OptionsGroup::Line->new(
  76. label => 'Keep',
  77. );
  78. $line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
  79. opt_id => 'keep_upper',
  80. type => 'bool',
  81. label => 'Upper part',
  82. default => $self->{cut_options}{keep_upper},
  83. ));
  84. $line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
  85. opt_id => 'keep_lower',
  86. type => 'bool',
  87. label => 'Lower part',
  88. default => $self->{cut_options}{keep_lower},
  89. ));
  90. $optgroup->append_line($line);
  91. }
  92. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  93. opt_id => 'rotate_lower',
  94. label => 'Rotate lower part upwards',
  95. type => 'bool',
  96. tooltip => 'If enabled, the lower part will be rotated by 180° so that the flat cut surface lies on the print bed.',
  97. default => $self->{cut_options}{rotate_lower},
  98. ));
  99. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  100. opt_id => 'preview',
  101. label => 'Show preview',
  102. type => 'bool',
  103. tooltip => 'If enabled, object will be cut in real time.',
  104. default => $self->{cut_options}{preview},
  105. ));
  106. {
  107. my $cut_button_sizer = Wx::BoxSizer->new(wxVERTICAL);
  108. $self->{btn_cut} = Wx::Button->new($self, -1, "Perform cut", wxDefaultPosition, wxDefaultSize);
  109. $self->{btn_cut}->SetDefault;
  110. $cut_button_sizer->Add($self->{btn_cut}, 0, wxALIGN_RIGHT | wxALL, 10);
  111. $self->{btn_cut_grid} = Wx::Button->new($self, -1, "Cut by grid…", wxDefaultPosition, wxDefaultSize);
  112. $cut_button_sizer->Add($self->{btn_cut_grid}, 0, wxALIGN_RIGHT | wxALL, 10);
  113. $optgroup->append_line(Slic3r::GUI::OptionsGroup::Line->new(
  114. sizer => $cut_button_sizer,
  115. ));
  116. }
  117. # left pane with tree
  118. my $left_sizer = Wx::BoxSizer->new(wxVERTICAL);
  119. $left_sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  120. # right pane with preview canvas
  121. my $canvas;
  122. if ($Slic3r::GUI::have_OpenGL) {
  123. $canvas = $self->{canvas} = Slic3r::GUI::3DScene->new($self);
  124. $canvas->load_object($self->{model_object}, undef, [0]);
  125. $canvas->set_auto_bed_shape;
  126. $canvas->SetSize([500,500]);
  127. $canvas->SetMinSize($canvas->GetSize);
  128. $canvas->zoom_to_volumes;
  129. }
  130. $self->{sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
  131. $self->{sizer}->Add($left_sizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
  132. $self->{sizer}->Add($canvas, 1, wxEXPAND | wxALL, 0) if $canvas;
  133. $self->SetSizer($self->{sizer});
  134. $self->SetMinSize($self->GetSize);
  135. $self->{sizer}->SetSizeHints($self);
  136. EVT_BUTTON($self, $self->{btn_cut}, sub {
  137. # Recalculate the cut if the preview was not active.
  138. $self->_perform_cut() unless $self->{mesh_cut_valid};
  139. # Adjust position / orientation of the split object halves.
  140. if (my $lower = $self->{new_model_objects}[0]) {
  141. if ($self->{cut_options}{rotate_lower} && $self->{cut_options}{axis} == Z) {
  142. $lower->rotate(PI, X);
  143. }
  144. $lower->center_around_origin; # align to Z = 0
  145. }
  146. if (my $upper = $self->{new_model_objects}[1]) {
  147. $upper->center_around_origin; # align to Z = 0
  148. }
  149. # Note that the window was already closed, so a pending update will not be executed.
  150. $self->{already_closed} = 1;
  151. $self->EndModal(wxID_OK);
  152. $self->Destroy();
  153. });
  154. EVT_BUTTON($self, $self->{btn_cut_grid}, sub {
  155. my $grid_x = Wx::GetTextFromUser("Enter the width of the desired tiles along the X axis:",
  156. "Cut by Grid", 100, $self);
  157. return if !looks_like_number($grid_x) || $grid_x <= 0;
  158. my $grid_y = Wx::GetTextFromUser("Enter the width of the desired tiles along the Y axis:",
  159. "Cut by Grid", 100, $self);
  160. return if !looks_like_number($grid_y) || $grid_y <= 0;
  161. my $process_dialog = Wx::ProgressDialog->new('Cutting…', "Cutting model by grid…", 100, $self, 0);
  162. $process_dialog->Pulse;
  163. my $meshes = $self->{model_object}->mesh->cut_by_grid(Slic3r::Pointf->new($grid_x, $grid_y));
  164. $self->{new_model_objects} = [];
  165. my $bb = $self->{model_object}->bounding_box;
  166. $self->{new_model} = my $model = Slic3r::Model->new;
  167. for my $i (0..$#$meshes) {
  168. push @{$self->{new_model_objects}}, my $o = $model->add_object(
  169. name => sprintf('%s (%d)', $self->{model_object}->name, $i+1),
  170. );
  171. my $v = $o->add_volume(
  172. mesh => $meshes->[$i],
  173. name => $o->name,
  174. );
  175. $o->center_around_origin;
  176. my $i = $o->add_instance(
  177. offset => Slic3r::Pointf->new(@{$o->origin_translation->negative}[X,Y]),
  178. );
  179. $i->offset->translate(
  180. 5 * ceil(($i->offset->x - $bb->center->x) / $grid_x),
  181. 5 * ceil(($i->offset->y - $bb->center->y) / $grid_y),
  182. );
  183. }
  184. $process_dialog->Destroy;
  185. # Note that the window was already closed, so a pending update will not be executed.
  186. $self->{already_closed} = 1;
  187. $self->EndModal(wxID_OK);
  188. $self->Destroy();
  189. });
  190. EVT_CLOSE($self, sub {
  191. # Note that the window was already closed, so a pending update will not be executed.
  192. $self->{already_closed} = 1;
  193. $self->EndModal(wxID_CANCEL);
  194. $self->Destroy();
  195. });
  196. $self->_update;
  197. return $self;
  198. }
  199. # scale Z down to original size since we're using the transformed mesh for 3D preview
  200. # and cut dialog but ModelObject::cut() needs Z without any instance transformation
  201. sub _mesh_slice_z_pos
  202. {
  203. my ($self) = @_;
  204. my $bb = $self->{model_object}->instance_bounding_box(0);
  205. my $z = $self->{cut_options}{axis} == X ? $bb->x_min
  206. : $self->{cut_options}{axis} == Y ? $bb->y_min
  207. : $bb->z_min;
  208. $z += $self->{cut_options}{z} / $self->{model_object}->instances->[0]->scaling_factor;
  209. return $z;
  210. }
  211. # Only perform live preview if just a single part of the object shall survive.
  212. sub _life_preview_active
  213. {
  214. my ($self) = @_;
  215. return $self->{cut_options}{preview} && ($self->{cut_options}{keep_upper} != $self->{cut_options}{keep_lower});
  216. }
  217. # Slice the mesh, keep the top / bottom part.
  218. sub _perform_cut
  219. {
  220. my ($self) = @_;
  221. # Early exit. If the cut is valid, don't recalculate it.
  222. return if $self->{mesh_cut_valid};
  223. my $z = $self->_mesh_slice_z_pos();
  224. my ($new_model) = $self->{model_object}->cut($self->{cut_options}{axis}, $z);
  225. my ($upper_object, $lower_object) = @{$new_model->objects};
  226. $self->{new_model} = $new_model;
  227. $self->{new_model_objects} = [];
  228. if ($self->{cut_options}{keep_upper} && $upper_object->volumes_count > 0) {
  229. $self->{new_model_objects}[1] = $upper_object;
  230. }
  231. if ($self->{cut_options}{keep_lower} && $lower_object->volumes_count > 0) {
  232. $self->{new_model_objects}[0] = $lower_object;
  233. }
  234. $self->{mesh_cut_valid} = 1;
  235. }
  236. sub _update {
  237. my ($self) = @_;
  238. # Don't update if the window was already closed.
  239. # We are not sure whether the action planned by wxTheApp->CallAfter() may be triggered after the window is closed.
  240. # Probably not, but better be safe than sorry, which is espetially true on multiple platforms.
  241. return if $self->{already_closed};
  242. # Only recalculate the cut, if the live cut preview is active.
  243. my $life_preview_active = $self->_life_preview_active();
  244. $self->_perform_cut() if $life_preview_active;
  245. {
  246. # scale Z down to original size since we're using the transformed mesh for 3D preview
  247. # and cut dialog but ModelObject::cut() needs Z without any instance transformation
  248. my $z = $self->_mesh_slice_z_pos();
  249. # update canvas
  250. if ($self->{canvas}) {
  251. # get volumes to render
  252. my @objects = ();
  253. if ($life_preview_active) {
  254. push @objects, grep defined, @{$self->{new_model_objects}};
  255. } else {
  256. push @objects, $self->{model_object};
  257. }
  258. # get section contour
  259. my @expolygons = ();
  260. foreach my $volume (@{$self->{model_object}->volumes}) {
  261. next if !$volume->mesh;
  262. next if $volume->modifier;
  263. my $expp = $volume->mesh->slice_at($self->{cut_options}{axis}, $z);
  264. push @expolygons, @$expp;
  265. }
  266. my $offset = $self->{model_object}->instances->[0]->offset;
  267. foreach my $expolygon (@expolygons) {
  268. $self->{model_object}->instances->[0]->transform_polygon($_)
  269. for @$expolygon;
  270. if ($self->{cut_options}{axis} != X) {
  271. $expolygon->translate(0, Slic3r::Geometry::scale($offset->y)); #)
  272. }
  273. if ($self->{cut_options}{axis} != Y) {
  274. $expolygon->translate(Slic3r::Geometry::scale($offset->x), 0);
  275. }
  276. }
  277. $self->{canvas}->reset_objects;
  278. $self->{canvas}->load_object($_, undef, [0]) for @objects;
  279. my $plane_z = $self->{cut_options}{z};
  280. $plane_z += 0.02 if !$self->{cut_options}{keep_upper};
  281. $plane_z -= 0.02 if !$self->{cut_options}{keep_lower};
  282. $self->{canvas}->SetCuttingPlane(
  283. $self->{cut_options}{axis},
  284. $plane_z,
  285. [@expolygons],
  286. );
  287. $self->{canvas}->Render;
  288. }
  289. }
  290. # update controls
  291. {
  292. my $z = $self->{cut_options}{z};
  293. my $optgroup = $self->{optgroup};
  294. {
  295. my $bb = $self->{model_object}->instance_bounding_box(0);
  296. my $max = $self->{cut_options}{axis} == X ? $bb->size->x
  297. : $self->{cut_options}{axis} == Y ? $bb->size->y ###
  298. : $bb->size->z;
  299. $optgroup->get_field('z')->set_range(0, $max);
  300. }
  301. $optgroup->get_field('keep_upper')->toggle(my $have_upper = abs($z - $optgroup->get_option('z')->max) > 0.1);
  302. $optgroup->get_field('keep_lower')->toggle(my $have_lower = $z > 0.1);
  303. $optgroup->get_field('rotate_lower')->toggle($z > 0 && $self->{cut_options}{keep_lower} && $self->{cut_options}{axis} == Z);
  304. $optgroup->get_field('preview')->toggle($self->{cut_options}{keep_upper} != $self->{cut_options}{keep_lower});
  305. # update cut button
  306. if (($self->{cut_options}{keep_upper} && $have_upper)
  307. || ($self->{cut_options}{keep_lower} && $have_lower)) {
  308. $self->{btn_cut}->Enable;
  309. } else {
  310. $self->{btn_cut}->Disable;
  311. }
  312. }
  313. }
  314. sub NewModelObjects {
  315. my ($self) = @_;
  316. return grep defined, @{ $self->{new_model_objects} };
  317. }
  318. 1;