MainFrame.pm 29 KB

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