Plater.pm 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. package Slic3r::GUI::Plater;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use File::Basename qw(basename dirname);
  6. use List::Util qw(sum first);
  7. use Slic3r::Geometry qw(X Y Z MIN MAX scale unscale deg2rad);
  8. use threads::shared qw(shared_clone);
  9. use Thread::Semaphore;
  10. use Wx qw(:button :cursor :dialog :filedialog :keycode :icon :font :id :listctrl :misc
  11. :panel :sizer :toolbar :window wxTheApp);
  12. use Wx::Event qw(EVT_BUTTON EVT_COMMAND EVT_KEY_DOWN EVT_LIST_ITEM_ACTIVATED
  13. EVT_LIST_ITEM_DESELECTED EVT_LIST_ITEM_SELECTED EVT_MOUSE_EVENTS EVT_PAINT EVT_TOOL
  14. EVT_CHOICE EVT_TIMER);
  15. use base 'Wx::Panel';
  16. use constant TB_ADD => &Wx::NewId;
  17. use constant TB_REMOVE => &Wx::NewId;
  18. use constant TB_RESET => &Wx::NewId;
  19. use constant TB_ARRANGE => &Wx::NewId;
  20. use constant TB_EXPORT_GCODE => &Wx::NewId;
  21. use constant TB_EXPORT_STL => &Wx::NewId;
  22. use constant TB_MORE => &Wx::NewId;
  23. use constant TB_FEWER => &Wx::NewId;
  24. use constant TB_45CW => &Wx::NewId;
  25. use constant TB_45CCW => &Wx::NewId;
  26. use constant TB_SCALE => &Wx::NewId;
  27. use constant TB_SPLIT => &Wx::NewId;
  28. use constant TB_VIEW => &Wx::NewId;
  29. use constant TB_SETTINGS => &Wx::NewId;
  30. use constant apply_config_timer_EVENT => &Wx::NewId;
  31. # package variables to avoid passing lexicals to threads
  32. our $THUMBNAIL_DONE_EVENT : shared = Wx::NewEventType;
  33. our $PROGRESS_BAR_EVENT : shared = Wx::NewEventType;
  34. our $ERROR_EVENT : shared = Wx::NewEventType;
  35. our $EXPORT_COMPLETED_EVENT : shared = Wx::NewEventType;
  36. our $PROCESS_COMPLETED_EVENT : shared = Wx::NewEventType;
  37. use constant FILAMENT_CHOOSERS_SPACING => 3;
  38. use constant PROCESS_DELAY => 0.5 * 1000; # milliseconds
  39. my $PreventListEvents = 0;
  40. my $sema = Thread::Semaphore->new;
  41. sub new {
  42. my $class = shift;
  43. my ($parent) = @_;
  44. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
  45. $self->{config} = Slic3r::Config->new_from_defaults(qw(
  46. bed_shape complete_objects extruder_clearance_radius skirts skirt_distance
  47. ));
  48. $self->{model} = Slic3r::Model->new;
  49. $self->{print} = Slic3r::Print->new;
  50. $self->{objects} = [];
  51. $self->{apply_config_timer} = Wx::Timer->new($self, apply_config_timer_EVENT)
  52. if $Slic3r::have_threads;
  53. $self->{print}->set_status_cb(sub {
  54. my ($percent, $message) = @_;
  55. if ($Slic3r::have_threads) {
  56. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $PROGRESS_BAR_EVENT, shared_clone([$percent, $message])));
  57. } else {
  58. $self->on_progress_event($percent, $message);
  59. }
  60. });
  61. $self->{canvas} = Slic3r::GUI::Plater::2D->new($self, [335,335], $self->{objects}, $self->{model}, $self->{config});
  62. $self->{canvas}->on_select_object(sub {
  63. my ($obj_idx) = @_;
  64. $self->select_object($obj_idx);
  65. });
  66. $self->{canvas}->on_double_click(sub {
  67. $self->object_cut_dialog if $self->selected_object;
  68. });
  69. $self->{canvas}->on_right_click(sub {
  70. my ($click_pos) = @_;
  71. my ($obj_idx, $object) = $self->selected_object;
  72. return if !defined $obj_idx;
  73. my $menu = $self->object_menu;
  74. $self->{canvas}->PopupMenu($menu, $click_pos);
  75. $menu->Destroy;
  76. });
  77. $self->{canvas}->on_instance_moved(sub {
  78. $self->update;
  79. });
  80. # toolbar for object manipulation
  81. if (!&Wx::wxMSW) {
  82. Wx::ToolTip::Enable(1);
  83. $self->{htoolbar} = Wx::ToolBar->new($self, -1, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL | wxTB_TEXT | wxBORDER_SIMPLE | wxTAB_TRAVERSAL);
  84. $self->{htoolbar}->AddTool(TB_ADD, "Add…", Wx::Bitmap->new("$Slic3r::var/brick_add.png", wxBITMAP_TYPE_PNG), '');
  85. $self->{htoolbar}->AddTool(TB_REMOVE, "Delete", Wx::Bitmap->new("$Slic3r::var/brick_delete.png", wxBITMAP_TYPE_PNG), '');
  86. $self->{htoolbar}->AddTool(TB_RESET, "Delete All", Wx::Bitmap->new("$Slic3r::var/cross.png", wxBITMAP_TYPE_PNG), '');
  87. $self->{htoolbar}->AddTool(TB_ARRANGE, "Arrange", Wx::Bitmap->new("$Slic3r::var/bricks.png", wxBITMAP_TYPE_PNG), '');
  88. $self->{htoolbar}->AddSeparator;
  89. $self->{htoolbar}->AddTool(TB_MORE, "More", Wx::Bitmap->new("$Slic3r::var/add.png", wxBITMAP_TYPE_PNG), '');
  90. $self->{htoolbar}->AddTool(TB_FEWER, "Fewer", Wx::Bitmap->new("$Slic3r::var/delete.png", wxBITMAP_TYPE_PNG), '');
  91. $self->{htoolbar}->AddSeparator;
  92. $self->{htoolbar}->AddTool(TB_45CCW, "45° ccw", Wx::Bitmap->new("$Slic3r::var/arrow_rotate_anticlockwise.png", wxBITMAP_TYPE_PNG), '');
  93. $self->{htoolbar}->AddTool(TB_45CW, "45° cw", Wx::Bitmap->new("$Slic3r::var/arrow_rotate_clockwise.png", wxBITMAP_TYPE_PNG), '');
  94. $self->{htoolbar}->AddTool(TB_SCALE, "Scale…", Wx::Bitmap->new("$Slic3r::var/arrow_out.png", wxBITMAP_TYPE_PNG), '');
  95. $self->{htoolbar}->AddTool(TB_SPLIT, "Split", Wx::Bitmap->new("$Slic3r::var/shape_ungroup.png", wxBITMAP_TYPE_PNG), '');
  96. $self->{htoolbar}->AddSeparator;
  97. $self->{htoolbar}->AddTool(TB_VIEW, "View/Cut…", Wx::Bitmap->new("$Slic3r::var/package.png", wxBITMAP_TYPE_PNG), '');
  98. $self->{htoolbar}->AddTool(TB_SETTINGS, "Settings…", Wx::Bitmap->new("$Slic3r::var/cog.png", wxBITMAP_TYPE_PNG), '');
  99. } else {
  100. my %tbar_buttons = (
  101. add => "Add…",
  102. remove => "Delete",
  103. reset => "Delete All",
  104. arrange => "Arrange",
  105. increase => "",
  106. decrease => "",
  107. rotate45ccw => "",
  108. rotate45cw => "",
  109. rotate => "Rotate…",
  110. changescale => "Scale…",
  111. split => "Split",
  112. view => "View/Cut…",
  113. settings => "Settings…",
  114. );
  115. $self->{btoolbar} = Wx::BoxSizer->new(wxHORIZONTAL);
  116. for (qw(add remove reset arrange increase decrease rotate45ccw rotate45cw rotate changescale split view settings)) {
  117. $self->{"btn_$_"} = Wx::Button->new($self, -1, $tbar_buttons{$_}, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
  118. $self->{btoolbar}->Add($self->{"btn_$_"});
  119. }
  120. }
  121. $self->{list} = Wx::ListView->new($self, -1, wxDefaultPosition, [250,-1], wxLC_SINGLE_SEL | wxLC_REPORT | wxBORDER_SUNKEN | wxTAB_TRAVERSAL | wxWANTS_CHARS);
  122. $self->{list}->InsertColumn(0, "Name", wxLIST_FORMAT_LEFT, 145);
  123. $self->{list}->InsertColumn(1, "Copies", wxLIST_FORMAT_CENTER, 45);
  124. $self->{list}->InsertColumn(2, "Scale", wxLIST_FORMAT_CENTER, wxLIST_AUTOSIZE_USEHEADER);
  125. EVT_LIST_ITEM_SELECTED($self, $self->{list}, \&list_item_selected);
  126. EVT_LIST_ITEM_DESELECTED($self, $self->{list}, \&list_item_deselected);
  127. EVT_LIST_ITEM_ACTIVATED($self, $self->{list}, \&list_item_activated);
  128. EVT_KEY_DOWN($self->{list}, sub {
  129. my ($list, $event) = @_;
  130. if ($event->GetKeyCode == WXK_TAB) {
  131. $list->Navigate($event->ShiftDown ? &Wx::wxNavigateBackward : &Wx::wxNavigateForward);
  132. } else {
  133. $event->Skip;
  134. }
  135. });
  136. # right pane buttons
  137. $self->{btn_export_gcode} = Wx::Button->new($self, -1, "Export G-code…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  138. $self->{btn_export_stl} = Wx::Button->new($self, -1, "Export STL…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
  139. $self->{btn_export_gcode}->SetFont($Slic3r::GUI::small_font);
  140. $self->{btn_export_stl}->SetFont($Slic3r::GUI::small_font);
  141. if ($Slic3r::GUI::have_button_icons) {
  142. my %icons = qw(
  143. add brick_add.png
  144. remove brick_delete.png
  145. reset cross.png
  146. arrange bricks.png
  147. export_gcode cog_go.png
  148. export_stl brick_go.png
  149. increase add.png
  150. decrease delete.png
  151. rotate45cw arrow_rotate_clockwise.png
  152. rotate45ccw arrow_rotate_anticlockwise.png
  153. changescale arrow_out.png
  154. split shape_ungroup.png
  155. view package.png
  156. settings cog.png
  157. );
  158. for (grep $self->{"btn_$_"}, keys %icons) {
  159. $self->{"btn_$_"}->SetBitmap(Wx::Bitmap->new("$Slic3r::var/$icons{$_}", wxBITMAP_TYPE_PNG));
  160. }
  161. }
  162. $self->selection_changed(0);
  163. $self->object_list_changed;
  164. EVT_BUTTON($self, $self->{btn_export_gcode}, sub {
  165. $self->export_gcode;
  166. Slic3r::thread_cleanup();
  167. });
  168. EVT_BUTTON($self, $self->{btn_export_stl}, \&export_stl);
  169. if ($self->{htoolbar}) {
  170. EVT_TOOL($self, TB_ADD, \&add);
  171. EVT_TOOL($self, TB_REMOVE, sub { $self->remove() }); # explicitly pass no argument to remove
  172. EVT_TOOL($self, TB_RESET, \&reset);
  173. EVT_TOOL($self, TB_ARRANGE, \&arrange);
  174. EVT_TOOL($self, TB_MORE, \&increase);
  175. EVT_TOOL($self, TB_FEWER, \&decrease);
  176. EVT_TOOL($self, TB_45CW, sub { $_[0]->rotate(-45) });
  177. EVT_TOOL($self, TB_45CCW, sub { $_[0]->rotate(45) });
  178. EVT_TOOL($self, TB_SCALE, \&changescale);
  179. EVT_TOOL($self, TB_SPLIT, \&split_object);
  180. EVT_TOOL($self, TB_VIEW, sub { $_[0]->object_cut_dialog });
  181. EVT_TOOL($self, TB_SETTINGS, sub { $_[0]->object_settings_dialog });
  182. } else {
  183. EVT_BUTTON($self, $self->{btn_add}, \&add);
  184. EVT_BUTTON($self, $self->{btn_remove}, sub { $self->remove() }); # explicitly pass no argument to remove
  185. EVT_BUTTON($self, $self->{btn_reset}, \&reset);
  186. EVT_BUTTON($self, $self->{btn_arrange}, \&arrange);
  187. EVT_BUTTON($self, $self->{btn_increase}, \&increase);
  188. EVT_BUTTON($self, $self->{btn_decrease}, \&decrease);
  189. EVT_BUTTON($self, $self->{btn_rotate45cw}, sub { $_[0]->rotate(-45) });
  190. EVT_BUTTON($self, $self->{btn_rotate45ccw}, sub { $_[0]->rotate(45) });
  191. EVT_BUTTON($self, $self->{btn_changescale}, \&changescale);
  192. EVT_BUTTON($self, $self->{btn_split}, \&split_object);
  193. EVT_BUTTON($self, $self->{btn_view}, sub { $_[0]->object_cut_dialog });
  194. EVT_BUTTON($self, $self->{btn_settings}, sub { $_[0]->object_settings_dialog });
  195. }
  196. $_->SetDropTarget(Slic3r::GUI::Plater::DropTarget->new($self))
  197. for $self, $self->{canvas}, $self->{list};
  198. EVT_COMMAND($self, -1, $THUMBNAIL_DONE_EVENT, sub {
  199. my ($self, $event) = @_;
  200. my ($obj_idx) = @{$event->GetData};
  201. return if !$self->{objects}[$obj_idx]; # object was deleted before thumbnail generation completed
  202. $self->on_thumbnail_made($obj_idx);
  203. });
  204. EVT_COMMAND($self, -1, $PROGRESS_BAR_EVENT, sub {
  205. my ($self, $event) = @_;
  206. my ($percent, $message) = @{$event->GetData};
  207. $self->on_progress_event($percent, $message);
  208. });
  209. EVT_COMMAND($self, -1, $ERROR_EVENT, sub {
  210. my ($self, $event) = @_;
  211. Slic3r::GUI::show_error($self, @{$event->GetData});
  212. });
  213. EVT_COMMAND($self, -1, $EXPORT_COMPLETED_EVENT, sub {
  214. my ($self, $event) = @_;
  215. $self->on_export_completed($event->GetData);
  216. });
  217. EVT_COMMAND($self, -1, $PROCESS_COMPLETED_EVENT, sub {
  218. my ($self, $event) = @_;
  219. $self->on_process_completed($event->GetData);
  220. Slic3r::thread_cleanup();
  221. });
  222. EVT_TIMER($self, apply_config_timer_EVENT, sub {
  223. my ($self, $event) = @_;
  224. $self->async_apply_config;
  225. });
  226. $self->{canvas}->update_bed_size;
  227. $self->update;
  228. {
  229. my $presets;
  230. if ($self->GetFrame->{mode} eq 'expert') {
  231. $presets = Wx::BoxSizer->new(wxVERTICAL);
  232. my %group_labels = (
  233. print => 'Print settings',
  234. filament => 'Filament',
  235. printer => 'Printer',
  236. );
  237. $self->{preset_choosers} = {};
  238. $self->{preset_choosers_sizers} = {};
  239. for my $group (qw(print filament printer)) {
  240. my $text = Wx::StaticText->new($self, -1, "$group_labels{$group}:", wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
  241. $text->SetFont($Slic3r::GUI::small_font);
  242. my $choice = Wx::Choice->new($self, -1, wxDefaultPosition, [140, -1], []);
  243. $choice->SetFont($Slic3r::GUI::small_font);
  244. $self->{preset_choosers}{$group} = [$choice];
  245. EVT_CHOICE($choice, $choice, sub { $self->on_select_preset($group, @_) });
  246. $self->{preset_choosers_sizers}{$group} = Wx::BoxSizer->new(wxVERTICAL);
  247. $self->{preset_choosers_sizers}{$group}->Add($choice, 0, wxEXPAND | wxBOTTOM, FILAMENT_CHOOSERS_SPACING);
  248. $presets->Add($text, 0, wxALIGN_LEFT | wxRIGHT, 4);
  249. $presets->Add($self->{preset_choosers_sizers}{$group}, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, 8);
  250. }
  251. }
  252. my $object_info_sizer;
  253. {
  254. my $box = Wx::StaticBox->new($self, -1, "Info");
  255. $object_info_sizer = Wx::StaticBoxSizer->new($box, wxVERTICAL);
  256. $object_info_sizer->SetMinSize([350,-1]);
  257. my $grid_sizer = Wx::FlexGridSizer->new(3, 4, 5, 5);
  258. $grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
  259. $grid_sizer->AddGrowableCol(1, 1);
  260. $grid_sizer->AddGrowableCol(3, 1);
  261. $object_info_sizer->Add($grid_sizer, 0, wxEXPAND);
  262. my @info = (
  263. size => "Size",
  264. volume => "Volume",
  265. facets => "Facets",
  266. materials => "Materials",
  267. manifold => "Manifold",
  268. );
  269. while (my $field = shift @info) {
  270. my $label = shift @info;
  271. my $text = Wx::StaticText->new($box, -1, "$label:", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  272. $text->SetFont($Slic3r::GUI::small_font);
  273. $grid_sizer->Add($text, 0);
  274. $self->{"object_info_$field"} = Wx::StaticText->new($box, -1, "", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  275. $self->{"object_info_$field"}->SetFont($Slic3r::GUI::small_font);
  276. if ($field eq 'manifold') {
  277. $self->{object_info_manifold_warning_icon} = Wx::StaticBitmap->new($box, -1, Wx::Bitmap->new("$Slic3r::var/error.png", wxBITMAP_TYPE_PNG));
  278. $self->{object_info_manifold_warning_icon}->Hide;
  279. my $h_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  280. $h_sizer->Add($self->{object_info_manifold_warning_icon}, 0);
  281. $h_sizer->Add($self->{"object_info_$field"}, 0);
  282. $grid_sizer->Add($h_sizer, 0, wxEXPAND);
  283. } else {
  284. $grid_sizer->Add($self->{"object_info_$field"}, 0);
  285. }
  286. }
  287. }
  288. my $right_buttons_sizer = Wx::BoxSizer->new(wxVERTICAL);
  289. $right_buttons_sizer->Add($presets, 0, wxEXPAND, 0) if defined $presets;
  290. $right_buttons_sizer->Add($self->{btn_export_gcode}, 0, wxEXPAND | wxTOP, 8);
  291. $right_buttons_sizer->Add($self->{btn_export_stl}, 0, wxEXPAND | wxTOP, 2);
  292. my $right_top_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  293. $right_top_sizer->Add($self->{list}, 1, wxEXPAND | wxLEFT, 5);
  294. $right_top_sizer->Add($right_buttons_sizer, 0, wxEXPAND | wxALL, 10);
  295. my $right_sizer = Wx::BoxSizer->new(wxVERTICAL);
  296. $right_sizer->Add($right_top_sizer, 1, wxEXPAND | wxBOTTOM, 10);
  297. $right_sizer->Add($object_info_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT, 5);
  298. my $hsizer = Wx::BoxSizer->new(wxHORIZONTAL);
  299. $hsizer->Add($self->{canvas}, 1, wxEXPAND | wxTOP, 1);
  300. $hsizer->Add($right_sizer, 0, wxEXPAND | wxBOTTOM, 0);
  301. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  302. $sizer->Add($self->{htoolbar}, 0, wxEXPAND, 0) if $self->{htoolbar};
  303. $sizer->Add($self->{btoolbar}, 0, wxEXPAND, 0) if $self->{btoolbar};
  304. $sizer->Add($hsizer, 1, wxEXPAND, 0);
  305. $sizer->SetSizeHints($self);
  306. $self->SetSizer($sizer);
  307. }
  308. return $self;
  309. }
  310. sub on_select_preset {
  311. my $self = shift;
  312. my ($group, $choice) = @_;
  313. if ($group eq 'filament' && @{$self->{preset_choosers}{filament}} > 1) {
  314. my @filament_presets = $self->filament_presets;
  315. $Slic3r::GUI::Settings->{presets}{filament} = $choice->GetString($filament_presets[0]) . ".ini";
  316. $Slic3r::GUI::Settings->{presets}{"filament_${_}"} = $choice->GetString($filament_presets[$_])
  317. for 1 .. $#filament_presets;
  318. wxTheApp->save_settings;
  319. return;
  320. }
  321. $self->GetFrame->{options_tabs}{$group}->select_preset($choice->GetSelection);
  322. }
  323. sub GetFrame {
  324. my ($self) = @_;
  325. return &Wx::GetTopLevelParent($self);
  326. }
  327. sub update_presets {
  328. my $self = shift;
  329. my ($group, $items, $selected) = @_;
  330. foreach my $choice (@{ $self->{preset_choosers}{$group} }) {
  331. my $sel = $choice->GetSelection;
  332. $choice->Clear;
  333. $choice->Append($_) for @$items;
  334. $choice->SetSelection($sel) if $sel <= $#$items;
  335. }
  336. $self->{preset_choosers}{$group}[0]->SetSelection($selected);
  337. }
  338. sub filament_presets {
  339. my $self = shift;
  340. return map $_->GetSelection, @{ $self->{preset_choosers}{filament} };
  341. }
  342. sub add {
  343. my $self = shift;
  344. my @input_files = wxTheApp->open_model($self);
  345. $self->load_file($_) for @input_files;
  346. }
  347. sub load_file {
  348. my $self = shift;
  349. my ($input_file) = @_;
  350. $Slic3r::GUI::Settings->{recent}{skein_directory} = dirname($input_file);
  351. wxTheApp->save_settings;
  352. my $process_dialog = Wx::ProgressDialog->new('Loading…', "Processing input file…", 100, $self, 0);
  353. $process_dialog->Pulse;
  354. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  355. my $model = eval { Slic3r::Model->read_from_file($input_file) };
  356. Slic3r::GUI::show_error($self, $@) if $@;
  357. if (defined $model) {
  358. $self->load_model_objects(@{$model->objects});
  359. $self->statusbar->SetStatusText("Loaded " . basename($input_file));
  360. }
  361. $process_dialog->Destroy;
  362. }
  363. sub load_model_objects {
  364. my ($self, @model_objects) = @_;
  365. my $bed_centerf = $self->bed_centerf;
  366. my $need_arrange = 0;
  367. my @obj_idx = ();
  368. foreach my $model_object (@model_objects) {
  369. my $o = $self->{model}->add_object($model_object);
  370. push @{ $self->{objects} }, Slic3r::GUI::Plater::Object->new(
  371. name => basename($model_object->input_file),
  372. );
  373. push @obj_idx, $#{ $self->{objects} };
  374. if ($model_object->instances_count == 0) {
  375. # if object has no defined position(s) we need to rearrange everything after loading
  376. $need_arrange = 1;
  377. # add a default instance and center object around origin
  378. $o->center_around_origin;
  379. $o->add_instance(offset => $bed_centerf);
  380. }
  381. $self->{print}->auto_assign_extruders($o);
  382. $self->{print}->add_model_object($o);
  383. }
  384. # if user turned autocentering off, automatic arranging would disappoint them
  385. if (!$Slic3r::GUI::Settings->{_}{autocenter}) {
  386. $need_arrange = 0;
  387. }
  388. $self->objects_loaded(\@obj_idx, no_arrange => !$need_arrange);
  389. }
  390. sub objects_loaded {
  391. my $self = shift;
  392. my ($obj_idxs, %params) = @_;
  393. foreach my $obj_idx (@$obj_idxs) {
  394. my $object = $self->{objects}[$obj_idx];
  395. my $model_object = $self->{model}->objects->[$obj_idx];
  396. $self->{list}->InsertStringItem($obj_idx, $object->name);
  397. $self->{list}->SetItemFont($obj_idx, Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL))
  398. if $self->{list}->can('SetItemFont'); # legacy code for wxPerl < 0.9918 not supporting SetItemFont()
  399. $self->{list}->SetItem($obj_idx, 1, $model_object->instances_count);
  400. $self->{list}->SetItem($obj_idx, 2, ($model_object->instances->[0]->scaling_factor * 100) . "%");
  401. $self->make_thumbnail($obj_idx);
  402. }
  403. $self->arrange unless $params{no_arrange};
  404. $self->update;
  405. $self->{list}->Update;
  406. $self->{list}->Select($obj_idxs->[-1], 1);
  407. $self->object_list_changed;
  408. $self->schedule_background_process;
  409. }
  410. sub bed_centerf {
  411. my ($self) = @_;
  412. my $bed_shape = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape});
  413. my $bed_center = $bed_shape->bounding_box->center;
  414. return Slic3r::Pointf->new(unscale($bed_center->x), unscale($bed_center->y)); #)
  415. }
  416. sub remove {
  417. my $self = shift;
  418. my ($obj_idx) = @_;
  419. $self->stop_background_process;
  420. # if no object index is supplied, remove the selected one
  421. if (!defined $obj_idx) {
  422. ($obj_idx, undef) = $self->selected_object;
  423. }
  424. splice @{$self->{objects}}, $obj_idx, 1;
  425. $self->{model}->delete_object($obj_idx);
  426. $self->{print}->delete_object($obj_idx);
  427. $self->{list}->DeleteItem($obj_idx);
  428. $self->object_list_changed;
  429. $self->select_object(undef);
  430. $self->update;
  431. $self->{canvas}->Refresh;
  432. $self->schedule_background_process;
  433. }
  434. sub reset {
  435. my $self = shift;
  436. $self->stop_background_process;
  437. @{$self->{objects}} = ();
  438. $self->{model}->clear_objects;
  439. $self->{print}->clear_objects;
  440. $self->{list}->DeleteAllItems;
  441. $self->object_list_changed;
  442. $self->select_object(undef);
  443. $self->{canvas}->Refresh;
  444. }
  445. sub increase {
  446. my $self = shift;
  447. my ($obj_idx, $object) = $self->selected_object;
  448. my $model_object = $self->{model}->objects->[$obj_idx];
  449. my $last_instance = $model_object->instances->[-1];
  450. my $i = $model_object->add_instance(
  451. offset => Slic3r::Pointf->new(map 10+$_, @{$last_instance->offset}),
  452. scaling_factor => $last_instance->scaling_factor,
  453. rotation => $last_instance->rotation,
  454. );
  455. $self->{print}->objects->[$obj_idx]->add_copy(@{$i->offset});
  456. $self->{list}->SetItem($obj_idx, 1, $model_object->instances_count);
  457. # only autoarrange if user has autocentering enabled
  458. if ($Slic3r::GUI::Settings->{_}{autocenter}) {
  459. $self->arrange;
  460. } else {
  461. $self->{canvas}->Refresh;
  462. }
  463. }
  464. sub decrease {
  465. my $self = shift;
  466. my ($obj_idx, $object) = $self->selected_object;
  467. my $model_object = $self->{model}->objects->[$obj_idx];
  468. if ($model_object->instances_count >= 2) {
  469. $model_object->delete_last_instance;
  470. $self->{print}->objects->[$obj_idx]->delete_last_copy;
  471. $self->{list}->SetItem($obj_idx, 1, $model_object->instances_count);
  472. } else {
  473. $self->remove;
  474. }
  475. if ($self->{objects}[$obj_idx]) {
  476. $self->{list}->Select($obj_idx, 0);
  477. $self->{list}->Select($obj_idx, 1);
  478. }
  479. $self->update;
  480. $self->{canvas}->Refresh;
  481. }
  482. sub rotate {
  483. my $self = shift;
  484. my ($angle, $axis) = @_;
  485. $axis //= Z;
  486. my ($obj_idx, $object) = $self->selected_object;
  487. return if !defined $obj_idx;
  488. my $model_object = $self->{model}->objects->[$obj_idx];
  489. my $model_instance = $model_object->instances->[0];
  490. # we need thumbnail to be computed before allowing rotation
  491. return if !$object->thumbnail;
  492. if (!defined $angle) {
  493. my $axis_name = $axis == X ? 'X' : $axis == Y ? 'Y' : 'Z';
  494. $angle = Wx::GetNumberFromUser("", "Enter the rotation angle:", "Rotate around $axis_name axis", $model_instance->rotation, -364, 364, $self);
  495. return if !$angle || $angle == -1;
  496. $angle = 0 - $angle; # rotate clockwise (be consistent with button icon)
  497. }
  498. {
  499. if ($axis == Z) {
  500. my $new_angle = $model_instance->rotation + $angle;
  501. $_->set_rotation($new_angle) for @{ $model_object->instances };
  502. $object->transform_thumbnail($self->{model}, $obj_idx);
  503. } else {
  504. # rotation around X and Y needs to be performed on mesh
  505. # so we first apply any Z rotation
  506. if ($model_instance->rotation != 0) {
  507. $model_object->rotate(deg2rad($model_instance->rotation), Z);
  508. $_->set_rotation(0) for @{ $model_object->instances };
  509. }
  510. $model_object->rotate(deg2rad($angle), $axis);
  511. $self->make_thumbnail($obj_idx);
  512. }
  513. $model_object->update_bounding_box;
  514. # update print and start background processing
  515. $self->stop_background_process;
  516. $self->{print}->add_model_object($model_object, $obj_idx);
  517. $self->schedule_background_process;
  518. }
  519. $self->selection_changed; # refresh info (size etc.)
  520. $self->update;
  521. $self->{canvas}->Refresh;
  522. }
  523. sub flip {
  524. my ($self, $axis) = @_;
  525. my ($obj_idx, $object) = $self->selected_object;
  526. return if !defined $obj_idx;
  527. my $model_object = $self->{model}->objects->[$obj_idx];
  528. my $model_instance = $model_object->instances->[0];
  529. # apply Z rotation before flipping
  530. if ($model_instance->rotation != 0) {
  531. $model_object->rotate(deg2rad($model_instance->rotation), Z);
  532. $_->set_rotation(0) for @{ $model_object->instances };
  533. }
  534. $model_object->flip($axis);
  535. $model_object->update_bounding_box;
  536. $self->make_thumbnail($obj_idx);
  537. # update print and start background processing
  538. $self->stop_background_process;
  539. $self->{print}->add_model_object($model_object, $obj_idx);
  540. $self->schedule_background_process;
  541. $self->selection_changed; # refresh info (size etc.)
  542. $self->update;
  543. $self->{canvas}->Refresh;
  544. }
  545. sub changescale {
  546. my $self = shift;
  547. my ($obj_idx, $object) = $self->selected_object;
  548. my $model_object = $self->{model}->objects->[$obj_idx];
  549. my $model_instance = $model_object->instances->[0];
  550. # we need thumbnail to be computed before allowing scaling
  551. return if !$object->thumbnail;
  552. # max scale factor should be above 2540 to allow importing files exported in inches
  553. my $scale = Wx::GetNumberFromUser("", "Enter the scale % for the selected object:", "Scale", $model_instance->scaling_factor*100, 0, 100000, $self);
  554. return if !$scale || $scale == -1;
  555. $self->{list}->SetItem($obj_idx, 2, "$scale%");
  556. $scale /= 100; # turn percent into factor
  557. {
  558. my $variation = $scale / $model_instance->scaling_factor;
  559. foreach my $range (@{ $model_object->layer_height_ranges }) {
  560. $range->[0] *= $variation;
  561. $range->[1] *= $variation;
  562. }
  563. $_->set_scaling_factor($scale) for @{ $model_object->instances };
  564. $model_object->update_bounding_box;
  565. # update print and start background processing
  566. $self->stop_background_process;
  567. $self->{print}->add_model_object($model_object, $obj_idx);
  568. $self->schedule_background_process;
  569. $object->transform_thumbnail($self->{model}, $obj_idx);
  570. }
  571. $self->selection_changed(1); # refresh info (size, volume etc.)
  572. $self->update;
  573. $self->{canvas}->Refresh;
  574. }
  575. sub arrange {
  576. my $self = shift;
  577. my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($self->{config}->bed_shape);
  578. eval {
  579. $self->{model}->arrange_objects($self->GetFrame->config->min_object_distance, $bb);
  580. };
  581. # ignore arrange failures on purpose: user has visual feedback and we don't need to warn him
  582. # when parts don't fit in print bed
  583. $self->update(1);
  584. $self->{canvas}->Refresh;
  585. }
  586. sub split_object {
  587. my $self = shift;
  588. my ($obj_idx, $current_object) = $self->selected_object;
  589. my $current_model_object = $self->{model}->objects->[$obj_idx];
  590. if (@{$current_model_object->volumes} > 1) {
  591. Slic3r::GUI::warning_catcher($self)->("The selected object couldn't be split because it contains more than one volume/material.");
  592. return;
  593. }
  594. my @new_meshes = @{$current_model_object->volumes->[0]->mesh->split};
  595. if (@new_meshes == 1) {
  596. Slic3r::GUI::warning_catcher($self)->("The selected object couldn't be split because it already contains a single part.");
  597. return;
  598. }
  599. $self->stop_background_process;
  600. # create a bogus Model object, we only need to instantiate the new Model::Object objects
  601. my $new_model = Slic3r::Model->new;
  602. my @model_objects = ();
  603. foreach my $mesh (@new_meshes) {
  604. $mesh->repair;
  605. my $model_object = $new_model->add_object(
  606. input_file => $current_model_object->input_file,
  607. config => $current_model_object->config->clone,
  608. layer_height_ranges => $current_model_object->layer_height_ranges, # TODO: clone this
  609. );
  610. $model_object->add_volume(
  611. mesh => $mesh,
  612. material_id => $current_model_object->volumes->[0]->material_id,
  613. );
  614. for my $instance_idx (0..$#{ $current_model_object->instances }) {
  615. my $current_instance = $current_model_object->instances->[$instance_idx];
  616. $model_object->add_instance(
  617. offset => Slic3r::Pointf->new(
  618. $current_instance->offset->[X] + ($instance_idx * 10),
  619. $current_instance->offset->[Y] + ($instance_idx * 10),
  620. ),
  621. rotation => $current_instance->rotation,
  622. scaling_factor => $current_instance->scaling_factor,
  623. );
  624. }
  625. # we need to center this single object around origin
  626. $model_object->center_around_origin;
  627. push @model_objects, $model_object;
  628. }
  629. # remove the original object before spawning the object_loaded event, otherwise
  630. # we'll pass the wrong $obj_idx to it (which won't be recognized after the
  631. # thumbnail thread returns)
  632. $self->remove($obj_idx);
  633. $current_object = $obj_idx = undef;
  634. # load all model objects at once, otherwise the plate would be rearranged after each one
  635. # causing original positions not to be kept
  636. $self->load_model_objects(@model_objects);
  637. }
  638. sub schedule_background_process {
  639. my ($self) = @_;
  640. $self->{apply_config_timer}->Start(PROCESS_DELAY, 1) # 1 = one shot
  641. if defined $self->{apply_config_timer};
  642. }
  643. sub async_apply_config {
  644. my ($self) = @_;
  645. # pause process thread before applying new config
  646. # since we don't want to touch data that is being used by the threads
  647. $self->suspend_background_process;
  648. # apply new config
  649. my $invalidated = $self->{print}->apply_config($self->GetFrame->config);
  650. return if !$Slic3r::GUI::Settings->{_}{background_processing};
  651. if ($invalidated) {
  652. # kill current thread if any
  653. $self->stop_background_process;
  654. } else {
  655. # TODO: restore process thread
  656. }
  657. # schedule a new process thread in case it wasn't running
  658. $self->start_background_process;
  659. }
  660. sub start_background_process {
  661. my ($self) = @_;
  662. $self->resume_background_process;
  663. return if !$Slic3r::have_threads;
  664. return if !@{$self->{objects}};
  665. return if $self->{process_thread};
  666. # It looks like declaring a local $SIG{__WARN__} prevents the ugly
  667. # "Attempt to free unreferenced scalar" warning...
  668. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  669. # don't start process thread if config is not valid
  670. eval {
  671. # this will throw errors if config is not valid
  672. $self->GetFrame->config->validate;
  673. $self->{print}->validate;
  674. };
  675. return if $@;
  676. # apply extra variables
  677. {
  678. my $extra = $self->GetFrame->extra_variables;
  679. $self->{print}->placeholder_parser->set($_, $extra->{$_}) for keys %$extra;
  680. }
  681. # start thread
  682. @_ = ();
  683. $self->{process_thread} = threads->create(sub {
  684. local $SIG{'KILL'} = sub {
  685. Slic3r::debugf "Background process cancelled; exiting thread...\n";
  686. Slic3r::thread_cleanup();
  687. threads->exit();
  688. };
  689. local $SIG{'STOP'} = sub {
  690. $sema->down;
  691. $sema->up;
  692. };
  693. eval {
  694. $self->{print}->process;
  695. };
  696. if ($@) {
  697. Slic3r::debugf "Discarding background process error: $@\n";
  698. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $PROCESS_COMPLETED_EVENT, 0));
  699. } else {
  700. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $PROCESS_COMPLETED_EVENT, 1));
  701. }
  702. Slic3r::thread_cleanup();
  703. });
  704. Slic3r::debugf "Background processing started.\n";
  705. }
  706. sub stop_background_process {
  707. my ($self) = @_;
  708. $self->{apply_config_timer}->Stop if defined $self->{apply_config_timer};
  709. $self->statusbar->SetCancelCallback(undef);
  710. $self->statusbar->StopBusy;
  711. $self->statusbar->SetStatusText("");
  712. if ($self->{process_thread}) {
  713. Slic3r::debugf "Killing background process.\n";
  714. $self->{process_thread}->kill('KILL')->join;
  715. $self->{process_thread} = undef;
  716. } else {
  717. Slic3r::debugf "No background process running.\n";
  718. }
  719. # if there's an export process, kill that one as well
  720. if ($self->{export_thread}) {
  721. Slic3r::debugf "Killing background export process.\n";
  722. $self->{export_thread}->kill('KILL')->join;
  723. $self->{export_thread} = undef;
  724. }
  725. }
  726. sub suspend_background_process {
  727. my ($self) = @_;
  728. $sema->down;
  729. $_->kill('STOP') for grep $_, $self->{process_thread}, $self->{export_thread};
  730. }
  731. sub resume_background_process {
  732. my ($self) = @_;
  733. $sema->up;
  734. }
  735. sub export_gcode {
  736. my $self = shift;
  737. return if !@{$self->{objects}};
  738. if ($self->{export_gcode_output_file}) {
  739. Wx::MessageDialog->new($self, "Another export job is currently running.", 'Error', wxOK | wxICON_ERROR)->ShowModal;
  740. return;
  741. }
  742. # if process is not running, validate config
  743. # (we assume that if it is running, config is valid)
  744. eval {
  745. # this will throw errors if config is not valid
  746. $self->GetFrame->config->validate;
  747. $self->{print}->validate;
  748. };
  749. Slic3r::GUI::catch_error($self) and return;
  750. # apply config and validate print
  751. my $config = $self->GetFrame->config;
  752. eval {
  753. # this will throw errors if config is not valid
  754. $config->validate;
  755. $self->{print}->apply_config($config);
  756. $self->{print}->validate;
  757. };
  758. if (!$Slic3r::have_threads) {
  759. Slic3r::GUI::catch_error($self) and return;
  760. }
  761. # select output file
  762. $self->{export_gcode_output_file} = $main::opt{output};
  763. {
  764. my $default_output_file = $self->{print}->expanded_output_filepath($self->{export_gcode_output_file});
  765. my $dlg = Wx::FileDialog->new($self, 'Save G-code file as:', wxTheApp->output_path(dirname($default_output_file)),
  766. basename($default_output_file), &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE);
  767. if ($dlg->ShowModal != wxID_OK) {
  768. $dlg->Destroy;
  769. $self->{export_gcode_output_file} = undef;
  770. return;
  771. }
  772. $Slic3r::GUI::Settings->{_}{last_output_path} = dirname($dlg->GetPath);
  773. wxTheApp->save_settings;
  774. $self->{export_gcode_output_file} = $Slic3r::GUI::MainFrame::last_output_file = $dlg->GetPath;
  775. $dlg->Destroy;
  776. }
  777. $self->statusbar->StartBusy;
  778. if ($Slic3r::have_threads) {
  779. $self->statusbar->SetCancelCallback(sub {
  780. $self->stop_background_process;
  781. $self->statusbar->SetStatusText("Export cancelled");
  782. });
  783. # start background process, whose completion event handler
  784. # will detect $self->{export_gcode_output_file} and proceed with export
  785. $self->start_background_process;
  786. } else {
  787. eval {
  788. $self->{print}->process;
  789. $self->{print}->export_gcode(output_file => $self->{export_gcode_output_file});
  790. };
  791. my $result = !Slic3r::GUI::catch_error($self);
  792. $self->on_export_completed($result);
  793. }
  794. }
  795. # This gets called only if we have threads.
  796. sub on_process_completed {
  797. my ($self, $result) = @_;
  798. $self->statusbar->SetCancelCallback(undef);
  799. $self->statusbar->StopBusy;
  800. $self->statusbar->SetStatusText("");
  801. Slic3r::debugf "Background processing completed.\n";
  802. $self->{process_thread}->detach if $self->{process_thread};
  803. $self->{process_thread} = undef;
  804. return if !$result;
  805. # if we have an export filename, start a new thread for exporting G-code
  806. if ($self->{export_gcode_output_file}) {
  807. @_ = ();
  808. # workaround for "Attempt to free un referenced scalar..."
  809. our $_thread_self = $self;
  810. $self->{export_thread} = threads->create(sub {
  811. local $SIG{'KILL'} = sub {
  812. Slic3r::debugf "Export process cancelled; exiting thread...\n";
  813. Slic3r::thread_cleanup();
  814. threads->exit();
  815. };
  816. local $SIG{'STOP'} = sub {
  817. $sema->down;
  818. $sema->up;
  819. };
  820. eval {
  821. $_thread_self->{print}->export_gcode(output_file => $_thread_self->{export_gcode_output_file});
  822. };
  823. if ($@) {
  824. Wx::PostEvent($_thread_self, Wx::PlThreadEvent->new(-1, $ERROR_EVENT, shared_clone([ $@ ])));
  825. Wx::PostEvent($_thread_self, Wx::PlThreadEvent->new(-1, $EXPORT_COMPLETED_EVENT, 0));
  826. } else {
  827. Wx::PostEvent($_thread_self, Wx::PlThreadEvent->new(-1, $EXPORT_COMPLETED_EVENT, 1));
  828. }
  829. Slic3r::thread_cleanup();
  830. });
  831. Slic3r::debugf "Background G-code export started.\n";
  832. }
  833. }
  834. # This gets called also if we have no threads.
  835. sub on_progress_event {
  836. my ($self, $percent, $message) = @_;
  837. $self->statusbar->SetProgress($percent);
  838. $self->statusbar->SetStatusText("$message…");
  839. }
  840. # This gets called also if we don't have threads.
  841. sub on_export_completed {
  842. my ($self, $result) = @_;
  843. $self->statusbar->SetCancelCallback(undef);
  844. $self->statusbar->StopBusy;
  845. $self->statusbar->SetStatusText("");
  846. Slic3r::debugf "Background export process completed.\n";
  847. $self->{export_thread}->detach if $self->{export_thread};
  848. $self->{export_thread} = undef;
  849. my $message;
  850. if ($result) {
  851. $message = "G-code file exported to " . $self->{export_gcode_output_file};
  852. } else {
  853. $message = "Export failed";
  854. }
  855. $self->{export_gcode_output_file} = undef;
  856. $self->statusbar->SetStatusText($message);
  857. wxTheApp->notify($message);
  858. }
  859. sub export_stl {
  860. my $self = shift;
  861. return if !@{$self->{objects}};
  862. my $output_file = $self->_get_export_file('STL') or return;
  863. Slic3r::Format::STL->write_file($output_file, $self->{model}, binary => 1);
  864. $self->statusbar->SetStatusText("STL file exported to $output_file");
  865. # this method gets executed in a separate thread by wxWidgets since it's a button handler
  866. Slic3r::thread_cleanup() if $Slic3r::have_threads;
  867. }
  868. sub export_amf {
  869. my $self = shift;
  870. return if !@{$self->{objects}};
  871. my $output_file = $self->_get_export_file('AMF') or return;
  872. Slic3r::Format::AMF->write_file($output_file, $self->{model});
  873. $self->statusbar->SetStatusText("AMF file exported to $output_file");
  874. # this method gets executed in a separate thread by wxWidgets since it's a menu handler
  875. Slic3r::thread_cleanup() if $Slic3r::have_threads;
  876. }
  877. sub _get_export_file {
  878. my $self = shift;
  879. my ($format) = @_;
  880. my $suffix = $format eq 'STL' ? '.stl' : '.amf.xml';
  881. my $output_file = $main::opt{output};
  882. {
  883. $output_file = $self->{print}->expanded_output_filepath($output_file);
  884. $output_file =~ s/\.gcode$/$suffix/i;
  885. my $dlg = Wx::FileDialog->new($self, "Save $format file as:", dirname($output_file),
  886. basename($output_file), &Slic3r::GUI::MODEL_WILDCARD, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  887. if ($dlg->ShowModal != wxID_OK) {
  888. $dlg->Destroy;
  889. return undef;
  890. }
  891. $output_file = $Slic3r::GUI::MainFrame::last_output_file = $dlg->GetPath;
  892. $dlg->Destroy;
  893. }
  894. return $output_file;
  895. }
  896. sub make_thumbnail {
  897. my $self = shift;
  898. my ($obj_idx) = @_;
  899. my $plater_object = $self->{objects}[$obj_idx];
  900. $plater_object->thumbnail(Slic3r::ExPolygon::Collection->new);
  901. my $cb = sub {
  902. $plater_object->make_thumbnail($self->{model}, $obj_idx);
  903. if ($Slic3r::have_threads) {
  904. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $THUMBNAIL_DONE_EVENT, shared_clone([ $obj_idx ])));
  905. Slic3r::thread_cleanup();
  906. threads->exit;
  907. } else {
  908. $self->on_thumbnail_made($obj_idx);
  909. }
  910. };
  911. @_ = ();
  912. $Slic3r::have_threads
  913. ? threads->create(sub { $cb->(); Slic3r::thread_cleanup(); })->detach
  914. : $cb->();
  915. }
  916. sub on_thumbnail_made {
  917. my $self = shift;
  918. my ($obj_idx) = @_;
  919. $self->{objects}[$obj_idx]->transform_thumbnail($self->{model}, $obj_idx);
  920. $self->update;
  921. $self->{canvas}->Refresh;
  922. }
  923. # this method gets called whenever print center is changed or the objects' bounding box changes
  924. # (i.e. when an object is added/removed/moved/rotated/scaled)
  925. sub update {
  926. my ($self, $force_autocenter) = @_;
  927. if ($Slic3r::GUI::Settings->{_}{autocenter} || $force_autocenter) {
  928. $self->{model}->center_instances_around_point($self->bed_centerf);
  929. }
  930. # sync model and print object instances
  931. for my $obj_idx (0..$#{$self->{objects}}) {
  932. my $model_object = $self->{model}->objects->[$obj_idx];
  933. my $print_object = $self->{print}->objects->[$obj_idx];
  934. $print_object->delete_all_copies;
  935. $print_object->add_copy(@{$_->offset}) for @{$model_object->instances};
  936. }
  937. $self->{canvas}->Refresh;
  938. }
  939. sub on_config_change {
  940. my $self = shift;
  941. my ($opt_key, $value) = @_;
  942. if ($opt_key eq 'extruders_count' && defined $value) {
  943. my $choices = $self->{preset_choosers}{filament};
  944. while (@$choices < $value) {
  945. my @presets = $choices->[0]->GetStrings;
  946. push @$choices, Wx::Choice->new($self, -1, wxDefaultPosition, [150, -1], [@presets]);
  947. $choices->[-1]->SetFont($Slic3r::GUI::small_font);
  948. $self->{preset_choosers_sizers}{filament}->Add($choices->[-1], 0, wxEXPAND | wxBOTTOM, FILAMENT_CHOOSERS_SPACING);
  949. EVT_CHOICE($choices->[-1], $choices->[-1], sub { $self->on_select_preset('filament', @_) });
  950. my $i = first { $choices->[-1]->GetString($_) eq ($Slic3r::GUI::Settings->{presets}{"filament_" . $#$choices} || '') } 0 .. $#presets;
  951. $choices->[-1]->SetSelection($i || 0);
  952. }
  953. while (@$choices > $value) {
  954. $self->{preset_choosers_sizers}{filament}->Remove(-1);
  955. $choices->[-1]->Destroy;
  956. pop @$choices;
  957. }
  958. $self->Layout;
  959. } elsif ($self->{config}->has($opt_key)) {
  960. $self->{config}->set($opt_key, $value);
  961. if ($opt_key eq 'bed_shape') {
  962. $self->{canvas}->update_bed_size;
  963. $self->update;
  964. }
  965. }
  966. return if !$self->GetFrame->is_loaded;
  967. # (re)start timer
  968. $self->schedule_background_process;
  969. }
  970. sub list_item_deselected {
  971. my ($self, $event) = @_;
  972. return if $PreventListEvents;
  973. if ($self->{list}->GetFirstSelected == -1) {
  974. $self->select_object(undef);
  975. $self->{canvas}->Refresh;
  976. }
  977. }
  978. sub list_item_selected {
  979. my ($self, $event) = @_;
  980. return if $PreventListEvents;
  981. my $obj_idx = $event->GetIndex;
  982. $self->select_object($obj_idx);
  983. $self->{canvas}->Refresh;
  984. }
  985. sub list_item_activated {
  986. my ($self, $event, $obj_idx) = @_;
  987. $obj_idx //= $event->GetIndex;
  988. $self->object_cut_dialog($obj_idx);
  989. }
  990. sub object_cut_dialog {
  991. my $self = shift;
  992. my ($obj_idx) = @_;
  993. if (!defined $obj_idx) {
  994. ($obj_idx, undef) = $self->selected_object;
  995. }
  996. if (!$Slic3r::GUI::have_OpenGL) {
  997. Slic3r::GUI::show_error($self, "Please install the OpenGL modules to use this feature (see build instructions).");
  998. return;
  999. }
  1000. my $dlg = Slic3r::GUI::Plater::ObjectCutDialog->new($self,
  1001. object => $self->{objects}[$obj_idx],
  1002. model_object => $self->{model}->objects->[$obj_idx],
  1003. );
  1004. $dlg->ShowModal;
  1005. if (my @new_objects = $dlg->NewModelObjects) {
  1006. $self->remove($obj_idx);
  1007. $self->load_model_objects(grep defined($_), @new_objects);
  1008. $self->arrange;
  1009. }
  1010. }
  1011. sub object_settings_dialog {
  1012. my $self = shift;
  1013. my ($obj_idx) = @_;
  1014. if (!defined $obj_idx) {
  1015. ($obj_idx, undef) = $self->selected_object;
  1016. }
  1017. my $model_object = $self->{model}->objects->[$obj_idx];
  1018. # validate config before opening the settings dialog because
  1019. # that dialog can't be closed if validation fails, but user
  1020. # can't fix any error which is outside that dialog
  1021. return unless $self->validate_config;
  1022. my $dlg = Slic3r::GUI::Plater::ObjectSettingsDialog->new($self,
  1023. object => $self->{objects}[$obj_idx],
  1024. model_object => $model_object,
  1025. );
  1026. $dlg->ShowModal;
  1027. # update thumbnail since parts may have changed
  1028. if ($dlg->PartsChanged) {
  1029. $self->make_thumbnail($obj_idx);
  1030. }
  1031. # update print
  1032. if ($dlg->PartsChanged || $dlg->PartSettingsChanged) {
  1033. $self->{print}->reload_object($obj_idx);
  1034. $self->schedule_background_process;
  1035. }
  1036. }
  1037. sub object_list_changed {
  1038. my $self = shift;
  1039. my $have_objects = @{$self->{objects}} ? 1 : 0;
  1040. my $method = $have_objects ? 'Enable' : 'Disable';
  1041. $self->{"btn_$_"}->$method
  1042. for grep $self->{"btn_$_"}, qw(reset arrange export_gcode export_stl);
  1043. if ($self->{htoolbar}) {
  1044. $self->{htoolbar}->EnableTool($_, $have_objects)
  1045. for (TB_RESET, TB_ARRANGE);
  1046. }
  1047. }
  1048. sub selection_changed {
  1049. my $self = shift;
  1050. my ($obj_idx, $object) = $self->selected_object;
  1051. my $have_sel = defined $obj_idx;
  1052. my $method = $have_sel ? 'Enable' : 'Disable';
  1053. $self->{"btn_$_"}->$method
  1054. for grep $self->{"btn_$_"}, qw(remove increase decrease rotate45cw rotate45ccw changescale split view settings);
  1055. if ($self->{htoolbar}) {
  1056. $self->{htoolbar}->EnableTool($_, $have_sel)
  1057. for (TB_REMOVE, TB_MORE, TB_FEWER, TB_45CW, TB_45CCW, TB_SCALE, TB_SPLIT, TB_VIEW, TB_SETTINGS);
  1058. }
  1059. if ($self->{object_info_size}) { # have we already loaded the info pane?
  1060. if ($have_sel) {
  1061. my $model_object = $self->{model}->objects->[$obj_idx];
  1062. my $model_instance = $model_object->instances->[0];
  1063. $self->{object_info_size}->SetLabel(sprintf("%.2f x %.2f x %.2f", @{$model_object->instance_bounding_box(0)->size}));
  1064. $self->{object_info_materials}->SetLabel($model_object->materials_count);
  1065. if (my $stats = $model_object->mesh_stats) {
  1066. $self->{object_info_volume}->SetLabel(sprintf('%.2f', $stats->{volume} * ($model_instance->scaling_factor**3)));
  1067. $self->{object_info_facets}->SetLabel(sprintf('%d (%d shells)', $model_object->facets_count, $stats->{number_of_parts}));
  1068. if (my $errors = sum(@$stats{qw(degenerate_facets edges_fixed facets_removed facets_added facets_reversed backwards_edges)})) {
  1069. $self->{object_info_manifold}->SetLabel(sprintf("Auto-repaired (%d errors)", $errors));
  1070. $self->{object_info_manifold_warning_icon}->Show;
  1071. # we don't show normals_fixed because we never provide normals
  1072. # to admesh, so it generates normals for all facets
  1073. my $message = sprintf '%d degenerate facets, %d edges fixed, %d facets removed, %d facets added, %d facets reversed, %d backwards edges',
  1074. @$stats{qw(degenerate_facets edges_fixed facets_removed facets_added facets_reversed backwards_edges)};
  1075. $self->{object_info_manifold}->SetToolTipString($message);
  1076. $self->{object_info_manifold_warning_icon}->SetToolTipString($message);
  1077. } else {
  1078. $self->{object_info_manifold}->SetLabel("Yes");
  1079. }
  1080. } else {
  1081. $self->{object_info_facets}->SetLabel($object->facets);
  1082. }
  1083. } else {
  1084. $self->{"object_info_$_"}->SetLabel("") for qw(size volume facets materials manifold);
  1085. $self->{object_info_manifold_warning_icon}->Hide;
  1086. $self->{object_info_manifold}->SetToolTipString("");
  1087. }
  1088. $self->Layout;
  1089. }
  1090. # prepagate the event to the frame (a custom Wx event would be cleaner)
  1091. $self->GetFrame->on_plater_selection_changed($have_sel);
  1092. }
  1093. sub select_object {
  1094. my ($self, $obj_idx) = @_;
  1095. $_->selected(0) for @{ $self->{objects} };
  1096. if (defined $obj_idx) {
  1097. $self->{objects}->[$obj_idx]->selected(1);
  1098. # We use this flag to avoid circular event handling
  1099. # Select() happens to fire a wxEVT_LIST_ITEM_SELECTED on Windows,
  1100. # whose event handler calls this method again and again and again
  1101. $PreventListEvents = 1;
  1102. $self->{list}->Select($obj_idx, 1);
  1103. $PreventListEvents = 0;
  1104. } else {
  1105. # TODO: deselect all in list
  1106. }
  1107. $self->selection_changed(1);
  1108. }
  1109. sub selected_object {
  1110. my $self = shift;
  1111. my $obj_idx = first { $self->{objects}[$_]->selected } 0..$#{ $self->{objects} };
  1112. return undef if !defined $obj_idx;
  1113. return ($obj_idx, $self->{objects}[$obj_idx]),
  1114. }
  1115. sub validate_config {
  1116. my $self = shift;
  1117. eval {
  1118. $self->GetFrame->config->validate;
  1119. };
  1120. return 0 if Slic3r::GUI::catch_error($self);
  1121. return 1;
  1122. }
  1123. sub statusbar {
  1124. my $self = shift;
  1125. return $self->GetFrame->{statusbar};
  1126. }
  1127. sub object_menu {
  1128. my ($self) = @_;
  1129. my $frame = $self->GetFrame;
  1130. my $menu = Wx::Menu->new;
  1131. $frame->_append_menu_item($menu, "Delete\tCtrl+Del", 'Remove the selected object', sub {
  1132. $self->remove;
  1133. });
  1134. $frame->_append_menu_item($menu, "Increase copies\tCtrl++", 'Place one more copy of the selected object', sub {
  1135. $self->increase;
  1136. });
  1137. $frame->_append_menu_item($menu, "Decrease copies\tCtrl+-", 'Remove one copy of the selected object', sub {
  1138. $self->decrease;
  1139. });
  1140. $menu->AppendSeparator();
  1141. $frame->_append_menu_item($menu, "Rotate 45° clockwise", 'Rotate the selected object by 45° clockwise', sub {
  1142. $self->rotate(-45);
  1143. });
  1144. $frame->_append_menu_item($menu, "Rotate 45° counter-clockwise", 'Rotate the selected object by 45° counter-clockwise', sub {
  1145. $self->rotate(+45);
  1146. });
  1147. my $rotateMenu = Wx::Menu->new;
  1148. $menu->AppendSubMenu($rotateMenu, "Rotate…", 'Rotate the selected object by an arbitrary angle');
  1149. $frame->_append_menu_item($rotateMenu, "Around X axis…", 'Rotate the selected object by an arbitrary angle around X axis', sub {
  1150. $self->rotate(undef, X);
  1151. });
  1152. $frame->_append_menu_item($rotateMenu, "Around Y axis…", 'Rotate the selected object by an arbitrary angle around Y axis', sub {
  1153. $self->rotate(undef, Y);
  1154. });
  1155. $frame->_append_menu_item($rotateMenu, "Around Z axis…", 'Rotate the selected object by an arbitrary angle around Z axis', sub {
  1156. $self->rotate(undef, Z);
  1157. });
  1158. my $flipMenu = Wx::Menu->new;
  1159. $menu->AppendSubMenu($flipMenu, "Flip…", 'Mirror the selected object');
  1160. $frame->_append_menu_item($flipMenu, "Along X axis…", 'Mirror the selected object along the X axis', sub {
  1161. $self->flip(X);
  1162. });
  1163. $frame->_append_menu_item($flipMenu, "Along Y axis…", 'Mirror the selected object along the Y axis', sub {
  1164. $self->flip(Y);
  1165. });
  1166. $frame->_append_menu_item($flipMenu, "Along Z axis…", 'Mirror the selected object along the Z axis', sub {
  1167. $self->flip(Z);
  1168. });
  1169. $frame->_append_menu_item($menu, "Scale…", 'Scale the selected object by an arbitrary factor', sub {
  1170. $self->changescale;
  1171. });
  1172. $frame->_append_menu_item($menu, "Split", 'Split the selected object into individual parts', sub {
  1173. $self->split_object;
  1174. });
  1175. $frame->_append_menu_item($menu, "View/Cut…", 'Open the 3D cutting tool', sub {
  1176. $self->object_cut_dialog;
  1177. });
  1178. $menu->AppendSeparator();
  1179. $frame->_append_menu_item($menu, "Settings…", 'Open the object editor dialog', sub {
  1180. $self->object_settings_dialog;
  1181. });
  1182. return $menu;
  1183. }
  1184. package Slic3r::GUI::Plater::DropTarget;
  1185. use Wx::DND;
  1186. use base 'Wx::FileDropTarget';
  1187. sub new {
  1188. my $class = shift;
  1189. my ($window) = @_;
  1190. my $self = $class->SUPER::new;
  1191. $self->{window} = $window;
  1192. return $self;
  1193. }
  1194. sub OnDropFiles {
  1195. my $self = shift;
  1196. my ($x, $y, $filenames) = @_;
  1197. # stop scalars leaking on older perl
  1198. # https://rt.perl.org/rt3/Public/Bug/Display.html?id=70602
  1199. @_ = ();
  1200. # only accept STL, OBJ and AMF files
  1201. return 0 if grep !/\.(?:stl|obj|amf(?:\.xml)?)$/i, @$filenames;
  1202. $self->{window}->load_file($_) for @$filenames;
  1203. }
  1204. package Slic3r::GUI::Plater::Object;
  1205. use Moo;
  1206. use List::Util qw(first);
  1207. use Slic3r::Geometry qw(X Y Z MIN MAX deg2rad);
  1208. has 'name' => (is => 'rw', required => 1);
  1209. has 'thumbnail' => (is => 'rw'); # ExPolygon::Collection in scaled model units with no transforms
  1210. has 'transformed_thumbnail' => (is => 'rw');
  1211. 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
  1212. has 'selected' => (is => 'rw', default => sub { 0 });
  1213. sub make_thumbnail {
  1214. my ($self, $model, $obj_idx) = @_;
  1215. # make method idempotent
  1216. $self->thumbnail->clear;
  1217. my $mesh = $model->objects->[$obj_idx]->raw_mesh;
  1218. if ($mesh->facets_count <= 5000) {
  1219. # remove polygons with area <= 1mm
  1220. my $area_threshold = Slic3r::Geometry::scale 1;
  1221. $self->thumbnail->append(
  1222. grep $_->area >= $area_threshold,
  1223. @{ $mesh->horizontal_projection }, # horizontal_projection returns scaled expolygons
  1224. );
  1225. $self->thumbnail->simplify(0.5);
  1226. } else {
  1227. my $convex_hull = Slic3r::ExPolygon->new($mesh->convex_hull);
  1228. $self->thumbnail->append($convex_hull);
  1229. }
  1230. return $self->thumbnail;
  1231. }
  1232. sub transform_thumbnail {
  1233. my ($self, $model, $obj_idx) = @_;
  1234. return unless defined $self->thumbnail;
  1235. my $model_object = $model->objects->[$obj_idx];
  1236. my $model_instance = $model_object->instances->[0];
  1237. # the order of these transformations MUST be the same everywhere, including
  1238. # in Slic3r::Print->add_model_object()
  1239. my $t = $self->thumbnail->clone;
  1240. $t->rotate(deg2rad($model_instance->rotation), Slic3r::Point->new(0,0));
  1241. $t->scale($model_instance->scaling_factor);
  1242. $self->transformed_thumbnail($t);
  1243. }
  1244. 1;