Plater.pm 102 KB

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