diff --git a/lib/Slic3r/GCode/Writer.pm b/lib/Slic3r/GCode/Writer.pm index 1563ca7ac..931cdd3c1 100644 --- a/lib/Slic3r/GCode/Writer.pm +++ b/lib/Slic3r/GCode/Writer.pm @@ -341,18 +341,18 @@ sub retract_for_toolchange { sub _retract { my ($self, $length, $restart_extra, $comment) = @_; - if ($self->config->use_firmware_retraction) { - return "G10 ; retract\n"; - } - my $gcode = ""; my $dE = $self->_extruder->retract($length, $restart_extra); if ($dE != 0) { - $gcode = sprintf "G1 %s%.5f F%.3f%s\n", - $self->_extrusion_axis, - $self->_extruder->E, - $self->_extruder->retract_speed_mm_min, - $self->_comment($comment); + 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" @@ -369,20 +369,20 @@ sub unretract { $gcode .= "M101 ; extruder on\n" if $self->config->gcode_flavor eq 'makerware'; - if ($self->config->use_firmware_retraction) { - $gcode .= "G11 ; unretract\n"; - $gcode .= $self->reset_e; - return $gcode; - } - my $dE = $self->_extruder->unretract; if ($dE != 0) { - # 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); + 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; diff --git a/t/retraction.t b/t/retraction.t index abbb0f7cc..8cacf686f 100644 --- a/t/retraction.t +++ b/t/retraction.t @@ -1,4 +1,4 @@ -use Test::More tests => 16; +use Test::More tests => 18; use strict; use warnings; @@ -159,6 +159,29 @@ use Slic3r::Test qw(_eq); is $z_restores, 0, 'no lift'; } +{ + my $config = Slic3r::Config->new_from_defaults; + $config->set('use_firmware_retraction', 1); + + my $print = Slic3r::Test::init_print('20mm_cube', config => $config); + my $retracted = 0; + my $double_retractions = my $double_unretractions = 0; + Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub { + my ($self, $cmd, $args, $info) = @_; + + if ($cmd eq 'G10') { + $double_retractions++ if $retracted; + $retracted = 1; + } elsif ($cmd eq 'G11') { + $double_unretractions++ if !$retracted; + $retracted = 0; + } + }); + + is $double_retractions, 0, 'no double retractions'; + is $double_unretractions, 0, 'no double unretractions'; +} + { my $config = Slic3r::Config->new_from_defaults; $config->set('only_retract_when_crossing_perimeters', 1);