MainFrame.pm 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 $qs_last_input_file;
  13. our $qs_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. # Cmd+, is standard on OS X - what about other operating systems?
  175. $self->_append_menu_item($fileMenu, "Preferences…\tCtrl+,", 'Application preferences', sub {
  176. Slic3r::GUI::Preferences->new($self)->ShowModal;
  177. }, wxID_PREFERENCES);
  178. $fileMenu->AppendSeparator();
  179. $self->_append_menu_item($fileMenu, "&Quit", 'Quit Slic3r', sub {
  180. $self->Close(0);
  181. }, wxID_EXIT);
  182. }
  183. # Plater menu
  184. unless ($self->{no_plater}) {
  185. my $plater = $self->{plater};
  186. $self->{plater_menu} = Wx::Menu->new;
  187. $self->_append_menu_item($self->{plater_menu}, "Export G-code...", 'Export current plate as G-code', sub {
  188. $plater->export_gcode;
  189. }, undef, 'cog_go.png');
  190. $self->_append_menu_item($self->{plater_menu}, "Export plate as STL...", 'Export current plate as STL', sub {
  191. $plater->export_stl;
  192. }, undef, 'brick_go.png');
  193. $self->_append_menu_item($self->{plater_menu}, "Export plate as AMF...", 'Export current plate as AMF', sub {
  194. $plater->export_amf;
  195. }, undef, 'brick_go.png');
  196. $self->{object_menu} = $self->{plater}->object_menu;
  197. $self->on_plater_selection_changed(0);
  198. }
  199. # Window menu
  200. my $windowMenu = Wx::Menu->new;
  201. {
  202. my $tab_offset = 0;
  203. if (!$self->{no_plater}) {
  204. $self->_append_menu_item($windowMenu, "Select &Plater Tab\tCtrl+1", 'Show the plater', sub {
  205. $self->select_tab(0);
  206. }, undef, 'application_view_tile.png');
  207. $self->_append_menu_item($windowMenu, "Select &Controller Tab\tCtrl+T", 'Show the printer controller', sub {
  208. $self->select_tab(1);
  209. }, undef, 'printer_empty.png');
  210. $windowMenu->AppendSeparator();
  211. $tab_offset += 2;
  212. }
  213. $self->_append_menu_item($windowMenu, "Select P&rint Settings Tab\tCtrl+2", 'Show the print settings', sub {
  214. $self->select_tab($tab_offset+0);
  215. }, undef, 'cog.png');
  216. $self->_append_menu_item($windowMenu, "Select &Filament Settings Tab\tCtrl+3", 'Show the filament settings', sub {
  217. $self->select_tab($tab_offset+1);
  218. }, undef, 'spool.png');
  219. $self->_append_menu_item($windowMenu, "Select Print&er Settings Tab\tCtrl+4", 'Show the printer settings', sub {
  220. $self->select_tab($tab_offset+2);
  221. }, undef, 'printer_empty.png');
  222. }
  223. # Help menu
  224. my $helpMenu = Wx::Menu->new;
  225. {
  226. $self->_append_menu_item($helpMenu, "&Configuration $Slic3r::GUI::ConfigWizard::wizard…", "Run Configuration $Slic3r::GUI::ConfigWizard::wizard", sub {
  227. $self->config_wizard;
  228. });
  229. $helpMenu->AppendSeparator();
  230. $self->_append_menu_item($helpMenu, "Slic3r &Website", 'Open the Slic3r website in your browser', sub {
  231. Wx::LaunchDefaultBrowser('http://slic3r.org/');
  232. });
  233. my $versioncheck = $self->_append_menu_item($helpMenu, "Check for &Updates...", 'Check for new Slic3r versions', sub {
  234. wxTheApp->check_version(1);
  235. });
  236. $versioncheck->Enable(wxTheApp->have_version_check);
  237. $self->_append_menu_item($helpMenu, "Slic3r &Manual", 'Open the Slic3r manual in your browser', sub {
  238. Wx::LaunchDefaultBrowser('http://manual.slic3r.org/');
  239. });
  240. $helpMenu->AppendSeparator();
  241. $self->_append_menu_item($helpMenu, "&About Slic3r", 'Show about dialog', sub {
  242. wxTheApp->about;
  243. });
  244. }
  245. # menubar
  246. # assign menubar to frame after appending items, otherwise special items
  247. # will not be handled correctly
  248. {
  249. my $menubar = Wx::MenuBar->new;
  250. $menubar->Append($fileMenu, "&File");
  251. $menubar->Append($self->{plater_menu}, "&Plater") if $self->{plater_menu};
  252. $menubar->Append($self->{object_menu}, "&Object") if $self->{object_menu};
  253. $menubar->Append($windowMenu, "&Window");
  254. $menubar->Append($helpMenu, "&Help");
  255. $self->SetMenuBar($menubar);
  256. }
  257. }
  258. sub is_loaded {
  259. my ($self) = @_;
  260. return $self->{loaded};
  261. }
  262. sub on_plater_selection_changed {
  263. my ($self, $have_selection) = @_;
  264. return if !defined $self->{object_menu};
  265. $self->{object_menu}->Enable($_->GetId, $have_selection)
  266. for $self->{object_menu}->GetMenuItems;
  267. }
  268. sub quick_slice {
  269. my $self = shift;
  270. my %params = @_;
  271. my $progress_dialog;
  272. eval {
  273. # validate configuration
  274. my $config = $self->config;
  275. $config->validate;
  276. # select input file
  277. my $input_file;
  278. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory} || $Slic3r::GUI::Settings->{recent}{config_directory} || '';
  279. if (!$params{reslice}) {
  280. 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);
  281. if ($dialog->ShowModal != wxID_OK) {
  282. $dialog->Destroy;
  283. return;
  284. }
  285. $input_file = Slic3r::decode_path($dialog->GetPaths);
  286. $dialog->Destroy;
  287. $qs_last_input_file = $input_file unless $params{export_svg};
  288. } else {
  289. if (!defined $qs_last_input_file) {
  290. Wx::MessageDialog->new($self, "No previously sliced file.",
  291. 'Error', wxICON_ERROR | wxOK)->ShowModal();
  292. return;
  293. }
  294. if (! -e $qs_last_input_file) {
  295. Wx::MessageDialog->new($self, "Previously sliced file ($qs_last_input_file) not found.",
  296. 'File Not Found', wxICON_ERROR | wxOK)->ShowModal();
  297. return;
  298. }
  299. $input_file = $qs_last_input_file;
  300. }
  301. my $input_file_basename = basename($input_file);
  302. $Slic3r::GUI::Settings->{recent}{skein_directory} = dirname($input_file);
  303. wxTheApp->save_settings;
  304. my $print_center;
  305. {
  306. my $bed_shape = Slic3r::Polygon->new_scale(@{$config->bed_shape});
  307. $print_center = Slic3r::Pointf->new_unscale(@{$bed_shape->bounding_box->center});
  308. }
  309. my $sprint = Slic3r::Print::Simple->new(
  310. print_center => $print_center,
  311. status_cb => sub {
  312. my ($percent, $message) = @_;
  313. return if &Wx::wxVERSION_STRING !~ / 2\.(8\.|9\.[2-9])/;
  314. $progress_dialog->Update($percent, "$message…");
  315. },
  316. );
  317. # keep model around
  318. my $model = Slic3r::Model->read_from_file($input_file);
  319. $sprint->apply_config($config);
  320. $sprint->set_model($model);
  321. {
  322. my $extra = $self->extra_variables;
  323. $sprint->placeholder_parser->set($_, $extra->{$_}) for keys %$extra;
  324. }
  325. # select output file
  326. my $output_file;
  327. if ($params{reslice}) {
  328. $output_file = $qs_last_output_file if defined $qs_last_output_file;
  329. } elsif ($params{save_as}) {
  330. $output_file = $sprint->expanded_output_filepath;
  331. $output_file =~ s/\.gcode$/.svg/i if $params{export_svg};
  332. my $dlg = Wx::FileDialog->new($self, 'Save ' . ($params{export_svg} ? 'SVG' : 'G-code') . ' file as:',
  333. wxTheApp->output_path(dirname($output_file)),
  334. basename($output_file), $params{export_svg} ? &Slic3r::GUI::FILE_WILDCARDS->{svg} : &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  335. if ($dlg->ShowModal != wxID_OK) {
  336. $dlg->Destroy;
  337. return;
  338. }
  339. $output_file = Slic3r::decode_path($dlg->GetPath);
  340. $qs_last_output_file = $output_file unless $params{export_svg};
  341. $Slic3r::GUI::Settings->{_}{last_output_path} = dirname($output_file);
  342. wxTheApp->save_settings;
  343. $dlg->Destroy;
  344. }
  345. # show processbar dialog
  346. $progress_dialog = Wx::ProgressDialog->new('Slicing…', "Processing $input_file_basename…",
  347. 100, $self, 0);
  348. $progress_dialog->Pulse;
  349. {
  350. my @warnings = ();
  351. local $SIG{__WARN__} = sub { push @warnings, $_[0] };
  352. $sprint->output_file($output_file);
  353. if ($params{export_svg}) {
  354. $sprint->export_svg;
  355. } else {
  356. $sprint->export_gcode;
  357. }
  358. $sprint->status_cb(undef);
  359. Slic3r::GUI::warning_catcher($self)->($_) for @warnings;
  360. }
  361. $progress_dialog->Destroy;
  362. undef $progress_dialog;
  363. my $message = "$input_file_basename was successfully sliced.";
  364. wxTheApp->notify($message);
  365. Wx::MessageDialog->new($self, $message, 'Slicing Done!',
  366. wxOK | wxICON_INFORMATION)->ShowModal;
  367. };
  368. Slic3r::GUI::catch_error($self, sub { $progress_dialog->Destroy if $progress_dialog });
  369. }
  370. sub repair_stl {
  371. my $self = shift;
  372. my $input_file;
  373. {
  374. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory} || $Slic3r::GUI::Settings->{recent}{config_directory} || '';
  375. my $dialog = Wx::FileDialog->new($self, 'Select the STL file to repair:', $dir, "", &Slic3r::GUI::FILE_WILDCARDS->{stl}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  376. if ($dialog->ShowModal != wxID_OK) {
  377. $dialog->Destroy;
  378. return;
  379. }
  380. $input_file = Slic3r::decode_path($dialog->GetPaths);
  381. $dialog->Destroy;
  382. }
  383. my $output_file = $input_file;
  384. {
  385. $output_file =~ s/\.stl$/_fixed.obj/i;
  386. my $dlg = Wx::FileDialog->new($self, "Save OBJ file (less prone to coordinate errors than STL) as:", dirname($output_file),
  387. basename($output_file), &Slic3r::GUI::FILE_WILDCARDS->{obj}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  388. if ($dlg->ShowModal != wxID_OK) {
  389. $dlg->Destroy;
  390. return undef;
  391. }
  392. $output_file = Slic3r::decode_path($dlg->GetPath);
  393. $dlg->Destroy;
  394. }
  395. my $tmesh = Slic3r::TriangleMesh->new;
  396. $tmesh->ReadSTLFile(Slic3r::encode_path($input_file));
  397. $tmesh->repair;
  398. $tmesh->WriteOBJFile(Slic3r::encode_path($output_file));
  399. Slic3r::GUI::show_info($self, "Your file was repaired.", "Repair");
  400. }
  401. sub extra_variables {
  402. my $self = shift;
  403. my %extra_variables = ();
  404. if ($self->{mode} eq 'expert') {
  405. $extra_variables{"${_}_preset"} = $self->{options_tabs}{$_}->get_current_preset->name
  406. for qw(print filament printer);
  407. }
  408. return { %extra_variables };
  409. }
  410. sub export_config {
  411. my $self = shift;
  412. my $config = $self->config;
  413. eval {
  414. # validate configuration
  415. $config->validate;
  416. };
  417. Slic3r::GUI::catch_error($self) and return;
  418. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  419. my $filename = $last_config ? basename($last_config) : "config.ini";
  420. my $dlg = Wx::FileDialog->new($self, 'Save configuration as:', $dir, $filename,
  421. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  422. if ($dlg->ShowModal == wxID_OK) {
  423. my $file = Slic3r::decode_path($dlg->GetPath);
  424. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  425. wxTheApp->save_settings;
  426. $last_config = $file;
  427. $config->save($file);
  428. }
  429. $dlg->Destroy;
  430. }
  431. sub load_config_file {
  432. my $self = shift;
  433. my ($file) = @_;
  434. if (!$file) {
  435. return unless $self->check_unsaved_changes;
  436. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  437. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
  438. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  439. return unless $dlg->ShowModal == wxID_OK;
  440. $file = Slic3r::decode_path($dlg->GetPaths);
  441. $dlg->Destroy;
  442. }
  443. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  444. wxTheApp->save_settings;
  445. $last_config = $file;
  446. for my $tab (values %{$self->{options_tabs}}) {
  447. $tab->load_config_file($file);
  448. }
  449. }
  450. sub export_configbundle {
  451. my $self = shift;
  452. eval {
  453. # validate current configuration in case it's dirty
  454. $self->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 = "Slic3r_config_bundle.ini";
  459. my $dlg = Wx::FileDialog->new($self, 'Save presets bundle as:', $dir, $filename,
  460. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  461. if ($dlg->ShowModal == wxID_OK) {
  462. my $file = Slic3r::decode_path($dlg->GetPath);
  463. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  464. wxTheApp->save_settings;
  465. # leave default category empty to prevent the bundle from being parsed as a normal config file
  466. my $ini = { _ => {} };
  467. $ini->{settings}{$_} = $Slic3r::GUI::Settings->{_}{$_} for qw(autocenter mode);
  468. $ini->{presets} = $Slic3r::GUI::Settings->{presets};
  469. if (-e "$Slic3r::GUI::datadir/simple.ini") {
  470. my $config = Slic3r::Config->load("$Slic3r::GUI::datadir/simple.ini");
  471. $ini->{simple} = $config->as_ini->{_};
  472. }
  473. foreach my $section (qw(print filament printer)) {
  474. my %presets = wxTheApp->presets($section);
  475. foreach my $preset_name (keys %presets) {
  476. my $config = Slic3r::Config->load($presets{$preset_name});
  477. $ini->{"$section:$preset_name"} = $config->as_ini->{_};
  478. }
  479. }
  480. Slic3r::Config->write_ini($file, $ini);
  481. }
  482. $dlg->Destroy;
  483. }
  484. sub load_configbundle {
  485. my $self = shift;
  486. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  487. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
  488. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  489. return unless $dlg->ShowModal == wxID_OK;
  490. my $file = Slic3r::decode_path($dlg->GetPaths);
  491. $dlg->Destroy;
  492. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  493. wxTheApp->save_settings;
  494. # load .ini file
  495. my $ini = Slic3r::Config->read_ini($file);
  496. if ($ini->{settings}) {
  497. $Slic3r::GUI::Settings->{_}{$_} = $ini->{settings}{$_} for keys %{$ini->{settings}};
  498. wxTheApp->save_settings;
  499. }
  500. if ($ini->{presets}) {
  501. $Slic3r::GUI::Settings->{presets} = $ini->{presets};
  502. wxTheApp->save_settings;
  503. }
  504. if ($ini->{simple}) {
  505. my $config = Slic3r::Config->load_ini_hash($ini->{simple});
  506. $config->save("$Slic3r::GUI::datadir/simple.ini");
  507. if ($self->{mode} eq 'simple') {
  508. foreach my $tab (values %{$self->{options_tabs}}) {
  509. $tab->load_config($config) for values %{$self->{options_tabs}};
  510. }
  511. }
  512. }
  513. my $imported = 0;
  514. foreach my $ini_category (sort keys %$ini) {
  515. next unless $ini_category =~ /^(print|filament|printer):(.+)$/;
  516. my ($section, $preset_name) = ($1, $2);
  517. my $config = Slic3r::Config->load_ini_hash($ini->{$ini_category});
  518. $config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $section, $preset_name);
  519. $imported++;
  520. }
  521. if ($self->{mode} eq 'expert') {
  522. foreach my $tab (values %{$self->{options_tabs}}) {
  523. $tab->load_presets;
  524. }
  525. }
  526. my $message = sprintf "%d presets successfully imported.", $imported;
  527. if ($self->{mode} eq 'simple' && $Slic3r::GUI::Settings->{_}{mode} eq 'expert') {
  528. Slic3r::GUI::show_info($self, "$message You need to restart Slic3r to make the changes effective.");
  529. } else {
  530. Slic3r::GUI::show_info($self, $message);
  531. }
  532. }
  533. sub load_config {
  534. my $self = shift;
  535. my ($config) = @_;
  536. foreach my $tab (values %{$self->{options_tabs}}) {
  537. $tab->load_config($config);
  538. }
  539. if ($self->{plater}) {
  540. $self->{plater}->on_config_change($config);
  541. }
  542. }
  543. sub config_wizard {
  544. my $self = shift;
  545. return unless $self->check_unsaved_changes;
  546. if (my $config = Slic3r::GUI::ConfigWizard->new($self)->run) {
  547. if ($self->{mode} eq 'expert') {
  548. for my $tab (values %{$self->{options_tabs}}) {
  549. $tab->select_default_preset;
  550. }
  551. } else {
  552. # TODO: select default settings in simple mode
  553. }
  554. $self->load_config($config);
  555. if ($self->{mode} eq 'expert') {
  556. for my $tab (values %{$self->{options_tabs}}) {
  557. $tab->save_preset('My Settings');
  558. }
  559. }
  560. }
  561. }
  562. =head2 config
  563. This method collects all config values from the tabs and merges them into a single config object.
  564. =cut
  565. sub config {
  566. my $self = shift;
  567. return Slic3r::Config->new_from_defaults
  568. if !exists $self->{options_tabs}{print}
  569. || !exists $self->{options_tabs}{filament}
  570. || !exists $self->{options_tabs}{printer};
  571. # retrieve filament presets and build a single config object for them
  572. my $filament_config;
  573. if (!$self->{plater} || $self->{plater}->filament_presets == 1 || $self->{mode} eq 'simple') {
  574. $filament_config = $self->{options_tabs}{filament}->config;
  575. } else {
  576. my $i = -1;
  577. foreach my $preset_idx ($self->{plater}->filament_presets) {
  578. $i++;
  579. my $config;
  580. if ($preset_idx == $self->{options_tabs}{filament}->current_preset) {
  581. # the selected preset for this extruder is the one in the tab
  582. # use the tab's config instead of the preset in case it is dirty
  583. # perhaps plater shouldn't expose dirty presets at all in multi-extruder environments.
  584. $config = $self->{options_tabs}{filament}->config;
  585. } else {
  586. my $preset = $self->{options_tabs}{filament}->get_preset($preset_idx);
  587. $config = $self->{options_tabs}{filament}->get_preset_config($preset);
  588. }
  589. if (!$filament_config) {
  590. $filament_config = $config->clone;
  591. next;
  592. }
  593. foreach my $opt_key (@{$config->get_keys}) {
  594. my $value = $filament_config->get($opt_key);
  595. next unless ref $value eq 'ARRAY';
  596. $value->[$i] = $config->get($opt_key)->[0];
  597. $filament_config->set($opt_key, $value);
  598. }
  599. }
  600. }
  601. my $config = Slic3r::Config->merge(
  602. Slic3r::Config->new_from_defaults,
  603. $self->{options_tabs}{print}->config,
  604. $self->{options_tabs}{printer}->config,
  605. $filament_config,
  606. );
  607. if ($self->{mode} eq 'simple') {
  608. # set some sensible defaults
  609. $config->set('first_layer_height', $config->nozzle_diameter->[0]);
  610. $config->set('avoid_crossing_perimeters', 1);
  611. $config->set('infill_every_layers', 10);
  612. } else {
  613. my $extruders_count = $self->{options_tabs}{printer}{extruders_count};
  614. $config->set("${_}_extruder", min($config->get("${_}_extruder"), $extruders_count))
  615. for qw(perimeter infill solid_infill support_material support_material_interface);
  616. }
  617. return $config;
  618. }
  619. sub filament_preset_names {
  620. my ($self) = @_;
  621. if ($self->{mode} eq 'simple') {
  622. return '';
  623. }
  624. return map $self->{options_tabs}{filament}->get_preset($_)->name,
  625. $self->{plater}->filament_presets;
  626. }
  627. sub check_unsaved_changes {
  628. my $self = shift;
  629. my @dirty = ();
  630. foreach my $tab (values %{$self->{options_tabs}}) {
  631. push @dirty, $tab->title if $tab->is_dirty;
  632. }
  633. if (@dirty) {
  634. my $titles = join ', ', @dirty;
  635. my $confirm = Wx::MessageDialog->new($self, "You have unsaved changes ($titles). Discard changes and continue anyway?",
  636. 'Unsaved Presets', wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT);
  637. return ($confirm->ShowModal == wxID_YES);
  638. }
  639. return 1;
  640. }
  641. sub select_tab {
  642. my ($self, $tab) = @_;
  643. $self->{tabpanel}->ChangeSelection($tab);
  644. }
  645. sub _append_menu_item {
  646. my ($self, $menu, $string, $description, $cb, $id, $icon) = @_;
  647. $id //= &Wx::NewId();
  648. my $item = $menu->Append($id, $string, $description);
  649. $self->_set_menu_item_icon($item, $icon);
  650. EVT_MENU($self, $id, $cb);
  651. return $item;
  652. }
  653. sub _set_menu_item_icon {
  654. my ($self, $menuItem, $icon) = @_;
  655. # SetBitmap was not available on OS X before Wx 0.9927
  656. if ($icon && $menuItem->can('SetBitmap')) {
  657. $menuItem->SetBitmap(Wx::Bitmap->new("$Slic3r::var/$icon", wxBITMAP_TYPE_PNG));
  658. }
  659. }
  660. 1;