Plater.pm 87 KB

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