diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index 1d1952309..17f297e20 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -14,6 +14,7 @@ has 'standby_points' => (is => 'rw'); has 'enable_loop_clipping' => (is => 'rw', default => sub {1}); has 'enable_wipe' => (is => 'rw', default => sub {0}); # at least one extruder has wipe enabled has 'layer_count' => (is => 'ro', required => 1 ); +has '_layer_index' => (is => 'rw', default => sub {-1}); # just a counter has 'layer' => (is => 'rw'); has 'region' => (is => 'rw'); has '_layer_islands' => (is => 'rw'); @@ -104,6 +105,7 @@ sub change_layer { my ($self, $layer) = @_; $self->layer($layer); + $self->_layer_index($self->_layer_index + 1); # avoid computing islands and overhangs if they're not needed $self->_layer_islands($layer->islands); @@ -124,7 +126,7 @@ sub change_layer { my $gcode = ""; if ($self->print_config->gcode_flavor =~ /^(?:makerware|sailfish)$/) { $gcode .= sprintf "M73 P%s%s\n", - int(99 * ($layer->id / ($self->layer_count - 1))), + int(99 * ($self->_layer_index / ($self->layer_count - 1))), ($self->print_config->gcode_comments ? ' ; update progress' : ''); } if ($self->print_config->first_layer_acceleration) { diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 1e85395de..d5588d366 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -315,9 +315,11 @@ sub init_extruders { } } +# this value is not supposed to be compared with $layer->id +# since they have different semantics sub layer_count { my $self = shift; - return max(map { scalar @{$_->layers} } @{$self->objects}); + return max(map $_->layer_count, @{$self->objects}); } sub regions_count { @@ -444,15 +446,15 @@ sub process { items => sub { my @items = (); # [layer_id, region_id] for my $region_id (0 .. ($self->regions_count-1)) { - push @items, map [$_, $region_id], 0..($object->layer_count-1); + push @items, map [$_, $region_id], 0..$#{$object->layers}; } @items; }, thread_cb => sub { my $q = shift; while (defined (my $obj_layer = $q->dequeue)) { - my ($layer_id, $region_id) = @$obj_layer; - my $layerm = $object->layers->[$layer_id]->regions->[$region_id]; + my ($i, $region_id) = @$obj_layer; + my $layerm = $object->layers->[$i]->regions->[$region_id]; $layerm->fills->append( $object->fill_maker->make_fill($layerm) ); } }, @@ -559,29 +561,31 @@ EOF ($type eq 'contour' ? 'white' : 'black'); }; + my @layers = sort { $a->print_z <=> $b->print_z } + map { @{$_->layers}, @{$_->support_layers} } + @{$self->objects}; + + my $layer_id = -1; my @previous_layer_slices = (); - for my $layer_id (0..$self->layer_count-1) { - my @layers = map $_->layers->[$layer_id], @{$self->objects}; - printf $fh qq{ \n}, $layer_id, +(grep defined $_, @layers)[0]->slice_z; + for my $layer (@layers) { + $layer_id++; + # TODO: remove slic3r:z for raft layers + printf $fh qq{ \n}, $layer_id, unscale($layer->slice_z); my @current_layer_slices = (); - for my $obj_idx (0 .. $#{$self->objects}) { - my $layer = $self->objects->[$obj_idx]->layers->[$layer_id] or next; - - # sort slices so that the outermost ones come first - my @slices = sort { $a->contour->contains_point($b->contour->first_point) ? 0 : 1 } @{$layer->slices}; - foreach my $copy (@{$self->objects->[$obj_idx]->_shifted_copies}) { - foreach my $slice (@slices) { - my $expolygon = $slice->clone; - $expolygon->translate(@$copy); - $print_polygon->($expolygon->contour, 'contour'); - $print_polygon->($_, 'hole') for @{$expolygon->holes}; - push @current_layer_slices, $expolygon; - } + # sort slices so that the outermost ones come first + my @slices = sort { $a->contour->encloses_point($b->contour->[0]) ? 0 : 1 } @{$layer->slices}; + foreach my $copy (@{$layer->object->copies}) { + foreach my $slice (@slices) { + my $expolygon = $slice->clone; + $expolygon->translate(@$copy); + $print_polygon->($expolygon->contour, 'contour'); + $print_polygon->($_, 'hole') for @{$expolygon->holes}; + push @current_layer_slices, $expolygon; } } # generate support material - if ($self->has_support_material && $layer_id > 0) { + if ($self->has_support_material && $layer->id > 0) { my (@supported_slices, @unsupported_slices) = (); foreach my $expolygon (@current_layer_slices) { my $intersection = intersection_ex( diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index 4b1738723..612eb8555 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -98,9 +98,12 @@ sub delete_all_copies { $self->_trigger_copies; } +# this is the *total* layer count +# this value is not supposed to be compared with $layer->id +# since they have different semantics sub layer_count { my $self = shift; - return scalar @{ $self->layers }; + return scalar @{ $self->layers } + scalar @{ $self->support_layers }; } sub bounding_box { @@ -354,9 +357,9 @@ sub make_perimeters { my $region_perimeters = $region->config->perimeters; if ($region->config->extra_perimeters && $region_perimeters > 0 && $region->config->fill_density > 0) { - for my $layer_id (0 .. $self->layer_count-2) { - my $layerm = $self->layers->[$layer_id]->regions->[$region_id]; - my $upper_layerm = $self->layers->[$layer_id+1]->regions->[$region_id]; + for my $i (0 .. $#{$self->layers}-1) { + my $layerm = $self->layers->[$i]->regions->[$region_id]; + my $upper_layerm = $self->layers->[$i+1]->regions->[$region_id]; my $perimeter_spacing = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_spacing; my $overlap = $perimeter_spacing; # one perimeter @@ -393,8 +396,7 @@ sub make_perimeters { # only add the perimeter if there's an intersection with the collapsed area last CYCLE if !@{ intersection($diff, $hypothetical_perimeter) }; - - Slic3r::debugf " adding one more perimeter at layer %d\n", $layer_id; + Slic3r::debugf " adding one more perimeter at layer %d\n", $layerm->id; $slice->extra_perimeters($extra_perimeters); } } @@ -404,11 +406,11 @@ sub make_perimeters { Slic3r::parallelize( threads => $self->print->config->threads, - items => sub { 0 .. ($self->layer_count-1) }, + items => sub { 0 .. $#{$self->layers} }, thread_cb => sub { my $q = shift; - while (defined (my $layer_id = $q->dequeue)) { - $self->layers->[$layer_id]->make_perimeters; + while (defined (my $i = $q->dequeue)) { + $self->layers->[$i]->make_perimeters; } }, collect_cb => sub {}, @@ -428,7 +430,7 @@ sub detect_surfaces_type { Slic3r::debugf "Detecting solid surfaces...\n"; for my $region_id (0 .. ($self->print->regions_count-1)) { - for my $i (0 .. ($self->layer_count-1)) { + for my $i (0 .. $#{$self->layers}) { my $layerm = $self->layers->[$i]->regions->[$region_id]; # prepare a reusable subroutine to make surface differences @@ -661,8 +663,8 @@ sub process_external_surfaces { for my $region_id (0 .. ($self->print->regions_count-1)) { $self->layers->[0]->regions->[$region_id]->process_external_surfaces(undef); - for my $layer_id (1 .. ($self->layer_count-1)) { - $self->layers->[$layer_id]->regions->[$region_id]->process_external_surfaces($self->layers->[$layer_id-1]); + for my $i (1 .. $#{$self->layers}) { + $self->layers->[$i]->regions->[$region_id]->process_external_surfaces($self->layers->[$i-1]); } } } @@ -673,7 +675,7 @@ sub discover_horizontal_shells { Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n"; for my $region_id (0 .. ($self->print->regions_count-1)) { - for (my $i = 0; $i < $self->layer_count; $i++) { + for (my $i = 0; $i <= $#{$self->layers}; $i++) { my $layerm = $self->layers->[$i]->regions->[$region_id]; if ($layerm->config->solid_infill_every_layers && $layerm->config->fill_density > 0 @@ -705,7 +707,7 @@ sub discover_horizontal_shells { abs($n - $i) <= $solid_layers-1; ($type == S_TYPE_TOP) ? $n-- : $n++) { - next if $n < 0 || $n >= $self->layer_count; + next if $n < 0 || $n > $#{$self->layers}; Slic3r::debugf " looking for neighbors on layer %d...\n", $n; my $neighbor_fill_surfaces = $self->layers->[$n]->regions->[$region_id]->fill_surfaces; @@ -811,8 +813,7 @@ sub combine_infill { return unless defined first { $_->config->infill_every_layers > 1 && $_->config->fill_density > 0 } @{$self->print->regions}; - my $layer_count = $self->layer_count; - my @layer_heights = map $self->layers->[$_]->height, 0 .. $layer_count-1; + my @layer_heights = map $_->height, @{$self->layers}; for my $region_id (0 .. ($self->print->regions_count-1)) { my $region = $self->print->regions->[$region_id]; @@ -922,7 +923,7 @@ sub combine_infill { sub generate_support_material { my $self = shift; return unless ($self->config->support_material || $self->config->raft_layers > 0) - && $self->layer_count >= 2; + && scalar(@{$self->layers}) >= 2; my $first_layer_flow = Slic3r::Flow->new_from_width( width => ($self->config->first_layer_extrusion_width || $self->config->support_material_extrusion_width), diff --git a/t/gcode.t b/t/gcode.t index 4cfb64327..9105082a9 100644 --- a/t/gcode.t +++ b/t/gcode.t @@ -1,4 +1,4 @@ -use Test::More tests => 7; +use Test::More tests => 8; use strict; use warnings; @@ -81,4 +81,21 @@ use Slic3r::Test; ok $print->total_used_filament > 0, 'final retraction is not considered in total used filament'; } +{ + my $config = Slic3r::Config->new_from_defaults; + $config->set('gcode_flavor', 'sailfish'); + $config->set('raft_layers', 3); + my $print = Slic3r::Test::init_print('20mm_cube', config => $config); + my @percent = (); + Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub { + my ($self, $cmd, $args, $info) = @_; + + if ($cmd eq 'M73') { + push @percent, $args->{P}; + } + }); + # the extruder heater is turned off when M73 P100 is reached + ok !(defined first { $_ > 100 } @percent), 'M73 is never given more than 100%'; +} + __END__