MainFrame.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package Slic3r::GUI::MainFrame;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use File::Basename qw(basename dirname);
  6. use List::Util qw(min);
  7. use Slic3r::Geometry qw(X Y Z);
  8. use Wx qw(:frame :bitmap :id :misc :notebook :panel :sizer :menu :dialog :filedialog
  9. :font :icon wxTheApp);
  10. use Wx::Event qw(EVT_CLOSE EVT_MENU);
  11. use base 'Wx::Frame';
  12. our $last_input_file;
  13. our $last_output_file;
  14. our $last_config;
  15. sub new {
  16. my ($class, %params) = @_;
  17. my $self = $class->SUPER::new(undef, -1, 'Slic3r', wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE);
  18. $self->SetIcon(Wx::Icon->new("$Slic3r::var/Slic3r_128px.png", wxBITMAP_TYPE_PNG) );
  19. # store input params
  20. $self->{mode} = $params{mode};
  21. $self->{mode} = 'expert' if $self->{mode} !~ /^(?:simple|expert)$/;
  22. $self->{no_plater} = $params{no_plater};
  23. $self->{loaded} = 0;
  24. # initialize tabpanel and menubar
  25. $self->_init_tabpanel;
  26. $self->_init_menubar;
  27. # initialize status bar
  28. $self->{statusbar} = Slic3r::GUI::ProgressStatusBar->new($self, -1);
  29. $self->{statusbar}->SetStatusText("Version $Slic3r::VERSION - Remember to check for updates at http://slic3r.org/");
  30. $self->SetStatusBar($self->{statusbar});
  31. $self->{loaded} = 1;
  32. # declare events
  33. EVT_CLOSE($self, sub {
  34. my (undef, $event) = @_;
  35. if ($event->CanVeto && !$self->check_unsaved_changes) {
  36. $event->Veto;
  37. return;
  38. }
  39. $event->Skip;
  40. });
  41. # initialize layout
  42. {
  43. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  44. $sizer->Add($self->{tabpanel}, 1, wxEXPAND);
  45. $sizer->SetSizeHints($self);
  46. $self->SetSizer($sizer);
  47. $self->Fit;
  48. $self->SetMinSize([760, 490]);
  49. $self->SetSize($self->GetMinSize);
  50. $self->Show;
  51. $self->Layout;
  52. }
  53. return $self;
  54. }
  55. sub _init_tabpanel {
  56. my ($self) = @_;
  57. $self->{tabpanel} = my $panel = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
  58. if (!$self->{no_plater}) {
  59. $panel->AddPage($self->{plater} = Slic3r::GUI::Plater->new($panel), "Plater");
  60. }
  61. $self->{options_tabs} = {};
  62. my $simple_config;
  63. if ($self->{mode} eq 'simple') {
  64. $simple_config = Slic3r::Config->load("$Slic3r::GUI::datadir/simple.ini")
  65. if -e "$Slic3r::GUI::datadir/simple.ini";
  66. }
  67. my $class_prefix = $self->{mode} eq 'simple' ? "Slic3r::GUI::SimpleTab::" : "Slic3r::GUI::Tab::";
  68. for my $tab_name (qw(print filament printer)) {
  69. my $tab;
  70. $tab = $self->{options_tabs}{$tab_name} = ($class_prefix . ucfirst $tab_name)->new($panel);
  71. $tab->on_value_change(sub {
  72. my ($opt_key, $value) = @_;
  73. my $config = $tab->config;
  74. if ($self->{plater}) {
  75. $self->{plater}->on_config_change($config); # propagate config change events to the plater
  76. $self->{plater}->on_extruders_change($value) if $opt_key eq 'extruders_count';
  77. }
  78. if ($self->{loaded}) { # don't save while loading for the first time
  79. if ($self->{mode} eq 'simple') {
  80. # save config
  81. $self->config->save("$Slic3r::GUI::datadir/simple.ini");
  82. # save a copy into each preset section
  83. # so that user gets the config when switching to expert mode
  84. $config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $tab->name, 'Simple Mode');
  85. $Slic3r::GUI::Settings->{presets}{$tab->name} = 'Simple Mode.ini';
  86. wxTheApp->save_settings;
  87. }
  88. $self->config->save($Slic3r::GUI::autosave) if $Slic3r::GUI::autosave;
  89. }
  90. });
  91. $tab->on_presets_changed(sub {
  92. if ($self->{plater}) {
  93. $self->{plater}->update_presets($tab_name, @_);
  94. $self->{plater}->on_config_change($tab->config);
  95. }
  96. });
  97. $tab->load_presets;
  98. $panel->AddPage($tab, $tab->title);
  99. $tab->load_config($simple_config) if $simple_config;
  100. }
  101. if ($self->{plater}) {
  102. $self->{plater}->on_select_preset(sub {
  103. my ($group, $preset) = @_;
  104. $self->{options_tabs}{$group}->select_preset($preset);
  105. });
  106. # load initial config
  107. $self->{plater}->on_config_change($self->config);
  108. }
  109. }
  110. sub _init_menubar {
  111. my ($self) = @_;
  112. # File menu
  113. my $fileMenu = Wx::Menu->new;
  114. {
  115. $self->_append_menu_item($fileMenu, "&Load Config…\tCtrl+L", 'Load exported configuration file', sub {
  116. $self->load_config_file;
  117. });
  118. $self->_append_menu_item($fileMenu, "&Export Config…\tCtrl+E", 'Export current configuration to file', sub {
  119. $self->export_config;
  120. });
  121. $self->_append_menu_item($fileMenu, "&Load Config Bundle…", 'Load presets from a bundle', sub {
  122. $self->load_configbundle;
  123. });
  124. $self->_append_menu_item($fileMenu, "&Export Config Bundle…", 'Export all presets to file', sub {
  125. $self->export_configbundle;
  126. });
  127. $fileMenu->AppendSeparator();
  128. my $repeat;
  129. $self->_append_menu_item($fileMenu, "Q&uick Slice…\tCtrl+U", 'Slice file', sub {
  130. $self->quick_slice;
  131. $repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
  132. });
  133. $self->_append_menu_item($fileMenu, "Quick Slice and Save &As…\tCtrl+Alt+U", 'Slice file and save as', sub {
  134. $self->quick_slice(save_as => 1);
  135. $repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
  136. });
  137. $repeat = $self->_append_menu_item($fileMenu, "&Repeat Last Quick Slice\tCtrl+Shift+U", 'Repeat last quick slice', sub {
  138. $self->quick_slice(reslice => 1);
  139. });
  140. $repeat->Enable(0);
  141. $fileMenu->AppendSeparator();
  142. $self->_append_menu_item($fileMenu, "Slice to SV&G…\tCtrl+G", 'Slice file to SVG', sub {
  143. $self->quick_slice(save_as => 1, export_svg => 1);
  144. });
  145. $fileMenu->AppendSeparator();
  146. $self->_append_menu_item($fileMenu, "Repair STL file…", 'Automatically repair an STL file', sub {
  147. $self->repair_stl;
  148. });
  149. $fileMenu->AppendSeparator();
  150. $self->_append_menu_item($fileMenu, "Preferences…", 'Application preferences', sub {
  151. Slic3r::GUI::Preferences->new($self)->ShowModal;
  152. }, wxID_PREFERENCES);
  153. $fileMenu->AppendSeparator();
  154. $self->_append_menu_item($fileMenu, "&Quit", 'Quit Slic3r', sub {
  155. $self->Close(0);
  156. }, wxID_EXIT);
  157. }
  158. # Plater menu
  159. unless ($self->{no_plater}) {
  160. my $plater = $self->{plater};
  161. $self->{plater_menu} = Wx::Menu->new;
  162. $self->_append_menu_item($self->{plater_menu}, "Export G-code...", 'Export current plate as G-code', sub {
  163. $plater->export_gcode;
  164. });
  165. $self->_append_menu_item($self->{plater_menu}, "Export STL...", 'Export current plate as STL', sub {
  166. $plater->export_stl;
  167. });
  168. $self->_append_menu_item($self->{plater_menu}, "Export AMF...", 'Export current plate as AMF', sub {
  169. $plater->export_amf;
  170. });
  171. $self->{plater_menu}->AppendSeparator();
  172. $self->_append_menu_item($self->{plater_menu}, "Toolpaths preview…", 'Open a viewer with toolpaths preview', sub {
  173. $plater->toolpaths_preview;
  174. });
  175. $self->{object_menu} = $self->{plater}->object_menu;
  176. $self->on_plater_selection_changed(0);
  177. }
  178. # Window menu
  179. my $windowMenu = Wx::Menu->new;
  180. {
  181. my $tab_count = $self->{no_plater} ? 3 : 4;
  182. $self->_append_menu_item($windowMenu, "Select &Plater Tab\tCtrl+1", 'Show the plater', sub {
  183. $self->select_tab(0);
  184. }) unless $self->{no_plater};
  185. $self->_append_menu_item($windowMenu, "Select P&rint Settings Tab\tCtrl+2", 'Show the print settings', sub {
  186. $self->select_tab($tab_count-3);
  187. });
  188. $self->_append_menu_item($windowMenu, "Select &Filament Settings Tab\tCtrl+3", 'Show the filament settings', sub {
  189. $self->select_tab($tab_count-2);
  190. });
  191. $self->_append_menu_item($windowMenu, "Select Print&er Settings Tab\tCtrl+4", 'Show the printer settings', sub {
  192. $self->select_tab($tab_count-1);
  193. });
  194. }
  195. # Help menu
  196. my $helpMenu = Wx::Menu->new;
  197. {
  198. $self->_append_menu_item($helpMenu, "&Configuration $Slic3r::GUI::ConfigWizard::wizard…", "Run Configuration $Slic3r::GUI::ConfigWizard::wizard", sub {
  199. $self->config_wizard;
  200. });
  201. $helpMenu->AppendSeparator();
  202. $self->_append_menu_item($helpMenu, "Slic3r &Website", 'Open the Slic3r website in your browser', sub {
  203. Wx::LaunchDefaultBrowser('http://slic3r.org/');
  204. });
  205. my $versioncheck = $self->_append_menu_item($helpMenu, "Check for &Updates...", 'Check for new Slic3r versions', sub {
  206. wxTheApp->check_version(manual => 1);
  207. });
  208. $versioncheck->Enable(wxTheApp->have_version_check);
  209. $self->_append_menu_item($helpMenu, "Slic3r &Manual", 'Open the Slic3r manual in your browser', sub {
  210. Wx::LaunchDefaultBrowser('http://manual.slic3r.org/');
  211. });
  212. $helpMenu->AppendSeparator();
  213. $self->_append_menu_item($helpMenu, "&About Slic3r", 'Show about dialog', sub {
  214. wxTheApp->about;
  215. });
  216. }
  217. # menubar
  218. # assign menubar to frame after appending items, otherwise special items
  219. # will not be handled correctly
  220. {
  221. my $menubar = Wx::MenuBar->new;
  222. $menubar->Append($fileMenu, "&File");
  223. $menubar->Append($self->{plater_menu}, "&Plater") if $self->{plater_menu};
  224. $menubar->Append($self->{object_menu}, "&Object") if $self->{object_menu};
  225. $menubar->Append($windowMenu, "&Window");
  226. $menubar->Append($helpMenu, "&Help");
  227. $self->SetMenuBar($menubar);
  228. }
  229. }
  230. sub is_loaded {
  231. my ($self) = @_;
  232. return $self->{loaded};
  233. }
  234. sub on_plater_selection_changed {
  235. my ($self, $have_selection) = @_;
  236. return if !defined $self->{object_menu};
  237. $self->{object_menu}->Enable($_->GetId, $have_selection)
  238. for $self->{object_menu}->GetMenuItems;
  239. }
  240. sub quick_slice {
  241. my $self = shift;
  242. my %params = @_;
  243. my $progress_dialog;
  244. eval {
  245. # validate configuration
  246. my $config = $self->config;
  247. $config->validate;
  248. # select input file
  249. my $input_file;
  250. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory} || $Slic3r::GUI::Settings->{recent}{config_directory} || '';
  251. if (!$params{reslice}) {
  252. my $dialog = Wx::FileDialog->new($self, 'Choose a file to slice (STL/OBJ/AMF):', $dir, "", &Slic3r::GUI::MODEL_WILDCARD, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  253. if ($dialog->ShowModal != wxID_OK) {
  254. $dialog->Destroy;
  255. return;
  256. }
  257. $input_file = $dialog->GetPaths;
  258. $dialog->Destroy;
  259. $last_input_file = $input_file unless $params{export_svg};
  260. } else {
  261. if (!defined $last_input_file) {
  262. Wx::MessageDialog->new($self, "No previously sliced file.",
  263. 'Error', wxICON_ERROR | wxOK)->ShowModal();
  264. return;
  265. }
  266. if (! -e $last_input_file) {
  267. Wx::MessageDialog->new($self, "Previously sliced file ($last_input_file) not found.",
  268. 'File Not Found', wxICON_ERROR | wxOK)->ShowModal();
  269. return;
  270. }
  271. $input_file = $last_input_file;
  272. }
  273. my $input_file_basename = basename($input_file);
  274. $Slic3r::GUI::Settings->{recent}{skein_directory} = dirname($input_file);
  275. wxTheApp->save_settings;
  276. my $sprint = Slic3r::Print::Simple->new(
  277. status_cb => sub {
  278. my ($percent, $message) = @_;
  279. return if &Wx::wxVERSION_STRING !~ / 2\.(8\.|9\.[2-9])/;
  280. $progress_dialog->Update($percent, "$message…");
  281. },
  282. );
  283. # keep model around
  284. my $model = Slic3r::Model->read_from_file($input_file);
  285. $sprint->apply_config($config);
  286. $sprint->set_model($model);
  287. {
  288. my $extra = $self->extra_variables;
  289. $sprint->placeholder_parser->set($_, $extra->{$_}) for keys %$extra;
  290. }
  291. # select output file
  292. my $output_file;
  293. if ($params{reslice}) {
  294. $output_file = $last_output_file if defined $last_output_file;
  295. } elsif ($params{save_as}) {
  296. $output_file = $sprint->expanded_output_filepath;
  297. $output_file =~ s/\.gcode$/.svg/i if $params{export_svg};
  298. my $dlg = Wx::FileDialog->new($self, 'Save ' . ($params{export_svg} ? 'SVG' : 'G-code') . ' file as:',
  299. wxTheApp->output_path(dirname($output_file)),
  300. basename($output_file), $params{export_svg} ? &Slic3r::GUI::FILE_WILDCARDS->{svg} : &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE);
  301. if ($dlg->ShowModal != wxID_OK) {
  302. $dlg->Destroy;
  303. return;
  304. }
  305. $output_file = $dlg->GetPath;
  306. $last_output_file = $output_file unless $params{export_svg};
  307. $Slic3r::GUI::Settings->{_}{last_output_path} = dirname($output_file);
  308. wxTheApp->save_settings;
  309. $dlg->Destroy;
  310. }
  311. # show processbar dialog
  312. $progress_dialog = Wx::ProgressDialog->new('Slicing…', "Processing $input_file_basename…",
  313. 100, $self, 0);
  314. $progress_dialog->Pulse;
  315. {
  316. my @warnings = ();
  317. local $SIG{__WARN__} = sub { push @warnings, $_[0] };
  318. $sprint->output_file($output_file);
  319. if ($params{export_svg}) {
  320. $sprint->export_svg;
  321. } else {
  322. $sprint->export_gcode;
  323. }
  324. $sprint->status_cb(undef);
  325. Slic3r::GUI::warning_catcher($self)->($_) for @warnings;
  326. }
  327. $progress_dialog->Destroy;
  328. undef $progress_dialog;
  329. my $message = "$input_file_basename was successfully sliced.";
  330. wxTheApp->notify($message);
  331. Wx::MessageDialog->new($self, $message, 'Slicing Done!',
  332. wxOK | wxICON_INFORMATION)->ShowModal;
  333. };
  334. Slic3r::GUI::catch_error($self, sub { $progress_dialog->Destroy if $progress_dialog });
  335. }
  336. sub repair_stl {
  337. my $self = shift;
  338. my $input_file;
  339. {
  340. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory} || $Slic3r::GUI::Settings->{recent}{config_directory} || '';
  341. my $dialog = Wx::FileDialog->new($self, 'Select the STL file to repair:', $dir, "", &Slic3r::GUI::FILE_WILDCARDS->{stl}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  342. if ($dialog->ShowModal != wxID_OK) {
  343. $dialog->Destroy;
  344. return;
  345. }
  346. $input_file = $dialog->GetPaths;
  347. $dialog->Destroy;
  348. }
  349. my $output_file = $input_file;
  350. {
  351. $output_file =~ s/\.stl$/_fixed.obj/i;
  352. my $dlg = Wx::FileDialog->new($self, "Save OBJ file (less prone to coordinate errors than STL) as:", dirname($output_file),
  353. basename($output_file), &Slic3r::GUI::FILE_WILDCARDS->{obj}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  354. if ($dlg->ShowModal != wxID_OK) {
  355. $dlg->Destroy;
  356. return undef;
  357. }
  358. $output_file = $dlg->GetPath;
  359. $dlg->Destroy;
  360. }
  361. my $tmesh = Slic3r::TriangleMesh->new;
  362. $tmesh->ReadSTLFile(Slic3r::encode_path($input_file));
  363. $tmesh->repair;
  364. $tmesh->WriteOBJFile(Slic3r::encode_path($output_file));
  365. Slic3r::GUI::show_info($self, "Your file was repaired.", "Repair");
  366. }
  367. sub extra_variables {
  368. my $self = shift;
  369. my %extra_variables = ();
  370. if ($self->{mode} eq 'expert') {
  371. $extra_variables{"${_}_preset"} = $self->{options_tabs}{$_}->current_preset->{name}
  372. for qw(print filament printer);
  373. }
  374. return { %extra_variables };
  375. }
  376. sub export_config {
  377. my $self = shift;
  378. my $config = $self->config;
  379. eval {
  380. # validate configuration
  381. $config->validate;
  382. };
  383. Slic3r::GUI::catch_error($self) and return;
  384. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  385. my $filename = $last_config ? basename($last_config) : "config.ini";
  386. my $dlg = Wx::FileDialog->new($self, 'Save configuration as:', $dir, $filename,
  387. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  388. if ($dlg->ShowModal == wxID_OK) {
  389. my $file = $dlg->GetPath;
  390. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  391. wxTheApp->save_settings;
  392. $last_config = $file;
  393. $config->save($file);
  394. }
  395. $dlg->Destroy;
  396. }
  397. sub load_config_file {
  398. my $self = shift;
  399. my ($file) = @_;
  400. if (!$file) {
  401. return unless $self->check_unsaved_changes;
  402. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  403. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
  404. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  405. return unless $dlg->ShowModal == wxID_OK;
  406. ($file) = $dlg->GetPaths;
  407. $dlg->Destroy;
  408. }
  409. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  410. wxTheApp->save_settings;
  411. $last_config = $file;
  412. for my $tab (values %{$self->{options_tabs}}) {
  413. $tab->load_config_file($file);
  414. }
  415. }
  416. sub export_configbundle {
  417. my $self = shift;
  418. eval {
  419. # validate current configuration in case it's dirty
  420. $self->config->validate;
  421. };
  422. Slic3r::GUI::catch_error($self) and return;
  423. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  424. my $filename = "Slic3r_config_bundle.ini";
  425. my $dlg = Wx::FileDialog->new($self, 'Save presets bundle as:', $dir, $filename,
  426. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  427. if ($dlg->ShowModal == wxID_OK) {
  428. my $file = $dlg->GetPath;
  429. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  430. wxTheApp->save_settings;
  431. # leave default category empty to prevent the bundle from being parsed as a normal config file
  432. my $ini = { _ => {} };
  433. $ini->{settings}{$_} = $Slic3r::GUI::Settings->{_}{$_} for qw(autocenter mode);
  434. $ini->{presets} = $Slic3r::GUI::Settings->{presets};
  435. if (-e "$Slic3r::GUI::datadir/simple.ini") {
  436. my $config = Slic3r::Config->load("$Slic3r::GUI::datadir/simple.ini");
  437. $ini->{simple} = $config->as_ini->{_};
  438. }
  439. foreach my $section (qw(print filament printer)) {
  440. my %presets = wxTheApp->presets($section);
  441. foreach my $preset_name (keys %presets) {
  442. my $config = Slic3r::Config->load($presets{$preset_name});
  443. $ini->{"$section:$preset_name"} = $config->as_ini->{_};
  444. }
  445. }
  446. Slic3r::Config->write_ini($file, $ini);
  447. }
  448. $dlg->Destroy;
  449. }
  450. sub load_configbundle {
  451. my $self = shift;
  452. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  453. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
  454. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  455. return unless $dlg->ShowModal == wxID_OK;
  456. my ($file) = $dlg->GetPaths;
  457. $dlg->Destroy;
  458. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  459. wxTheApp->save_settings;
  460. # load .ini file
  461. my $ini = Slic3r::Config->read_ini($file);
  462. if ($ini->{settings}) {
  463. $Slic3r::GUI::Settings->{_}{$_} = $ini->{settings}{$_} for keys %{$ini->{settings}};
  464. wxTheApp->save_settings;
  465. }
  466. if ($ini->{presets}) {
  467. $Slic3r::GUI::Settings->{presets} = $ini->{presets};
  468. wxTheApp->save_settings;
  469. }
  470. if ($ini->{simple}) {
  471. my $config = Slic3r::Config->load_ini_hash($ini->{simple});
  472. $config->save("$Slic3r::GUI::datadir/simple.ini");
  473. if ($self->{mode} eq 'simple') {
  474. foreach my $tab (values %{$self->{options_tabs}}) {
  475. $tab->load_config($config) for values %{$self->{options_tabs}};
  476. }
  477. }
  478. }
  479. my $imported = 0;
  480. foreach my $ini_category (sort keys %$ini) {
  481. next unless $ini_category =~ /^(print|filament|printer):(.+)$/;
  482. my ($section, $preset_name) = ($1, $2);
  483. my $config = Slic3r::Config->load_ini_hash($ini->{$ini_category});
  484. $config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $section, $preset_name);
  485. $imported++;
  486. }
  487. if ($self->{mode} eq 'expert') {
  488. foreach my $tab (values %{$self->{options_tabs}}) {
  489. $tab->load_presets;
  490. }
  491. }
  492. my $message = sprintf "%d presets successfully imported.", $imported;
  493. if ($self->{mode} eq 'simple' && $Slic3r::GUI::Settings->{_}{mode} eq 'expert') {
  494. Slic3r::GUI::show_info($self, "$message You need to restart Slic3r to make the changes effective.");
  495. } else {
  496. Slic3r::GUI::show_info($self, $message);
  497. }
  498. }
  499. sub load_config {
  500. my $self = shift;
  501. my ($config) = @_;
  502. foreach my $tab (values %{$self->{options_tabs}}) {
  503. $tab->load_config($config);
  504. }
  505. }
  506. sub config_wizard {
  507. my $self = shift;
  508. return unless $self->check_unsaved_changes;
  509. if (my $config = Slic3r::GUI::ConfigWizard->new($self)->run) {
  510. if ($self->{mode} eq 'expert') {
  511. for my $tab (values %{$self->{options_tabs}}) {
  512. $tab->select_default_preset;
  513. }
  514. } else {
  515. # TODO: select default settings in simple mode
  516. }
  517. $self->load_config($config);
  518. if ($self->{mode} eq 'expert') {
  519. for my $tab (values %{$self->{options_tabs}}) {
  520. $tab->save_preset('My Settings');
  521. }
  522. }
  523. }
  524. }
  525. =head2 config
  526. This method collects all config values from the tabs and merges them into a single config object.
  527. =cut
  528. sub config {
  529. my $self = shift;
  530. return Slic3r::Config->new_from_defaults
  531. if !exists $self->{options_tabs}{print}
  532. || !exists $self->{options_tabs}{filament}
  533. || !exists $self->{options_tabs}{printer};
  534. # retrieve filament presets and build a single config object for them
  535. my $filament_config;
  536. if (!$self->{plater} || $self->{plater}->filament_presets == 1 || $self->{mode} eq 'simple') {
  537. $filament_config = $self->{options_tabs}{filament}->config;
  538. } else {
  539. # TODO: handle dirty presets.
  540. # perhaps plater shouldn't expose dirty presets at all in multi-extruder environments.
  541. my $i = -1;
  542. foreach my $preset_idx ($self->{plater}->filament_presets) {
  543. $i++;
  544. my $preset = $self->{options_tabs}{filament}->get_preset($preset_idx);
  545. my $config = $self->{options_tabs}{filament}->get_preset_config($preset);
  546. if (!$filament_config) {
  547. $filament_config = $config->clone;
  548. next;
  549. }
  550. foreach my $opt_key (@{$config->get_keys}) {
  551. my $value = $filament_config->get($opt_key);
  552. next unless ref $value eq 'ARRAY';
  553. $value->[$i] = $config->get($opt_key)->[0];
  554. $filament_config->set($opt_key, $value);
  555. }
  556. }
  557. }
  558. my $config = Slic3r::Config->merge(
  559. Slic3r::Config->new_from_defaults,
  560. $self->{options_tabs}{print}->config,
  561. $self->{options_tabs}{printer}->config,
  562. $filament_config,
  563. );
  564. if ($self->{mode} eq 'simple') {
  565. # set some sensible defaults
  566. $config->set('first_layer_height', $config->nozzle_diameter->[0]);
  567. $config->set('avoid_crossing_perimeters', 1);
  568. $config->set('infill_every_layers', 10);
  569. } else {
  570. my $extruders_count = $self->{options_tabs}{printer}{extruders_count};
  571. $config->set("${_}_extruder", min($config->get("${_}_extruder"), $extruders_count))
  572. for qw(perimeter infill support_material support_material_interface);
  573. }
  574. return $config;
  575. }
  576. sub check_unsaved_changes {
  577. my $self = shift;
  578. my @dirty = map $_->title, grep $_->is_dirty, values %{$self->{options_tabs}};
  579. if (@dirty) {
  580. my $titles = join ', ', @dirty;
  581. my $confirm = Wx::MessageDialog->new($self, "You have unsaved changes ($titles). Discard changes and continue anyway?",
  582. 'Unsaved Presets', wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT);
  583. return ($confirm->ShowModal == wxID_YES);
  584. }
  585. return 1;
  586. }
  587. sub select_tab {
  588. my ($self, $tab) = @_;
  589. $self->{tabpanel}->ChangeSelection($tab);
  590. }
  591. sub _append_menu_item {
  592. my ($self, $menu, $string, $description, $cb, $id) = @_;
  593. $id //= &Wx::NewId();
  594. my $item = $menu->Append($id, $string, $description);
  595. EVT_MENU($self, $id, $cb);
  596. return $item;
  597. }
  598. 1;