MainFrame.pm 30 KB

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