2013-05-18 14:57:44 +00:00
|
|
|
package Slic3r::GCode::Layer;
|
|
|
|
use Moo;
|
|
|
|
|
2013-05-19 13:33:54 +00:00
|
|
|
use List::Util qw(first);
|
2013-05-18 14:57:44 +00:00
|
|
|
use Slic3r::Geometry qw(X Y unscale);
|
|
|
|
|
2014-01-04 23:36:33 +00:00
|
|
|
has 'print' => (is => 'ro', required => 1);
|
|
|
|
has 'gcodegen' => (is => 'ro', required => 1, handles => [qw(extruders)]);
|
2013-12-15 15:17:12 +00:00
|
|
|
has 'shift' => (is => 'ro', default => sub { [0,0] });
|
2013-05-18 14:57:44 +00:00
|
|
|
|
|
|
|
has 'spiralvase' => (is => 'lazy');
|
2013-08-28 14:51:58 +00:00
|
|
|
has 'vibration_limit' => (is => 'lazy');
|
2013-08-28 17:50:16 +00:00
|
|
|
has 'arc_fitting' => (is => 'lazy');
|
2013-05-30 18:06:05 +00:00
|
|
|
has 'skirt_done' => (is => 'rw', default => sub { {} }); # print_z => 1
|
2013-05-18 14:57:44 +00:00
|
|
|
has 'brim_done' => (is => 'rw');
|
|
|
|
has 'second_layer_things_done' => (is => 'rw');
|
|
|
|
has '_last_obj_copy' => (is => 'rw');
|
|
|
|
|
|
|
|
sub _build_spiralvase {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-01-02 16:24:23 +00:00
|
|
|
return $self->print->config->spiral_vase
|
|
|
|
? Slic3r::GCode::SpiralVase->new(config => $self->print->config)
|
2013-05-18 14:57:44 +00:00
|
|
|
: undef;
|
|
|
|
}
|
|
|
|
|
2013-08-28 14:51:58 +00:00
|
|
|
sub _build_vibration_limit {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-01-02 16:24:23 +00:00
|
|
|
return $self->print->config->vibration_limit
|
|
|
|
? Slic3r::GCode::VibrationLimit->new(config => $self->print->config)
|
2013-08-28 14:51:58 +00:00
|
|
|
: undef;
|
|
|
|
}
|
|
|
|
|
2013-08-28 17:50:16 +00:00
|
|
|
sub _build_arc_fitting {
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-01-02 16:24:23 +00:00
|
|
|
return $self->print->config->gcode_arcs
|
|
|
|
? Slic3r::GCode::ArcFitting->new(config => $self->print->config)
|
2013-08-28 17:50:16 +00:00
|
|
|
: undef;
|
|
|
|
}
|
|
|
|
|
2013-05-18 14:57:44 +00:00
|
|
|
sub process_layer {
|
|
|
|
my $self = shift;
|
|
|
|
my ($layer, $object_copies) = @_;
|
|
|
|
my $gcode = "";
|
|
|
|
|
2014-01-02 16:24:23 +00:00
|
|
|
my $object = $layer->object;
|
|
|
|
|
2013-07-28 11:39:15 +00:00
|
|
|
# check whether we're going to apply spiralvase logic
|
|
|
|
my $spiralvase = defined $self->spiralvase
|
2014-01-02 16:24:23 +00:00
|
|
|
&& ($layer->id > 0 || $self->print->config->brim_width == 0)
|
|
|
|
&& ($layer->id >= $self->print->config->skirt_height && $self->print->config->skirt_height != -1)
|
|
|
|
&& !defined(first { $_->config->bottom_solid_layers > $layer->id } @{$layer->regions});
|
2013-07-28 11:39:15 +00:00
|
|
|
|
|
|
|
# if we're going to apply spiralvase to this layer, disable loop clipping
|
|
|
|
$self->gcodegen->enable_loop_clipping(!$spiralvase);
|
|
|
|
|
2013-05-18 14:57:44 +00:00
|
|
|
if (!$self->second_layer_things_done && $layer->id == 1) {
|
2014-01-04 23:36:33 +00:00
|
|
|
for my $extruder_id (sort keys %{$self->extruders}) {
|
|
|
|
my $extruder = $self->extruders->{$extruder_id};
|
|
|
|
$gcode .= $self->gcodegen->set_temperature($extruder->temperature, 0, $extruder->id)
|
|
|
|
if $extruder->temperature && $extruder->temperature != $extruder->first_layer_temperature;
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
2014-01-02 16:24:23 +00:00
|
|
|
$gcode .= $self->gcodegen->set_bed_temperature($self->print->config->bed_temperature)
|
|
|
|
if $self->print->config->bed_temperature && $self->print->config->bed_temperature != $self->print->config->first_layer_bed_temperature;
|
2013-05-18 14:57:44 +00:00
|
|
|
$self->second_layer_things_done(1);
|
|
|
|
}
|
|
|
|
|
2013-08-09 17:46:20 +00:00
|
|
|
# set new layer - this will change Z and force a retraction if retract_layer_change is enabled
|
2013-05-18 14:57:44 +00:00
|
|
|
$gcode .= $self->gcodegen->change_layer($layer);
|
2014-01-02 16:24:23 +00:00
|
|
|
$gcode .= $self->gcodegen->replace_variables($self->print->config->layer_gcode, {
|
2013-08-09 17:46:20 +00:00
|
|
|
layer_num => $self->gcodegen->layer->id,
|
2014-01-02 16:24:23 +00:00
|
|
|
}) . "\n" if $self->print->config->layer_gcode;
|
2013-05-18 14:57:44 +00:00
|
|
|
|
|
|
|
# extrude skirt
|
2014-01-02 16:24:23 +00:00
|
|
|
if (((values %{$self->skirt_done}) < $self->print->config->skirt_height || $self->print->config->skirt_height == -1)
|
2013-12-24 14:15:53 +00:00
|
|
|
&& !$self->skirt_done->{$layer->print_z}) {
|
2013-05-18 14:57:44 +00:00
|
|
|
$self->gcodegen->set_shift(@{$self->shift});
|
2014-01-04 23:36:33 +00:00
|
|
|
my @extruder_ids = sort keys %{$self->extruders};
|
|
|
|
$gcode .= $self->gcodegen->set_extruder($extruder_ids[0]);
|
2013-05-18 14:57:44 +00:00
|
|
|
# skip skirt if we have a large brim
|
2014-01-02 16:24:23 +00:00
|
|
|
if ($layer->id < $self->print->config->skirt_height || $self->print->config->skirt_height == -1) {
|
2013-05-18 14:57:44 +00:00
|
|
|
# distribute skirt loops across all extruders
|
2013-09-16 08:33:30 +00:00
|
|
|
my @skirt_loops = @{$self->print->skirt};
|
|
|
|
for my $i (0 .. $#skirt_loops) {
|
2013-05-18 14:57:44 +00:00
|
|
|
# when printing layers > 0 ignore 'min_skirt_length' and
|
|
|
|
# just use the 'skirts' setting; also just use the current extruder
|
2014-01-02 16:24:23 +00:00
|
|
|
last if ($layer->id > 0) && ($i >= $self->print->config->skirts);
|
2014-01-04 23:36:33 +00:00
|
|
|
$gcode .= $self->gcodegen->set_extruder(($i/@extruder_ids) % @extruder_ids)
|
2013-05-18 14:57:44 +00:00
|
|
|
if $layer->id == 0;
|
2013-09-16 08:33:30 +00:00
|
|
|
$gcode .= $self->gcodegen->extrude_loop($skirt_loops[$i], 'skirt');
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-30 18:06:05 +00:00
|
|
|
$self->skirt_done->{$layer->print_z} = 1;
|
2013-05-18 14:57:44 +00:00
|
|
|
$self->gcodegen->straight_once(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
# extrude brim
|
|
|
|
if (!$self->brim_done) {
|
2014-01-03 17:27:46 +00:00
|
|
|
$gcode .= $self->gcodegen->set_extruder($self->print->objects->[0]->config->support_material_extruder-1);
|
2013-05-18 14:57:44 +00:00
|
|
|
$self->gcodegen->set_shift(@{$self->shift});
|
|
|
|
$gcode .= $self->gcodegen->extrude_loop($_, 'brim') for @{$self->print->brim};
|
|
|
|
$self->brim_done(1);
|
|
|
|
$self->gcodegen->straight_once(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $copy (@$object_copies) {
|
|
|
|
$self->gcodegen->new_object(1) if ($self->_last_obj_copy // '') ne "$copy";
|
|
|
|
$self->_last_obj_copy("$copy");
|
|
|
|
|
|
|
|
$self->gcodegen->set_shift(map $self->shift->[$_] + unscale $copy->[$_], X,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
|
2014-01-02 16:24:23 +00:00
|
|
|
if ($layer->isa('Slic3r::Layer::Support')) {
|
2013-09-17 21:38:23 +00:00
|
|
|
if ($layer->support_interface_fills->count > 0) {
|
2014-01-03 17:27:46 +00:00
|
|
|
$gcode .= $self->gcodegen->set_extruder($object->config->support_material_interface_extruder-1);
|
2014-01-02 16:24:23 +00:00
|
|
|
my %params = (speed => $object->config->support_material_speed*60);
|
|
|
|
$gcode .= $self->gcodegen->extrude_path($_, 'support material interface', %params)
|
2013-08-29 09:47:59 +00:00
|
|
|
for @{$layer->support_interface_fills->chained_path_from($self->gcodegen->last_pos, 0)};
|
2013-07-29 18:49:54 +00:00
|
|
|
}
|
2013-09-17 21:38:23 +00:00
|
|
|
if ($layer->support_fills->count > 0) {
|
2014-01-03 17:27:46 +00:00
|
|
|
$gcode .= $self->gcodegen->set_extruder($object->config->support_material_extruder-1);
|
2014-01-02 16:24:23 +00:00
|
|
|
my %params = (speed => $object->config->support_material_speed*60);
|
|
|
|
$gcode .= $self->gcodegen->extrude_path($_, 'support material', %params)
|
2013-08-29 09:47:59 +00:00
|
|
|
for @{$layer->support_fills->chained_path_from($self->gcodegen->last_pos, 0)};
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# tweak region ordering to save toolchanges
|
|
|
|
my @region_ids = 0 .. ($self->print->regions_count-1);
|
|
|
|
if ($self->gcodegen->multiple_extruders) {
|
|
|
|
my $last_extruder = $self->gcodegen->extruder;
|
2014-01-02 16:24:23 +00:00
|
|
|
my $best_region_id = first { $self->print->regions->[$_]->config->perimeter_extruder-1 eq $last_extruder } @region_ids;
|
2013-05-18 14:57:44 +00:00
|
|
|
@region_ids = ($best_region_id, grep $_ != $best_region_id, @region_ids) if $best_region_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $region_id (@region_ids) {
|
2013-07-29 18:49:54 +00:00
|
|
|
my $layerm = $layer->regions->[$region_id] or next;
|
2013-05-18 14:57:44 +00:00
|
|
|
my $region = $self->print->regions->[$region_id];
|
2014-01-02 16:24:23 +00:00
|
|
|
$self->gcodegen->region($region);
|
2013-05-18 14:57:44 +00:00
|
|
|
|
|
|
|
my @islands = ();
|
2014-01-02 16:24:23 +00:00
|
|
|
if ($self->print->config->avoid_crossing_perimeters) {
|
2013-06-02 17:49:21 +00:00
|
|
|
push @islands, { perimeters => [], fills => [] }
|
|
|
|
for 1 .. (@{$layer->slices} || 1); # make sure we have at least one island hash to avoid failure of the -1 subscript below
|
2013-05-18 14:57:44 +00:00
|
|
|
PERIMETER: foreach my $perimeter (@{$layerm->perimeters}) {
|
|
|
|
for my $i (0 .. $#{$layer->slices}-1) {
|
2013-11-21 15:21:42 +00:00
|
|
|
if ($layer->slices->[$i]->contour->contains_point($perimeter->first_point)) {
|
2013-07-16 07:49:34 +00:00
|
|
|
push @{ $islands[$i]{perimeters} }, $perimeter;
|
2013-05-18 14:57:44 +00:00
|
|
|
next PERIMETER;
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 07:49:34 +00:00
|
|
|
push @{ $islands[-1]{perimeters} }, $perimeter; # optimization
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
FILL: foreach my $fill (@{$layerm->fills}) {
|
|
|
|
for my $i (0 .. $#{$layer->slices}-1) {
|
2013-11-21 15:21:42 +00:00
|
|
|
if ($layer->slices->[$i]->contour->contains_point($fill->first_point)) {
|
2013-07-16 07:49:34 +00:00
|
|
|
push @{ $islands[$i]{fills} }, $fill;
|
2013-05-18 14:57:44 +00:00
|
|
|
next FILL;
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 07:49:34 +00:00
|
|
|
push @{ $islands[-1]{fills} }, $fill; # optimization
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
push @islands, {
|
|
|
|
perimeters => $layerm->perimeters,
|
|
|
|
fills => $layerm->fills,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $island (@islands) {
|
|
|
|
# give priority to infill if we were already using its extruder and it wouldn't
|
|
|
|
# be good for perimeters
|
2014-01-02 16:24:23 +00:00
|
|
|
if ($self->print->config->infill_first
|
|
|
|
|| ($self->gcodegen->multiple_extruders && $region->config->infill_extruder-1 == $self->gcodegen->extruder->id && $region->config->infill_extruder != $region->config->perimeter_extruder)) {
|
2013-05-18 14:57:44 +00:00
|
|
|
$gcode .= $self->_extrude_infill($island, $region);
|
|
|
|
$gcode .= $self->_extrude_perimeters($island, $region);
|
|
|
|
} else {
|
|
|
|
$gcode .= $self->_extrude_perimeters($island, $region);
|
|
|
|
$gcode .= $self->_extrude_infill($island, $region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# apply spiral vase post-processing if this layer contains suitable geometry
|
|
|
|
$gcode = $self->spiralvase->process_layer($gcode, $layer)
|
2013-07-28 11:39:15 +00:00
|
|
|
if $spiralvase;
|
2013-05-18 14:57:44 +00:00
|
|
|
|
2013-08-28 14:51:58 +00:00
|
|
|
# apply vibration limit if enabled
|
|
|
|
$gcode = $self->vibration_limit->process($gcode)
|
2014-01-02 16:24:23 +00:00
|
|
|
if $self->print->config->vibration_limit != 0;
|
2013-08-28 14:51:58 +00:00
|
|
|
|
2013-08-28 17:50:16 +00:00
|
|
|
# apply arc fitting if enabled
|
|
|
|
$gcode = $self->arc_fitting->process($gcode)
|
2014-01-02 16:24:23 +00:00
|
|
|
if $self->print->config->gcode_arcs;
|
2013-08-28 17:50:16 +00:00
|
|
|
|
2013-05-18 14:57:44 +00:00
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _extrude_perimeters {
|
|
|
|
my $self = shift;
|
|
|
|
my ($island, $region) = @_;
|
|
|
|
|
|
|
|
return "" if !@{ $island->{perimeters} };
|
2013-05-19 08:47:00 +00:00
|
|
|
|
|
|
|
my $gcode = "";
|
2014-01-03 17:27:46 +00:00
|
|
|
$gcode .= $self->gcodegen->set_extruder($region->config->perimeter_extruder-1);
|
2013-05-19 08:47:00 +00:00
|
|
|
$gcode .= $self->gcodegen->extrude($_, 'perimeter') for @{ $island->{perimeters} };
|
|
|
|
return $gcode;
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _extrude_infill {
|
|
|
|
my $self = shift;
|
|
|
|
my ($island, $region) = @_;
|
|
|
|
|
|
|
|
return "" if !@{ $island->{fills} };
|
|
|
|
|
|
|
|
my $gcode = "";
|
2014-01-03 17:27:46 +00:00
|
|
|
$gcode .= $self->gcodegen->set_extruder($region->config->infill_extruder-1);
|
2013-05-18 14:57:44 +00:00
|
|
|
for my $fill (@{ $island->{fills} }) {
|
|
|
|
if ($fill->isa('Slic3r::ExtrusionPath::Collection')) {
|
|
|
|
$gcode .= $self->gcodegen->extrude($_, 'fill')
|
2013-08-29 09:47:59 +00:00
|
|
|
for @{$fill->chained_path_from($self->gcodegen->last_pos, 0)};
|
2013-05-18 14:57:44 +00:00
|
|
|
} else {
|
|
|
|
$gcode .= $self->gcodegen->extrude($fill, 'fill') ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|