MainFrame.pm 31 KB

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