From 71ec90a1dd02241c38845fc3e693a0ba87555601 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Tue, 21 Oct 2014 20:36:52 +0200 Subject: [PATCH] Finish porting the Extruder class to libslic3r --- lib/Slic3r.pm | 1 - lib/Slic3r/Extruder.pm | 42 ----------------------------------- lib/Slic3r/Print.pm | 5 ++--- xs/src/libslic3r/Extruder.cpp | 25 +++++++++++++++++++++ xs/src/libslic3r/Extruder.hpp | 6 +++++ xs/xsp/Extruder.xsp | 7 ++++++ 6 files changed, 40 insertions(+), 46 deletions(-) delete mode 100644 lib/Slic3r/Extruder.pm diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 911df206d..fa1f3b987 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -38,7 +38,6 @@ use Moo 1.003001; use Slic3r::XS; # import all symbols (constants etc.) before they get parsed use Slic3r::Config; use Slic3r::ExPolygon; -use Slic3r::Extruder; use Slic3r::ExtrusionLoop; use Slic3r::ExtrusionPath; use Slic3r::ExtrusionPath::Collection; diff --git a/lib/Slic3r/Extruder.pm b/lib/Slic3r/Extruder.pm deleted file mode 100644 index 404dc14bc..000000000 --- a/lib/Slic3r/Extruder.pm +++ /dev/null @@ -1,42 +0,0 @@ -package Slic3r::Extruder; -use strict; -use warnings; - -require Exporter; -our @ISA = qw(Exporter); -our @EXPORT_OK = qw(EXTRUDER_ROLE_PERIMETER EXTRUDER_ROLE_INFILL EXTRUDER_ROLE_SUPPORT_MATERIAL - EXTRUDER_ROLE_SUPPORT_MATERIAL_INTERFACE); -our %EXPORT_TAGS = (roles => \@EXPORT_OK); - -use Slic3r::Geometry qw(PI scale); - -# has 'e_per_mm3' => (is => 'lazy'); -# has 'retract_speed_mm_min' => (is => 'lazy'); - -use constant EXTRUDER_ROLE_PERIMETER => 1; -use constant EXTRUDER_ROLE_INFILL => 2; -use constant EXTRUDER_ROLE_SUPPORT_MATERIAL => 3; -use constant EXTRUDER_ROLE_SUPPORT_MATERIAL_INTERFACE => 4; - - -sub e_per_mm3 { - my $self = shift; - return $self->extrusion_multiplier * (4 / (($self->filament_diameter ** 2) * PI)); -} - -sub retract_speed_mm_min { - my $self = shift; - return $self->retract_speed * 60; -} - -sub extruded_volume { - my ($self, $E) = @_; - return $E * ($self->filament_diameter**2) * PI/4; -} - -sub e_per_mm { - my ($self, $mm3_per_mm) = @_; - return $mm3_per_mm * $self->e_per_mm3; -} - -1; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 4234f57df..f429aae80 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -946,9 +946,8 @@ sub write_gcode { $self->total_used_filament(0); $self->total_extruded_volume(0); foreach my $extruder (@{$gcodegen->extruders}) { - # the final retraction doesn't really count as "used filament" - my $used_filament = $extruder->absolute_E + $extruder->retract_length; - my $extruded_volume = $extruder->extruded_volume($used_filament); + my $used_filament = $extruder->used_filament; + my $extruded_volume = $extruder->extruded_volume; printf $fh "; filament used = %.1fmm (%.1fcm3)\n", $used_filament, $extruded_volume/1000; diff --git a/xs/src/libslic3r/Extruder.cpp b/xs/src/libslic3r/Extruder.cpp index d3919e5e4..f3cf5ab2c 100644 --- a/xs/src/libslic3r/Extruder.cpp +++ b/xs/src/libslic3r/Extruder.cpp @@ -7,6 +7,11 @@ Extruder::Extruder(int id, PrintConfig *config) config(config) { reset(); + + // cache values that are going to be called often + this->e_per_mm3 = this->extrusion_multiplier() + * (4 / ((this->filament_diameter() * this->filament_diameter()) * PI)); + this->retract_speed_mm_min = this->retract_speed() * 60; } void @@ -66,6 +71,26 @@ Extruder::unretract() return dE; } +double +Extruder::e_per_mm(double mm3_per_mm) const +{ + return mm3_per_mm * this->e_per_mm3; +} + +double +Extruder::extruded_volume() const +{ + return this->used_filament() * (this->filament_diameter() * this->filament_diameter()) * PI/4; +} + +double +Extruder::used_filament() const +{ + // Any current amount of retraction should not affect used filament, since + // it represents empty volume in the nozzle. We add it back to E. + return this->absolute_E + this->retracted; +} + Pointf Extruder::extruder_offset() const { diff --git a/xs/src/libslic3r/Extruder.hpp b/xs/src/libslic3r/Extruder.hpp index 99182c58a..c68160996 100644 --- a/xs/src/libslic3r/Extruder.hpp +++ b/xs/src/libslic3r/Extruder.hpp @@ -16,6 +16,9 @@ class Extruder double extrude(double dE); double retract(double length, double restart_extra); double unretract(); + double e_per_mm(double mm3_per_mm) const; + double extruded_volume() const; + double used_filament() const; Pointf extruder_offset() const; double nozzle_diameter() const; @@ -38,7 +41,10 @@ class Extruder double absolute_E; double retracted; double restart_extra; + double e_per_mm3; + double retract_speed_mm_min; + private: PrintConfig *config; }; diff --git a/xs/xsp/Extruder.xsp b/xs/xsp/Extruder.xsp index 43bdbfabc..219bdebc2 100644 --- a/xs/xsp/Extruder.xsp +++ b/xs/xsp/Extruder.xsp @@ -14,6 +14,9 @@ double extrude(double dE); double retract(double length, double restart_extra); double unretract(); + double e_per_mm(double mm3_per_mm); + double extruded_volume(); + double used_filament(); int id() const %code%{ RETVAL = THIS->id; %}; @@ -34,6 +37,10 @@ %code%{ RETVAL = THIS->restart_extra; %}; double set_restart_extra(double val) const %code%{ RETVAL = THIS->restart_extra = val; %}; + double e_per_mm3() + %code%{ RETVAL = THIS->e_per_mm3; %}; + double retract_speed_mm_min() + %code%{ RETVAL = THIS->retract_speed_mm_min; %}; Clone extruder_offset() const; double nozzle_diameter() const;