MainFrame.pm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. Slic3r::GUI::restore_window_size($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. Slic3r::GUI::save_window_size($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. my $vol_idx = $event->GetInt;
  185. $self->{plater}->select_object_from_cpp($obj_idx < 0 ? undef: $obj_idx, $vol_idx<0 ? -1 : $vol_idx);
  186. });
  187. # The following event is emited by the C++ GUI implementation on object settings change.
  188. EVT_COMMAND($self, -1, $OBJECT_SETTINGS_CHANGED_EVENT, sub {
  189. my ($self, $event) = @_;
  190. my $line = $event->GetString;
  191. my ($obj_idx, $parts_changed, $part_settings_changed) = split('',$line);
  192. $self->{plater}->changed_object_settings($obj_idx, $parts_changed, $part_settings_changed);
  193. });
  194. # The following event is emited by the C++ GUI implementation on object remove.
  195. EVT_COMMAND($self, -1, $OBJECT_REMOVE_EVENT, sub {
  196. my ($self, $event) = @_;
  197. $self->{plater}->remove();
  198. });
  199. # The following event is emited by the C++ GUI implementation on extruder change for object.
  200. EVT_COMMAND($self, -1, $UPDATE_SCENE_EVENT, sub {
  201. my ($self, $event) = @_;
  202. $self->{plater}->update();
  203. });
  204. Slic3r::GUI::create_preset_tabs($VALUE_CHANGE_EVENT, $PRESETS_CHANGED_EVENT);
  205. $self->{options_tabs} = {};
  206. for my $tab_name (qw(print filament sla_material printer)) {
  207. $self->{options_tabs}{$tab_name} = Slic3r::GUI::get_preset_tab("$tab_name");
  208. }
  209. # Update progress bar with an event sent by the slicing core.
  210. EVT_COMMAND($self, -1, $PROGRESS_BAR_EVENT, sub {
  211. my ($self, $event) = @_;
  212. if (defined $self->{progress_dialog}) {
  213. # If a progress dialog is open, update it.
  214. $self->{progress_dialog}->Update($event->GetInt, $event->GetString . "…");
  215. } else {
  216. # Otherwise update the main window status bar.
  217. $self->{statusbar}->SetProgress($event->GetInt);
  218. $self->{statusbar}->SetStatusText($event->GetString . "…");
  219. }
  220. });
  221. EVT_COMMAND($self, -1, $ERROR_EVENT, sub {
  222. my ($self, $event) = @_;
  223. Slic3r::GUI::show_error($self, $event->GetString);
  224. });
  225. if ($self->{plater}) {
  226. $self->{plater}->on_select_preset(sub {
  227. my ($group, $name) = @_;
  228. $self->{options_tabs}{$group}->select_preset($name);
  229. });
  230. # load initial config
  231. my $full_config = wxTheApp->{preset_bundle}->full_config;
  232. $self->{plater}->on_config_change($full_config);
  233. # Show a correct number of filament fields.
  234. if (defined $full_config->nozzle_diameter){ # nozzle_diameter is undefined when SLA printer is selected
  235. $self->{plater}->on_extruders_change(int(@{$full_config->nozzle_diameter}));
  236. }
  237. # Show correct preset comboboxes according to the printer_technology
  238. $self->{plater}->show_preset_comboboxes(($full_config->printer_technology eq "FFF") ? 0 : 1);
  239. }
  240. }
  241. sub _init_menubar {
  242. my ($self) = @_;
  243. # File menu
  244. my $fileMenu = Wx::Menu->new;
  245. {
  246. wxTheApp->append_menu_item($fileMenu, L("Open STL/OBJ/AMF/3MF…\tCtrl+O"), L('Open a model'), sub {
  247. $self->{plater}->add if $self->{plater};
  248. }, undef, undef); #'brick_add.png');
  249. $self->_append_menu_item($fileMenu, L("&Load Config…\tCtrl+L"), L('Load exported configuration file'), sub {
  250. $self->load_config_file;
  251. }, undef, 'plugin_add.png');
  252. $self->_append_menu_item($fileMenu, L("&Export Config…\tCtrl+E"), L('Export current configuration to file'), sub {
  253. $self->export_config;
  254. }, undef, 'plugin_go.png');
  255. $self->_append_menu_item($fileMenu, L("&Load Config Bundle…"), L('Load presets from a bundle'), sub {
  256. $self->load_configbundle;
  257. }, undef, 'lorry_add.png');
  258. $self->_append_menu_item($fileMenu, L("&Export Config Bundle…"), L('Export all presets to file'), sub {
  259. $self->export_configbundle;
  260. }, undef, 'lorry_go.png');
  261. $fileMenu->AppendSeparator();
  262. $self->_append_menu_item($fileMenu, L("Slice to PNG…"), L('Slice file to a set of PNG files'), sub {
  263. $self->slice_to_png;
  264. }, undef, 'shape_handles.png');
  265. $self->{menu_item_reslice_now} = $self->_append_menu_item(
  266. $fileMenu, L("(&Re)Slice Now\tCtrl+S"), L('Start new slicing process'),
  267. sub { $self->reslice_now; }, undef, 'shape_handles.png');
  268. $fileMenu->AppendSeparator();
  269. $self->_append_menu_item($fileMenu, L("Repair STL file…"), L('Automatically repair an STL file'), sub {
  270. $self->repair_stl;
  271. }, undef, 'wrench.png');
  272. $fileMenu->AppendSeparator();
  273. $self->_append_menu_item($fileMenu, L("&Quit"), L('Quit Slic3r'), sub {
  274. $self->Close(0);
  275. }, wxID_EXIT);
  276. }
  277. # Plater menu
  278. unless ($self->{no_plater}) {
  279. my $plater = $self->{plater};
  280. $self->{plater_menu} = Wx::Menu->new;
  281. $self->_append_menu_item($self->{plater_menu}, L("Export G-code..."), L('Export current plate as G-code'), sub {
  282. $plater->export_gcode;
  283. }, undef, 'cog_go.png');
  284. $self->_append_menu_item($self->{plater_menu}, L("Export plate as STL..."), L('Export current plate as STL'), sub {
  285. $plater->export_stl;
  286. }, undef, 'brick_go.png');
  287. $self->_append_menu_item($self->{plater_menu}, L("Export plate as AMF..."), L('Export current plate as AMF'), sub {
  288. $plater->export_amf;
  289. }, undef, 'brick_go.png');
  290. $self->_append_menu_item($self->{plater_menu}, L("Export plate as 3MF..."), L('Export current plate as 3MF'), sub {
  291. $plater->export_3mf;
  292. }, undef, 'brick_go.png');
  293. $self->{object_menu} = $self->{plater}->object_menu;
  294. $self->on_plater_selection_changed(0);
  295. }
  296. # Window menu
  297. my $windowMenu = Wx::Menu->new;
  298. {
  299. my $tab_offset = 0;
  300. if (!$self->{no_plater}) {
  301. $self->_append_menu_item($windowMenu, L("Select &Plater Tab\tCtrl+1"), L('Show the plater'), sub {
  302. $self->select_tab(0);
  303. }, undef, 'application_view_tile.png');
  304. $tab_offset += 1;
  305. }
  306. if (!$self->{no_controller}) {
  307. $self->_append_menu_item($windowMenu, L("Select &Controller Tab\tCtrl+T"), L('Show the printer controller'), sub {
  308. $self->select_tab(1);
  309. }, undef, 'printer_empty.png');
  310. $tab_offset += 1;
  311. }
  312. if ($tab_offset > 0) {
  313. $windowMenu->AppendSeparator();
  314. }
  315. $self->_append_menu_item($windowMenu, L("Select P&rint Settings Tab\tCtrl+2"), L('Show the print settings'), sub {
  316. $self->select_tab($tab_offset+0);
  317. }, undef, 'cog.png');
  318. $self->_append_menu_item($windowMenu, L("Select &Filament Settings Tab\tCtrl+3"), L('Show the filament settings'), sub {
  319. $self->select_tab($tab_offset+1);
  320. }, undef, 'spool.png');
  321. $self->_append_menu_item($windowMenu, L("Select Print&er Settings Tab\tCtrl+4"), L('Show the printer settings'), sub {
  322. $self->select_tab($tab_offset+2);
  323. }, undef, 'printer_empty.png');
  324. }
  325. # View menu
  326. if (!$self->{no_plater}) {
  327. $self->{viewMenu} = Wx::Menu->new;
  328. # \xA0 is a non-breaing space. It is entered here to spoil the automatic accelerators,
  329. # as the simple numeric accelerators spoil all numeric data entry.
  330. # The camera control accelerators are captured by 3DScene Perl module instead.
  331. my $accel = ($^O eq 'MSWin32') ? sub { $_[0] . "\t\xA0" . $_[1] } : sub { $_[0] };
  332. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Iso'), '0'), L('Iso View') , sub { $self->select_view('iso' ); });
  333. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Top'), '1'), L('Top View') , sub { $self->select_view('top' ); });
  334. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Bottom'), '2'), L('Bottom View') , sub { $self->select_view('bottom' ); });
  335. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Front'), '3'), L('Front View') , sub { $self->select_view('front' ); });
  336. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Rear'), '4'), L('Rear View') , sub { $self->select_view('rear' ); });
  337. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Left'), '5'), L('Left View') , sub { $self->select_view('left' ); });
  338. $self->_append_menu_item($self->{viewMenu}, $accel->(L('Right'), '6'), L('Right View') , sub { $self->select_view('right' ); });
  339. }
  340. # Help menu
  341. my $helpMenu = Wx::Menu->new;
  342. {
  343. $self->_append_menu_item($helpMenu, L("Prusa 3D Drivers"), L('Open the Prusa3D drivers download page in your browser'), sub {
  344. Wx::LaunchDefaultBrowser('http://www.prusa3d.com/drivers/');
  345. });
  346. $self->_append_menu_item($helpMenu, L("Prusa Edition Releases"), L('Open the Prusa Edition releases page in your browser'), sub {
  347. Wx::LaunchDefaultBrowser('http://github.com/prusa3d/slic3r/releases');
  348. });
  349. # my $versioncheck = $self->_append_menu_item($helpMenu, "Check for &Updates...", 'Check for new Slic3r versions', sub {
  350. # wxTheApp->check_version(1);
  351. # });
  352. # $versioncheck->Enable(wxTheApp->have_version_check);
  353. $self->_append_menu_item($helpMenu, L("Slic3r &Website"), L('Open the Slic3r website in your browser'), sub {
  354. Wx::LaunchDefaultBrowser('http://slic3r.org/');
  355. });
  356. $self->_append_menu_item($helpMenu, L("Slic3r &Manual"), L('Open the Slic3r manual in your browser'), sub {
  357. Wx::LaunchDefaultBrowser('http://manual.slic3r.org/');
  358. });
  359. $helpMenu->AppendSeparator();
  360. $self->_append_menu_item($helpMenu, L("System Info"), L('Show system information'), sub {
  361. wxTheApp->system_info;
  362. });
  363. $self->_append_menu_item($helpMenu, L("Show &Configuration Folder"), L('Show user configuration folder (datadir)'), sub {
  364. Slic3r::GUI::desktop_open_datadir_folder();
  365. });
  366. $self->_append_menu_item($helpMenu, L("Report an Issue"), L('Report an issue on the Slic3r Prusa Edition'), sub {
  367. Wx::LaunchDefaultBrowser('http://github.com/prusa3d/slic3r/issues/new');
  368. });
  369. $self->_append_menu_item($helpMenu, L("&About Slic3r"), L('Show about dialog'), sub {
  370. Slic3r::GUI::about;
  371. });
  372. }
  373. # menubar
  374. # assign menubar to frame after appending items, otherwise special items
  375. # will not be handled correctly
  376. {
  377. my $menubar = Wx::MenuBar->new;
  378. $menubar->Append($fileMenu, L("&File"));
  379. $menubar->Append($self->{plater_menu}, L("&Plater")) if $self->{plater_menu};
  380. $menubar->Append($self->{object_menu}, L("&Object")) if $self->{object_menu};
  381. $menubar->Append($windowMenu, L("&Window"));
  382. $menubar->Append($self->{viewMenu}, L("&View")) if $self->{viewMenu};
  383. # Add additional menus from C++
  384. Slic3r::GUI::add_menus($menubar, $self->{preferences_event}, $self->{lang_ch_event});
  385. $menubar->Append($helpMenu, L("&Help"));
  386. $self->SetMenuBar($menubar);
  387. }
  388. }
  389. sub is_loaded {
  390. my ($self) = @_;
  391. return $self->{loaded};
  392. }
  393. # Selection of a 3D object changed on the platter.
  394. sub on_plater_selection_changed {
  395. my ($self, $have_selection) = @_;
  396. return if !defined $self->{object_menu};
  397. $self->{object_menu}->Enable($_->GetId, $have_selection)
  398. for $self->{object_menu}->GetMenuItems;
  399. }
  400. sub slice_to_png {
  401. my $self = shift;
  402. $self->{plater}->stop_background_process;
  403. $self->{plater}->async_apply_config;
  404. $appController->print_ctl()->slice_to_png();
  405. }
  406. sub reslice_now {
  407. my ($self) = @_;
  408. $self->{plater}->reslice if $self->{plater};
  409. }
  410. sub repair_stl {
  411. my $self = shift;
  412. my $input_file;
  413. {
  414. my $dialog = Wx::FileDialog->new($self, L('Select the STL file to repair:'),
  415. wxTheApp->{app_config}->get_last_dir, "",
  416. &Slic3r::GUI::FILE_WILDCARDS->{stl}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  417. if ($dialog->ShowModal != wxID_OK) {
  418. $dialog->Destroy;
  419. return;
  420. }
  421. $input_file = $dialog->GetPaths;
  422. $dialog->Destroy;
  423. }
  424. my $output_file = $input_file;
  425. {
  426. $output_file =~ s/\.[sS][tT][lL]$/_fixed.obj/;
  427. my $dlg = Wx::FileDialog->new($self, L("Save OBJ file (less prone to coordinate errors than STL) as:"), dirname($output_file),
  428. basename($output_file), &Slic3r::GUI::FILE_WILDCARDS->{obj}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  429. if ($dlg->ShowModal != wxID_OK) {
  430. $dlg->Destroy;
  431. return undef;
  432. }
  433. $output_file = $dlg->GetPath;
  434. $dlg->Destroy;
  435. }
  436. my $tmesh = Slic3r::TriangleMesh->new;
  437. $tmesh->ReadSTLFile($input_file);
  438. $tmesh->repair;
  439. $tmesh->WriteOBJFile($output_file);
  440. Slic3r::GUI::show_info($self, L("Your file was repaired."), L("Repair"));
  441. }
  442. sub export_config {
  443. my $self = shift;
  444. # Generate a cummulative configuration for the selected print, filaments and printer.
  445. my $config = wxTheApp->{preset_bundle}->full_config();
  446. # Validate the cummulative configuration.
  447. eval { $config->validate; };
  448. Slic3r::GUI::catch_error($self) and return;
  449. # Ask user for the file name for the config file.
  450. my $dlg = Wx::FileDialog->new($self, L('Save configuration as:'),
  451. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  452. $last_config ? basename($last_config) : "config.ini",
  453. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  454. my $file = ($dlg->ShowModal == wxID_OK) ? $dlg->GetPath : undef;
  455. $dlg->Destroy;
  456. if (defined $file) {
  457. wxTheApp->{app_config}->update_config_dir(dirname($file));
  458. $last_config = $file;
  459. $config->save($file);
  460. }
  461. }
  462. # Load a config file containing a Print, Filament & Printer preset.
  463. sub load_config_file {
  464. my ($self, $file) = @_;
  465. if (!$file) {
  466. return unless Slic3r::GUI::check_unsaved_changes;
  467. my $dlg = Wx::FileDialog->new($self, L('Select configuration to load:'),
  468. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  469. "config.ini",
  470. 'INI files (*.ini, *.gcode)|*.ini;*.INI;*.gcode;*.g', wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  471. return unless $dlg->ShowModal == wxID_OK;
  472. $file = $dlg->GetPaths;
  473. $dlg->Destroy;
  474. }
  475. eval { wxTheApp->{preset_bundle}->load_config_file($file); };
  476. # Dont proceed further if the config file cannot be loaded.
  477. return if Slic3r::GUI::catch_error($self);
  478. $_->load_current_preset for (values %{$self->{options_tabs}});
  479. wxTheApp->{app_config}->update_config_dir(dirname($file));
  480. $last_config = $file;
  481. }
  482. sub export_configbundle {
  483. my ($self) = @_;
  484. return unless Slic3r::GUI::check_unsaved_changes;
  485. # validate current configuration in case it's dirty
  486. eval { wxTheApp->{preset_bundle}->full_config->validate; };
  487. Slic3r::GUI::catch_error($self) and return;
  488. # Ask user for a file name.
  489. my $dlg = Wx::FileDialog->new($self, L('Save presets bundle as:'),
  490. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  491. "Slic3r_config_bundle.ini",
  492. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  493. my $file = ($dlg->ShowModal == wxID_OK) ? $dlg->GetPath : undef;
  494. $dlg->Destroy;
  495. if (defined $file) {
  496. # Export the config bundle.
  497. wxTheApp->{app_config}->update_config_dir(dirname($file));
  498. eval { wxTheApp->{preset_bundle}->export_configbundle($file); };
  499. Slic3r::GUI::catch_error($self) and return;
  500. }
  501. }
  502. # Loading a config bundle with an external file name used to be used
  503. # to auto-install a config bundle on a fresh user account,
  504. # but that behavior was not documented and likely buggy.
  505. sub load_configbundle {
  506. my ($self, $file, $reset_user_profile) = @_;
  507. return unless Slic3r::GUI::check_unsaved_changes;
  508. if (!$file) {
  509. my $dlg = Wx::FileDialog->new($self, L('Select configuration to load:'),
  510. $last_config ? dirname($last_config) : wxTheApp->{app_config}->get_last_dir,
  511. "config.ini",
  512. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  513. return unless $dlg->ShowModal == wxID_OK;
  514. $file = $dlg->GetPaths;
  515. $dlg->Destroy;
  516. }
  517. wxTheApp->{app_config}->update_config_dir(dirname($file));
  518. my $presets_imported = 0;
  519. eval { $presets_imported = wxTheApp->{preset_bundle}->load_configbundle($file); };
  520. Slic3r::GUI::catch_error($self) and return;
  521. # Load the currently selected preset into the GUI, update the preset selection box.
  522. foreach my $tab (values %{$self->{options_tabs}}) {
  523. $tab->load_current_preset;
  524. }
  525. my $message = sprintf L("%d presets successfully imported."), $presets_imported;
  526. Slic3r::GUI::show_info($self, $message);
  527. }
  528. # Load a provied DynamicConfig into the Print / Filament / Printer tabs, thus modifying the active preset.
  529. # Also update the platter with the new presets.
  530. sub load_config {
  531. my ($self, $config) = @_;
  532. $_->load_config($config) foreach values %{$self->{options_tabs}};
  533. $self->{plater}->on_config_change($config) if $self->{plater};
  534. }
  535. sub select_tab {
  536. my ($self, $tab) = @_;
  537. $self->{tabpanel}->SetSelection($tab);
  538. }
  539. # Set a camera direction, zoom to all objects.
  540. sub select_view {
  541. my ($self, $direction) = @_;
  542. if (! $self->{no_plater}) {
  543. $self->{plater}->select_view($direction);
  544. }
  545. }
  546. sub _append_menu_item {
  547. my ($self, $menu, $string, $description, $cb, $id, $icon) = @_;
  548. $id //= &Wx::NewId();
  549. my $item = $menu->Append($id, $string, $description);
  550. $self->_set_menu_item_icon($item, $icon);
  551. EVT_MENU($self, $id, $cb);
  552. return $item;
  553. }
  554. sub _set_menu_item_icon {
  555. my ($self, $menuItem, $icon) = @_;
  556. # SetBitmap was not available on OS X before Wx 0.9927
  557. if ($icon && $menuItem->can('SetBitmap')) {
  558. $menuItem->SetBitmap(Wx::Bitmap->new(Slic3r::var($icon), wxBITMAP_TYPE_PNG));
  559. }
  560. }
  561. # Called after the Preferences dialog is closed and the program settings are saved.
  562. # Update the UI based on the current preferences.
  563. sub update_ui_from_settings {
  564. my ($self) = @_;
  565. $self->{menu_item_reslice_now}->Enable(! wxTheApp->{app_config}->get("background_processing"));
  566. $self->{plater}->update_ui_from_settings if ($self->{plater});
  567. for my $tab_name (qw(print filament printer)) {
  568. $self->{options_tabs}{$tab_name}->update_ui_from_settings;
  569. }
  570. }
  571. 1;