diff --git a/MANIFEST b/MANIFEST index 8c04cbc4f..c5869c19c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -55,6 +55,7 @@ t/arcs.t t/clean_polylines.t t/clipper.t t/collinear.t +t/custom_gcode.t t/dynamic.t t/fill.t t/geometry.t diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index 72bc89fa1..84622b80f 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -7,6 +7,7 @@ use Slic3r::Geometry qw(scale unscale scaled_epsilon points_coincide PI X Y B); has 'multiple_extruders' => (is => 'ro', default => sub {0} ); has 'layer' => (is => 'rw'); +has 'move_z_callback' => (is => 'rw'); has 'shift_x' => (is => 'rw', default => sub {0} ); has 'shift_y' => (is => 'rw', default => sub {0} ); has 'z' => (is => 'rw'); @@ -79,6 +80,7 @@ sub move_z { $self->speed('travel'); $gcode .= $self->G0(undef, $z, 0, $comment || ('move to next layer (' . $self->layer->id . ')')) unless ($current_z // -1) != ($self->z // -1); + $gcode .= $self->move_z_callback->() if defined $self->move_z_callback; } return $gcode; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 0ac8c11ff..a8cb82a83 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -738,8 +738,13 @@ sub write_gcode { # set new layer, but don't move Z as support material interfaces may need an intermediate one $gcodegen->layer($self->objects->[$object_copies->[0][0]]->layers->[$layer_id]); $gcodegen->elapsed_time(0); - $gcode .= $Slic3r::Config->replace_options($Slic3r::Config->layer_gcode) . "\n" - if $Slic3r::Config->layer_gcode; + + # prepare callback to call as soon as a Z command is generated + $gcodegen->move_z_callback(sub { + $gcodegen->move_z_callback(undef); # circular ref or not? + return $Slic3r::Config->replace_options($Slic3r::Config->layer_gcode) . "\n" + if $Slic3r::Config->layer_gcode; + }); # extrude skirt if ($skirt_done < $Slic3r::Config->skirt_height) { diff --git a/t/custom_gcode.t b/t/custom_gcode.t new file mode 100644 index 000000000..ea36c0cf1 --- /dev/null +++ b/t/custom_gcode.t @@ -0,0 +1,41 @@ +use Test::More tests => 1; +use strict; +use warnings; + +BEGIN { + use FindBin; + use lib "$FindBin::Bin/../lib"; +} + +use Slic3r; +use Slic3r::Test; + +my $config = Slic3r::Config->new_from_defaults; + +my $test = sub { + my ($conf) = @_; + $conf ||= $config; + + my $print = Slic3r::Test::init_print('2x20x10', config => $conf); + + my $last_move_was_z_change = 0; + Slic3r::Test::GCodeReader->new(gcode => Slic3r::Test::gcode($print))->parse(sub { + my ($self, $cmd, $args, $info) = @_; + + if ($last_move_was_z_change && $cmd ne $config->layer_gcode) { + fail 'custom layer G-code was not applied after Z change'; + } + if (!$last_move_was_z_change && $cmd eq $config->layer_gcode) { + fail 'custom layer G-code was not applied after Z change'; + } + + $last_move_was_z_change = (defined $info->{dist_Z} && $info->{dist_Z} > 0); + }); + + 1; +}; + +$config->set('layer_gcode', '_MY_CUSTOM_GCODE_'); +ok $test->(), "custom layer G-code is applied after Z move and before other moves"; + +__END__