MainFrame.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 :panel :sizer :menu :dialog :filedialog
  10. :font :icon :notebook wxTheApp);
  11. use Wx::Event qw(EVT_CLOSE 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) = @_;
  18. my $self = $class->SUPER::new(undef, -1, 'Slic3r', wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE);
  19. if ($^O eq 'MSWin32') {
  20. $self->SetIcon(Wx::Icon->new($Slic3r::var->("Slic3r.ico"), wxBITMAP_TYPE_ICO));
  21. } else {
  22. $self->SetIcon(Wx::Icon->new($Slic3r::var->("Slic3r_128px.png"), wxBITMAP_TYPE_PNG));
  23. }
  24. $self->{loaded} = 0;
  25. $self->{preset_editor_tabs} = {}; # group => panel
  26. # initialize tabpanel and menubar
  27. $self->_init_tabpanel;
  28. $self->_init_menubar;
  29. # set default tooltip timer in msec
  30. # SetAutoPop supposedly accepts long integers but some bug doesn't allow for larger values
  31. # (SetAutoPop is not available on GTK.)
  32. eval { Wx::ToolTip::SetAutoPop(32767) };
  33. # initialize status bar
  34. $self->{statusbar} = Slic3r::GUI::ProgressStatusBar->new($self, -1);
  35. $self->{statusbar}->SetStatusText("Version $Slic3r::VERSION - Remember to check for updates at http://slic3r.org/");
  36. $self->SetStatusBar($self->{statusbar});
  37. $self->{loaded} = 1;
  38. # initialize layout
  39. {
  40. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  41. $sizer->Add($self->{tabpanel}, 1, wxEXPAND);
  42. $sizer->SetSizeHints($self);
  43. $self->SetSizer($sizer);
  44. $self->Fit;
  45. $self->SetMinSize([760, 490]);
  46. $self->SetSize($self->GetMinSize);
  47. wxTheApp->restore_window_pos($self, "main_frame");
  48. $self->Show;
  49. $self->Layout;
  50. }
  51. # declare events
  52. EVT_CLOSE($self, sub {
  53. my (undef, $event) = @_;
  54. if ($event->CanVeto) {
  55. if (!$self->{plater}->prompt_unsaved_changes) {
  56. $event->Veto;
  57. return;
  58. }
  59. if ($self->{controller} && $self->{controller}->printing) {
  60. my $confirm = Wx::MessageDialog->new($self, "You are currently printing. Do you want to stop printing and continue anyway?",
  61. 'Unfinished Print', wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT);
  62. if ($confirm->ShowModal == wxID_NO) {
  63. $event->Veto;
  64. return;
  65. }
  66. }
  67. }
  68. # save window size
  69. wxTheApp->save_window_pos($self, "main_frame");
  70. # propagate event
  71. $event->Skip;
  72. });
  73. return $self;
  74. }
  75. sub _init_tabpanel {
  76. my ($self) = @_;
  77. $self->{tabpanel} = my $panel = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
  78. EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{tabpanel}, sub {
  79. my $panel = $self->{tabpanel}->GetCurrentPage;
  80. $panel->OnActivate if $panel->can('OnActivate');
  81. });
  82. $panel->AddPage($self->{plater} = Slic3r::GUI::Plater->new($panel), "Plater");
  83. $panel->AddPage($self->{controller} = Slic3r::GUI::Controller->new($panel), "Controller")
  84. if ($Slic3r::GUI::Settings->{_}{show_host});
  85. if ($Slic3r::GUI::Settings->{_}{tabbed_preset_editors}) {
  86. foreach my $group (qw(print filament printer)) {
  87. $self->{plater}->show_preset_editor($group, 0, $panel);
  88. }
  89. }
  90. }
  91. sub _init_menubar {
  92. my ($self) = @_;
  93. # File menu
  94. my $fileMenu = Wx::Menu->new;
  95. {
  96. wxTheApp->append_menu_item($fileMenu, "Open STL/OBJ/AMF/3MF…\tCtrl+O", 'Open a model', sub {
  97. $self->{plater}->add if $self->{plater};
  98. }, undef, 'brick_add.png');
  99. wxTheApp->append_menu_item($fileMenu, "Open 2.5D TIN mesh…", 'Import a 2.5D TIN mesh', sub {
  100. $self->{plater}->add_tin if $self->{plater};
  101. }, undef, 'map_add.png');
  102. $fileMenu->AppendSeparator();
  103. wxTheApp->append_menu_item($fileMenu, "&Load Config…\tCtrl+L", 'Load exported configuration file', sub {
  104. $self->load_config_file;
  105. }, undef, 'plugin_add.png');
  106. wxTheApp->append_menu_item($fileMenu, "&Export Config…\tCtrl+E", 'Export current configuration to file', sub {
  107. $self->export_config;
  108. }, undef, 'plugin_go.png');
  109. wxTheApp->append_menu_item($fileMenu, "&Load Config Bundle…", 'Load presets from a bundle', sub {
  110. $self->load_configbundle;
  111. }, undef, 'lorry_add.png');
  112. wxTheApp->append_menu_item($fileMenu, "&Export Config Bundle…", 'Export all presets to file', sub {
  113. $self->export_configbundle;
  114. }, undef, 'lorry_go.png');
  115. wxTheApp->append_menu_item($fileMenu, "&Import Config from GCode-File…", 'Load presets from a created GCode-File', sub {
  116. $self->import_fromGCode;
  117. }, undef, 'lorry_import.png');
  118. $fileMenu->AppendSeparator();
  119. my $repeat;
  120. wxTheApp->append_menu_item($fileMenu, "Q&uick Slice…\tCtrl+U", 'Slice file', sub {
  121. wxTheApp->CallAfter(sub {
  122. $self->quick_slice;
  123. $repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
  124. });
  125. }, undef, 'cog_go.png');
  126. wxTheApp->append_menu_item($fileMenu, "Quick Slice and Save &As…\tCtrl+Alt+U", 'Slice file and save as', sub {
  127. wxTheApp->CallAfter(sub {
  128. $self->quick_slice(save_as => 1);
  129. $repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
  130. });
  131. }, undef, 'cog_go.png');
  132. $repeat = wxTheApp->append_menu_item($fileMenu, "&Repeat Last Quick Slice\tCtrl+Shift+U", 'Repeat last quick slice', sub {
  133. wxTheApp->CallAfter(sub {
  134. $self->quick_slice(reslice => 1);
  135. });
  136. }, undef, 'cog_go.png');
  137. $repeat->Enable(0);
  138. $fileMenu->AppendSeparator();
  139. wxTheApp->append_menu_item($fileMenu, "Slice to SV&G…\tCtrl+G", 'Slice file to SVG', sub {
  140. $self->quick_slice(save_as => 1, export_svg => 1);
  141. }, undef, 'shape_handles.png');
  142. $fileMenu->AppendSeparator();
  143. wxTheApp->append_menu_item($fileMenu, "Repair STL file…", 'Automatically repair an STL file', sub {
  144. $self->repair_stl;
  145. }, undef, 'wrench.png');
  146. $fileMenu->AppendSeparator();
  147. # Cmd+, is standard on OS X - what about other operating systems?
  148. wxTheApp->append_menu_item($fileMenu, "Preferences…\tCtrl+,", 'Application preferences', sub {
  149. Slic3r::GUI::Preferences->new($self)->ShowModal;
  150. }, wxID_PREFERENCES);
  151. $fileMenu->AppendSeparator();
  152. wxTheApp->append_menu_item($fileMenu, "&Quit", 'Quit Slic3r', sub {
  153. $self->Close(0);
  154. }, wxID_EXIT);
  155. }
  156. # Plater menu
  157. {
  158. my $plater = $self->{plater};
  159. $self->{plater_menu} = Wx::Menu->new;
  160. {
  161. my $selectMenu = $self->{plater_select_menu} = Wx::Menu->new;
  162. wxTheApp->append_submenu($self->{plater_menu}, "Select", 'Select an object in the plater', $selectMenu, undef, 'brick.png');
  163. }
  164. wxTheApp->append_menu_item($self->{plater_menu}, "Undo\tCtrl+Z", 'Undo', sub {
  165. $plater->undo;
  166. }, undef, 'arrow_undo.png');
  167. wxTheApp->append_menu_item($self->{plater_menu}, "Redo\tCtrl+Shift+Z", 'Redo', sub {
  168. $plater->redo;
  169. }, undef, 'arrow_redo.png');
  170. wxTheApp->append_menu_item($self->{plater_menu}, "Select Next Object\tCtrl+Right", 'Select Next Object in the plater', sub {
  171. $plater->select_next;
  172. }, undef, 'arrow_right.png');
  173. wxTheApp->append_menu_item($self->{plater_menu}, "Select Prev Object\tCtrl+Left", 'Select Previous Object in the plater', sub {
  174. $plater->select_prev;
  175. }, undef, 'arrow_left.png');
  176. wxTheApp->append_menu_item($self->{plater_menu}, "Zoom In\tCtrl+up", 'Zoom In',
  177. sub { $self->{plater}->zoom('in') }, undef, 'zoom_in.png');
  178. wxTheApp->append_menu_item($self->{plater_menu}, "Zoom Out\tCtrl+down", 'Zoom Out',
  179. sub { $self->{plater}->zoom('out') }, undef, 'zoom_out.png');
  180. $self->{plater_menu}->AppendSeparator();
  181. wxTheApp->append_menu_item($self->{plater_menu}, "Export G-code...", 'Export current plate as G-code', sub {
  182. $plater->export_gcode;
  183. }, undef, 'cog_go.png');
  184. wxTheApp->append_menu_item($self->{plater_menu}, "Export plate as STL...", 'Export current plate as STL', sub {
  185. $plater->export_stl;
  186. }, undef, 'brick_go.png');
  187. wxTheApp->append_menu_item($self->{plater_menu}, "Export plate with modifiers as AMF...", 'Export current plate as AMF, including all modifier meshes', sub {
  188. $plater->export_amf;
  189. }, undef, 'brick_go.png');
  190. wxTheApp->append_menu_item($self->{plater_menu}, "Export plate with modifiers as 3MF...", 'Export current plate as 3MF, including all modifier meshes', sub {
  191. $plater->export_tmf;
  192. }, undef, 'brick_go.png');
  193. $self->{object_menu} = $self->{plater}->object_menu;
  194. $self->on_plater_object_list_changed(0);
  195. $self->on_plater_selection_changed(0);
  196. }
  197. # Settings menu
  198. my $settingsMenu = Wx::Menu->new;
  199. {
  200. wxTheApp->append_menu_item($settingsMenu, "P&rint Settings…\tCtrl+1", 'Show the print settings editor', sub {
  201. $self->{plater}->show_preset_editor('print');
  202. }, undef, 'cog.png');
  203. wxTheApp->append_menu_item($settingsMenu, "&Filament Settings…\tCtrl+2", 'Show the filament settings editor', sub {
  204. $self->{plater}->show_preset_editor('filament');
  205. }, undef, 'spool.png');
  206. wxTheApp->append_menu_item($settingsMenu, "Print&er Settings…\tCtrl+3", 'Show the printer settings editor', sub {
  207. $self->{plater}->show_preset_editor('printer');
  208. }, undef, 'printer_empty.png');
  209. }
  210. # View menu
  211. {
  212. $self->{viewMenu} = Wx::Menu->new;
  213. wxTheApp->append_menu_item($self->{viewMenu}, "Top\tCtrl+4" , 'Top View' , sub { $self->select_view('top' ); });
  214. wxTheApp->append_menu_item($self->{viewMenu}, "Bottom\tCtrl+5" , 'Bottom View' , sub { $self->select_view('bottom' ); });
  215. wxTheApp->append_menu_item($self->{viewMenu}, "Left\tCtrl+6" , 'Left View' , sub { $self->select_view('left' ); });
  216. wxTheApp->append_menu_item($self->{viewMenu}, "Right\tCtrl+7" , 'Right View' , sub { $self->select_view('right' ); });
  217. wxTheApp->append_menu_item($self->{viewMenu}, "Front\tCtrl+8" , 'Front View' , sub { $self->select_view('front' ); });
  218. wxTheApp->append_menu_item($self->{viewMenu}, "Back\tCtrl+9" , 'Back View' , sub { $self->select_view('back' ); });
  219. wxTheApp->append_menu_item($self->{viewMenu}, "Diagonal\tCtrl+0", 'Diagonal View', sub { $self->select_view('diagonal'); });
  220. $self->{viewMenu}->AppendSeparator();
  221. $self->{color_toolpaths_by_role} = wxTheApp->append_menu_item($self->{viewMenu},
  222. "Color Toolpaths by Role",
  223. 'Color toolpaths according to perimeter/infill/support material',
  224. sub {
  225. $Slic3r::GUI::Settings->{_}{color_toolpaths_by} = 'role';
  226. wxTheApp->save_settings;
  227. $self->{plater}{preview3D}->reload_print;
  228. },
  229. undef, undef, wxITEM_RADIO
  230. );
  231. $self->{color_toolpaths_by_extruder} = wxTheApp->append_menu_item($self->{viewMenu},
  232. "Color Toolpaths by Filament",
  233. 'Color toolpaths using the configured extruder/filament color',
  234. sub {
  235. $Slic3r::GUI::Settings->{_}{color_toolpaths_by} = 'extruder';
  236. wxTheApp->save_settings;
  237. $self->{plater}{preview3D}->reload_print;
  238. },
  239. undef, undef, wxITEM_RADIO
  240. );
  241. if ($Slic3r::GUI::Settings->{_}{color_toolpaths_by} eq 'role') {
  242. $self->{color_toolpaths_by_role}->Check(1);
  243. } else {
  244. $self->{color_toolpaths_by_extruder}->Check(1);
  245. }
  246. }
  247. # Window menu
  248. my $windowMenu = Wx::Menu->new;
  249. {
  250. wxTheApp->append_menu_item($windowMenu, "&Plater\tCtrl+T", 'Show the plater', sub {
  251. $self->select_tab(0);
  252. }, undef, 'application_view_tile.png');
  253. wxTheApp->append_menu_item($windowMenu, "&Controller\tCtrl+Y", 'Show the printer controller', sub {
  254. if ($Slic3r::GUI::Settings->{_}{show_host}) {
  255. $self->select_tab(1);
  256. } else {
  257. my $confirm = Wx::MessageDialog->new($self, "The printer controller is currently disabled in the preferences. Do you want to enable it?",
  258. 'Controller', wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT);
  259. if ($confirm->ShowModal == wxID_YES) {
  260. $Slic3r::GUI::Settings->{_}{show_host} = 1;
  261. wxTheApp->save_settings;
  262. Slic3r::GUI::show_info($self, "The controller is now enabled. You must restart Slic3r now to make the change effective.", "Controller");
  263. }
  264. }
  265. }, undef, 'printer_empty.png');
  266. wxTheApp->append_menu_item($windowMenu, "DLP Projector…\tCtrl+P", 'Open projector window for DLP printing', sub {
  267. $self->{plater}->pause_background_process;
  268. $self->{slaconfig} = Slic3r::Config->new;
  269. Slic3r::GUI::SLAPrintOptions->new($self)->ShowModal;
  270. $self->{plater}->resume_background_process;
  271. }, undef, 'film.png');
  272. }
  273. # Help menu
  274. my $helpMenu = Wx::Menu->new;
  275. {
  276. wxTheApp->append_menu_item($helpMenu, "&Configuration $Slic3r::GUI::ConfigWizard::wizard…", "Run Configuration $Slic3r::GUI::ConfigWizard::wizard", sub {
  277. $self->config_wizard;
  278. });
  279. $helpMenu->AppendSeparator();
  280. wxTheApp->append_menu_item($helpMenu, "Slic3r &Website", 'Open the Slic3r website in your browser', sub {
  281. Wx::LaunchDefaultBrowser('http://slic3r.org/');
  282. });
  283. my $versioncheck = wxTheApp->append_menu_item($helpMenu, "Check for &Updates...", 'Check for new Slic3r versions', sub {
  284. wxTheApp->check_version(1);
  285. });
  286. $versioncheck->Enable(wxTheApp->have_version_check);
  287. wxTheApp->append_menu_item($helpMenu, "Slic3r &Manual", 'Open the Slic3r manual in your browser', sub {
  288. Wx::LaunchDefaultBrowser('http://manual.slic3r.org/');
  289. });
  290. $helpMenu->AppendSeparator();
  291. wxTheApp->append_menu_item($helpMenu, "&About Slic3r", 'Show about dialog', sub {
  292. wxTheApp->about;
  293. });
  294. }
  295. # menubar
  296. # assign menubar to frame after appending items, otherwise special items
  297. # will not be handled correctly
  298. {
  299. my $menubar = Wx::MenuBar->new;
  300. $menubar->Append($fileMenu, "&File");
  301. $menubar->Append($self->{plater_menu}, "&Plater") if $self->{plater_menu};
  302. $menubar->Append($self->{object_menu}, "&Object") if $self->{object_menu};
  303. $menubar->Append($settingsMenu, "&Settings");
  304. $menubar->Append($self->{viewMenu}, "&View") if $self->{viewMenu};
  305. $menubar->Append($windowMenu, "&Window");
  306. $menubar->Append($helpMenu, "&Help");
  307. $self->SetMenuBar($menubar);
  308. }
  309. }
  310. sub is_loaded {
  311. my ($self) = @_;
  312. return $self->{loaded};
  313. }
  314. sub on_undo_redo_stacks_changed {
  315. my $self = shift;
  316. # Enable undo or redo if they have operations in their stack.
  317. $self->{plater_menu}->Enable($self->{plater_menu}->FindItem("Undo\tCtrl+Z"), $#{$self->{plater}->{undo_stack}} < 0 ? 0 : 1);
  318. $self->{plater_menu}->Enable( $self->{plater_menu}->FindItem("Redo\tCtrl+Shift+Z"), $#{$self->{plater}->{redo_stack}} < 0 ? 0 : 1);
  319. }
  320. sub on_plater_object_list_changed {
  321. my ($self, $have_objects) = @_;
  322. return if !defined $self->{plater_menu};
  323. $self->{plater_menu}->Enable($_->GetId, $have_objects)
  324. for $self->{plater_menu}->GetMenuItems;
  325. $self->on_undo_redo_stacks_changed;
  326. }
  327. sub on_plater_selection_changed {
  328. my ($self, $have_selection) = @_;
  329. return if !defined $self->{object_menu};
  330. $self->{object_menu}->Enable($_->GetId, $have_selection)
  331. for $self->{object_menu}->GetMenuItems;
  332. $self->on_undo_redo_stacks_changed;
  333. }
  334. sub quick_slice {
  335. my $self = shift;
  336. my %params = @_;
  337. my $progress_dialog;
  338. eval {
  339. # validate configuration
  340. my $config = $self->{plater}->config;
  341. $config->validate;
  342. # select input file
  343. my $input_file;
  344. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory} || $Slic3r::GUI::Settings->{recent}{config_directory} || '';
  345. if (!$params{reslice}) {
  346. my $dialog = Wx::FileDialog->new($self, 'Choose a file to slice (STL/OBJ/AMF/3MF):', $dir, "", &Slic3r::GUI::MODEL_WILDCARD, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  347. if ($dialog->ShowModal != wxID_OK) {
  348. $dialog->Destroy;
  349. return;
  350. }
  351. $input_file = Slic3r::decode_path($dialog->GetPaths);
  352. $dialog->Destroy;
  353. $qs_last_input_file = $input_file unless $params{export_svg};
  354. } else {
  355. if (!defined $qs_last_input_file) {
  356. Wx::MessageDialog->new($self, "No previously sliced file.",
  357. 'Error', wxICON_ERROR | wxOK)->ShowModal();
  358. return;
  359. }
  360. if (! -e $qs_last_input_file) {
  361. Wx::MessageDialog->new($self, "Previously sliced file ($qs_last_input_file) not found.",
  362. 'File Not Found', wxICON_ERROR | wxOK)->ShowModal();
  363. return;
  364. }
  365. $input_file = $qs_last_input_file;
  366. }
  367. my $input_file_basename = basename($input_file);
  368. $Slic3r::GUI::Settings->{recent}{skein_directory} = dirname($input_file);
  369. wxTheApp->save_settings;
  370. my $print_center;
  371. {
  372. my $bed_shape = Slic3r::Polygon->new_scale(@{$config->bed_shape});
  373. $print_center = Slic3r::Pointf->new_unscale(@{$bed_shape->bounding_box->center});
  374. }
  375. my $sprint = Slic3r::Print::Simple->new(
  376. print_center => $print_center,
  377. status_cb => sub {
  378. my ($percent, $message) = @_;
  379. return if &Wx::wxVERSION_STRING !~ / 2\.(8\.|9\.[2-9])/;
  380. $progress_dialog->Update($percent, "$message…");
  381. },
  382. );
  383. # keep model around
  384. my $model = Slic3r::Model->read_from_file($input_file);
  385. $sprint->apply_config($config);
  386. $sprint->set_model($model);
  387. # FIXME: populate placeholders (preset names etc.)
  388. # select output file
  389. my $output_file;
  390. if ($params{reslice}) {
  391. $output_file = $qs_last_output_file if defined $qs_last_output_file;
  392. } elsif ($params{save_as}) {
  393. $output_file = $sprint->output_filepath;
  394. $output_file =~ s/\.gcode$/.svg/i if $params{export_svg};
  395. my $dlg = Wx::FileDialog->new($self, 'Save ' . ($params{export_svg} ? 'SVG' : 'G-code') . ' file as:',
  396. wxTheApp->output_path(dirname($output_file)),
  397. basename($output_file), $params{export_svg} ? &Slic3r::GUI::FILE_WILDCARDS->{svg} : &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  398. if ($dlg->ShowModal != wxID_OK) {
  399. $dlg->Destroy;
  400. return;
  401. }
  402. $output_file = Slic3r::decode_path($dlg->GetPath);
  403. $qs_last_output_file = $output_file unless $params{export_svg};
  404. $Slic3r::GUI::Settings->{_}{last_output_path} = dirname($output_file);
  405. wxTheApp->save_settings;
  406. $dlg->Destroy;
  407. }
  408. # show processbar dialog
  409. $progress_dialog = Wx::ProgressDialog->new('Slicing…', "Processing $input_file_basename…",
  410. 100, $self, 0);
  411. $progress_dialog->Pulse;
  412. {
  413. my @warnings = ();
  414. local $SIG{__WARN__} = sub { push @warnings, $_[0] };
  415. $sprint->output_file($output_file);
  416. if ($params{export_svg}) {
  417. $sprint->export_svg;
  418. } else {
  419. $sprint->export_gcode;
  420. }
  421. $sprint->status_cb(undef);
  422. Slic3r::GUI::warning_catcher($self)->($_) for @warnings;
  423. }
  424. $progress_dialog->Destroy;
  425. undef $progress_dialog;
  426. my $message = "$input_file_basename was successfully sliced.";
  427. wxTheApp->notify($message);
  428. Wx::MessageDialog->new($self, $message, 'Slicing Done!',
  429. wxOK | wxICON_INFORMATION)->ShowModal;
  430. };
  431. Slic3r::GUI::catch_error($self, sub { $progress_dialog->Destroy if $progress_dialog });
  432. }
  433. sub repair_stl {
  434. my $self = shift;
  435. my $input_file;
  436. {
  437. my $dir = $Slic3r::GUI::Settings->{recent}{skein_directory} || $Slic3r::GUI::Settings->{recent}{config_directory} || '';
  438. my $dialog = Wx::FileDialog->new($self, 'Select the STL file to repair:', $dir, "", &Slic3r::GUI::FILE_WILDCARDS->{stl}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  439. if ($dialog->ShowModal != wxID_OK) {
  440. $dialog->Destroy;
  441. return;
  442. }
  443. $input_file = Slic3r::decode_path($dialog->GetPaths);
  444. $dialog->Destroy;
  445. }
  446. my $output_file = $input_file;
  447. {
  448. $output_file =~ s/\.stl$/_fixed.obj/i;
  449. my $dlg = Wx::FileDialog->new($self, "Save OBJ file (less prone to coordinate errors than STL) as:", dirname($output_file),
  450. basename($output_file), &Slic3r::GUI::FILE_WILDCARDS->{obj}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  451. if ($dlg->ShowModal != wxID_OK) {
  452. $dlg->Destroy;
  453. return undef;
  454. }
  455. $output_file = Slic3r::decode_path($dlg->GetPath);
  456. $dlg->Destroy;
  457. }
  458. my $tmesh = Slic3r::TriangleMesh->new;
  459. $tmesh->ReadSTLFile($input_file);
  460. $tmesh->repair;
  461. $tmesh->WriteOBJFile($output_file);
  462. Slic3r::GUI::show_info($self, "Your file was repaired.", "Repair");
  463. }
  464. sub export_config {
  465. my $self = shift;
  466. my $config = $self->{plater}->config;
  467. eval {
  468. # validate configuration
  469. $config->validate;
  470. };
  471. Slic3r::GUI::catch_error($self) and return;
  472. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  473. my $filename = $last_config ? basename($last_config) : "config.ini";
  474. my $dlg = Wx::FileDialog->new($self, 'Save configuration as:', $dir, $filename,
  475. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  476. if ($dlg->ShowModal == wxID_OK) {
  477. my $file = Slic3r::decode_path($dlg->GetPath);
  478. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  479. wxTheApp->save_settings;
  480. $last_config = $file;
  481. $config->save($file);
  482. }
  483. $dlg->Destroy;
  484. }
  485. sub load_config_file {
  486. my $self = shift;
  487. my ($file) = @_;
  488. if (!$file) {
  489. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  490. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
  491. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  492. return unless $dlg->ShowModal == wxID_OK;
  493. $file = Slic3r::decode_path($dlg->GetPaths);
  494. $dlg->Destroy;
  495. }
  496. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  497. wxTheApp->save_settings;
  498. $last_config = $file;
  499. my $name = wxTheApp->add_external_preset($file);
  500. $self->{plater}->load_presets;
  501. $self->{plater}->select_preset_by_name($name, $_) for qw(print filament printer);
  502. }
  503. sub import_fromGCode{ # import configuration from gcode file sliced with Slic3r
  504. my ($self, $file) = @_;
  505. if (!$file) {
  506. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  507. my $dlg = Wx::FileDialog->new($self, 'Select GCode File to load config from:', $dir, &Slic3r::GUI::FILE_WILDCARDS->{gcode}, &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  508. return unless $dlg->ShowModal == wxID_OK;
  509. $file = Slic3r::decode_path($dlg->GetPaths);
  510. $dlg->Destroy;
  511. }
  512. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  513. wxTheApp->save_settings;
  514. # open $file and read the first line to make sure it was sliced using Slic3r
  515. open(FILEFIRSTLINE, '<', "$file")
  516. or die( "Can't open file to import gcode: $!" );
  517. my $firstLine = <FILEFIRSTLINE>;
  518. close (FILEFIRSTLINE);
  519. # Exit, if file was not sliced using Slic3r
  520. if( index($firstLine, "generated by Slic3r") < 0){
  521. return;
  522. }
  523. # if file sliced by Slic3r, read it
  524. open(GFILE, "$file")
  525. or die( "Can't open file to import gcode: $!" );
  526. my @lines = reverse <GFILE>; # read the file from the back
  527. my $line;
  528. my @settinglines="";
  529. foreach $line (@lines) {
  530. # if line is empty, we're done -> EOF
  531. if (substr($line, 0, 1) eq ";"){
  532. $line = substr($line,2, length($line)-2);
  533. push @settinglines, $line;
  534. } else {
  535. last;
  536. }
  537. }
  538. close (GFILE);
  539. # (over-) write config to temp-file -> <gcode-filename.ini>
  540. my $tempfile = substr $file, 0, rindex( $file, q{.} );
  541. $tempfile="$tempfile.ini";
  542. open (TEMPFILESETTINGS, "> $tempfile");
  543. print TEMPFILESETTINGS @settinglines;
  544. close (TEMPFILESETTINGS);
  545. $self->load_config_file($tempfile);
  546. # todo: do we want to delete the <gcode-filename.ini> file after load_config?
  547. }
  548. sub export_configbundle {
  549. my $self = shift;
  550. eval {
  551. # validate current configuration in case it's dirty
  552. $self->{plater}->config->validate;
  553. };
  554. Slic3r::GUI::catch_error($self) and return;
  555. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  556. my $filename = "Slic3r_config_bundle.ini";
  557. my $dlg = Wx::FileDialog->new($self, 'Save presets bundle as:', $dir, $filename,
  558. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
  559. if ($dlg->ShowModal == wxID_OK) {
  560. my $file = Slic3r::decode_path($dlg->GetPath);
  561. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  562. wxTheApp->save_settings;
  563. # leave default category empty to prevent the bundle from being parsed as a normal config file
  564. my $ini = { _ => {} };
  565. $ini->{settings}{$_} = $Slic3r::GUI::Settings->{_}{$_} for qw(autocenter);
  566. $ini->{presets} = $Slic3r::GUI::Settings->{presets};
  567. foreach my $section (qw(print filament printer)) {
  568. my @presets = @{wxTheApp->presets->{$section}};
  569. foreach my $preset (@presets) {
  570. next if $preset->default || $preset->external;
  571. $ini->{"$section:" . $preset->name} = $preset->load_config->as_ini->{_};
  572. }
  573. }
  574. Slic3r::Config->write_ini($file, $ini);
  575. }
  576. $dlg->Destroy;
  577. }
  578. sub load_configbundle {
  579. my ($self, $file, $skip_no_id) = @_;
  580. if (!$file) {
  581. my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
  582. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
  583. &Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  584. return unless $dlg->ShowModal == wxID_OK;
  585. $file = Slic3r::decode_path($dlg->GetPaths);
  586. $dlg->Destroy;
  587. }
  588. $Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
  589. wxTheApp->save_settings;
  590. # load .ini file
  591. my $ini = Slic3r::Config->read_ini($file);
  592. if ($ini->{settings}) {
  593. $Slic3r::GUI::Settings->{_}{$_} = $ini->{settings}{$_} for keys %{$ini->{settings}};
  594. wxTheApp->save_settings;
  595. }
  596. if ($ini->{presets}) {
  597. $Slic3r::GUI::Settings->{presets} = $ini->{presets};
  598. wxTheApp->save_settings;
  599. }
  600. my $imported = 0;
  601. INI_BLOCK: foreach my $ini_category (sort keys %$ini) {
  602. next unless $ini_category =~ /^(print|filament|printer):(.+)$/;
  603. my ($section, $preset_name) = ($1, $2);
  604. my $config = Slic3r::Config->load_ini_hash($ini->{$ini_category});
  605. next if $skip_no_id && !$config->get($section . "_settings_id");
  606. {
  607. my @current_presets = @{wxTheApp->presets->{$section}};
  608. my %current_ids = map { $_ => 1 }
  609. grep $_,
  610. map $_->dirty_config->get($section . "_settings_id"),
  611. @current_presets;
  612. next INI_BLOCK if exists $current_ids{$config->get($section . "_settings_id")};
  613. }
  614. $config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $section, $preset_name);
  615. Slic3r::debugf "Imported %s preset %s\n", $section, $preset_name;
  616. $imported++;
  617. }
  618. $self->{plater}->load_presets;
  619. return if !$imported;
  620. my $message = sprintf "%d presets successfully imported.", $imported;
  621. Slic3r::GUI::show_info($self, $message);
  622. }
  623. sub load_config {
  624. my ($self, $config) = @_;
  625. $self->{plater}->load_config($config);
  626. }
  627. sub config_wizard {
  628. my $self = shift;
  629. if (my $config = Slic3r::GUI::ConfigWizard->new($self)->run) {
  630. foreach my $group (qw(print filament printer)) {
  631. my $name = 'My Settings';
  632. $config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $group, $name);
  633. $Slic3r::GUI::Settings->{presets}{$group} = "$name.ini";
  634. $self->{plater}->load_presets;
  635. $self->{plater}->select_preset_by_name($name, $group);
  636. }
  637. }
  638. }
  639. sub select_tab {
  640. my ($self, $tab) = @_;
  641. $self->{tabpanel}->SetSelection($tab);
  642. }
  643. # Set a camera direction, zoom to all objects.
  644. sub select_view {
  645. my ($self, $direction) = @_;
  646. $self->{plater}->select_view($direction);
  647. }
  648. 1;