From 4862d832b535d0bfe0235f124621cddf0a8bca9f Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 9 Aug 2013 19:46:20 +0200 Subject: [PATCH] Simplify layer change code now that we have distinct support layers --- lib/Slic3r/GCode.pm | 4 ++-- lib/Slic3r/GCode/Layer.pm | 24 ++++++------------------ lib/Slic3r/Print.pm | 6 +++++- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index 6c58b2dbb..873479ec5 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -17,7 +17,6 @@ has 'layer' => (is => 'rw'); has '_layer_islands' => (is => 'rw'); has '_upper_layer_islands' => (is => 'rw'); has '_layer_overhangs' => (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'); @@ -132,6 +131,8 @@ sub change_layer { $gcode .= $self->set_acceleration($self->config->default_acceleration); } } + + $gcode .= $self->move_z($layer->print_z); return $gcode; } @@ -153,7 +154,6 @@ sub move_z { $self->speed('travel'); $gcode .= $self->G0(undef, $z, 0, $comment || ('move to next layer (' . $self->layer->id . ')')) if !defined $self->z || abs($z - ($self->z - $self->lifted)) > epsilon; - $gcode .= $self->move_z_callback->() if defined $self->move_z_callback; } elsif ($z < $self->z && $z > ($self->z - $self->lifted + epsilon)) { # we're moving to a layer height which is greater than the nominal current one # (nominal = actual - lifted) and less than the actual one. we're basically diff --git a/lib/Slic3r/GCode/Layer.pm b/lib/Slic3r/GCode/Layer.pm index 307ab0e32..1b21b3fca 100644 --- a/lib/Slic3r/GCode/Layer.pm +++ b/lib/Slic3r/GCode/Layer.pm @@ -46,23 +46,16 @@ sub process_layer { $self->second_layer_things_done(1); } - # set new layer, but don't move Z as support material contact areas may need an intermediate one + # set new layer - this will change Z and force a retraction if retract_layer_change is enabled $gcode .= $self->gcodegen->change_layer($layer); - - # prepare callback to call as soon as a Z command is generated - $self->gcodegen->move_z_callback(sub { - $self->gcodegen->move_z_callback(undef); # circular ref or not? - return "" if !$Slic3r::Config->layer_gcode; - return $Slic3r::Config->replace_options($Slic3r::Config->layer_gcode, { - layer_num => $self->gcodegen->layer->id, - }) . "\n"; - }); + $gcode .= $Slic3r::Config->replace_options($Slic3r::Config->layer_gcode, { + layer_num => $self->gcodegen->layer->id, + }) . "\n" if $Slic3r::Config->layer_gcode; # extrude skirt if ((values %{$self->skirt_done}) < $Slic3r::Config->skirt_height && !$self->skirt_done->{$layer->print_z}) { $self->gcodegen->set_shift(@{$self->shift}); - $gcode .= $self->gcodegen->set_extruder($self->extruders->[0]); # move_z requires extruder - $gcode .= $self->gcodegen->move_z($layer->print_z); + $gcode .= $self->gcodegen->set_extruder($self->extruders->[0]); # skip skirt if we have a large brim if ($layer->id < $Slic3r::Config->skirt_height) { # distribute skirt loops across all extruders @@ -81,8 +74,7 @@ sub process_layer { # extrude brim if (!$self->brim_done) { - $gcode .= $self->gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]); # move_z requires extruder - $gcode .= $self->gcodegen->move_z($layer->print_z); + $gcode .= $self->gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]); $self->gcodegen->set_shift(@{$self->shift}); $gcode .= $self->gcodegen->extrude_loop($_, 'brim') for @{$self->print->brim}; $self->brim_done(1); @@ -98,7 +90,6 @@ sub process_layer { # extrude support material before other things because it might use a lower Z # and also because we avoid travelling on other things when printing it if ($self->print->has_support_material && $layer->isa('Slic3r::Layer::Support')) { - $gcode .= $self->gcodegen->move_z($layer->print_z); if ($layer->support_interface_fills) { $gcode .= $self->gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_interface_extruder-1]); $gcode .= $self->gcodegen->extrude_path($_, 'support material interface') @@ -111,9 +102,6 @@ sub process_layer { } } - # set actual Z - this will force a retraction - $gcode .= $self->gcodegen->move_z($layer->print_z); - # tweak region ordering to save toolchanges my @region_ids = 0 .. ($self->print->regions_count-1); if ($self->gcodegen->multiple_extruders) { diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index ac75f542c..544851915 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -732,7 +732,7 @@ sub write_gcode { # set up our extruder object my $gcodegen = Slic3r::GCode->new( config => $self->config, - extruders => $self->extruders, + extruders => $self->extruders, # we should only pass the *used* extruders (but maintain the Tx indices right!) layer_count => $self->layer_count, ); print $fh "G21 ; set units to millimeters\n" if $Slic3r::Config->gcode_flavor ne 'makerware'; @@ -768,6 +768,10 @@ sub write_gcode { } } + # always start with first extruder + # TODO: make sure we select the first *used* extruder + print $fh $gcodegen->set_extruder($self->extruders->[0]); + # calculate X,Y shift to center print around specified origin my $print_bb = $self->bounding_box; my $print_size = $print_bb->size;