diff --git a/lib/Slic3r/ExtrusionPath.pm b/lib/Slic3r/ExtrusionPath.pm index 200278496..c55cd02ae 100644 --- a/lib/Slic3r/ExtrusionPath.pm +++ b/lib/Slic3r/ExtrusionPath.pm @@ -19,7 +19,7 @@ has 'polyline' => ( ); # height is the vertical thickness of the extrusion expressed in mm -has 'height' => (is => 'ro'); +has 'height' => (is => 'rw'); has 'flow_spacing' => (is => 'rw'); has 'role' => (is => 'rw', required => 1); diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index d71627f39..6c21e5721 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -6,7 +6,7 @@ use Slic3r::ExtrusionPath ':roles'; use Slic3r::Geometry qw(scale unscale scaled_epsilon points_coincide PI X Y); has 'multiple_extruders' => (is => 'ro', default => sub {0} ); -has 'layer' => (is => 'rw'); +has 'layer' => (is => 'rw'); # this is not very correct, we should replace it with explicit layer_id and avoid using $self->layer->flow at all here because it's too general has 'shift_x' => (is => 'rw', default => sub {0} ); has 'shift_y' => (is => 'rw', default => sub {0} ); has 'z' => (is => 'rw', default => sub {0} ); @@ -52,16 +52,12 @@ my %role_speeds = ( sub change_layer { my $self = shift; - my ($layer) = @_; + my ($layer, %params) = @_; $self->layer($layer); - my $z = $Slic3r::Config->z_offset + $layer->print_z * &Slic3r::SCALING_FACTOR; my $gcode = ""; - - $gcode .= $self->retract(move_z => $z); - $gcode .= $self->G0(undef, $z, 0, 'move to next layer (' . $layer->id . ')') - if $self->z != $z && !$self->lifted; + $gcode .= $self->move_z($layer->print_z) unless $params{dont_move_z}; $gcode .= $Slic3r::Config->replace_options($Slic3r::Config->layer_gcode) . "\n" if $Slic3r::Config->layer_gcode; @@ -69,6 +65,21 @@ sub change_layer { return $gcode; } +# this method accepts Z in scaled coordinates +sub move_z { + my $self = shift; + my ($z, $comment) = @_; + + $z = $Slic3r::Config->z_offset + $z * &Slic3r::SCALING_FACTOR; + + my $gcode = ""; + $gcode .= $self->retract(move_z => $z); + $gcode .= $self->G0(undef, $z, 0, $comment || 'move to next layer (' . $self->layer->id . ')') + if $self->z != $z && !$self->lifted; + + return $gcode; +} + sub extrude { my $self = shift; diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm index 4a2c89689..18169fd1d 100644 --- a/lib/Slic3r/Layer.pm +++ b/lib/Slic3r/Layer.pm @@ -18,14 +18,15 @@ has 'flow' => (is => 'ro', default => sub { $Slic3r::flow }); has 'slices' => (is => 'rw'); # ordered collection of extrusion paths to fill surfaces for support material -has 'support_fills' => (is => 'rw'); +has 'support_fills' => (is => 'rw'); +has 'support_interface_fills' => (is => 'rw'); sub _trigger_id { my $self = shift; $_->_trigger_layer for @{$self->regions || []}; } -# Z used for slicing +# Z used for slicing in scaled coordinates sub _build_slice_z { my $self = shift; @@ -36,17 +37,37 @@ sub _build_slice_z { / &Slic3r::SCALING_FACTOR; #/ } -# Z used for printing +# Z used for printing in scaled coordinates sub _build_print_z { my $self = shift; return ($Slic3r::Config->get_value('first_layer_height') + ($self->id * $Slic3r::Config->layer_height)) / &Slic3r::SCALING_FACTOR; } +# layer height in unscaled coordinates sub _build_height { my $self = shift; return $self->id == 0 ? $Slic3r::Config->get_value('first_layer_height') : $Slic3r::Config->layer_height; } +# layer height of interface paths in unscaled coordinates +sub support_material_interface_height { + my $self = shift; + + return $self->height if $self->id == 0; + + # this is not very correct because: + # - we should sum our height with the actual upper layers height (which might be different) + # - we should use the actual flow of the upper layer bridges, not the default one + # ...but we're close enough for now + return 2*$self->height - $self->flow->nozzle_diameter; +} + +# Z used for printing support material interface in scaled coordinates +sub support_material_interface_z { + my $self = shift; + return $self->print_z - ($self->height - $self->support_material_interface_height) / &Slic3r::SCALING_FACTOR; +} + sub region { my $self = shift; my ($region_id) = @_; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 06ef9e5c7..8053dfd2e 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -702,8 +702,8 @@ sub write_gcode { if $Slic3r::Config->bed_temperature && $Slic3r::Config->bed_temperature != $Slic3r::Config->first_layer_bed_temperature; } - # go to layer (just use the first one, we only need Z from it) - $gcode .= $gcodegen->change_layer($self->objects->[$object_copies->[0][0]]->layers->[$layer_id]); + # set new layer, but don't move Z as support material interfaces may need an intermediate one + $gcode .= $gcodegen->change_layer($self->objects->[$object_copies->[0][0]]->layers->[$layer_id], dont_move_z => 1); $gcodegen->elapsed_time(0); # extrude skirt @@ -742,6 +742,23 @@ sub write_gcode { $gcodegen->shift_x($shift[X] + unscale $copy->[X]); $gcodegen->shift_y($shift[Y] + unscale $copy->[Y]); + # 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 ($Slic3r::Config->support_material) { + $gcode .= $gcodegen->move_z($layer->support_material_interface_z) + if @{ $layer->support_interface_fills->paths }; + $gcode .= $gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]); + $gcode .= $gcodegen->extrude_path($_, 'support material interface') + for $layer->support_interface_fills->shortest_path($gcodegen->last_pos); + + $gcode .= $gcodegen->move_z($layer->print_z); + $gcode .= $gcodegen->extrude_path($_, 'support material') + for $layer->support_fills->shortest_path($gcodegen->last_pos); + } + + # set actual Z + $gcode .= $gcodegen->move_z($layer->print_z); + foreach my $region_id (0 .. ($self->regions_count-1)) { my $layerm = $layer->regions->[$region_id]; my $region = $self->regions->[$region_id]; @@ -766,13 +783,6 @@ sub write_gcode { } } } - - # extrude support material - if ($layer->support_fills) { - $gcode .= $gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]); - $gcode .= $gcodegen->extrude_path($_, 'support material') - for $layer->support_fills->shortest_path($gcodegen->last_pos); - } } return if !$gcode; diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index 09ed0b05e..dac760250 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -517,7 +517,8 @@ sub generate_support_material { # determine support regions in each layer (for upper layers) Slic3r::debugf "Detecting regions\n"; - my %layers = (); + my %layers = (); # this represents the areas of each layer having to support upper layers (excluding interfaces) + my %layers_interfaces = (); # this represents the areas of each layer having an overhang in the immediately upper layer { my @current_support_regions = (); # expolygons we've started to support (i.e. below the empty interface layers) my @queue = (); # the number of items of this array determines the number of empty interface layers @@ -525,6 +526,10 @@ sub generate_support_material { my $layer = $self->layers->[$i]; my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef; + # $queue[-1] contains the overhangs of the upper layer, regardless of any empty interface layers + # $queue[0] contains the overhangs of the first upper layer above the empty interface layers + $layers_interfaces{$i} = [@{ $queue[-1] || [] }]; + # step 1: generate support material in current layer (for upper layers) push @current_support_regions, @{ shift @queue } if @queue && $i < $#{$self->layers}; @@ -535,7 +540,10 @@ sub generate_support_material { $layers{$i} = diff_ex( [ map @$_, @current_support_regions ], - [ map @$_, map $_->offset_ex($distance_from_object), @{$layer->slices} ], + [ + (map @$_, map $_->offset_ex($distance_from_object), @{$layer->slices}), + (map @$_, @{ $layers_interfaces{$i} }), + ], ); $_->simplify($flow->scaled_spacing * 2) for @{$layers{$i}}; @@ -599,12 +607,13 @@ sub generate_support_material { Slic3r::debugf "Applying patterns\n"; { my $clip_pattern = sub { - my ($layer_id, $expolygons) = @_; + my ($layer_id, $expolygons, $height) = @_; my @paths = (); foreach my $expolygon (@$expolygons) { push @paths, map $_->pack, map { + $_->height($height); $_->flow_spacing($self->print->first_layer_support_material_flow->spacing) if $layer_id == 0; $_; @@ -616,29 +625,41 @@ sub generate_support_material { return @paths; }; my %layer_paths = (); + my %layer_interface_paths = (); + my $process_layer = sub { + my ($layer_id) = @_; + + my $layer = $self->layers->[$layer_id]; + my $paths = [ $clip_pattern->($layer_id, $layers{$layer_id}, $layer->height) ]; + my $interface_paths = [ $clip_pattern->($layer_id, $layers_interfaces{$layer_id}, $layer->support_material_interface_height) ]; + return ($paths, $interface_paths); + }; Slic3r::parallelize( items => [ keys %layers ], thread_cb => sub { my $q = shift; my $paths = {}; + my $interface_paths = {}; while (defined (my $layer_id = $q->dequeue)) { - $paths->{$layer_id} = [ $clip_pattern->($layer_id, $layers{$layer_id}) ]; + ($paths->{$layer_id}, $interface_paths->{$layer_id}) = $process_layer->($layer_id); } - return $paths; + return [ $paths, $interface_paths ]; }, collect_cb => sub { my $paths = shift; - $layer_paths{$_} = $paths->{$_} for keys %$paths; + ($layer_paths{$_}, $layer_interface_paths{$_}) = @{ $paths->{$_} } for keys %$paths; }, no_threads_cb => sub { - $layer_paths{$_} = [ $clip_pattern->($_, $layers{$_}) ] for keys %layers; + ($layer_paths{$_}, $layer_interface_paths{$_}) = $process_layer->($_) for keys %layers; }, ); foreach my $layer_id (keys %layer_paths) { my $layer = $self->layers->[$layer_id]; $layer->support_fills(Slic3r::ExtrusionPath::Collection->new); + $layer->support_interface_fills(Slic3r::ExtrusionPath::Collection->new); push @{$layer->support_fills->paths}, @{$layer_paths{$layer_id}}; + push @{$layer->support_interface_fills->paths}, @{$layer_interface_paths{$layer_id}}; } } }