From 2d366c4852db38ea021f021a12e634f1eeb0854d Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Wed, 28 Aug 2013 20:13:18 +0200 Subject: [PATCH] Refactor E code and keep track of per-extruder consumend filament --- lib/Slic3r/Extruder.pm | 17 ++++++++++++++++- lib/Slic3r/GCode.pm | 19 ++++--------------- lib/Slic3r/GUI/Plater.pm | 1 + lib/Slic3r/Print.pm | 21 ++++++++------------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/Slic3r/Extruder.pm b/lib/Slic3r/Extruder.pm index 91c6314cb..1171b766f 100644 --- a/lib/Slic3r/Extruder.pm +++ b/lib/Slic3r/Extruder.pm @@ -12,9 +12,11 @@ use constant OPTIONS => [qw( has 'id' => (is => 'rw', required => 1); has $_ => (is => 'ro', required => 1) for @{&OPTIONS}; +has 'config'=> (is => 'ro', required => 1); has 'bridge_flow' => (is => 'lazy'); -has 'e' => (is => 'rw', default => sub {0} ); +has 'E' => (is => 'rw', default => sub {0} ); +has 'absolute_E' => (is => 'rw', default => sub {0} ); has 'retracted' => (is => 'rw', default => sub {0} ); has 'restart_extra' => (is => 'rw', default => sub {0} ); has 'e_per_mm3' => (is => 'lazy'); @@ -44,6 +46,19 @@ sub _build_scaled_wipe_distance { return scale($self->retract_length / $self->retract_speed * $Slic3r::Config->travel_speed * 0.8); } +sub extrude { + my ($self, $E) = @_; + + $self->E(0) if $self->config->use_relative_e_distances; + $self->absolute_E($self->absolute_E + $E); + return $self->E($self->E + $E); +} + +sub extruded_volume { + my ($self) = @_; + return $self->absolute_E * ($self->filament_diameter**2) * PI/4; +} + sub make_flow { my $self = shift; return Slic3r::Flow->new(nozzle_diameter => $self->nozzle_diameter, @_); diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index 3093d1439..f01013fd7 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -29,7 +29,6 @@ has 'new_object' => (is => 'rw', default => sub {0}); has 'straight_once' => (is => 'rw', default => sub {1}); has 'extruder' => (is => 'rw'); has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds -has 'total_extrusion_length' => (is => 'rw', default => sub {0} ); has 'lifted' => (is => 'rw', default => sub {0} ); has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } ); has 'last_speed' => (is => 'rw', default => sub {""}); @@ -330,12 +329,7 @@ sub extrude_path { # calculate extrusion length for this line my $E = 0; - if ($e) { - $E = $e * $line_length; - $self->extruder->e(0) if $self->config->use_relative_e_distances; - $self->total_extrusion_length($self->total_extrusion_length + $E); - $E = $self->extruder->e($self->extruder->e + $E); - } + $E = $self->extruder->extrude($e * $line_length) if $e; # compose G-code line $gcode .= sprintf "G1 X%.3f Y%.3f", @@ -557,11 +551,9 @@ sub unretract { if ($to_unretract) { $self->speed('retract'); if ($self->config->extrusion_axis) { - $self->extruder->e(0) if $self->config->use_relative_e_distances; - $self->total_extrusion_length($self->total_extrusion_length + $to_unretract); # use G1 instead of G0 because G0 will blend the restart with the previous travel move $gcode .= sprintf "G1 E%.5f F%.3f", - $self->extruder->e($self->extruder->e + $to_unretract), + $self->extruder->extrude($to_unretract), $self->extruder->retract_speed_mm_min; $gcode .= " ; compensate retraction" if $self->config->gcode_comments; $gcode .= "\n"; @@ -577,7 +569,7 @@ sub reset_e { my ($self) = @_; return "" if $self->config->gcode_flavor =~ /^(?:mach3|makerware|sailfish)$/; - $self->extruder->e(0) if $self->extruder; + $self->extruder->E(0) if $self->extruder; return sprintf "G92 %s0%s\n", $self->config->extrusion_axis, ($self->config->gcode_comments ? ' ; reset extrusion distance' : '') if $self->config->extrusion_axis && !$self->config->use_relative_e_distances; } @@ -630,10 +622,7 @@ sub _Gx { # output extrusion distance if ($e && $self->config->extrusion_axis) { - $self->extruder->e(0) if $self->config->use_relative_e_distances; - $self->extruder->e($self->extruder->e + $e); - $self->total_extrusion_length($self->total_extrusion_length + $e); - $gcode .= sprintf " %s%.5f", $self->config->extrusion_axis, $self->extruder->e; + $gcode .= sprintf " %s%.5f", $self->config->extrusion_axis, $self->extruder->extrude($e); } $gcode .= " ; $comment" if $comment && $self->config->gcode_comments; diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index c58590193..ad9843fd3 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -696,6 +696,7 @@ sub export_gcode2 { my %params = ( output_file => $output_file, status_cb => sub { $params{progressbar}->(@_) }, + quiet => 1, ); if ($params{export_svg}) { $print->export_svg(%params); diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 390823727..122053091 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -15,7 +15,6 @@ 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 'total_extrusion_length' => (is => 'rw'); has 'processing_time' => (is => 'rw'); has 'extruders' => (is => 'rw', default => sub {[]}); has 'regions' => (is => 'rw', default => sub {[]}); @@ -236,6 +235,7 @@ sub init_extruders { ); for my $extruder_id (keys %{{ map {$_ => 1} @used_extruders }}) { $self->extruders->[$extruder_id] = Slic3r::Extruder->new( + config => $self->config, id => $extruder_id, map { $_ => $self->config->get($_)->[$extruder_id] // $self->config->get($_)->[0] } #/ @{&Slic3r::Extruder::OPTIONS} @@ -484,8 +484,9 @@ sub export_gcode { $self->processing_time - int($self->processing_time/60)*60; # TODO: more statistics! - printf "Filament required: %.1fmm (%.1fcm3)\n", - $self->total_extrusion_length, $self->total_extrusion_volume; + print map sprintf("Filament required: %.1fmm (%.1fcm3)\n", + $_->absolute_E, $_->extruded_volume/1000), + @{$self->extruders}; } } @@ -895,16 +896,15 @@ sub write_gcode { print $fh $buffer->flush; } - # save statistic data - $self->total_extrusion_length($gcodegen->total_extrusion_length); - # write end commands to file print $fh $gcodegen->retract if $gcodegen->extruder; # empty prints don't even set an extruder print $fh $gcodegen->set_fan(0); printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->end_gcode); - printf $fh "; filament used = %.1fmm (%.1fcm3)\n", - $self->total_extrusion_length, $self->total_extrusion_volume; + foreach my $extruder (@{$self->extruders}) { + printf $fh "; filament used = %.1fmm (%.1fcm3)\n", + $extruder->absolute_E, $extruder->extruded_volume/1000; + } if ($Slic3r::Config->gcode_comments) { # append full config @@ -920,11 +920,6 @@ sub write_gcode { close $fh; } -sub total_extrusion_volume { - my $self = shift; - return $self->total_extrusion_length * ($self->extruders->[0]->filament_diameter**2) * PI/4 / 1000; -} - # this method will return the supplied input file path after expanding its # format variables with their values sub expanded_output_filepath {