Plater.pm 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  1. # The "Plater" tab. It contains the "3D", "2D", "Preview" and "Layers" subtabs.
  2. package Slic3r::GUI::Plater;
  3. use strict;
  4. use warnings;
  5. use utf8;
  6. use File::Basename qw(basename dirname);
  7. use List::Util qw(sum first max);
  8. use Slic3r::Geometry qw(X Y Z scale unscale deg2rad rad2deg);
  9. use threads::shared qw(shared_clone);
  10. use Wx qw(:button :colour :cursor :dialog :filedialog :keycode :icon :font :id :listctrl :misc
  11. :panel :sizer :toolbar :window wxTheApp :notebook :combobox wxNullBitmap);
  12. use Wx::Event qw(EVT_BUTTON EVT_TOGGLEBUTTON EVT_COMMAND EVT_KEY_DOWN EVT_LIST_ITEM_ACTIVATED
  13. EVT_LIST_ITEM_DESELECTED EVT_LIST_ITEM_SELECTED EVT_LEFT_DOWN EVT_MOUSE_EVENTS EVT_PAINT EVT_TOOL
  14. EVT_CHOICE EVT_COMBOBOX EVT_TIMER EVT_NOTEBOOK_PAGE_CHANGED);
  15. use Slic3r::Geometry qw(PI);
  16. use base 'Wx::Panel';
  17. use constant TB_ADD => &Wx::NewId;
  18. use constant TB_REMOVE => &Wx::NewId;
  19. use constant TB_RESET => &Wx::NewId;
  20. use constant TB_ARRANGE => &Wx::NewId;
  21. use constant TB_EXPORT_GCODE => &Wx::NewId;
  22. use constant TB_EXPORT_STL => &Wx::NewId;
  23. use constant TB_MORE => &Wx::NewId;
  24. use constant TB_FEWER => &Wx::NewId;
  25. use constant TB_45CW => &Wx::NewId;
  26. use constant TB_45CCW => &Wx::NewId;
  27. use constant TB_SCALE => &Wx::NewId;
  28. use constant TB_SPLIT => &Wx::NewId;
  29. use constant TB_CUT => &Wx::NewId;
  30. use constant TB_SETTINGS => &Wx::NewId;
  31. use constant TB_LAYER_EDITING => &Wx::NewId;
  32. use Wx::Locale gettext => 'L';
  33. # package variables to avoid passing lexicals to threads
  34. our $PROGRESS_BAR_EVENT : shared = Wx::NewEventType;
  35. our $ERROR_EVENT : shared = Wx::NewEventType;
  36. # Emitted from the worker thread when the G-code export is finished.
  37. our $EXPORT_COMPLETED_EVENT : shared = Wx::NewEventType;
  38. our $PROCESS_COMPLETED_EVENT : shared = Wx::NewEventType;
  39. use constant FILAMENT_CHOOSERS_SPACING => 0;
  40. use constant PROCESS_DELAY => 0.5 * 1000; # milliseconds
  41. my $PreventListEvents = 0;
  42. our $appController;
  43. sub new {
  44. my ($class, $parent) = @_;
  45. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
  46. $self->{config} = Slic3r::Config::new_from_defaults_keys([qw(
  47. bed_shape complete_objects extruder_clearance_radius skirts skirt_distance brim_width variable_layer_height
  48. serial_port serial_speed host_type print_host printhost_apikey printhost_cafile
  49. nozzle_diameter single_extruder_multi_material wipe_tower wipe_tower_x wipe_tower_y wipe_tower_width
  50. wipe_tower_rotation_angle extruder_colour filament_colour max_print_height printer_model
  51. )]);
  52. # C++ Slic3r::Model with Perl extensions in Slic3r/Model.pm
  53. $self->{model} = Slic3r::Model->new;
  54. # C++ Slic3r::Print with Perl extensions in Slic3r/Print.pm
  55. $self->{print} = Slic3r::Print->new;
  56. # List of Perl objects Slic3r::GUI::Plater::Object, representing a 2D preview of the platter.
  57. $self->{objects} = [];
  58. $self->{gcode_preview_data} = Slic3r::GCode::PreviewData->new;
  59. $self->{print}->set_status_cb(sub {
  60. my ($percent, $message) = @_;
  61. my $event = Wx::CommandEvent->new($PROGRESS_BAR_EVENT);
  62. $event->SetString($message);
  63. $event->SetInt($percent);
  64. Wx::PostEvent($self, $event);
  65. });
  66. # Initialize preview notebook
  67. $self->{preview_notebook} = Wx::Notebook->new($self, -1, wxDefaultPosition, [335,335], wxNB_BOTTOM);
  68. # Initialize handlers for canvases
  69. my $on_select_object = sub {
  70. my ($obj_idx) = @_;
  71. # Ignore the special objects (the wipe tower proxy and such).
  72. $self->select_object((defined($obj_idx) && $obj_idx >= 0 && $obj_idx < 1000) ? $obj_idx : undef);
  73. };
  74. my $on_double_click = sub {
  75. $self->object_settings_dialog if $self->selected_object;
  76. };
  77. my $on_right_click = sub {
  78. my ($canvas, $click_pos_x, $click_pos_y) = @_;
  79. my ($obj_idx, $object) = $self->selected_object;
  80. return if !defined $obj_idx;
  81. my $menu = $self->object_menu;
  82. $canvas->PopupMenu($menu, $click_pos_x, $click_pos_y);
  83. $menu->Destroy;
  84. };
  85. my $on_instances_moved = sub {
  86. $self->update;
  87. };
  88. # callback to enable/disable action buttons
  89. my $enable_action_buttons = sub {
  90. my ($enable) = @_;
  91. $self->{btn_export_gcode}->Enable($enable);
  92. $self->{btn_reslice}->Enable($enable);
  93. $self->{btn_print}->Enable($enable);
  94. $self->{btn_send_gcode}->Enable($enable);
  95. };
  96. # callback to react to gizmo scale
  97. my $on_gizmo_scale_uniformly = sub {
  98. my ($scale) = @_;
  99. my ($obj_idx, $object) = $self->selected_object;
  100. return if !defined $obj_idx;
  101. my $model_object = $self->{model}->objects->[$obj_idx];
  102. my $model_instance = $model_object->instances->[0];
  103. $self->stop_background_process;
  104. my $variation = $scale / $model_instance->scaling_factor;
  105. #FIXME Scale the layer height profile?
  106. foreach my $range (@{ $model_object->layer_height_ranges }) {
  107. $range->[0] *= $variation;
  108. $range->[1] *= $variation;
  109. }
  110. $_->set_scaling_factor($scale) for @{ $model_object->instances };
  111. $self->{list}->SetItem($obj_idx, 2, ($model_object->instances->[0]->scaling_factor * 100) . "%");
  112. # $object->transform_thumbnail($self->{model}, $obj_idx);
  113. #update print and start background processing
  114. $self->{print}->add_model_object($model_object, $obj_idx);
  115. $self->selection_changed(1); # refresh info (size, volume etc.)
  116. $self->update;
  117. $self->schedule_background_process;
  118. };
  119. # callback to react to gizmo rotate
  120. # omitting last three parameters means rotation around Z
  121. # otherwise they are the components of the rotation axis vector
  122. my $on_gizmo_rotate = sub {
  123. my ($angle, $axis_x, $axis_y, $axis_z) = @_;
  124. if (!defined $axis_x) {
  125. $self->rotate(rad2deg($angle), Z, 'absolute');
  126. }
  127. else {
  128. $self->rotate(rad2deg($angle), undef, 'absolute', $axis_x, $axis_y, $axis_z) if $angle != 0;
  129. }
  130. };
  131. # callback to update object's geometry info while using gizmos
  132. my $on_update_geometry_info = sub {
  133. my ($size_x, $size_y, $size_z, $scale_factor) = @_;
  134. my ($obj_idx, $object) = $self->selected_object;
  135. if ((defined $obj_idx) && ($self->{object_info_size})) { # have we already loaded the info pane?
  136. $self->{object_info_size}->SetLabel(sprintf("%.2f x %.2f x %.2f", $size_x, $size_y, $size_z));
  137. my $model_object = $self->{model}->objects->[$obj_idx];
  138. if (my $stats = $model_object->mesh_stats) {
  139. $self->{object_info_volume}->SetLabel(sprintf('%.2f', $stats->{volume} * $scale_factor**3));
  140. }
  141. }
  142. };
  143. # Initialize 3D plater
  144. if ($Slic3r::GUI::have_OpenGL) {
  145. $self->{canvas3D} = Slic3r::GUI::Plater::3D->new($self->{preview_notebook}, $self->{objects}, $self->{model}, $self->{print}, $self->{config});
  146. $self->{preview_notebook}->AddPage($self->{canvas3D}, L('3D'));
  147. Slic3r::GUI::_3DScene::register_on_select_object_callback($self->{canvas3D}, $on_select_object);
  148. Slic3r::GUI::_3DScene::register_on_double_click_callback($self->{canvas3D}, $on_double_click);
  149. Slic3r::GUI::_3DScene::register_on_right_click_callback($self->{canvas3D}, sub { $on_right_click->($self->{canvas3D}, @_); });
  150. Slic3r::GUI::_3DScene::register_on_arrange_callback($self->{canvas3D}, sub { $self->arrange });
  151. Slic3r::GUI::_3DScene::register_on_rotate_object_left_callback($self->{canvas3D}, sub { $self->rotate(-45, Z, 'relative') });
  152. Slic3r::GUI::_3DScene::register_on_rotate_object_right_callback($self->{canvas3D}, sub { $self->rotate( 45, Z, 'relative') });
  153. Slic3r::GUI::_3DScene::register_on_scale_object_uniformly_callback($self->{canvas3D}, sub { $self->changescale(undef) });
  154. Slic3r::GUI::_3DScene::register_on_increase_objects_callback($self->{canvas3D}, sub { $self->increase() });
  155. Slic3r::GUI::_3DScene::register_on_decrease_objects_callback($self->{canvas3D}, sub { $self->decrease() });
  156. Slic3r::GUI::_3DScene::register_on_remove_object_callback($self->{canvas3D}, sub { $self->remove() });
  157. Slic3r::GUI::_3DScene::register_on_instance_moved_callback($self->{canvas3D}, $on_instances_moved);
  158. Slic3r::GUI::_3DScene::register_on_enable_action_buttons_callback($self->{canvas3D}, $enable_action_buttons);
  159. Slic3r::GUI::_3DScene::register_on_gizmo_scale_uniformly_callback($self->{canvas3D}, $on_gizmo_scale_uniformly);
  160. Slic3r::GUI::_3DScene::register_on_gizmo_rotate_callback($self->{canvas3D}, $on_gizmo_rotate);
  161. Slic3r::GUI::_3DScene::register_on_update_geometry_info_callback($self->{canvas3D}, $on_update_geometry_info);
  162. Slic3r::GUI::_3DScene::enable_gizmos($self->{canvas3D}, 1);
  163. Slic3r::GUI::_3DScene::enable_shader($self->{canvas3D}, 1);
  164. Slic3r::GUI::_3DScene::enable_force_zoom_to_bed($self->{canvas3D}, 1);
  165. Slic3r::GUI::_3DScene::register_on_wipe_tower_moved_callback($self->{canvas3D}, sub {
  166. my ($x, $y) = @_;
  167. my $cfg = Slic3r::Config->new;
  168. $cfg->set('wipe_tower_x', $x);
  169. $cfg->set('wipe_tower_y', $y);
  170. $self->GetFrame->{options_tabs}{print}->load_config($cfg);
  171. });
  172. Slic3r::GUI::_3DScene::register_on_model_update_callback($self->{canvas3D}, sub {
  173. if (wxTheApp->{app_config}->get("background_processing")) {
  174. $self->schedule_background_process;
  175. } else {
  176. # Hide the print info box, it is no more valid.
  177. $self->print_info_box_show(0);
  178. }
  179. });
  180. Slic3r::GUI::_3DScene::register_on_viewport_changed_callback($self->{canvas3D}, sub { Slic3r::GUI::_3DScene::set_viewport_from_scene($self->{preview3D}->canvas, $self->{canvas3D}); });
  181. }
  182. Slic3r::GUI::register_on_request_update_callback(sub { $self->schedule_background_process; });
  183. # # Initialize 2D preview canvas
  184. # $self->{canvas} = Slic3r::GUI::Plater::2D->new($self->{preview_notebook}, wxDefaultSize, $self->{objects}, $self->{model}, $self->{config});
  185. # $self->{preview_notebook}->AddPage($self->{canvas}, L('2D'));
  186. # $self->{canvas}->on_select_object($on_select_object);
  187. # $self->{canvas}->on_double_click($on_double_click);
  188. # $self->{canvas}->on_right_click(sub { $on_right_click->($self->{canvas}, @_); });
  189. # $self->{canvas}->on_instances_moved($on_instances_moved);
  190. # Initialize 3D toolpaths preview
  191. if ($Slic3r::GUI::have_OpenGL) {
  192. $self->{preview3D} = Slic3r::GUI::Plater::3DPreview->new($self->{preview_notebook}, $self->{print}, $self->{gcode_preview_data}, $self->{config});
  193. Slic3r::GUI::_3DScene::enable_legend_texture($self->{preview3D}->canvas, 1);
  194. Slic3r::GUI::_3DScene::enable_dynamic_background($self->{preview3D}->canvas, 1);
  195. Slic3r::GUI::_3DScene::register_on_viewport_changed_callback($self->{preview3D}->canvas, sub { Slic3r::GUI::_3DScene::set_viewport_from_scene($self->{canvas3D}, $self->{preview3D}->canvas); });
  196. $self->{preview_notebook}->AddPage($self->{preview3D}, L('Preview'));
  197. $self->{preview3D_page_idx} = $self->{preview_notebook}->GetPageCount-1;
  198. }
  199. # Initialize toolpaths preview
  200. if ($Slic3r::GUI::have_OpenGL) {
  201. $self->{toolpaths2D} = Slic3r::GUI::Plater::2DToolpaths->new($self->{preview_notebook}, $self->{print});
  202. $self->{preview_notebook}->AddPage($self->{toolpaths2D}, L('Layers'));
  203. }
  204. EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{preview_notebook}, sub {
  205. my $preview = $self->{preview_notebook}->GetCurrentPage;
  206. if (($preview != $self->{preview3D}) && ($preview != $self->{canvas3D})) {
  207. $preview->OnActivate if $preview->can('OnActivate');
  208. } elsif ($preview == $self->{preview3D}) {
  209. $self->{preview3D}->reload_print;
  210. # sets the canvas as dirty to force a render at the 1st idle event (wxWidgets IsShownOnScreen() is buggy and cannot be used reliably)
  211. Slic3r::GUI::_3DScene::set_as_dirty($self->{preview3D}->canvas);
  212. } elsif ($preview == $self->{canvas3D}) {
  213. if (Slic3r::GUI::_3DScene::is_reload_delayed($self->{canvas3D})) {
  214. my $selections = $self->collect_selections;
  215. Slic3r::GUI::_3DScene::set_objects_selections($self->{canvas3D}, \@$selections);
  216. Slic3r::GUI::_3DScene::reload_scene($self->{canvas3D}, 1);
  217. }
  218. # sets the canvas as dirty to force a render at the 1st idle event (wxWidgets IsShownOnScreen() is buggy and cannot be used reliably)
  219. Slic3r::GUI::_3DScene::set_as_dirty($self->{canvas3D});
  220. }
  221. });
  222. # toolbar for object manipulation
  223. if (!&Wx::wxMSW) {
  224. Wx::ToolTip::Enable(1);
  225. $self->{htoolbar} = Wx::ToolBar->new($self, -1, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL | wxTB_TEXT | wxBORDER_SIMPLE | wxTAB_TRAVERSAL);
  226. $self->{htoolbar}->AddTool(TB_ADD, L("Add…"), Wx::Bitmap->new(Slic3r::var("brick_add.png"), wxBITMAP_TYPE_PNG), '');
  227. $self->{htoolbar}->AddTool(TB_REMOVE, L("Delete"), Wx::Bitmap->new(Slic3r::var("brick_delete.png"), wxBITMAP_TYPE_PNG), '');
  228. $self->{htoolbar}->AddTool(TB_RESET, L("Delete All"), Wx::Bitmap->new(Slic3r::var("cross.png"), wxBITMAP_TYPE_PNG), '');
  229. $self->{htoolbar}->AddTool(TB_ARRANGE, L("Arrange"), Wx::Bitmap->new(Slic3r::var("bricks.png"), wxBITMAP_TYPE_PNG), '');
  230. $self->{htoolbar}->AddSeparator;
  231. $self->{htoolbar}->AddTool(TB_MORE, L("More"), Wx::Bitmap->new(Slic3r::var("add.png"), wxBITMAP_TYPE_PNG), '');
  232. $self->{htoolbar}->AddTool(TB_FEWER, L("Fewer"), Wx::Bitmap->new(Slic3r::var("delete.png"), wxBITMAP_TYPE_PNG), '');
  233. $self->{htoolbar}->AddSeparator;
  234. $self->{htoolbar}->AddTool(TB_45CCW, L("45° ccw"), Wx::Bitmap->new(Slic3r::var("arrow_rotate_anticlockwise.png"), wxBITMAP_TYPE_PNG), '');
  235. $self->{htoolbar}->AddTool(TB_45CW, L("45° cw"), Wx::Bitmap->new(Slic3r::var("arrow_rotate_clockwise.png"), wxBITMAP_TYPE_PNG), '');
  236. $self->{htoolbar}->AddTool(TB_SCALE, L("Scale…"), Wx::Bitmap->new(Slic3r::var("arrow_out.png"), wxBITMAP_TYPE_PNG), '');
  237. $self->{htoolbar}->AddTool(TB_SPLIT, L("Split"), Wx::Bitmap->new(Slic3r::var("shape_ungroup.png"), wxBITMAP_TYPE_PNG), '');
  238. $self->{htoolbar}->AddTool(TB_CUT, L("Cut…"), Wx::Bitmap->new(Slic3r::var("package.png"), wxBITMAP_TYPE_PNG), '');
  239. $self->{htoolbar}->AddSeparator;
  240. $self->{htoolbar}->AddTool(TB_SETTINGS, L("Settings…"), Wx::Bitmap->new(Slic3r::var("cog.png"), wxBITMAP_TYPE_PNG), '');
  241. $self->{htoolbar}->AddTool(TB_LAYER_EDITING, L('Layer Editing'), Wx::Bitmap->new(Slic3r::var("variable_layer_height.png"), wxBITMAP_TYPE_PNG), wxNullBitmap, 1, 0, 'Layer Editing');
  242. } else {
  243. my %tbar_buttons = (
  244. add => L("Add…"),
  245. remove => L("Delete"),
  246. reset => L("Delete All"),
  247. arrange => L("Arrange"),
  248. increase => "",
  249. decrease => "",
  250. rotate45ccw => "",
  251. rotate45cw => "",
  252. changescale => L("Scale…"),
  253. split => L("Split"),
  254. cut => L("Cut…"),
  255. settings => L("Settings…"),
  256. layer_editing => L("Layer editing"),
  257. );
  258. $self->{btoolbar} = Wx::BoxSizer->new(wxHORIZONTAL);
  259. for (qw(add remove reset arrange increase decrease rotate45ccw rotate45cw changescale split cut settings)) {
  260. $self->{"btn_$_"} = Wx::Button->new($self, -1, $tbar_buttons{$_}, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
  261. $self->{btoolbar}->Add($self->{"btn_$_"});
  262. }
  263. $self->{"btn_layer_editing"} = Wx::ToggleButton->new($self, -1, $tbar_buttons{'layer_editing'}, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
  264. $self->{btoolbar}->Add($self->{"btn_layer_editing"});
  265. }
  266. ### Panel for right column
  267. $self->{right_panel} = Wx::Panel->new($self, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
  268. ### Scrolled Window for info boxes
  269. my $scrolled_window_sizer = $self->{scrolled_window_sizer} = Wx::BoxSizer->new(wxVERTICAL);
  270. $scrolled_window_sizer->SetMinSize([310, -1]);
  271. my $scrolled_window_panel = $self->{scrolled_window_panel} = Wx::ScrolledWindow->new($self->{right_panel}, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
  272. $scrolled_window_panel->SetSizer($scrolled_window_sizer);
  273. $scrolled_window_panel->SetScrollbars(1, 1, 1, 1);
  274. $self->{list} = Wx::ListView->new($scrolled_window_panel, -1, wxDefaultPosition, wxDefaultSize,
  275. wxLC_SINGLE_SEL | wxLC_REPORT | wxBORDER_SUNKEN | wxTAB_TRAVERSAL | wxWANTS_CHARS );
  276. $self->{list}->InsertColumn(0, L("Name"), wxLIST_FORMAT_LEFT, 145);
  277. $self->{list}->InsertColumn(1, L("Copies"), wxLIST_FORMAT_CENTER, 45);
  278. $self->{list}->InsertColumn(2, L("Scale"), wxLIST_FORMAT_CENTER, wxLIST_AUTOSIZE_USEHEADER);
  279. EVT_LIST_ITEM_SELECTED($self, $self->{list}, \&list_item_selected);
  280. EVT_LIST_ITEM_DESELECTED($self, $self->{list}, \&list_item_deselected);
  281. EVT_LIST_ITEM_ACTIVATED($self, $self->{list}, \&list_item_activated);
  282. EVT_KEY_DOWN($self->{list}, sub {
  283. my ($list, $event) = @_;
  284. if ($event->GetKeyCode == WXK_TAB) {
  285. $list->Navigate($event->ShiftDown ? &Wx::wxNavigateBackward : &Wx::wxNavigateForward);
  286. } elsif ($event->GetKeyCode == WXK_DELETE ||
  287. ($event->GetKeyCode == WXK_BACK && &Wx::wxMAC) ) {
  288. $self->remove;
  289. } else {
  290. $event->Skip;
  291. }
  292. });
  293. # right pane buttons
  294. $self->{btn_export_gcode} = Wx::Button->new($self->{right_panel}, -1, L("Export G-code…"), wxDefaultPosition, [-1, 30], wxBU_LEFT);
  295. $self->{btn_reslice} = Wx::Button->new($self->{right_panel}, -1, L("Slice now"), wxDefaultPosition, [-1, 30], wxBU_LEFT);
  296. $self->{btn_print} = Wx::Button->new($self->{right_panel}, -1, L("Print…"), wxDefaultPosition, [-1, 30], wxBU_LEFT);
  297. $self->{btn_send_gcode} = Wx::Button->new($self->{right_panel}, -1, L("Send to printer"), wxDefaultPosition, [-1, 30], wxBU_LEFT);
  298. $self->{btn_export_stl} = Wx::Button->new($self->{right_panel}, -1, L("Export STL…"), wxDefaultPosition, [-1, 30], wxBU_LEFT);
  299. #$self->{btn_export_gcode}->SetFont($Slic3r::GUI::small_font);
  300. #$self->{btn_export_stl}->SetFont($Slic3r::GUI::small_font);
  301. $self->{btn_print}->Hide;
  302. $self->{btn_send_gcode}->Hide;
  303. my %icons = qw(
  304. add brick_add.png
  305. remove brick_delete.png
  306. reset cross.png
  307. arrange bricks.png
  308. export_gcode cog_go.png
  309. print arrow_up.png
  310. send_gcode arrow_up.png
  311. reslice reslice.png
  312. export_stl brick_go.png
  313. increase add.png
  314. decrease delete.png
  315. rotate45cw arrow_rotate_clockwise.png
  316. rotate45ccw arrow_rotate_anticlockwise.png
  317. changescale arrow_out.png
  318. split shape_ungroup.png
  319. cut package.png
  320. settings cog.png
  321. );
  322. for (grep $self->{"btn_$_"}, keys %icons) {
  323. $self->{"btn_$_"}->SetBitmap(Wx::Bitmap->new(Slic3r::var($icons{$_}), wxBITMAP_TYPE_PNG));
  324. }
  325. $self->selection_changed(0);
  326. $self->object_list_changed;
  327. EVT_BUTTON($self, $self->{btn_export_gcode}, sub {
  328. $self->export_gcode;
  329. });
  330. EVT_BUTTON($self, $self->{btn_print}, sub {
  331. $self->{print_file} = $self->export_gcode(Wx::StandardPaths::Get->GetTempDir());
  332. });
  333. EVT_BUTTON($self, $self->{btn_send_gcode}, sub {
  334. $self->{send_gcode_file} = $self->export_gcode(Wx::StandardPaths::Get->GetTempDir());
  335. });
  336. EVT_BUTTON($self, $self->{btn_reslice}, \&reslice);
  337. EVT_BUTTON($self, $self->{btn_export_stl}, \&export_stl);
  338. if ($self->{htoolbar}) {
  339. EVT_TOOL($self, TB_ADD, sub { $self->add; });
  340. EVT_TOOL($self, TB_REMOVE, sub { $self->remove() }); # explicitly pass no argument to remove
  341. EVT_TOOL($self, TB_RESET, sub { $self->reset; });
  342. EVT_TOOL($self, TB_ARRANGE, sub { $self->arrange; });
  343. EVT_TOOL($self, TB_MORE, sub { $self->increase; });
  344. EVT_TOOL($self, TB_FEWER, sub { $self->decrease; });
  345. EVT_TOOL($self, TB_45CW, sub { $_[0]->rotate(-45, Z, 'relative') });
  346. EVT_TOOL($self, TB_45CCW, sub { $_[0]->rotate(45, Z, 'relative') });
  347. EVT_TOOL($self, TB_SCALE, sub { $self->changescale(undef); });
  348. EVT_TOOL($self, TB_SPLIT, sub { $self->split_object; });
  349. EVT_TOOL($self, TB_CUT, sub { $_[0]->object_cut_dialog });
  350. EVT_TOOL($self, TB_SETTINGS, sub { $_[0]->object_settings_dialog });
  351. EVT_TOOL($self, TB_LAYER_EDITING, sub {
  352. my $state = Slic3r::GUI::_3DScene::is_layers_editing_enabled($self->{canvas3D});
  353. $self->{htoolbar}->ToggleTool(TB_LAYER_EDITING, ! $state);
  354. $self->on_layer_editing_toggled(! $state);
  355. });
  356. } else {
  357. EVT_BUTTON($self, $self->{btn_add}, sub { $self->add; });
  358. EVT_BUTTON($self, $self->{btn_remove}, sub { $self->remove() }); # explicitly pass no argument to remove
  359. EVT_BUTTON($self, $self->{btn_reset}, sub { $self->reset; });
  360. EVT_BUTTON($self, $self->{btn_arrange}, sub { $self->arrange; });
  361. EVT_BUTTON($self, $self->{btn_increase}, sub { $self->increase; });
  362. EVT_BUTTON($self, $self->{btn_decrease}, sub { $self->decrease; });
  363. EVT_BUTTON($self, $self->{btn_rotate45cw}, sub { $_[0]->rotate(-45, Z, 'relative') });
  364. EVT_BUTTON($self, $self->{btn_rotate45ccw}, sub { $_[0]->rotate(45, Z, 'relative') });
  365. EVT_BUTTON($self, $self->{btn_changescale}, sub { $self->changescale(undef); });
  366. EVT_BUTTON($self, $self->{btn_split}, sub { $self->split_object; });
  367. EVT_BUTTON($self, $self->{btn_cut}, sub { $_[0]->object_cut_dialog });
  368. EVT_BUTTON($self, $self->{btn_settings}, sub { $_[0]->object_settings_dialog });
  369. EVT_TOGGLEBUTTON($self, $self->{btn_layer_editing}, sub { $self->on_layer_editing_toggled($self->{btn_layer_editing}->GetValue); });
  370. }
  371. $_->SetDropTarget(Slic3r::GUI::Plater::DropTarget->new($self))
  372. for grep defined($_),
  373. $self, $self->{canvas3D}, $self->{preview3D}, $self->{list};
  374. # $self, $self->{canvas}, $self->{canvas3D}, $self->{preview3D}, $self->{list};
  375. EVT_COMMAND($self, -1, $PROGRESS_BAR_EVENT, sub {
  376. my ($self, $event) = @_;
  377. $self->on_progress_event($event->GetInt, $event->GetString);
  378. });
  379. EVT_COMMAND($self, -1, $ERROR_EVENT, sub {
  380. my ($self, $event) = @_;
  381. Slic3r::GUI::show_error($self, $event->GetString);
  382. });
  383. EVT_COMMAND($self, -1, $EXPORT_COMPLETED_EVENT, sub {
  384. my ($self, $event) = @_;
  385. $self->on_export_completed($event->GetInt);
  386. });
  387. EVT_COMMAND($self, -1, $PROCESS_COMPLETED_EVENT, sub {
  388. my ($self, $event) = @_;
  389. $self->on_process_completed($event->GetInt ? undef : $event->GetString);
  390. });
  391. {
  392. my $timer_id = Wx::NewId();
  393. $self->{apply_config_timer} = Wx::Timer->new($self, $timer_id);
  394. EVT_TIMER($self, $timer_id, sub {
  395. my ($self, $event) = @_;
  396. $self->async_apply_config;
  397. });
  398. }
  399. # $self->{canvas}->update_bed_size;
  400. if ($self->{canvas3D}) {
  401. Slic3r::GUI::_3DScene::set_bed_shape($self->{canvas3D}, $self->{config}->bed_shape);
  402. Slic3r::GUI::_3DScene::zoom_to_bed($self->{canvas3D});
  403. }
  404. if ($self->{preview3D}) {
  405. Slic3r::GUI::_3DScene::set_bed_shape($self->{preview3D}->canvas, $self->{config}->bed_shape);
  406. }
  407. $self->update;
  408. {
  409. my $presets;
  410. {
  411. $presets = $self->{presets_sizer} = Wx::FlexGridSizer->new(3, 2, 1, 2);
  412. $presets->AddGrowableCol(1, 1);
  413. $presets->SetFlexibleDirection(wxHORIZONTAL);
  414. my %group_labels = (
  415. print => L('Print settings'),
  416. filament => L('Filament'),
  417. printer => L('Printer'),
  418. );
  419. # UI Combo boxes for a print, multiple filaments, and a printer.
  420. # Initially a single filament combo box is created, but the number of combo boxes for the filament selection may increase,
  421. # once a printer preset with multiple extruders is activated.
  422. # $self->{preset_choosers}{$group}[$idx]
  423. $self->{preset_choosers} = {};
  424. for my $group (qw(print filament printer)) {
  425. my $text = Wx::StaticText->new($self->{right_panel}, -1, "$group_labels{$group}:", wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
  426. $text->SetFont($Slic3r::GUI::small_font);
  427. my $choice = Wx::BitmapComboBox->new($self->{right_panel}, -1, "", wxDefaultPosition, wxDefaultSize, [], wxCB_READONLY);
  428. if ($group eq 'filament') {
  429. EVT_LEFT_DOWN($choice, sub { $self->filament_color_box_lmouse_down(0, @_); } );
  430. }
  431. $self->{preset_choosers}{$group} = [$choice];
  432. # setup the listener
  433. EVT_COMBOBOX($choice, $choice, sub {
  434. my ($choice) = @_;
  435. wxTheApp->CallAfter(sub {
  436. $self->_on_select_preset($group, $choice, 0);
  437. });
  438. });
  439. $presets->Add($text, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL | wxRIGHT, 4);
  440. $presets->Add($choice, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND | wxBOTTOM, 1);
  441. }
  442. $presets->Layout;
  443. }
  444. my $frequently_changed_parameters_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  445. Slic3r::GUI::add_frequently_changed_parameters($self->{right_panel}, $frequently_changed_parameters_sizer, $presets);
  446. my $object_info_sizer;
  447. {
  448. my $box = Wx::StaticBox->new($scrolled_window_panel, -1, L("Info"));
  449. $object_info_sizer = Wx::StaticBoxSizer->new($box, wxVERTICAL);
  450. $object_info_sizer->SetMinSize([300,-1]);
  451. my $grid_sizer = Wx::FlexGridSizer->new(3, 4, 5, 5);
  452. $grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
  453. $grid_sizer->AddGrowableCol(1, 1);
  454. $grid_sizer->AddGrowableCol(3, 1);
  455. $object_info_sizer->Add($grid_sizer, 0, wxEXPAND);
  456. my @info = (
  457. size => L("Size"),
  458. volume => L("Volume"),
  459. facets => L("Facets"),
  460. materials => L("Materials"),
  461. manifold => L("Manifold"),
  462. );
  463. while (my $field = shift @info) {
  464. my $label = shift @info;
  465. my $text = Wx::StaticText->new($scrolled_window_panel, -1, "$label:", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  466. $text->SetFont($Slic3r::GUI::small_font);
  467. $grid_sizer->Add($text, 0);
  468. $self->{"object_info_$field"} = Wx::StaticText->new($scrolled_window_panel, -1, "", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  469. $self->{"object_info_$field"}->SetFont($Slic3r::GUI::small_font);
  470. if ($field eq 'manifold') {
  471. $self->{object_info_manifold_warning_icon} = Wx::StaticBitmap->new($scrolled_window_panel, -1, Wx::Bitmap->new(Slic3r::var("error.png"), wxBITMAP_TYPE_PNG));
  472. $self->{object_info_manifold_warning_icon}->Hide;
  473. my $h_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  474. $h_sizer->Add($self->{object_info_manifold_warning_icon}, 0);
  475. $h_sizer->Add($self->{"object_info_$field"}, 0);
  476. $grid_sizer->Add($h_sizer, 0, wxEXPAND);
  477. } else {
  478. $grid_sizer->Add($self->{"object_info_$field"}, 0);
  479. }
  480. }
  481. }
  482. my $print_info_sizer = $self->{print_info_sizer} = Wx::StaticBoxSizer->new(
  483. Wx::StaticBox->new($scrolled_window_panel, -1, L("Sliced Info")), wxVERTICAL);
  484. $print_info_sizer->SetMinSize([300,-1]);
  485. my $buttons_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  486. $self->{buttons_sizer} = $buttons_sizer;
  487. $buttons_sizer->AddStretchSpacer(1);
  488. $buttons_sizer->Add($self->{btn_export_stl}, 0, wxALIGN_RIGHT, 0);
  489. $buttons_sizer->Add($self->{btn_reslice}, 0, wxALIGN_RIGHT, 0);
  490. $buttons_sizer->Add($self->{btn_print}, 0, wxALIGN_RIGHT, 0);
  491. $buttons_sizer->Add($self->{btn_send_gcode}, 0, wxALIGN_RIGHT, 0);
  492. $buttons_sizer->Add($self->{btn_export_gcode}, 0, wxALIGN_RIGHT, 0);
  493. $scrolled_window_sizer->Add($self->{list}, 1, wxEXPAND, 5);
  494. $scrolled_window_sizer->Add($object_info_sizer, 0, wxEXPAND, 0);
  495. $scrolled_window_sizer->Add($print_info_sizer, 0, wxEXPAND, 0);
  496. my $right_sizer = Wx::BoxSizer->new(wxVERTICAL);
  497. $right_sizer->SetMinSize([320,-1]);
  498. $right_sizer->Add($presets, 0, wxEXPAND | wxTOP, 10) if defined $presets;
  499. $right_sizer->Add($frequently_changed_parameters_sizer, 0, wxEXPAND | wxTOP, 0) if defined $frequently_changed_parameters_sizer;
  500. $right_sizer->Add($buttons_sizer, 0, wxEXPAND | wxBOTTOM, 5);
  501. $right_sizer->Add($scrolled_window_panel, 1, wxEXPAND | wxALL, 1);
  502. # Show the box initially, let it be shown after the slicing is finished.
  503. $self->print_info_box_show(0);
  504. $self->{right_panel}->SetSizer($right_sizer);
  505. my $hsizer = Wx::BoxSizer->new(wxHORIZONTAL);
  506. $hsizer->Add($self->{preview_notebook}, 1, wxEXPAND | wxTOP, 1);
  507. $hsizer->Add($self->{right_panel}, 0, wxEXPAND | wxLEFT | wxRIGHT, 3);
  508. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  509. $sizer->Add($self->{htoolbar}, 0, wxEXPAND, 0) if $self->{htoolbar};
  510. $sizer->Add($self->{btoolbar}, 0, wxEXPAND, 0) if $self->{btoolbar};
  511. $sizer->Add($hsizer, 1, wxEXPAND, 0);
  512. $sizer->SetSizeHints($self);
  513. $self->SetSizer($sizer);
  514. }
  515. # Last correct selected item for each preset
  516. {
  517. $self->{selected_item_print} = 0;
  518. $self->{selected_item_filament} = 0;
  519. $self->{selected_item_printer} = 0;
  520. }
  521. $self->update_ui_from_settings();
  522. return $self;
  523. }
  524. # sets the callback
  525. sub on_select_preset {
  526. my ($self, $cb) = @_;
  527. $self->{on_select_preset} = $cb;
  528. }
  529. # Called from the platter combo boxes selecting the active print, filament or printer.
  530. sub _on_select_preset {
  531. my ($self, $group, $choice, $idx) = @_;
  532. # If user changed a filament preset and the selected machine is equipped with multiple extruders,
  533. # there are multiple filament selection combo boxes shown at the platter. In that case
  534. # don't propagate the filament selection changes to the tab.
  535. if ($group eq 'filament') {
  536. wxTheApp->{preset_bundle}->set_filament_preset($idx, $choice->GetStringSelection);
  537. }
  538. if ($group eq 'filament' && @{$self->{preset_choosers}{filament}} > 1) {
  539. # Only update the platter UI for the 2nd and other filaments.
  540. wxTheApp->{preset_bundle}->update_platter_filament_ui($idx, $choice);
  541. } else {
  542. my $selected_item = $choice->GetSelection();
  543. return if ($selected_item == $self->{"selected_item_$group"} &&
  544. !Slic3r::GUI::get_preset_tab($group)->current_preset_is_dirty);
  545. my $selected_string = $choice->GetString($selected_item);
  546. if ($selected_string eq ("------- ".L("System presets")." -------") ||
  547. $selected_string eq ("------- ".L("User presets")." -------") ){
  548. $choice->SetSelection($self->{"selected_item_$group"});
  549. return;
  550. }
  551. # call GetSelection() in scalar context as it's context-aware
  552. # $self->{on_select_preset}->($group, $choice->GetStringSelection)
  553. $self->{on_select_preset}->($group, $selected_string)
  554. if $self->{on_select_preset};
  555. $self->{"selected_item_$group"} = $selected_item;
  556. }
  557. # Synchronize config.ini with the current selections.
  558. wxTheApp->{preset_bundle}->export_selections(wxTheApp->{app_config});
  559. # get new config and generate on_config_change() event for updating plater and other things
  560. $self->on_config_change(wxTheApp->{preset_bundle}->full_config);
  561. }
  562. sub on_layer_editing_toggled {
  563. my ($self, $new_state) = @_;
  564. Slic3r::GUI::_3DScene::enable_layers_editing($self->{canvas3D}, $new_state);
  565. if ($new_state && ! Slic3r::GUI::_3DScene::is_layers_editing_enabled($self->{canvas3D})) {
  566. # Initialization of the OpenGL shaders failed. Disable the tool.
  567. if ($self->{htoolbar}) {
  568. $self->{htoolbar}->EnableTool(TB_LAYER_EDITING, 0);
  569. $self->{htoolbar}->ToggleTool(TB_LAYER_EDITING, 0);
  570. } else {
  571. $self->{"btn_layer_editing"}->Disable;
  572. $self->{"btn_layer_editing"}->SetValue(0);
  573. }
  574. }
  575. $self->{canvas3D}->Refresh;
  576. $self->{canvas3D}->Update;
  577. }
  578. sub GetFrame {
  579. my ($self) = @_;
  580. return &Wx::GetTopLevelParent($self);
  581. }
  582. # Called after the Preferences dialog is closed and the program settings are saved.
  583. # Update the UI based on the current preferences.
  584. sub update_ui_from_settings
  585. {
  586. my ($self) = @_;
  587. if (defined($self->{btn_reslice}) && $self->{buttons_sizer}->IsShown($self->{btn_reslice}) != (! wxTheApp->{app_config}->get("background_processing"))) {
  588. $self->{buttons_sizer}->Show($self->{btn_reslice}, ! wxTheApp->{app_config}->get("background_processing"));
  589. $self->{buttons_sizer}->Layout;
  590. }
  591. }
  592. # Update preset combo boxes (Print settings, Filament, Printer) from their respective tabs.
  593. # Called by
  594. # Slic3r::GUI::Tab::Print::_on_presets_changed
  595. # Slic3r::GUI::Tab::Filament::_on_presets_changed
  596. # Slic3r::GUI::Tab::Printer::_on_presets_changed
  597. # when the presets are loaded or the user selects another preset.
  598. # For Print settings and Printer, synchronize the selection index with their tabs.
  599. # For Filament, synchronize the selection index for a single extruder printer only, otherwise keep the selection.
  600. sub update_presets {
  601. # $group: one of qw(print filament printer)
  602. # $presets: PresetCollection
  603. my ($self, $group, $presets) = @_;
  604. my @choosers = @{$self->{preset_choosers}{$group}};
  605. if ($group eq 'filament') {
  606. my $choice_idx = 0;
  607. if (int(@choosers) == 1) {
  608. # Single filament printer, synchronize the filament presets.
  609. wxTheApp->{preset_bundle}->set_filament_preset(0, wxTheApp->{preset_bundle}->filament->get_selected_preset->name);
  610. }
  611. foreach my $choice (@choosers) {
  612. wxTheApp->{preset_bundle}->update_platter_filament_ui($choice_idx, $choice);
  613. $choice_idx += 1;
  614. }
  615. } elsif ($group eq 'print') {
  616. wxTheApp->{preset_bundle}->print->update_platter_ui($choosers[0]);
  617. } elsif ($group eq 'printer') {
  618. # Update the print choosers to only contain the compatible presets, update the dirty flags.
  619. wxTheApp->{preset_bundle}->print->update_platter_ui($self->{preset_choosers}{print}->[0]);
  620. # Update the printer choosers, update the dirty flags.
  621. wxTheApp->{preset_bundle}->printer->update_platter_ui($choosers[0]);
  622. # Update the filament choosers to only contain the compatible presets, update the color preview,
  623. # update the dirty flags.
  624. my $choice_idx = 0;
  625. foreach my $choice (@{$self->{preset_choosers}{filament}}) {
  626. wxTheApp->{preset_bundle}->update_platter_filament_ui($choice_idx, $choice);
  627. $choice_idx += 1;
  628. }
  629. }
  630. # Synchronize config.ini with the current selections.
  631. wxTheApp->{preset_bundle}->export_selections(wxTheApp->{app_config});
  632. }
  633. sub add {
  634. my ($self) = @_;
  635. my @input_files = wxTheApp->open_model($self);
  636. $self->load_files(\@input_files);
  637. }
  638. sub load_files {
  639. my ($self, $input_files) = @_;
  640. return if ! defined($input_files) || ! scalar(@$input_files);
  641. my $nozzle_dmrs = $self->{config}->get('nozzle_diameter');
  642. # One of the files is potentionally a bundle of files. Don't bundle them, but load them one by one.
  643. # Only bundle .stls or .objs if the printer has multiple extruders.
  644. my $one_by_one = (@$nozzle_dmrs <= 1) || (@$input_files == 1) ||
  645. defined(first { $_ =~ /.[aA][mM][fF]$/ || $_ =~ /.[aA][mM][fF].[xX][mM][lL]$/ || $_ =~ /.[zZ][iI][pP].[aA][mM][fF]$/ || $_ =~ /.3[mM][fF]$/ || $_ =~ /.[pP][rR][uI][sS][aA]$/ } @$input_files);
  646. my $process_dialog = Wx::ProgressDialog->new(L('Loading…'), L("Processing input file\n") . basename($input_files->[0]), 100, $self, 0);
  647. $process_dialog->Pulse;
  648. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  649. # new_model to collect volumes, if all of them come from .stl or .obj and there is a chance, that they will be
  650. # possibly merged into a single multi-part object.
  651. my $new_model = $one_by_one ? undef : Slic3r::Model->new;
  652. # Object indices for the UI.
  653. my @obj_idx = ();
  654. # Collected file names to display the final message on the status bar.
  655. my @loaded_files = ();
  656. # For all input files.
  657. for (my $i = 0; $i < @$input_files; $i += 1) {
  658. my $input_file = $input_files->[$i];
  659. $process_dialog->Update(100. * $i / @$input_files, L("Processing input file\n") . basename($input_file));
  660. my $model;
  661. if (($input_file =~ /.3[mM][fF]$/) || ($input_file =~ /.[zZ][iI][pP].[aA][mM][fF]$/))
  662. {
  663. $model = eval { Slic3r::Model->read_from_archive($input_file, wxTheApp->{preset_bundle}, 0) };
  664. Slic3r::GUI::show_error($self, $@) if $@;
  665. $_->load_current_preset for (values %{$self->GetFrame->{options_tabs}});
  666. wxTheApp->{app_config}->update_config_dir(dirname($input_file));
  667. # forces the update of the config here, or it will invalidate the imported layer heights profile if done using the timer
  668. # and if the config contains a "layer_height" different from the current defined one
  669. $self->async_apply_config;
  670. }
  671. else
  672. {
  673. $model = eval { Slic3r::Model->read_from_file($input_file, 0) };
  674. Slic3r::GUI::show_error($self, $@) if $@;
  675. }
  676. next if ! defined $model;
  677. if ($model->looks_like_multipart_object) {
  678. my $dialog = Wx::MessageDialog->new($self,
  679. L("This file contains several objects positioned at multiple heights. "
  680. . "Instead of considering them as multiple objects, should I consider\n"
  681. . "this file as a single object having multiple parts?\n"),
  682. L('Multi-part object detected'), wxICON_WARNING | wxYES | wxNO);
  683. $model->convert_multipart_object(scalar(@$nozzle_dmrs)) if $dialog->ShowModal() == wxID_YES;
  684. }
  685. if ($one_by_one) {
  686. push @obj_idx, $self->load_model_objects(@{$model->objects});
  687. } else {
  688. # This must be an .stl or .obj file, which may contain a maximum of one volume.
  689. $new_model->add_object($_) for (@{$model->objects});
  690. }
  691. }
  692. if ($new_model) {
  693. my $dialog = Wx::MessageDialog->new($self,
  694. L("Multiple objects were loaded for a multi-material printer.\n"
  695. . "Instead of considering them as multiple objects, should I consider\n"
  696. . "these files to represent a single object having multiple parts?\n"),
  697. L('Multi-part object detected'), wxICON_WARNING | wxYES | wxNO);
  698. $new_model->convert_multipart_object(scalar(@$nozzle_dmrs)) if $dialog->ShowModal() == wxID_YES;
  699. push @obj_idx, $self->load_model_objects(@{$new_model->objects});
  700. }
  701. # Note the current directory for the file open dialog.
  702. wxTheApp->{app_config}->update_skein_dir(dirname($input_files->[-1]));
  703. $process_dialog->Destroy;
  704. $self->statusbar->SetStatusText(L("Loaded ") . join(',', @loaded_files));
  705. return @obj_idx;
  706. }
  707. sub load_model_objects {
  708. my ($self, @model_objects) = @_;
  709. my $bed_centerf = $self->bed_centerf;
  710. my $bed_shape = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape});
  711. my $bed_size = $bed_shape->bounding_box->size;
  712. my $need_arrange = 0;
  713. my $scaled_down = 0;
  714. my @obj_idx = ();
  715. foreach my $model_object (@model_objects) {
  716. my $o = $self->{model}->add_object($model_object);
  717. my $object_name = $model_object->name;
  718. $object_name = basename($model_object->input_file) if ($object_name eq '');
  719. push @{ $self->{objects} }, Slic3r::GUI::Plater::Object->new(name => $object_name);
  720. push @obj_idx, $#{ $self->{objects} };
  721. if ($model_object->instances_count == 0) {
  722. # if object has no defined position(s) we need to rearrange everything after loading
  723. $need_arrange = 1;
  724. # add a default instance and center object around origin
  725. $o->center_around_origin; # also aligns object to Z = 0
  726. $o->add_instance(offset => $bed_centerf);
  727. }
  728. {
  729. # if the object is too large (more than 5 times the bed), scale it down
  730. my $size = $o->bounding_box->size;
  731. my $ratio = max($size->x / unscale($bed_size->x), $size->y / unscale($bed_size->y));
  732. if ($ratio > 10000) {
  733. # the size of the object is too big -> this could lead to overflow when moving to clipper coordinates,
  734. # so scale down the mesh
  735. $o->scale_xyz(Slic3r::Pointf3->new(1/$ratio, 1/$ratio, 1/$ratio));
  736. $scaled_down = 1;
  737. }
  738. elsif ($ratio > 5) {
  739. $_->set_scaling_factor(1/$ratio) for @{$o->instances};
  740. $scaled_down = 1;
  741. }
  742. }
  743. $self->{print}->auto_assign_extruders($o);
  744. $self->{print}->add_model_object($o);
  745. }
  746. # if user turned autocentering off, automatic arranging would disappoint them
  747. if (! wxTheApp->{app_config}->get("autocenter")) {
  748. $need_arrange = 0;
  749. }
  750. if ($scaled_down) {
  751. Slic3r::GUI::show_info(
  752. $self,
  753. L('Your object appears to be too large, so it was automatically scaled down to fit your print bed.'),
  754. L('Object too large?'),
  755. );
  756. }
  757. foreach my $obj_idx (@obj_idx) {
  758. my $object = $self->{objects}[$obj_idx];
  759. my $model_object = $self->{model}->objects->[$obj_idx];
  760. $self->{list}->InsertStringItem($obj_idx, $object->name);
  761. $self->{list}->SetItemFont($obj_idx, Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL))
  762. if $self->{list}->can('SetItemFont'); # legacy code for wxPerl < 0.9918 not supporting SetItemFont()
  763. $self->{list}->SetItem($obj_idx, 1, $model_object->instances_count);
  764. $self->{list}->SetItem($obj_idx, 2, ($model_object->instances->[0]->scaling_factor * 100) . "%");
  765. # $self->reset_thumbnail($obj_idx);
  766. }
  767. $self->arrange if $need_arrange;
  768. $self->update;
  769. # zoom to objects
  770. Slic3r::GUI::_3DScene::zoom_to_volumes($self->{canvas3D}) if $self->{canvas3D};
  771. $self->{list}->Update;
  772. $self->{list}->Select($obj_idx[-1], 1);
  773. $self->object_list_changed;
  774. $self->schedule_background_process;
  775. return @obj_idx;
  776. }
  777. sub bed_centerf {
  778. my ($self) = @_;
  779. my $bed_shape = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape});
  780. my $bed_center = $bed_shape->bounding_box->center;
  781. return Slic3r::Pointf->new(unscale($bed_center->x), unscale($bed_center->y)); #)
  782. }
  783. sub remove {
  784. my $self = shift;
  785. my ($obj_idx) = @_;
  786. $self->stop_background_process;
  787. # Prevent toolpaths preview from rendering while we modify the Print object
  788. $self->{toolpaths2D}->enabled(0) if $self->{toolpaths2D};
  789. $self->{preview3D}->enabled(0) if $self->{preview3D};
  790. # if no object index is supplied, remove the selected one
  791. if (! defined $obj_idx) {
  792. ($obj_idx, undef) = $self->selected_object;
  793. return if ! defined $obj_idx;
  794. }
  795. splice @{$self->{objects}}, $obj_idx, 1;
  796. $self->{model}->delete_object($obj_idx);
  797. $self->{print}->delete_object($obj_idx);
  798. $self->{list}->DeleteItem($obj_idx);
  799. $self->object_list_changed;
  800. $self->select_object(undef);
  801. $self->update;
  802. $self->schedule_background_process;
  803. }
  804. sub reset {
  805. my $self = shift;
  806. $self->stop_background_process;
  807. # Prevent toolpaths preview from rendering while we modify the Print object
  808. $self->{toolpaths2D}->enabled(0) if $self->{toolpaths2D};
  809. $self->{preview3D}->enabled(0) if $self->{preview3D};
  810. @{$self->{objects}} = ();
  811. $self->{model}->clear_objects;
  812. $self->{print}->clear_objects;
  813. $self->{list}->DeleteAllItems;
  814. $self->object_list_changed;
  815. $self->select_object(undef);
  816. $self->update;
  817. }
  818. sub increase {
  819. my ($self, $copies) = @_;
  820. $copies //= 1;
  821. my ($obj_idx, $object) = $self->selected_object;
  822. return if ! defined $obj_idx;
  823. my $model_object = $self->{model}->objects->[$obj_idx];
  824. my $instance = $model_object->instances->[-1];
  825. for my $i (1..$copies) {
  826. $instance = $model_object->add_instance(
  827. offset => Slic3r::Pointf->new(map 10+$_, @{$instance->offset}),
  828. scaling_factor => $instance->scaling_factor,
  829. rotation => $instance->rotation,
  830. );
  831. $self->{print}->objects->[$obj_idx]->add_copy($instance->offset);
  832. }
  833. $self->{list}->SetItem($obj_idx, 1, $model_object->instances_count);
  834. # only autoarrange if user has autocentering enabled
  835. $self->stop_background_process;
  836. if (wxTheApp->{app_config}->get("autocenter")) {
  837. $self->arrange;
  838. } else {
  839. $self->update;
  840. }
  841. $self->schedule_background_process;
  842. }
  843. sub decrease {
  844. my ($self, $copies_asked) = @_;
  845. my $copies = $copies_asked // 1;
  846. my ($obj_idx, $object) = $self->selected_object;
  847. return if ! defined $obj_idx;
  848. $self->stop_background_process;
  849. my $model_object = $self->{model}->objects->[$obj_idx];
  850. if ($model_object->instances_count > $copies) {
  851. for my $i (1..$copies) {
  852. $model_object->delete_last_instance;
  853. $self->{print}->objects->[$obj_idx]->delete_last_copy;
  854. }
  855. $self->{list}->SetItem($obj_idx, 1, $model_object->instances_count);
  856. } elsif (defined $copies_asked) {
  857. # The "decrease" came from the "set number of copies" dialog.
  858. $self->remove;
  859. } else {
  860. # The "decrease" came from the "-" button. Don't allow the object to disappear.
  861. $self->resume_background_process;
  862. return;
  863. }
  864. if ($self->{objects}[$obj_idx]) {
  865. $self->{list}->Select($obj_idx, 0);
  866. $self->{list}->Select($obj_idx, 1);
  867. }
  868. $self->update;
  869. $self->schedule_background_process;
  870. }
  871. sub set_number_of_copies {
  872. my ($self) = @_;
  873. $self->pause_background_process;
  874. # get current number of copies
  875. my ($obj_idx, $object) = $self->selected_object;
  876. my $model_object = $self->{model}->objects->[$obj_idx];
  877. # prompt user
  878. my $copies = -1;
  879. $copies = Wx::GetNumberFromUser("", L("Enter the number of copies of the selected object:"), L("Copies"), $model_object->instances_count, 0, 1000, $self);
  880. my $diff = $copies - $model_object->instances_count;
  881. if ($diff == 0 || $copies == -1) {
  882. # no variation
  883. $self->resume_background_process;
  884. } elsif ($diff > 0) {
  885. $self->increase($diff);
  886. } elsif ($diff < 0) {
  887. $self->decrease(-$diff);
  888. }
  889. }
  890. sub _get_number_from_user {
  891. my ($self, $title, $prompt_message, $error_message, $default, $only_positive) = @_;
  892. for (;;) {
  893. my $value = Wx::GetTextFromUser($prompt_message, $title, $default, $self);
  894. # Accept both dashes and dots as a decimal separator.
  895. $value =~ s/,/./;
  896. # If scaling value is being entered, remove the trailing percent sign.
  897. $value =~ s/%$// if $only_positive;
  898. # User canceled the selection, return undef.
  899. return if $value eq '';
  900. # Validate a numeric value.
  901. return $value if ($value =~ /^-?\d*(?:\.\d*)?$/) && (! $only_positive || $value > 0);
  902. Wx::MessageBox(
  903. $error_message .
  904. (($only_positive && $value <= 0) ?
  905. ": ".$value.L("\nNon-positive value.") :
  906. ": ".$value.L("\nNot a numeric value.")),
  907. L("Slic3r Error"), wxOK | wxICON_EXCLAMATION, $self);
  908. $default = $value;
  909. }
  910. }
  911. sub rotate {
  912. my ($self, $angle, $axis, $relative_key, $axis_x, $axis_y, $axis_z) = @_;
  913. $relative_key //= 'absolute'; # relative or absolute coordinates
  914. $axis_x //= 0;
  915. $axis_y //= 0;
  916. $axis_z //= 0;
  917. my $relative = $relative_key eq 'relative';
  918. my ($obj_idx, $object) = $self->selected_object;
  919. return if !defined $obj_idx;
  920. my $model_object = $self->{model}->objects->[$obj_idx];
  921. my $model_instance = $model_object->instances->[0];
  922. if (!defined $angle) {
  923. my $axis_name = $axis == X ? 'X' : $axis == Y ? 'Y' : 'Z';
  924. my $default = $axis == Z ? rad2deg($model_instance->rotation) : 0;
  925. $angle = $self->_get_number_from_user(L("Enter the rotation angle:"), L("Rotate around ").$axis_name.(" axis"), L("Invalid rotation angle entered"), $default);
  926. return if $angle eq '';
  927. }
  928. # Let's calculate vector of rotation axis (if we don't have it already)
  929. # The minus is there so that the direction is the same as was established
  930. if (defined $axis) {
  931. if ($axis == X) {
  932. $axis_x = -1;
  933. }
  934. if ($axis == Y) {
  935. $axis_y = -1;
  936. }
  937. }
  938. $self->stop_background_process;
  939. if (defined $axis && $axis == Z) {
  940. my $new_angle = deg2rad($angle);
  941. foreach my $inst (@{ $model_object->instances }) {
  942. my $rotation = ($relative ? $inst->rotation : 0.) + $new_angle;
  943. while ($rotation > 2.0 * PI) {
  944. $rotation -= 2.0 * PI;
  945. }
  946. while ($rotation < 0.0) {
  947. $rotation += 2.0 * PI;
  948. }
  949. $inst->set_rotation($rotation);
  950. Slic3r::GUI::_3DScene::update_gizmos_data($self->{canvas3D}) if ($self->{canvas3D});
  951. }
  952. # $object->transform_thumbnail($self->{model}, $obj_idx);
  953. } else {
  954. if (defined $axis) {
  955. # rotation around X and Y needs to be performed on mesh
  956. # so we first apply any Z rotation
  957. if ($model_instance->rotation != 0) {
  958. $model_object->rotate($model_instance->rotation, Slic3r::Pointf3->new(0, 0, -1));
  959. $_->set_rotation(0) for @{ $model_object->instances };
  960. }
  961. }
  962. $model_object->rotate(deg2rad($angle), Slic3r::Pointf3->new($axis_x, $axis_y, $axis_z));
  963. # # realign object to Z = 0
  964. # $model_object->center_around_origin;
  965. # $self->reset_thumbnail($obj_idx);
  966. }
  967. # update print and start background processing
  968. $self->{print}->add_model_object($model_object, $obj_idx);
  969. $self->selection_changed; # refresh info (size etc.)
  970. $self->update;
  971. $self->schedule_background_process;
  972. }
  973. sub mirror {
  974. my ($self, $axis) = @_;
  975. my ($obj_idx, $object) = $self->selected_object;
  976. return if !defined $obj_idx;
  977. my $model_object = $self->{model}->objects->[$obj_idx];
  978. my $model_instance = $model_object->instances->[0];
  979. # apply Z rotation before mirroring
  980. if ($model_instance->rotation != 0) {
  981. $model_object->rotate($model_instance->rotation, Slic3r::Pointf3->new(0, 0, 1));
  982. $_->set_rotation(0) for @{ $model_object->instances };
  983. }
  984. $model_object->mirror($axis);
  985. # # realign object to Z = 0
  986. # $model_object->center_around_origin;
  987. # $self->reset_thumbnail($obj_idx);
  988. # update print and start background processing
  989. $self->stop_background_process;
  990. $self->{print}->add_model_object($model_object, $obj_idx);
  991. $self->selection_changed; # refresh info (size etc.)
  992. $self->update;
  993. $self->schedule_background_process;
  994. }
  995. sub changescale {
  996. my ($self, $axis, $tosize) = @_;
  997. my ($obj_idx, $object) = $self->selected_object;
  998. return if !defined $obj_idx;
  999. my $model_object = $self->{model}->objects->[$obj_idx];
  1000. my $model_instance = $model_object->instances->[0];
  1001. my $object_size = $model_object->instance_bounding_box(0)->size;
  1002. if (defined $axis) {
  1003. my $axis_name = $axis == X ? 'X' : $axis == Y ? 'Y' : 'Z';
  1004. my $scale;
  1005. if ($tosize) {
  1006. my $cursize = $object_size->[$axis];
  1007. my $newsize = $self->_get_number_from_user(
  1008. L('Enter the new size for the selected object:'),
  1009. L("Scale along ").$axis_name, L('Invalid scaling value entered'), $cursize, 1);
  1010. return if $newsize eq '';
  1011. $scale = $newsize / $cursize * 100;
  1012. } else {
  1013. $scale = $self->_get_number_from_user(L('Enter the scale % for the selected object:'), L("Scale along ").$axis_name, L('Invalid scaling value entered'), 100, 1);
  1014. return if $scale eq '';
  1015. }
  1016. # apply Z rotation before scaling
  1017. if ($model_instance->rotation != 0) {
  1018. $model_object->rotate($model_instance->rotation, Slic3r::Pointf3->new(0, 0, 1));
  1019. $_->set_rotation(0) for @{ $model_object->instances };
  1020. }
  1021. my $versor = [1,1,1];
  1022. $versor->[$axis] = $scale/100;
  1023. $model_object->scale_xyz(Slic3r::Pointf3->new(@$versor));
  1024. #FIXME Scale the layer height profile when $axis == Z?
  1025. #FIXME Scale the layer height ranges $axis == Z?
  1026. # object was already aligned to Z = 0, so no need to realign it
  1027. # $self->reset_thumbnail($obj_idx);
  1028. } else {
  1029. my $scale;
  1030. if ($tosize) {
  1031. my $cursize = max(@$object_size);
  1032. my $newsize = $self->_get_number_from_user(L('Enter the new max size for the selected object:'), L('Scale'), L('Invalid scaling value entered'), $cursize, 1);
  1033. return if ! defined($newsize) || $newsize eq '';
  1034. $scale = $model_instance->scaling_factor * $newsize / $cursize * 100;
  1035. } else {
  1036. # max scale factor should be above 2540 to allow importing files exported in inches
  1037. $scale = $self->_get_number_from_user(L('Enter the scale % for the selected object:'), L('Scale'), L('Invalid scaling value entered'), $model_instance->scaling_factor*100, 1);
  1038. return if ! defined($scale) || $scale eq '';
  1039. }
  1040. $self->{list}->SetItem($obj_idx, 2, "$scale%");
  1041. $scale /= 100; # turn percent into factor
  1042. my $variation = $scale / $model_instance->scaling_factor;
  1043. #FIXME Scale the layer height profile?
  1044. foreach my $range (@{ $model_object->layer_height_ranges }) {
  1045. $range->[0] *= $variation;
  1046. $range->[1] *= $variation;
  1047. }
  1048. $_->set_scaling_factor($scale) for @{ $model_object->instances };
  1049. # $object->transform_thumbnail($self->{model}, $obj_idx);
  1050. }
  1051. # update print and start background processing
  1052. $self->stop_background_process;
  1053. $self->{print}->add_model_object($model_object, $obj_idx);
  1054. $self->selection_changed(1); # refresh info (size, volume etc.)
  1055. $self->update;
  1056. $self->schedule_background_process;
  1057. }
  1058. sub arrange {
  1059. my $self = shift;
  1060. $self->pause_background_process;
  1061. # my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($self->{config}->bed_shape);
  1062. # my $success = $self->{model}->arrange_objects(wxTheApp->{preset_bundle}->full_config->min_object_distance, $bb);
  1063. # Update is not implemented in C++ so we cannot call this for now
  1064. $self->{appController}->arrange_model;
  1065. # ignore arrange failures on purpose: user has visual feedback and we don't need to warn him
  1066. # when parts don't fit in print bed
  1067. # Force auto center of the aligned grid of of objects on the print bed.
  1068. $self->update(0);
  1069. }
  1070. sub split_object {
  1071. my $self = shift;
  1072. my ($obj_idx, $current_object) = $self->selected_object;
  1073. # we clone model object because split_object() adds the split volumes
  1074. # into the same model object, thus causing duplicates when we call load_model_objects()
  1075. my $new_model = $self->{model}->clone; # store this before calling get_object()
  1076. my $current_model_object = $new_model->get_object($obj_idx);
  1077. if ($current_model_object->volumes_count > 1) {
  1078. Slic3r::GUI::warning_catcher($self)->(L("The selected object can't be split because it contains more than one volume/material."));
  1079. return;
  1080. }
  1081. $self->pause_background_process;
  1082. my @model_objects = @{$current_model_object->split_object};
  1083. if (@model_objects == 1) {
  1084. $self->resume_background_process;
  1085. Slic3r::GUI::warning_catcher($self)->(L("The selected object couldn't be split because it contains only one part."));
  1086. $self->resume_background_process;
  1087. return;
  1088. }
  1089. $_->center_around_origin for (@model_objects);
  1090. $self->remove($obj_idx);
  1091. $current_object = $obj_idx = undef;
  1092. # load all model objects at once, otherwise the plate would be rearranged after each one
  1093. # causing original positions not to be kept
  1094. $self->load_model_objects(@model_objects);
  1095. }
  1096. sub schedule_background_process {
  1097. my ($self) = @_;
  1098. if (defined $self->{apply_config_timer}) {
  1099. $self->{apply_config_timer}->Start(PROCESS_DELAY, 1); # 1 = one shot
  1100. }
  1101. }
  1102. # Executed asynchronously by a timer every PROCESS_DELAY (0.5 second).
  1103. # The timer is started by schedule_background_process(),
  1104. sub async_apply_config {
  1105. my ($self) = @_;
  1106. # pause process thread before applying new config
  1107. # since we don't want to touch data that is being used by the threads
  1108. $self->pause_background_process;
  1109. # apply new config
  1110. my $invalidated = $self->{print}->apply_config(wxTheApp->{preset_bundle}->full_config);
  1111. # Just redraw the 3D canvas without reloading the scene.
  1112. $self->{canvas3D}->Refresh if Slic3r::GUI::_3DScene::is_layers_editing_enabled($self->{canvas3D});
  1113. # Hide the slicing results if the current slicing status is no more valid.
  1114. $self->print_info_box_show(0) if $invalidated;
  1115. if (wxTheApp->{app_config}->get("background_processing")) {
  1116. if ($invalidated) {
  1117. # kill current thread if any
  1118. $self->stop_background_process;
  1119. } else {
  1120. $self->resume_background_process;
  1121. }
  1122. # schedule a new process thread in case it wasn't running
  1123. $self->start_background_process;
  1124. }
  1125. # Reset preview canvases. If the print has been invalidated, the preview canvases will be cleared.
  1126. # Otherwise they will be just refreshed.
  1127. if ($invalidated) {
  1128. $self->{gcode_preview_data}->reset;
  1129. $self->{toolpaths2D}->reload_print if $self->{toolpaths2D};
  1130. $self->{preview3D}->reload_print if $self->{preview3D};
  1131. # We also need to reload 3D scene because of the wipe tower preview box
  1132. if ($self->{config}->wipe_tower) {
  1133. my $selections = $self->collect_selections;
  1134. Slic3r::GUI::_3DScene::set_objects_selections($self->{canvas3D}, \@$selections);
  1135. Slic3r::GUI::_3DScene::reload_scene($self->{canvas3D}, 1) if $self->{canvas3D}
  1136. }
  1137. }
  1138. }
  1139. sub start_background_process {
  1140. my ($self) = @_;
  1141. return if !@{$self->{objects}};
  1142. return if $self->{process_thread};
  1143. # It looks like declaring a local $SIG{__WARN__} prevents the ugly
  1144. # "Attempt to free unreferenced scalar" warning...
  1145. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  1146. # don't start process thread if config is not valid
  1147. eval {
  1148. # this will throw errors if config is not valid
  1149. wxTheApp->{preset_bundle}->full_config->validate;
  1150. $self->{print}->validate;
  1151. };
  1152. if ($@) {
  1153. $self->statusbar->SetStatusText($@);
  1154. return;
  1155. }
  1156. # Copy the names of active presets into the placeholder parser.
  1157. wxTheApp->{preset_bundle}->export_selections_pp($self->{print}->placeholder_parser);
  1158. # start thread
  1159. @_ = ();
  1160. $self->{process_thread} = Slic3r::spawn_thread(sub {
  1161. eval {
  1162. $self->{print}->process;
  1163. };
  1164. my $event = Wx::CommandEvent->new($PROCESS_COMPLETED_EVENT);
  1165. if ($@) {
  1166. Slic3r::debugf "Background process error: $@\n";
  1167. $event->SetInt(0);
  1168. $event->SetString($@);
  1169. } else {
  1170. $event->SetInt(1);
  1171. }
  1172. Wx::PostEvent($self, $event);
  1173. Slic3r::thread_cleanup();
  1174. });
  1175. Slic3r::debugf "Background processing started.\n";
  1176. }
  1177. sub stop_background_process {
  1178. my ($self) = @_;
  1179. $self->{apply_config_timer}->Stop if defined $self->{apply_config_timer};
  1180. $self->statusbar->SetCancelCallback(undef);
  1181. $self->statusbar->StopBusy;
  1182. $self->statusbar->SetStatusText("");
  1183. $self->{toolpaths2D}->reload_print if $self->{toolpaths2D};
  1184. $self->{preview3D}->reload_print if $self->{preview3D};
  1185. if ($self->{process_thread}) {
  1186. Slic3r::debugf "Killing background process.\n";
  1187. Slic3r::kill_all_threads();
  1188. $self->{process_thread} = undef;
  1189. } else {
  1190. Slic3r::debugf "No background process running.\n";
  1191. }
  1192. # if there's an export process, kill that one as well
  1193. if ($self->{export_thread}) {
  1194. Slic3r::debugf "Killing background export process.\n";
  1195. Slic3r::kill_all_threads();
  1196. $self->{export_thread} = undef;
  1197. }
  1198. }
  1199. sub pause_background_process {
  1200. my ($self) = @_;
  1201. if ($self->{process_thread} || $self->{export_thread}) {
  1202. Slic3r::pause_all_threads();
  1203. return 1;
  1204. } elsif (defined $self->{apply_config_timer} && $self->{apply_config_timer}->IsRunning) {
  1205. $self->{apply_config_timer}->Stop;
  1206. return 1;
  1207. }
  1208. return 0;
  1209. }
  1210. sub resume_background_process {
  1211. my ($self) = @_;
  1212. if ($self->{process_thread} || $self->{export_thread}) {
  1213. Slic3r::resume_all_threads();
  1214. }
  1215. }
  1216. sub reslice {
  1217. # explicitly cancel a previous thread and start a new one.
  1218. my ($self) = @_;
  1219. # Don't reslice if export of G-code or sending to OctoPrint is running.
  1220. if (! defined($self->{export_gcode_output_file}) && ! defined($self->{send_gcode_file})) {
  1221. # Stop the background processing threads, stop the async update timer.
  1222. $self->stop_background_process;
  1223. # Rather perform one additional unnecessary update of the print object instead of skipping a pending async update.
  1224. $self->async_apply_config;
  1225. $self->statusbar->SetCancelCallback(sub {
  1226. $self->stop_background_process;
  1227. $self->statusbar->SetStatusText(L("Slicing cancelled"));
  1228. # this updates buttons status
  1229. $self->object_list_changed;
  1230. });
  1231. $self->start_background_process;
  1232. }
  1233. }
  1234. sub export_gcode {
  1235. my ($self, $output_file) = @_;
  1236. return if !@{$self->{objects}};
  1237. if ($self->{export_gcode_output_file}) {
  1238. Wx::MessageDialog->new($self, L("Another export job is currently running."), L('Error'), wxOK | wxICON_ERROR)->ShowModal;
  1239. return;
  1240. }
  1241. # if process is not running, validate config
  1242. # (we assume that if it is running, config is valid)
  1243. eval {
  1244. # this will throw errors if config is not valid
  1245. wxTheApp->{preset_bundle}->full_config->validate;
  1246. $self->{print}->validate;
  1247. };
  1248. Slic3r::GUI::catch_error($self) and return;
  1249. # apply config and validate print
  1250. my $config = wxTheApp->{preset_bundle}->full_config;
  1251. eval {
  1252. # this will throw errors if config is not valid
  1253. $config->validate;
  1254. $self->{print}->apply_config($config);
  1255. $self->{print}->validate;
  1256. };
  1257. Slic3r::GUI::catch_error($self) and return;
  1258. # Copy the names of active presets into the placeholder parser.
  1259. wxTheApp->{preset_bundle}->export_selections_pp($self->{print}->placeholder_parser);
  1260. # select output file
  1261. if ($output_file) {
  1262. $self->{export_gcode_output_file} = eval { $self->{print}->output_filepath($output_file) };
  1263. Slic3r::GUI::catch_error($self) and return;
  1264. } else {
  1265. my $default_output_file = eval { $self->{print}->output_filepath($main::opt{output} // '') };
  1266. Slic3r::GUI::catch_error($self) and return;
  1267. # If possible, remove accents from accented latin characters.
  1268. # This function is useful for generating file names to be processed by legacy firmwares.
  1269. $default_output_file = Slic3r::GUI::fold_utf8_to_ascii($default_output_file);
  1270. my $dlg = Wx::FileDialog->new($self, L('Save G-code file as:'),
  1271. wxTheApp->{app_config}->get_last_output_dir(dirname($default_output_file)),
  1272. basename($default_output_file), &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  1273. if ($dlg->ShowModal != wxID_OK) {
  1274. $dlg->Destroy;
  1275. return;
  1276. }
  1277. my $path = $dlg->GetPath;
  1278. wxTheApp->{app_config}->update_last_output_dir(dirname($path));
  1279. $self->{export_gcode_output_file} = $path;
  1280. $dlg->Destroy;
  1281. }
  1282. $self->statusbar->StartBusy;
  1283. $self->statusbar->SetCancelCallback(sub {
  1284. $self->stop_background_process;
  1285. $self->statusbar->SetStatusText(L("Export cancelled"));
  1286. $self->{export_gcode_output_file} = undef;
  1287. $self->{send_gcode_file} = undef;
  1288. # this updates buttons status
  1289. $self->object_list_changed;
  1290. });
  1291. # start background process, whose completion event handler
  1292. # will detect $self->{export_gcode_output_file} and proceed with export
  1293. $self->start_background_process;
  1294. # this updates buttons status
  1295. $self->object_list_changed;
  1296. return $self->{export_gcode_output_file};
  1297. }
  1298. # This gets called only if we have threads.
  1299. sub on_process_completed {
  1300. my ($self, $error) = @_;
  1301. $self->statusbar->SetCancelCallback(undef);
  1302. $self->statusbar->StopBusy;
  1303. $self->statusbar->SetStatusText($error // "");
  1304. Slic3r::debugf "Background processing completed.\n";
  1305. $self->{process_thread}->detach if $self->{process_thread};
  1306. $self->{process_thread} = undef;
  1307. # if we're supposed to perform an explicit export let's display the error in a dialog
  1308. if ($error && $self->{export_gcode_output_file}) {
  1309. $self->{export_gcode_output_file} = undef;
  1310. Slic3r::GUI::show_error($self, $error);
  1311. }
  1312. return if $error;
  1313. $self->{toolpaths2D}->reload_print if $self->{toolpaths2D};
  1314. $self->{preview3D}->reload_print if $self->{preview3D};
  1315. # in case this was MM print, wipe tower bounding box on 3D tab might need redrawing with exact depth:
  1316. my $selections = $self->collect_selections;
  1317. Slic3r::GUI::_3DScene::set_objects_selections($self->{canvas3D}, \@$selections);
  1318. Slic3r::GUI::_3DScene::reload_scene($self->{canvas3D}, 1);
  1319. # if we have an export filename, start a new thread for exporting G-code
  1320. if ($self->{export_gcode_output_file}) {
  1321. @_ = ();
  1322. # workaround for "Attempt to free un referenced scalar..."
  1323. our $_thread_self = $self;
  1324. $self->{export_thread} = Slic3r::spawn_thread(sub {
  1325. eval {
  1326. $_thread_self->{print}->export_gcode(output_file => $_thread_self->{export_gcode_output_file}, gcode_preview_data => $_thread_self->{gcode_preview_data});
  1327. };
  1328. my $export_completed_event = Wx::CommandEvent->new($EXPORT_COMPLETED_EVENT);
  1329. if ($@) {
  1330. {
  1331. my $error_event = Wx::CommandEvent->new($ERROR_EVENT);
  1332. $error_event->SetString($@);
  1333. Wx::PostEvent($_thread_self, $error_event);
  1334. }
  1335. $export_completed_event->SetInt(0);
  1336. $export_completed_event->SetString($@);
  1337. } else {
  1338. $export_completed_event->SetInt(1);
  1339. }
  1340. Wx::PostEvent($_thread_self, $export_completed_event);
  1341. Slic3r::thread_cleanup();
  1342. });
  1343. Slic3r::debugf "Background G-code export started.\n";
  1344. }
  1345. }
  1346. # This gets called also if we have no threads.
  1347. sub on_progress_event {
  1348. my ($self, $percent, $message) = @_;
  1349. $self->statusbar->SetProgress($percent);
  1350. $self->statusbar->SetStatusText("$message…");
  1351. }
  1352. # Called when the G-code export finishes, either successfully or with an error.
  1353. # This gets called also if we don't have threads.
  1354. sub on_export_completed {
  1355. my ($self, $result) = @_;
  1356. $self->statusbar->SetCancelCallback(undef);
  1357. $self->statusbar->StopBusy;
  1358. $self->statusbar->SetStatusText("");
  1359. Slic3r::debugf "Background export process completed.\n";
  1360. $self->{export_thread}->detach if $self->{export_thread};
  1361. $self->{export_thread} = undef;
  1362. my $message;
  1363. my $send_gcode = 0;
  1364. my $do_print = 0;
  1365. if ($result) {
  1366. # G-code file exported successfully.
  1367. if ($self->{print_file}) {
  1368. $message = L("File added to print queue");
  1369. $do_print = 1;
  1370. } elsif ($self->{send_gcode_file}) {
  1371. $message = L("Sending G-code file to the Printer Host ...");
  1372. $send_gcode = 1;
  1373. } else {
  1374. $message = L("G-code file exported to ") . $self->{export_gcode_output_file};
  1375. }
  1376. } else {
  1377. $message = L("Export failed");
  1378. }
  1379. $self->{export_gcode_output_file} = undef;
  1380. $self->statusbar->SetStatusText($message);
  1381. wxTheApp->notify($message);
  1382. $self->do_print if $do_print;
  1383. # Send $self->{send_gcode_file} to OctoPrint.
  1384. if ($send_gcode) {
  1385. my $host = Slic3r::PrintHost::get_print_host($self->{config});
  1386. if ($host->send_gcode($self->{send_gcode_file})) {
  1387. $self->statusbar->SetStatusText(L("Upload to host finished."));
  1388. } else {
  1389. $self->statusbar->SetStatusText("");
  1390. }
  1391. }
  1392. $self->{print_file} = undef;
  1393. $self->{send_gcode_file} = undef;
  1394. $self->print_info_box_show(1);
  1395. # this updates buttons status
  1396. $self->object_list_changed;
  1397. # refresh preview
  1398. $self->{toolpaths2D}->reload_print if $self->{toolpaths2D};
  1399. $self->{preview3D}->reload_print if $self->{preview3D};
  1400. }
  1401. # Fill in the "Sliced info" box with the result of the G-code generator.
  1402. sub print_info_box_show {
  1403. my ($self, $show) = @_;
  1404. my $scrolled_window_panel = $self->{scrolled_window_panel};
  1405. my $scrolled_window_sizer = $self->{scrolled_window_sizer};
  1406. return if (!$show && ($scrolled_window_sizer->IsShown(2) == $show));
  1407. if ($show) {
  1408. my $print_info_sizer = $self->{print_info_sizer};
  1409. $print_info_sizer->Clear(1);
  1410. my $grid_sizer = Wx::FlexGridSizer->new(2, 2, 5, 5);
  1411. $grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
  1412. $grid_sizer->AddGrowableCol(1, 1);
  1413. $grid_sizer->AddGrowableCol(3, 1);
  1414. $print_info_sizer->Add($grid_sizer, 0, wxEXPAND);
  1415. my @info = (
  1416. L("Used Filament (m)")
  1417. => sprintf("%.2f" , $self->{print}->total_used_filament / 1000),
  1418. L("Used Filament (mm³)")
  1419. => sprintf("%.2f" , $self->{print}->total_extruded_volume),
  1420. L("Used Filament (g)"),
  1421. => sprintf("%.2f" , $self->{print}->total_weight),
  1422. L("Cost"),
  1423. => sprintf("%.2f" , $self->{print}->total_cost),
  1424. L("Estimated printing time (normal mode)")
  1425. => $self->{print}->estimated_normal_print_time,
  1426. L("Estimated printing time (silent mode)")
  1427. => $self->{print}->estimated_silent_print_time
  1428. );
  1429. while ( my $label = shift @info) {
  1430. my $value = shift @info;
  1431. next if $value eq "N/A";
  1432. my $text = Wx::StaticText->new($scrolled_window_panel, -1, "$label:", wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
  1433. $text->SetFont($Slic3r::GUI::small_font);
  1434. $grid_sizer->Add($text, 0);
  1435. my $field = Wx::StaticText->new($scrolled_window_panel, -1, $value, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  1436. $field->SetFont($Slic3r::GUI::small_font);
  1437. $grid_sizer->Add($field, 0);
  1438. }
  1439. }
  1440. $scrolled_window_sizer->Show(2, $show);
  1441. $scrolled_window_panel->Layout;
  1442. }
  1443. sub do_print {
  1444. my ($self) = @_;
  1445. my $controller = $self->GetFrame->{controller};
  1446. my $printer_preset = wxTheApp->{preset_bundle}->printer->get_edited_preset;
  1447. my $printer_panel = $controller->add_printer($printer_preset->name, $printer_preset->config);
  1448. my $filament_stats = $self->{print}->filament_stats;
  1449. my $filament_names = wxTheApp->{preset_bundle}->filament_presets;
  1450. $filament_stats = { map { $filament_names->[$_] => $filament_stats->{$_} } keys %$filament_stats };
  1451. $printer_panel->load_print_job($self->{print_file}, $filament_stats);
  1452. }
  1453. sub export_stl {
  1454. my ($self) = @_;
  1455. return if !@{$self->{objects}};
  1456. # Ask user for a file name to write into.
  1457. my $output_file = $self->_get_export_file('STL') or return;
  1458. # Store a binary STL.
  1459. $self->{model}->store_stl($output_file, 1);
  1460. $self->statusbar->SetStatusText(L("STL file exported to ").$output_file);
  1461. }
  1462. sub reload_from_disk {
  1463. my ($self) = @_;
  1464. my ($obj_idx, $object) = $self->selected_object;
  1465. return if !defined $obj_idx;
  1466. my $model_object = $self->{model}->objects->[$obj_idx];
  1467. #FIXME convert to local file encoding
  1468. return if !$model_object->input_file
  1469. || !-e Slic3r::encode_path($model_object->input_file);
  1470. my @new_obj_idx = $self->load_files([$model_object->input_file]);
  1471. return if !@new_obj_idx;
  1472. foreach my $new_obj_idx (@new_obj_idx) {
  1473. my $o = $self->{model}->objects->[$new_obj_idx];
  1474. $o->clear_instances;
  1475. $o->add_instance($_) for @{$model_object->instances};
  1476. #$o->invalidate_bounding_box;
  1477. if ($o->volumes_count == $model_object->volumes_count) {
  1478. for my $i (0..($o->volumes_count-1)) {
  1479. $o->get_volume($i)->config->apply($model_object->get_volume($i)->config);
  1480. }
  1481. }
  1482. #FIXME restore volumes and their configs, layer_height_ranges, layer_height_profile, layer_height_profile_valid,
  1483. }
  1484. $self->remove($obj_idx);
  1485. }
  1486. sub export_object_stl {
  1487. my ($self) = @_;
  1488. my ($obj_idx, $object) = $self->selected_object;
  1489. return if !defined $obj_idx;
  1490. my $model_object = $self->{model}->objects->[$obj_idx];
  1491. # Ask user for a file name to write into.
  1492. my $output_file = $self->_get_export_file('STL') or return;
  1493. $model_object->mesh->write_binary($output_file);
  1494. $self->statusbar->SetStatusText(L("STL file exported to ").$output_file);
  1495. }
  1496. sub fix_through_netfabb {
  1497. my ($self) = @_;
  1498. my ($obj_idx, $object) = $self->selected_object;
  1499. return if !defined $obj_idx;
  1500. my $model_object = $self->{model}->objects->[$obj_idx];
  1501. my $model_fixed = Slic3r::Model->new;
  1502. Slic3r::GUI::fix_model_by_win10_sdk_gui($model_object, $self->{print}, $model_fixed);
  1503. my @new_obj_idx = $self->load_model_objects(@{$model_fixed->objects});
  1504. return if !@new_obj_idx;
  1505. foreach my $new_obj_idx (@new_obj_idx) {
  1506. my $o = $self->{model}->objects->[$new_obj_idx];
  1507. $o->clear_instances;
  1508. $o->add_instance($_) for @{$model_object->instances};
  1509. #$o->invalidate_bounding_box;
  1510. if ($o->volumes_count == $model_object->volumes_count) {
  1511. for my $i (0..($o->volumes_count-1)) {
  1512. $o->get_volume($i)->config->apply($model_object->get_volume($i)->config);
  1513. }
  1514. }
  1515. #FIXME restore volumes and their configs, layer_height_ranges, layer_height_profile, layer_height_profile_valid,
  1516. }
  1517. $self->remove($obj_idx);
  1518. }
  1519. sub export_amf {
  1520. my ($self) = @_;
  1521. return if !@{$self->{objects}};
  1522. # Ask user for a file name to write into.
  1523. my $output_file = $self->_get_export_file('AMF') or return;
  1524. my $res = $self->{model}->store_amf($output_file, $self->{print}, $self->{export_option});
  1525. if ($res)
  1526. {
  1527. $self->statusbar->SetStatusText(L("AMF file exported to ").$output_file);
  1528. }
  1529. else
  1530. {
  1531. $self->statusbar->SetStatusText(L("Error exporting AMF file ").$output_file);
  1532. }
  1533. }
  1534. sub export_3mf {
  1535. my ($self) = @_;
  1536. return if !@{$self->{objects}};
  1537. # Ask user for a file name to write into.
  1538. my $output_file = $self->_get_export_file('3MF') or return;
  1539. my $res = $self->{model}->store_3mf($output_file, $self->{print}, $self->{export_option});
  1540. if ($res)
  1541. {
  1542. $self->statusbar->SetStatusText(L("3MF file exported to ").$output_file);
  1543. }
  1544. else
  1545. {
  1546. $self->statusbar->SetStatusText(L("Error exporting 3MF file ").$output_file);
  1547. }
  1548. }
  1549. # Ask user to select an output file for a given file format (STl, AMF, 3MF).
  1550. # Propose a default file name based on the 'output_filename_format' configuration value.
  1551. sub _get_export_file {
  1552. my ($self, $format) = @_;
  1553. my $suffix = '';
  1554. my $wildcard = 'known';
  1555. if ($format eq 'STL')
  1556. {
  1557. $suffix = '.stl';
  1558. $wildcard = 'stl';
  1559. }
  1560. elsif ($format eq 'AMF')
  1561. {
  1562. if (&Wx::wxMAC) {
  1563. # It seems that MacOS does not like double extension
  1564. $suffix = '.amf';
  1565. } else {
  1566. $suffix = '.zip.amf';
  1567. }
  1568. $wildcard = 'amf';
  1569. }
  1570. elsif ($format eq '3MF')
  1571. {
  1572. $suffix = '.3mf';
  1573. $wildcard = 'threemf';
  1574. }
  1575. # Copy the names of active presets into the placeholder parser.
  1576. wxTheApp->{preset_bundle}->export_selections_pp($self->{print}->placeholder_parser);
  1577. my $output_file = eval { $self->{print}->output_filepath($main::opt{output} // '') };
  1578. Slic3r::GUI::catch_error($self) and return undef;
  1579. $output_file =~ s/\.[gG][cC][oO][dD][eE]$/$suffix/;
  1580. my $dlg = Wx::FileDialog->new($self, L("Save ").$format.L(" file as:"), dirname($output_file),
  1581. basename($output_file), &Slic3r::GUI::FILE_WILDCARDS->{$wildcard}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  1582. Slic3r::GUI::add_export_option($dlg, $format);
  1583. if ($dlg->ShowModal != wxID_OK) {
  1584. $dlg->Destroy;
  1585. return undef;
  1586. }
  1587. $output_file = $dlg->GetPath;
  1588. $self->{export_option} = Slic3r::GUI::get_export_option($dlg);
  1589. $dlg->Destroy;
  1590. return $output_file;
  1591. }
  1592. #sub reset_thumbnail {
  1593. # my ($self, $obj_idx) = @_;
  1594. # $self->{objects}[$obj_idx]->thumbnail(undef);
  1595. #}
  1596. # this method gets called whenever print center is changed or the objects' bounding box changes
  1597. # (i.e. when an object is added/removed/moved/rotated/scaled)
  1598. sub update {
  1599. my ($self, $force_autocenter) = @_;
  1600. if (wxTheApp->{app_config}->get("autocenter") || $force_autocenter) {
  1601. $self->{model}->center_instances_around_point($self->bed_centerf);
  1602. }
  1603. my $running = $self->pause_background_process;
  1604. my $invalidated = $self->{print}->reload_model_instances();
  1605. # The mere fact that no steps were invalidated when reloading model instances
  1606. # doesn't mean that all steps were done: for example, validation might have
  1607. # failed upon previous instance move, so we have no running thread and no steps
  1608. # are invalidated on this move, thus we need to schedule a new run.
  1609. if ($invalidated || !$running) {
  1610. $self->schedule_background_process;
  1611. } else {
  1612. $self->resume_background_process;
  1613. }
  1614. $self->print_info_box_show(0);
  1615. # $self->{canvas}->reload_scene if $self->{canvas};
  1616. my $selections = $self->collect_selections;
  1617. Slic3r::GUI::_3DScene::set_objects_selections($self->{canvas3D}, \@$selections);
  1618. Slic3r::GUI::_3DScene::reload_scene($self->{canvas3D}, 0);
  1619. $self->{preview3D}->reset_gcode_preview_data if $self->{preview3D};
  1620. $self->{preview3D}->reload_print if $self->{preview3D};
  1621. }
  1622. # When a number of extruders changes, the UI needs to be updated to show a single filament selection combo box per extruder.
  1623. # Also the wxTheApp->{preset_bundle}->filament_presets needs to be resized accordingly
  1624. # and some reasonable default has to be selected for the additional extruders.
  1625. sub on_extruders_change {
  1626. my ($self, $num_extruders) = @_;
  1627. my $choices = $self->{preset_choosers}{filament};
  1628. while (int(@$choices) < $num_extruders) {
  1629. # copy strings from first choice
  1630. my @presets = $choices->[0]->GetStrings;
  1631. # initialize new choice
  1632. my $choice = Wx::BitmapComboBox->new($self->{right_panel}, -1, "", wxDefaultPosition, wxDefaultSize, [@presets], wxCB_READONLY);
  1633. my $extruder_idx = scalar @$choices;
  1634. EVT_LEFT_DOWN($choice, sub { $self->filament_color_box_lmouse_down($extruder_idx, @_); } );
  1635. push @$choices, $choice;
  1636. # copy icons from first choice
  1637. $choice->SetItemBitmap($_, $choices->[0]->GetItemBitmap($_)) for 0..$#presets;
  1638. # insert new choice into sizer
  1639. $self->{presets_sizer}->Insert(4 + ($#$choices-1)*2, 0, 0);
  1640. $self->{presets_sizer}->Insert(5 + ($#$choices-1)*2, $choice, 0, wxEXPAND | wxBOTTOM, FILAMENT_CHOOSERS_SPACING);
  1641. # setup the listener
  1642. EVT_COMBOBOX($choice, $choice, sub {
  1643. my ($choice) = @_;
  1644. wxTheApp->CallAfter(sub {
  1645. $self->_on_select_preset('filament', $choice, $extruder_idx);
  1646. });
  1647. });
  1648. # initialize selection
  1649. wxTheApp->{preset_bundle}->update_platter_filament_ui($extruder_idx, $choice);
  1650. }
  1651. # remove unused choices if any
  1652. while (@$choices > $num_extruders) {
  1653. $self->{presets_sizer}->Remove(4 + ($#$choices-1)*2); # label
  1654. $self->{presets_sizer}->Remove(4 + ($#$choices-1)*2); # wxChoice
  1655. $choices->[-1]->Destroy;
  1656. pop @$choices;
  1657. }
  1658. $self->Layout;
  1659. }
  1660. sub on_config_change {
  1661. my ($self, $config) = @_;
  1662. my $update_scheduled;
  1663. foreach my $opt_key (@{$self->{config}->diff($config)}) {
  1664. $self->{config}->set($opt_key, $config->get($opt_key));
  1665. if ($opt_key eq 'bed_shape') {
  1666. # $self->{canvas}->update_bed_size;
  1667. Slic3r::GUI::_3DScene::set_bed_shape($self->{canvas3D}, $self->{config}->bed_shape) if $self->{canvas3D};
  1668. Slic3r::GUI::_3DScene::set_bed_shape($self->{preview3D}->canvas, $self->{config}->bed_shape) if $self->{preview3D};
  1669. $update_scheduled = 1;
  1670. } elsif ($opt_key =~ '^wipe_tower' || $opt_key eq 'single_extruder_multi_material') {
  1671. $update_scheduled = 1;
  1672. } elsif ($opt_key eq 'serial_port') {
  1673. $self->{btn_print}->Show($config->get('serial_port'));
  1674. $self->Layout;
  1675. } elsif ($opt_key eq 'print_host') {
  1676. $self->{btn_send_gcode}->Show($config->get('print_host'));
  1677. $self->Layout;
  1678. } elsif ($opt_key eq 'variable_layer_height') {
  1679. if ($config->get('variable_layer_height') != 1) {
  1680. if ($self->{htoolbar}) {
  1681. $self->{htoolbar}->EnableTool(TB_LAYER_EDITING, 0);
  1682. $self->{htoolbar}->ToggleTool(TB_LAYER_EDITING, 0);
  1683. } else {
  1684. $self->{"btn_layer_editing"}->Disable;
  1685. $self->{"btn_layer_editing"}->SetValue(0);
  1686. }
  1687. Slic3r::GUI::_3DScene::enable_layers_editing($self->{canvas3D}, 0);
  1688. $self->{canvas3D}->Refresh;
  1689. $self->{canvas3D}->Update;
  1690. } elsif (Slic3r::GUI::_3DScene::is_layers_editing_allowed($self->{canvas3D})) {
  1691. # Want to allow the layer editing, but do it only if the OpenGL supports it.
  1692. if ($self->{htoolbar}) {
  1693. $self->{htoolbar}->EnableTool(TB_LAYER_EDITING, 1);
  1694. } else {
  1695. $self->{"btn_layer_editing"}->Enable;
  1696. }
  1697. }
  1698. } elsif ($opt_key eq 'extruder_colour') {
  1699. $update_scheduled = 1;
  1700. my $extruder_colors = $config->get('extruder_colour');
  1701. $self->{preview3D}->set_number_extruders(scalar(@{$extruder_colors}));
  1702. } elsif ($opt_key eq 'max_print_height') {
  1703. $update_scheduled = 1;
  1704. } elsif ($opt_key eq 'printer_model') {
  1705. # update to force bed selection (for texturing)
  1706. Slic3r::GUI::_3DScene::set_bed_shape($self->{canvas3D}, $self->{config}->bed_shape) if $self->{canvas3D};
  1707. Slic3r::GUI::_3DScene::set_bed_shape($self->{preview3D}->canvas, $self->{config}->bed_shape) if $self->{preview3D};
  1708. $update_scheduled = 1;
  1709. }
  1710. }
  1711. $self->update if $update_scheduled;
  1712. return if !$self->GetFrame->is_loaded;
  1713. # (re)start timer
  1714. $self->schedule_background_process;
  1715. }
  1716. sub list_item_deselected {
  1717. my ($self, $event) = @_;
  1718. return if $PreventListEvents;
  1719. $self->{_lecursor} = Wx::BusyCursor->new();
  1720. if ($self->{list}->GetFirstSelected == -1) {
  1721. $self->select_object(undef);
  1722. # $self->{canvas}->Refresh;
  1723. Slic3r::GUI::_3DScene::deselect_volumes($self->{canvas3D}) if $self->{canvas3D};
  1724. Slic3r::GUI::_3DScene::render($self->{canvas3D}) if $self->{canvas3D};
  1725. }
  1726. undef $self->{_lecursor};
  1727. }
  1728. sub list_item_selected {
  1729. my ($self, $event) = @_;
  1730. return if $PreventListEvents;
  1731. $self->{_lecursor} = Wx::BusyCursor->new();
  1732. my $obj_idx = $event->GetIndex;
  1733. $self->select_object($obj_idx);
  1734. # $self->{canvas}->Refresh;
  1735. if ($self->{canvas3D}) {
  1736. my $selections = $self->collect_selections;
  1737. Slic3r::GUI::_3DScene::update_volumes_selection($self->{canvas3D}, \@$selections);
  1738. Slic3r::GUI::_3DScene::render($self->{canvas3D});
  1739. }
  1740. undef $self->{_lecursor};
  1741. }
  1742. sub collect_selections {
  1743. my ($self) = @_;
  1744. my $selections = [];
  1745. foreach my $o (@{$self->{objects}}) {
  1746. push(@$selections, $o->selected);
  1747. }
  1748. return $selections;
  1749. }
  1750. sub list_item_activated {
  1751. my ($self, $event, $obj_idx) = @_;
  1752. $obj_idx //= $event->GetIndex;
  1753. $self->object_settings_dialog($obj_idx);
  1754. }
  1755. # Called when clicked on the filament preset combo box.
  1756. # When clicked on the icon, show the color picker.
  1757. sub filament_color_box_lmouse_down
  1758. {
  1759. my ($self, $extruder_idx, $combobox, $event) = @_;
  1760. my $pos = $event->GetLogicalPosition(Wx::ClientDC->new($combobox));
  1761. my( $x, $y ) = ( $pos->x, $pos->y );
  1762. if ($x > 24) {
  1763. # Let the combo box process the mouse click.
  1764. $event->Skip;
  1765. } else {
  1766. # Swallow the mouse click and open the color picker.
  1767. my $data = Wx::ColourData->new;
  1768. $data->SetChooseFull(1);
  1769. my $dialog = Wx::ColourDialog->new($self->GetFrame, $data);
  1770. if ($dialog->ShowModal == wxID_OK) {
  1771. my $cfg = Slic3r::Config->new;
  1772. my $colors = wxTheApp->{preset_bundle}->full_config->get('extruder_colour');
  1773. $colors->[$extruder_idx] = $dialog->GetColourData->GetColour->GetAsString(wxC2S_HTML_SYNTAX);
  1774. $cfg->set('extruder_colour', $colors);
  1775. $self->GetFrame->{options_tabs}{printer}->load_config($cfg);
  1776. wxTheApp->{preset_bundle}->update_platter_filament_ui($extruder_idx, $combobox);
  1777. }
  1778. $dialog->Destroy();
  1779. }
  1780. }
  1781. sub object_cut_dialog {
  1782. my ($self, $obj_idx) = @_;
  1783. if (!defined $obj_idx) {
  1784. ($obj_idx, undef) = $self->selected_object;
  1785. }
  1786. if (!$Slic3r::GUI::have_OpenGL) {
  1787. Slic3r::GUI::show_error($self, L("Please install the OpenGL modules to use this feature (see build instructions)."));
  1788. return;
  1789. }
  1790. my $dlg = Slic3r::GUI::Plater::ObjectCutDialog->new($self,
  1791. object => $self->{objects}[$obj_idx],
  1792. model_object => $self->{model}->objects->[$obj_idx],
  1793. );
  1794. return unless $dlg->ShowModal == wxID_OK;
  1795. if (my @new_objects = $dlg->NewModelObjects) {
  1796. $self->remove($obj_idx);
  1797. $self->load_model_objects(grep defined($_), @new_objects);
  1798. $self->arrange;
  1799. Slic3r::GUI::_3DScene::zoom_to_volumes($self->{canvas3D}) if $self->{canvas3D};
  1800. }
  1801. }
  1802. sub object_settings_dialog {
  1803. my ($self, $obj_idx) = @_;
  1804. ($obj_idx, undef) = $self->selected_object if !defined $obj_idx;
  1805. my $model_object = $self->{model}->objects->[$obj_idx];
  1806. # validate config before opening the settings dialog because
  1807. # that dialog can't be closed if validation fails, but user
  1808. # can't fix any error which is outside that dialog
  1809. eval { wxTheApp->{preset_bundle}->full_config->validate; };
  1810. return if Slic3r::GUI::catch_error($_[0]);
  1811. my $dlg = Slic3r::GUI::Plater::ObjectSettingsDialog->new($self,
  1812. object => $self->{objects}[$obj_idx],
  1813. model_object => $model_object,
  1814. config => wxTheApp->{preset_bundle}->full_config,
  1815. );
  1816. $self->pause_background_process;
  1817. $dlg->ShowModal;
  1818. # # update thumbnail since parts may have changed
  1819. # if ($dlg->PartsChanged) {
  1820. # # recenter and re-align to Z = 0
  1821. # $model_object->center_around_origin;
  1822. # $self->reset_thumbnail($obj_idx);
  1823. # }
  1824. # update print
  1825. if ($dlg->PartsChanged || $dlg->PartSettingsChanged) {
  1826. $self->stop_background_process;
  1827. $self->{print}->reload_object($obj_idx);
  1828. $self->schedule_background_process;
  1829. # $self->{canvas}->reload_scene if $self->{canvas};
  1830. my $selections = $self->collect_selections;
  1831. Slic3r::GUI::_3DScene::set_objects_selections($self->{canvas3D}, \@$selections);
  1832. Slic3r::GUI::_3DScene::reload_scene($self->{canvas3D}, 0);
  1833. } else {
  1834. $self->resume_background_process;
  1835. }
  1836. }
  1837. # Called to update various buttons depending on whether there are any objects or
  1838. # whether background processing (export of a G-code, sending to Octoprint, forced background re-slicing) is active.
  1839. sub object_list_changed {
  1840. my $self = shift;
  1841. # Enable/disable buttons depending on whether there are any objects on the platter.
  1842. my $have_objects = @{$self->{objects}} ? 1 : 0;
  1843. my $variable_layer_height_allowed = $self->{config}->variable_layer_height && Slic3r::GUI::_3DScene::is_layers_editing_allowed($self->{canvas3D});
  1844. if ($self->{htoolbar}) {
  1845. # On OSX or Linux
  1846. $self->{htoolbar}->EnableTool($_, $have_objects)
  1847. for (TB_RESET, TB_ARRANGE, TB_LAYER_EDITING);
  1848. $self->{htoolbar}->EnableTool(TB_LAYER_EDITING, 0) if (! $variable_layer_height_allowed);
  1849. } else {
  1850. # On MSW
  1851. my $method = $have_objects ? 'Enable' : 'Disable';
  1852. $self->{"btn_$_"}->$method
  1853. for grep $self->{"btn_$_"}, qw(reset arrange reslice export_gcode export_stl print send_gcode layer_editing);
  1854. $self->{"btn_layer_editing"}->Disable if (! $variable_layer_height_allowed);
  1855. }
  1856. my $export_in_progress = $self->{export_gcode_output_file} || $self->{send_gcode_file};
  1857. my $model_fits = $self->{canvas3D} ? Slic3r::GUI::_3DScene::check_volumes_outside_state($self->{canvas3D}, $self->{config}) : 1;
  1858. # $model_fits == 1 -> ModelInstance::PVS_Partly_Outside
  1859. my $method = ($have_objects && ! $export_in_progress && ($model_fits != 1)) ? 'Enable' : 'Disable';
  1860. $self->{"btn_$_"}->$method
  1861. for grep $self->{"btn_$_"}, qw(reslice export_gcode print send_gcode);
  1862. }
  1863. # Selection of an active 3D object changed.
  1864. sub selection_changed {
  1865. my ($self) = @_;
  1866. my ($obj_idx, $object) = $self->selected_object;
  1867. my $have_sel = defined $obj_idx;
  1868. $self->{right_panel}->Freeze;
  1869. if ($self->{htoolbar}) {
  1870. # On OSX or Linux
  1871. $self->{htoolbar}->EnableTool($_, $have_sel)
  1872. for (TB_REMOVE, TB_MORE, TB_FEWER, TB_45CW, TB_45CCW, TB_SCALE, TB_SPLIT, TB_CUT, TB_SETTINGS);
  1873. } else {
  1874. # On MSW
  1875. my $method = $have_sel ? 'Enable' : 'Disable';
  1876. $self->{"btn_$_"}->$method
  1877. for grep $self->{"btn_$_"}, qw(remove increase decrease rotate45cw rotate45ccw changescale split cut settings);
  1878. }
  1879. if ($self->{object_info_size}) { # have we already loaded the info pane?
  1880. if ($have_sel) {
  1881. my $model_object = $self->{model}->objects->[$obj_idx];
  1882. #FIXME print_info runs model fixing in two rounds, it is very slow, it should not be performed here!
  1883. # $model_object->print_info;
  1884. my $model_instance = $model_object->instances->[0];
  1885. $self->{object_info_size}->SetLabel(sprintf("%.2f x %.2f x %.2f", @{$model_object->instance_bounding_box(0)->size}));
  1886. $self->{object_info_materials}->SetLabel($model_object->materials_count);
  1887. if (my $stats = $model_object->mesh_stats) {
  1888. $self->{object_info_volume}->SetLabel(sprintf('%.2f', $stats->{volume} * ($model_instance->scaling_factor**3)));
  1889. $self->{object_info_facets}->SetLabel(sprintf(L('%d (%d shells)'), $model_object->facets_count, $stats->{number_of_parts}));
  1890. if (my $errors = sum(@$stats{qw(degenerate_facets edges_fixed facets_removed facets_added facets_reversed backwards_edges)})) {
  1891. $self->{object_info_manifold}->SetLabel(sprintf(L("Auto-repaired (%d errors)"), $errors));
  1892. $self->{object_info_manifold_warning_icon}->Show;
  1893. # we don't show normals_fixed because we never provide normals
  1894. # to admesh, so it generates normals for all facets
  1895. my $message = sprintf L('%d degenerate facets, %d edges fixed, %d facets removed, %d facets added, %d facets reversed, %d backwards edges'),
  1896. @$stats{qw(degenerate_facets edges_fixed facets_removed facets_added facets_reversed backwards_edges)};
  1897. $self->{object_info_manifold}->SetToolTipString($message);
  1898. $self->{object_info_manifold_warning_icon}->SetToolTipString($message);
  1899. } else {
  1900. $self->{object_info_manifold}->SetLabel(L("Yes"));
  1901. $self->{object_info_manifold_warning_icon}->Hide;
  1902. $self->{object_info_manifold}->SetToolTipString("");
  1903. $self->{object_info_manifold_warning_icon}->SetToolTipString("");
  1904. }
  1905. } else {
  1906. $self->{object_info_facets}->SetLabel($object->facets);
  1907. }
  1908. } else {
  1909. $self->{"object_info_$_"}->SetLabel("") for qw(size volume facets materials manifold);
  1910. $self->{object_info_manifold_warning_icon}->Hide;
  1911. $self->{object_info_manifold}->SetToolTipString("");
  1912. $self->{object_info_manifold_warning_icon}->SetToolTipString("");
  1913. }
  1914. $self->Layout;
  1915. }
  1916. # prepagate the event to the frame (a custom Wx event would be cleaner)
  1917. $self->GetFrame->on_plater_selection_changed($have_sel);
  1918. $self->{right_panel}->Thaw;
  1919. }
  1920. sub select_object {
  1921. my ($self, $obj_idx) = @_;
  1922. # remove current selection
  1923. foreach my $o (0..$#{$self->{objects}}) {
  1924. $PreventListEvents = 1;
  1925. $self->{objects}->[$o]->selected(0);
  1926. $self->{list}->Select($o, 0);
  1927. $PreventListEvents = 0;
  1928. }
  1929. if (defined $obj_idx) {
  1930. $self->{objects}->[$obj_idx]->selected(1);
  1931. # We use this flag to avoid circular event handling
  1932. # Select() happens to fire a wxEVT_LIST_ITEM_SELECTED on Windows,
  1933. # whose event handler calls this method again and again and again
  1934. $PreventListEvents = 1;
  1935. $self->{list}->Select($obj_idx, 1);
  1936. $PreventListEvents = 0;
  1937. } else {
  1938. # TODO: deselect all in list
  1939. }
  1940. $self->selection_changed(1);
  1941. }
  1942. sub selected_object {
  1943. my ($self) = @_;
  1944. my $obj_idx = first { $self->{objects}[$_]->selected } 0..$#{ $self->{objects} };
  1945. return defined $obj_idx ? ($obj_idx, $self->{objects}[$obj_idx]) : undef;
  1946. }
  1947. sub statusbar {
  1948. return $_[0]->GetFrame->{statusbar};
  1949. }
  1950. sub object_menu {
  1951. my ($self) = @_;
  1952. my $frame = $self->GetFrame;
  1953. my $menu = Wx::Menu->new;
  1954. my $accel = ($^O eq 'MSWin32') ? sub { $_[0] . "\t\xA0" . $_[1] } : sub { $_[0] };
  1955. $frame->_append_menu_item($menu, $accel->(L('Delete'), 'Del'), L('Remove the selected object'), sub {
  1956. $self->remove;
  1957. }, undef, 'brick_delete.png');
  1958. $frame->_append_menu_item($menu, $accel->(L('Increase copies'), '+'), L('Place one more copy of the selected object'), sub {
  1959. $self->increase;
  1960. }, undef, 'add.png');
  1961. $frame->_append_menu_item($menu, $accel->(L('Decrease copies'), '-'), L('Remove one copy of the selected object'), sub {
  1962. $self->decrease;
  1963. }, undef, 'delete.png');
  1964. $frame->_append_menu_item($menu, L("Set number of copies…"), L('Change the number of copies of the selected object'), sub {
  1965. $self->set_number_of_copies;
  1966. }, undef, 'textfield.png');
  1967. $menu->AppendSeparator();
  1968. $frame->_append_menu_item($menu, $accel->(L('Rotate 45° clockwise'), 'l'), L('Rotate the selected object by 45° clockwise'), sub {
  1969. $self->rotate(-45, Z, 'relative');
  1970. }, undef, 'arrow_rotate_clockwise.png');
  1971. $frame->_append_menu_item($menu, $accel->(L('Rotate 45° counter-clockwise'), 'r'), L('Rotate the selected object by 45° counter-clockwise'), sub {
  1972. $self->rotate(+45, Z, 'relative');
  1973. }, undef, 'arrow_rotate_anticlockwise.png');
  1974. my $rotateMenu = Wx::Menu->new;
  1975. my $rotateMenuItem = $menu->AppendSubMenu($rotateMenu, L("Rotate"), L('Rotate the selected object by an arbitrary angle'));
  1976. $frame->_set_menu_item_icon($rotateMenuItem, 'textfield.png');
  1977. $frame->_append_menu_item($rotateMenu, L("Around X axis…"), L('Rotate the selected object by an arbitrary angle around X axis'), sub {
  1978. $self->rotate(undef, X);
  1979. }, undef, 'bullet_red.png');
  1980. $frame->_append_menu_item($rotateMenu, L("Around Y axis…"), L('Rotate the selected object by an arbitrary angle around Y axis'), sub {
  1981. $self->rotate(undef, Y);
  1982. }, undef, 'bullet_green.png');
  1983. $frame->_append_menu_item($rotateMenu, L("Around Z axis…"), L('Rotate the selected object by an arbitrary angle around Z axis'), sub {
  1984. $self->rotate(undef, Z);
  1985. }, undef, 'bullet_blue.png');
  1986. my $mirrorMenu = Wx::Menu->new;
  1987. my $mirrorMenuItem = $menu->AppendSubMenu($mirrorMenu, L("Mirror"), L('Mirror the selected object'));
  1988. $frame->_set_menu_item_icon($mirrorMenuItem, 'shape_flip_horizontal.png');
  1989. $frame->_append_menu_item($mirrorMenu, L("Along X axis…"), L('Mirror the selected object along the X axis'), sub {
  1990. $self->mirror(X);
  1991. }, undef, 'bullet_red.png');
  1992. $frame->_append_menu_item($mirrorMenu, L("Along Y axis…"), L('Mirror the selected object along the Y axis'), sub {
  1993. $self->mirror(Y);
  1994. }, undef, 'bullet_green.png');
  1995. $frame->_append_menu_item($mirrorMenu, L("Along Z axis…"), L('Mirror the selected object along the Z axis'), sub {
  1996. $self->mirror(Z);
  1997. }, undef, 'bullet_blue.png');
  1998. my $scaleMenu = Wx::Menu->new;
  1999. my $scaleMenuItem = $menu->AppendSubMenu($scaleMenu, L("Scale"), L('Scale the selected object along a single axis'));
  2000. $frame->_set_menu_item_icon($scaleMenuItem, 'arrow_out.png');
  2001. $frame->_append_menu_item($scaleMenu, $accel->(L('Uniformly…'), 's'), L('Scale the selected object along the XYZ axes'), sub {
  2002. $self->changescale(undef);
  2003. });
  2004. $frame->_append_menu_item($scaleMenu, L("Along X axis…"), L('Scale the selected object along the X axis'), sub {
  2005. $self->changescale(X);
  2006. }, undef, 'bullet_red.png');
  2007. $frame->_append_menu_item($scaleMenu, L("Along Y axis…"), L('Scale the selected object along the Y axis'), sub {
  2008. $self->changescale(Y);
  2009. }, undef, 'bullet_green.png');
  2010. $frame->_append_menu_item($scaleMenu, L("Along Z axis…"), L('Scale the selected object along the Z axis'), sub {
  2011. $self->changescale(Z);
  2012. }, undef, 'bullet_blue.png');
  2013. my $scaleToSizeMenu = Wx::Menu->new;
  2014. my $scaleToSizeMenuItem = $menu->AppendSubMenu($scaleToSizeMenu, L("Scale to size"), L('Scale the selected object along a single axis'));
  2015. $frame->_set_menu_item_icon($scaleToSizeMenuItem, 'arrow_out.png');
  2016. $frame->_append_menu_item($scaleToSizeMenu, L("Uniformly…"), L('Scale the selected object along the XYZ axes'), sub {
  2017. $self->changescale(undef, 1);
  2018. });
  2019. $frame->_append_menu_item($scaleToSizeMenu, L("Along X axis…"), L('Scale the selected object along the X axis'), sub {
  2020. $self->changescale(X, 1);
  2021. }, undef, 'bullet_red.png');
  2022. $frame->_append_menu_item($scaleToSizeMenu, L("Along Y axis…"), L('Scale the selected object along the Y axis'), sub {
  2023. $self->changescale(Y, 1);
  2024. }, undef, 'bullet_green.png');
  2025. $frame->_append_menu_item($scaleToSizeMenu, L("Along Z axis…"), L('Scale the selected object along the Z axis'), sub {
  2026. $self->changescale(Z, 1);
  2027. }, undef, 'bullet_blue.png');
  2028. $frame->_append_menu_item($menu, L("Split"), L('Split the selected object into individual parts'), sub {
  2029. $self->split_object;
  2030. }, undef, 'shape_ungroup.png');
  2031. $frame->_append_menu_item($menu, L("Cut…"), L('Open the 3D cutting tool'), sub {
  2032. $self->object_cut_dialog;
  2033. }, undef, 'package.png');
  2034. $menu->AppendSeparator();
  2035. $frame->_append_menu_item($menu, L("Settings…"), L('Open the object editor dialog'), sub {
  2036. $self->object_settings_dialog;
  2037. }, undef, 'cog.png');
  2038. $menu->AppendSeparator();
  2039. $frame->_append_menu_item($menu, L("Reload from Disk"), L('Reload the selected file from Disk'), sub {
  2040. $self->reload_from_disk;
  2041. }, undef, 'arrow_refresh.png');
  2042. $frame->_append_menu_item($menu, L("Export object as STL…"), L('Export this single object as STL file'), sub {
  2043. $self->export_object_stl;
  2044. }, undef, 'brick_go.png');
  2045. if (Slic3r::GUI::is_windows10) {
  2046. $frame->_append_menu_item($menu, L("Fix STL through Netfabb"), L('Fix the model by sending it to a Netfabb cloud service through Windows 10 API'), sub {
  2047. $self->fix_through_netfabb;
  2048. }, undef, 'brick_go.png');
  2049. }
  2050. return $menu;
  2051. }
  2052. # Set a camera direction, zoom to all objects.
  2053. sub select_view {
  2054. my ($self, $direction) = @_;
  2055. my $idx_page = $self->{preview_notebook}->GetSelection;
  2056. my $page = ($idx_page == &Wx::wxNOT_FOUND) ? L('3D') : $self->{preview_notebook}->GetPageText($idx_page);
  2057. if ($page eq L('Preview')) {
  2058. Slic3r::GUI::_3DScene::select_view($self->{preview3D}->canvas, $direction);
  2059. Slic3r::GUI::_3DScene::set_viewport_from_scene($self->{canvas3D}, $self->{preview3D}->canvas);
  2060. } else {
  2061. Slic3r::GUI::_3DScene::select_view($self->{canvas3D}, $direction);
  2062. Slic3r::GUI::_3DScene::set_viewport_from_scene($self->{preview3D}->canvas, $self->{canvas3D});
  2063. }
  2064. }
  2065. package Slic3r::GUI::Plater::DropTarget;
  2066. use Wx::DND;
  2067. use base 'Wx::FileDropTarget';
  2068. sub new {
  2069. my ($class, $window) = @_;
  2070. my $self = $class->SUPER::new;
  2071. $self->{window} = $window;
  2072. return $self;
  2073. }
  2074. sub OnDropFiles {
  2075. my ($self, $x, $y, $filenames) = @_;
  2076. # stop scalars leaking on older perl
  2077. # https://rt.perl.org/rt3/Public/Bug/Display.html?id=70602
  2078. @_ = ();
  2079. # only accept STL, OBJ, AMF, 3MF and PRUSA files
  2080. return 0 if grep !/\.(?:[sS][tT][lL]|[oO][bB][jJ]|[aA][mM][fF]|[3][mM][fF]|[aA][mM][fF].[xX][mM][lL]|[zZ][iI][pP].[aA][mM][lL]|[pP][rR][uU][sS][aA])$/, @$filenames;
  2081. $self->{window}->load_files($filenames);
  2082. }
  2083. # 2D preview of an object. Each object is previewed by its convex hull.
  2084. package Slic3r::GUI::Plater::Object;
  2085. use Moo;
  2086. has 'name' => (is => 'rw', required => 1);
  2087. #has 'thumbnail' => (is => 'rw'); # ExPolygon::Collection in scaled model units with no transforms
  2088. #has 'transformed_thumbnail' => (is => 'rw');
  2089. #has 'instance_thumbnails' => (is => 'ro', default => sub { [] }); # array of ExPolygon::Collection objects, each one representing the actual placed thumbnail of each instance in pixel units
  2090. has 'selected' => (is => 'rw', default => sub { 0 });
  2091. #sub make_thumbnail {
  2092. # my ($self, $model, $obj_idx) = @_;
  2093. # # make method idempotent
  2094. # $self->thumbnail->clear;
  2095. # # raw_mesh is the non-transformed (non-rotated, non-scaled, non-translated) sum of non-modifier object volumes.
  2096. # my $mesh = $model->objects->[$obj_idx]->raw_mesh;
  2097. ##FIXME The "correct" variant could be extremely slow.
  2098. ## if ($mesh->facets_count <= 5000) {
  2099. ## # remove polygons with area <= 1mm
  2100. ## my $area_threshold = Slic3r::Geometry::scale 1;
  2101. ## $self->thumbnail->append(
  2102. ## grep $_->area >= $area_threshold,
  2103. ## @{ $mesh->horizontal_projection }, # horizontal_projection returns scaled expolygons
  2104. ## );
  2105. ## $self->thumbnail->simplify(0.5);
  2106. ## } else {
  2107. # my $convex_hull = Slic3r::ExPolygon->new($mesh->convex_hull);
  2108. # $self->thumbnail->append($convex_hull);
  2109. ## }
  2110. # return $self->thumbnail;
  2111. #}
  2112. #
  2113. #sub transform_thumbnail {
  2114. # my ($self, $model, $obj_idx) = @_;
  2115. #
  2116. # return unless defined $self->thumbnail;
  2117. #
  2118. # my $model_object = $model->objects->[$obj_idx];
  2119. # my $model_instance = $model_object->instances->[0];
  2120. #
  2121. # # the order of these transformations MUST be the same everywhere, including
  2122. # # in Slic3r::Print->add_model_object()
  2123. # my $t = $self->thumbnail->clone;
  2124. # $t->rotate($model_instance->rotation, Slic3r::Point->new(0,0));
  2125. # $t->scale($model_instance->scaling_factor);
  2126. #
  2127. # $self->transformed_thumbnail($t);
  2128. #}
  2129. 1;