MainFrame.pm 29 KB

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