MainFrame.pm 30 KB

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