MainFrame.pm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. # The main frame, the parent of all.
  2. package Slic3r::GUI::MainFrame;
  3. use strict;
  4. use warnings;
  5. use utf8;
  6. use File::Basename qw(basename dirname);
  7. use FindBin;
  8. use List::Util qw(min first);
  9. use Slic3r::Geometry qw(X Y);
  10. use Wx qw(:frame :bitmap :id :misc :notebook :panel :sizer :menu :dialog :filedialog
  11. :font :icon wxTheApp);
  12. use Wx::Event qw(EVT_CLOSE EVT_COMMAND EVT_MENU EVT_NOTEBOOK_PAGE_CHANGED);
  13. use base 'Wx::Frame';
  14. our $qs_last_input_file;
  15. our $qs_last_output_file;
  16. our $last_config;
  17. # Events to be sent from a C++ Tab implementation:
  18. # 1) To inform about a change of a configuration value.
  19. our $VALUE_CHANGE_EVENT = Wx::NewEventType;
  20. # 2) To inform about a preset selection change or a "modified" status change.
  21. our $PRESETS_CHANGED_EVENT = Wx::NewEventType;
  22. # 3) To inform about a click on Browse button
  23. our $BUTTON_BROWSE_EVENT = Wx::NewEventType;
  24. # 4) To inform about a click on Test button
  25. our $BUTTON_TEST_EVENT = Wx::NewEventType;
  26. sub new {
  27. my ($class, %params) = @_;
  28. my $self = $class->SUPER::new(undef, -1, $Slic3r::FORK_NAME . ' - ' . $Slic3r::VERSION, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE);
  29. Slic3r::GUI::set_main_frame($self);
  30. if ($^O eq 'MSWin32') {
  31. # Load the icon either from the exe, or from the ico file.
  32. my $iconfile = Slic3r::decode_path($FindBin::Bin) . '\slic3r.exe';
  33. $iconfile = Slic3r::var("Slic3r.ico") unless -f $iconfile;
  34. $self->SetIcon(Wx::Icon->new($iconfile, wxBITMAP_TYPE_ICO));
  35. } else {
  36. $self->SetIcon(Wx::Icon->new(Slic3r::var("Slic3r_128px.png"), wxBITMAP_TYPE_PNG));
  37. }
  38. # store input params
  39. # If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
  40. $self->{no_controller} = $params{no_controller};
  41. $self->{no_plater} = $params{no_plater};
  42. $self->{loaded} = 0;
  43. $self->{lang_ch_event} = $params{lang_ch_event};
  44. # initialize tabpanel and menubar
  45. $self->_init_tabpanel;
  46. $self->_init_menubar;
  47. # set default tooltip timer in msec
  48. # SetAutoPop supposedly accepts long integers but some bug doesn't allow for larger values
  49. # (SetAutoPop is not available on GTK.)
  50. eval { Wx::ToolTip::SetAutoPop(32767) };
  51. # initialize status bar
  52. $self->{statusbar} = Slic3r::GUI::ProgressStatusBar->new($self, -1);
  53. $self->{statusbar}->SetStatusText("Version $Slic3r::VERSION - Remember to check for updates at http://github.com/prusa3d/slic3r/releases");
  54. $self->SetStatusBar($self->{statusbar});
  55. $self->{loaded} = 1;
  56. # initialize layout
  57. {
  58. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  59. $sizer->Add($self->{tabpanel}, 1, wxEXPAND);
  60. $sizer->SetSizeHints($self);
  61. $self->SetSizer($sizer);
  62. $self->Fit;
  63. $self->SetMinSize([760, 490]);
  64. $self->SetSize($self->GetMinSize);
  65. wxTheApp->restore_window_pos($self, "main_frame");
  66. $self->Show;
  67. $self->Layout;
  68. }
  69. # declare events
  70. EVT_CLOSE($self, sub {
  71. my (undef, $event) = @_;
  72. if ($event->CanVeto && !$self->check_unsaved_changes) {
  73. $event->Veto;
  74. return;
  75. }
  76. # save window size
  77. wxTheApp->save_window_pos($self, "main_frame");
  78. # Save the slic3r.ini. Usually the ini file is saved from "on idle" callback,
  79. # but in rare cases it may not have been called yet.
  80. wxTheApp->{app_config}->save;
  81. # propagate event
  82. $event->Skip;
  83. });
  84. $self->update_ui_from_settings;
  85. return $self;
  86. }
  87. sub _init_tabpanel {
  88. my ($self) = @_;
  89. $self->{tabpanel} = my $panel = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
  90. Slic3r::GUI::set_tab_panel($panel);
  91. EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{tabpanel}, sub {
  92. my $panel = $self->{tabpanel}->GetCurrentPage;
  93. $panel->OnActivate if $panel->can('OnActivate');
  94. });
  95. if (!$self->{no_plater}) {
  96. $panel->AddPage($self->{plater} = Slic3r::GUI::Plater->new($panel), "Plater");
  97. if (!$self->{no_controller}) {
  98. $panel->AddPage($self->{controller} = Slic3r::GUI::Controller->new($panel), "Controller");
  99. }
  100. }
  101. #TODO this is an example of a Slic3r XS interface call to add a new preset editor page to the main view.
  102. # The following event is emited by the C++ Tab implementation on config value change.
  103. EVT_COMMAND($self, -1, $VALUE_CHANGE_EVENT, sub {
  104. my ($self, $event) = @_;
  105. my $str = $event->GetString;
  106. my ($opt_key, $name) = ($str =~ /(.*) (.*)/);
  107. #print "VALUE_CHANGE_EVENT: ", $opt_key, "\n";
  108. my $tab = Slic3r::GUI::get_preset_tab($name);
  109. my $config = $tab->get_config;
  110. if ($self->{plater}) {
  111. $self->{plater}->on_config_change($config); # propagate config change events to the plater
  112. if ($opt_key eq 'extruders_count'){
  113. my $value = $event->GetInt();
  114. $self->{plater}->on_extruders_change($value);
  115. }
  116. }
  117. # don't save while loading for the first time
  118. $self->config->save($Slic3r::GUI::autosave) if $Slic3r::GUI::autosave && $self->{loaded};
  119. });
  120. # The following event is emited by the C++ Tab implementation on preset selection,
  121. # or when the preset's "modified" status changes.
  122. EVT_COMMAND($self, -1, $PRESETS_CHANGED_EVENT, sub {
  123. my ($self, $event) = @_;
  124. my $tab_name = $event->GetString;
  125. my $tab = Slic3r::GUI::get_preset_tab($tab_name);
  126. if ($self->{plater}) {
  127. # Update preset combo boxes (Print settings, Filament, Printer) from their respective tabs.
  128. my $presets = $tab->get_presets;
  129. if (defined $presets){
  130. my $reload_dependent_tabs = $tab->get_dependent_tabs;
  131. $self->{plater}->update_presets($tab_name, $reload_dependent_tabs, $presets);
  132. if ($tab_name eq 'printer') {
  133. # Printer selected at the Printer tab, update "compatible" marks at the print and filament selectors.
  134. for my $tab_name_other (qw(print filament)) {
  135. # If the printer tells us that the print or filament preset has been switched or invalidated,
  136. # refresh the print or filament tab page. Otherwise just refresh the combo box.
  137. my $update_action = ($reload_dependent_tabs && (first { $_ eq $tab_name_other } (@{$reload_dependent_tabs})))
  138. ? 'load_current_preset' : 'update_tab_ui';
  139. $self->{options_tabs}{$tab_name_other}->$update_action;
  140. }
  141. # Update the controller printers.
  142. $self->{controller}->update_presets($presets) if $self->{controller};
  143. }
  144. $self->{plater}->on_config_change($tab->get_config);
  145. }
  146. }
  147. });
  148. # The following event is emited by the C++ Tab implementation ,
  149. # when the Browse button was clicked
  150. EVT_COMMAND($self, -1, $BUTTON_BROWSE_EVENT, sub {
  151. my ($self, $event) = @_;
  152. my $msg = $event->GetString;
  153. print "BUTTON_BROWSE_EVENT: ", $msg, "\n";
  154. # look for devices
  155. my $entries;
  156. {
  157. my $res = Net::Bonjour->new('http');
  158. $res->discover;
  159. $entries = [ $res->entries ];
  160. }
  161. if (@{$entries}) {
  162. my $dlg = Slic3r::GUI::BonjourBrowser->new($self, $entries);
  163. my $tab = Slic3r::GUI::get_preset_tab("printer");
  164. $tab->load_key_value('octoprint_host', $dlg->GetValue . ":" . $dlg->GetPort)
  165. if $dlg->ShowModal == wxID_OK;
  166. } else {
  167. Wx::MessageDialog->new($self, 'No Bonjour device found', 'Device Browser', wxOK | wxICON_INFORMATION)->ShowModal;
  168. }
  169. });
  170. # The following event is emited by the C++ Tab implementation ,
  171. # when the Test button was clicked
  172. EVT_COMMAND($self, -1, $BUTTON_TEST_EVENT, sub {
  173. my ($self, $event) = @_;
  174. my $msg = $event->GetString;
  175. print "BUTTON_TEST_EVENT: ", $msg, "\n";
  176. my $ua = LWP::UserAgent->new;
  177. $ua->timeout(10);
  178. my $config = Slic3r::GUI::get_preset_tab("printer")->get_config;
  179. my $res = $ua->get(
  180. "http://" . $config->octoprint_host . "/api/version",
  181. 'X-Api-Key' => $config->octoprint_apikey,
  182. );
  183. if ($res->is_success) {
  184. Slic3r::GUI::show_info($self, "Connection to OctoPrint works correctly.", "Success!");
  185. } else {
  186. Slic3r::GUI::show_error($self,
  187. "I wasn't able to connect to OctoPrint (" . $res->status_line . "). "
  188. . "Check hostname and OctoPrint version (at least 1.1.0 is required).");
  189. }
  190. });
  191. # A variable to inform C++ Tab implementation about disabling of Browse button
  192. $self->{is_disabled_button_browse} = (!eval "use Net::Bonjour; 1") ? 1 : 0 ;
  193. # A variable to inform C++ Tab implementation about user_agent
  194. $self->{is_user_agent} = (eval "use LWP::UserAgent; 1") ? 1 : 0 ;
  195. Slic3r::GUI::create_preset_tabs(wxTheApp->{preset_bundle}, wxTheApp->{app_config},
  196. $self->{no_controller}, $self->{is_disabled_button_browse},
  197. $self->{is_user_agent},
  198. $VALUE_CHANGE_EVENT, $PRESETS_CHANGED_EVENT,
  199. $BUTTON_BROWSE_EVENT, $BUTTON_TEST_EVENT);
  200. $self->{options_tabs} = {};
  201. for my $tab_name (qw(print filament printer)) {
  202. $self->{options_tabs}{$tab_name} = Slic3r::GUI::get_preset_tab("$tab_name");
  203. }
  204. if ($self->{plater}) {
  205. $self->{plater}->on_select_preset(sub {
  206. my ($group, $name) = @_;
  207. $self->{options_tabs}{$group}->select_preset($name);
  208. });
  209. # load initial config
  210. my $full_config = wxTheApp->{preset_bundle}->full_config;
  211. $self->{plater}->on_config_change($full_config);
  212. # Show a correct number of filament fields.
  213. $self->{plater}->on_extruders_change(int(@{$full_config->nozzle_diameter}));
  214. }
  215. }
  216. sub _init_menubar {
  217. my ($self) = @_;
  218. # File menu
  219. my $fileMenu = Wx::Menu->new;
  220. {
  221. wxTheApp->append_menu_item($fileMenu, "Open STL/OBJ/AMF…\tCtrl+O", 'Open a model', sub {
  222. $self->{plater}->add if $self->{plater};
  223. }, undef, undef); #'brick_add.png');
  224. $self->_append_menu_item($fileMenu, "&Load Config…\tCtrl+L", 'Load exported configuration file', sub {
  225. $self->load_config_file;
  226. }, undef, 'plugin_add.png');
  227. $self->_append_menu_item($fileMenu, "&Export Config…\tCtrl+E", 'Export current configuration to file', sub {
  228. $self->export_config;
  229. }, undef, 'plugin_go.png');
  230. $self->_append_menu_item($fileMenu, "&Load Config Bundle…", 'Load presets from a bundle', sub {
  231. $self->load_configbundle;
  232. }, undef, 'lorry_add.png');
  233. $self->_append_menu_item($fileMenu, "&Export Config Bundle…", 'Export all presets to file', sub {
  234. $self->export_configbundle;
  235. }, undef, 'lorry_go.png');
  236. $fileMenu->AppendSeparator();
  237. my $repeat;
  238. $self->_append_menu_item($fileMenu, "Q&uick Slice…\tCtrl+U", 'Slice a file into a G-code', sub {
  239. wxTheApp->CallAfter(sub {
  240. $self->quick_slice;
  241. $repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
  242. });
  243. }, undef, 'cog_go.png');
  244. $self->_append_menu_item($fileMenu, "Quick Slice and Save &As…\tCtrl+Alt+U", 'Slice a file into a G-code, save as', sub {
  245. wxTheApp->CallAfter(sub {
  246. $self->quick_slice(save_as => 1);
  247. $repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
  248. });
  249. }, undef, 'cog_go.png');
  250. $repeat = $self->_append_menu_item($fileMenu, "&Repeat Last Quick Slice\tCtrl+Shift+U", 'Repeat last quick slice', sub {
  251. wxTheApp->CallAfter(sub {
  252. $self->quick_slice(reslice => 1);
  253. });
  254. }, undef, 'cog_go.png');
  255. $repeat->Enable(0);
  256. $fileMenu->AppendSeparator();
  257. $self->_append_menu_item($fileMenu, "Slice to SV&G…\tCtrl+G", 'Slice file to a multi-layer SVG', sub {
  258. $self->quick_slice(save_as => 1, export_svg => 1);
  259. }, undef, 'shape_handles.png');
  260. $self->{menu_item_reslice_now} = $self->_append_menu_item(
  261. $fileMenu, "(&Re)Slice Now\tCtrl+S", 'Start new slicing process',
  262. sub { $self->reslice_now; }, undef, 'shape_handles.png');
  263. $fileMenu->AppendSeparator();
  264. $self->_append_menu_item($fileMenu, "Repair STL file…", 'Automatically repair an STL file', sub {
  265. $self->repair_stl;
  266. }, undef, 'wrench.png');
  267. $fileMenu->AppendSeparator();
  268. # Cmd+, is standard on OS X - what about other operating systems?
  269. $self->_append_menu_item($fileMenu, "Preferences…\tCtrl+,", 'Application preferences', sub {
  270. Slic3r::GUI::Preferences->new($self)->ShowModal;
  271. }, wxID_PREFERENCES);
  272. $fileMenu->AppendSeparator();
  273. $self->_append_menu_item($fileMenu, "&Quit", 'Quit Slic3r', sub {
  274. $self->Close(0);
  275. }, wxID_EXIT);
  276. }
  277. # Plater menu
  278. unless ($self->{no_plater}) {
  279. my $plater = $self->{plater};
  280. $self->{plater_menu} = Wx::Menu->new;
  281. $self->_append_menu_item($self->{plater_menu}, "Export G-code...", 'Export current plate as G-code', sub {
  282. $plater->export_gcode;
  283. }, undef, 'cog_go.png');
  284. $self->_append_menu_item($self->{plater_menu}, "Export plate as STL...", 'Export current plate as STL', sub {
  285. $plater->export_stl;
  286. }, undef, 'brick_go.png');
  287. $self->_append_menu_item($self->{plater_menu}, "Export plate as AMF...", 'Export current plate as AMF', sub {
  288. $plater->export_amf;
  289. }, undef, 'brick_go.png');
  290. $self->_append_menu_item($self->{plater_menu}, "Export plate as 3MF...", 'Export current plate as 3MF', sub {
  291. $plater->export_3mf;
  292. }, undef, 'brick_go.png');
  293. $self->{object_menu} = $self->{plater}->object_menu;
  294. $self->on_plater_selection_changed(0);
  295. }
  296. # Window menu
  297. my $windowMenu = Wx::Menu->new;
  298. {
  299. my $tab_offset = 0;
  300. if (!$self->{no_plater}) {
  301. $self->_append_menu_item($windowMenu, "Select &Plater Tab\tCtrl+1", 'Show the plater', sub {
  302. $self->select_tab(0);
  303. }, undef, 'application_view_tile.png');
  304. $tab_offset += 1;
  305. }
  306. if (!$self->{no_controller}) {
  307. $self->_append_menu_item($windowMenu, "Select &Controller Tab\tCtrl+T", 'Show the printer controller', sub {
  308. $self->select_tab(1);
  309. }, undef, 'printer_empty.png');
  310. $tab_offset += 1;
  311. }
  312. if ($tab_offset > 0) {
  313. $windowMenu->AppendSeparator();
  314. }
  315. $self->_append_menu_item($windowMenu, "Select P&rint Settings Tab\tCtrl+2", 'Show the print settings', sub {
  316. $self->select_tab($tab_offset+0);
  317. }, undef, 'cog.png');
  318. $self->_append_menu_item($windowMenu, "Select &Filament Settings Tab\tCtrl+3", 'Show the filament settings', sub {
  319. $self->select_tab($tab_offset+1);
  320. }, undef, 'spool.png');
  321. $self->_append_menu_item($windowMenu, "Select Print&er Settings Tab\tCtrl+4", 'Show the printer settings', sub {
  322. $self->select_tab($tab_offset+2);
  323. }, undef, 'printer_empty.png');
  324. }
  325. # View menu
  326. if (!$self->{no_plater}) {
  327. $self->{viewMenu} = Wx::Menu->new;
  328. # \xA0 is a non-breaing space. It is entered here to spoil the automatic accelerators,
  329. # as the simple numeric accelerators spoil all numeric data entry.
  330. # The camera control accelerators are captured by 3DScene Perl module instead.
  331. my $accel = ($^O eq 'MSWin32') ? sub { $_[0] . "\t\xA0" . $_[1] } : sub { $_[0] };
  332. $self->_append_menu_item($self->{viewMenu}, $accel->('Iso', '0'), 'Iso View' , sub { $self->select_view('iso' ); });
  333. $self->_append_menu_item($self->{viewMenu}, $accel->('Top', '1'), 'Top View' , sub { $self->select_view('top' ); });
  334. $self->_append_menu_item($self->{viewMenu}, $accel->('Bottom', '2'), 'Bottom View' , sub { $self->select_view('bottom' ); });
  335. $self->_append_menu_item($self->{viewMenu}, $accel->('Front', '3'), 'Front View' , sub { $self->select_view('front' ); });
  336. $self->_append_menu_item($self->{viewMenu}, $accel->('Rear', '4'), 'Rear View' , sub { $self->select_view('rear' ); });
  337. $self->_append_menu_item($self->{viewMenu}, $accel->('Left', '5'), 'Left View' , sub { $self->select_view('left' ); });
  338. $self->_append_menu_item($self->{viewMenu}, $accel->('Right', '6'), 'Right View' , sub { $self->select_view('right' ); });
  339. }
  340. # Help menu
  341. my $helpMenu = Wx::Menu->new;
  342. {
  343. $self->_append_menu_item($helpMenu, "&Configuration $Slic3r::GUI::ConfigWizard::wizard…", "Run Configuration $Slic3r::GUI::ConfigWizard::wizard", sub {
  344. # Run the config wizard, offer the "reset user profile" checkbox.
  345. $self->config_wizard(0);
  346. });
  347. $helpMenu->AppendSeparator();
  348. $self->_append_menu_item($helpMenu, "Prusa 3D Drivers", 'Open the Prusa3D drivers download page in your browser', sub {
  349. Wx::LaunchDefaultBrowser('http://www.prusa3d.com/drivers/');
  350. });
  351. $self->_append_menu_item($helpMenu, "Prusa Edition Releases", 'Open the Prusa Edition releases page in your browser', sub {
  352. Wx::LaunchDefaultBrowser('http://github.com/prusa3d/slic3r/releases');
  353. });
  354. # my $versioncheck = $self->_append_menu_item($helpMenu, "Check for &Updates...", 'Check for new Slic3r versions', sub {
  355. # wxTheApp->check_version(1);
  356. # });
  357. # $versioncheck->Enable(wxTheApp->have_version_check);
  358. $self->_append_menu_item($helpMenu, "Slic3r &Website", 'Open the Slic3r website in your browser', sub {
  359. Wx::LaunchDefaultBrowser('http://slic3r.org/');
  360. });
  361. $self->_append_menu_item($helpMenu, "Slic3r &Manual", 'Open the Slic3r manual in your browser', sub {
  362. Wx::LaunchDefaultBrowser('http://manual.slic3r.org/');
  363. });
  364. $helpMenu->AppendSeparator();
  365. $self->_append_menu_item($helpMenu, "System Info", 'Show system information', sub {
  366. wxTheApp->system_info;
  367. });
  368. $self->_append_menu_item($helpMenu, "Report an Issue", 'Report an issue on the Slic3r Prusa Edition', sub {
  369. Wx::LaunchDefaultBrowser('http://github.com/prusa3d/slic3r/issues/new');
  370. });
  371. $self->_append_menu_item($helpMenu, "&About Slic3r", 'Show about dialog', sub {
  372. wxTheApp->about;
  373. });
  374. }
  375. # menubar
  376. # assign menubar to frame after appending items, otherwise special items
  377. # will not be handled correctly
  378. {
  379. my $menubar = Wx::MenuBar->new;
  380. $menubar->Append($fileMenu, "&File");
  381. $menubar->Append($self->{plater_menu}, "&Plater") if $self->{plater_menu};
  382. $menubar->Append($self->{object_menu}, "&Object") if $self->{object_menu};
  383. $menubar->Append($windowMenu, "&Window");
  384. $menubar->Append($self->{viewMenu}, "&View") if $self->{viewMenu};
  385. # Add an optional debug menu
  386. # (Select application language from the list of installed languages)
  387. # In production code, the add_debug_menu() call should do nothing.
  388. Slic3r::GUI::add_debug_menu($menubar, $self->{lang_ch_event});
  389. $menubar->Append($helpMenu, "&Help");
  390. $self->SetMenuBar($menubar);
  391. }
  392. }
  393. sub is_loaded {
  394. my ($self) = @_;
  395. return $self->{loaded};
  396. }
  397. # Selection of a 3D object changed on the platter.
  398. sub on_plater_selection_changed {
  399. my ($self, $have_selection) = @_;
  400. return if !defined $self->{object_menu};
  401. $self->{object_menu}->Enable($_->GetId, $have_selection)
  402. for $self->{object_menu}->GetMenuItems;
  403. }
  404. # To perform the "Quck Slice", "Quick Slice and Save As", "Repeat last Quick Slice" and "Slice to SVG".
  405. sub quick_slice {
  406. my ($self, %params) = @_;
  407. my $progress_dialog;
  408. eval {
  409. # validate configuration
  410. my $config = wxTheApp->{preset_bundle}->full_config();
  411. $config->validate;
  412. # select input file
  413. my $input_file;
  414. if (!$params{reslice}) {
  415. my $dialog = Wx::FileDialog->new($self, 'Choose a file to slice (STL/OBJ/AMF/3MF/PRUSA):',
  416. wxTheApp->{app_config}->get_last_dir, "",
  417. &Slic3r::GUI::MODEL_WILDCARD, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  418. if ($dialog->ShowModal != wxID_OK) {
  419. $dialog->Destroy;
  420. return;
  421. }
  422. $input_file = $dialog->GetPaths;
  423. $dialog->Destroy;
  424. $qs_last_input_file = $input_file unless $params{export_svg};
  425. } else {
  426. if (!defined $qs_last_input_file) {
  427. Wx::MessageDialog->new($self, "No previously sliced file.",
  428. 'Error', wxICON_ERROR | wxOK)->ShowModal();
  429. return;
  430. }
  431. if (! -e $qs_last_input_file) {
  432. Wx::MessageDialog->new($self, "Previously sliced file ($qs_last_input_file) not found.",
  433. 'File Not Found', wxICON_ERROR | wxOK)->ShowModal();
  434. return;
  435. }
  436. $input_file = $qs_last_input_file;
  437. }
  438. my $input_file_basename = basename($input_file);
  439. wxTheApp->{app_config}->update_skein_dir(dirname($input_file));
  440. my $print_center;
  441. {
  442. my $bed_shape = Slic3r::Polygon->new_scale(@{$config->bed_shape});
  443. $print_center = Slic3r::Pointf->new_unscale(@{$bed_shape->bounding_box->center});
  444. }
  445. my $sprint = Slic3r::Print::Simple->new(
  446. print_center => $print_center,
  447. status_cb => sub {
  448. my ($percent, $message) = @_;
  449. $progress_dialog->Update($percent, "$message…");
  450. },
  451. );
  452. # keep model around
  453. my $model = Slic3r::Model->read_from_file($input_file);
  454. $sprint->apply_config($config);
  455. $sprint->set_model($model);
  456. # Copy the names of active presets into the placeholder parser.
  457. wxTheApp->{preset_bundle}->export_selections_pp($sprint->placeholder_parser);
  458. # select output file
  459. my $output_file;
  460. if ($params{reslice}) {
  461. $output_file = $qs_last_output_file if defined $qs_last_output_file;
  462. } elsif ($params{save_as}) {
  463. # The following line may die if the output_filename_format template substitution fails.
  464. $output_file = $sprint->output_filepath;
  465. $output_file =~ s/\.[gG][cC][oO][dD][eE]$/.svg/ if $params{export_svg};
  466. my $dlg = Wx::FileDialog->new($self, 'Save ' . ($params{export_svg} ? 'SVG' : 'G-code') . ' file as:',
  467. wxTheApp->{app_config}->get_last_output_dir(dirname($output_file)),
  468. basename($output_file), $params{export_svg} ? &Slic3r::GUI::FILE_WILDCARDS->{svg} : &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  469. if ($dlg->ShowModal != wxID_OK) {
  470. $dlg->Destroy;
  471. return;
  472. }
  473. $output_file = $dlg->GetPath;
  474. $qs_last_output_file = $output_file unless $params{export_svg};
  475. wxTheApp->{app_config}->update_last_output_dir(dirname($output_file));
  476. $dlg->Destroy;
  477. }
  478. # show processbar dialog
  479. $progress_dialog = Wx::ProgressDialog->new('Slicing…', "Processing $input_file_basename…",
  480. 100, $self, 0);
  481. $progress_dialog->Pulse;
  482. {
  483. my @warnings = ();
  484. local $SIG{__WARN__} = sub { push @warnings, $_[0] };
  485. $sprint->output_file($output_file);
  486. if ($params{export_svg}) {
  487. $sprint->export_svg;
  488. } else {
  489. $sprint->export_gcode;
  490. }
  491. $sprint->status_cb(undef);
  492. Slic3r::GUI::warning_catcher($self)->($_) for @warnings;
  493. }
  494. $progress_dialog->Destroy;
  495. undef $progress_dialog;
  496. my $message = "$input_file_basename was successfully sliced.";
  497. wxTheApp->notify($message);
  498. Wx::MessageDialog->new($self, $message, 'Slicing Done!',
  499. wxOK | wxICON_INFORMATION)->ShowModal;
  500. };
  501. Slic3r::GUI::catch_error($self, sub { $progress_dialog->Destroy if $progress_dialog });
  502. }
  503. sub reslice_now {
  504. my ($self) = @_;
  505. $self->{plater}->reslice if $self->{plater};
  506. }
  507. sub repair_stl {
  508. my $self = shift;
  509. my $input_file;
  510. {
  511. my $dialog = Wx::FileDialog->new($self, 'Select the STL file to repair:',
  512. wxTheApp->{app_config}->get_last_dir, "",
  513. &Slic3r::GUI::FILE_WILDCARDS->{stl}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  514. if ($dialog->ShowModal != wxID_OK) {
  515. $dialog->Destroy;
  516. return;
  517. }
  518. $input_file = $dialog->GetPaths;
  519. $dialog->Destroy;
  520. }
  521. my $output_file = $input_file;
  522. {
  523. $output_file =~ s/\.[sS][tT][lL]$/_fixed.obj/;
  524. my $dlg = Wx::FileDialog->new($self, "Save OBJ file (less prone to coordinate errors than STL) as:", dirname($output_file),
  525. basename($output_file), &Slic3r::GUI::FILE_WILDCARDS->{obj}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  526. if ($dlg->ShowModal != wxID_OK) {
  527. $dlg->Destroy;
  528. return undef;
  529. }
  530. $output_file = $dlg->GetPath;
  531. $dlg->Destroy;
  532. }
  533. my $tmesh = Slic3r::TriangleMesh->new;
  534. $tmesh->ReadSTLFile($input_file);
  535. $tmesh->repair;
  536. $tmesh->WriteOBJFile($output_file);
  537. Slic3r::GUI::show_info($self, "Your file was repaired.", "Repair");
  538. }
  539. sub export_config {
  540. my $self = shift;
  541. # Generate a cummulative configuration for the selected print, filaments and printer.
  542. my $config = wxTheApp->{preset_bundle}->full_config();
  543. # Validate the cummulative configuration.
  544. eval { $config->validate; };
  545. Slic3r::GUI::catch_error($self) and return;
  546. # Ask user for the file name for the config file.
  547. my $dlg = Wx::FileDialog->new($self, 'Save configuration as:',
  548. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  549. $last_config ? basename($last_config) : "config.ini",
  550. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  551. my $file = ($dlg->ShowModal == wxID_OK) ? $dlg->GetPath : undef;
  552. $dlg->Destroy;
  553. if (defined $file) {
  554. wxTheApp->{app_config}->update_config_dir(dirname($file));
  555. $last_config = $file;
  556. $config->save($file);
  557. }
  558. }
  559. # Load a config file containing a Print, Filament & Printer preset.
  560. sub load_config_file {
  561. my ($self, $file) = @_;
  562. if (!$file) {
  563. return unless $self->check_unsaved_changes;
  564. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:',
  565. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  566. "config.ini",
  567. 'INI files (*.ini, *.gcode)|*.ini;*.INI;*.gcode;*.g', wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  568. return unless $dlg->ShowModal == wxID_OK;
  569. $file = $dlg->GetPaths;
  570. $dlg->Destroy;
  571. }
  572. eval { wxTheApp->{preset_bundle}->load_config_file($file); };
  573. # Dont proceed further if the config file cannot be loaded.
  574. return if Slic3r::GUI::catch_error($self);
  575. $_->load_current_preset for (values %{$self->{options_tabs}});
  576. wxTheApp->{app_config}->update_config_dir(dirname($file));
  577. $last_config = $file;
  578. }
  579. sub export_configbundle {
  580. my ($self) = @_;
  581. return unless $self->check_unsaved_changes;
  582. # validate current configuration in case it's dirty
  583. eval { wxTheApp->{preset_bundle}->full_config->validate; };
  584. Slic3r::GUI::catch_error($self) and return;
  585. # Ask user for a file name.
  586. my $dlg = Wx::FileDialog->new($self, 'Save presets bundle as:',
  587. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  588. "Slic3r_config_bundle.ini",
  589. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  590. my $file = ($dlg->ShowModal == wxID_OK) ? $dlg->GetPath : undef;
  591. $dlg->Destroy;
  592. if (defined $file) {
  593. # Export the config bundle.
  594. wxTheApp->{app_config}->update_config_dir(dirname($file));
  595. eval { wxTheApp->{preset_bundle}->export_configbundle($file); };
  596. Slic3r::GUI::catch_error($self) and return;
  597. }
  598. }
  599. # Loading a config bundle with an external file name used to be used
  600. # to auto-install a config bundle on a fresh user account,
  601. # but that behavior was not documented and likely buggy.
  602. sub load_configbundle {
  603. my ($self, $file, $reset_user_profile) = @_;
  604. return unless $self->check_unsaved_changes;
  605. if (!$file) {
  606. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:',
  607. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  608. "config.ini",
  609. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  610. return unless $dlg->ShowModal == wxID_OK;
  611. $file = $dlg->GetPaths;
  612. $dlg->Destroy;
  613. }
  614. wxTheApp->{app_config}->update_config_dir(dirname($file));
  615. my $presets_imported = 0;
  616. eval { $presets_imported = wxTheApp->{preset_bundle}->load_configbundle($file); };
  617. Slic3r::GUI::catch_error($self) and return;
  618. # Load the currently selected preset into the GUI, update the preset selection box.
  619. foreach my $tab (values %{$self->{options_tabs}}) {
  620. $tab->load_current_preset;
  621. }
  622. my $message = sprintf "%d presets successfully imported.", $presets_imported;
  623. Slic3r::GUI::show_info($self, $message);
  624. }
  625. # Load a provied DynamicConfig into the Print / Filament / Printer tabs, thus modifying the active preset.
  626. # Also update the platter with the new presets.
  627. sub load_config {
  628. my ($self, $config) = @_;
  629. $_->load_config($config) foreach values %{$self->{options_tabs}};
  630. $self->{plater}->on_config_change($config) if $self->{plater};
  631. }
  632. sub config_wizard {
  633. my ($self, $fresh_start) = @_;
  634. # Exit wizard if there are unsaved changes and the user cancels the action.
  635. return unless $self->check_unsaved_changes;
  636. # Enumerate the profiles bundled with the Slic3r installation under resources/profiles.
  637. my $directory = Slic3r::resources_dir() . "/profiles";
  638. my @profiles = ();
  639. if (opendir(DIR, Slic3r::encode_path($directory))) {
  640. while (my $file = readdir(DIR)) {
  641. if ($file =~ /\.ini$/) {
  642. $file =~ s/\.ini$//;
  643. push @profiles, Slic3r::decode_path($file);
  644. }
  645. }
  646. closedir(DIR);
  647. }
  648. # Open the wizard.
  649. if (my $result = Slic3r::GUI::ConfigWizard->new($self, \@profiles, $fresh_start)->run) {
  650. eval {
  651. if ($result->{reset_user_profile}) {
  652. wxTheApp->{preset_bundle}->reset(1);
  653. }
  654. if (defined $result->{config}) {
  655. # Load and save the settings into print, filament and printer presets.
  656. wxTheApp->{preset_bundle}->load_config('My Settings', $result->{config});
  657. } else {
  658. # Wizard returned a name of a preset bundle bundled with the installation. Unpack it.
  659. wxTheApp->{preset_bundle}->load_configbundle($directory . '/' . $result->{preset_name} . '.ini');
  660. }
  661. };
  662. Slic3r::GUI::catch_error($self) and return;
  663. # Load the currently selected preset into the GUI, update the preset selection box.
  664. foreach my $tab (values %{$self->{options_tabs}}) {
  665. $tab->load_current_preset;
  666. }
  667. }
  668. }
  669. # This is called when closing the application, when loading a config file or when starting the config wizard
  670. # to notify the user whether he is aware that some preset changes will be lost.
  671. sub check_unsaved_changes {
  672. my $self = shift;
  673. my @dirty = ();
  674. foreach my $tab (values %{$self->{options_tabs}}) {
  675. push @dirty, $tab->title if $tab->current_preset_is_dirty;
  676. }
  677. if (@dirty) {
  678. my $titles = join ', ', @dirty;
  679. my $confirm = Wx::MessageDialog->new($self, "You have unsaved changes ($titles). Discard changes and continue anyway?",
  680. 'Unsaved Presets', wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT);
  681. return $confirm->ShowModal == wxID_YES;
  682. }
  683. return 1;
  684. }
  685. sub select_tab {
  686. my ($self, $tab) = @_;
  687. $self->{tabpanel}->SetSelection($tab);
  688. }
  689. # Set a camera direction, zoom to all objects.
  690. sub select_view {
  691. my ($self, $direction) = @_;
  692. if (! $self->{no_plater}) {
  693. $self->{plater}->select_view($direction);
  694. }
  695. }
  696. sub _append_menu_item {
  697. my ($self, $menu, $string, $description, $cb, $id, $icon) = @_;
  698. $id //= &Wx::NewId();
  699. my $item = $menu->Append($id, $string, $description);
  700. $self->_set_menu_item_icon($item, $icon);
  701. EVT_MENU($self, $id, $cb);
  702. return $item;
  703. }
  704. sub _set_menu_item_icon {
  705. my ($self, $menuItem, $icon) = @_;
  706. # SetBitmap was not available on OS X before Wx 0.9927
  707. if ($icon && $menuItem->can('SetBitmap')) {
  708. $menuItem->SetBitmap(Wx::Bitmap->new(Slic3r::var($icon), wxBITMAP_TYPE_PNG));
  709. }
  710. }
  711. # Called after the Preferences dialog is closed and the program settings are saved.
  712. # Update the UI based on the current preferences.
  713. sub update_ui_from_settings {
  714. my ($self) = @_;
  715. $self->{menu_item_reslice_now}->Enable(! wxTheApp->{app_config}->get("background_processing"));
  716. $self->{plater}->update_ui_from_settings if ($self->{plater});
  717. for my $tab_name (qw(print filament printer)) {
  718. $self->{options_tabs}{$tab_name}->update_ui_from_settings;
  719. }
  720. }
  721. 1;