Plater.pm 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471
  1. # The "Plater" tab. It contains the "3D", "2D", "Preview" and "Layers" subtabs.
  2. package Slic3r::GUI::Plater::UndoOperation;
  3. use strict;
  4. use warnings;
  5. sub new{
  6. my $class = shift;
  7. my $self = {
  8. type => shift,
  9. object_identifier => shift,
  10. attributes => shift,
  11. };
  12. bless ($self, $class);
  13. return $self;
  14. }
  15. package Slic3r::GUI::Plater;
  16. use strict;
  17. use warnings;
  18. use utf8;
  19. use File::Basename qw(basename dirname);
  20. use List::Util qw(sum first max none any);
  21. use Slic3r::Geometry qw(X Y Z MIN MAX scale unscale deg2rad rad2deg);
  22. use Math::Trig qw(acos);
  23. use LWP::UserAgent;
  24. use threads::shared qw(shared_clone);
  25. use Wx qw(:button :cursor :dialog :filedialog :keycode :icon :font :id :misc
  26. :panel :sizer :toolbar :window wxTheApp :notebook :combobox);
  27. use Wx::Event qw(EVT_BUTTON EVT_COMMAND EVT_KEY_DOWN EVT_MOUSE_EVENTS EVT_PAINT EVT_TOOL
  28. EVT_CHOICE EVT_COMBOBOX EVT_TIMER EVT_NOTEBOOK_PAGE_CHANGED EVT_LEFT_UP EVT_CLOSE);
  29. use base qw(Wx::Panel Class::Accessor);
  30. use Slic3r::GUI::ColorScheme;
  31. __PACKAGE__->mk_accessors(qw(presets));
  32. use constant TB_ADD => &Wx::NewId;
  33. use constant TB_REMOVE => &Wx::NewId;
  34. use constant TB_RESET => &Wx::NewId;
  35. use constant TB_ARRANGE => &Wx::NewId;
  36. use constant TB_EXPORT_GCODE => &Wx::NewId;
  37. use constant TB_EXPORT_STL => &Wx::NewId;
  38. use constant TB_MORE => &Wx::NewId;
  39. use constant TB_FEWER => &Wx::NewId;
  40. use constant TB_X90CW => &Wx::NewId;
  41. use constant TB_X90CCW => &Wx::NewId;
  42. use constant TB_Y90CW => &Wx::NewId;
  43. use constant TB_Y90CCW => &Wx::NewId;
  44. use constant TB_Z90CW => &Wx::NewId;
  45. use constant TB_Z90CCW => &Wx::NewId;
  46. use constant TB_45CW => &Wx::NewId;
  47. use constant TB_45CCW => &Wx::NewId;
  48. use constant TB_ROTFACE => &Wx::NewId;
  49. use constant TB_SCALE => &Wx::NewId;
  50. use constant TB_SPLIT => &Wx::NewId;
  51. use constant TB_CUT => &Wx::NewId;
  52. use constant TB_LAYERS => &Wx::NewId;
  53. use constant TB_SETTINGS => &Wx::NewId;
  54. # package variables to avoid passing lexicals to threads
  55. our $THUMBNAIL_DONE_EVENT : shared = Wx::NewEventType;
  56. our $PROGRESS_BAR_EVENT : shared = Wx::NewEventType;
  57. our $ERROR_EVENT : shared = Wx::NewEventType;
  58. our $EXPORT_COMPLETED_EVENT : shared = Wx::NewEventType;
  59. our $PROCESS_COMPLETED_EVENT : shared = Wx::NewEventType;
  60. use constant FILAMENT_CHOOSERS_SPACING => 0;
  61. use constant PROCESS_DELAY => 0.5 * 1000; # milliseconds
  62. sub new {
  63. my $class = shift;
  64. my ($parent) = @_;
  65. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
  66. $self->{config} = Slic3r::Config->new_from_defaults(qw(
  67. bed_shape complete_objects extruder_clearance_radius skirts skirt_distance brim_width
  68. serial_port serial_speed host_type print_host octoprint_apikey shortcuts filament_colour
  69. ));
  70. $self->{model} = Slic3r::Model->new;
  71. $self->{print} = Slic3r::Print->new;
  72. $self->{processed} = 0;
  73. # List of Perl objects Slic3r::GUI::Plater::Object, representing a 2D preview of the platter.
  74. $self->{objects} = [];
  75. # Objects identifier used for undo/redo operations. It's a one time id assigned to each newly created object.
  76. $self->{object_identifier} = 0;
  77. # Stack of undo operations.
  78. $self->{undo_stack} = [];
  79. # Stack of redo operations.
  80. $self->{redo_stack} = [];
  81. $self->{print}->set_status_cb(sub {
  82. my ($percent, $message) = @_;
  83. if ($Slic3r::have_threads) {
  84. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $PROGRESS_BAR_EVENT, shared_clone([$percent, $message])));
  85. } else {
  86. $self->on_progress_event($percent, $message);
  87. }
  88. });
  89. # Initialize preview notebook
  90. $self->{preview_notebook} = Wx::Notebook->new($self, -1, wxDefaultPosition, [335,335], wxNB_BOTTOM);
  91. # Initialize handlers for canvases
  92. my $on_select_object = sub {
  93. my ($obj_idx) = @_;
  94. $self->select_object($obj_idx);
  95. };
  96. my $on_double_click = sub {
  97. $self->object_settings_dialog if $self->selected_object;
  98. };
  99. my $on_right_click = sub {
  100. my ($canvas, $click_pos) = @_;
  101. my ($obj_idx, $object) = $self->selected_object;
  102. return if !defined $obj_idx;
  103. my $menu = $self->object_menu;
  104. $canvas->PopupMenu($menu, $click_pos);
  105. $menu->Destroy;
  106. };
  107. my $on_instances_moved = sub {
  108. $self->on_model_change;
  109. };
  110. # Initialize 3D plater
  111. if ($Slic3r::GUI::have_OpenGL) {
  112. $self->{canvas3D} = Slic3r::GUI::Plater::3D->new($self->{preview_notebook}, $self->{objects}, $self->{model}, $self->{config});
  113. $self->{preview_notebook}->AddPage($self->{canvas3D}, '3D');
  114. $self->{canvas3D}->set_on_select_object($on_select_object);
  115. $self->{canvas3D}->set_on_double_click($on_double_click);
  116. $self->{canvas3D}->set_on_right_click(sub { $on_right_click->($self->{canvas3D}, @_); });
  117. $self->{canvas3D}->set_on_instances_moved($on_instances_moved);
  118. $self->{canvas3D}->on_viewport_changed(sub {
  119. $self->{preview3D}->canvas->set_viewport_from_scene($self->{canvas3D});
  120. });
  121. }
  122. # Initialize 2D preview canvas
  123. $self->{canvas} = Slic3r::GUI::Plater::2D->new($self->{preview_notebook}, wxDefaultSize, $self->{objects}, $self->{model}, $self->{config});
  124. $self->{preview_notebook}->AddPage($self->{canvas}, '2D');
  125. $self->{canvas}->on_select_object($on_select_object);
  126. $self->{canvas}->on_double_click($on_double_click);
  127. $self->{canvas}->on_right_click(sub { $on_right_click->($self->{canvas}, @_); });
  128. $self->{canvas}->on_instances_moved($on_instances_moved);
  129. # Initialize 3D toolpaths preview
  130. $self->{preview3D_page_idx} = -1;
  131. if ($Slic3r::GUI::have_OpenGL) {
  132. $self->{preview3D} = Slic3r::GUI::Plater::3DPreview->new($self->{preview_notebook}, $self->{print});
  133. $self->{preview3D}->canvas->on_viewport_changed(sub {
  134. $self->{canvas3D}->set_viewport_from_scene($self->{preview3D}->canvas);
  135. });
  136. $self->{preview_notebook}->AddPage($self->{preview3D}, 'Preview');
  137. $self->{preview3D_page_idx} = $self->{preview_notebook}->GetPageCount-1;
  138. }
  139. # Initialize toolpaths preview
  140. $self->{toolpaths2D_page_idx} = -1;
  141. if ($Slic3r::GUI::have_OpenGL) {
  142. $self->{toolpaths2D} = Slic3r::GUI::Plater::2DToolpaths->new($self->{preview_notebook}, $self->{print});
  143. $self->{preview_notebook}->AddPage($self->{toolpaths2D}, 'Layers');
  144. $self->{toolpaths2D_page_idx} = $self->{preview_notebook}->GetPageCount-1;
  145. }
  146. EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{preview_notebook}, sub {
  147. wxTheApp->CallAfter(sub {
  148. my $sel = $self->{preview_notebook}->GetSelection;
  149. if ($sel == $self->{preview3D_page_idx} || $sel == $self->{toolpaths2D_page_idx}) {
  150. if (!$Slic3r::GUI::Settings->{_}{background_processing} && !$self->{processed}) {
  151. $self->statusbar->SetCancelCallback(sub {
  152. $self->stop_background_process;
  153. $self->statusbar->SetStatusText("Slicing cancelled");
  154. $self->{preview_notebook}->SetSelection(0);
  155. });
  156. $self->start_background_process;
  157. } else {
  158. $self->{preview3D}->load_print
  159. if $sel == $self->{preview3D_page_idx};
  160. }
  161. }
  162. });
  163. });
  164. # toolbar for object manipulation
  165. if (!&Wx::wxMSW) {
  166. Wx::ToolTip::Enable(1);
  167. $self->{htoolbar} = Wx::ToolBar->new($self, -1, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL | wxTB_TEXT | wxBORDER_SIMPLE | wxTAB_TRAVERSAL);
  168. $self->{htoolbar}->AddTool(TB_ADD, "Add…", Wx::Bitmap->new($Slic3r::var->("brick_add.png"), wxBITMAP_TYPE_PNG), '');
  169. $self->{htoolbar}->AddTool(TB_REMOVE, "Delete", Wx::Bitmap->new($Slic3r::var->("brick_delete.png"), wxBITMAP_TYPE_PNG), '');
  170. $self->{htoolbar}->AddTool(TB_RESET, "Delete All", Wx::Bitmap->new($Slic3r::var->("cross.png"), wxBITMAP_TYPE_PNG), '');
  171. $self->{htoolbar}->AddTool(TB_ARRANGE, "Arrange", Wx::Bitmap->new($Slic3r::var->("bricks.png"), wxBITMAP_TYPE_PNG), '');
  172. $self->{htoolbar}->AddSeparator;
  173. $self->{htoolbar}->AddTool(TB_MORE, "More", Wx::Bitmap->new($Slic3r::var->("add.png"), wxBITMAP_TYPE_PNG), '');
  174. $self->{htoolbar}->AddTool(TB_FEWER, "Fewer", Wx::Bitmap->new($Slic3r::var->("delete.png"), wxBITMAP_TYPE_PNG), '');
  175. $self->{htoolbar}->AddSeparator;
  176. if ($Slic3r::GUI::Settings->{_}{rotation_controls} eq 'xyz' || $Slic3r::GUI::Settings->{_}{rotation_controls} eq 'xyz-big') {
  177. $self->{htoolbar}->AddTool(TB_X90CCW, "90° X ccw", Wx::Bitmap->new($Slic3r::var->(@rotateX90ccwT), wxBITMAP_TYPE_PNG), '');
  178. $self->{htoolbar}->AddTool(TB_X90CW, "90° X cw", Wx::Bitmap->new($Slic3r::var->(@rotateX90cwT), wxBITMAP_TYPE_PNG), '');
  179. $self->{htoolbar}->AddTool(TB_Y90CCW, "90° Y ccw", Wx::Bitmap->new($Slic3r::var->(@rotateY90ccwT), wxBITMAP_TYPE_PNG), '');
  180. $self->{htoolbar}->AddTool(TB_Y90CW, "90° Y cw", Wx::Bitmap->new($Slic3r::var->(@rotateY90cwT), wxBITMAP_TYPE_PNG), '');
  181. }
  182. $self->{htoolbar}->AddTool(TB_45CCW, "45° ccw", Wx::Bitmap->new($Slic3r::var->(@rotateZ45ccwT), wxBITMAP_TYPE_PNG), '');
  183. $self->{htoolbar}->AddTool(TB_45CW, "45° cw", Wx::Bitmap->new($Slic3r::var->(@rotateZ45cwT), wxBITMAP_TYPE_PNG), '');
  184. $self->{htoolbar}->AddTool(TB_ROTFACE, "Rotate face", Wx::Bitmap->new($Slic3r::var->("rotate_face.png"), wxBITMAP_TYPE_PNG), '');
  185. $self->{htoolbar}->AddSeparator;
  186. $self->{htoolbar}->AddTool(TB_SCALE, "Scale…", Wx::Bitmap->new($Slic3r::var->("arrow_out.png"), wxBITMAP_TYPE_PNG), '');
  187. $self->{htoolbar}->AddTool(TB_SPLIT, "Split", Wx::Bitmap->new($Slic3r::var->("shape_ungroup.png"), wxBITMAP_TYPE_PNG), '');
  188. $self->{htoolbar}->AddTool(TB_CUT, "Cut…", Wx::Bitmap->new($Slic3r::var->("package.png"), wxBITMAP_TYPE_PNG), '');
  189. $self->{htoolbar}->AddSeparator;
  190. $self->{htoolbar}->AddTool(TB_SETTINGS, "Settings…", Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG), '');
  191. $self->{htoolbar}->AddTool(TB_LAYERS, "Layer heights…", Wx::Bitmap->new($Slic3r::var->("variable_layer_height.png"), wxBITMAP_TYPE_PNG), '');
  192. } else {
  193. my %tbar_buttons = (
  194. add => "Add…",
  195. remove => "Delete",
  196. reset => "Delete All",
  197. arrange => "Arrange",
  198. increase => "",
  199. decrease => "",
  200. rotateX90ccw => "",
  201. rotateX90cw => "",
  202. rotateY90ccw => "",
  203. rotateY90cw => "",
  204. rotateZ90ccw => "",
  205. rotateZ90cw => "",
  206. rotateZ45ccw => "",
  207. rotateZ45cw => "",
  208. rotateFace => "",
  209. changescale => "Scale…",
  210. split => "Split",
  211. cut => "Cut…",
  212. layers => "Layer heights…",
  213. settings => "Settings…",
  214. );
  215. my %tbar_buttonsToolTip = (
  216. add => "Adds new Objects",
  217. remove => "Delete Object",
  218. reset => "Clears the Plate of all Objects",
  219. arrange => "Automaitally arrange Objects",
  220. increase => "Add another Copy",
  221. decrease => "Remove a Copy",
  222. rotateX90ccw => "Rotate around X by 90° counter clockwise",
  223. rotateX90cw => "Rotate around X by 90° clockwise",
  224. rotateY90ccw => "Rotate around Y by 90° counter clockwise",
  225. rotateY90cw => "Rotate around Y by 90° clockwise",
  226. rotateZ90ccw => "Rotate around Z by 90° counter clockwise",
  227. rotateZ90cw => "Rotate around Z by 90° clockwise",
  228. rotateZ45ccw => "Rotate around Z by 45° counter clockwise",
  229. rotateZ45cw => "Rotate around Z by 45° clockwise",
  230. rotateFace => "Rotate to Face",
  231. changescale => "Change Scale of Object",
  232. split => "Split Object",
  233. cut => "Cut Object",
  234. settings => "Settings, Parts, Modifiers and Layers",
  235. );
  236. $self->{btoolbar} = Wx::BoxSizer->new(wxHORIZONTAL);
  237. my @buttons = qw(add remove reset arrange increase decrease);
  238. if ($Slic3r::GUI::Settings->{_}{rotation_controls} eq 'xyz' || $Slic3r::GUI::Settings->{_}{rotation_controls} eq 'xyz-big') {
  239. push @buttons, qw(rotateX90ccw rotateX90cw rotateY90ccw rotateY90cw rotateZ45ccw rotateZ45cw);
  240. }
  241. push @buttons, qw(changescale split cut settings);
  242. for (@buttons) {
  243. $self->{"btn_$_"} = Wx::Button->new($self, -1, $tbar_buttons{$_}, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
  244. $self->{btoolbar}->Add($self->{"btn_$_"});
  245. $self->{"btn_$_"}->SetToolTipString($tbar_buttonsToolTip{$_});
  246. }
  247. }
  248. # right pane buttons
  249. $self->{btn_export_gcode} = Wx::Button->new($self, -1, "Export G-code…", wxDefaultPosition, [-1, -1], wxBU_LEFT);
  250. $self->{btn_print} = Wx::Button->new($self, -1, "Print…", wxDefaultPosition, [-1, -1], wxBU_LEFT);
  251. $self->{btn_send_gcode} = Wx::Button->new($self, -1, "Send to printer", wxDefaultPosition, [-1, -1], wxBU_LEFT);
  252. $self->{btn_export_stl} = Wx::Button->new($self, -1, "Export STL…", wxDefaultPosition, [-1, -1], wxBU_LEFT);
  253. #$self->{btn_export_gcode}->SetFont($Slic3r::GUI::small_font);
  254. #$self->{btn_export_stl}->SetFont($Slic3r::GUI::small_font);
  255. $self->{btn_print}->Hide;
  256. $self->{btn_send_gcode}->Hide;
  257. if ($Slic3r::GUI::have_button_icons) {
  258. my %icons = (
  259. add => "brick_add.png",
  260. remove => "brick_delete.png",
  261. reset => "cross.png",
  262. arrange => "bricks.png",
  263. export_gcode => "cog_go.png",
  264. print => "arrow_up.png",
  265. send_gcode => "arrow_up.png",
  266. export_stl => "brick_go.png",
  267. increase => "add.png",
  268. decrease => "delete.png",
  269. rotateX90cw => @rotateX90cwT,
  270. rotateX90ccw => @rotateX90ccwT,
  271. rotateY90cw => @rotateY90cwT,
  272. rotateY90ccw => @rotateY90ccwT,
  273. rotateZ90cw => @rotateZ90cwT,
  274. rotateZ90ccw => @rotateZ90ccwT,
  275. rotateZ45cw => @rotateZ45cwT,
  276. rotateZ45ccw => @rotateZ45ccwT,
  277. rotateFace => "rotate_face.png",
  278. changescale => "arrow_out.png",
  279. split => "shape_ungroup.png",
  280. cut => "package.png",
  281. layers => "variable_layer_height.png",
  282. settings => "cog.png",
  283. );
  284. for (grep $self->{"btn_$_"}, keys %icons) {
  285. $self->{"btn_$_"}->SetBitmap(Wx::Bitmap->new($Slic3r::var->($icons{$_}), wxBITMAP_TYPE_PNG));
  286. }
  287. }
  288. $self->selection_changed(0);
  289. $self->object_list_changed;
  290. EVT_BUTTON($self, $self->{btn_export_gcode}, sub {
  291. $self->export_gcode;
  292. });
  293. EVT_BUTTON($self, $self->{btn_print}, sub {
  294. $self->{print_file} = $self->export_gcode(Wx::StandardPaths::Get->GetTempDir());
  295. });
  296. EVT_LEFT_UP($self->{btn_send_gcode}, sub {
  297. my (undef, $e) = @_;
  298. my $alt = $e->ShiftDown;
  299. wxTheApp->CallAfter(sub {
  300. $self->prepare_send($alt);
  301. });
  302. });
  303. EVT_BUTTON($self, $self->{btn_export_stl}, \&export_stl);
  304. if ($self->{htoolbar}) {
  305. EVT_TOOL($self, TB_ADD, sub { $self->add; });
  306. EVT_TOOL($self, TB_REMOVE, sub { $self->remove() }); # explicitly pass no argument to remove
  307. EVT_TOOL($self, TB_RESET, sub { $self->reset; });
  308. EVT_TOOL($self, TB_ARRANGE, sub { $self->arrange; });
  309. EVT_TOOL($self, TB_MORE, sub { $self->increase; });
  310. EVT_TOOL($self, TB_FEWER, sub { $self->decrease; });
  311. if ($Slic3r::GUI::Settings->{_}{rotation_controls} eq 'xyz' || $Slic3r::GUI::Settings->{_}{rotation_controls} eq 'xyz-big') {
  312. EVT_TOOL($self, TB_X90CW, sub { $_[0]->rotate(-90, X) });
  313. EVT_TOOL($self, TB_X90CCW, sub { $_[0]->rotate(90, X) });
  314. EVT_TOOL($self, TB_Y90CW, sub { $_[0]->rotate(-90, Y) });
  315. EVT_TOOL($self, TB_Y90CCW, sub { $_[0]->rotate(90, Y) });
  316. EVT_TOOL($self, TB_Z90CW, sub { $_[0]->rotate(-90, Z) });
  317. EVT_TOOL($self, TB_Z90CCW, sub { $_[0]->rotate(90, Z) });
  318. }
  319. EVT_TOOL($self, TB_45CW, sub { $_[0]->rotate(-45, Z) });
  320. EVT_TOOL($self, TB_45CCW, sub { $_[0]->rotate(45, Z) });
  321. EVT_TOOL($self, TB_ROTFACE, sub { $_[0]->rotate_face });
  322. EVT_TOOL($self, TB_SCALE, sub { $self->changescale(undef); });
  323. EVT_TOOL($self, TB_SPLIT, sub { $self->split_object; });
  324. EVT_TOOL($self, TB_CUT, sub { $_[0]->object_cut_dialog });
  325. EVT_TOOL($self, TB_LAYERS, sub { $_[0]->object_layers_dialog });
  326. EVT_TOOL($self, TB_SETTINGS, sub { $_[0]->object_settings_dialog });
  327. } else {
  328. EVT_BUTTON($self, $self->{btn_add}, sub { $self->add; });
  329. EVT_BUTTON($self, $self->{btn_remove}, sub { $self->remove() }); # explicitly pass no argument to remove
  330. EVT_BUTTON($self, $self->{btn_reset}, sub { $self->reset; });
  331. EVT_BUTTON($self, $self->{btn_arrange}, sub { $self->arrange; });
  332. EVT_BUTTON($self, $self->{btn_increase}, sub { $self->increase; });
  333. EVT_BUTTON($self, $self->{btn_decrease}, sub { $self->decrease; });
  334. if ($Slic3r::GUI::Settings->{_}{extended_gui} >= 2) { # if Toolbar enabled
  335. EVT_BUTTON($self, $self->{btn_rotateX90cw}, sub { $_[0]->rotate(-90, X) });
  336. EVT_BUTTON($self, $self->{btn_rotateX90ccw}, sub { $_[0]->rotate(90, X) });
  337. EVT_BUTTON($self, $self->{btn_rotateY90cw}, sub { $_[0]->rotate(-90, Y) });
  338. EVT_BUTTON($self, $self->{btn_rotateY90ccw}, sub { $_[0]->rotate(90, Y) });
  339. EVT_BUTTON($self, $self->{btn_rotateZ90cw}, sub { $_[0]->rotate(-90, Z) });
  340. EVT_BUTTON($self, $self->{btn_rotateZ90ccw}, sub { $_[0]->rotate(90, Z) });
  341. }
  342. EVT_BUTTON($self, $self->{btn_rotateZ45cw}, sub { $_[0]->rotate(-45, Z) });
  343. EVT_BUTTON($self, $self->{btn_rotateZ45ccw}, sub { $_[0]->rotate(45, Z) });
  344. EVT_BUTTON($self, $self->{btn_rotateFace}, sub { $_[0]->rotate_face });
  345. EVT_BUTTON($self, $self->{btn_rotate45cw}, sub { $_[0]->rotate(-45) });
  346. EVT_BUTTON($self, $self->{btn_rotate45ccw}, sub { $_[0]->rotate(45) });
  347. EVT_BUTTON($self, $self->{btn_changescale}, sub { $self->changescale(undef); });
  348. EVT_BUTTON($self, $self->{btn_split}, sub { $self->split_object; });
  349. EVT_BUTTON($self, $self->{btn_cut}, sub { $_[0]->object_cut_dialog });
  350. EVT_BUTTON($self, $self->{btn_layers}, sub { $_[0]->object_layers_dialog });
  351. EVT_BUTTON($self, $self->{btn_settings}, sub { $_[0]->object_settings_dialog });
  352. }
  353. $_->SetDropTarget(Slic3r::GUI::Plater::DropTarget->new($self))
  354. for grep defined($_),
  355. $self, $self->{canvas}, $self->{canvas3D}, $self->{preview3D};
  356. EVT_COMMAND($self, -1, $THUMBNAIL_DONE_EVENT, sub {
  357. my ($self, $event) = @_;
  358. my ($obj_idx) = @{$event->GetData};
  359. return if !$self->{objects}[$obj_idx]; # object was deleted before thumbnail generation completed
  360. $self->on_thumbnail_made($obj_idx);
  361. });
  362. EVT_COMMAND($self, -1, $PROGRESS_BAR_EVENT, sub {
  363. my ($self, $event) = @_;
  364. my ($percent, $message) = @{$event->GetData};
  365. $self->on_progress_event($percent, $message);
  366. });
  367. EVT_COMMAND($self, -1, $ERROR_EVENT, sub {
  368. my ($self, $event) = @_;
  369. Slic3r::GUI::show_error($self, @{$event->GetData});
  370. });
  371. EVT_COMMAND($self, -1, $EXPORT_COMPLETED_EVENT, sub {
  372. my ($self, $event) = @_;
  373. $self->on_export_completed($event->GetData);
  374. });
  375. EVT_COMMAND($self, -1, $PROCESS_COMPLETED_EVENT, sub {
  376. my ($self, $event) = @_;
  377. $self->on_process_completed($event->GetData);
  378. });
  379. if ($Slic3r::have_threads) {
  380. my $timer_id = Wx::NewId();
  381. $self->{apply_config_timer} = Wx::Timer->new($self, $timer_id);
  382. EVT_TIMER($self, $timer_id, sub {
  383. my ($self, $event) = @_;
  384. $self->async_apply_config;
  385. });
  386. }
  387. $self->{canvas}->update_bed_size;
  388. if ($self->{canvas3D}) {
  389. $self->{canvas3D}->update_bed_size;
  390. $self->{canvas3D}->zoom_to_bed;
  391. }
  392. if ($self->{preview3D}) {
  393. $self->{preview3D}->set_bed_shape($self->{config}->bed_shape);
  394. }
  395. {
  396. my $presets = $self->{presets_sizer} = Wx::FlexGridSizer->new(3, 3, 1, 2);
  397. $presets->AddGrowableCol(1, 1);
  398. $presets->SetFlexibleDirection(wxHORIZONTAL);
  399. my %group_labels = (
  400. print => 'Print settings',
  401. filament => 'Filament',
  402. printer => 'Printer',
  403. );
  404. $self->{preset_choosers} = {};
  405. $self->{preset_choosers_names} = {}; # wxChoice* => []
  406. for my $group (qw(print filament printer)) {
  407. # label
  408. my $text = Wx::StaticText->new($self, -1, "$group_labels{$group}:", wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
  409. $text->SetFont($Slic3r::GUI::small_font);
  410. # dropdown control
  411. my $choice = Wx::BitmapComboBox->new($self, -1, "", wxDefaultPosition, wxDefaultSize, [], wxCB_READONLY);
  412. $self->{preset_choosers}{$group} = [$choice];
  413. # setup the listener
  414. EVT_COMBOBOX($choice, $choice, sub {
  415. my ($choice) = @_;
  416. wxTheApp->CallAfter(sub {
  417. $self->_on_change_combobox($group, $choice);
  418. });
  419. });
  420. # settings button
  421. my $settings_btn = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG),
  422. wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
  423. EVT_BUTTON($self, $settings_btn, sub {
  424. $self->show_preset_editor($group, 0);
  425. });
  426. $presets->Add($text, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL | wxRIGHT, 4);
  427. $presets->Add($choice, 1, wxALIGN_CENTER_VERTICAL | wxEXPAND | wxBOTTOM, 0);
  428. $presets->Add($settings_btn, 0, wxALIGN_CENTER_VERTICAL | wxEXPAND | wxLEFT, 3);
  429. }
  430. {
  431. my $o = $self->{settings_override_panel} = Slic3r::GUI::Plater::OverrideSettingsPanel->new($self,
  432. on_change => sub {
  433. my ($opt_key) = @_;
  434. my ($preset) = $self->selected_presets('print');
  435. $preset->load_config;
  436. # If this option is not in the override panel it means it was manually deleted,
  437. # so let's restore the profile value.
  438. if (!$self->{settings_override_config}->has($opt_key)) {
  439. $preset->_dirty_config->set($opt_key, $preset->_config->get($opt_key));
  440. } else {
  441. # Apply the overrides to the current Print preset, potentially making it dirty
  442. $preset->_dirty_config->apply($self->{settings_override_config});
  443. # If this is a configured shortcut (and not just a dirty option),
  444. # save it now.
  445. if (any { $_ eq $opt_key } @{$preset->dirty_config->shortcuts}) {
  446. $preset->save([$opt_key]);
  447. }
  448. }
  449. $self->load_presets;
  450. $self->config_changed;
  451. # Reload the open tab if any
  452. if (my $print_tab = $self->GetFrame->{preset_editor_tabs}{print}) {
  453. $print_tab->load_presets;
  454. $print_tab->reload_preset;
  455. }
  456. });
  457. $o->can_add(0);
  458. $o->can_delete(1);
  459. $o->set_opt_keys([ Slic3r::GUI::PresetEditor::Print->options ]);
  460. $self->{settings_override_config} = Slic3r::Config->new;
  461. $o->set_default_config($self->{settings_override_config});
  462. $o->set_config($self->{settings_override_config});
  463. }
  464. my $object_info_sizer;
  465. {
  466. my $box = Wx::StaticBox->new($self, -1, "Info");
  467. $object_info_sizer = Wx::StaticBoxSizer->new($box, wxVERTICAL);
  468. $object_info_sizer->SetMinSize([350,-1]);
  469. {
  470. my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  471. $object_info_sizer->Add($sizer, 0, wxEXPAND | wxBOTTOM, 5);
  472. my $text = Wx::StaticText->new($self, -1, "Object:", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  473. $text->SetFont($Slic3r::GUI::small_font);
  474. $sizer->Add($text, 0, wxALIGN_CENTER_VERTICAL);
  475. # We supply a bogus width to wxChoice (sizer will override it and stretch
  476. # the control anyway), because if we leave the default (-1) it will stretch
  477. # too much according to the contents, and this is bad with long file names.
  478. $self->{object_info_choice} = Wx::Choice->new($self, -1, wxDefaultPosition, [100,-1], []);
  479. $self->{object_info_choice}->SetFont($Slic3r::GUI::small_font);
  480. $sizer->Add($self->{object_info_choice}, 1, wxALIGN_CENTER_VERTICAL);
  481. EVT_CHOICE($self, $self->{object_info_choice}, sub {
  482. $self->select_object($self->{object_info_choice}->GetSelection);
  483. $self->refresh_canvases;
  484. });
  485. }
  486. my $grid_sizer = Wx::FlexGridSizer->new(3, 4, 5, 5);
  487. $grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
  488. $grid_sizer->AddGrowableCol(1, 1);
  489. $grid_sizer->AddGrowableCol(3, 1);
  490. $object_info_sizer->Add($grid_sizer, 0, wxEXPAND);
  491. my @info = (
  492. copies => "Copies",
  493. size => "Size",
  494. volume => "Volume",
  495. facets => "Facets",
  496. materials => "Materials",
  497. manifold => "Manifold",
  498. );
  499. while (my $field = shift @info) {
  500. my $label = shift @info;
  501. my $text = Wx::StaticText->new($self, -1, "$label:", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  502. $text->SetFont($Slic3r::GUI::small_font);
  503. $grid_sizer->Add($text, 0);
  504. $self->{"object_info_$field"} = Wx::StaticText->new($self, -1, "", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  505. $self->{"object_info_$field"}->SetFont($Slic3r::GUI::small_font);
  506. if ($field eq 'manifold') {
  507. $self->{object_info_manifold_warning_icon} = Wx::StaticBitmap->new($self, -1, Wx::Bitmap->new($Slic3r::var->("error.png"), wxBITMAP_TYPE_PNG));
  508. $self->{object_info_manifold_warning_icon}->Hide;
  509. my $h_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  510. $h_sizer->Add($self->{object_info_manifold_warning_icon}, 0);
  511. $h_sizer->Add($self->{"object_info_$field"}, 0);
  512. $grid_sizer->Add($h_sizer, 0, wxEXPAND);
  513. } else {
  514. $grid_sizer->Add($self->{"object_info_$field"}, 0);
  515. }
  516. }
  517. }
  518. my $print_info_sizer;
  519. {
  520. my $box = Wx::StaticBox->new($self, -1, "Print Summary");
  521. $print_info_sizer = Wx::StaticBoxSizer->new($box, wxVERTICAL);
  522. $print_info_sizer->SetMinSize([350,-1]);
  523. my $grid_sizer = Wx::FlexGridSizer->new(2, 2, 5, 5);
  524. $grid_sizer->SetFlexibleDirection(wxHORIZONTAL);
  525. $grid_sizer->AddGrowableCol(1, 1);
  526. $grid_sizer->AddGrowableCol(3, 1);
  527. $print_info_sizer->Add($grid_sizer, 0, wxEXPAND);
  528. my @info = (
  529. fil => "Used Filament",
  530. cost => "Cost",
  531. );
  532. while (my $field = shift @info) {
  533. my $label = shift @info;
  534. my $text = Wx::StaticText->new($self, -1, "$label:", wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
  535. $text->SetFont($Slic3r::GUI::small_font);
  536. $grid_sizer->Add($text, 0);
  537. $self->{"print_info_$field"} = Wx::StaticText->new($self, -1, "", wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
  538. $self->{"print_info_$field"}->SetFont($Slic3r::GUI::small_font);
  539. $grid_sizer->Add($self->{"print_info_$field"}, 0);
  540. }
  541. $self->{sliced_info_box} = $print_info_sizer;
  542. }
  543. my $buttons_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  544. $buttons_sizer->AddStretchSpacer(1);
  545. $buttons_sizer->Add($self->{btn_export_stl}, 0, wxALIGN_RIGHT, 0);
  546. $buttons_sizer->Add($self->{btn_print}, 0, wxALIGN_RIGHT, 0);
  547. $buttons_sizer->Add($self->{btn_send_gcode}, 0, wxALIGN_RIGHT, 0);
  548. $buttons_sizer->Add($self->{btn_export_gcode}, 0, wxALIGN_RIGHT, 0);
  549. $self->{right_sizer} = my $right_sizer = Wx::BoxSizer->new(wxVERTICAL);
  550. $right_sizer->Add($presets, 0, wxEXPAND | wxTOP, 10) if defined $presets;
  551. $right_sizer->Add($buttons_sizer, 0, wxEXPAND | wxBOTTOM, 5);
  552. $right_sizer->Add($self->{settings_override_panel}, 1, wxEXPAND, 5);
  553. $right_sizer->Add($object_info_sizer, 0, wxEXPAND, 0);
  554. $right_sizer->Add($print_info_sizer, 0, wxEXPAND, 0);
  555. $right_sizer->Hide($print_info_sizer);
  556. my $hsizer = Wx::BoxSizer->new(wxHORIZONTAL);
  557. $hsizer->Add($self->{preview_notebook}, 1, wxEXPAND | wxTOP, 1);
  558. $hsizer->Add($right_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT, 3);
  559. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  560. $sizer->Add($self->{htoolbar}, 0, wxEXPAND, 0) if $self->{htoolbar};
  561. $sizer->Add($self->{btoolbar}, 0, wxEXPAND, 0) if $self->{btoolbar};
  562. $sizer->Add($hsizer, 1, wxEXPAND, 0);
  563. $sizer->SetSizeHints($self);
  564. $self->SetSizer($sizer);
  565. }
  566. $self->load_presets;
  567. $self->_on_select_preset($_) for qw(printer filament print);
  568. return $self;
  569. }
  570. sub prompt_unsaved_changes {
  571. my ($self) = @_;
  572. foreach my $group (qw(printer filament print)) {
  573. foreach my $choice (@{$self->{preset_choosers}{$group}}) {
  574. my $pp = $self->{preset_choosers_names}{$choice};
  575. for my $i (0..$#$pp) {
  576. my $preset = first { $_->name eq $pp->[$i] } @{wxTheApp->presets->{$group}};
  577. if (!$preset->prompt_unsaved_changes($self)) {
  578. # Restore the previous one
  579. $choice->SetSelection($i);
  580. return 0;
  581. }
  582. }
  583. }
  584. }
  585. return 1;
  586. }
  587. sub _on_change_combobox {
  588. my ($self, $group, $choice) = @_;
  589. if (0) {
  590. # This code is disabled because wxPerl doesn't provide GetCurrentSelection
  591. my $current_name = $self->{preset_choosers_names}{$choice}[$choice->GetCurrentSelection];
  592. my $current = first { $_->name eq $current_name } @{wxTheApp->presets->{$group}};
  593. if (!$current->prompt_unsaved_changes($self)) {
  594. # Restore the previous one
  595. $choice->SetSelection($choice->GetCurrentSelection);
  596. return;
  597. }
  598. } else {
  599. return 0 if !$self->prompt_unsaved_changes;
  600. }
  601. wxTheApp->CallAfter(sub {
  602. $self->_on_select_preset($group);
  603. # This will remove the "(modified)" mark from any dirty preset handled here.
  604. $self->load_presets;
  605. });
  606. }
  607. sub _on_select_preset {
  608. my ($self, $group) = @_;
  609. my @presets = $self->selected_presets($group);
  610. my $s_presets = $Slic3r::GUI::Settings->{presets};
  611. my $changed = !$s_presets->{$group} || $s_presets->{$group} ne $presets[0]->name;
  612. $s_presets->{$group} = $presets[0]->name;
  613. $s_presets->{"${group}_${_}"} = $presets[$_]->name for 1..$#presets;
  614. wxTheApp->save_settings;
  615. # Ignore overrides in the plater, we only care about the preset configs.
  616. my $config = $self->config(1);
  617. $self->on_extruders_change(scalar @{$config->get('nozzle_diameter')});
  618. if ($group eq 'print') {
  619. my $o_config = $self->{settings_override_config};
  620. my $o_panel = $self->{settings_override_panel};
  621. my $shortcuts = $config->get('shortcuts');
  622. # Re-populate the override panel with the configured shortcuts
  623. # and the dirty options.
  624. $o_config->clear;
  625. foreach my $opt_key (@$shortcuts, $presets[0]->dirty_options) {
  626. # Don't add shortcut for shortcuts!
  627. next if $opt_key eq 'shortcuts';
  628. $o_config->set($opt_key, $config->get($opt_key));
  629. }
  630. $o_panel->set_default_config($config);
  631. $o_panel->set_fixed_options(\@$shortcuts);
  632. $o_panel->update_optgroup;
  633. } elsif ($group eq 'printer') {
  634. # reload print and filament settings to honor their compatible_printer options
  635. $self->load_presets;
  636. }
  637. $self->config_changed;
  638. }
  639. sub load_config {
  640. my ($self, $config) = @_;
  641. # This method is called with the CLI options.
  642. # We add them to the visible overrides.
  643. $self->{settings_override_config}->apply($config);
  644. $self->{settings_override_panel}->update_optgroup;
  645. $self->config_changed;
  646. }
  647. sub GetFrame {
  648. my ($self) = @_;
  649. return &Wx::GetTopLevelParent($self);
  650. }
  651. sub load_presets {
  652. my ($self) = @_;
  653. my $selected_printer_name;
  654. foreach my $group (qw(printer filament print)) {
  655. my @presets = @{wxTheApp->presets->{$group}};
  656. # Skip presets not compatible with the selected printer, if they
  657. # have other compatible printers configured (and at least one of them exists).
  658. if ($group eq 'filament' || $group eq 'print') {
  659. my %printer_names = map { $_->name => 1 } @{ wxTheApp->presets->{printer} };
  660. for (my $i = 0; $i <= $#presets; ++$i) {
  661. my $config = $presets[$i]->dirty_config;
  662. next if !$config->has('compatible_printers');
  663. my @compat = @{$config->compatible_printers};
  664. if (@compat
  665. && (none { $_ eq $selected_printer_name } @compat)
  666. && (any { $printer_names{$_} } @compat)) {
  667. splice @presets, $i, 1;
  668. --$i;
  669. }
  670. }
  671. }
  672. # Only show the default presets if we have no other presets.
  673. if (@presets > 1) {
  674. @presets = grep { !$_->default } @presets;
  675. }
  676. # get the wxChoice objects for this group
  677. my @choosers = @{ $self->{preset_choosers}{$group} };
  678. # find the currently selected one(s) according to the saved file
  679. my @sel = ();
  680. if (my $current = $Slic3r::GUI::Settings->{presets}{$group}) {
  681. push @sel, grep defined, first { $presets[$_]->name eq $current } 0..$#presets;
  682. }
  683. for my $i (1..(@choosers-1)) {
  684. if (my $current = $Slic3r::GUI::Settings->{presets}{"${group}_$i"}) {
  685. push @sel, grep defined, first { $presets[$_]->name eq $current } 0..$#presets;
  686. }
  687. }
  688. @sel = (0) if !@sel;
  689. # populate the wxChoice objects
  690. my @preset_names = ();
  691. foreach my $choice (@choosers) {
  692. $choice->Clear;
  693. $self->{preset_choosers_names}{$choice} = [];
  694. foreach my $preset (@presets) {
  695. # load/generate the proper icon
  696. my $bitmap;
  697. if ($group eq 'filament') {
  698. my $config = $preset->dirty_config;
  699. if ($preset->default || !$config->has('filament_colour')) {
  700. $bitmap = Wx::Bitmap->new($Slic3r::var->("spool.png"), wxBITMAP_TYPE_PNG);
  701. } else {
  702. my $rgb_hex = $config->filament_colour->[0];
  703. $rgb_hex =~ s/^#//;
  704. my @rgb = unpack 'C*', pack 'H*', $rgb_hex;
  705. my $image = Wx::Image->new(16,16);
  706. $image->SetRGB(Wx::Rect->new(0,0,16,16), @rgb);
  707. $bitmap = Wx::Bitmap->new($image);
  708. }
  709. } elsif ($group eq 'print') {
  710. $bitmap = Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG);
  711. } elsif ($group eq 'printer') {
  712. $bitmap = Wx::Bitmap->new($Slic3r::var->("printer_empty.png"), wxBITMAP_TYPE_PNG);
  713. }
  714. $choice->AppendString($preset->dropdown_name, $bitmap);
  715. push @{$self->{preset_choosers_names}{$choice}}, $preset->name;
  716. }
  717. my $selected = shift @sel;
  718. if (defined $selected && $selected <= $#presets) {
  719. # call SetSelection() only after SetString() otherwise the new string
  720. # won't be picked up as the visible string
  721. $choice->SetSelection($selected);
  722. my $preset_name = $self->{preset_choosers_names}{$choice}[$selected];
  723. push @preset_names, $preset_name;
  724. # TODO: populate other filament preset placeholders
  725. $selected_printer_name = $preset_name if $group eq 'printer';
  726. }
  727. }
  728. $self->{print}->placeholder_parser->set_multiple("${group}_preset", [ @preset_names ]);
  729. }
  730. }
  731. sub select_preset_by_name {
  732. my ($self, $name, $group, $n) = @_;
  733. # $n is optional
  734. my $presets = wxTheApp->presets->{$group};
  735. my $choosers = $self->{preset_choosers}{$group};
  736. my $names = $self->{preset_choosers_names}{$choosers->[0]};
  737. my $i = first { $names->[$_] eq $name } 0..$#$names;
  738. return if !defined $i;
  739. if (defined $n && $n <= $#$choosers) {
  740. $choosers->[$n]->SetSelection($i);
  741. } else {
  742. $_->SetSelection($i) for @$choosers;
  743. }
  744. $self->_on_select_preset($group);
  745. }
  746. sub selected_presets {
  747. my ($self, $group) = @_;
  748. my %presets = ();
  749. foreach my $group (qw(printer filament print)) {
  750. $presets{$group} = [];
  751. foreach my $choice (@{$self->{preset_choosers}{$group}}) {
  752. my $sel = $choice->GetSelection;
  753. $sel = 0 if $sel == -1;
  754. push @{ $presets{$group} },
  755. grep { $_->name eq $self->{preset_choosers_names}{$choice}[$sel] }
  756. @{wxTheApp->presets->{$group}};
  757. }
  758. }
  759. return $group ? @{$presets{$group}} : %presets;
  760. }
  761. sub show_preset_editor {
  762. my ($self, $group, $i, $panel) = @_;
  763. wxTheApp->CallAfter(sub {
  764. my @presets = $self->selected_presets($group);
  765. my $preset_editor;
  766. my $dlg;
  767. my $mainframe = $self->GetFrame;
  768. my $tabpanel = $mainframe->{tabpanel};
  769. if (exists $mainframe->{preset_editor_tabs}{$group}) {
  770. my $tabindex = 0;
  771. $tabindex = 1 if $Slic3r::GUI::Settings->{_}{show_host};
  772. $tabindex += 1 if $group eq 'print';
  773. $tabindex += 2 if $group eq 'filament';
  774. $tabindex += 3 if $group eq 'printer';
  775. # we already have an open editor
  776. $tabpanel->SetSelection($tabindex);
  777. return;
  778. } elsif ($Slic3r::GUI::Settings->{_}{tabbed_preset_editors}) {
  779. my $class = "Slic3r::GUI::PresetEditor::" . ucfirst($group);
  780. $mainframe->{preset_editor_tabs}{$group} = $preset_editor = $class->new($tabpanel);
  781. $tabpanel->AddPage($preset_editor, ucfirst($group) . " Settings");
  782. } else {
  783. my $class = "Slic3r::GUI::PresetEditorDialog::" . ucfirst($group);
  784. $dlg = $class->new($self);
  785. $preset_editor = $dlg->preset_editor;
  786. }
  787. $preset_editor->select_preset_by_name($presets[$i // 0]->name);
  788. $preset_editor->on_value_change(sub {
  789. # Re-load the presets in order to toggle the (modified) suffix
  790. $self->load_presets;
  791. # Update shortcuts
  792. $self->_on_select_preset($group);
  793. # Use the new config wherever we actually use its contents
  794. $self->config_changed;
  795. });
  796. my $cb = sub {
  797. my ($group, $preset) = @_;
  798. # Re-load the presets as they might have changed.
  799. $self->load_presets;
  800. # Select the preset in plater too
  801. $self->select_preset_by_name($preset->name, $group, $i, 1);
  802. };
  803. $preset_editor->on_select_preset($cb);
  804. $preset_editor->on_save_preset($cb);
  805. if ($dlg) {
  806. $dlg->Show;
  807. }
  808. });
  809. }
  810. # Returns the current config by merging the selected presets and the overrides.
  811. sub config {
  812. my ($self, $ignore_overrides) = @_;
  813. # use a DynamicConfig because FullPrintConfig is not enough
  814. my $config = Slic3r::Config->new_from_defaults;
  815. # get defaults also for the values tracked by the Plater's config
  816. # (for example 'shortcuts')
  817. $config->apply(Slic3r::Config->new_from_defaults(@{$self->{config}->get_keys}));
  818. my %classes = map { $_ => "Slic3r::GUI::PresetEditor::".ucfirst($_) }
  819. qw(print filament printer);
  820. my %presets = $self->selected_presets;
  821. $config->apply($_->dirty_config) for @{ $presets{printer} };
  822. if (@{ $presets{filament} }) {
  823. my $filament_config = $presets{filament}[0]->dirty_config;
  824. for my $i (1..$#{ $presets{filament} }) {
  825. my $preset = $presets{filament}[$i];
  826. my $config = $preset->dirty_config;
  827. foreach my $opt_key (@{$config->get_keys}) {
  828. if ($filament_config->has($opt_key)) {
  829. my $value = $filament_config->get($opt_key);
  830. next unless ref $value eq 'ARRAY';
  831. $value->[$i] = $config->get($opt_key)->[0];
  832. $filament_config->set($opt_key, $value);
  833. }
  834. }
  835. }
  836. $config->apply($filament_config);
  837. }
  838. $config->apply($_->dirty_config) for @{ $presets{print} };
  839. $config->apply($self->{settings_override_config})
  840. unless $ignore_overrides;
  841. return $config;
  842. }
  843. sub get_object_index {
  844. my $self = shift;
  845. my ($object_indentifier) = @_;
  846. return undef if !defined $object_indentifier;
  847. for (my $i = 0; $i <= $#{$self->{objects}}; $i++){
  848. if ($self->{objects}->[$i]->identifier eq $object_indentifier) {
  849. return $i;
  850. }
  851. }
  852. return undef;
  853. }
  854. sub add_undo_operation {
  855. my $self = shift;
  856. my @parameters = @_;
  857. my $type = $parameters[0];
  858. my $object_identifier = $parameters[1];
  859. my @attributes = @parameters[2..$#parameters]; # operation values.
  860. my $new_undo_operation = new Slic3r::GUI::Plater::UndoOperation($type, $object_identifier, \@attributes);
  861. push @{$self->{undo_stack}}, $new_undo_operation;
  862. $self->{redo_stack} = [];
  863. $self->limit_undo_operations(8); # Current limit of undo/redo operations.
  864. $self->GetFrame->on_undo_redo_stacks_changed;
  865. return $new_undo_operation;
  866. }
  867. sub limit_undo_operations {
  868. my ($self, $limit)= @_;
  869. return if !defined $limit;
  870. # Delete undo operations succeeded by 4 operations or more to save memory.
  871. while ($#{$self->{undo_stack}} + 1 > $limit) {
  872. print "Removing an old operation.\n";
  873. splice @{$self->{undo_stack}}, 0, 1;
  874. }
  875. }
  876. sub undo {
  877. my $self = shift;
  878. my $operation = pop @{$self->{undo_stack}};
  879. return if !defined $operation;
  880. push @{$self->{redo_stack}}, $operation;
  881. my $type = $operation->{type};
  882. if ($type eq "ROTATE") {
  883. my $object_id = $operation->{object_identifier};
  884. my $obj_idx = $self->get_object_index($object_id);
  885. $self->select_object($obj_idx);
  886. my $angle = $operation->{attributes}->[0];
  887. my $axis = $operation->{attributes}->[1];
  888. $self->rotate(-1 * $angle, $axis, 'true'); # Apply inverse transformation.
  889. } elsif ($type eq "INCREASE") {
  890. my $object_id = $operation->{object_identifier};
  891. my $obj_idx = $self->get_object_index($object_id);
  892. $self->select_object($obj_idx);
  893. my $copies = $operation->{attributes}->[0];
  894. $self->decrease($copies, 'true');
  895. } elsif ($type eq "DECREASE") {
  896. my $object_id = $operation->{object_identifier};
  897. my $obj_idx = $self->get_object_index($object_id);
  898. $self->select_object($obj_idx);
  899. my $copies = $operation->{attributes}->[0];
  900. $self->increase($copies, 'true');
  901. } elsif ($type eq "MIRROR") {
  902. my $object_id = $operation->{object_identifier};
  903. my $obj_idx = $self->get_object_index($object_id);
  904. $self->select_object($obj_idx);
  905. my $axis = $operation->{attributes}->[0];
  906. $self->mirror($axis, 'true');
  907. } elsif ($type eq "REMOVE") {
  908. my $_model = $operation->{attributes}->[0];
  909. $self->load_model_objects(@{$_model->objects});
  910. $self->{object_identifier}--; # Decrement the identifier as we will change the object identifier with the saved one.
  911. $self->{objects}->[-1]->identifier($operation->{object_identifier});
  912. } elsif ($type eq "CUT" || $type eq "SPLIT") {
  913. # Delete the produced objects.
  914. my $obj_identifiers_start = $operation->{attributes}->[2];
  915. for (my $i_object = 0; $i_object < $#{$operation->{attributes}->[1]->objects} + 1; $i_object++) {
  916. $self->remove($self->get_object_index($obj_identifiers_start++), 'true');
  917. }
  918. # Add the original object.
  919. $self->load_model_objects(@{$operation->{attributes}->[0]->objects});
  920. $self->{object_identifier}--;
  921. $self->{objects}->[-1]->identifier($operation->{object_identifier}); # Add the original assigned identifier.
  922. } elsif ($type eq "CHANGE_SCALE") {
  923. my $object_id = $operation->{object_identifier};
  924. my $obj_idx = $self->get_object_index($object_id);
  925. $self->select_object($obj_idx);
  926. my $axis = $operation->{attributes}->[0];
  927. my $tosize = $operation->{attributes}->[1];
  928. my $saved_scale = $operation->{attributes}->[3];
  929. $self->changescale($axis, $tosize, $saved_scale, 'true');
  930. } elsif ($type eq "RESET") {
  931. # Revert changes to the plater object identifier. It's modified when adding new objects only not when undo/redo is executed.
  932. my $current_objects_identifier = $self->{object_identifier};
  933. my $_model = $operation->{attributes}->[0];
  934. $self->load_model_objects(@{$_model->objects});
  935. $self->{object_identifier} = $current_objects_identifier;
  936. # don't forget the identifiers.
  937. my $objects_count = $#{$operation->{attributes}->[0]->objects} + 1;
  938. foreach my $identifier (@{$operation->{attributes}->[1]})
  939. {
  940. $self->{objects}->[-$objects_count]->identifier($identifier);
  941. $objects_count--;
  942. }
  943. } elsif ($type eq "ADD") {
  944. my $objects_count = $#{$operation->{attributes}->[0]->objects} + 1;
  945. my $identifier_start = $operation->{attributes}->[1];
  946. for (my $identifier = $identifier_start; $identifier < $objects_count + $identifier_start; $identifier++) {
  947. my $obj_idx = $self->get_object_index($identifier);
  948. $self->remove($obj_idx, 'true');
  949. }
  950. } elsif ($type eq "GROUP"){
  951. my @ops = @{$operation->{attributes}};
  952. push @{$self->{undo_stack}}, @ops;
  953. foreach my $op (@ops) {
  954. $self->undo;
  955. pop @{$self->{redo_stack}};
  956. }
  957. }
  958. }
  959. sub redo {
  960. my $self = shift;
  961. my $operation = pop @{$self->{redo_stack}};
  962. return if !defined $operation;
  963. push @{$self->{undo_stack}}, $operation;
  964. my $type = $operation->{type};
  965. if ($type eq "ROTATE") {
  966. my $object_id = $operation->{object_identifier};
  967. my $obj_idx = $self->get_object_index($object_id);
  968. $self->select_object($obj_idx);
  969. my $angle = $operation->{attributes}->[0];
  970. my $axis = $operation->{attributes}->[1];
  971. $self->rotate($angle, $axis, 'true');
  972. } elsif ($type eq "INCREASE") {
  973. my $object_id = $operation->{object_identifier};
  974. my $obj_idx = $self->get_object_index($object_id);
  975. $self->select_object($obj_idx);
  976. my $copies = $operation->{attributes}->[0];
  977. $self->increase($copies, 'true');
  978. } elsif ($type eq "DECREASE") {
  979. my $object_id = $operation->{object_identifier};
  980. my $obj_idx = $self->get_object_index($object_id);
  981. $self->select_object($obj_idx);
  982. my $copies = $operation->{attributes}->[0];
  983. $self->decrease($copies, 'true');
  984. } elsif ($type eq "MIRROR") {
  985. my $object_id = $operation->{object_identifier};
  986. my $obj_idx = $self->get_object_index($object_id);
  987. $self->select_object($obj_idx);
  988. my $axis = $operation->{attributes}->[0];
  989. $self->mirror($axis, 'true');
  990. } elsif ($type eq "REMOVE") {
  991. my $object_id = $operation->{object_identifier};
  992. my $obj_idx = $self->get_object_index($object_id);
  993. $self->select_object($obj_idx);
  994. $self->remove(undef, 'true');
  995. } elsif ($type eq "CUT" || $type eq "SPLIT") {
  996. # Delete the org objects.
  997. $self->remove($self->get_object_index($operation->{object_identifier}), 'true');
  998. # Add the new objects and revert changes to the plater object identifier.
  999. my $current_objects_identifier = $self->{object_identifier};
  1000. $self->load_model_objects(@{$operation->{attributes}->[1]->objects});
  1001. $self->{object_identifier} = $current_objects_identifier;
  1002. # Add their identifiers.
  1003. my $obj_identifiers_start = $operation->{attributes}->[2];
  1004. my $obj_count = $#{$operation->{attributes}->[1]->objects} + 1;
  1005. for (my $i_object = 0; $i_object <= $#{$operation->{attributes}->[1]->objects}; $i_object++){
  1006. $self->{objects}->[-$obj_count]->identifier($obj_identifiers_start++);
  1007. $obj_count--;
  1008. }
  1009. } elsif ($type eq "CHANGE_SCALE") {
  1010. my $object_id = $operation->{object_identifier};
  1011. my $obj_idx = $self->get_object_index($object_id);
  1012. $self->select_object($obj_idx);
  1013. my $axis = $operation->{attributes}->[0];
  1014. my $tosize = $operation->{attributes}->[1];
  1015. my $old_scale = $operation->{attributes}->[2];
  1016. $self->changescale($axis, $tosize, $old_scale, 'true');
  1017. } elsif ($type eq "RESET") {
  1018. $self->reset('true');
  1019. } elsif ($type eq "ADD") {
  1020. # Revert changes to the plater object identifier. It's modified when adding new objects only not when undo/redo is executed.
  1021. my $current_objects_identifier = $self->{object_identifier};
  1022. $self->load_model_objects(@{$operation->{attributes}->[0]->objects});
  1023. $self->{object_identifier} = $current_objects_identifier;
  1024. my $objects_count = $#{$operation->{attributes}->[0]->objects} + 1;
  1025. my $start_identifier = $operation->{attributes}->[1];
  1026. foreach my $object (@{$operation->{attributes}->[0]->objects})
  1027. {
  1028. $self->{objects}->[-$objects_count]->identifier($start_identifier++);
  1029. $objects_count--;
  1030. }
  1031. } elsif ($type eq "GROUP"){
  1032. my @ops = @{$operation->{attributes}};
  1033. foreach my $op (@ops) {
  1034. push @{$self->{redo_stack}}, $op;
  1035. $self->redo;
  1036. pop @{$self->{undo_stack}};
  1037. }
  1038. }
  1039. }
  1040. sub add {
  1041. my $self = shift;
  1042. # Save the current object identifier to track added objects.
  1043. my $start_object_id = $self->{object_identifier};
  1044. my @input_files = wxTheApp->open_model($self);
  1045. $self->load_file($_) for @input_files;
  1046. # Check if no objects are added.
  1047. if ($start_object_id == $self->{object_identifier}) {
  1048. return;
  1049. }
  1050. # Save the added objects.
  1051. my $new_model = $self->{model}->new;
  1052. # Get newly added objects count.
  1053. my $new_objects_count = $self->{object_identifier} - $start_object_id;
  1054. for (my $i_object = $start_object_id; $i_object < $new_objects_count + $start_object_id; $i_object++){
  1055. my $object_index = $self->get_object_index($i_object);
  1056. $new_model->add_object($self->{model}->get_object($object_index));
  1057. }
  1058. $self->add_undo_operation("ADD", undef, $new_model, $start_object_id);
  1059. }
  1060. sub add_tin {
  1061. my $self = shift;
  1062. my @input_files = wxTheApp->open_model($self);
  1063. return if !@input_files;
  1064. my $offset = Wx::GetNumberFromUser("", "Enter the minimum thickness in mm (i.e. the offset from the lowest point):", "2.5D TIN",
  1065. 5, 0, 1000000, $self);
  1066. return if $offset < 0;
  1067. foreach my $input_file (@input_files) {
  1068. my $model = eval { Slic3r::Model->read_from_file($input_file) };
  1069. Slic3r::GUI::show_error($self, $@) if $@;
  1070. next if !$model;
  1071. if ($model->looks_like_multipart_object) {
  1072. Slic3r::GUI::show_error($self, "Multi-part models cannot be opened as 2.5D TIN files. Please load a single continuous mesh.");
  1073. next;
  1074. }
  1075. my $model_object = $model->get_object(0);
  1076. eval {
  1077. $model_object->get_volume(0)->extrude_tin($offset);
  1078. };
  1079. Slic3r::GUI::show_error($self, $@) if $@;
  1080. $self->load_model_objects($model_object);
  1081. }
  1082. }
  1083. sub load_file {
  1084. my $self = shift;
  1085. my ($input_file, $obj_idx_to_load) = @_;
  1086. $Slic3r::GUI::Settings->{recent}{skein_directory} = dirname($input_file);
  1087. wxTheApp->save_settings;
  1088. my $process_dialog = Wx::ProgressDialog->new('Loading…', "Processing input file…", 100, $self, 0);
  1089. $process_dialog->Pulse;
  1090. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  1091. my $model = eval { Slic3r::Model->read_from_file($input_file) };
  1092. Slic3r::GUI::show_error($self, $@) if $@;
  1093. my @obj_idx = ();
  1094. if (defined $model) {
  1095. if ($model->looks_like_multipart_object) {
  1096. my $dialog = Wx::MessageDialog->new($self,
  1097. "This file contains several objects positioned at multiple heights. "
  1098. . "Instead of considering them as multiple objects, should I consider\n"
  1099. . "this file as a single object having multiple parts?\n",
  1100. 'Multi-part object detected', wxICON_WARNING | wxYES | wxNO);
  1101. if ($dialog->ShowModal() == wxID_YES) {
  1102. $model->convert_multipart_object;
  1103. }
  1104. }
  1105. for my $obj_idx (0..($model->objects_count-1)) {
  1106. my $object = $model->objects->[$obj_idx];
  1107. $object->set_input_file($input_file);
  1108. for my $vol_idx (0..($object->volumes_count-1)) {
  1109. my $volume = $object->get_volume($vol_idx);
  1110. $volume->set_input_file($input_file);
  1111. $volume->set_input_file_obj_idx($obj_idx);
  1112. $volume->set_input_file_obj_idx($vol_idx);
  1113. }
  1114. }
  1115. my $i = 0;
  1116. if (defined $obj_idx_to_load) {
  1117. return () if $obj_idx_to_load >= $model->objects_count;
  1118. @obj_idx = $self->load_model_objects($model->get_object($obj_idx_to_load));
  1119. $i = $obj_idx_to_load;
  1120. } else {
  1121. @obj_idx = $self->load_model_objects(@{$model->objects});
  1122. }
  1123. foreach my $obj_idx (@obj_idx) {
  1124. $self->{objects}[$obj_idx]->input_file($input_file);
  1125. $self->{objects}[$obj_idx]->input_file_obj_idx($i++);
  1126. }
  1127. $self->statusbar->SetStatusText("Loaded " . basename($input_file));
  1128. if($self->{scaled_down}) {
  1129. $self->statusbar->SetStatusText('Your object appears to be too large, so it was automatically scaled down to fit your print bed.');
  1130. }
  1131. if($self->{outside_bounds}) {
  1132. $self->statusbar->SetStatusText('Some of your object(s) appear to be outside the print bed. Use the arrange button to correct this.');
  1133. }
  1134. }
  1135. $process_dialog->Destroy;
  1136. # Empty the redo stack
  1137. $self->{redo_stack} = [];
  1138. return @obj_idx;
  1139. }
  1140. sub load_model_objects {
  1141. my ($self, @model_objects) = @_;
  1142. # Always restart background process when adding new objects.
  1143. # This prevents lack of processing in some circumstances when background process is
  1144. # running but adding a new object does not invalidate anything.
  1145. $self->stop_background_process;
  1146. my $bed_centerf = $self->bed_centerf;
  1147. my $bed_shape = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape});
  1148. my $bed_size = $bed_shape->bounding_box->size;
  1149. my $need_arrange = 0;
  1150. my @obj_idx = ();
  1151. foreach my $model_object (@model_objects) {
  1152. my $o = $self->{model}->add_object($model_object);
  1153. $o->repair;
  1154. push @{ $self->{objects} }, Slic3r::GUI::Plater::Object->new(
  1155. name => $model_object->name || basename($model_object->input_file), identifier =>
  1156. $self->{object_identifier}++
  1157. );
  1158. push @obj_idx, $#{ $self->{objects} };
  1159. if ($model_object->instances_count == 0) {
  1160. if ($Slic3r::GUI::Settings->{_}{autocenter}) {
  1161. # if object has no defined position(s) we need to rearrange everything after loading
  1162. $need_arrange = 1;
  1163. # add a default instance and center object around origin
  1164. $o->center_around_origin; # also aligns object to Z = 0
  1165. $o->add_instance(offset => $bed_centerf);
  1166. } else {
  1167. # if user turned autocentering off, automatic arranging would disappoint them
  1168. $need_arrange = 0;
  1169. if ($Slic3r::GUI::Settings->{_}{autoalignz}) {
  1170. $o->align_to_ground; # aligns object to Z = 0
  1171. }
  1172. $o->add_instance();
  1173. }
  1174. } else {
  1175. if ($Slic3r::GUI::Settings->{_}{autoalignz}) {
  1176. # if object has defined positions we still need to ensure it's aligned to Z = 0
  1177. $o->align_to_ground;
  1178. }
  1179. }
  1180. {
  1181. # if the object is too large (more than 5 times the bed), scale it down
  1182. my $size = $o->bounding_box->size;
  1183. my $ratio = max(@$size[X,Y]) / unscale(max(@$bed_size[X,Y]));
  1184. if ($ratio > 5) {
  1185. $_->set_scaling_factor(1/$ratio) for @{$o->instances};
  1186. $self->{scaled_down} = 1;
  1187. }
  1188. }
  1189. {
  1190. # if after scaling the object does not fit on the bed provide a warning
  1191. my $bed_bounds = Slic3r::Geometry::BoundingBoxf->new_from_points($self->{config}->bed_shape);
  1192. my $o_bounds = $o->bounding_box;
  1193. my $min = Slic3r::Pointf->new($o_bounds->x_min, $o_bounds->y_min);
  1194. my $max = Slic3r::Pointf->new($o_bounds->x_max, $o_bounds->y_max);
  1195. if (!$bed_bounds->contains_point($min) || !$bed_bounds->contains_point($max))
  1196. {
  1197. $self->{outside_bounds} = 1;
  1198. }
  1199. }
  1200. $self->{print}->auto_assign_extruders($o);
  1201. $self->{print}->add_model_object($o);
  1202. }
  1203. $self->make_thumbnail($_) for @obj_idx;
  1204. $self->arrange if $need_arrange;
  1205. $self->on_model_change;
  1206. # zoom to objects
  1207. $self->{canvas3D}->zoom_to_volumes
  1208. if $self->{canvas3D};
  1209. $self->object_list_changed;
  1210. return @obj_idx;
  1211. }
  1212. sub bed_centerf {
  1213. my ($self) = @_;
  1214. my $bed_shape = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape});
  1215. my $bed_center = $bed_shape->bounding_box->center;
  1216. return Slic3r::Pointf->new(unscale($bed_center->x), unscale($bed_center->y)); #)
  1217. }
  1218. sub remove {
  1219. my $self = shift;
  1220. my ($obj_idx, $dont_push) = @_;
  1221. $self->stop_background_process;
  1222. # Prevent toolpaths preview from rendering while we modify the Print object
  1223. $self->{toolpaths2D}->enabled(0) if $self->{toolpaths2D};
  1224. $self->{preview3D}->enabled(0) if $self->{preview3D};
  1225. # if no object index is supplied, remove the selected one
  1226. if (!defined $obj_idx) {
  1227. ($obj_idx, undef) = $self->selected_object;
  1228. }
  1229. # Save the object identifier and copy the object for undo/redo operations.
  1230. my $object_id = $self->{objects}->[$obj_idx]->identifier;
  1231. my $new_model = Slic3r::Model->new; # store this before calling get_object()
  1232. $new_model->add_object($self->{model}->get_object($obj_idx));
  1233. splice @{$self->{objects}}, $obj_idx, 1;
  1234. $self->{model}->delete_object($obj_idx);
  1235. $self->{print}->delete_object($obj_idx);
  1236. $self->object_list_changed;
  1237. $self->select_object(undef);
  1238. $self->on_model_change;
  1239. if (!defined $dont_push) {
  1240. $self->add_undo_operation("REMOVE", $object_id, $new_model);
  1241. }
  1242. }
  1243. sub reset {
  1244. my ($self, $dont_push) = @_;
  1245. $self->stop_background_process;
  1246. # Prevent toolpaths preview from rendering while we modify the Print object
  1247. $self->{toolpaths2D}->enabled(0) if $self->{toolpaths2D};
  1248. $self->{preview3D}->enabled(0) if $self->{preview3D};
  1249. # Save the current model.
  1250. my $current_model = $self->{model}->clone;
  1251. if (!defined $dont_push) {
  1252. # Get the identifiers of the curent model objects.
  1253. my $objects_identifiers = [];
  1254. for (my $i = 0; $i <= $#{$self->{objects}}; $i++){
  1255. push @{$objects_identifiers}, $self->{objects}->[$i]->identifier;
  1256. }
  1257. $self->add_undo_operation("RESET", undef, $current_model, $objects_identifiers);
  1258. }
  1259. @{$self->{objects}} = ();
  1260. $self->{model}->clear_objects;
  1261. $self->{print}->clear_objects;
  1262. $self->object_list_changed;
  1263. $self->select_object(undef);
  1264. $self->on_model_change;
  1265. }
  1266. sub increase {
  1267. my ($self, $copies, $dont_push) = @_;
  1268. $copies //= 1;
  1269. my ($obj_idx, $object) = $self->selected_object;
  1270. my $model_object = $self->{model}->objects->[$obj_idx];
  1271. my $instance = $model_object->instances->[-1];
  1272. for my $i (1..$copies) {
  1273. $instance = $model_object->add_instance(
  1274. offset => Slic3r::Pointf->new(map 10+$_, @{$instance->offset}),
  1275. z_translation => $instance->z_translation,
  1276. scaling_factor => $instance->scaling_factor,
  1277. scaling_vector => $instance->scaling_vector,
  1278. rotation => $instance->rotation,
  1279. x_rotation => $instance->x_rotation,
  1280. y_rotation => $instance->y_rotation,
  1281. );
  1282. $self->{print}->objects->[$obj_idx]->add_copy($instance->offset);
  1283. }
  1284. if (!defined $dont_push) {
  1285. $self->add_undo_operation("INCREASE", $object->identifier , $copies);
  1286. }
  1287. # only autoarrange if user has autocentering enabled
  1288. $self->stop_background_process;
  1289. if ($Slic3r::GUI::Settings->{_}{autocenter}) {
  1290. $self->arrange;
  1291. } else {
  1292. $self->on_model_change;
  1293. }
  1294. }
  1295. sub decrease {
  1296. my ($self, $copies, $dont_push) = @_;
  1297. $copies //= 1;
  1298. $self->stop_background_process;
  1299. my ($obj_idx, $object) = $self->selected_object;
  1300. my $model_object = $self->{model}->objects->[$obj_idx];
  1301. if ($model_object->instances_count > $copies) {
  1302. for my $i (1..$copies) {
  1303. $model_object->delete_last_instance;
  1304. $self->{print}->objects->[$obj_idx]->delete_last_copy;
  1305. }
  1306. if (!defined $dont_push) {
  1307. $self->add_undo_operation("DECREASE", $object->identifier, $copies);
  1308. }
  1309. } else {
  1310. $self->remove;
  1311. }
  1312. $self->on_model_change;
  1313. }
  1314. sub set_number_of_copies {
  1315. my ($self) = @_;
  1316. $self->pause_background_process;
  1317. # get current number of copies
  1318. my ($obj_idx, $object) = $self->selected_object;
  1319. my $model_object = $self->{model}->objects->[$obj_idx];
  1320. # prompt user
  1321. my $copies = Wx::GetNumberFromUser("", "Enter the number of copies of the selected object:", "Copies", $model_object->instances_count, 0, 1000, $self);
  1322. return if $copies == -1;
  1323. my $diff = $copies - $model_object->instances_count;
  1324. if ($diff == 0) {
  1325. # no variation
  1326. $self->resume_background_process;
  1327. } elsif ($diff > 0) {
  1328. $self->increase($diff);
  1329. } elsif ($diff < 0) {
  1330. $self->decrease(-$diff);
  1331. }
  1332. }
  1333. sub center_selected_object_on_bed {
  1334. my ($self) = @_;
  1335. my ($obj_idx, $object) = $self->selected_object;
  1336. return if !defined $obj_idx;
  1337. my $model_object = $self->{model}->objects->[$obj_idx];
  1338. my $bb = $model_object->bounding_box;
  1339. my $size = $bb->size;
  1340. my $vector = Slic3r::Pointf->new(
  1341. $self->bed_centerf->x - $bb->x_min - $size->x/2,
  1342. $self->bed_centerf->y - $bb->y_min - $size->y/2, #//
  1343. );
  1344. $_->offset->translate(@$vector) for @{$model_object->instances};
  1345. $self->on_model_change;
  1346. }
  1347. sub rotate_face {
  1348. my $self = shift;
  1349. my ($obj_idx, $object) = $self->selected_object;
  1350. return if !defined $obj_idx;
  1351. # Get the selected normal
  1352. if (!$Slic3r::GUI::have_OpenGL) {
  1353. Slic3r::GUI::show_error($self, "Please install the OpenGL modules to use this feature (see build instructions).");
  1354. return;
  1355. }
  1356. my $dlg = Slic3r::GUI::Plater::ObjectRotateFaceDialog->new($self,
  1357. object => $self->{objects}[$obj_idx],
  1358. model_object => $self->{model}->objects->[$obj_idx],
  1359. );
  1360. return unless $dlg->ShowModal == wxID_OK;
  1361. my $normal = $dlg->SelectedNormal;
  1362. return if !defined $normal;
  1363. my $axis = $dlg->SelectedAxis;
  1364. return if !defined $axis;
  1365. # Actual math to rotate
  1366. my $angleToXZ = atan2($normal->y(),$normal->x());
  1367. my $angleToZ = acos(-$normal->z());
  1368. $self->rotate(-rad2deg($angleToXZ),Z);
  1369. $self->rotate(rad2deg($angleToZ),Y);
  1370. if($axis == Z){
  1371. $self->add_undo_operation("GROUP", $object->identifier, splice(@{$self->{undo_stack}},-2));
  1372. } else {
  1373. if($axis == X){
  1374. $self->rotate(90,Y);
  1375. } else {
  1376. $self->rotate(90,X);
  1377. }
  1378. $self->add_undo_operation("GROUP", $object->identifier, splice(@{$self->{undo_stack}},-3));
  1379. }
  1380. }
  1381. sub rotate {
  1382. my $self = shift;
  1383. my ($angle, $axis, $dont_push) = @_;
  1384. # angle is in degrees
  1385. $axis //= Z;
  1386. my ($obj_idx, $object) = $self->selected_object;
  1387. return if !defined $obj_idx;
  1388. my $model_object = $self->{model}->objects->[$obj_idx];
  1389. my $model_instance = $model_object->instances->[0];
  1390. # we need thumbnail to be computed before allowing rotation
  1391. return if !$object->thumbnail;
  1392. if (!defined $angle) {
  1393. my $axis_name = $axis == X ? 'X' : $axis == Y ? 'Y' : 'Z';
  1394. my $default = $axis == Z ? rad2deg($model_instance->rotation) : 0;
  1395. # Wx::GetNumberFromUser() does not support decimal numbers
  1396. $angle = Wx::GetTextFromUser("Enter the rotation angle:", "Rotate around $axis_name axis",
  1397. $default, $self);
  1398. return if !$angle || $angle !~ /^-?\d*(?:\.\d*)?$/ || $angle == -1;
  1399. }
  1400. $self->stop_background_process;
  1401. if ($axis == Z) {
  1402. my $new_angle = deg2rad($angle);
  1403. $_->set_rotation($_->rotation + $new_angle) for @{ $model_object->instances };
  1404. $object->transform_thumbnail($self->{model}, $obj_idx);
  1405. } else {
  1406. # rotation around X and Y needs to be performed on mesh
  1407. # so we first apply any Z rotation
  1408. $model_object->transform_by_instance($model_instance, 1);
  1409. $model_object->rotate(deg2rad($angle), $axis);
  1410. # realign object to Z = 0
  1411. $model_object->center_around_origin;
  1412. $self->make_thumbnail($obj_idx);
  1413. }
  1414. $model_object->update_bounding_box;
  1415. # update print and start background processing
  1416. $self->{print}->add_model_object($model_object, $obj_idx);
  1417. if (!defined $dont_push) {
  1418. $self->add_undo_operation("ROTATE", $object->identifier, $angle, $axis);
  1419. }
  1420. $self->selection_changed; # refresh info (size etc.)
  1421. $self->on_model_change;
  1422. }
  1423. sub mirror {
  1424. my ($self, $axis, $dont_push) = @_;
  1425. my ($obj_idx, $object) = $self->selected_object;
  1426. return if !defined $obj_idx;
  1427. my $model_object = $self->{model}->objects->[$obj_idx];
  1428. my $model_instance = $model_object->instances->[0];
  1429. # apply Z rotation before mirroring
  1430. $model_object->transform_by_instance($model_instance, 1);
  1431. $model_object->mirror($axis);
  1432. $model_object->update_bounding_box;
  1433. # realign object to Z = 0
  1434. $model_object->center_around_origin;
  1435. $self->make_thumbnail($obj_idx);
  1436. # update print and start background processing
  1437. $self->stop_background_process;
  1438. $self->{print}->add_model_object($model_object, $obj_idx);
  1439. if (!defined $dont_push) {
  1440. $self->add_undo_operation("MIRROR", $object->identifier, $axis);
  1441. }
  1442. $self->selection_changed; # refresh info (size etc.)
  1443. $self->on_model_change;
  1444. }
  1445. sub changescale {
  1446. my ($self, $axis, $tosize, $saved_scale, $dont_push) = @_;
  1447. my ($obj_idx, $object) = $self->selected_object;
  1448. return if !defined $obj_idx;
  1449. my $model_object = $self->{model}->objects->[$obj_idx];
  1450. my $model_instance = $model_object->instances->[0];
  1451. # we need thumbnail to be computed before allowing scaling
  1452. return if !$object->thumbnail;
  1453. my $object_size = $model_object->bounding_box->size;
  1454. my $bed_size = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape})->bounding_box->size;
  1455. my $old_scale;
  1456. my $scale;
  1457. if (defined $axis) {
  1458. my $axis_name = $axis == X ? 'X' : $axis == Y ? 'Y' : 'Z';
  1459. if (!defined $saved_scale) {
  1460. if ($tosize) {
  1461. my $cursize = $object_size->[$axis];
  1462. # Wx::GetNumberFromUser() does not support decimal numbers
  1463. my $newsize = Wx::GetTextFromUser(
  1464. sprintf("Enter the new size for the selected object (print bed: %smm):", $bed_size->[$axis]),
  1465. "Scale along $axis_name",
  1466. $cursize, $self);
  1467. return if !$newsize || $newsize !~ /^\d*(?:\.\d*)?$/ || $newsize < 0;
  1468. $scale = $newsize / $cursize * 100;
  1469. $old_scale = $cursize / $newsize * 100;
  1470. } else {
  1471. # Wx::GetNumberFromUser() does not support decimal numbers
  1472. $scale = Wx::GetTextFromUser("Enter the scale % for the selected object:",
  1473. "Scale along $axis_name", 100, $self);
  1474. $scale =~ s/%$//;
  1475. return if !$scale || $scale !~ /^\d*(?:\.\d*)?$/ || $scale < 0;
  1476. $old_scale = 100 * 100 / $scale;
  1477. }
  1478. }
  1479. else {
  1480. $scale = $saved_scale;
  1481. }
  1482. # apply Z rotation before scaling
  1483. $model_object->transform_by_instance($model_instance, 1);
  1484. my $versor = [1,1,1];
  1485. $versor->[$axis] = $scale/100;
  1486. $model_object->scale_xyz(Slic3r::Pointf3->new(@$versor));
  1487. # object was already aligned to Z = 0, so no need to realign it
  1488. $self->make_thumbnail($obj_idx);
  1489. } else {
  1490. if (!defined $saved_scale) {
  1491. if ($tosize) {
  1492. my $cursize = max(@$object_size);
  1493. # Wx::GetNumberFromUser() does not support decimal numbers
  1494. my $newsize = Wx::GetTextFromUser("Enter the new max size for the selected object:",
  1495. "Scale", $cursize, $self);
  1496. return if !$newsize || $newsize !~ /^\d*(?:\.\d*)?$/ || $newsize < 0;
  1497. $scale = $model_instance->scaling_factor * $newsize / $cursize * 100;
  1498. $old_scale = $model_instance->scaling_factor * 100;
  1499. } else {
  1500. # max scale factor should be above 2540 to allow importing files exported in inches
  1501. # Wx::GetNumberFromUser() does not support decimal numbers
  1502. $scale = Wx::GetTextFromUser("Enter the scale % for the selected object:", 'Scale',
  1503. $model_instance->scaling_factor * 100, $self);
  1504. return if !$scale || $scale !~ /^\d*(?:\.\d*)?$/ || $scale < 0;
  1505. $old_scale = $model_instance->scaling_factor * 100;
  1506. }
  1507. return if !$scale || $scale < 0;
  1508. } else {
  1509. $scale = $saved_scale;
  1510. }
  1511. $scale /= 100; # turn percent into factor
  1512. my $variation = $scale / $model_instance->scaling_factor;
  1513. foreach my $range (@{ $model_object->layer_height_ranges }) {
  1514. $range->[0] *= $variation;
  1515. $range->[1] *= $variation;
  1516. }
  1517. $_->set_scaling_factor($scale) for @{ $model_object->instances };
  1518. $object->transform_thumbnail($self->{model}, $obj_idx);
  1519. $scale *= 100;
  1520. }
  1521. # Add the new undo operation.
  1522. if (!defined $dont_push) {
  1523. $self->add_undo_operation("CHANGE_SCALE", $object->identifier, $axis, $tosize, $scale, $old_scale);
  1524. }
  1525. $model_object->update_bounding_box;
  1526. # update print and start background processing
  1527. $self->stop_background_process;
  1528. $self->{print}->add_model_object($model_object, $obj_idx);
  1529. $self->selection_changed(1); # refresh info (size, volume etc.)
  1530. $self->on_model_change;
  1531. }
  1532. sub arrange {
  1533. my $self = shift;
  1534. $self->pause_background_process;
  1535. my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($self->{config}->bed_shape);
  1536. my $success = $self->{model}->arrange_objects($self->config->min_object_distance, $bb);
  1537. # ignore arrange failures on purpose: user has visual feedback and we don't need to warn him
  1538. # when parts don't fit in print bed
  1539. $self->statusbar->SetStatusText('Objects were arranged.');
  1540. $self->on_model_change(1);
  1541. }
  1542. sub split_object {
  1543. my ($self, $dont_push) = @_;
  1544. my ($obj_idx, $current_object) = $self->selected_object;
  1545. # we clone model object because split_object() adds the split volumes
  1546. # into the same model object, thus causing duplicates when we call load_model_objects()
  1547. my $new_model = $self->{model}->clone; # store this before calling get_object()
  1548. my $current_model_object = $new_model->get_object($obj_idx);
  1549. if ($current_model_object->volumes_count > 1) {
  1550. Slic3r::GUI::warning_catcher($self)->("The selected object can't be split because it contains more than one volume/material.");
  1551. return;
  1552. }
  1553. $self->pause_background_process;
  1554. # Save the curent model object for undo/redo operataions.
  1555. my $org_object_model = Slic3r::Model->new;
  1556. $org_object_model->add_object($current_model_object);
  1557. # Save the org object identifier.
  1558. my $object_id = $self->{objects}->[$obj_idx]->identifier;
  1559. my @model_objects = @{$current_model_object->split_object};
  1560. if (@model_objects == 1) {
  1561. $self->resume_background_process;
  1562. Slic3r::GUI::warning_catcher($self)->("The selected object couldn't be split because it contains only one part.");
  1563. $self->resume_background_process;
  1564. return;
  1565. }
  1566. foreach my $object (@model_objects) {
  1567. $object->instances->[$_]->offset->translate($_ * 10, $_ * 10)
  1568. for 1..$#{ $object->instances };
  1569. # we need to center this single object around origin
  1570. $object->center_around_origin;
  1571. }
  1572. # remove the original object before spawning the object_loaded event, otherwise
  1573. # we'll pass the wrong $obj_idx to it (which won't be recognized after the
  1574. # thumbnail thread returns)
  1575. $self->remove($obj_idx, 'true'); # Don't push to the undo stack it's considered a split opeation not a remove one.
  1576. $current_object = $obj_idx = undef;
  1577. # Save the object identifiers used in undo/redo operations.
  1578. my $new_objects_id_start = $self->{object_identifier};
  1579. print "The new object identifier start for split is " .$new_objects_id_start . "\n";
  1580. # load all model objects at once, otherwise the plate would be rearranged after each one
  1581. # causing original positions not to be kept
  1582. $self->load_model_objects(@model_objects);
  1583. # Create two models to save the current object and the resulted objects.
  1584. my $new_objects_model = Slic3r::Model->new;
  1585. foreach my $new_object (@model_objects) {
  1586. $new_objects_model->add_object($new_object);
  1587. }
  1588. $self->add_undo_operation("SPLIT", $object_id, $org_object_model, $new_objects_model, $new_objects_id_start);
  1589. }
  1590. sub toggle_print_stats {
  1591. my ($self, $show) = @_;
  1592. return if !$self->GetFrame->is_loaded;
  1593. if ($show) {
  1594. $self->{right_sizer}->Show($self->{sliced_info_box});
  1595. } else {
  1596. $self->{right_sizer}->Hide($self->{sliced_info_box});
  1597. }
  1598. $self->{right_sizer}->Layout;
  1599. }
  1600. sub config_changed {
  1601. my $self = shift;
  1602. my $config = $self->config;
  1603. if ($Slic3r::GUI::autosave) {
  1604. $config->save($Slic3r::GUI::autosave);
  1605. }
  1606. # Apply changes to the plater-specific config options.
  1607. foreach my $opt_key (@{$self->{config}->diff($config)}) {
  1608. # Ignore overrides. No need to set them in our config; we'll use them directly below.
  1609. next if $opt_key eq 'overrides';
  1610. $self->{config}->set($opt_key, $config->get($opt_key));
  1611. if ($opt_key eq 'bed_shape') {
  1612. $self->{canvas}->update_bed_size;
  1613. $self->{canvas3D}->update_bed_size if $self->{canvas3D};
  1614. $self->{preview3D}->set_bed_shape($self->{config}->bed_shape)
  1615. if $self->{preview3D};
  1616. $self->on_model_change;
  1617. } elsif ($opt_key eq 'serial_port') {
  1618. if ($config->get('serial_port')) {
  1619. $self->{btn_print}->Show;
  1620. } else {
  1621. $self->{btn_print}->Hide;
  1622. }
  1623. $self->Layout;
  1624. } elsif ($opt_key eq 'print_host') {
  1625. if ($config->get('print_host')) {
  1626. $self->{btn_send_gcode}->Show;
  1627. } else {
  1628. $self->{btn_send_gcode}->Hide;
  1629. }
  1630. $self->Layout;
  1631. }
  1632. }
  1633. return if !$self->GetFrame->is_loaded;
  1634. $self->toggle_print_stats(0);
  1635. if ($Slic3r::GUI::Settings->{_}{background_processing}) {
  1636. # (re)start timer
  1637. $self->schedule_background_process;
  1638. } else {
  1639. $self->async_apply_config;
  1640. }
  1641. }
  1642. sub schedule_background_process {
  1643. my ($self) = @_;
  1644. warn 'schedule_background_process() is not supposed to be called when background processing is disabled'
  1645. if !$Slic3r::GUI::Settings->{_}{background_processing};
  1646. $self->{processed} = 0;
  1647. if (defined $self->{apply_config_timer}) {
  1648. $self->{apply_config_timer}->Start(PROCESS_DELAY, 1); # 1 = one shot
  1649. }
  1650. }
  1651. # Executed asynchronously by a timer every PROCESS_DELAY (0.5 second).
  1652. # The timer is started by schedule_background_process(),
  1653. sub async_apply_config {
  1654. my ($self) = @_;
  1655. # pause process thread before applying new config
  1656. # since we don't want to touch data that is being used by the threads
  1657. $self->pause_background_process;
  1658. # apply new config
  1659. my $invalidated = $self->{print}->apply_config($self->config);
  1660. # reset preview canvases (invalidated contents will be hidden)
  1661. $self->{toolpaths2D}->reload_print if $self->{toolpaths2D};
  1662. $self->{preview3D}->reload_print if $self->{preview3D};
  1663. $self->{AdaptiveLayersDialog}->reload_preview if $self->{AdaptiveLayersDialog};
  1664. if (!$Slic3r::GUI::Settings->{_}{background_processing}) {
  1665. $self->hide_preview if $invalidated;
  1666. return;
  1667. }
  1668. if ($invalidated) {
  1669. # kill current thread if any
  1670. $self->stop_background_process;
  1671. # remove the sliced statistics box because something changed.
  1672. $self->toggle_print_stats(0);
  1673. } else {
  1674. $self->resume_background_process;
  1675. }
  1676. # schedule a new process thread in case it wasn't running
  1677. $self->start_background_process;
  1678. }
  1679. sub start_background_process {
  1680. my ($self) = @_;
  1681. return if !$Slic3r::have_threads;
  1682. return if $self->{process_thread};
  1683. if (!@{$self->{objects}}) {
  1684. $self->on_process_completed;
  1685. return;
  1686. }
  1687. # It looks like declaring a local $SIG{__WARN__} prevents the ugly
  1688. # "Attempt to free unreferenced scalar" warning...
  1689. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  1690. # don't start process thread if config is not valid
  1691. eval {
  1692. # this will throw errors if config is not valid
  1693. $self->config->validate;
  1694. $self->{print}->validate;
  1695. };
  1696. if ($@) {
  1697. $self->statusbar->SetStatusText($@);
  1698. return;
  1699. }
  1700. if ($Slic3r::GUI::Settings->{_}{threads}) {
  1701. $self->{print}->config->set('threads', $Slic3r::GUI::Settings->{_}{threads});
  1702. }
  1703. # start thread
  1704. @_ = ();
  1705. $self->{process_thread} = Slic3r::spawn_thread(sub {
  1706. eval {
  1707. $self->{print}->process;
  1708. };
  1709. if ($@) {
  1710. Slic3r::debugf "Background process error: $@\n";
  1711. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $PROCESS_COMPLETED_EVENT, $@));
  1712. } else {
  1713. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $PROCESS_COMPLETED_EVENT, undef));
  1714. }
  1715. Slic3r::thread_cleanup();
  1716. });
  1717. Slic3r::debugf "Background processing started.\n";
  1718. }
  1719. sub stop_background_process {
  1720. my ($self) = @_;
  1721. $self->{apply_config_timer}->Stop if defined $self->{apply_config_timer};
  1722. $self->statusbar->SetCancelCallback(undef);
  1723. $self->statusbar->StopBusy;
  1724. $self->statusbar->SetStatusText("");
  1725. $self->{toolpaths2D}->reload_print if $self->{toolpaths2D};
  1726. $self->{preview3D}->reload_print if $self->{preview3D};
  1727. $self->{AdaptiveLayersDialog}->reload_preview if $self->{AdaptiveLayersDialog};
  1728. if ($self->{process_thread}) {
  1729. Slic3r::debugf "Killing background process.\n";
  1730. Slic3r::kill_all_threads();
  1731. $self->{process_thread} = undef;
  1732. } else {
  1733. Slic3r::debugf "No background process running.\n";
  1734. }
  1735. # if there's an export process, kill that one as well
  1736. if ($self->{export_thread}) {
  1737. Slic3r::debugf "Killing background export process.\n";
  1738. Slic3r::kill_all_threads();
  1739. $self->{export_thread} = undef;
  1740. }
  1741. }
  1742. sub pause_background_process {
  1743. my ($self) = @_;
  1744. if ($self->{process_thread} || $self->{export_thread}) {
  1745. Slic3r::pause_all_threads();
  1746. return 1;
  1747. } elsif (defined $self->{apply_config_timer} && $self->{apply_config_timer}->IsRunning) {
  1748. $self->{apply_config_timer}->Stop;
  1749. return 0; # we didn't actually pause any running thread; need to reschedule
  1750. }
  1751. return 0;
  1752. }
  1753. sub resume_background_process {
  1754. my ($self) = @_;
  1755. if ($self->{process_thread} || $self->{export_thread}) {
  1756. Slic3r::resume_all_threads();
  1757. }
  1758. }
  1759. sub export_gcode {
  1760. my ($self, $output_file) = @_;
  1761. return if !@{$self->{objects}};
  1762. if ($self->{export_gcode_output_file}) {
  1763. Wx::MessageDialog->new($self, "Another export job is currently running.", 'Error', wxOK | wxICON_ERROR)->ShowModal;
  1764. return;
  1765. }
  1766. # if process is not running, validate config
  1767. # (we assume that if it is running, config is valid)
  1768. eval {
  1769. # this will throw errors if config is not valid
  1770. $self->config->validate;
  1771. $self->{print}->validate;
  1772. };
  1773. Slic3r::GUI::catch_error($self) and return;
  1774. # apply config and validate print
  1775. my $config = $self->config;
  1776. eval {
  1777. # this will throw errors if config is not valid
  1778. $config->validate;
  1779. $self->{print}->apply_config($config);
  1780. $self->{print}->validate;
  1781. };
  1782. if (!$Slic3r::have_threads) {
  1783. Slic3r::GUI::catch_error($self) and return;
  1784. }
  1785. # select output file
  1786. if ($output_file) {
  1787. $self->{export_gcode_output_file} = $self->{print}->output_filepath($output_file);
  1788. } else {
  1789. my $default_output_file = $self->{print}->output_filepath($main::opt{output} // '');
  1790. my $dlg = Wx::FileDialog->new($self, 'Save G-code file as:', wxTheApp->output_path(dirname($default_output_file)),
  1791. basename($default_output_file), &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  1792. if ($dlg->ShowModal != wxID_OK) {
  1793. $dlg->Destroy;
  1794. return;
  1795. }
  1796. my $path = Slic3r::decode_path($dlg->GetPath);
  1797. $Slic3r::GUI::Settings->{_}{last_output_path} = dirname($path);
  1798. wxTheApp->save_settings;
  1799. $self->{export_gcode_output_file} = $path;
  1800. $dlg->Destroy;
  1801. }
  1802. $self->statusbar->StartBusy;
  1803. if ($Slic3r::have_threads) {
  1804. $self->statusbar->SetCancelCallback(sub {
  1805. $self->stop_background_process;
  1806. $self->statusbar->SetStatusText("Export cancelled");
  1807. $self->{export_gcode_output_file} = undef;
  1808. $self->{send_gcode_file} = undef;
  1809. # this updates buttons status
  1810. $self->object_list_changed;
  1811. });
  1812. # start background process, whose completion event handler
  1813. # will detect $self->{export_gcode_output_file} and proceed with export
  1814. $self->start_background_process;
  1815. } else {
  1816. eval {
  1817. $self->{print}->process;
  1818. $self->{print}->export_gcode(output_file => $self->{export_gcode_output_file});
  1819. };
  1820. my $result = !Slic3r::GUI::catch_error($self);
  1821. $self->on_export_completed($result);
  1822. }
  1823. # this updates buttons status
  1824. $self->object_list_changed;
  1825. $self->toggle_print_stats(1);
  1826. return $self->{export_gcode_output_file};
  1827. }
  1828. # This gets called only if we have threads.
  1829. sub on_process_completed {
  1830. my ($self, $error) = @_;
  1831. $self->statusbar->SetCancelCallback(undef);
  1832. $self->statusbar->StopBusy;
  1833. $self->statusbar->SetStatusText($error // "");
  1834. Slic3r::debugf "Background processing completed.\n";
  1835. $self->{process_thread}->detach if $self->{process_thread};
  1836. $self->{process_thread} = undef;
  1837. $self->{processed} = 1;
  1838. # if we're supposed to perform an explicit export let's display the error in a dialog
  1839. if ($error && $self->{export_gcode_output_file}) {
  1840. $self->{export_gcode_output_file} = undef;
  1841. Slic3r::GUI::show_error($self, $error);
  1842. }
  1843. return if $error;
  1844. $self->{toolpaths2D}->reload_print if $self->{toolpaths2D};
  1845. $self->{preview3D}->reload_print if $self->{preview3D};
  1846. $self->{AdaptiveLayersDialog}->reload_preview if $self->{AdaptiveLayersDialog};
  1847. # if we have an export filename, start a new thread for exporting G-code
  1848. if ($self->{export_gcode_output_file}) {
  1849. @_ = ();
  1850. # workaround for "Attempt to free un referenced scalar..."
  1851. our $_thread_self = $self;
  1852. $self->{export_thread} = Slic3r::spawn_thread(sub {
  1853. eval {
  1854. $_thread_self->{print}->export_gcode(output_file => $_thread_self->{export_gcode_output_file});
  1855. };
  1856. if ($@) {
  1857. Wx::PostEvent($_thread_self, Wx::PlThreadEvent->new(-1, $ERROR_EVENT, shared_clone([ $@ ])));
  1858. Wx::PostEvent($_thread_self, Wx::PlThreadEvent->new(-1, $EXPORT_COMPLETED_EVENT, 0));
  1859. } else {
  1860. Wx::PostEvent($_thread_self, Wx::PlThreadEvent->new(-1, $EXPORT_COMPLETED_EVENT, 1));
  1861. }
  1862. Slic3r::thread_cleanup();
  1863. });
  1864. Slic3r::debugf "Background G-code export started.\n";
  1865. }
  1866. }
  1867. # This gets called also if we have no threads.
  1868. sub on_progress_event {
  1869. my ($self, $percent, $message) = @_;
  1870. $self->statusbar->SetProgress($percent);
  1871. $self->statusbar->SetStatusText("$message…");
  1872. }
  1873. # This gets called also if we don't have threads.
  1874. sub on_export_completed {
  1875. my ($self, $result) = @_;
  1876. $self->statusbar->SetCancelCallback(undef);
  1877. $self->statusbar->StopBusy;
  1878. $self->statusbar->SetStatusText("");
  1879. Slic3r::debugf "Background export process completed.\n";
  1880. $self->{export_thread}->detach if $self->{export_thread};
  1881. $self->{export_thread} = undef;
  1882. my $message;
  1883. my $send_gcode = 0;
  1884. my $do_print = 0;
  1885. if ($result) {
  1886. if ($self->{print_file}) {
  1887. $message = "File added to print queue";
  1888. $do_print = 1;
  1889. } elsif ($self->{send_gcode_file}) {
  1890. $message = "Sending G-code file to the " . $self->{config}->host_type . " server...";
  1891. $send_gcode = 1;
  1892. } else {
  1893. $message = "G-code file exported to " . $self->{export_gcode_output_file};
  1894. }
  1895. } else {
  1896. $message = "Export failed";
  1897. }
  1898. $self->{export_gcode_output_file} = undef;
  1899. $self->statusbar->SetStatusText($message);
  1900. wxTheApp->notify($message);
  1901. $self->do_print if $do_print;
  1902. $self->send_gcode if $send_gcode;
  1903. $self->{print_file} = undef;
  1904. $self->{send_gcode_file} = undef;
  1905. {
  1906. my $fil = sprintf(
  1907. '%.2fcm (%.2fcm³%s)',
  1908. $self->{print}->total_used_filament / 10,
  1909. $self->{print}->total_extruded_volume / 1000,
  1910. $self->{print}->total_weight
  1911. ? sprintf(', %.2fg', $self->{print}->total_weight)
  1912. : '',
  1913. );
  1914. my $cost = $self->{print}->total_cost
  1915. ? sprintf("%.2f" , $self->{print}->total_cost)
  1916. : 'n.a.';
  1917. $self->{print_info_fil}->SetLabel($fil);
  1918. $self->{print_info_cost}->SetLabel($cost);
  1919. }
  1920. # this updates buttons status
  1921. $self->object_list_changed;
  1922. }
  1923. sub do_print {
  1924. my ($self) = @_;
  1925. my $controller = $self->GetFrame->{controller} or return;
  1926. my %current_presets = $self->selected_presets;
  1927. my $printer_panel = $controller->add_printer($current_presets{printer}->[0], $self->config);
  1928. my $filament_stats = $self->{print}->filament_stats;
  1929. $filament_stats = { map { $current_presets{filament}[$_]->name => $filament_stats->{$_} } keys %$filament_stats };
  1930. $printer_panel->load_print_job($self->{print_file}, $filament_stats);
  1931. $self->GetFrame->select_tab(1);
  1932. }
  1933. sub prepare_send {
  1934. my ($self, $skip_dialog) = @_;
  1935. return if !$self->{btn_send_gcode}->IsEnabled;
  1936. my $filename = basename($self->{print}->output_filepath($main::opt{output} // ''));
  1937. if (!$skip_dialog) {
  1938. # When the alt key is pressed, bypass the dialog.
  1939. my $dlg = Slic3r::GUI::Plater::OctoPrintSpoolDialog->new($self, $filename);
  1940. return unless $dlg->ShowModal == wxID_OK;
  1941. $filename = $dlg->{filename};
  1942. }
  1943. if (!$Slic3r::GUI::Settings->{octoprint}{overwrite}) {
  1944. my $progress = Wx::ProgressDialog->new('Querying OctoPrint…',
  1945. "Checking whether file already exists…", 100, $self, 0);
  1946. $progress->Pulse;
  1947. my $ua = LWP::UserAgent->new;
  1948. $ua->timeout(5);
  1949. my $res;
  1950. if ($self->{config}->print_host) {
  1951. if($self->{config}->host_type eq 'octoprint'){
  1952. $res = $ua->get(
  1953. "http://" . $self->{config}->print_host . "/api/files/local",
  1954. 'X-Api-Key' => $self->{config}->octoprint_apikey,
  1955. );
  1956. }else {
  1957. $res = $ua->get(
  1958. "http://" . $self->{config}->print_host . "/rr_files",
  1959. );
  1960. }
  1961. }
  1962. $progress->Destroy;
  1963. if ($res->is_success) {
  1964. my $searchterm = ($self->{config}->host_type eq 'octoprint') ? '/"name":\s*"\Q$filename\E"/' : '"'.$filename.'"';
  1965. if ($res->decoded_content =~ $searchterm) {
  1966. my $dialog = Wx::MessageDialog->new($self,
  1967. "It looks like a file with the same name already exists in the server. "
  1968. . "Shall I overwrite it?",
  1969. $self->{config}->host_type, wxICON_WARNING | wxYES | wxNO);
  1970. if ($dialog->ShowModal() == wxID_NO) {
  1971. return;
  1972. }
  1973. }
  1974. } else {
  1975. my $message = "Error while connecting to the " . $self->{config}->host_type . " server: " . $res->status_line;
  1976. Slic3r::GUI::show_error($self, $message);
  1977. return;
  1978. }
  1979. }
  1980. $self->{send_gcode_file_print} = $Slic3r::GUI::Settings->{octoprint}{start};
  1981. $self->{send_gcode_file} = $self->export_gcode(Wx::StandardPaths::Get->GetTempDir() . "/" . $filename);
  1982. }
  1983. sub send_gcode {
  1984. my ($self) = @_;
  1985. $self->statusbar->StartBusy;
  1986. my $ua = LWP::UserAgent->new;
  1987. $ua->timeout(180);
  1988. my $path = Slic3r::encode_path($self->{send_gcode_file});
  1989. my $filename = basename($self->{print}->output_filepath($main::opt{output} // ''));
  1990. my $res;
  1991. if($self->{config}->print_host){
  1992. if($self->{config}->host_type eq 'octoprint'){
  1993. $res = $ua->post(
  1994. "http://" . $self->{config}->print_host . "/api/files/local",
  1995. Content_Type => 'form-data',
  1996. 'X-Api-Key' => $self->{config}->octoprint_apikey,
  1997. Content => [
  1998. # OctoPrint doesn't like Windows paths so we use basename()
  1999. # Also, since we need to read from filesystem we process it through encode_path()
  2000. file => [ $path, basename($path) ],
  2001. print => $self->{send_gcode_file_print} ? 1 : 0,
  2002. ],
  2003. );
  2004. }else{
  2005. # slurp the file we would send into a string - should be someplace to reference this but could not find it?
  2006. local $/=undef;
  2007. open (my $gch,$path);
  2008. my $gcode=<$gch>;
  2009. close($gch);
  2010. # get the time string
  2011. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  2012. my $t = sprintf("%4d-%02d-%02dT%02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec);
  2013. my $req = HTTP::Request->new(POST => "http://" . $self->{config}->print_host . "/rr_upload?name=0:/gcodes/" . basename($path) . "&time=$t",);
  2014. $req->content( $gcode );
  2015. $res = $ua->request($req);
  2016. if ($res->is_success) {
  2017. if ($self->{send_gcode_file_print}) {
  2018. $res = $ua->get(
  2019. "http://" . $self->{config}->print_host . "/rr_gcode?gcode=M32%20" . basename($path),
  2020. );
  2021. }
  2022. }
  2023. }
  2024. }
  2025. $self->statusbar->StopBusy;
  2026. if ($res->is_success) {
  2027. $self->statusbar->SetStatusText("G-code file successfully uploaded to the " . $self->{config}->host_type . " server");
  2028. } else {
  2029. my $message = "Error while uploading to the " . $self->{config}->host_type . " server: " . $res->status_line;
  2030. Slic3r::GUI::show_error($self, $message);
  2031. $self->statusbar->SetStatusText($message);
  2032. }
  2033. }
  2034. sub export_stl {
  2035. my $self = shift;
  2036. return if !@{$self->{objects}};
  2037. my $output_file = $self->_get_export_file('STL') or return;
  2038. $self->{model}->write_stl($output_file, 1);
  2039. $self->statusbar->SetStatusText("STL file exported to $output_file");
  2040. }
  2041. sub reload_from_disk {
  2042. my ($self) = @_;
  2043. my ($obj_idx, $object) = $self->selected_object;
  2044. return if !defined $obj_idx;
  2045. if (!$object->input_file) {
  2046. Slic3r::GUI::warning_catcher($self)->("The selected object couldn't be reloaded because it isn't referenced to its input file any more. This is the case after performing operations like cut or split.");
  2047. return;
  2048. }
  2049. if (!-e $object->input_file) {
  2050. Slic3r::GUI::warning_catcher($self)->("The selected object couldn't be reloaded because the file doesn't exist anymore on the disk.");
  2051. return;
  2052. }
  2053. # Only reload the selected object and not all objects from the input file.
  2054. my @new_obj_idx = $self->load_file($object->input_file, $object->input_file_obj_idx);
  2055. if (!@new_obj_idx) {
  2056. Slic3r::GUI::warning_catcher($self)->("The selected object couldn't be reloaded because the new file doesn't contain the object.");
  2057. return;
  2058. }
  2059. my $org_obj = $self->{model}->objects->[$obj_idx];
  2060. # check if the object is dependant of more than one file
  2061. my $org_obj_has_modifiers=0;
  2062. for my $i (0..($org_obj->volumes_count-1)) {
  2063. if ($org_obj->input_file ne $org_obj->get_volume($i)->input_file) {
  2064. $org_obj_has_modifiers=1;
  2065. last;
  2066. }
  2067. }
  2068. my $reload_behavior = $Slic3r::GUI::Settings->{_}{reload_behavior};
  2069. # ask the user how to proceed, if option is selected in preferences
  2070. if ($org_obj_has_modifiers && !$Slic3r::GUI::Settings->{_}{reload_hide_dialog}) {
  2071. my $dlg = Slic3r::GUI::ReloadDialog->new(undef,$reload_behavior);
  2072. my $res = $dlg->ShowModal;
  2073. if ($res==wxID_CANCEL) {
  2074. $self->remove($_) for @new_obj_idx;
  2075. $dlg->Destroy;
  2076. return;
  2077. }
  2078. $reload_behavior = $dlg->GetSelection;
  2079. my $save = 0;
  2080. if ($reload_behavior != $Slic3r::GUI::Settings->{_}{reload_behavior}) {
  2081. $Slic3r::GUI::Settings->{_}{reload_behavior} = $reload_behavior;
  2082. $save = 1;
  2083. }
  2084. if ($dlg->GetHideOnNext) {
  2085. $Slic3r::GUI::Settings->{_}{reload_hide_dialog} = 1;
  2086. $save = 1;
  2087. }
  2088. Slic3r::GUI->save_settings if $save;
  2089. $dlg->Destroy;
  2090. }
  2091. my $volume_unmatched=0;
  2092. foreach my $new_obj_idx (@new_obj_idx) {
  2093. my $new_obj = $self->{model}->objects->[$new_obj_idx];
  2094. $new_obj->clear_instances;
  2095. $new_obj->add_instance($_) for @{$org_obj->instances};
  2096. $new_obj->config->apply($org_obj->config);
  2097. my $new_vol_idx = 0;
  2098. my $org_vol_idx = 0;
  2099. my $new_vol_count=$new_obj->volumes_count;
  2100. my $org_vol_count=$org_obj->volumes_count;
  2101. while ($new_vol_idx<=$new_vol_count-1) {
  2102. if (($org_vol_idx<=$org_vol_count-1) && ($org_obj->get_volume($org_vol_idx)->input_file eq $new_obj->input_file)) {
  2103. # apply config from the matching volumes
  2104. $new_obj->get_volume($new_vol_idx++)->config->apply($org_obj->get_volume($org_vol_idx++)->config);
  2105. } else {
  2106. # reload has more volumes than original (first file), apply config from the first volume
  2107. $new_obj->get_volume($new_vol_idx++)->config->apply($org_obj->get_volume(0)->config);
  2108. $volume_unmatched=1;
  2109. }
  2110. }
  2111. $org_vol_idx=$org_vol_count if $reload_behavior==2; # Reload behavior: discard
  2112. while (($org_vol_idx<=$org_vol_count-1) && ($org_obj->get_volume($org_vol_idx)->input_file eq $new_obj->input_file)) {
  2113. # original has more volumes (first file), skip those
  2114. $org_vol_idx++;
  2115. $volume_unmatched=1;
  2116. }
  2117. while ($org_vol_idx<=$org_vol_count-1) {
  2118. if ($reload_behavior==1) { # Reload behavior: copy
  2119. my $new_volume = $new_obj->add_volume($org_obj->get_volume($org_vol_idx));
  2120. $new_volume->mesh->translate(@{$org_obj->origin_translation->negative});
  2121. $new_volume->mesh->translate(@{$new_obj->origin_translation});
  2122. if ($new_volume->name =~ m/link to path\z/) {
  2123. my $new_name = $new_volume->name;
  2124. $new_name =~ s/ - no link to path$/ - copied/;
  2125. $new_volume->set_name($new_name);
  2126. }elsif(!($new_volume->name =~ m/copied\z/)) {
  2127. $new_volume->set_name($new_volume->name . " - copied");
  2128. }
  2129. }else{ # Reload behavior: Reload all, also fallback solution if ini was manually edited to a wrong value
  2130. if ($org_obj->get_volume($org_vol_idx)->input_file) {
  2131. my $model = eval { Slic3r::Model->read_from_file($org_obj->get_volume($org_vol_idx)->input_file) };
  2132. if ($@) {
  2133. $org_obj->get_volume($org_vol_idx)->set_input_file("");
  2134. }elsif ($org_obj->get_volume($org_vol_idx)->input_file_obj_idx > ($model->objects_count-1)) {
  2135. # Object Index for that part / modifier not found in current version of the file
  2136. $org_obj->get_volume($org_vol_idx)->set_input_file("");
  2137. }else{
  2138. my $prt_mod_obj = $model->objects->[$org_obj->get_volume($org_vol_idx)->input_file_obj_idx];
  2139. if ($org_obj->get_volume($org_vol_idx)->input_file_vol_idx > ($prt_mod_obj->volumes_count-1)) {
  2140. # Volume Index for that part / modifier not found in current version of the file
  2141. $org_obj->get_volume($org_vol_idx)->set_input_file("");
  2142. }else{
  2143. # all checks passed, load new mesh and copy metadata
  2144. my $new_volume = $new_obj->add_volume($prt_mod_obj->get_volume($org_obj->get_volume($org_vol_idx)->input_file_vol_idx));
  2145. $new_volume->set_input_file($org_obj->get_volume($org_vol_idx)->input_file);
  2146. $new_volume->set_input_file_obj_idx($org_obj->get_volume($org_vol_idx)->input_file_obj_idx);
  2147. $new_volume->set_input_file_vol_idx($org_obj->get_volume($org_vol_idx)->input_file_vol_idx);
  2148. $new_volume->config->apply($org_obj->get_volume($org_vol_idx)->config);
  2149. $new_volume->set_modifier($org_obj->get_volume($org_vol_idx)->modifier);
  2150. $new_volume->mesh->translate(@{$new_obj->origin_translation});
  2151. }
  2152. }
  2153. }
  2154. if (!$org_obj->get_volume($org_vol_idx)->input_file) {
  2155. my $new_volume = $new_obj->add_volume($org_obj->get_volume($org_vol_idx)); # error -> copy old mesh
  2156. $new_volume->mesh->translate(@{$org_obj->origin_translation->negative});
  2157. $new_volume->mesh->translate(@{$new_obj->origin_translation});
  2158. if ($new_volume->name =~ m/copied\z/) {
  2159. my $new_name = $new_volume->name;
  2160. $new_name =~ s/ - copied$/ - no link to path/;
  2161. $new_volume->set_name($new_name);
  2162. }elsif(!($new_volume->name =~ m/link to path\z/)) {
  2163. $new_volume->set_name($new_volume->name . " - no link to path");
  2164. }
  2165. $volume_unmatched=1;
  2166. }
  2167. }
  2168. $org_vol_idx++;
  2169. }
  2170. }
  2171. $self->remove($obj_idx);
  2172. # TODO: refresh object list which contains wrong count and scale
  2173. # Trigger thumbnail generation again, because the remove() method altered
  2174. # object indexes before background thumbnail generation called its completion
  2175. # event, so the on_thumbnail_made callback is called with the wrong $obj_idx.
  2176. # When porting to C++ we'll probably have cleaner ways to do this.
  2177. $self->make_thumbnail($_-1) for @new_obj_idx;
  2178. # update print
  2179. $self->stop_background_process;
  2180. $self->{print}->reload_object($_-1) for @new_obj_idx;
  2181. $self->on_model_change;
  2182. # Empty the redo stack
  2183. $self->{redo_stack} = [];
  2184. if ($volume_unmatched) {
  2185. Slic3r::GUI::warning_catcher($self)->("At least 1 volume couldn't be matched between the original object and the reloaded one.");
  2186. }
  2187. }
  2188. sub export_object_stl {
  2189. my $self = shift;
  2190. my ($obj_idx, $object) = $self->selected_object;
  2191. return if !defined $obj_idx;
  2192. my $model_object = $self->{model}->objects->[$obj_idx];
  2193. my $output_file = $self->_get_export_file('STL') or return;
  2194. $model_object->mesh->write_binary($output_file);
  2195. $self->statusbar->SetStatusText("STL file exported to $output_file");
  2196. }
  2197. # Export function for a single AMF output
  2198. sub export_object_amf {
  2199. my $self = shift;
  2200. my ($obj_idx, $object) = $self->selected_object;
  2201. return if !defined $obj_idx;
  2202. my $local_model = Slic3r::Model->new;
  2203. my $model_object = $self->{model}->objects->[$obj_idx];
  2204. # copy model_object -> local_model
  2205. $local_model->add_object($model_object);
  2206. my $output_file = $self->_get_export_file('AMF') or return;
  2207. $local_model->write_amf($output_file);
  2208. $self->statusbar->SetStatusText("AMF file exported to $output_file");
  2209. }
  2210. # Export function for a single 3MF output
  2211. sub export_object_tmf {
  2212. my $self = shift;
  2213. my ($obj_idx, $object) = $self->selected_object;
  2214. return if !defined $obj_idx;
  2215. my $local_model = Slic3r::Model->new;
  2216. my $model_object = $self->{model}->objects->[$obj_idx];
  2217. # copy model_object -> local_model
  2218. $local_model->add_object($model_object);
  2219. my $output_file = $self->_get_export_file('TMF') or return;
  2220. $local_model->write_tmf($output_file);
  2221. $self->statusbar->SetStatusText("3MF file exported to $output_file");
  2222. }
  2223. sub export_amf {
  2224. my $self = shift;
  2225. return if !@{$self->{objects}};
  2226. my $output_file = $self->_get_export_file('AMF') or return;
  2227. $self->{model}->write_amf($output_file);
  2228. $self->statusbar->SetStatusText("AMF file exported to $output_file");
  2229. }
  2230. sub export_tmf {
  2231. my $self = shift;
  2232. return if !@{$self->{objects}};
  2233. my $output_file = $self->_get_export_file('TMF') or return;
  2234. $self->{model}->write_tmf($output_file);
  2235. $self->statusbar->SetStatusText("3MF file exported to $output_file");
  2236. }
  2237. sub _get_export_file {
  2238. my $self = shift;
  2239. my ($format) = @_;
  2240. my $suffix = $format eq 'STL' ? '.stl' : ( $format eq 'AMF' ? '.amf' : '.3mf');
  2241. my $output_file = $main::opt{output};
  2242. {
  2243. $output_file = $self->{print}->output_filepath($output_file // '');
  2244. $output_file =~ s/\.gcode$/$suffix/i;
  2245. my $dlg;
  2246. $dlg = Wx::FileDialog->new($self, "Save $format file as:", dirname($output_file),
  2247. basename($output_file), &Slic3r::GUI::STL_MODEL_WILDCARD, wxFD_SAVE | wxFD_OVERWRITE_PROMPT)
  2248. if $format eq 'STL';
  2249. $dlg = Wx::FileDialog->new($self, "Save $format file as:", dirname($output_file),
  2250. basename($output_file), &Slic3r::GUI::AMF_MODEL_WILDCARD, wxFD_SAVE | wxFD_OVERWRITE_PROMPT)
  2251. if $format eq 'AMF';
  2252. $dlg = Wx::FileDialog->new($self, "Save $format file as:", dirname($output_file),
  2253. basename($output_file), &Slic3r::GUI::TMF_MODEL_WILDCARD, wxFD_SAVE | wxFD_OVERWRITE_PROMPT)
  2254. if $format eq 'TMF';
  2255. if ($dlg->ShowModal != wxID_OK) {
  2256. $dlg->Destroy;
  2257. return undef;
  2258. }
  2259. $output_file = Slic3r::decode_path($dlg->GetPath);
  2260. $dlg->Destroy;
  2261. }
  2262. return $output_file;
  2263. }
  2264. sub make_thumbnail {
  2265. my $self = shift;
  2266. my ($obj_idx) = @_;
  2267. my $plater_object = $self->{objects}[$obj_idx];
  2268. return if($plater_object->remaking_thumbnail);
  2269. $plater_object->remaking_thumbnail(1);
  2270. $plater_object->thumbnail(Slic3r::ExPolygon::Collection->new);
  2271. my $cb = sub {
  2272. $plater_object->make_thumbnail($self->{model}, $obj_idx);
  2273. if ($Slic3r::have_threads) {
  2274. Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $THUMBNAIL_DONE_EVENT, shared_clone([ $obj_idx ])));
  2275. Slic3r::thread_cleanup();
  2276. threads->exit;
  2277. } else {
  2278. $self->on_thumbnail_made($obj_idx);
  2279. }
  2280. };
  2281. @_ = ();
  2282. $Slic3r::have_threads
  2283. ? threads->create(sub { $cb->(); Slic3r::thread_cleanup(); })->detach
  2284. : $cb->();
  2285. }
  2286. sub on_thumbnail_made {
  2287. my $self = shift;
  2288. my ($obj_idx) = @_;
  2289. $self->{objects}[$obj_idx]->remaking_thumbnail(0);
  2290. $self->{objects}[$obj_idx]->transform_thumbnail($self->{model}, $obj_idx);
  2291. $self->refresh_canvases;
  2292. }
  2293. # this method gets called whenever print center is changed or the objects' bounding box changes
  2294. # (i.e. when an object is added/removed/moved/rotated/scaled)
  2295. sub on_model_change {
  2296. my ($self, $force_autocenter) = @_;
  2297. # reload the select submenu (if already initialized)
  2298. if (my $menu = $self->GetFrame->{plater_select_menu}) {
  2299. $menu->DeleteItem($_) for $menu->GetMenuItems;
  2300. for my $i (0..$#{$self->{objects}}) {
  2301. my $name = $self->{objects}->[$i]->name;
  2302. my $count = $self->{model}->get_object($i)->instances_count;
  2303. if ($count > 1) {
  2304. $name .= " (${count}x)";
  2305. }
  2306. my $item = wxTheApp->append_menu_item($menu, $name, 'Select object', sub {
  2307. $self->select_object($i);
  2308. $self->refresh_canvases;
  2309. }, undef, undef, wxITEM_CHECK);
  2310. $item->Check(1) if $self->{objects}->[$i]->selected;
  2311. }
  2312. }
  2313. # reload the objects info choice
  2314. if (my $choice = $self->{object_info_choice}) {
  2315. $choice->Clear;
  2316. for my $i (0..$#{$self->{objects}}) {
  2317. my $name = $self->{objects}->[$i]->name;
  2318. my $count = $self->{model}->get_object($i)->instances_count;
  2319. if ($count > 1) {
  2320. $name .= " (${count}x)";
  2321. }
  2322. $choice->Append($name);
  2323. }
  2324. my ($obj_idx, $object) = $self->selected_object;
  2325. $choice->SetSelection($obj_idx // -1);
  2326. }
  2327. my $running = $self->pause_background_process;
  2328. if ($Slic3r::GUI::Settings->{_}{autocenter} || $force_autocenter) {
  2329. $self->{model}->center_instances_around_point($self->bed_centerf);
  2330. }
  2331. $self->refresh_canvases;
  2332. my $invalidated = $self->{print}->reload_model_instances();
  2333. if ($Slic3r::GUI::Settings->{_}{background_processing}) {
  2334. if ($invalidated || !$running) {
  2335. # The mere fact that no steps were invalidated when reloading model instances
  2336. # doesn't mean that all steps were done: for example, validation might have
  2337. # failed upon previous instance move, so we have no running thread and no steps
  2338. # are invalidated on this move, thus we need to schedule a new run.
  2339. $self->schedule_background_process;
  2340. $self->toggle_print_stats(0);
  2341. } else {
  2342. $self->resume_background_process;
  2343. }
  2344. } else {
  2345. $self->hide_preview;
  2346. }
  2347. }
  2348. sub hide_preview {
  2349. my ($self) = @_;
  2350. my $sel = $self->{preview_notebook}->GetSelection;
  2351. if ($sel == $self->{preview3D_page_idx} || $sel == $self->{toolpaths2D_page_idx}) {
  2352. $self->{preview_notebook}->SetSelection(0);
  2353. }
  2354. $self->{processed} = 0;
  2355. }
  2356. sub on_extruders_change {
  2357. my ($self, $num_extruders) = @_;
  2358. my $choices = $self->{preset_choosers}{filament};
  2359. while (@$choices < $num_extruders) {
  2360. # copy strings from first choice
  2361. my @presets = $choices->[0]->GetStrings;
  2362. # initialize new choice
  2363. my $choice = Wx::BitmapComboBox->new($self, -1, "", wxDefaultPosition, wxDefaultSize, [@presets], wxCB_READONLY);
  2364. push @$choices, $choice;
  2365. # copy icons from first choice
  2366. $choice->SetItemBitmap($_, $choices->[0]->GetItemBitmap($_)) for 0..$#presets;
  2367. # settings button
  2368. my $settings_btn = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG),
  2369. wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
  2370. # insert new row into sizer
  2371. $self->{presets_sizer}->Insert(6 + ($#$choices-1)*3, 0, 0);
  2372. $self->{presets_sizer}->Insert(7 + ($#$choices-1)*3, $choice, 0, wxEXPAND | wxBOTTOM, FILAMENT_CHOOSERS_SPACING);
  2373. $self->{presets_sizer}->Insert(8 + ($#$choices-1)*3, $settings_btn, 0, wxEXPAND | wxLEFT, 4);
  2374. # setup the listeners
  2375. EVT_COMBOBOX($choice, $choice, sub {
  2376. my ($choice) = @_;
  2377. wxTheApp->CallAfter(sub {
  2378. $self->_on_change_combobox('filament', $choice);
  2379. });
  2380. });
  2381. EVT_BUTTON($self, $settings_btn, sub {
  2382. $self->show_preset_editor('filament', $#$choices);
  2383. });
  2384. # initialize selection
  2385. my $i = first { $choice->GetString($_) eq ($Slic3r::GUI::Settings->{presets}{"filament_" . $#$choices} || '') } 0 .. $#presets;
  2386. $choice->SetSelection($i || 0);
  2387. }
  2388. # remove unused choices if any
  2389. while (@$choices > $num_extruders) {
  2390. my $i = 6 + ($#$choices-1)*3;
  2391. $self->{presets_sizer}->Remove($i); # label
  2392. $self->{presets_sizer}->Remove($i); # wxChoice
  2393. my $settings_btn = $self->{presets_sizer}->GetItem($i)->GetWindow;
  2394. $self->{presets_sizer}->Remove($i); # settings btn
  2395. $settings_btn->Destroy;
  2396. $choices->[-1]->Destroy;
  2397. pop @$choices;
  2398. }
  2399. $self->Layout;
  2400. }
  2401. sub object_cut_dialog {
  2402. my $self = shift;
  2403. my ($obj_idx) = @_;
  2404. if (!defined $obj_idx) {
  2405. ($obj_idx, undef) = $self->selected_object;
  2406. }
  2407. if (!$Slic3r::GUI::have_OpenGL) {
  2408. Slic3r::GUI::show_error($self, "Please install the OpenGL modules to use this feature (see build instructions).");
  2409. return;
  2410. }
  2411. my $dlg = Slic3r::GUI::Plater::ObjectCutDialog->new($self,
  2412. object => $self->{objects}[$obj_idx],
  2413. model_object => $self->{model}->objects->[$obj_idx],
  2414. );
  2415. return unless $dlg->ShowModal == wxID_OK;
  2416. if (my @new_objects = $dlg->NewModelObjects) {
  2417. my $process_dialog = Wx::ProgressDialog->new('Loading…', "Loading new objects…", 100, $self, 0);
  2418. $process_dialog->Pulse;
  2419. # Create two models to save the current object and the resulted objects.
  2420. my $new_objects_model = Slic3r::Model->new;
  2421. foreach my $new_object (@new_objects) {
  2422. $new_objects_model->add_object($new_object);
  2423. }
  2424. my $org_object_model = Slic3r::Model->new;
  2425. $org_object_model->add_object($self->{model}->get_object($obj_idx));
  2426. # Save the object identifiers used in undo/redo operations.
  2427. my $object_id = $self->{objects}->[$obj_idx]->identifier;
  2428. my $new_objects_id_start = $self->{object_identifier};
  2429. $self->add_undo_operation("CUT", $object_id, $org_object_model, $new_objects_model, $new_objects_id_start);
  2430. $self->remove($obj_idx, 'true');
  2431. $self->load_model_objects(grep defined($_), @new_objects);
  2432. $self->arrange if @new_objects <= 2; # don't arrange for grid cuts
  2433. $process_dialog->Destroy;
  2434. }
  2435. }
  2436. sub object_layers_dialog {
  2437. my $self = shift;
  2438. my ($obj_idx) = @_;
  2439. $self->object_settings_dialog($obj_idx, adaptive_layers => 1);
  2440. }
  2441. sub object_settings_dialog {
  2442. my $self = shift;
  2443. my ($obj_idx, %params) = @_;
  2444. if (!defined $obj_idx) {
  2445. ($obj_idx, undef) = $self->selected_object;
  2446. }
  2447. my $model_object = $self->{model}->objects->[$obj_idx];
  2448. # validate config before opening the settings dialog because
  2449. # that dialog can't be closed if validation fails, but user
  2450. # can't fix any error which is outside that dialog
  2451. return unless $self->validate_config;
  2452. my $dlg = Slic3r::GUI::Plater::ObjectSettingsDialog->new($self,
  2453. object => $self->{objects}[$obj_idx],
  2454. model_object => $model_object,
  2455. obj_idx => $obj_idx,
  2456. );
  2457. # store pointer to the adaptive layer tab to push preview updates
  2458. $self->{AdaptiveLayersDialog} = $dlg->{adaptive_layers};
  2459. # and jump directly to the tab if called by "promo-button"
  2460. $dlg->{tabpanel}->SetSelection(1) if $params{adaptive_layers};
  2461. $self->pause_background_process;
  2462. $dlg->ShowModal;
  2463. $self->{AdaptiveLayersDialog} = undef;
  2464. # update thumbnail since parts may have changed
  2465. if ($dlg->PartsChanged) {
  2466. # recenter and re-align to Z = 0
  2467. $model_object->center_around_origin;
  2468. $self->make_thumbnail($obj_idx);
  2469. }
  2470. # update print
  2471. if ($dlg->PartsChanged || $dlg->PartSettingsChanged) {
  2472. $self->stop_background_process;
  2473. $self->{print}->reload_object($obj_idx);
  2474. $self->on_model_change;
  2475. } else {
  2476. $self->resume_background_process;
  2477. }
  2478. }
  2479. sub object_list_changed {
  2480. my $self = shift;
  2481. my $have_objects = @{$self->{objects}} ? 1 : 0;
  2482. my $method = $have_objects ? 'Enable' : 'Disable';
  2483. $self->{"btn_$_"}->$method
  2484. for grep $self->{"btn_$_"}, qw(reset arrange export_gcode export_stl print send_gcode);
  2485. if ($self->{export_gcode_output_file} || $self->{send_gcode_file}) {
  2486. $self->{btn_export_gcode}->Disable;
  2487. $self->{btn_print}->Disable;
  2488. $self->{btn_send_gcode}->Disable;
  2489. }
  2490. if ($self->{htoolbar}) {
  2491. $self->{htoolbar}->EnableTool($_, $have_objects)
  2492. for (TB_RESET, TB_ARRANGE);
  2493. }
  2494. # prepagate the event to the frame (a custom Wx event would be cleaner)
  2495. $self->GetFrame->on_plater_object_list_changed($have_objects);
  2496. }
  2497. sub selection_changed {
  2498. my $self = shift;
  2499. my ($obj_idx, $object) = $self->selected_object;
  2500. my $have_sel = defined $obj_idx;
  2501. # Remove selection in 2d Plater.
  2502. $self->{canvas}->{selected_instance} = undef;
  2503. if (my $menu = $self->GetFrame->{plater_select_menu}) {
  2504. $_->Check(0) for $menu->GetMenuItems;
  2505. if ($have_sel) {
  2506. $menu->FindItemByPosition($obj_idx)->Check(1);
  2507. }
  2508. }
  2509. my $method = $have_sel ? 'Enable' : 'Disable';
  2510. $self->{"btn_$_"}->$method
  2511. for grep $self->{"btn_$_"}, qw(remove increase decrease rotateX90cw rotateX90ccw rotateY90cw rotateY90ccw rotateZ90cw rotateZ90ccw rotateZ45cw rotateZ45ccw rotateFace changescale split cut layers settings);
  2512. if ($self->{htoolbar}) {
  2513. $self->{htoolbar}->EnableTool($_, $have_sel)
  2514. for (TB_REMOVE, TB_MORE, TB_FEWER, TB_X90CW, TB_X90CCW, TB_Y90CW, TB_Y90CCW, TB_Z90CW, TB_Z90CCW, TB_45CW, TB_45CCW, TB_ROTFACE, TB_SCALE, TB_SPLIT, TB_CUT, TB_LAYERS, TB_SETTINGS);
  2515. }
  2516. if ($self->{object_info_size}) { # have we already loaded the info pane?
  2517. if ($have_sel) {
  2518. my $model_object = $self->{model}->objects->[$obj_idx];
  2519. $self->{object_info_choice}->SetSelection($obj_idx);
  2520. $self->{object_info_copies}->SetLabel($model_object->instances_count);
  2521. my $model_instance = $model_object->instances->[0];
  2522. {
  2523. my $size_string = sprintf "%.2f x %.2f x %.2f", @{$model_object->instance_bounding_box(0)->size};
  2524. if ($model_instance->scaling_factor != 1) {
  2525. $size_string .= sprintf " (%s%%)", $model_instance->scaling_factor * 100;
  2526. }
  2527. $self->{object_info_size}->SetLabel($size_string);
  2528. }
  2529. $self->{object_info_materials}->SetLabel($model_object->materials_count);
  2530. my $raw_mesh = $model_object->raw_mesh;
  2531. $raw_mesh->repair; # this calculates number_of_parts
  2532. if (my $stats = $raw_mesh->stats) {
  2533. $self->{object_info_volume}->SetLabel(sprintf('%.2f', $raw_mesh->volume * ($model_instance->scaling_factor**3)));
  2534. $self->{object_info_facets}->SetLabel(sprintf('%d (%d shells)', $model_object->facets_count, $stats->{number_of_parts}));
  2535. if (my $errors = sum(@$stats{qw(degenerate_facets edges_fixed facets_removed facets_added facets_reversed backwards_edges)})) {
  2536. $self->{object_info_manifold}->SetLabel(sprintf("Auto-repaired (%d errors)", $errors));
  2537. $self->{object_info_manifold_warning_icon}->Show;
  2538. # we don't show normals_fixed because we never provide normals
  2539. # to admesh, so it generates normals for all facets
  2540. my $message = sprintf '%d degenerate facets, %d edges fixed, %d facets removed, %d facets added, %d facets reversed, %d backwards edges',
  2541. @$stats{qw(degenerate_facets edges_fixed facets_removed facets_added facets_reversed backwards_edges)};
  2542. $self->{object_info_manifold}->SetToolTipString($message);
  2543. $self->{object_info_manifold_warning_icon}->SetToolTipString($message);
  2544. } else {
  2545. $self->{object_info_manifold}->SetLabel("Yes");
  2546. }
  2547. } else {
  2548. $self->{object_info_facets}->SetLabel($object->facets);
  2549. }
  2550. } else {
  2551. $self->{object_info_choice}->SetSelection(-1);
  2552. $self->{"object_info_$_"}->SetLabel("") for qw(copies size volume facets materials manifold);
  2553. $self->{object_info_manifold_warning_icon}->Hide;
  2554. $self->{object_info_manifold}->SetToolTipString("");
  2555. }
  2556. $self->Layout;
  2557. }
  2558. # prepagate the event to the frame (a custom Wx event would be cleaner)
  2559. $self->GetFrame->on_plater_selection_changed($have_sel);
  2560. }
  2561. sub select_object {
  2562. my ($self, $obj_idx) = @_;
  2563. $_->selected(0) for @{ $self->{objects} };
  2564. $_->selected_instance(-1) for @{ $self->{objects} };
  2565. if (defined $obj_idx) {
  2566. $self->{objects}->[$obj_idx]->selected(1);
  2567. $self->{objects}->[$obj_idx]->selected_instance(0);
  2568. }
  2569. $self->selection_changed(1);
  2570. }
  2571. sub select_next {
  2572. my ($self) = @_;
  2573. return if !@{$self->{objects}};
  2574. my ($obj_idx, $object) = $self->selected_object;
  2575. if (!defined $obj_idx || $obj_idx == $#{$self->{objects}}) {
  2576. $obj_idx = 0;
  2577. } else {
  2578. $obj_idx++;
  2579. }
  2580. $self->select_object($obj_idx);
  2581. $self->refresh_canvases;
  2582. }
  2583. sub select_prev {
  2584. my ($self) = @_;
  2585. return if !@{$self->{objects}};
  2586. my ($obj_idx, $object) = $self->selected_object;
  2587. if (!defined $obj_idx || $obj_idx == 0) {
  2588. $obj_idx = $#{$self->{objects}};
  2589. } else {
  2590. $obj_idx--;
  2591. }
  2592. $self->select_object($obj_idx);
  2593. $self->refresh_canvases;
  2594. }
  2595. sub selected_object {
  2596. my $self = shift;
  2597. my $obj_idx = first { $self->{objects}[$_]->selected } 0..$#{ $self->{objects} };
  2598. return undef if !defined $obj_idx;
  2599. return ($obj_idx, $self->{objects}[$obj_idx]),
  2600. }
  2601. sub refresh_canvases {
  2602. my ($self) = @_;
  2603. $self->{canvas}->Refresh;
  2604. $self->{canvas3D}->update if $self->{canvas3D};
  2605. $self->{preview3D}->reload_print if $self->{preview3D};
  2606. }
  2607. sub validate_config {
  2608. my $self = shift;
  2609. eval {
  2610. $self->config->validate;
  2611. };
  2612. return 0 if Slic3r::GUI::catch_error($self);
  2613. return 1;
  2614. }
  2615. sub statusbar {
  2616. my $self = shift;
  2617. return $self->GetFrame->{statusbar};
  2618. }
  2619. sub object_menu {
  2620. my ($self) = @_;
  2621. my $frame = $self->GetFrame;
  2622. my $menu = Wx::Menu->new;
  2623. wxTheApp->append_menu_item($menu, "Delete\tCtrl+Del", 'Remove the selected object', sub {
  2624. $self->remove;
  2625. }, undef, 'brick_delete.png');
  2626. wxTheApp->append_menu_item($menu, "Increase copies\tCtrl++", 'Place one more copy of the selected object', sub {
  2627. $self->increase;
  2628. }, undef, 'add.png');
  2629. wxTheApp->append_menu_item($menu, "Decrease copies\tCtrl+-", 'Remove one copy of the selected object', sub {
  2630. $self->decrease;
  2631. }, undef, 'delete.png');
  2632. wxTheApp->append_menu_item($menu, "Set number of copies…", 'Change the number of copies of the selected object', sub {
  2633. $self->set_number_of_copies;
  2634. }, undef, 'textfield.png');
  2635. if (!$Slic3r::GUI::Settings->{_}{autocenter}){
  2636. $menu->AppendSeparator();
  2637. wxTheApp->append_menu_item($menu, "Move to bed center", 'Center object around bed center', sub {
  2638. $self->center_selected_object_on_bed;
  2639. }, undef, 'arrow_in.png');
  2640. }
  2641. wxTheApp->append_menu_item($menu, "Rotate Face to Plane", 'Rotates the selected object to have the selected face parallel with a plane', sub {
  2642. $self->rotate_face;
  2643. }, undef, 'rotate_face.png');
  2644. $menu->AppendSeparator();
  2645. {
  2646. my $rotateMenu = Wx::Menu->new;
  2647. wxTheApp->append_menu_item($rotateMenu, "180° around X axis", 'Rotate the selected object by 180° around X axis', sub {
  2648. $self->rotate(180, X);
  2649. }, undef, @rotateX90ccw);
  2650. wxTheApp->append_menu_item($rotateMenu, "180° around Y axis", 'Rotate the selected object by 180° around Y axis', sub {
  2651. $self->rotate(180, Y);
  2652. }, undef, @rotateY90ccw);
  2653. wxTheApp->append_menu_item($rotateMenu, "180° around Z axis", 'Rotate the selected object by 180° around Z axis', sub {
  2654. $self->rotate(180, Z);
  2655. }, undef, @rotateZ90ccw);
  2656. $rotateMenu->AppendSeparator();
  2657. wxTheApp->append_menu_item($rotateMenu, "Around X axis…", 'Rotate the selected object by an arbitrary angle around X axis', sub {
  2658. $self->rotate(undef, X);
  2659. }, undef, @rotateX90ccw);
  2660. wxTheApp->append_menu_item($rotateMenu, "Around Y axis…", 'Rotate the selected object by an arbitrary angle around Y axis', sub {
  2661. $self->rotate(undef, Y);
  2662. }, undef, @rotateY90ccw);
  2663. wxTheApp->append_menu_item($rotateMenu, "Around Z axis…", 'Rotate the selected object by an arbitrary angle around Z axis', sub {
  2664. $self->rotate(undef, Z);
  2665. }, undef, @rotateZ90ccw);
  2666. wxTheApp->append_submenu($menu, "Rotate", 'Rotate the selected object', $rotateMenu, undef, 'arrow_rotate_anticlockwise.png');
  2667. }
  2668. {
  2669. my $mirrorMenu = Wx::Menu->new;
  2670. wxTheApp->append_menu_item($mirrorMenu, "Along X axis", 'Mirror the selected object along the X axis', sub {
  2671. $self->mirror(X);
  2672. }, undef, @mirrorX);
  2673. wxTheApp->append_menu_item($mirrorMenu, "Along Y axis", 'Mirror the selected object along the Y axis', sub {
  2674. $self->mirror(Y);
  2675. }, undef, @mirrorY);
  2676. wxTheApp->append_menu_item($mirrorMenu, "Along Z axis", 'Mirror the selected object along the Z axis', sub {
  2677. $self->mirror(Z);
  2678. }, undef, @mirrorZ);
  2679. wxTheApp->append_submenu($menu, "Mirror", 'Mirror the selected object', $mirrorMenu, undef, 'shape_flip_horizontal.png');
  2680. }
  2681. {
  2682. my $scaleMenu = Wx::Menu->new;
  2683. wxTheApp->append_menu_item($scaleMenu, "Uniformly…", 'Scale the selected object along the XYZ axes', sub {
  2684. $self->changescale(undef);
  2685. });
  2686. wxTheApp->append_menu_item($scaleMenu, "Along X axis…", 'Scale the selected object along the X axis', sub {
  2687. $self->changescale(X);
  2688. }, undef, 'bullet_red.png');
  2689. wxTheApp->append_menu_item($scaleMenu, "Along Y axis…", 'Scale the selected object along the Y axis', sub {
  2690. $self->changescale(Y);
  2691. }, undef, 'bullet_green.png');
  2692. wxTheApp->append_menu_item($scaleMenu, "Along Z axis…", 'Scale the selected object along the Z axis', sub {
  2693. $self->changescale(Z);
  2694. }, undef, 'bullet_blue.png');
  2695. wxTheApp->append_submenu($menu, "Scale", 'Scale the selected object by a given factor', $scaleMenu, undef, 'arrow_out.png');
  2696. }
  2697. {
  2698. my $scaleToSizeMenu = Wx::Menu->new;
  2699. wxTheApp->append_menu_item($scaleToSizeMenu, "Uniformly…", 'Scale the selected object along the XYZ axes', sub {
  2700. $self->changescale(undef, 1);
  2701. });
  2702. wxTheApp->append_menu_item($scaleToSizeMenu, "Along X axis…", 'Scale the selected object along the X axis', sub {
  2703. $self->changescale(X, 1);
  2704. }, undef, 'bullet_red.png');
  2705. wxTheApp->append_menu_item($scaleToSizeMenu, "Along Y axis…", 'Scale the selected object along the Y axis', sub {
  2706. $self->changescale(Y, 1);
  2707. }, undef, 'bullet_green.png');
  2708. wxTheApp->append_menu_item($scaleToSizeMenu, "Along Z axis…", 'Scale the selected object along the Z axis', sub {
  2709. $self->changescale(Z, 1);
  2710. }, undef, 'bullet_blue.png');
  2711. wxTheApp->append_submenu($menu, "Scale to size", 'Scale the selected object to match a given size', $scaleToSizeMenu, undef, 'arrow_out.png');
  2712. }
  2713. wxTheApp->append_menu_item($menu, "Split", 'Split the selected object into individual parts', sub {
  2714. $self->split_object;
  2715. }, undef, 'shape_ungroup.png');
  2716. wxTheApp->append_menu_item($menu, "Cut…", 'Open the 3D cutting tool', sub {
  2717. $self->object_cut_dialog;
  2718. }, undef, 'package.png');
  2719. wxTheApp->append_menu_item($menu, "Layer heights…", 'Open the dynamic layer height control', sub {
  2720. $self->object_layers_dialog;
  2721. }, undef, 'variable_layer_height.png');
  2722. $menu->AppendSeparator();
  2723. wxTheApp->append_menu_item($menu, "Settings…", 'Open the object editor dialog', sub {
  2724. $self->object_settings_dialog;
  2725. }, undef, 'cog.png');
  2726. $menu->AppendSeparator();
  2727. wxTheApp->append_menu_item($menu, "Reload from Disk", 'Reload the selected file from Disk', sub {
  2728. $self->reload_from_disk;
  2729. }, undef, 'arrow_refresh.png');
  2730. wxTheApp->append_menu_item($menu, "Export object as STL…", 'Export this single object as STL file', sub {
  2731. $self->export_object_stl;
  2732. }, undef, 'brick_go.png');
  2733. wxTheApp->append_menu_item($menu, "Export object and modifiers as AMF…", 'Export this single object and all associated modifiers as AMF file', sub {
  2734. $self->export_object_amf;
  2735. }, undef, 'brick_go.png');
  2736. wxTheApp->append_menu_item($menu, "Export object and modifiers as 3MF…", 'Export this single object and all associated modifiers as 3MF file', sub {
  2737. $self->export_object_tmf;
  2738. }, undef, 'brick_go.png');
  2739. return $menu;
  2740. }
  2741. # Set a camera direction, zoom to all objects.
  2742. sub select_view {
  2743. my ($self, $direction) = @_;
  2744. my $idx_page = $self->{preview_notebook}->GetSelection;
  2745. my $page = ($idx_page == &Wx::wxNOT_FOUND) ? '3D' : $self->{preview_notebook}->GetPageText($idx_page);
  2746. if ($page eq 'Preview') {
  2747. $self->{preview3D}->canvas->select_view($direction);
  2748. $self->{canvas3D}->set_viewport_from_scene($self->{preview3D}->canvas);
  2749. } else {
  2750. $self->{canvas3D}->select_view($direction);
  2751. $self->{preview3D}->canvas->set_viewport_from_scene($self->{canvas3D});
  2752. }
  2753. }
  2754. sub zoom{
  2755. my ($self, $direction) = @_;
  2756. #Apply Zoom to the current active tab
  2757. my ($currentSelection) = $self->{preview_notebook}->GetSelection;
  2758. if($currentSelection == 0){
  2759. $self->{canvas3D}->zoom($direction) if($self->{canvas3D});
  2760. }
  2761. elsif($currentSelection == 2){ #3d Preview tab
  2762. $self->{preview3D}->canvas->zoom($direction) if($self->{preview3D});
  2763. }
  2764. elsif($currentSelection == 3) { #2D toolpaths tab
  2765. $self->{toolpaths2D}->{canvas}->zoom($direction) if($self->{toolpaths2D});
  2766. }
  2767. }
  2768. package Slic3r::GUI::Plater::DropTarget;
  2769. use Wx::DND;
  2770. use base 'Wx::FileDropTarget';
  2771. sub new {
  2772. my $class = shift;
  2773. my ($window) = @_;
  2774. my $self = $class->SUPER::new;
  2775. $self->{window} = $window;
  2776. return $self;
  2777. }
  2778. sub OnDropFiles {
  2779. my $self = shift;
  2780. my ($x, $y, $filenames) = @_;
  2781. # stop scalars leaking on older perl
  2782. # https://rt.perl.org/rt3/Public/Bug/Display.html?id=70602
  2783. @_ = ();
  2784. # only accept STL, OBJ and AMF files
  2785. return 0 if grep !/\.(?:stl|obj|amf(?:\.xml)?)$/i, @$filenames;
  2786. $self->{window}->load_file($_) for @$filenames;
  2787. }
  2788. # 2D preview of an object. Each object is previewed by its convex hull.
  2789. package Slic3r::GUI::Plater::Object;
  2790. use Moo;
  2791. use List::Util qw(first);
  2792. use Slic3r::Geometry qw(X Y Z MIN MAX deg2rad);
  2793. has 'name' => (is => 'rw', required => 1);
  2794. has 'identifier' => (is => 'rw', required => 1);
  2795. has 'input_file' => (is => 'rw');
  2796. has 'input_file_obj_idx' => (is => 'rw');
  2797. has 'thumbnail' => (is => 'rw'); # ExPolygon::Collection in scaled model units with no transforms
  2798. has 'transformed_thumbnail' => (is => 'rw');
  2799. has 'remaking_thumbnail' => (is => 'rw', default => sub { 0 });
  2800. 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
  2801. has 'selected' => (is => 'rw', default => sub { 0 });
  2802. has 'selected_instance' => (is => 'rw', default => sub { -1 });
  2803. sub make_thumbnail {
  2804. my ($self, $model, $obj_idx) = @_;
  2805. # make method idempotent
  2806. $self->thumbnail->clear;
  2807. my $mesh = $model->objects->[$obj_idx]->raw_mesh;
  2808. # Apply x, y rotations and scaling vector in case of reading a 3MF model object.
  2809. my $model_instance = $model->objects->[$obj_idx]->instances->[0];
  2810. $mesh->rotate_x($model_instance->x_rotation);
  2811. $mesh->rotate_y($model_instance->y_rotation);
  2812. $mesh->scale_xyz($model_instance->scaling_vector);
  2813. if ($mesh->facets_count <= 5000) {
  2814. # remove polygons with area <= 1mm
  2815. my $area_threshold = Slic3r::Geometry::scale 1;
  2816. $self->thumbnail->append(
  2817. grep $_->area >= $area_threshold,
  2818. @{ $mesh->horizontal_projection }, # horizontal_projection returns scaled expolygons
  2819. );
  2820. $self->thumbnail->simplify(0.5);
  2821. } else {
  2822. my $convex_hull = Slic3r::ExPolygon->new($mesh->convex_hull);
  2823. $self->thumbnail->append($convex_hull);
  2824. }
  2825. return $self->thumbnail;
  2826. }
  2827. sub transform_thumbnail {
  2828. my ($self, $model, $obj_idx) = @_;
  2829. return unless defined $self->thumbnail;
  2830. my $model_object = $model->objects->[$obj_idx];
  2831. my $model_instance = $model_object->instances->[0];
  2832. # the order of these transformations MUST be the same everywhere, including
  2833. # in Slic3r::Print->add_model_object()
  2834. my $t = $self->thumbnail->clone;
  2835. $t->rotate($model_instance->rotation, Slic3r::Point->new(0,0));
  2836. $t->scale($model_instance->scaling_factor);
  2837. $self->transformed_thumbnail($t);
  2838. }
  2839. package Slic3r::GUI::Plater::OctoPrintSpoolDialog;
  2840. use Wx qw(:dialog :id :misc :sizer :icon wxTheApp);
  2841. use Wx::Event qw(EVT_BUTTON EVT_TEXT_ENTER);
  2842. use base 'Wx::Dialog';
  2843. sub new {
  2844. my $class = shift;
  2845. my ($parent, $filename) = @_;
  2846. my $self = $class->SUPER::new($parent, -1, "Send to Server", wxDefaultPosition,
  2847. [400, -1]);
  2848. $self->{filename} = $filename;
  2849. $Slic3r::GUI::Settings->{octoprint} //= {};
  2850. my $optgroup;
  2851. $optgroup = Slic3r::GUI::OptionsGroup->new(
  2852. parent => $self,
  2853. title => 'Send to Server',
  2854. on_change => sub {
  2855. my ($opt_id) = @_;
  2856. if ($opt_id eq 'filename') {
  2857. $self->{filename} = $optgroup->get_value($opt_id);
  2858. } else {
  2859. $Slic3r::GUI::Settings->{octoprint}{$opt_id} = $optgroup->get_value($opt_id);
  2860. }
  2861. },
  2862. label_width => 200,
  2863. );
  2864. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  2865. opt_id => 'filename',
  2866. type => 's',
  2867. label => 'File name',
  2868. width => 200,
  2869. tooltip => 'The name used for labelling the print job.',
  2870. default => $filename,
  2871. ));
  2872. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  2873. opt_id => 'overwrite',
  2874. type => 'bool',
  2875. label => 'Overwrite existing file',
  2876. tooltip => 'If selected, any existing file with the same name will be overwritten without confirmation.',
  2877. default => $Slic3r::GUI::Settings->{octoprint}{overwrite} // 0,
  2878. ));
  2879. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  2880. opt_id => 'start',
  2881. type => 'bool',
  2882. label => 'Start print',
  2883. tooltip => 'If selected, print will start after the upload.',
  2884. default => $Slic3r::GUI::Settings->{octoprint}{start} // 0,
  2885. ));
  2886. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  2887. $sizer->Add($optgroup->sizer, 0, wxEXPAND | wxTOP | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  2888. my $buttons = $self->CreateStdDialogButtonSizer(wxOK | wxCANCEL);
  2889. $sizer->Add($buttons, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  2890. EVT_BUTTON($self, wxID_OK, sub {
  2891. wxTheApp->save_settings;
  2892. $self->EndModal(wxID_OK);
  2893. $self->Destroy;
  2894. });
  2895. $self->SetSizer($sizer);
  2896. $sizer->SetSizeHints($self);
  2897. return $self;
  2898. }
  2899. 1;