MainFrame.pm 31 KB

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