Plater.pm 101 KB

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