MainFrame.pm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 FindBin;
  8. use List::Util qw(min first);
  9. use Slic3r::Geometry qw(X Y);
  10. use Wx qw(:frame :bitmap :id :misc :notebook :panel :sizer :menu :dialog :filedialog :dirdialog
  11. :font :icon wxTheApp);
  12. use Wx::Event qw(EVT_CLOSE EVT_COMMAND EVT_MENU EVT_NOTEBOOK_PAGE_CHANGED);
  13. use base 'Wx::Frame';
  14. use Wx::Locale gettext => 'L';
  15. our $qs_last_input_file;
  16. our $qs_last_output_file;
  17. our $last_config;
  18. our $appController;
  19. # Events to be sent from a C++ Tab implementation:
  20. # 1) To inform about a change of a configuration value.
  21. our $VALUE_CHANGE_EVENT = Wx::NewEventType;
  22. # 2) To inform about a preset selection change or a "modified" status change.
  23. our $PRESETS_CHANGED_EVENT = Wx::NewEventType;
  24. # 3) To update the status bar with the progress information.
  25. our $PROGRESS_BAR_EVENT = Wx::NewEventType;
  26. # 4) To display an error dialog box from a thread on the UI thread.
  27. our $ERROR_EVENT = Wx::NewEventType;
  28. # 5) To inform about a change of object selection
  29. our $OBJECT_SELECTION_CHANGED_EVENT = Wx::NewEventType;
  30. # 6) To inform about a change of object settings
  31. our $OBJECT_SETTINGS_CHANGED_EVENT = Wx::NewEventType;
  32. # 7) To inform about a remove of object
  33. our $OBJECT_REMOVE_EVENT = Wx::NewEventType;
  34. # 8) To inform about a update of the scene
  35. our $UPDATE_SCENE_EVENT = Wx::NewEventType;
  36. sub new {
  37. my ($class, %params) = @_;
  38. my $self = $class->SUPER::new(undef, -1, $Slic3r::FORK_NAME . ' - ' . $Slic3r::VERSION, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE);
  39. Slic3r::GUI::set_main_frame($self);
  40. $appController = Slic3r::AppController->new();
  41. if ($^O eq 'MSWin32') {
  42. # Load the icon either from the exe, or from the ico file.
  43. my $iconfile = Slic3r::decode_path($FindBin::Bin) . '\slic3r.exe';
  44. $iconfile = Slic3r::var("Slic3r.ico") unless -f $iconfile;
  45. $self->SetIcon(Wx::Icon->new($iconfile, wxBITMAP_TYPE_ICO));
  46. } else {
  47. $self->SetIcon(Wx::Icon->new(Slic3r::var("Slic3r_128px.png"), wxBITMAP_TYPE_PNG));
  48. }
  49. # store input params
  50. $self->{no_plater} = $params{no_plater};
  51. $self->{loaded} = 0;
  52. $self->{lang_ch_event} = $params{lang_ch_event};
  53. $self->{preferences_event} = $params{preferences_event};
  54. # initialize tabpanel and menubar
  55. $self->_init_tabpanel;
  56. $self->_init_menubar;
  57. # set default tooltip timer in msec
  58. # SetAutoPop supposedly accepts long integers but some bug doesn't allow for larger values
  59. # (SetAutoPop is not available on GTK.)
  60. eval { Wx::ToolTip::SetAutoPop(32767) };
  61. # initialize status bar
  62. $self->{statusbar} = Slic3r::GUI::ProgressStatusBar->new();
  63. $self->{statusbar}->Embed;
  64. $self->{statusbar}->SetStatusText(L("Version ").$Slic3r::VERSION.L(" - Remember to check for updates at http://github.com/prusa3d/slic3r/releases"));
  65. # Make the global status bar and its progress indicator available in C++
  66. Slic3r::GUI::set_progress_status_bar($self->{statusbar});
  67. $appController->set_global_progress_indicator($self->{statusbar});
  68. $appController->set_model($self->{plater}->{model});
  69. $appController->set_print($self->{plater}->{print});
  70. $self->{plater}->{appController} = $appController;
  71. $self->{loaded} = 1;
  72. # initialize layout
  73. {
  74. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  75. $sizer->Add($self->{tabpanel}, 1, wxEXPAND);
  76. $sizer->SetSizeHints($self);
  77. $self->SetSizer($sizer);
  78. $self->Fit;
  79. $self->SetMinSize([760, 490]);
  80. $self->SetSize($self->GetMinSize);
  81. wxTheApp->restore_window_pos($self, "main_frame");
  82. $self->Show;
  83. $self->Layout;
  84. }
  85. # declare events
  86. EVT_CLOSE($self, sub {
  87. my (undef, $event) = @_;
  88. if ($event->CanVeto && !Slic3r::GUI::check_unsaved_changes) {
  89. $event->Veto;
  90. return;
  91. }
  92. # save window size
  93. wxTheApp->save_window_pos($self, "main_frame");
  94. # Save the slic3r.ini. Usually the ini file is saved from "on idle" callback,
  95. # but in rare cases it may not have been called yet.
  96. wxTheApp->{app_config}->save;
  97. $self->{statusbar}->ResetCancelCallback();
  98. $self->{plater}->{print} = undef if($self->{plater});
  99. Slic3r::GUI::_3DScene::remove_all_canvases();
  100. Slic3r::GUI::deregister_on_request_update_callback();
  101. # propagate event
  102. $event->Skip;
  103. });
  104. $self->update_ui_from_settings;
  105. Slic3r::GUI::update_mode();
  106. return $self;
  107. }
  108. sub _init_tabpanel {
  109. my ($self) = @_;
  110. $self->{tabpanel} = my $panel = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
  111. Slic3r::GUI::set_tab_panel($panel);
  112. EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{tabpanel}, sub {
  113. my $panel = $self->{tabpanel}->GetCurrentPage;
  114. $panel->OnActivate if $panel->can('OnActivate');
  115. for my $tab_name (qw(print filament printer)) {
  116. Slic3r::GUI::get_preset_tab("$tab_name")->OnActivate if ("$tab_name" eq $panel->GetName);
  117. }
  118. });
  119. if (!$self->{no_plater}) {
  120. $panel->AddPage($self->{plater} = Slic3r::GUI::Plater->new($panel,
  121. event_object_selection_changed => $OBJECT_SELECTION_CHANGED_EVENT,
  122. event_object_settings_changed => $OBJECT_SETTINGS_CHANGED_EVENT,
  123. event_remove_object => $OBJECT_REMOVE_EVENT,
  124. event_update_scene => $UPDATE_SCENE_EVENT,
  125. ), L("Plater"));
  126. }
  127. #TODO this is an example of a Slic3r XS interface call to add a new preset editor page to the main view.
  128. # The following event is emited by the C++ Tab implementation on config value change.
  129. EVT_COMMAND($self, -1, $VALUE_CHANGE_EVENT, sub {
  130. my ($self, $event) = @_;
  131. my $str = $event->GetString;
  132. my ($opt_key, $name) = ($str =~ /(.*) (.*)/);
  133. #print "VALUE_CHANGE_EVENT: ", $opt_key, "\n";
  134. my $tab = Slic3r::GUI::get_preset_tab($name);
  135. my $config = $tab->get_config;
  136. if ($self->{plater}) {
  137. $self->{plater}->on_config_change($config); # propagate config change events to the plater
  138. if ($opt_key eq 'extruders_count'){
  139. my $value = $event->GetInt();
  140. $self->{plater}->on_extruders_change($value);
  141. }
  142. if ($opt_key eq 'printer_technology'){
  143. my $value = $event->GetInt();# 0 ~ "ptFFF"; 1 ~ "ptSLA"
  144. $self->{plater}->show_preset_comboboxes($value);
  145. }
  146. }
  147. # don't save while loading for the first time
  148. $self->config->save($Slic3r::GUI::autosave) if $Slic3r::GUI::autosave && $self->{loaded};
  149. });
  150. # The following event is emited by the C++ Tab implementation on preset selection,
  151. # or when the preset's "modified" status changes.
  152. EVT_COMMAND($self, -1, $PRESETS_CHANGED_EVENT, sub {
  153. my ($self, $event) = @_;
  154. my $tab_name = $event->GetString;
  155. my $tab = Slic3r::GUI::get_preset_tab($tab_name);
  156. if ($self->{plater}) {
  157. # Update preset combo boxes (Print settings, Filament, Material, Printer) from their respective tabs.
  158. my $presets = $tab->get_presets;
  159. if (defined $presets){
  160. my $reload_dependent_tabs = $tab->get_dependent_tabs;
  161. $self->{plater}->update_presets($tab_name, $reload_dependent_tabs, $presets);
  162. $self->{plater}->{"selected_item_$tab_name"} = $tab->get_selected_preset_item;
  163. if ($tab_name eq 'printer') {
  164. # Printer selected at the Printer tab, update "compatible" marks at the print and filament selectors.
  165. for my $tab_name_other (qw(print filament sla_material)) {
  166. # If the printer tells us that the print or filament preset has been switched or invalidated,
  167. # refresh the print or filament tab page. Otherwise just refresh the combo box.
  168. my $update_action = ($reload_dependent_tabs && (first { $_ eq $tab_name_other } (@{$reload_dependent_tabs})))
  169. ? 'load_current_preset' : 'update_tab_ui';
  170. $self->{options_tabs}{$tab_name_other}->$update_action;
  171. }
  172. }
  173. $self->{plater}->on_config_change($tab->get_config);
  174. }
  175. }
  176. });
  177. # The following event is emited by the C++ Tab implementation on object selection change.
  178. EVT_COMMAND($self, -1, $OBJECT_SELECTION_CHANGED_EVENT, sub {
  179. my ($self, $event) = @_;
  180. my $obj_idx = $event->GetId;
  181. my $child = $event->GetInt == 1 ? 1 : undef;
  182. $self->{plater}->select_object($obj_idx < 0 ? undef: $obj_idx, $child);
  183. $self->{plater}->item_changed_selection($obj_idx);
  184. });
  185. # The following event is emited by the C++ GUI implementation on object settings change.
  186. EVT_COMMAND($self, -1, $OBJECT_SETTINGS_CHANGED_EVENT, sub {
  187. my ($self, $event) = @_;
  188. my $line = $event->GetString;
  189. my ($obj_idx, $parts_changed, $part_settings_changed) = split('',$line);
  190. $self->{plater}->changed_object_settings($obj_idx, $parts_changed, $part_settings_changed);
  191. });
  192. # The following event is emited by the C++ GUI implementation on object remove.
  193. EVT_COMMAND($self, -1, $OBJECT_REMOVE_EVENT, sub {
  194. my ($self, $event) = @_;
  195. $self->{plater}->remove();
  196. });
  197. # The following event is emited by the C++ GUI implementation on extruder change for object.
  198. EVT_COMMAND($self, -1, $UPDATE_SCENE_EVENT, sub {
  199. my ($self, $event) = @_;
  200. $self->{plater}->update();
  201. });
  202. Slic3r::GUI::create_preset_tabs($VALUE_CHANGE_EVENT, $PRESETS_CHANGED_EVENT);
  203. $self->{options_tabs} = {};
  204. for my $tab_name (qw(print filament sla_material printer)) {
  205. $self->{options_tabs}{$tab_name} = Slic3r::GUI::get_preset_tab("$tab_name");
  206. }
  207. # Update progress bar with an event sent by the slicing core.
  208. EVT_COMMAND($self, -1, $PROGRESS_BAR_EVENT, sub {
  209. my ($self, $event) = @_;
  210. if (defined $self->{progress_dialog}) {
  211. # If a progress dialog is open, update it.
  212. $self->{progress_dialog}->Update($event->GetInt, $event->GetString . "…");
  213. } else {
  214. # Otherwise update the main window status bar.
  215. $self->{statusbar}->SetProgress($event->GetInt);
  216. $self->{statusbar}->SetStatusText($event->GetString . "…");
  217. }
  218. });
  219. EVT_COMMAND($self, -1, $ERROR_EVENT, sub {
  220. my ($self, $event) = @_;
  221. Slic3r::GUI::show_error($self, $event->GetString);
  222. });
  223. if ($self->{plater}) {
  224. $self->{plater}->on_select_preset(sub {
  225. my ($group, $name) = @_;
  226. $self->{options_tabs}{$group}->select_preset($name);
  227. });
  228. # load initial config
  229. my $full_config = wxTheApp->{preset_bundle}->full_config;
  230. $self->{plater}->on_config_change($full_config);
  231. # Show a correct number of filament fields.
  232. if (defined $full_config->nozzle_diameter){ # nozzle_diameter is undefined when SLA printer is selected
  233. $self->{plater}->on_extruders_change(int(@{$full_config->nozzle_diameter}));
  234. }
  235. # Show correct preset comboboxes according to the printer_technology
  236. $self->{plater}->show_preset_comboboxes(($full_config->printer_technology eq "FFF") ? 0 : 1);
  237. }
  238. }
  239. sub _init_menubar {
  240. my ($self) = @_;
  241. # File menu
  242. my $fileMenu = Wx::Menu->new;
  243. {
  244. wxTheApp->append_menu_item($fileMenu, L("Open STL/OBJ/AMF/3MF…\tCtrl+O"), L('Open a model'), sub {
  245. $self->{plater}->add if $self->{plater};
  246. }, undef, undef); #'brick_add.png');
  247. $self->_append_menu_item($fileMenu, L("&Load Config…\tCtrl+L"), L('Load exported configuration file'), sub {
  248. $self->load_config_file;
  249. }, undef, 'plugin_add.png');
  250. $self->_append_menu_item($fileMenu, L("&Export Config…\tCtrl+E"), L('Export current configuration to file'), sub {
  251. $self->export_config;
  252. }, undef, 'plugin_go.png');
  253. $self->_append_menu_item($fileMenu, L("&Load Config Bundle…"), L('Load presets from a bundle'), sub {
  254. $self->load_configbundle;
  255. }, undef, 'lorry_add.png');
  256. $self->_append_menu_item($fileMenu, L("&Export Config Bundle…"), L('Export all presets to file'), sub {
  257. $self->export_configbundle;
  258. }, undef, 'lorry_go.png');
  259. $fileMenu->AppendSeparator();
  260. $self->_append_menu_item($fileMenu, L("Slice to PNG…"), L('Slice file to a set of PNG files'), sub {
  261. $self->slice_to_png;
  262. }, undef, 'shape_handles.png');
  263. $self->{menu_item_reslice_now} = $self->_append_menu_item(
  264. $fileMenu, L("(&Re)Slice Now\tCtrl+S"), L('Start new slicing process'),
  265. sub { $self->reslice_now; }, undef, 'shape_handles.png');
  266. $fileMenu->AppendSeparator();
  267. $self->_append_menu_item($fileMenu, L("Repair STL file…"), L('Automatically repair an STL file'), sub {
  268. $self->repair_stl;
  269. }, undef, 'wrench.png');
  270. $fileMenu->AppendSeparator();
  271. $self->_append_menu_item($fileMenu, L("&Quit"), L('Quit Slic3r'), sub {
  272. $self->Close(0);
  273. }, wxID_EXIT);
  274. }
  275. # Plater menu
  276. unless ($self->{no_plater}) {
  277. my $plater = $self->{plater};
  278. $self->{plater_menu} = Wx::Menu->new;
  279. $self->_append_menu_item($self->{plater_menu}, L("Export G-code..."), L('Export current plate as G-code'), sub {
  280. $plater->export_gcode;
  281. }, undef, 'cog_go.png');
  282. $self->_append_menu_item($self->{plater_menu}, L("Export plate as STL..."), L('Export current plate as STL'), sub {
  283. $plater->export_stl;
  284. }, undef, 'brick_go.png');
  285. $self->_append_menu_item($self->{plater_menu}, L("Export plate as AMF..."), L('Export current plate as AMF'), sub {
  286. $plater->export_amf;
  287. }, undef, 'brick_go.png');
  288. $self->_append_menu_item($self->{plater_menu}, L("Export plate as 3MF..."), L('Export current plate as 3MF'), sub {
  289. $plater->export_3mf;
  290. }, undef, 'brick_go.png');
  291. $self->{object_menu} = $self->{plater}->object_menu;
  292. $self->on_plater_selection_changed(0);
  293. }
  294. # Window menu
  295. my $windowMenu = Wx::Menu->new;
  296. {
  297. my $tab_offset = 0;
  298. if (!$self->{no_plater}) {
  299. $self->_append_menu_item($windowMenu, L("Select &Plater Tab\tCtrl+1"), L('Show the plater'), sub {
  300. $self->select_tab(0);
  301. }, undef, 'application_view_tile.png');
  302. $tab_offset += 1;
  303. }
  304. if (!$self->{no_controller}) {
  305. $self->_append_menu_item($windowMenu, L("Select &Controller Tab\tCtrl+T"), L('Show the printer controller'), sub {
  306. $self->select_tab(1);
  307. }, undef, 'printer_empty.png');
  308. $tab_offset += 1;
  309. }
  310. if ($tab_offset > 0) {
  311. $windowMenu->AppendSeparator();
  312. }
  313. $self->_append_menu_item($windowMenu, L("Select P&rint Settings Tab\tCtrl+2"), L('Show the print settings'), sub {
  314. $self->select_tab($tab_offset+0);
  315. }, undef, 'cog.png');
  316. $self->_append_menu_item($windowMenu, L("Select &Filament Settings Tab\tCtrl+3"), L('Show the filament settings'), sub {
  317. $self->select_tab($tab_offset+1);
  318. }, undef, 'spool.png');
  319. $self->_append_menu_item($windowMenu, L("Select Print&er Settings Tab\tCtrl+4"), L('Show the printer settings'), sub {
  320. $self->select_tab($tab_offset+2);
  321. }, undef, 'printer_empty.png');
  322. }
  323. # View menu
  324. if (!$self->{no_plater}) {
  325. $self->{viewMenu} = Wx::Menu->new;
  326. # \xA0 is a non-breaing space. It is entered here to spoil the automatic accelerators,
  327. # as the simple numeric accelerators spoil all numeric data entry.
  328. # The camera control accelerators are captured by 3DScene Perl module instead.
  329. my $accel = ($^O eq 'MSWin32') ? sub { $_[0] . "\t\xA0" . $_[1] } : sub { $_[0] };
  330. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Iso'), '0'), L('Iso View') , sub { $self->select_view('iso' ); });
  331. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Top'), '1'), L('Top View') , sub { $self->select_view('top' ); });
  332. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Bottom'), '2'), L('Bottom View') , sub { $self->select_view('bottom' ); });
  333. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Front'), '3'), L('Front View') , sub { $self->select_view('front' ); });
  334. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Rear'), '4'), L('Rear View') , sub { $self->select_view('rear' ); });
  335. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Left'), '5'), L('Left View') , sub { $self->select_view('left' ); });
  336. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Right'), '6'), L('Right View') , sub { $self->select_view('right' ); });
  337. }
  338. # Help menu
  339. my $helpMenu = Wx::Menu->new;
  340. {
  341. $self->_append_menu_item($helpMenu, L("Prusa 3D Drivers"), L('Open the Prusa3D drivers download page in your browser'), sub {
  342. Wx::LaunchDefaultBrowser('http://www.prusa3d.com/drivers/');
  343. });
  344. $self->_append_menu_item($helpMenu, L("Prusa Edition Releases"), L('Open the Prusa Edition releases page in your browser'), sub {
  345. Wx::LaunchDefaultBrowser('http://github.com/prusa3d/slic3r/releases');
  346. });
  347. # my $versioncheck = $self->_append_menu_item($helpMenu, "Check for &Updates...", 'Check for new Slic3r versions', sub {
  348. # wxTheApp->check_version(1);
  349. # });
  350. # $versioncheck->Enable(wxTheApp->have_version_check);
  351. $self->_append_menu_item($helpMenu, L("Slic3r &Website"), L('Open the Slic3r website in your browser'), sub {
  352. Wx::LaunchDefaultBrowser('http://slic3r.org/');
  353. });
  354. $self->_append_menu_item($helpMenu, L("Slic3r &Manual"), L('Open the Slic3r manual in your browser'), sub {
  355. Wx::LaunchDefaultBrowser('http://manual.slic3r.org/');
  356. });
  357. $helpMenu->AppendSeparator();
  358. $self->_append_menu_item($helpMenu, L("System Info"), L('Show system information'), sub {
  359. wxTheApp->system_info;
  360. });
  361. $self->_append_menu_item($helpMenu, L("Show &Configuration Folder"), L('Show user configuration folder (datadir)'), sub {
  362. Slic3r::GUI::desktop_open_datadir_folder();
  363. });
  364. $self->_append_menu_item($helpMenu, L("Report an Issue"), L('Report an issue on the Slic3r Prusa Edition'), sub {
  365. Wx::LaunchDefaultBrowser('http://github.com/prusa3d/slic3r/issues/new');
  366. });
  367. $self->_append_menu_item($helpMenu, L("&About Slic3r"), L('Show about dialog'), sub {
  368. Slic3r::GUI::about;
  369. });
  370. }
  371. # menubar
  372. # assign menubar to frame after appending items, otherwise special items
  373. # will not be handled correctly
  374. {
  375. my $menubar = Wx::MenuBar->new;
  376. $menubar->Append($fileMenu, L("&File"));
  377. $menubar->Append($self->{plater_menu}, L("&Plater")) if $self->{plater_menu};
  378. $menubar->Append($self->{object_menu}, L("&Object")) if $self->{object_menu};
  379. $menubar->Append($windowMenu, L("&Window"));
  380. $menubar->Append($self->{viewMenu}, L("&View")) if $self->{viewMenu};
  381. # Add additional menus from C++
  382. Slic3r::GUI::add_menus($menubar, $self->{preferences_event}, $self->{lang_ch_event});
  383. $menubar->Append($helpMenu, L("&Help"));
  384. $self->SetMenuBar($menubar);
  385. }
  386. }
  387. sub is_loaded {
  388. my ($self) = @_;
  389. return $self->{loaded};
  390. }
  391. # Selection of a 3D object changed on the platter.
  392. sub on_plater_selection_changed {
  393. my ($self, $have_selection) = @_;
  394. return if !defined $self->{object_menu};
  395. $self->{object_menu}->Enable($_->GetId, $have_selection)
  396. for $self->{object_menu}->GetMenuItems;
  397. }
  398. sub slice_to_png {
  399. my $self = shift;
  400. $self->{plater}->stop_background_process;
  401. $self->{plater}->async_apply_config;
  402. $appController->print_ctl()->slice_to_png();
  403. }
  404. sub reslice_now {
  405. my ($self) = @_;
  406. $self->{plater}->reslice if $self->{plater};
  407. }
  408. sub repair_stl {
  409. my $self = shift;
  410. my $input_file;
  411. {
  412. my $dialog = Wx::FileDialog->new($self, L('Select the STL file to repair:'),
  413. wxTheApp->{app_config}->get_last_dir, "",
  414. &Slic3r::GUI::FILE_WILDCARDS->{stl}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  415. if ($dialog->ShowModal != wxID_OK) {
  416. $dialog->Destroy;
  417. return;
  418. }
  419. $input_file = $dialog->GetPaths;
  420. $dialog->Destroy;
  421. }
  422. my $output_file = $input_file;
  423. {
  424. $output_file =~ s/\.[sS][tT][lL]$/_fixed.obj/;
  425. my $dlg = Wx::FileDialog->new($self, L("Save OBJ file (less prone to coordinate errors than STL) as:"), dirname($output_file),
  426. basename($output_file), &Slic3r::GUI::FILE_WILDCARDS->{obj}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  427. if ($dlg->ShowModal != wxID_OK) {
  428. $dlg->Destroy;
  429. return undef;
  430. }
  431. $output_file = $dlg->GetPath;
  432. $dlg->Destroy;
  433. }
  434. my $tmesh = Slic3r::TriangleMesh->new;
  435. $tmesh->ReadSTLFile($input_file);
  436. $tmesh->repair;
  437. $tmesh->WriteOBJFile($output_file);
  438. Slic3r::GUI::show_info($self, L("Your file was repaired."), L("Repair"));
  439. }
  440. sub export_config {
  441. my $self = shift;
  442. # Generate a cummulative configuration for the selected print, filaments and printer.
  443. my $config = wxTheApp->{preset_bundle}->full_config();
  444. # Validate the cummulative configuration.
  445. eval { $config->validate; };
  446. Slic3r::GUI::catch_error($self) and return;
  447. # Ask user for the file name for the config file.
  448. my $dlg = Wx::FileDialog->new($self, L('Save configuration as:'),
  449. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  450. $last_config ? basename($last_config) : "config.ini",
  451. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  452. my $file = ($dlg->ShowModal == wxID_OK) ? $dlg->GetPath : undef;
  453. $dlg->Destroy;
  454. if (defined $file) {
  455. wxTheApp->{app_config}->update_config_dir(dirname($file));
  456. $last_config = $file;
  457. $config->save($file);
  458. }
  459. }
  460. # Load a config file containing a Print, Filament & Printer preset.
  461. sub load_config_file {
  462. my ($self, $file) = @_;
  463. if (!$file) {
  464. return unless Slic3r::GUI::check_unsaved_changes;
  465. my $dlg = Wx::FileDialog->new($self, L('Select configuration to load:'),
  466. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  467. "config.ini",
  468. 'INI files (*.ini, *.gcode)|*.ini;*.INI;*.gcode;*.g', wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  469. return unless $dlg->ShowModal == wxID_OK;
  470. $file = $dlg->GetPaths;
  471. $dlg->Destroy;
  472. }
  473. eval { wxTheApp->{preset_bundle}->load_config_file($file); };
  474. # Dont proceed further if the config file cannot be loaded.
  475. return if Slic3r::GUI::catch_error($self);
  476. $_->load_current_preset for (values %{$self->{options_tabs}});
  477. wxTheApp->{app_config}->update_config_dir(dirname($file));
  478. $last_config = $file;
  479. }
  480. sub export_configbundle {
  481. my ($self) = @_;
  482. return unless Slic3r::GUI::check_unsaved_changes;
  483. # validate current configuration in case it's dirty
  484. eval { wxTheApp->{preset_bundle}->full_config->validate; };
  485. Slic3r::GUI::catch_error($self) and return;
  486. # Ask user for a file name.
  487. my $dlg = Wx::FileDialog->new($self, L('Save presets bundle as:'),
  488. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  489. "Slic3r_config_bundle.ini",
  490. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  491. my $file = ($dlg->ShowModal == wxID_OK) ? $dlg->GetPath : undef;
  492. $dlg->Destroy;
  493. if (defined $file) {
  494. # Export the config bundle.
  495. wxTheApp->{app_config}->update_config_dir(dirname($file));
  496. eval { wxTheApp->{preset_bundle}->export_configbundle($file); };
  497. Slic3r::GUI::catch_error($self) and return;
  498. }
  499. }
  500. # Loading a config bundle with an external file name used to be used
  501. # to auto-install a config bundle on a fresh user account,
  502. # but that behavior was not documented and likely buggy.
  503. sub load_configbundle {
  504. my ($self, $file, $reset_user_profile) = @_;
  505. return unless Slic3r::GUI::check_unsaved_changes;
  506. if (!$file) {
  507. my $dlg = Wx::FileDialog->new($self, L('Select configuration to load:'),
  508. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  509. "config.ini",
  510. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  511. return unless $dlg->ShowModal == wxID_OK;
  512. $file = $dlg->GetPaths;
  513. $dlg->Destroy;
  514. }
  515. wxTheApp->{app_config}->update_config_dir(dirname($file));
  516. my $presets_imported = 0;
  517. eval { $presets_imported = wxTheApp->{preset_bundle}->load_configbundle($file); };
  518. Slic3r::GUI::catch_error($self) and return;
  519. # Load the currently selected preset into the GUI, update the preset selection box.
  520. foreach my $tab (values %{$self->{options_tabs}}) {
  521. $tab->load_current_preset;
  522. }
  523. my $message = sprintf L("%d presets successfully imported."), $presets_imported;
  524. Slic3r::GUI::show_info($self, $message);
  525. }
  526. # Load a provied DynamicConfig into the Print / Filament / Printer tabs, thus modifying the active preset.
  527. # Also update the platter with the new presets.
  528. sub load_config {
  529. my ($self, $config) = @_;
  530. $_->load_config($config) foreach values %{$self->{options_tabs}};
  531. $self->{plater}->on_config_change($config) if $self->{plater};
  532. }
  533. sub select_tab {
  534. my ($self, $tab) = @_;
  535. $self->{tabpanel}->SetSelection($tab);
  536. }
  537. # Set a camera direction, zoom to all objects.
  538. sub select_view {
  539. my ($self, $direction) = @_;
  540. if (! $self->{no_plater}) {
  541. $self->{plater}->select_view($direction);
  542. }
  543. }
  544. sub _append_menu_item {
  545. my ($self, $menu, $string, $description, $cb, $id, $icon) = @_;
  546. $id //= &Wx::NewId();
  547. my $item = $menu->Append($id, $string, $description);
  548. $self->_set_menu_item_icon($item, $icon);
  549. EVT_MENU($self, $id, $cb);
  550. return $item;
  551. }
  552. sub _set_menu_item_icon {
  553. my ($self, $menuItem, $icon) = @_;
  554. # SetBitmap was not available on OS X before Wx 0.9927
  555. if ($icon && $menuItem->can('SetBitmap')) {
  556. $menuItem->SetBitmap(Wx::Bitmap->new(Slic3r::var($icon), wxBITMAP_TYPE_PNG));
  557. }
  558. }
  559. # Called after the Preferences dialog is closed and the program settings are saved.
  560. # Update the UI based on the current preferences.
  561. sub update_ui_from_settings {
  562. my ($self) = @_;
  563. $self->{menu_item_reslice_now}->Enable(! wxTheApp->{app_config}->get("background_processing"));
  564. $self->{plater}->update_ui_from_settings if ($self->{plater});
  565. for my $tab_name (qw(print filament printer)) {
  566. $self->{options_tabs}{$tab_name}->update_ui_from_settings;
  567. }
  568. }
  569. 1;