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
|
2014-02-13 00:00:17 +00:00
|
|
|
if (defined $self->spiralvase) {
|
|
|
|
$self->spiralvase->enable(
|
|
|
|
($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
|
2014-02-13 15:06:52 +00:00
|
|
|
$self->gcodegen->enable_loop_clipping(!defined $self->spiralvase || !$self->spiralvase->enable);
|
2013-07-28 11:39:15 +00:00
|
|
|
|
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-03-16 23:39:07 +00:00
|
|
|
$gcode .= $self->gcodegen->placeholder_parser->process($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-04-05 08:09:05 +00:00
|
|
|
my $extruder_id = $extruder_ids[($i/@extruder_ids) % @extruder_ids];
|
|
|
|
$gcode .= $self->gcodegen->set_extruder($extruder_id)
|
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
|
|
|
|
2014-04-19 09:57:01 +00:00
|
|
|
# group extrusions by island
|
|
|
|
my @perimeters_by_island = map [], 0..$#{$layer->slices}; # slice idx => @perimeters
|
|
|
|
my @infill_by_island = map [], 0..$#{$layer->slices}; # slice idx => @fills
|
|
|
|
|
2014-04-25 17:11:17 +00:00
|
|
|
# NOTE: we assume $layer->slices was already ordered with chained_path()!
|
|
|
|
|
2014-04-19 09:57:01 +00:00
|
|
|
PERIMETER: foreach my $perimeter (@{$layerm->perimeters}) {
|
|
|
|
for my $i (0 .. $#{$layer->slices}-1) {
|
|
|
|
if ($layer->slices->[$i]->contour->contains_point($perimeter->first_point)) {
|
|
|
|
push @{ $perimeters_by_island[$i] }, $perimeter;
|
|
|
|
next PERIMETER;
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-19 09:57:01 +00:00
|
|
|
push @{ $perimeters_by_island[-1] }, $perimeter; # optimization
|
|
|
|
}
|
|
|
|
FILL: foreach my $fill (@{$layerm->fills}) {
|
|
|
|
for my $i (0 .. $#{$layer->slices}-1) {
|
|
|
|
if ($layer->slices->[$i]->contour->contains_point($fill->first_point)) {
|
|
|
|
push @{ $infill_by_island[$i] }, $fill;
|
|
|
|
next FILL;
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-19 09:57:01 +00:00
|
|
|
push @{ $infill_by_island[-1] }, $fill; # optimization
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 09:57:01 +00:00
|
|
|
for my $i (0 .. $#{$layer->slices}) {
|
2013-05-18 14:57:44 +00:00
|
|
|
# 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)) {
|
2014-04-19 09:57:01 +00:00
|
|
|
$gcode .= $self->_extrude_infill($infill_by_island[$i], $region);
|
|
|
|
$gcode .= $self->_extrude_perimeters($perimeters_by_island[$i], $region);
|
2013-05-18 14:57:44 +00:00
|
|
|
} else {
|
2014-04-19 09:57:01 +00:00
|
|
|
$gcode .= $self->_extrude_perimeters($perimeters_by_island[$i], $region);
|
|
|
|
$gcode .= $self->_extrude_infill($infill_by_island[$i], $region);
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# apply spiral vase post-processing if this layer contains suitable geometry
|
2014-02-13 15:06:52 +00:00
|
|
|
# (we must feed all the G-code into the post-processor, including the first
|
|
|
|
# bottom non-spiral layers otherwise it will mess with positions)
|
|
|
|
$gcode = $self->spiralvase->process_layer($gcode)
|
2014-02-13 00:00:17 +00:00
|
|
|
if defined $self->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;
|
2014-04-19 09:57:01 +00:00
|
|
|
my ($island_perimeters, $region) = @_;
|
2013-05-18 14:57:44 +00:00
|
|
|
|
2014-04-19 09:57:01 +00:00
|
|
|
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);
|
2014-04-19 09:57:01 +00:00
|
|
|
$gcode .= $self->gcodegen->extrude($_, 'perimeter') for @$island_perimeters;
|
2013-05-19 08:47:00 +00:00
|
|
|
return $gcode;
|
2013-05-18 14:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _extrude_infill {
|
|
|
|
my $self = shift;
|
2014-04-19 09:57:01 +00:00
|
|
|
my ($island_fills, $region) = @_;
|
2013-05-18 14:57:44 +00:00
|
|
|
|
2014-04-19 09:57:01 +00:00
|
|
|
return "" if !@$island_fills;
|
2013-05-18 14:57:44 +00:00
|
|
|
|
|
|
|
my $gcode = "";
|
2014-01-03 17:27:46 +00:00
|
|
|
$gcode .= $self->gcodegen->set_extruder($region->config->infill_extruder-1);
|
2014-04-19 09:57:01 +00:00
|
|
|
for my $fill (@$island_fills) {
|
2013-05-18 14:57:44 +00:00
|
|
|
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;
|