ObjectPartsPanel.pm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. # Configuration of mesh modifiers and their parameters.
  2. # This panel is inserted into ObjectSettingsDialog.
  3. package Slic3r::GUI::Plater::ObjectPartsPanel;
  4. use strict;
  5. use warnings;
  6. use utf8;
  7. use File::Basename qw(basename);
  8. use Wx qw(:misc :sizer :treectrl :button :keycode wxTAB_TRAVERSAL wxSUNKEN_BORDER wxBITMAP_TYPE_PNG wxID_CANCEL wxMOD_CONTROL
  9. wxTheApp);
  10. use Wx::Event qw(EVT_BUTTON EVT_TREE_ITEM_COLLAPSING EVT_TREE_SEL_CHANGED EVT_TREE_KEY_DOWN EVT_KEY_DOWN);
  11. use List::Util qw(max);
  12. use base 'Wx::Panel';
  13. use constant ICON_OBJECT => 0;
  14. use constant ICON_SOLIDMESH => 1;
  15. use constant ICON_MODIFIERMESH => 2;
  16. sub new {
  17. my ($class, $parent, %params) = @_;
  18. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
  19. # C++ type Slic3r::ModelObject
  20. my $object = $self->{model_object} = $params{model_object};
  21. # Save state for sliders.
  22. $self->{move_options} = {
  23. x => 0,
  24. y => 0,
  25. z => 0,
  26. };
  27. $self->{last_coords} = {
  28. x => 0,
  29. y => 0,
  30. z => 0,
  31. };
  32. # create TreeCtrl
  33. my $tree = $self->{tree} = Wx::TreeCtrl->new($self, -1, wxDefaultPosition, [300, 100],
  34. wxTR_NO_BUTTONS | wxSUNKEN_BORDER | wxTR_HAS_VARIABLE_ROW_HEIGHT
  35. | wxTR_SINGLE);
  36. {
  37. $self->{tree_icons} = Wx::ImageList->new(16, 16, 1);
  38. $tree->AssignImageList($self->{tree_icons});
  39. $self->{tree_icons}->Add(Wx::Bitmap->new(Slic3r::var("brick.png"), wxBITMAP_TYPE_PNG)); # ICON_OBJECT
  40. $self->{tree_icons}->Add(Wx::Bitmap->new(Slic3r::var("package.png"), wxBITMAP_TYPE_PNG)); # ICON_SOLIDMESH
  41. $self->{tree_icons}->Add(Wx::Bitmap->new(Slic3r::var("plugin.png"), wxBITMAP_TYPE_PNG)); # ICON_MODIFIERMESH
  42. my $rootId = $tree->AddRoot("Object", ICON_OBJECT);
  43. $tree->SetPlData($rootId, { type => 'object' });
  44. }
  45. # buttons
  46. $self->{btn_load_part} = Wx::Button->new($self, -1, "Load part…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  47. $self->{btn_load_modifier} = Wx::Button->new($self, -1, "Load modifier…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  48. $self->{btn_load_lambda_modifier} = Wx::Button->new($self, -1, "Load generic…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  49. $self->{btn_delete} = Wx::Button->new($self, -1, "Delete part", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  50. $self->{btn_split} = Wx::Button->new($self, -1, "Split part", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  51. $self->{btn_move_up} = Wx::Button->new($self, -1, "", wxDefaultPosition, [40, -1], wxBU_LEFT);
  52. $self->{btn_move_down} = Wx::Button->new($self, -1, "", wxDefaultPosition, [40, -1], wxBU_LEFT);
  53. $self->{btn_load_part}->SetBitmap(Wx::Bitmap->new(Slic3r::var("brick_add.png"), wxBITMAP_TYPE_PNG));
  54. $self->{btn_load_modifier}->SetBitmap(Wx::Bitmap->new(Slic3r::var("brick_add.png"), wxBITMAP_TYPE_PNG));
  55. $self->{btn_load_lambda_modifier}->SetBitmap(Wx::Bitmap->new(Slic3r::var("brick_add.png"), wxBITMAP_TYPE_PNG));
  56. $self->{btn_delete}->SetBitmap(Wx::Bitmap->new(Slic3r::var("brick_delete.png"), wxBITMAP_TYPE_PNG));
  57. $self->{btn_split}->SetBitmap(Wx::Bitmap->new(Slic3r::var("shape_ungroup.png"), wxBITMAP_TYPE_PNG));
  58. $self->{btn_move_up}->SetBitmap(Wx::Bitmap->new(Slic3r::var("bullet_arrow_up.png"), wxBITMAP_TYPE_PNG));
  59. $self->{btn_move_down}->SetBitmap(Wx::Bitmap->new(Slic3r::var("bullet_arrow_down.png"), wxBITMAP_TYPE_PNG));
  60. # buttons sizer
  61. my $buttons_sizer = Wx::GridSizer->new(2, 3);
  62. $buttons_sizer->Add($self->{btn_load_part}, 0, wxEXPAND | wxBOTTOM | wxRIGHT, 5);
  63. $buttons_sizer->Add($self->{btn_load_modifier}, 0, wxEXPAND | wxBOTTOM | wxRIGHT, 5);
  64. $buttons_sizer->Add($self->{btn_load_lambda_modifier}, 0, wxEXPAND | wxBOTTOM, 5);
  65. $buttons_sizer->Add($self->{btn_delete}, 0, wxEXPAND | wxRIGHT, 5);
  66. $buttons_sizer->Add($self->{btn_split}, 0, wxEXPAND | wxRIGHT, 5);
  67. {
  68. my $up_down_sizer = Wx::GridSizer->new(1, 2);
  69. $up_down_sizer->Add($self->{btn_move_up}, 0, wxEXPAND | wxRIGHT, 5);
  70. $up_down_sizer->Add($self->{btn_move_down}, 0, wxEXPAND, 5);
  71. $buttons_sizer->Add($up_down_sizer, 0, wxEXPAND, 5);
  72. }
  73. $self->{btn_load_part}->SetFont($Slic3r::GUI::small_font);
  74. $self->{btn_load_modifier}->SetFont($Slic3r::GUI::small_font);
  75. $self->{btn_load_lambda_modifier}->SetFont($Slic3r::GUI::small_font);
  76. $self->{btn_delete}->SetFont($Slic3r::GUI::small_font);
  77. $self->{btn_split}->SetFont($Slic3r::GUI::small_font);
  78. $self->{btn_move_up}->SetFont($Slic3r::GUI::small_font);
  79. $self->{btn_move_down}->SetFont($Slic3r::GUI::small_font);
  80. # part settings panel
  81. $self->{settings_panel} = Slic3r::GUI::Plater::OverrideSettingsPanel->new($self, on_change => sub { $self->{part_settings_changed} = 1; $self->_update_canvas; });
  82. my $settings_sizer = Wx::StaticBoxSizer->new($self->{staticbox} = Wx::StaticBox->new($self, -1, "Part Settings"), wxVERTICAL);
  83. $settings_sizer->Add($self->{settings_panel}, 1, wxEXPAND | wxALL, 0);
  84. my $optgroup_movers;
  85. $optgroup_movers = $self->{optgroup_movers} = Slic3r::GUI::OptionsGroup->new(
  86. parent => $self,
  87. title => 'Move',
  88. on_change => sub {
  89. my ($opt_id) = @_;
  90. # There seems to be an issue with wxWidgets 3.0.2/3.0.3, where the slider
  91. # genates tens of events for a single value change.
  92. # Only trigger the recalculation if the value changes
  93. # or a live preview was activated and the mesh cut is not valid yet.
  94. if ($self->{move_options}{$opt_id} != $optgroup_movers->get_value($opt_id)) {
  95. $self->{move_options}{$opt_id} = $optgroup_movers->get_value($opt_id);
  96. wxTheApp->CallAfter(sub {
  97. $self->_update;
  98. });
  99. }
  100. },
  101. label_width => 20,
  102. );
  103. $optgroup_movers->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  104. opt_id => 'x',
  105. type => 'slider',
  106. label => 'X',
  107. default => 0,
  108. min => -($self->{model_object}->bounding_box->size->x)*4,
  109. max => $self->{model_object}->bounding_box->size->x*4,
  110. full_width => 1,
  111. ));
  112. $optgroup_movers->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  113. opt_id => 'y',
  114. type => 'slider',
  115. label => 'Y',
  116. default => 0,
  117. min => -($self->{model_object}->bounding_box->size->y)*4,
  118. max => $self->{model_object}->bounding_box->size->y*4,
  119. full_width => 1,
  120. ));
  121. $optgroup_movers->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  122. opt_id => 'z',
  123. type => 'slider',
  124. label => 'Z',
  125. default => 0,
  126. min => -($self->{model_object}->bounding_box->size->z)*4,
  127. max => $self->{model_object}->bounding_box->size->z*4,
  128. full_width => 1,
  129. ));
  130. # left pane with tree
  131. my $left_sizer = Wx::BoxSizer->new(wxVERTICAL);
  132. $left_sizer->Add($tree, 3, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10);
  133. $left_sizer->Add($buttons_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10);
  134. $left_sizer->Add($settings_sizer, 5, wxEXPAND | wxALL, 0);
  135. $left_sizer->Add($optgroup_movers->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  136. # right pane with preview canvas
  137. my $canvas;
  138. if ($Slic3r::GUI::have_OpenGL) {
  139. $canvas = $self->{canvas} = Slic3r::GUI::3DScene->new($self);
  140. Slic3r::GUI::_3DScene::enable_picking($canvas, 1);
  141. Slic3r::GUI::_3DScene::set_select_by($canvas, 'volume');
  142. Slic3r::GUI::_3DScene::register_on_select_object_callback($canvas, sub {
  143. my ($volume_idx) = @_;
  144. $self->reload_tree($volume_idx);
  145. });
  146. Slic3r::GUI::_3DScene::load_model_object($canvas, $self->{model_object}, 0, [0]);
  147. Slic3r::GUI::_3DScene::set_auto_bed_shape($canvas);
  148. Slic3r::GUI::_3DScene::set_axes_length($canvas, 2.0 * max(@{ Slic3r::GUI::_3DScene::get_volumes_bounding_box($canvas)->size }));
  149. $canvas->SetSize([500,700]);
  150. Slic3r::GUI::_3DScene::set_config($canvas, $self->GetParent->GetParent->GetParent->{config});
  151. Slic3r::GUI::_3DScene::update_volumes_colors_by_extruder($canvas);
  152. Slic3r::GUI::_3DScene::enable_force_zoom_to_bed($canvas, 1);
  153. }
  154. $self->{sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
  155. $self->{sizer}->Add($left_sizer, 0, wxEXPAND | wxALL, 0);
  156. $self->{sizer}->Add($canvas, 1, wxEXPAND | wxALL, 0) if $canvas;
  157. $self->SetSizer($self->{sizer});
  158. $self->{sizer}->SetSizeHints($self);
  159. # attach events
  160. EVT_TREE_ITEM_COLLAPSING($self, $tree, sub {
  161. my ($self, $event) = @_;
  162. $event->Veto;
  163. });
  164. EVT_TREE_SEL_CHANGED($self, $tree, sub {
  165. my ($self, $event) = @_;
  166. return if $self->{disable_tree_sel_changed_event};
  167. $self->selection_changed;
  168. });
  169. EVT_TREE_KEY_DOWN($self, $tree, \&on_tree_key_down);
  170. EVT_BUTTON($self, $self->{btn_load_part}, sub { $self->on_btn_load(0) });
  171. EVT_BUTTON($self, $self->{btn_load_modifier}, sub { $self->on_btn_load(1) });
  172. EVT_BUTTON($self, $self->{btn_load_lambda_modifier}, sub { $self->on_btn_lambda(1) });
  173. EVT_BUTTON($self, $self->{btn_delete}, \&on_btn_delete);
  174. EVT_BUTTON($self, $self->{btn_split}, \&on_btn_split);
  175. EVT_BUTTON($self, $self->{btn_move_up}, \&on_btn_move_up);
  176. EVT_BUTTON($self, $self->{btn_move_down}, \&on_btn_move_down);
  177. EVT_KEY_DOWN($canvas, sub {
  178. my ($canvas, $event) = @_;
  179. if ($event->GetKeyCode == WXK_DELETE) {
  180. $canvas->GetParent->on_btn_delete;
  181. } else {
  182. $event->Skip;
  183. }
  184. });
  185. $self->reload_tree;
  186. return $self;
  187. }
  188. sub reload_tree {
  189. my ($self, $selected_volume_idx) = @_;
  190. $selected_volume_idx //= -1;
  191. my $object = $self->{model_object};
  192. my $tree = $self->{tree};
  193. my $rootId = $tree->GetRootItem;
  194. # despite wxWidgets states that DeleteChildren "will not generate any events unlike Delete() method",
  195. # the MSW implementation of DeleteChildren actually calls Delete() for each item, so
  196. # EVT_TREE_SEL_CHANGED is being called, with bad effects (the event handler is called; this
  197. # subroutine is never continued; an invisible EndModal is called on the dialog causing Plater
  198. # to continue its logic and rescheduling the background process etc. GH #2774)
  199. $self->{disable_tree_sel_changed_event} = 1;
  200. $tree->DeleteChildren($rootId);
  201. $self->{disable_tree_sel_changed_event} = 0;
  202. my $selectedId = $rootId;
  203. foreach my $volume_id (0..$#{$object->volumes}) {
  204. my $volume = $object->volumes->[$volume_id];
  205. my $icon = $volume->modifier ? ICON_MODIFIERMESH : ICON_SOLIDMESH;
  206. my $itemId = $tree->AppendItem($rootId, $volume->name || $volume_id, $icon);
  207. if ($volume_id == $selected_volume_idx) {
  208. $selectedId = $itemId;
  209. }
  210. $tree->SetPlData($itemId, {
  211. type => 'volume',
  212. volume_id => $volume_id,
  213. });
  214. }
  215. $tree->ExpandAll;
  216. Slic3r::GUI->CallAfter(sub {
  217. $self->{tree}->SelectItem($selectedId);
  218. # SelectItem() should trigger EVT_TREE_SEL_CHANGED as per wxWidgets docs,
  219. # but in fact it doesn't if the given item is already selected (this happens
  220. # on first load)
  221. $self->selection_changed;
  222. });
  223. }
  224. sub get_selection {
  225. my ($self) = @_;
  226. my $nodeId = $self->{tree}->GetSelection;
  227. if ($nodeId->IsOk) {
  228. return $self->{tree}->GetPlData($nodeId);
  229. }
  230. return undef;
  231. }
  232. sub selection_changed {
  233. my ($self) = @_;
  234. # deselect all meshes
  235. if ($self->{canvas}) {
  236. Slic3r::GUI::_3DScene::deselect_volumes($self->{canvas});
  237. }
  238. # disable things as if nothing is selected
  239. $self->{'btn_' . $_}->Disable for (qw(delete move_up move_down split));
  240. $self->{settings_panel}->disable;
  241. $self->{settings_panel}->set_config(undef);
  242. # reset move sliders
  243. $self->{optgroup_movers}->set_value("x", 0);
  244. $self->{optgroup_movers}->set_value("y", 0);
  245. $self->{optgroup_movers}->set_value("z", 0);
  246. $self->{move_options} = {
  247. x => 0,
  248. y => 0,
  249. z => 0,
  250. };
  251. $self->{last_coords} = {
  252. x => 0,
  253. y => 0,
  254. z => 0,
  255. };
  256. if (my $itemData = $self->get_selection) {
  257. my ($config, @opt_keys);
  258. if ($itemData->{type} eq 'volume') {
  259. # select volume in 3D preview
  260. if ($self->{canvas}) {
  261. Slic3r::GUI::_3DScene::select_volume($self->{canvas}, $itemData->{volume_id});
  262. }
  263. $self->{btn_delete}->Enable;
  264. $self->{btn_split}->Enable;
  265. $self->{btn_move_up}->Enable if $itemData->{volume_id} > 0;
  266. $self->{btn_move_down}->Enable if $itemData->{volume_id} + 1 < $self->{model_object}->volumes_count;
  267. # attach volume config to settings panel
  268. my $volume = $self->{model_object}->volumes->[ $itemData->{volume_id} ];
  269. if ($volume->modifier) {
  270. $self->{optgroup_movers}->enable;
  271. } else {
  272. $self->{optgroup_movers}->disable;
  273. }
  274. $config = $volume->config;
  275. $self->{staticbox}->SetLabel('Part Settings');
  276. # get default values
  277. @opt_keys = @{Slic3r::Config::PrintRegion->new->get_keys};
  278. } elsif ($itemData->{type} eq 'object') {
  279. # select nothing in 3D preview
  280. # attach object config to settings panel
  281. $self->{optgroup_movers}->disable;
  282. $self->{staticbox}->SetLabel('Object Settings');
  283. @opt_keys = (map @{$_->get_keys}, Slic3r::Config::PrintObject->new, Slic3r::Config::PrintRegion->new);
  284. $config = $self->{model_object}->config;
  285. }
  286. # get default values
  287. my $default_config = Slic3r::Config::new_from_defaults_keys(\@opt_keys);
  288. # decide which settings will be shown by default
  289. if ($itemData->{type} eq 'object') {
  290. $config->set_ifndef('wipe_into_objects', 0);
  291. $config->set_ifndef('wipe_into_infill', 0);
  292. }
  293. # append default extruder
  294. push @opt_keys, 'extruder';
  295. $default_config->set('extruder', 0);
  296. $config->set_ifndef('extruder', 0);
  297. $self->{settings_panel}->set_default_config($default_config);
  298. $self->{settings_panel}->set_config($config);
  299. $self->{settings_panel}->set_opt_keys(\@opt_keys);
  300. # disable minus icon to remove the settings
  301. if ($itemData->{type} eq 'object') {
  302. $self->{settings_panel}->set_fixed_options([qw(extruder), qw(wipe_into_infill), qw(wipe_into_objects)]);
  303. } else {
  304. $self->{settings_panel}->set_fixed_options([qw(extruder)]);
  305. }
  306. $self->{settings_panel}->enable;
  307. }
  308. Slic3r::GUI::_3DScene::render($self->{canvas}) if $self->{canvas};
  309. }
  310. sub on_btn_load {
  311. my ($self, $is_modifier) = @_;
  312. my @input_files = wxTheApp->open_model($self);
  313. foreach my $input_file (@input_files) {
  314. my $model = eval { Slic3r::Model->read_from_file($input_file) };
  315. if ($@) {
  316. Slic3r::GUI::show_error($self, $@);
  317. next;
  318. }
  319. foreach my $object (@{$model->objects}) {
  320. foreach my $volume (@{$object->volumes}) {
  321. my $new_volume = $self->{model_object}->add_volume($volume);
  322. $new_volume->set_modifier($is_modifier);
  323. $new_volume->set_name(basename($input_file));
  324. # apply the same translation we applied to the object
  325. $new_volume->mesh->translate(@{$self->{model_object}->origin_translation});
  326. # set a default extruder value, since user can't add it manually
  327. $new_volume->config->set_ifndef('extruder', 0);
  328. $self->{parts_changed} = 1;
  329. }
  330. }
  331. }
  332. $self->{model_object}->center_around_origin if $self->{parts_changed};
  333. $self->_parts_changed;
  334. }
  335. sub on_btn_lambda {
  336. my ($self, $is_modifier) = @_;
  337. my $dlg = Slic3r::GUI::Plater::LambdaObjectDialog->new($self);
  338. if ($dlg->ShowModal() == wxID_CANCEL) {
  339. return;
  340. }
  341. my $params = $dlg->ObjectParameter;
  342. my $type = "".$params->{"type"};
  343. my $name = "lambda-".$params->{"type"};
  344. my $mesh;
  345. if ($type eq "box") {
  346. $mesh = Slic3r::TriangleMesh::cube($params->{"dim"}[0], $params->{"dim"}[1], $params->{"dim"}[2]);
  347. } elsif ($type eq "cylinder") {
  348. $mesh = Slic3r::TriangleMesh::cylinder($params->{"cyl_r"}, $params->{"cyl_h"});
  349. } elsif ($type eq "sphere") {
  350. $mesh = Slic3r::TriangleMesh::sphere($params->{"sph_rho"});
  351. } elsif ($type eq "slab") {
  352. $mesh = Slic3r::TriangleMesh::cube($self->{model_object}->bounding_box->size->x*1.5, $self->{model_object}->bounding_box->size->y*1.5, $params->{"slab_h"});
  353. # box sets the base coordinate at 0,0, move to center of plate and move it up to initial_z
  354. $mesh->translate(-$self->{model_object}->bounding_box->size->x*1.5/2.0, -$self->{model_object}->bounding_box->size->y*1.5/2.0, $params->{"slab_z"});
  355. } else {
  356. return;
  357. }
  358. $mesh->repair;
  359. my $new_volume = $self->{model_object}->add_volume(mesh => $mesh);
  360. $new_volume->set_modifier($is_modifier);
  361. $new_volume->set_name($name);
  362. # set a default extruder value, since user can't add it manually
  363. $new_volume->config->set_ifndef('extruder', 0);
  364. $self->{parts_changed} = 1;
  365. $self->_parts_changed;
  366. }
  367. sub on_tree_key_down {
  368. my ($self, $event) = @_;
  369. my $keycode = $event->GetKeyCode;
  370. # Wx >= 0.9911
  371. if (defined(&Wx::TreeEvent::GetKeyEvent)) {
  372. if ($event->GetKeyEvent->GetModifiers & wxMOD_CONTROL) {
  373. if ($keycode == WXK_UP) {
  374. $event->Skip;
  375. $self->on_btn_move_up;
  376. } elsif ($keycode == WXK_DOWN) {
  377. $event->Skip;
  378. $self->on_btn_move_down;
  379. }
  380. } elsif ($keycode == WXK_DELETE) {
  381. $self->on_btn_delete;
  382. }
  383. }
  384. }
  385. sub on_btn_move_up {
  386. my ($self) = @_;
  387. my $itemData = $self->get_selection;
  388. if ($itemData && $itemData->{type} eq 'volume') {
  389. my $volume_id = $itemData->{volume_id};
  390. if ($self->{model_object}->move_volume_up($volume_id)) {
  391. Slic3r::GUI::_3DScene::move_volume_up($self->{canvas}, $volume_id);
  392. $self->{parts_changed} = 1;
  393. $self->reload_tree($volume_id - 1);
  394. }
  395. }
  396. }
  397. sub on_btn_move_down {
  398. my ($self) = @_;
  399. my $itemData = $self->get_selection;
  400. if ($itemData && $itemData->{type} eq 'volume') {
  401. my $volume_id = $itemData->{volume_id};
  402. if ($self->{model_object}->move_volume_down($volume_id)) {
  403. Slic3r::GUI::_3DScene::move_volume_down($self->{canvas}, $volume_id);
  404. $self->{parts_changed} = 1;
  405. $self->reload_tree($volume_id + 1);
  406. }
  407. }
  408. }
  409. sub on_btn_delete {
  410. my ($self) = @_;
  411. my $itemData = $self->get_selection;
  412. if ($itemData && $itemData->{type} eq 'volume') {
  413. my $volume = $self->{model_object}->volumes->[$itemData->{volume_id}];
  414. # if user is deleting the last solid part, throw error
  415. if (!$volume->modifier && scalar(grep !$_->modifier, @{$self->{model_object}->volumes}) == 1) {
  416. Slic3r::GUI::show_error($self, "You can't delete the last solid part from this object.");
  417. return;
  418. }
  419. $self->{model_object}->delete_volume($itemData->{volume_id});
  420. $self->{parts_changed} = 1;
  421. }
  422. $self->{model_object}->center_around_origin if $self->{parts_changed};
  423. $self->_parts_changed;
  424. }
  425. sub on_btn_split {
  426. my ($self) = @_;
  427. my $itemData = $self->get_selection;
  428. if ($itemData && $itemData->{type} eq 'volume') {
  429. my $volume = $self->{model_object}->volumes->[$itemData->{volume_id}];
  430. my $nozzle_dmrs = $self->GetParent->GetParent->GetParent->{config}->get('nozzle_diameter');
  431. $self->{parts_changed} = 1 if $volume->split(scalar(@$nozzle_dmrs)) > 1;
  432. }
  433. $self->_parts_changed;
  434. }
  435. sub _parts_changed {
  436. my ($self) = @_;
  437. $self->reload_tree;
  438. if ($self->{canvas}) {
  439. Slic3r::GUI::_3DScene::reset_volumes($self->{canvas});
  440. Slic3r::GUI::_3DScene::load_model_object($self->{canvas}, $self->{model_object}, 0, [0]);
  441. Slic3r::GUI::_3DScene::zoom_to_volumes($self->{canvas});
  442. Slic3r::GUI::_3DScene::update_volumes_colors_by_extruder($self->{canvas});
  443. Slic3r::GUI::_3DScene::render($self->{canvas});
  444. }
  445. }
  446. sub CanClose {
  447. my $self = shift;
  448. return 1; # skip validation for now
  449. # validate options before allowing user to dismiss the dialog
  450. # the validate method only works on full configs so we have
  451. # to merge our settings with the default ones
  452. my $config = $self->GetParent->GetParent->GetParent->GetParent->GetParent->config->clone;
  453. eval {
  454. $config->apply($self->model_object->config);
  455. $config->validate;
  456. };
  457. return ! Slic3r::GUI::catch_error($self);
  458. }
  459. sub Destroy {
  460. my ($self) = @_;
  461. $self->{canvas}->Destroy if ($self->{canvas});
  462. }
  463. sub PartsChanged {
  464. my ($self) = @_;
  465. return $self->{parts_changed};
  466. }
  467. sub PartSettingsChanged {
  468. my ($self) = @_;
  469. return $self->{part_settings_changed};
  470. }
  471. sub _update_canvas {
  472. my ($self) = @_;
  473. if ($self->{canvas}) {
  474. Slic3r::GUI::_3DScene::reset_volumes($self->{canvas});
  475. Slic3r::GUI::_3DScene::load_model_object($self->{canvas}, $self->{model_object}, 0, [0]);
  476. # restore selection, if any
  477. if (my $itemData = $self->get_selection) {
  478. if ($itemData->{type} eq 'volume') {
  479. Slic3r::GUI::_3DScene::select_volume($self->{canvas}, $itemData->{volume_id});
  480. }
  481. }
  482. Slic3r::GUI::_3DScene::update_volumes_colors_by_extruder($self->{canvas});
  483. Slic3r::GUI::_3DScene::render($self->{canvas});
  484. }
  485. }
  486. sub _update {
  487. my ($self) = @_;
  488. my ($m_x, $m_y, $m_z) = ($self->{move_options}{x}, $self->{move_options}{y}, $self->{move_options}{z});
  489. my ($l_x, $l_y, $l_z) = ($self->{last_coords}{x}, $self->{last_coords}{y}, $self->{last_coords}{z});
  490. my $itemData = $self->get_selection;
  491. if ($itemData && $itemData->{type} eq 'volume') {
  492. my $d = Slic3r::Pointf3->new($m_x - $l_x, $m_y - $l_y, $m_z - $l_z);
  493. my $volume = $self->{model_object}->volumes->[$itemData->{volume_id}];
  494. $volume->mesh->translate(@{$d});
  495. $self->{last_coords}{x} = $m_x;
  496. $self->{last_coords}{y} = $m_y;
  497. $self->{last_coords}{z} = $m_z;
  498. }
  499. $self->{parts_changed} = 1;
  500. my @objects = ();
  501. push @objects, $self->{model_object};
  502. Slic3r::GUI::_3DScene::reset_volumes($self->{canvas});
  503. Slic3r::GUI::_3DScene::load_model_object($self->{canvas}, $_, 0, [0]) for @objects;
  504. Slic3r::GUI::_3DScene::update_volumes_colors_by_extruder($self->{canvas});
  505. Slic3r::GUI::_3DScene::render($self->{canvas});
  506. }
  507. 1;