SkeinPanel.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package Slic3r::GUI::SkeinPanel;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use File::Basename qw(basename dirname);
  6. use Slic3r::Geometry qw(X Y);
  7. use Wx qw(:sizer :progressdialog wxOK wxICON_INFORMATION wxICON_WARNING wxICON_ERROR wxICON_QUESTION
  8. wxOK wxCANCEL wxID_OK wxFD_OPEN wxFD_SAVE wxDEFAULT wxNORMAL);
  9. use Wx::Event qw(EVT_BUTTON);
  10. use base 'Wx::Panel';
  11. our $last_skein_dir;
  12. our $last_config_dir;
  13. our $last_input_file;
  14. our $last_output_file;
  15. our $last_config;
  16. sub new {
  17. my $class = shift;
  18. my ($parent) = @_;
  19. my $self = $class->SUPER::new($parent, -1);
  20. no warnings 'qw';
  21. my %panels = (
  22. printer => {
  23. title => 'Printer',
  24. options => [qw(nozzle_diameter#0 bed_size print_center z_offset gcode_flavor use_relative_e_distances)],
  25. },
  26. filament => {
  27. title => 'Filament',
  28. options => [qw(filament_diameter#0 extrusion_multiplier#0 temperature#0 first_layer_temperature#0 bed_temperature first_layer_bed_temperature)],
  29. },
  30. print_speed => {
  31. title => 'Print speed',
  32. options => [qw(perimeter_speed small_perimeter_speed external_perimeter_speed infill_speed solid_infill_speed top_solid_infill_speed bridge_speed)],
  33. },
  34. speed => {
  35. title => 'Other speed settings',
  36. options => [qw(travel_speed first_layer_speed)],
  37. },
  38. accuracy => {
  39. title => 'Accuracy',
  40. options => [qw(layer_height first_layer_height infill_every_layers)],
  41. },
  42. print => {
  43. title => 'Print settings',
  44. options => [qw(perimeters solid_layers fill_density fill_angle fill_pattern solid_fill_pattern randomize_start)],
  45. },
  46. retract => {
  47. title => 'Retraction',
  48. options => [qw(retract_length retract_lift retract_speed retract_restart_extra retract_before_travel)],
  49. },
  50. cooling => {
  51. title => 'Cooling',
  52. options => [qw(cooling min_fan_speed max_fan_speed bridge_fan_speed fan_below_layer_time slowdown_below_layer_time min_print_speed disable_fan_first_layers fan_always_on)],
  53. label_width => 450,
  54. },
  55. skirt => {
  56. title => 'Skirt',
  57. options => [qw(skirts skirt_distance skirt_height brim_width)],
  58. },
  59. gcode => {
  60. title => 'G-code',
  61. options => [qw(start_gcode end_gcode layer_gcode gcode_comments post_process)],
  62. label_width => 260,
  63. },
  64. sequential_printing => {
  65. title => 'Sequential printing',
  66. options => [qw(complete_objects extruder_clearance_radius extruder_clearance_height)],
  67. },
  68. extrusion => {
  69. title => 'Extrusion',
  70. options => [qw(extrusion_width first_layer_extrusion_width perimeters_extrusion_width infill_extrusion_width support_material_extrusion_width bridge_flow_ratio)],
  71. },
  72. output => {
  73. title => 'Output',
  74. options => [qw(output_filename_format duplicate_distance)],
  75. },
  76. other => {
  77. title => 'Other',
  78. options => [ ($Slic3r::have_threads ? qw(threads) : ()), qw(extra_perimeters) ],
  79. },
  80. notes => {
  81. title => 'Notes',
  82. options => [qw(notes)],
  83. },
  84. support_material => {
  85. title => 'Support material',
  86. options => [qw(support_material support_material_threshold support_material_pattern support_material_spacing support_material_angle support_material_extruder)],
  87. },
  88. );
  89. $self->{panels} = \%panels;
  90. my $tabpanel = Wx::Notebook->new($self, -1, Wx::wxDefaultPosition, Wx::wxDefaultSize, &Wx::wxNB_TOP);
  91. my $make_tab = sub {
  92. my @cols = @_;
  93. my $tab = Wx::Panel->new($tabpanel, -1);
  94. my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  95. foreach my $col (@cols) {
  96. my $vertical_sizer = Wx::BoxSizer->new(wxVERTICAL);
  97. for my $optgroup (@$col) {
  98. next unless @{ $panels{$optgroup}{options} };
  99. my $optpanel = Slic3r::GUI::OptionsGroup->new($tab, %{$panels{$optgroup}});
  100. $vertical_sizer->Add($optpanel, 0, wxEXPAND | wxALL, 10);
  101. }
  102. $sizer->Add($vertical_sizer);
  103. }
  104. $tab->SetSizer($sizer);
  105. return $tab;
  106. };
  107. my @tabs = (
  108. $make_tab->([qw(accuracy skirt support_material)], [qw(print retract)]),
  109. $make_tab->([qw(cooling notes)]),
  110. $make_tab->([qw(printer filament)], [qw(print_speed speed)]),
  111. $make_tab->([qw(gcode)]),
  112. $make_tab->([qw(extrusion)], [qw(output other sequential_printing)]),
  113. );
  114. $tabpanel->AddPage(Slic3r::GUI::Plater->new($tabpanel), "Plater");
  115. $tabpanel->AddPage($tabs[0], "Print Settings");
  116. $tabpanel->AddPage($tabs[1], "Cooling");
  117. $tabpanel->AddPage($tabs[2], "Printer and Filament");
  118. $tabpanel->AddPage($tabs[3], "G-code");
  119. $tabpanel->AddPage($tabs[4], "Advanced");
  120. my $buttons_sizer;
  121. {
  122. $buttons_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  123. my $slice_button = Wx::Button->new($self, -1, "Quick slice…");
  124. $slice_button->SetDefault();
  125. $buttons_sizer->Add($slice_button, 0, wxRIGHT, 20);
  126. EVT_BUTTON($self, $slice_button, sub { $self->do_slice });
  127. my $save_button = Wx::Button->new($self, -1, "Save config...");
  128. $buttons_sizer->Add($save_button, 0, wxRIGHT, 5);
  129. EVT_BUTTON($self, $save_button, sub { $self->save_config });
  130. my $load_button = Wx::Button->new($self, -1, "Load config...");
  131. $buttons_sizer->Add($load_button, 0, wxRIGHT, 5);
  132. EVT_BUTTON($self, $load_button, sub { $self->load_config });
  133. my $text = Wx::StaticText->new($self, -1, "Remember to check for updates at http://slic3r.org/\nVersion: $Slic3r::VERSION", Wx::wxDefaultPosition, Wx::wxDefaultSize, wxALIGN_RIGHT);
  134. my $font = Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL);
  135. $text->SetFont($font);
  136. $buttons_sizer->Add($text, 1, wxEXPAND | wxALIGN_RIGHT);
  137. }
  138. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  139. $sizer->Add($buttons_sizer, 0, wxEXPAND | wxALL, 10);
  140. $sizer->Add($tabpanel);
  141. $sizer->SetSizeHints($self);
  142. $self->SetSizer($sizer);
  143. $self->Layout;
  144. $_->() for @Slic3r::GUI::OptionsGroup::reload_callbacks;
  145. return $self;
  146. }
  147. our $model_wildcard = "STL files (*.stl)|*.stl;*.STL|OBJ files (*.obj)|*.obj;*.OBJ|AMF files (*.amf)|*.amf;*.AMF;*.xml;*.XML";
  148. our $ini_wildcard = "INI files *.ini|*.ini;*.INI";
  149. our $gcode_wildcard = "G-code files *.gcode|*.gcode;*.GCODE";
  150. our $svg_wildcard = "SVG files *.svg|*.svg;*.SVG";
  151. sub do_slice {
  152. my $self = shift;
  153. my %params = @_;
  154. my $process_dialog;
  155. eval {
  156. # validate configuration
  157. Slic3r::Config->validate;
  158. # confirm slicing of more than one copies
  159. my $copies = $Slic3r::duplicate_grid->[X] * $Slic3r::duplicate_grid->[Y];
  160. $copies = $Slic3r::duplicate if $Slic3r::duplicate > 1;
  161. if ($copies > 1) {
  162. my $confirmation = Wx::MessageDialog->new($self, "Are you sure you want to slice $copies copies?",
  163. 'Confirm', wxICON_QUESTION | wxOK | wxCANCEL);
  164. return unless $confirmation->ShowModal == wxID_OK;
  165. }
  166. # select input file
  167. my $dir = $last_skein_dir || $last_config_dir || "";
  168. my $input_file;
  169. if (!$params{reslice}) {
  170. my $dialog = Wx::FileDialog->new($self, 'Choose a file to slice (STL/OBJ/AMF):', $dir, "", $model_wildcard, wxFD_OPEN | &Wx::wxFD_FILE_MUST_EXIST);
  171. if ($dialog->ShowModal != wxID_OK) {
  172. $dialog->Destroy;
  173. return;
  174. }
  175. $input_file = $dialog->GetPaths;
  176. $dialog->Destroy;
  177. $last_input_file = $input_file;
  178. } else {
  179. if (!defined $last_input_file) {
  180. Wx::MessageDialog->new($self, "No previously sliced file",
  181. 'Confirm', wxICON_ERROR | wxOK)->ShowModal();
  182. return;
  183. }
  184. if (! -e $last_input_file) {
  185. Wx::MessageDialog->new($self, "Cannot find previously sliced file!",
  186. 'Confirm', wxICON_ERROR | wxOK)->ShowModal();
  187. return;
  188. }
  189. $input_file = $last_input_file;
  190. }
  191. my $input_file_basename = basename($input_file);
  192. $last_skein_dir = dirname($input_file);
  193. my $print = Slic3r::Print->new;
  194. $print->add_object_from_file($input_file);
  195. $print->validate;
  196. # select output file
  197. my $output_file = $main::opt{output};
  198. if ($params{reslice}) {
  199. $output_file = $last_output_file if defined $last_output_file;
  200. } elsif ($params{save_as}) {
  201. $output_file = $print->expanded_output_filepath($output_file);
  202. $output_file =~ s/\.gcode$/.svg/i if $params{export_svg};
  203. my $dlg = Wx::FileDialog->new($self, 'Save ' . ($params{export_svg} ? 'SVG' : 'G-code') . ' file as:', dirname($output_file),
  204. basename($output_file), $params{export_svg} ? $svg_wildcard : $gcode_wildcard, wxFD_SAVE);
  205. if ($dlg->ShowModal != wxID_OK) {
  206. $dlg->Destroy;
  207. return;
  208. }
  209. $output_file = $last_output_file = $dlg->GetPath;
  210. $dlg->Destroy;
  211. }
  212. # show processbar dialog
  213. $process_dialog = Wx::ProgressDialog->new('Slicing...', "Processing $input_file_basename...",
  214. 100, $self, 0);
  215. $process_dialog->Pulse;
  216. {
  217. my @warnings = ();
  218. local $SIG{__WARN__} = sub { push @warnings, $_[0] };
  219. my %export_params = (
  220. output_file => $output_file,
  221. status_cb => sub {
  222. my ($percent, $message) = @_;
  223. if (&Wx::wxVERSION_STRING =~ / 2\.(8\.|9\.[2-9])/) {
  224. $process_dialog->Update($percent, "$message...");
  225. }
  226. },
  227. );
  228. if ($params{export_svg}) {
  229. $print->export_svg(%export_params);
  230. } else {
  231. $print->export_gcode(%export_params);
  232. }
  233. Slic3r::GUI::warning_catcher($self)->($_) for @warnings;
  234. }
  235. $process_dialog->Destroy;
  236. undef $process_dialog;
  237. my $message = "$input_file_basename was successfully sliced";
  238. $message .= sprintf " in %d minutes and %.3f seconds",
  239. int($print->processing_time/60),
  240. $print->processing_time - int($print->processing_time/60)*60
  241. if $print->processing_time;
  242. $message .= ".";
  243. Slic3r::GUI::notify($message);
  244. Wx::MessageDialog->new($self, $message, 'Done!',
  245. wxOK | wxICON_INFORMATION)->ShowModal;
  246. };
  247. Slic3r::GUI::catch_error($self, sub { $process_dialog->Destroy if $process_dialog });
  248. }
  249. sub save_config {
  250. my $self = shift;
  251. my $process_dialog;
  252. eval {
  253. # validate configuration
  254. Slic3r::Config->validate;
  255. };
  256. Slic3r::GUI::catch_error($self, sub { $process_dialog->Destroy if $process_dialog }) and return;
  257. my $dir = $last_config ? dirname($last_config) : $last_config_dir || $last_skein_dir || "";
  258. my $filename = $last_config ? basename($last_config) : "config.ini";
  259. my $dlg = Wx::FileDialog->new($self, 'Save configuration as:', $dir, $filename,
  260. $ini_wildcard, wxFD_SAVE | &Wx::wxFD_OVERWRITE_PROMPT);
  261. if ($dlg->ShowModal == wxID_OK) {
  262. my $file = $dlg->GetPath;
  263. $last_config_dir = dirname($file);
  264. $last_config = $file;
  265. Slic3r::Config->save($file);
  266. }
  267. $dlg->Destroy;
  268. }
  269. sub load_config {
  270. my $self = shift;
  271. my $dir = $last_config ? dirname($last_config) : $last_config_dir || $last_skein_dir || "";
  272. my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
  273. $ini_wildcard, wxFD_OPEN | &Wx::wxFD_FILE_MUST_EXIST);
  274. if ($dlg->ShowModal == wxID_OK) {
  275. my ($file) = $dlg->GetPaths;
  276. $last_config_dir = dirname($file);
  277. $last_config = $file;
  278. eval {
  279. local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
  280. Slic3r::Config->load($file);
  281. };
  282. Slic3r::GUI::catch_error($self);
  283. $_->() for @Slic3r::GUI::OptionsGroup::reload_callbacks;
  284. }
  285. $dlg->Destroy;
  286. }
  287. 1;