MainFrame.pm 28 KB

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