MainFrame.pm 28 KB

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