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