From 02df73c94bf36b8e327b6121dab8e7d797176ded Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 16 Dec 2013 00:54:59 +0100 Subject: [PATCH] More refactoring to clean up the Print object API --- lib/Slic3r/GUI/Plater.pm | 21 ++++++--------------- lib/Slic3r/GUI/SkeinPanel.pm | 23 +++++++++-------------- lib/Slic3r/Print.pm | 31 +++++++++++-------------------- lib/Slic3r/Test.pm | 1 + slic3r.pl | 28 ++++++++++++++++++++-------- 5 files changed, 47 insertions(+), 57 deletions(-) diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 7368baff..3b74e2a9 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -741,30 +741,20 @@ sub export_gcode2 { my @warnings = (); local $SIG{__WARN__} = sub { push @warnings, $_[0] }; - my %params = ( - output_file => $output_file, - status_cb => sub { $params{progressbar}->(@_) }, - quiet => 1, - ); + $print->status_cb(sub { $params{progressbar}->(@_) }); if ($params{export_svg}) { $print->export_svg(%params); } else { + $print->process; $print->export_gcode(%params); } + $print->status_cb(undef); Slic3r::GUI::warning_catcher($self, $Slic3r::have_threads ? sub { Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $MESSAGE_DIALOG_EVENT, shared_clone([@_]))); } : undef)->($_) for @warnings; } - my $message = "Your files were successfully sliced"; - if ($print->processing_time) { - $message .= ' in'; - my $minutes = int($print->processing_time/60); - $message .= sprintf " %d minutes and", $minutes if $minutes; - $message .= sprintf " %.1f seconds", $print->processing_time - $minutes*60; - } - $message .= "."; - $params{on_completed}->($message); + $params{on_completed}->(); }; $params{catch_error}->(); } @@ -777,7 +767,8 @@ sub on_export_completed { $self->{export_thread} = undef; $self->statusbar->SetCancelCallback(undef); $self->statusbar->StopBusy; - $self->statusbar->SetStatusText("G-code file exported to $self->{output_file}"); + my $message = "G-code file exported to $self->{output_file}"; + $self->statusbar->SetStatusText($message); &Wx::wxTheApp->notify($message); } diff --git a/lib/Slic3r/GUI/SkeinPanel.pm b/lib/Slic3r/GUI/SkeinPanel.pm index b52ba342..8dc1df82 100644 --- a/lib/Slic3r/GUI/SkeinPanel.pm +++ b/lib/Slic3r/GUI/SkeinPanel.pm @@ -179,31 +179,26 @@ sub quick_slice { local $SIG{__WARN__} = sub { push @warnings, $_[0] }; my %export_params = ( output_file => $output_file, - status_cb => sub { - my ($percent, $message) = @_; - if (&Wx::wxVERSION_STRING =~ / 2\.(8\.|9\.[2-9])/) { - $process_dialog->Update($percent, "$messageā€¦"); - } - }, ); + $print->status_cb(sub { + my ($percent, $message) = @_; + if (&Wx::wxVERSION_STRING =~ / 2\.(8\.|9\.[2-9])/) { + $process_dialog->Update($percent, "$messageā€¦"); + } + }); if ($params{export_svg}) { $print->export_svg(%export_params); } else { + $print->process; $print->export_gcode(%export_params); } + $print->status_cb(undef); Slic3r::GUI::warning_catcher($self)->($_) for @warnings; } $process_dialog->Destroy; undef $process_dialog; - my $message = "$input_file_basename was successfully sliced"; - if ($print->processing_time) { - $message .= ' in'; - my $minutes = int($print->processing_time/60); - $message .= sprintf " %d minutes and", $minutes if $minutes; - $message .= sprintf " %.1f seconds", $print->processing_time - $minutes*60; - } - $message .= "."; + my $message = "$input_file_basename was successfully sliced."; &Wx::wxTheApp->notify($message); Wx::MessageDialog->new($self, $message, 'Slicing Done!', wxOK | wxICON_INFORMATION)->ShowModal; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index ea875b9f..8fb1c667 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -9,12 +9,11 @@ use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN MAX PI scale unscale move_points c convex_hull); use Slic3r::Geometry::Clipper qw(diff_ex union_ex union_pt intersection_ex intersection offset offset2 union_pt_chained JT_ROUND JT_SQUARE); -use Time::HiRes qw(gettimeofday tv_interval); has 'config' => (is => 'rw', default => sub { Slic3r::Config->new_from_defaults }, trigger => 1); has 'extra_variables' => (is => 'rw', default => sub {{}}); has 'objects' => (is => 'rw', default => sub {[]}); -has 'processing_time' => (is => 'rw'); +has 'status_cb' => (is => 'rw'); has 'extruders' => (is => 'rw', default => sub {[]}); has 'regions' => (is => 'rw', default => sub {[]}); has 'support_material_flow' => (is => 'rw'); @@ -304,13 +303,11 @@ sub _simplify_slices { } } -sub export_gcode { - my $self = shift; - my %params = @_; +sub process { + my ($self) = @_; $self->init_extruders; - my $status_cb = $params{status_cb} || sub {}; - my $t0 = [gettimeofday]; + my $status_cb = $self->status_cb // sub {}; # skein the STL into layers # each layer has surfaces with holes @@ -427,6 +424,13 @@ sub export_gcode { eval "use Slic3r::Test::SectionCut"; Slic3r::Test::SectionCut->new(print => $self)->export_svg("section_cut.svg"); } +} + +sub export_gcode { + my $self = shift; + my %params = @_; + + my $status_cb = $self->status_cb // sub {}; # output everything to a G-code file my $output_file = $self->expanded_output_filepath($params{output_file}); @@ -442,19 +446,6 @@ sub export_gcode { system($_, $output_file); } } - - # output some statistics - unless ($params{quiet}) { - $self->processing_time(tv_interval($t0)); - printf "Done. Process took %d minutes and %.3f seconds\n", - int($self->processing_time/60), - $self->processing_time - int($self->processing_time/60)*60; - - # TODO: more statistics! - print map sprintf("Filament required: %.1fmm (%.1fcm3)\n", - $_->absolute_E, $_->extruded_volume/1000), - @{$self->extruders}; - } } sub export_svg { diff --git a/lib/Slic3r/Test.pm b/lib/Slic3r/Test.pm index c857f349..34e0e493 100644 --- a/lib/Slic3r/Test.pm +++ b/lib/Slic3r/Test.pm @@ -119,6 +119,7 @@ sub gcode { my ($print) = @_; my $fh = IO::Scalar->new(\my $gcode); + $print->process; $print->export_gcode(output_fh => $fh, quiet => 1); $fh->close; diff --git a/slic3r.pl b/slic3r.pl index f35c1ff2..00d99409 100755 --- a/slic3r.pl +++ b/slic3r.pl @@ -13,6 +13,7 @@ use List::Util qw(first); use POSIX qw(setlocale LC_NUMERIC); use Slic3r; use Slic3r::Geometry qw(X Y); +use Time::HiRes qw(gettimeofday tv_interval); $|++; our %opt = (); @@ -151,21 +152,32 @@ if (@ARGV) { # slicing from command line next; } - my $print = Slic3r::Print->new(config => $config); - $print->add_model_object($_) for @{$model->objects}; - undef $model; # free memory - $print->validate; - my %params = ( - output_file => $opt{output}, + my $print = Slic3r::Print->new( + config => $config, status_cb => sub { my ($percent, $message) = @_; printf "=> %s\n", $message; }, ); + $print->add_model_object($_) for @{$model->objects}; + undef $model; # free memory + $print->validate; if ($opt{export_svg}) { - $print->export_svg(%params); + $print->export_svg(output_file => $opt{output}); } else { - $print->export_gcode(%params); + my $t0 = [gettimeofday]; + $print->process; + $print->export_gcode(output_file => $opt{output}); + + # output some statistics + { + my $duration = tv_interval($t0); + printf "Done. Process took %d minutes and %.3f seconds\n", + int($duration/60), ($duration - int($duration/60)*60); # % truncates to integer + } + print map sprintf("Filament required: %.1fmm (%.1fcm3)\n", + $_->absolute_E, $_->extruded_volume/1000), + @{$print->extruders}; } } } else {