Plater.pm 103 KB

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