From ee3fb7caa2bd13c4fe45aee775c83d20a16463c9 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 9 Nov 2014 19:02:45 +0100 Subject: [PATCH] Ported GCodeWriter to XS (faster G-code export!) --- lib/Slic3r.pm | 2 +- lib/Slic3r/GCode.pm | 10 +- lib/Slic3r/GCode/Writer.pm | 426 -------------------------- lib/Slic3r/Print.pm | 2 +- xs/lib/Slic3r/XS.pm | 1 + xs/src/libslic3r/GCodeWriter.cpp | 499 +++++++++++++++++++++++++++++++ xs/src/libslic3r/GCodeWriter.hpp | 63 ++++ xs/xsp/GCodeWriter.xsp | 63 ++++ xs/xsp/my.map | 6 + xs/xsp/typemap.xspt | 7 + 10 files changed, 646 insertions(+), 433 deletions(-) delete mode 100644 lib/Slic3r/GCode/Writer.pm create mode 100644 xs/src/libslic3r/GCodeWriter.cpp create mode 100644 xs/src/libslic3r/GCodeWriter.hpp create mode 100644 xs/xsp/GCodeWriter.xsp diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 12a5aa428..984ebc92f 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -56,7 +56,6 @@ use Slic3r::GCode::PlaceholderParser; use Slic3r::GCode::Reader; use Slic3r::GCode::SpiralVase; use Slic3r::GCode::VibrationLimit; -use Slic3r::GCode::Writer; use Slic3r::Geometry qw(PI); use Slic3r::Geometry::Clipper; use Slic3r::Layer; @@ -177,6 +176,7 @@ sub thread_cleanup { *Slic3r::ExtrusionPath::Collection::DESTROY = sub {}; *Slic3r::Flow::DESTROY = sub {}; *Slic3r::GCode::PlaceholderParser::DESTROY = sub {}; + *Slic3r::GCode::Writer::DESTROY = sub {}; *Slic3r::Geometry::BoundingBox::DESTROY = sub {}; *Slic3r::Geometry::BoundingBoxf::DESTROY = sub {}; *Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {}; diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index 03ef4ea94..7f36b5f50 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -271,7 +271,7 @@ sub _extrude_path { # calculate extrusion length per distance unit my $e_per_mm = $self->writer->extruder->e_per_mm3 * $path->mm3_per_mm; - $e_per_mm = 0 if !$self->writer->_extrusion_axis; + $e_per_mm = 0 if !$self->writer->extrusion_axis; # set speed my $F; @@ -307,7 +307,7 @@ sub _extrude_path { $gcode .= $path->gcode($self->writer->extruder, $e_per_mm, $F, $self->origin->x - $extruder_offset->x, $self->origin->y - $extruder_offset->y, #- - $self->writer->_extrusion_axis, + $self->writer->extrusion_axis, $self->config->gcode_comments ? " ; $description" : ""); if ($self->enable_wipe) { @@ -375,7 +375,7 @@ sub travel_to { # If avoid_crossing_perimeters is disabled or the straight_once flag is set, # perform a straight move with a retraction. $gcode .= $self->retract; - $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($point), $comment); + $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($point), $comment || ''); } # Re-allow avoid_crossing_perimeters for the next travel moves @@ -425,7 +425,7 @@ sub retract { if ($self->config->get_at('wipe', $self->writer->extruder->id) && $self->_wipe_path) { # Reduce feedrate a bit; travel speed is often too high to move on existing material. # Too fast = ripping of existing material; too slow = short wipe path, thus more blob. - my $wipe_speed = $self->writer->config->travel_speed * 0.8; + my $wipe_speed = $self->writer->config->get('travel_speed') * 0.8; # get the retraction length my $length = $toolchange @@ -484,7 +484,7 @@ sub unretract { my $gcode = ""; $gcode .= $self->writer->unlift; - $gcode .= $self->writer->unretract('compensate retraction'); + $gcode .= $self->writer->unretract; return $gcode; } diff --git a/lib/Slic3r/GCode/Writer.pm b/lib/Slic3r/GCode/Writer.pm deleted file mode 100644 index 931cdd3c1..000000000 --- a/lib/Slic3r/GCode/Writer.pm +++ /dev/null @@ -1,426 +0,0 @@ -package Slic3r::GCode::Writer; -use Moo; - -use List::Util qw(min max first); -use Slic3r::Geometry qw(X Y epsilon); - -has 'config' => (is => 'ro', default => sub { Slic3r::Config::GCode->new }); -has 'multiple_extruders' => (is => 'rw', default => sub { 0 }); -has '_extrusion_axis' => (is => 'rw', default => sub { 'E' }); -has '_extruders' => (is => 'ro', default => sub {{}}); -has '_extruder' => (is => 'rw', reader => 'extruder'); -has '_last_acceleration' => (is => 'rw', default => sub { 0 }); -has '_last_fan_speed' => (is => 'rw', default => sub { 0 }); -has '_lifted' => (is => 'rw', default => sub { 0 }); -has '_pos' => (is => 'rw', default => sub { Slic3r::Pointf3->new }); - -sub apply_print_config { - my ($self, $print_config) = @_; - - $self->config->apply_print_config($print_config); - - if ($self->config->gcode_flavor eq 'mach3') { - $self->_extrusion_axis('A'); - } elsif ($self->config->gcode_flavor eq 'no-extrusion') { - $self->_extrusion_axis(''); - } else { - $self->_extrusion_axis($self->config->extrusion_axis); - } -} - -sub set_extruders { - my ($self, $extruder_ids) = @_; - - foreach my $i (@$extruder_ids) { - $self->_extruders->{$i} = my $e = Slic3r::Extruder->new($i, $self->config); - } - - # we enable support for multiple extruder if any extruder greater than 0 is used - # (even if prints only uses that one) since we need to output Tx commands - # first extruder has index 0 - $self->multiple_extruders(max(@$extruder_ids) > 0); -} - -sub preamble { - my ($self) = @_; - - my $gcode = ""; - - if ($self->config->gcode_flavor ne 'makerware') { - $gcode .= "G21 ; set units to millimeters\n"; - $gcode .= "G90 ; use absolute coordinates\n"; - } - if ($self->config->gcode_flavor =~ /^(?:reprap|teacup)$/) { - if ($self->config->use_relative_e_distances) { - $gcode .= "M83 ; use relative distances for extrusion\n"; - } else { - $gcode .= "M82 ; use absolute distances for extrusion\n"; - } - $gcode .= $self->reset_e(1); - } - return $gcode; -} - -sub set_temperature { - my ($self, $temperature, $wait, $tool) = @_; - - return "" if $wait && $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/; - - my ($code, $comment) = ($wait && $self->config->gcode_flavor ne 'teacup') - ? ('M109', 'wait for temperature to be reached') - : ('M104', 'set temperature'); - my $gcode = sprintf "$code %s%d %s; $comment\n", - ($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature, - (defined $tool && ($self->multiple_extruders || $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/)) ? "T$tool " : ""; - - $gcode .= "M116 ; wait for temperature to be reached\n" - if $self->config->gcode_flavor eq 'teacup' && $wait; - - return $gcode; -} - -sub set_bed_temperature { - my ($self, $temperature, $wait) = @_; - - my ($code, $comment) = ($wait && $self->config->gcode_flavor ne 'teacup') - ? (($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/ ? 'M109' : 'M190'), 'wait for bed temperature to be reached') - : ('M140', 'set bed temperature'); - my $gcode = sprintf "$code %s%d ; $comment\n", - ($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature; - - $gcode .= "M116 ; wait for bed temperature to be reached\n" - if $self->config->gcode_flavor eq 'teacup' && $wait; - - return $gcode; -} - -sub set_fan { - my ($self, $speed, $dont_save) = @_; - - if ($self->_last_fan_speed != $speed || $dont_save) { - $self->_last_fan_speed($speed) if !$dont_save; - if ($speed == 0) { - my $code = $self->config->gcode_flavor eq 'teacup' - ? 'M106 S0' - : $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/ - ? 'M127' - : 'M107'; - return sprintf "$code%s\n", ($self->config->gcode_comments ? ' ; disable fan' : ''); - } else { - if ($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/) { - return sprintf "M126%s\n", ($self->config->gcode_comments ? ' ; enable fan' : ''); - } else { - return sprintf "M106 %s%d%s\n", ($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), - (255 * $speed / 100), ($self->config->gcode_comments ? ' ; enable fan' : ''); - } - } - } - return ""; -} - -sub set_acceleration { - my ($self, $acceleration) = @_; - - return "" if !$acceleration || $acceleration == $self->_last_acceleration; - - $self->_last_acceleration($acceleration); - return sprintf "M204 S%s%s\n", - $acceleration, ($self->config->gcode_comments ? ' ; adjust acceleration' : ''); -} - -sub update_progress { - my ($self, $num, $tot, $allow_100) = @_; - - return "" if $self->config->gcode_flavor !~ /^(?:makerware|sailfish)$/; - - my $percent = int($num/$tot*100); - $percent = min($percent, 99) if !$allow_100; - return sprintf "M73 P%s%s\n", - $percent, - $self->_comment('update progress'); -} - -sub need_toolchange { - my ($self, $extruder_id) = @_; - - # return false if this extruder was already selected - return (!defined $self->_extruder) || ($self->_extruder->id != $extruder_id); -} - -sub set_extruder { - my ($self, $extruder_id) = @_; - - return "" if !$self->need_toolchange; - return $self->toolchange($extruder_id); -} - -sub toolchange { - my ($self, $extruder_id) = @_; - - # set the new extruder - $self->_extruder($self->_extruders->{$extruder_id}); - - # return the toolchange command - # if we are running a single-extruder setup, just set the extruder and return nothing - my $gcode = ""; - if ($self->multiple_extruders) { - $gcode .= sprintf "%s%d%s\n", - ($self->config->gcode_flavor eq 'makerware' - ? 'M135 T' - : $self->config->gcode_flavor eq 'sailfish' - ? 'M108 T' - : 'T'), - $extruder_id, - ($self->config->gcode_comments ? ' ; change extruder' : ''); - - $gcode .= $self->reset_e(1); - } - return $gcode; -} - -sub reset_e { - my ($self, $force) = @_; - - return "" if $self->config->gcode_flavor =~ /^(?:mach3|makerware|sailfish)$/; - - if (defined $self->_extruder) { - return "" if $self->_extruder->E == 0 && !$force; - $self->_extruder->set_E(0) if $self->_extruder; - } - - if ($self->_extrusion_axis ne '' && !$self->config->use_relative_e_distances) { - return sprintf "G92 %s0%s\n", $self->config->extrusion_axis, ($self->config->gcode_comments ? ' ; reset extrusion distance' : ''); - } else { - return ""; - } -} - -sub _comment { - my ($self, $comment) = @_; - - return "" if (!defined $comment) || ($comment eq '') || !$self->config->gcode_comments; - return " ; $comment"; -} - -sub set_speed { - my ($self, $F, $comment) = @_; - - return sprintf "G1 F%.3f%s\n", - $F, - $self->_comment($comment); -} - -sub travel_to_xy { - my ($self, $pointf, $comment) = @_; - - $self->_pos->set_x($pointf->x); - $self->_pos->set_y($pointf->y); # )) - return sprintf "G1 X%.3f Y%.3f F%.3f%s\n", - @$pointf, - $self->config->travel_speed*60, - $self->_comment($comment); -} - -sub travel_to_xyz { - my ($self, $pointf3, $comment) = @_; - - # If target Z is lower than current Z but higher than nominal Z we - # don't perform the Z move but we only move in the XY plane and - # adjust the nominal Z by reducing the lift amount that will be - # used for unlift. - if (!$self->will_move_z($pointf3->z)) { - my $nominal_z = $self->_pos->z - $self->_lifted; - $self->_lifted($self->_lifted - ($pointf3->z - $nominal_z)); - return $self->travel_to_xy(Slic3r::Pointf->new(@$pointf3[X,Y])); - } - - # In all the other cases, we perform an actual XYZ move and cancel - # the lift. - $self->_lifted(0); - return $self->_travel_to_xyz($pointf3, $comment); -} - -sub _travel_to_xyz { - my ($self, $pointf3, $comment) = @_; - - $self->_pos($pointf3); - return sprintf "G1 X%.3f Y%.3f Z%.3f F%.3f%s\n", - @$pointf3, - $self->config->travel_speed*60, - $self->_comment($comment); -} - -sub travel_to_z { - my ($self, $z, $comment) = @_; - - # If target Z is lower than current Z but higher than nominal Z - # we don't perform the move but we only adjust the nominal Z by - # reducing the lift amount that will be used for unlift. - if (!$self->will_move_z($z)) { - my $nominal_z = $self->_pos->z - $self->_lifted; - $self->_lifted($self->_lifted - ($z - $nominal_z)); - return ""; - } - - # In all the other cases, we perform an actual Z move and cancel - # the lift. - $self->_lifted(0); - return $self->_travel_to_z($z, $comment); -} - -sub _travel_to_z { - my ($self, $z, $comment) = @_; - - $self->_pos->set_z($z); - return sprintf "G1 Z%.3f F%.3f%s\n", - $z, - $self->config->travel_speed*60, - $self->_comment($comment); -} - -sub will_move_z { - my ($self, $z) = @_; - - # If target Z is lower than current Z but higher than nominal Z - # we don't perform an actual Z move. - if ($self->_lifted > 0) { - my $nominal_z = $self->_pos->z - $self->_lifted; - if ($z >= $nominal_z && $z <= $self->_pos->z) { - return 0; - } - } - return 1; -} - -sub extrude_to_xy { - my ($self, $pointf, $dE, $comment) = @_; - - $self->_pos->set_x($pointf->x); - $self->_pos->set_y($pointf->y); # )) - $self->_extruder->extrude($dE); - return sprintf "G1 X%.3f Y%.3f %s%.5f%s\n", - @$pointf, - $self->_extrusion_axis, - $self->_extruder->E, - $self->_comment($comment); -} - -sub extrude_to_xyz { - my ($self, $pointf3, $dE, $comment) = @_; - - $self->_pos($pointf3); - $self->_lifted(0); - $self->_extruder->extrude($dE); - return sprintf "G1 X%.3f Y%.3f Z%.3f %s%.5f%s\n", - @$pointf3, - $self->_extrusion_axis, - $self->_extruder->E, - $self->_comment($comment); -} - -sub retract { - my ($self) = @_; - - return $self->_retract( - $self->_extruder->retract_length, - $self->_extruder->retract_restart_extra, - 'retract', - ); -} - -sub retract_for_toolchange { - my ($self) = @_; - - return $self->_retract( - $self->_extruder->retract_length_toolchange, - $self->_extruder->retract_restart_extra_toolchange, - 'retract for toolchange', - ); -} - -sub _retract { - my ($self, $length, $restart_extra, $comment) = @_; - - my $gcode = ""; - my $dE = $self->_extruder->retract($length, $restart_extra); - if ($dE != 0) { - if ($self->config->use_firmware_retraction) { - $gcode .= "G10 ; retract\n"; - } else { - $gcode = sprintf "G1 %s%.5f F%.3f%s\n", - $self->_extrusion_axis, - $self->_extruder->E, - $self->_extruder->retract_speed_mm_min, - $self->_comment($comment); - } - } - - $gcode .= "M103 ; extruder off\n" - if $self->config->gcode_flavor eq 'makerware'; - - return $gcode; -} - -sub unretract { - my ($self, $comment) = @_; - - my $gcode = ""; - - $gcode .= "M101 ; extruder on\n" - if $self->config->gcode_flavor eq 'makerware'; - - my $dE = $self->_extruder->unretract; - if ($dE != 0) { - if ($self->config->use_firmware_retraction) { - $gcode .= "G11 ; unretract\n"; - $gcode .= $self->reset_e; - return $gcode; - } else { - # use G1 instead of G0 because G0 will blend the restart with the previous travel move - $gcode .= sprintf "G1 %s%.5f F%.3f%s\n", - $self->_extrusion_axis, - $self->_extruder->E, - $self->_extruder->retract_speed_mm_min, - $self->_comment($comment); - } - } - - return $gcode; -} - -# If this method is called more than once before calling unlift(), -# it will not perform subsequent lifts, even if Z was raised manually -# (i.e. with travel_to_z()) and thus _lifted was reduced. -sub lift { - my ($self) = @_; - - if ($self->_lifted == 0 && $self->config->retract_lift->[0] > 0) { - my $to_lift = $self->config->retract_lift->[0]; - $self->_lifted($to_lift); - return $self->_travel_to_z($self->_pos->z + $to_lift, 'lift Z'); - } - return ""; -} - -sub unlift { - my ($self) = @_; - - my $gcode = ""; - if ($self->_lifted > 0) { - $gcode .= $self->_travel_to_z($self->_pos->z - $self->_lifted, 'restore layer Z'); - $self->_lifted(0); - } - return $gcode; -} - -sub hasmultiple_extruders { - my ($self) = @_; - return $self->multiple_extruders; -} - -sub extruders { - my ($self) = @_; - return [ sort { $a->id <=> $b->id } values %{$self->_extruders} ]; -} - -1; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 83290cb2b..b50cfbaf6 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -633,7 +633,7 @@ sub write_gcode { if ($layer->id == 0 && $finished_objects > 0) { printf $fh $gcodegen->writer->set_bed_temperature($self->config->first_layer_bed_temperature), if $self->config->first_layer_bed_temperature; - $print_first_layer_temperature->(); + $print_first_layer_temperature->(0); } print $fh $buffer->append( $layer_gcode->process_layer($layer, [$copy]), diff --git a/xs/lib/Slic3r/XS.pm b/xs/lib/Slic3r/XS.pm index 4e112eda4..dbb4df69c 100644 --- a/xs/lib/Slic3r/XS.pm +++ b/xs/lib/Slic3r/XS.pm @@ -190,6 +190,7 @@ package main; for my $class (qw( Slic3r::Config Slic3r::Config::Full + Slic3r::Config::GCode Slic3r::Config::Print Slic3r::Config::PrintObject Slic3r::Config::PrintRegion diff --git a/xs/src/libslic3r/GCodeWriter.cpp b/xs/src/libslic3r/GCodeWriter.cpp new file mode 100644 index 000000000..0133c2265 --- /dev/null +++ b/xs/src/libslic3r/GCodeWriter.cpp @@ -0,0 +1,499 @@ +#include "GCodeWriter.hpp" +#include +#include +#include +#include + +#define FLAVOR_IS(val) this->config.gcode_flavor == val +#define FLAVOR_IS_NOT(val) this->config.gcode_flavor != val +#define COMMENT(comment) if (this->config.gcode_comments && !comment.empty()) gcode << " ; " << comment; +#define PRECISION(val, precision) std::fixed << std::setprecision(precision) << val +#define XYZF_NUM(val) PRECISION(val, 3) +#define E_NUM(val) PRECISION(val, 5) + +namespace Slic3r { + +Extruder* +GCodeWriter::extruder() +{ + return this->_extruder; +} + +std::string +GCodeWriter::extrusion_axis() const +{ + return this->_extrusion_axis; +} + +void +GCodeWriter::apply_print_config(const PrintConfig &print_config) +{ + this->config.apply(print_config, true); + + if (FLAVOR_IS(gcfMach3)) { + this->_extrusion_axis = "A"; + } else if (FLAVOR_IS(gcfNoExtrusion)) { + this->_extrusion_axis = ""; + } else { + this->_extrusion_axis = this->config.extrusion_axis; + } +} + +void +GCodeWriter::set_extruders(const std::vector &extruder_ids) +{ + for (std::vector::const_iterator i = extruder_ids.begin(); i != extruder_ids.end(); ++i) { + this->extruders.insert( std::pair(*i, Extruder(*i, &this->config)) ); + } + + /* we enable support for multiple extruder if any extruder greater than 0 is used + (even if prints only uses that one) since we need to output Tx commands + first extruder has index 0 */ + this->multiple_extruders = (*std::max_element(extruder_ids.begin(), extruder_ids.end())) > 0; +} + +std::string +GCodeWriter::preamble() +{ + std::string gcode; + + if (FLAVOR_IS_NOT(gcfMakerWare)) { + gcode += "G21 ; set units to millimeters\n"; + gcode += "G90 ; use absolute coordinates\n"; + } + if (FLAVOR_IS(gcfRepRap) || FLAVOR_IS(gcfTeacup)) { + if (this->config.use_relative_e_distances) { + gcode += "M83 ; use relative distances for extrusion\n"; + } else { + gcode += "M82 ; use absolute distances for extrusion\n"; + } + gcode += this->reset_e(true); + } + + return gcode; +} + +std::string +GCodeWriter::set_temperature(unsigned int temperature, bool wait, int tool) +{ + if (wait && (FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish))) + return ""; + + std::string code, comment; + if (wait && FLAVOR_IS_NOT(gcfTeacup)) { + code = "M109"; + comment = "wait for temperature to be reached"; + } else { + code = "M104"; + comment = "set temperature"; + } + + std::ostringstream gcode; + gcode << code << " "; + if (FLAVOR_IS(gcfMach3)) { + gcode << "P"; + } else { + gcode << "S"; + } + gcode << temperature; + if (tool != -1 && (this->multiple_extruders || FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish))) { + gcode << " T" << tool; + } + gcode << " ; " << comment << "\n"; + + if (FLAVOR_IS(gcfTeacup) && wait) + gcode << "M116 ; wait for temperature to be reached\n"; + + return gcode.str(); +} + +std::string +GCodeWriter::set_bed_temperature(unsigned int temperature, bool wait) +{ + std::string code, comment; + if (wait && FLAVOR_IS_NOT(gcfTeacup)) { + if (FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish)) { + code = "M109"; + } else { + code = "M190"; + } + comment = "set bed temperature"; + } else { + code = "M140"; + comment = "wait for bed temperature to be reached"; + } + + std::ostringstream gcode; + gcode << code << " "; + if (FLAVOR_IS(gcfMach3)) { + gcode << "P"; + } else { + gcode << "S"; + } + gcode << temperature << " ; " << comment << "\n"; + + if (FLAVOR_IS(gcfTeacup) && wait) + gcode << "M116 ; wait for bed temperature to be reached\n"; + + return gcode.str(); +} + +std::string +GCodeWriter::set_fan(unsigned int speed, bool dont_save) +{ + std::ostringstream gcode; + if (this->_last_fan_speed != speed || dont_save) { + if (!dont_save) this->_last_fan_speed = speed; + + if (speed == 0) { + if (FLAVOR_IS(gcfTeacup)) { + gcode << "M106 S0"; + } else if (FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish)) { + gcode << "M127"; + } else { + gcode << "M107"; + } + if (this->config.gcode_comments) gcode << " ; disable fan"; + gcode << "\n"; + } else { + if (FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish)) { + gcode << "M126"; + } else { + gcode << "M106 "; + if (FLAVOR_IS(gcfMach3)) { + gcode << "P"; + } else { + gcode << "S"; + } + gcode << (255.0 * speed / 100.0); + } + if (this->config.gcode_comments) gcode << " ; enable fan"; + gcode << "\n"; + } + } + return gcode.str(); +} + +std::string +GCodeWriter::set_acceleration(unsigned int acceleration) +{ + if (acceleration == 0 || acceleration == this->_last_acceleration) + return ""; + + this->_last_acceleration = acceleration; + + std::ostringstream gcode; + gcode << "M204 S" << acceleration; + if (this->config.gcode_comments) gcode << " ; adjust acceleration"; + gcode << "\n"; + + return gcode.str(); +} + +std::string +GCodeWriter::reset_e(bool force) +{ + if (FLAVOR_IS(gcfMach3) + || FLAVOR_IS(gcfMakerWare) + || FLAVOR_IS(gcfSailfish)) + return ""; + + if (this->_extruder != NULL) { + if (this->_extruder->E == 0 && !force) return ""; + this->_extruder->E = 0; + } + + if (!this->_extrusion_axis.empty() && !this->config.use_relative_e_distances) { + std::ostringstream gcode; + gcode << "G92 " << this->_extrusion_axis << "0"; + if (this->config.gcode_comments) gcode << " ; reset extrusion distance"; + gcode << "\n"; + return gcode.str(); + } else { + return ""; + } +} + +std::string +GCodeWriter::update_progress(unsigned int num, unsigned int tot, bool allow_100) +{ + if (FLAVOR_IS_NOT(gcfMakerWare) && FLAVOR_IS_NOT(gcfSailfish)) + return ""; + + unsigned int percent = 100.0 * num / tot; + if (!allow_100) percent = std::min(percent, (unsigned int)99); + + std::ostringstream gcode; + gcode << "M73 P" << percent; + if (this->config.gcode_comments) gcode << " ; update progress"; + gcode << "\n"; + return gcode.str(); +} + +bool +GCodeWriter::need_toolchange(unsigned int extruder_id) const +{ + // return false if this extruder was already selected + return (this->_extruder == NULL) || (this->_extruder->id != extruder_id); +} + +std::string +GCodeWriter::set_extruder(unsigned int extruder_id) +{ + if (!this->need_toolchange(extruder_id)) return ""; + return this->toolchange(extruder_id); +} + +std::string +GCodeWriter::toolchange(unsigned int extruder_id) +{ + // set the new extruder + this->_extruder = &this->extruders.find(extruder_id)->second; + + // return the toolchange command + // if we are running a single-extruder setup, just set the extruder and return nothing + std::ostringstream gcode; + if (this->multiple_extruders) { + if (FLAVOR_IS(gcfMakerWare)) { + gcode << "M135 T"; + } else if (FLAVOR_IS(gcfSailfish)) { + gcode << "M108 T"; + } else { + gcode << "T"; + } + gcode << extruder_id; + if (this->config.gcode_comments) gcode << " ; change extruder"; + gcode << "\n"; + + gcode << this->reset_e(true); + } + return gcode.str(); +} + +std::string +GCodeWriter::set_speed(double F, const std::string &comment) +{ + std::ostringstream gcode; + gcode << "G1 F" << F; + COMMENT(comment); + gcode << "\n"; + return gcode.str(); +} + +std::string +GCodeWriter::travel_to_xy(const Pointf &point, const std::string &comment) +{ + this->_pos.x = point.x; + this->_pos.y = point.y; + + std::ostringstream gcode; + gcode << "G1 X" << XYZF_NUM(point.x) + << " Y" << XYZF_NUM(point.y) + << " F" << XYZF_NUM(this->config.travel_speed.value * 60.0); + COMMENT(comment); + gcode << "\n"; + return gcode.str(); +} + +std::string +GCodeWriter::travel_to_xyz(const Pointf3 &point, const std::string &comment) +{ + /* If target Z is lower than current Z but higher than nominal Z we + don't perform the Z move but we only move in the XY plane and + adjust the nominal Z by reducing the lift amount that will be + used for unlift. */ + if (!this->will_move_z(point.z)) { + double nominal_z = this->_pos.z - this->_lifted; + this->_lifted = this->_lifted - (point.z - nominal_z); + return this->travel_to_xy(point); + } + + /* In all the other cases, we perform an actual XYZ move and cancel + the lift. */ + this->_lifted = 0; + this->_pos = point; + + std::ostringstream gcode; + gcode << "G1 X" << XYZF_NUM(point.x) + << " Y" << XYZF_NUM(point.y) + << " Z" << XYZF_NUM(point.z) + << " F" << XYZF_NUM(this->config.travel_speed.value * 60.0); + COMMENT(comment); + gcode << "\n"; + return gcode.str(); +} + +std::string +GCodeWriter::travel_to_z(double z, const std::string &comment) +{ + /* If target Z is lower than current Z but higher than nominal Z + we don't perform the move but we only adjust the nominal Z by + reducing the lift amount that will be used for unlift. */ + if (!this->will_move_z(z)) { + double nominal_z = this->_pos.z - this->_lifted; + this->_lifted = this->_lifted - (z - nominal_z); + return ""; + } + + /* In all the other cases, we perform an actual Z move and cancel + the lift. */ + this->_lifted = 0; + return this->_travel_to_z(z, comment); +} + +std::string +GCodeWriter::_travel_to_z(double z, const std::string &comment) +{ + this->_pos.z = z; + + std::ostringstream gcode; + gcode << "G1 Z" << XYZF_NUM(z) + << " F" << XYZF_NUM(this->config.travel_speed.value * 60.0); + COMMENT(comment); + gcode << "\n"; + return gcode.str(); +} + +bool +GCodeWriter::will_move_z(double z) const +{ + /* If target Z is lower than current Z but higher than nominal Z + we don't perform an actual Z move. */ + if (this->_lifted > 0) { + double nominal_z = this->_pos.z - this->_lifted; + if (z >= nominal_z && z <= this->_pos.z) + return false; + } + return true; +} + +std::string +GCodeWriter::extrude_to_xy(const Pointf &point, double dE, const std::string &comment) +{ + this->_pos.x = point.x; + this->_pos.y = point.y; + this->_extruder->extrude(dE); + + std::ostringstream gcode; + gcode << "G1 X" << XYZF_NUM(point.x) + << " Y" << XYZF_NUM(point.y) + << " " << this->_extrusion_axis << E_NUM(this->_extruder->E); + COMMENT(comment); + gcode << "\n"; + return gcode.str(); +} + +std::string +GCodeWriter::extrude_to_xyz(const Pointf3 &point, double dE, const std::string &comment) +{ + this->_pos = point; + this->_lifted = 0; + this->_extruder->extrude(dE); + + std::ostringstream gcode; + gcode << "G1 X" << XYZF_NUM(point.x) + << " Y" << XYZF_NUM(point.y) + << " Z" << XYZF_NUM(point.z) + << " " << this->_extrusion_axis << E_NUM(this->_extruder->E); + COMMENT(comment); + gcode << "\n"; + return gcode.str(); +} + +std::string +GCodeWriter::retract() +{ + return this->_retract( + this->_extruder->retract_length(), + this->_extruder->retract_restart_extra(), + "retract" + ); +} + +std::string +GCodeWriter::retract_for_toolchange() +{ + return this->_retract( + this->_extruder->retract_length_toolchange(), + this->_extruder->retract_restart_extra_toolchange(), + "retract for toolchange" + ); +} + +std::string +GCodeWriter::_retract(double length, double restart_extra, const std::string &comment) +{ + std::ostringstream gcode; + double dE = this->_extruder->retract(length, restart_extra); + if (dE != 0) { + if (this->config.use_firmware_retraction) { + gcode << "G10 ; retract\n"; + } else { + gcode << "G1 " << this->_extrusion_axis << E_NUM(this->_extruder->E) + << " F" << this->_extruder->retract_speed_mm_min; + COMMENT(comment); + gcode << "\n"; + } + } + + if (FLAVOR_IS(gcfMakerWare)) + gcode << "M103 ; extruder off\n"; + + return gcode.str(); +} + +std::string +GCodeWriter::unretract() +{ + std::ostringstream gcode; + + if (FLAVOR_IS(gcfMakerWare)) + gcode << "M101 ; extruder on\n"; + + double dE = this->_extruder->unretract(); + if (dE != 0) { + if (this->config.use_firmware_retraction) { + gcode << "G11 ; unretract\n"; + gcode << this->reset_e(); + } else { + // use G1 instead of G0 because G0 will blend the restart with the previous travel move + gcode << "G1 " << this->_extrusion_axis << E_NUM(this->_extruder->E) + << " F" << this->_extruder->retract_speed_mm_min; + if (this->config.gcode_comments) gcode << " ; unretract"; + gcode << "\n"; + } + } + + return gcode.str(); +} + +/* If this method is called more than once before calling unlift(), + it will not perform subsequent lifts, even if Z was raised manually + (i.e. with travel_to_z()) and thus _lifted was reduced. */ +std::string +GCodeWriter::lift() +{ + double target_lift = this->config.retract_lift.get_at(0); + if (this->_lifted == 0 && target_lift > 0) { + this->_lifted = target_lift; + return this->_travel_to_z(this->_pos.z + target_lift, "lift Z"); + } + return ""; +} + +std::string +GCodeWriter::unlift() +{ + std::string gcode; + if (this->_lifted > 0) { + gcode += this->_travel_to_z(this->_pos.z - this->_lifted, "restore layer Z"); + this->_lifted = 0; + } + return gcode; +} + +#ifdef SLIC3RXS +REGISTER_CLASS(GCodeWriter, "GCode::Writer"); +#endif + +} \ No newline at end of file diff --git a/xs/src/libslic3r/GCodeWriter.hpp b/xs/src/libslic3r/GCodeWriter.hpp new file mode 100644 index 000000000..5500a19f5 --- /dev/null +++ b/xs/src/libslic3r/GCodeWriter.hpp @@ -0,0 +1,63 @@ +#ifndef slic3r_GCodeWriter_hpp_ +#define slic3r_GCodeWriter_hpp_ + +#include +#include +#include "Extruder.hpp" +#include "Point.hpp" +#include "PrintConfig.hpp" + +namespace Slic3r { + +class GCodeWriter { + public: + GCodeConfig config; + std::map extruders; + bool multiple_extruders; + + GCodeWriter() + : multiple_extruders(false), _extrusion_axis("E"), _extruder(NULL), + _last_acceleration(0), _last_fan_speed(0), _lifted(0) + {}; + Extruder* extruder(); + std::string extrusion_axis() const; + void apply_print_config(const PrintConfig &print_config); + void set_extruders(const std::vector &extruder_ids); + std::string preamble(); + std::string set_temperature(unsigned int temperature, bool wait = false, int tool = -1); + std::string set_bed_temperature(unsigned int temperature, bool wait = false); + std::string set_fan(unsigned int speed, bool dont_save = false); + std::string set_acceleration(unsigned int acceleration); + std::string reset_e(bool force = false); + std::string update_progress(unsigned int num, unsigned int tot, bool allow_100 = false); + bool need_toolchange(unsigned int extruder_id) const; + std::string set_extruder(unsigned int extruder_id); + std::string toolchange(unsigned int extruder_id); + std::string set_speed(double F, const std::string &comment = std::string()); + std::string travel_to_xy(const Pointf &point, const std::string &comment = std::string()); + std::string travel_to_xyz(const Pointf3 &point, const std::string &comment = std::string()); + std::string travel_to_z(double z, const std::string &comment = std::string()); + bool will_move_z(double z) const; + std::string extrude_to_xy(const Pointf &point, double dE, const std::string &comment = std::string()); + std::string extrude_to_xyz(const Pointf3 &point, double dE, const std::string &comment = std::string()); + std::string retract(); + std::string retract_for_toolchange(); + std::string unretract(); + std::string lift(); + std::string unlift(); + + private: + std::string _extrusion_axis; + Extruder* _extruder; + unsigned int _last_acceleration; + unsigned int _last_fan_speed; + double _lifted; + Pointf3 _pos; + + std::string _travel_to_z(double z, const std::string &comment); + std::string _retract(double length, double restart_extra, const std::string &comment); +}; + +} + +#endif diff --git a/xs/xsp/GCodeWriter.xsp b/xs/xsp/GCodeWriter.xsp new file mode 100644 index 000000000..ee9049e56 --- /dev/null +++ b/xs/xsp/GCodeWriter.xsp @@ -0,0 +1,63 @@ +%module{Slic3r::XS}; + +%{ +#include +#include "libslic3r/GCodeWriter.hpp" +%} + +%name{Slic3r::GCode::Writer} class GCodeWriter { + GCodeWriter(); + ~GCodeWriter(); + + Ref config() + %code%{ RETVAL = &THIS->config; %}; + bool multiple_extruders() + %code{% RETVAL = THIS->multiple_extruders; %}; + Ref extruder(); + std::string extrusion_axis(); + void apply_print_config(PrintConfig* print_config) + %code{% THIS->apply_print_config(*print_config); %}; + void set_extruders(std::vector extruder_ids); + std::string preamble(); + std::string set_temperature(unsigned int temperature, bool wait = false, int tool = -1); + std::string set_bed_temperature(unsigned int temperature, bool wait = false); + std::string set_fan(unsigned int speed, bool dont_save = false); + std::string set_acceleration(unsigned int acceleration); + std::string reset_e(bool force = false); + std::string update_progress(unsigned int num, unsigned int tot, bool allow_100 = false); + bool need_toolchange(unsigned int extruder_id); + std::string set_extruder(unsigned int extruder_id); + std::string toolchange(unsigned int extruder_id); + std::string set_speed(double F, std::string comment = std::string()); + std::string travel_to_xy(Pointf* point, std::string comment = std::string()) + %code{% RETVAL = THIS->travel_to_xy(*point, comment); %}; + std::string travel_to_xyz(Pointf3* point, std::string comment = std::string()) + %code{% RETVAL = THIS->travel_to_xyz(*point, comment); %}; + std::string travel_to_z(double z, std::string comment = std::string()); + bool will_move_z(double z); + std::string extrude_to_xy(Pointf* point, double dE, std::string comment = std::string()) + %code{% RETVAL = THIS->extrude_to_xy(*point, dE, comment); %}; + std::string extrude_to_xyz(Pointf3* point, double dE, std::string comment = std::string()) + %code{% RETVAL = THIS->extrude_to_xyz(*point, dE, comment); %}; + std::string retract(); + std::string retract_for_toolchange(); + std::string unretract(); + std::string lift(); + std::string unlift(); +%{ + +SV* +GCodeWriter::extruders() + CODE: + AV* av = newAV(); + av_fill(av, THIS->extruders.size()-1); + int i = 0; + for (std::map::iterator it = THIS->extruders.begin(); it != THIS->extruders.end(); ++it) { + av_store(av, i++, perl_to_SV_ref(it->second)); + } + RETVAL = newRV_noinc((SV*)av); + OUTPUT: + RETVAL + +%} +}; diff --git a/xs/xsp/my.map b/xs/xsp/my.map index 41476ca2c..8429f3ee7 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -111,6 +111,8 @@ SurfaceCollection* O_OBJECT_SLIC3R Ref O_OBJECT_SLIC3R_T Extruder* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T +Clone O_OBJECT_SLIC3R_T Model* O_OBJECT_SLIC3R Ref O_OBJECT_SLIC3R_T @@ -159,6 +161,10 @@ MotionPlanner* O_OBJECT_SLIC3R Ref O_OBJECT_SLIC3R_T Clone O_OBJECT_SLIC3R_T +GCodeWriter* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T +Clone O_OBJECT_SLIC3R_T + ExtrusionLoopRole T_UV ExtrusionRole T_UV FlowRole T_UV diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 01718a8cb..351e1ccd8 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -9,6 +9,8 @@ %typemap{std::vector*}; %typemap{std::vector}; %typemap{std::vector*}; +%typemap{std::vector}; +%typemap{std::vector*}; %typemap{std::vector}; %typemap{t_layer_height_ranges}; %typemap{SV*}; @@ -82,6 +84,9 @@ %typemap{MotionPlanner*}; %typemap{Ref}{simple}; %typemap{Clone}{simple}; +%typemap{GCodeWriter*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; %typemap{Surface*}; %typemap{Ref}{simple}; @@ -127,6 +132,8 @@ %typemap{TriangleMeshPtrs}; %typemap{Ref}{simple}; %typemap{Extruder*}; +%typemap{Ref}{simple}; +%typemap{Clone}{simple}; %typemap{Model*}; %typemap{Ref}{simple}; %typemap{Clone}{simple};