Refactoring: moved Slic3r::GCode::Base to Slic3r::GCode::Writer

This commit is contained in:
Alessandro Ranellucci 2014-10-25 10:42:07 +02:00
parent c2e710d092
commit 7f57f007cd
12 changed files with 154 additions and 212 deletions

View file

@ -9,9 +9,10 @@ use Slic3r::Geometry qw(epsilon scale unscale scaled_epsilon points_coincide PI
use Slic3r::Geometry::Clipper qw(union_ex offset_ex);
use Slic3r::Surface ':types';
extends 'Slic3r::GCode::Base';
has 'config' => (is => 'ro', default => sub { Slic3r::Config::Full->new });
has '_writer' => (is => 'ro', default => sub { Slic3r::GCode::Writer->new },
handles => [qw(set_fan set_temperature set_bed_temperature
extruders reset_e multiple_extruders extruder)]);
has 'placeholder_parser' => (is => 'rw', default => sub { Slic3r::GCode::PlaceholderParser->new });
has 'ooze_prevention' => (is => 'rw');
has 'enable_loop_clipping' => (is => 'rw', default => sub {1});
@ -36,17 +37,17 @@ has 'wipe_path' => (is => 'rw');
sub apply_print_config {
my ($self, $print_config) = @_;
$self->SUPER::apply_print_config($print_config);
$self->_writer->apply_print_config($print_config);
$self->config->apply_print_config($print_config);
}
sub set_extruders {
my ($self, $extruder_ids) = @_;
$self->SUPER::set_extruders($extruder_ids);
$self->_writer->set_extruders($extruder_ids);
# enable wipe path generation if any extruder has wipe enabled
$self->enable_wipe(defined first { $_->wipe } values %{$self->_extruders});
$self->enable_wipe(defined first { $self->config->get_at('wipe', $_) } @$extruder_ids);
}
sub set_shift {
@ -82,15 +83,14 @@ sub change_layer {
my $gcode = "";
if (defined $self->layer_count) {
# TODO: cap this to 99% and add an explicit M73 P100 in the end G-code
$gcode .= $self->update_progress(int(99 * ($self->_layer_index / ($self->layer_count - 1))));
$gcode .= $self->_writer->update_progress(int(99 * ($self->_layer_index / ($self->layer_count - 1))));
}
my $z = $layer->print_z + $self->config->z_offset; # in unscaled coordinates
if ($self->_extruder->retract_layer_change && $self->will_move_z($z)) {
if ($self->config->get_at('retract_layer_change', $self->_writer->extruder->id) && $self->_writer->will_move_z($z)) {
$gcode .= $self->retract;
$gcode .= $self->lift;
}
$gcode .= $self->travel_to_z($z, 'move to next layer (' . $self->layer->id . ')');
$gcode .= $self->_writer->travel_to_z($z, 'move to next layer (' . $self->layer->id . ')');
return $gcode;
}
@ -120,7 +120,7 @@ sub extrude_loop {
} elsif ($self->config->seam_position eq 'nearest' || $self->config->seam_position eq 'aligned') {
# simplify polygon in order to skip false positives in concave/convex detection
my $polygon = $loop->polygon;
my @simplified = @{$polygon->simplify(scale $self->_extruder->nozzle_diameter/2)};
my @simplified = @{$polygon->simplify(scale $self->config->get_at('nozzle_diameter', $self->_writer->extruder->id)/2)};
# concave vertices have priority
my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified;
@ -166,7 +166,7 @@ sub extrude_loop {
# if polyline was shorter than the clipping distance we'd get a null polyline, so
# we discard it in that case
my $clip_length = $self->enable_loop_clipping
? scale($self->_extruder->nozzle_diameter) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER
? scale($self->config->get_at('nozzle_diameter', $self->_writer->extruder->id)) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER
: 0;
# get paths
@ -183,7 +183,7 @@ sub extrude_loop {
my $gcode = join '', map $self->_extrude_path($_, $description, $speed), @paths;
# reset acceleration
$gcode .= $self->set_acceleration($self->config->default_acceleration);
$gcode .= $self->_writer->set_acceleration($self->config->default_acceleration);
$self->wipe_path($paths[-1]->polyline->clone) if $self->enable_wipe; # TODO: don't limit wipe to last path
@ -200,7 +200,7 @@ sub extrude_loop {
# we make sure we don't exceed the segment length because we don't know
# the rotation of the second segment so we might cross the object boundary
my $first_segment = Slic3r::Line->new(@$last_path_polyline[0,1]);
my $distance = min(scale($self->_extruder->nozzle_diameter), $first_segment->length);
my $distance = min(scale($self->config->get_at('nozzle_diameter', $self->_writer->extruder->id)), $first_segment->length);
my $point = $first_segment->point_at($distance);
$point->rotate($angle, $last_path_polyline->first_point);
@ -217,7 +217,7 @@ sub extrude_path {
my $gcode = $self->_extrude_path($path, $description, $speed);
# reset acceleration
$gcode .= $self->set_acceleration($self->config->default_acceleration);
$gcode .= $self->_writer->set_acceleration($self->config->default_acceleration);
return $gcode;
}
@ -252,12 +252,12 @@ sub _extrude_path {
} else {
$acceleration = $self->config->default_acceleration;
}
$gcode .= $self->set_acceleration($acceleration);
$gcode .= $self->_writer->set_acceleration($acceleration);
}
# calculate extrusion length per distance unit
my $e_per_mm = $self->_extruder->e_per_mm3 * $path->mm3_per_mm;
$e_per_mm = 0 if !$self->_extrusion_axis;
my $e_per_mm = $self->_writer->extruder->e_per_mm3 * $path->mm3_per_mm;
$e_per_mm = 0 if !$self->_writer->_extrusion_axis;
# set speed
my $F;
@ -289,10 +289,11 @@ sub _extrude_path {
$gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge;
my $path_length = unscale $path->length;
{
$gcode .= $path->gcode($self->_extruder, $e_per_mm, $F,
$self->shift_x - $self->_extruder->extruder_offset->x,
$self->shift_y - $self->_extruder->extruder_offset->y, #,,
$self->_extrusion_axis,
my $extruder_offset = $self->config->get_at('extruder_offset', $self->_writer->extruder->id);
$gcode .= $path->gcode($self->_writer->extruder, $e_per_mm, $F,
$self->shift_x - $extruder_offset->x,
$self->shift_y - $extruder_offset->y, #,,
$self->_writer->_extrusion_axis,
$self->config->gcode_comments ? " ; $description" : "");
if ($self->enable_wipe) {
@ -324,7 +325,7 @@ sub travel_to {
# skip retraction if the travel move is contained in an island in the current layer
# *and* in an island in the upper layer (so that the ooze will not be visible)
if ($travel->length < scale $self->_extruder->retract_before_travel
if ($travel->length < scale $self->config->get_at('retract_before_travel', $self->_writer->extruder->id)
|| ($self->config->only_retract_when_crossing_perimeters
&& $self->config->fill_density > 0
&& (first { $_->contains_line($travel) } @{$self->_upper_layer_islands})
@ -332,11 +333,11 @@ sub travel_to {
|| (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && (first { $_->contains_line($travel) } @{$self->layer->support_islands}))
) {
$self->straight_once(0);
$gcode .= $self->travel_to_xy($self->point_to_gcode($point), $comment);
$gcode .= $self->_writer->travel_to_xy($self->point_to_gcode($point), $comment);
} elsif (!$self->config->avoid_crossing_perimeters || $self->straight_once) {
$self->straight_once(0);
$gcode .= $self->retract;
$gcode .= $self->travel_to_xy($self->point_to_gcode($point), $comment);
$gcode .= $self->_writer->travel_to_xy($self->point_to_gcode($point), $comment);
} else {
if ($self->new_object) {
$self->new_object(0);
@ -383,7 +384,7 @@ sub _plan {
# append the actual path and return
# use G1 because we rely on paths being straight (G0 may make round paths)
$gcode .= join '',
map $self->travel_to_xy($self->point_to_gcode($_->b), $comment),
map $self->_writer->travel_to_xy($self->point_to_gcode($_->b), $comment),
@travel;
return $gcode;
}
@ -391,25 +392,25 @@ sub _plan {
sub retract {
my ($self, $toolchange) = @_;
return "" if !defined $self->_extruder;
return "" if !defined $self->_writer->extruder;
my $gcode = "";
# wipe (if it's enabled for this extruder and we have a stored wipe path)
if ($self->_extruder->wipe && $self->wipe_path) {
if ($self->config->get_at('wipe', $self->_writer->extruder->id) && $self->wipe_path) {
# Reduce feedrate a bit; travel speed is often too high to move on existing material.
# Too fast = ripping of existing material; too slow = short wipe path, thus more blob.
my $wipe_speed = $self->gcode_config->travel_speed * 0.8;
my $wipe_speed = $self->_writer->config->travel_speed * 0.8;
# get the retraction length
my $length = $toolchange
? $self->_extruder->retract_length
: $self->_extruder->retract_length_toolchange;
? $self->_writer->extruder->retract_length
: $self->_writer->extruder->retract_length_toolchange;
# Calculate how long we need to travel in order to consume the required
# amount of retraction. In other words, how far do we move in XY at $wipe_speed
# for the time needed to consume retract_length at retract_speed?
my $wipe_dist = scale($length / $self->_extruder->retract_speed * $wipe_speed);
my $wipe_dist = scale($length / $self->_writer->extruder->retract_speed * $wipe_speed);
# Take the stored wipe path and replace first point with the current actual position
# (they might be different, for example, in case of loop clipping).
@ -427,25 +428,25 @@ sub retract {
# Reduce retraction length a bit to avoid effective retraction speed to be greater than the configured one
# due to rounding (TODO: test and/or better math for this)
my $dE = $length * ($segment_length / $wipe_dist) * 0.95;
$gcode .= $self->set_speed($wipe_speed*60);
$gcode .= $self->extrude_to_xy(
$gcode .= $self->_writer->set_speed($wipe_speed*60);
$gcode .= $self->_writer->extrude_to_xy(
$self->point_to_gcode($line->b),
-$dE,
'retract;_WIPE',
);
$retracted += $dE;
}
$self->_extruder->set_retracted($self->_extruder->retracted + $retracted);
$self->_writer->extruder->set_retracted($self->_writer->extruder->retracted + $retracted);
}
# The parent class will decide whether we need to perform an actual retraction
# (the extruder might be already retracted fully or partially). We call these
# methods even if we performed wipe, since this will ensure the entire retraction
# length is honored in case wipe path was too short.p
$gcode .= $toolchange ? $self->retract_for_toolchange : $self->SUPER::retract;
$gcode .= $toolchange ? $self->_writer->retract_for_toolchange : $self->_writer->retract;
$gcode .= $self->reset_e;
$gcode .= $self->lift;
$gcode .= $self->_writer->reset_e;
$gcode .= $self->_writer->lift;
return $gcode;
}
@ -454,8 +455,8 @@ sub unretract {
my ($self) = @_;
my $gcode = "";
$gcode .= $self->unlift;
$gcode .= $self->SUPER::unretract('compensate retraction');
$gcode .= $self->_writer->unlift;
$gcode .= $self->_writer->unretract('compensate retraction');
return $gcode;
}
@ -463,29 +464,30 @@ sub unretract {
sub point_to_gcode {
my ($self, $point) = @_;
my $extruder_offset = $self->config->get_at('extruder_offset', $self->_writer->extruder->id);
return Slic3r::Pointf->new(
($point->x * &Slic3r::SCALING_FACTOR) + $self->shift_x - $self->_extruder->extruder_offset->x,
($point->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $self->_extruder->extruder_offset->y, #**
($point->x * &Slic3r::SCALING_FACTOR) + $self->shift_x - $extruder_offset->x,
($point->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $extruder_offset->y, #**
);
}
sub set_extruder {
my ($self, $extruder_id) = @_;
return "" if !$self->need_toolchange($extruder_id);
return "" if !$self->_writer->need_toolchange($extruder_id);
# if we are running a single-extruder setup, just set the extruder and return nothing
if (!$self->_multiple_extruders) {
return $self->_toolchange($extruder_id);
if (!$self->_writer->multiple_extruders) {
return $self->_writer->toolchange($extruder_id);
}
# prepend retraction on the current extruder
my $gcode = $self->retract(1);
# append custom toolchange G-code
if (defined $self->_extruder && $self->config->toolchange_gcode) {
if (defined $self->_writer->extruder && $self->config->toolchange_gcode) {
$gcode .= sprintf "%s\n", $self->placeholder_parser->process($self->config->toolchange_gcode, {
previous_extruder => $self->_extruder->id,
previous_extruder => $self->_writer->extruder->id,
next_extruder => $extruder_id,
});
}
@ -493,10 +495,10 @@ sub set_extruder {
# if ooze prevention is enabled, park current extruder in the nearest
# standby point and set it to the standby temperature
$gcode .= $self->ooze_prevention->pre_toolchange($self)
if $self->ooze_prevention && defined $self->_extruder;
if $self->ooze_prevention && defined $self->_writer->extruder;
# append the toolchange command
$gcode .= $self->_toolchange($extruder_id);
$gcode .= $self->_writer->toolchange($extruder_id);
# set the new extruder to the operating temperature
$gcode .= $self->ooze_prevention->post_toolchange($self)