More refactoring to GCode.pm: make writer() public and fix usage of Slic3r::GCode without a Slic3r::Layer object

This commit is contained in:
Alessandro Ranellucci 2014-10-28 21:47:09 +01:00
parent a38ec14cb1
commit 268de5c8e5
6 changed files with 97 additions and 84 deletions

View File

@ -15,13 +15,12 @@ use Slic3r::Surface ':types';
has 'origin' => (is => 'rw', default => sub { Slic3r::Pointf->new }); has 'origin' => (is => 'rw', default => sub { Slic3r::Pointf->new });
has 'config' => (is => 'ro', default => sub { Slic3r::Config::Full->new }); has 'config' => (is => 'ro', default => sub { Slic3r::Config::Full->new });
has '_writer' => (is => 'ro', default => sub { Slic3r::GCode::Writer->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 'placeholder_parser' => (is => 'rw', default => sub { Slic3r::GCode::PlaceholderParser->new });
has 'ooze_prevention' => (is => 'rw'); has 'ooze_prevention' => (is => 'rw');
has 'enable_loop_clipping' => (is => 'rw', default => sub {1}); 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 'enable_wipe' => (is => 'rw', default => sub {0}); # at least one extruder has wipe enabled
has 'enable_cooling_markers' => (is =>'rw', default => sub {0});
has 'layer_count' => (is => 'ro'); has 'layer_count' => (is => 'ro');
has '_layer_index' => (is => 'rw', default => sub {-1}); # just a counter has '_layer_index' => (is => 'rw', default => sub {-1}); # just a counter
has 'layer' => (is => 'rw'); has 'layer' => (is => 'rw');
@ -32,6 +31,7 @@ has '_external_mp' => (is => 'rw');
has '_layer_mp' => (is => 'rw'); has '_layer_mp' => (is => 'rw');
has 'new_object' => (is => 'rw', default => sub {0}); # this flag triggers the use of the external configuration space for avoid_crossing_perimeters for the next travel move has 'new_object' => (is => 'rw', default => sub {0}); # this flag triggers the use of the external configuration space for avoid_crossing_perimeters for the next travel move
has 'straight_once' => (is => 'rw', default => sub {1}); # this flag disables avoid_crossing_perimeters just for the next travel move has 'straight_once' => (is => 'rw', default => sub {1}); # this flag disables avoid_crossing_perimeters just for the next travel move
has 'first_layer' => (is => 'rw', default => sub {0}); # this flag triggers first layer speeds
has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } ); has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
has '_wipe_path' => (is => 'rw'); has '_wipe_path' => (is => 'rw');
@ -39,14 +39,14 @@ has '_wipe_path' => (is => 'rw');
sub apply_print_config { sub apply_print_config {
my ($self, $print_config) = @_; my ($self, $print_config) = @_;
$self->_writer->apply_print_config($print_config); $self->writer->apply_print_config($print_config);
$self->config->apply_print_config($print_config); $self->config->apply_print_config($print_config);
} }
sub set_extruders { sub set_extruders {
my ($self, $extruder_ids) = @_; my ($self, $extruder_ids) = @_;
$self->_writer->set_extruders($extruder_ids); $self->writer->set_extruders($extruder_ids);
# enable wipe path generation if any extruder has wipe enabled # enable wipe path generation if any extruder has wipe enabled
$self->enable_wipe(defined first { $self->config->get_at('wipe', $_) } @$extruder_ids); $self->enable_wipe(defined first { $self->config->get_at('wipe', $_) } @$extruder_ids);
@ -76,6 +76,7 @@ sub change_layer {
$self->layer($layer); $self->layer($layer);
$self->_layer_index($self->_layer_index + 1); $self->_layer_index($self->_layer_index + 1);
$self->first_layer($layer->id == 0);
# avoid computing islands and overhangs if they're not needed # avoid computing islands and overhangs if they're not needed
$self->_layer_islands($layer->islands); $self->_layer_islands($layer->islands);
@ -89,14 +90,14 @@ sub change_layer {
my $gcode = ""; my $gcode = "";
if (defined $self->layer_count) { if (defined $self->layer_count) {
# TODO: cap this to 99% and add an explicit M73 P100 in the end G-code # TODO: cap this to 99% and add an explicit M73 P100 in the end G-code
$gcode .= $self->_writer->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 my $z = $layer->print_z + $self->config->z_offset; # in unscaled coordinates
if ($self->config->get_at('retract_layer_change', $self->_writer->extruder->id) && $self->_writer->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->retract;
} }
$gcode .= $self->_writer->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; return $gcode;
} }
@ -126,7 +127,7 @@ sub extrude_loop {
} elsif ($self->config->seam_position eq 'nearest' || $self->config->seam_position eq 'aligned') { } 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 # simplify polygon in order to skip false positives in concave/convex detection
my $polygon = $loop->polygon; my $polygon = $loop->polygon;
my @simplified = @{$polygon->simplify(scale $self->config->get_at('nozzle_diameter', $self->_writer->extruder->id)/2)}; my @simplified = @{$polygon->simplify(scale $self->config->get_at('nozzle_diameter', $self->writer->extruder->id)/2)};
# concave vertices have priority # concave vertices have priority
my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified; my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified;
@ -135,7 +136,7 @@ sub extrude_loop {
@candidates = map @{$_->convex_points(PI*2/3)}, @simplified if !@candidates; @candidates = map @{$_->convex_points(PI*2/3)}, @simplified if !@candidates;
# retrieve the last start position for this object # retrieve the last start position for this object
my $obj_ptr; my $obj_ptr = 0;
if (defined $self->layer) { if (defined $self->layer) {
$obj_ptr = $self->layer->object->ptr; $obj_ptr = $self->layer->object->ptr;
if (defined $self->_seam_position->{$self->layer->object}) { if (defined $self->_seam_position->{$self->layer->object}) {
@ -172,7 +173,7 @@ sub extrude_loop {
# if polyline was shorter than the clipping distance we'd get a null polyline, so # if polyline was shorter than the clipping distance we'd get a null polyline, so
# we discard it in that case # we discard it in that case
my $clip_length = $self->enable_loop_clipping my $clip_length = $self->enable_loop_clipping
? scale($self->config->get_at('nozzle_diameter', $self->_writer->extruder->id)) * &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; : 0;
# get paths # get paths
@ -189,7 +190,7 @@ sub extrude_loop {
my $gcode = join '', map $self->_extrude_path($_, $description, $speed), @paths; my $gcode = join '', map $self->_extrude_path($_, $description, $speed), @paths;
# reset acceleration # reset acceleration
$gcode .= $self->_writer->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 $self->_wipe_path($paths[-1]->polyline->clone) if $self->enable_wipe; # TODO: don't limit wipe to last path
@ -206,7 +207,7 @@ sub extrude_loop {
# we make sure we don't exceed the segment length because we don't know # 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 # 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 $first_segment = Slic3r::Line->new(@$last_path_polyline[0,1]);
my $distance = min(scale($self->config->get_at('nozzle_diameter', $self->_writer->extruder->id)), $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); my $point = $first_segment->point_at($distance);
$point->rotate($angle, $last_path_polyline->first_point); $point->rotate($angle, $last_path_polyline->first_point);
@ -223,7 +224,7 @@ sub extrude_path {
my $gcode = $self->_extrude_path($path, $description, $speed); my $gcode = $self->_extrude_path($path, $description, $speed);
# reset acceleration # reset acceleration
$gcode .= $self->_writer->set_acceleration($self->config->default_acceleration); $gcode .= $self->writer->set_acceleration($self->config->default_acceleration);
return $gcode; return $gcode;
} }
@ -247,7 +248,7 @@ sub _extrude_path {
# adjust acceleration # adjust acceleration
{ {
my $acceleration; my $acceleration;
if ($self->config->first_layer_acceleration && $self->layer->id == 0) { if ($self->config->first_layer_acceleration && $self->first_layer) {
$acceleration = $self->config->first_layer_acceleration; $acceleration = $self->config->first_layer_acceleration;
} elsif ($self->config->perimeter_acceleration && $path->is_perimeter) { } elsif ($self->config->perimeter_acceleration && $path->is_perimeter) {
$acceleration = $self->config->perimeter_acceleration; $acceleration = $self->config->perimeter_acceleration;
@ -258,12 +259,12 @@ sub _extrude_path {
} else { } else {
$acceleration = $self->config->default_acceleration; $acceleration = $self->config->default_acceleration;
} }
$gcode .= $self->_writer->set_acceleration($acceleration); $gcode .= $self->writer->set_acceleration($acceleration);
} }
# calculate extrusion length per distance unit # calculate extrusion length per distance unit
my $e_per_mm = $self->_writer->extruder->e_per_mm3 * $path->mm3_per_mm; my $e_per_mm = $self->writer->extruder->e_per_mm3 * $path->mm3_per_mm;
$e_per_mm = 0 if !$self->_writer->_extrusion_axis; $e_per_mm = 0 if !$self->writer->_extrusion_axis;
# set speed # set speed
my $F; my $F;
@ -287,19 +288,19 @@ sub _extrude_path {
} }
$F *= 60; # convert mm/sec to mm/min $F *= 60; # convert mm/sec to mm/min
if ($self->layer->id == 0) { if ($self->first_layer) {
$F = $self->config->get_abs_value_over('first_layer_speed', $F/60) * 60; $F = $self->config->get_abs_value_over('first_layer_speed', $F/60) * 60;
} }
# extrude arc or line # extrude arc or line
$gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge; $gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge && $self->enable_cooling_markers;
my $path_length = unscale $path->length; my $path_length = unscale $path->length;
{ {
my $extruder_offset = $self->config->get_at('extruder_offset', $self->_writer->extruder->id); my $extruder_offset = $self->config->get_at('extruder_offset', $self->writer->extruder->id);
$gcode .= $path->gcode($self->_writer->extruder, $e_per_mm, $F, $gcode .= $path->gcode($self->writer->extruder, $e_per_mm, $F,
$self->origin->x - $extruder_offset->x, $self->origin->x - $extruder_offset->x,
$self->origin->y - $extruder_offset->y, #- $self->origin->y - $extruder_offset->y, #-
$self->_writer->_extrusion_axis, $self->writer->_extrusion_axis,
$self->config->gcode_comments ? " ; $description" : ""); $self->config->gcode_comments ? " ; $description" : "");
if ($self->enable_wipe) { if ($self->enable_wipe) {
@ -307,7 +308,7 @@ sub _extrude_path {
$self->_wipe_path->reverse; $self->_wipe_path->reverse;
} }
} }
$gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge; $gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge && $self->enable_cooling_markers;
$self->last_pos($path->last_point); $self->last_pos($path->last_point);
if ($self->config->cooling) { if ($self->config->cooling) {
@ -335,7 +336,7 @@ sub travel_to {
# upper layer (so that ooze will not be visible) # upper layer (so that ooze will not be visible)
# - the path that will be extruded after this travel move is a support material # - the path that will be extruded after this travel move is a support material
# extrusion and the travel move is contained in a single support material island # extrusion and the travel move is contained in a single support material island
if ($travel->length < scale $self->config->get_at('retract_before_travel', $self->_writer->extruder->id) if ($travel->length < scale $self->config->get_at('retract_before_travel', $self->writer->extruder->id)
|| ($self->config->only_retract_when_crossing_perimeters || ($self->config->only_retract_when_crossing_perimeters
&& $self->config->fill_density > 0 && $self->config->fill_density > 0
&& (first { $_->contains_line($travel) } @{$self->_upper_layer_islands}) && (first { $_->contains_line($travel) } @{$self->_upper_layer_islands})
@ -343,7 +344,7 @@ sub travel_to {
|| (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && (first { $_->contains_line($travel) } @{$self->layer->support_islands})) || (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && (first { $_->contains_line($travel) } @{$self->layer->support_islands}))
) { ) {
# Just perform a straight travel move without any retraction. # Just perform a straight travel move without any retraction.
$gcode .= $self->_writer->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) { } elsif ($self->config->avoid_crossing_perimeters && !$self->straight_once) {
# If avoid_crossing_perimeters is enabled and the straight_once flag is not set # If avoid_crossing_perimeters is enabled and the straight_once flag is not set
# we need to plan a multi-segment travel move inside the configuration space. # we need to plan a multi-segment travel move inside the configuration space.
@ -367,7 +368,7 @@ sub travel_to {
# If avoid_crossing_perimeters is disabled or the straight_once flag is set, # If avoid_crossing_perimeters is disabled or the straight_once flag is set,
# perform a straight move with a retraction. # perform a straight move with a retraction.
$gcode .= $self->retract; $gcode .= $self->retract;
$gcode .= $self->_writer->travel_to_xy($self->point_to_gcode($point), $comment); $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($point), $comment);
} }
# Re-allow avoid_crossing_perimeters for the next travel moves # Re-allow avoid_crossing_perimeters for the next travel moves
@ -401,7 +402,7 @@ sub _plan {
# append the actual path and return # append the actual path and return
# use G1 because we rely on paths being straight (G0 may make round paths) # use G1 because we rely on paths being straight (G0 may make round paths)
$gcode .= join '', $gcode .= join '',
map $self->_writer->travel_to_xy($self->point_to_gcode($_->b), $comment), map $self->writer->travel_to_xy($self->point_to_gcode($_->b), $comment),
@travel; @travel;
return $gcode; return $gcode;
} }
@ -409,25 +410,25 @@ sub _plan {
sub retract { sub retract {
my ($self, $toolchange) = @_; my ($self, $toolchange) = @_;
return "" if !defined $self->_writer->extruder; return "" if !defined $self->writer->extruder;
my $gcode = ""; my $gcode = "";
# wipe (if it's enabled for this extruder and we have a stored wipe path) # wipe (if it's enabled for this extruder and we have a stored wipe path)
if ($self->config->get_at('wipe', $self->_writer->extruder->id) && $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. # 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. # Too fast = ripping of existing material; too slow = short wipe path, thus more blob.
my $wipe_speed = $self->_writer->config->travel_speed * 0.8; my $wipe_speed = $self->writer->config->travel_speed * 0.8;
# get the retraction length # get the retraction length
my $length = $toolchange my $length = $toolchange
? $self->_writer->extruder->retract_length ? $self->writer->extruder->retract_length
: $self->_writer->extruder->retract_length_toolchange; : $self->writer->extruder->retract_length_toolchange;
# Calculate how long we need to travel in order to consume the required # 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 # 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? # for the time needed to consume retract_length at retract_speed?
my $wipe_dist = scale($length / $self->_writer->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 # 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). # (they might be different, for example, in case of loop clipping).
@ -445,25 +446,25 @@ sub retract {
# Reduce retraction length a bit to avoid effective retraction speed to be greater than the configured one # 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) # due to rounding (TODO: test and/or better math for this)
my $dE = $length * ($segment_length / $wipe_dist) * 0.95; my $dE = $length * ($segment_length / $wipe_dist) * 0.95;
$gcode .= $self->_writer->set_speed($wipe_speed*60); $gcode .= $self->writer->set_speed($wipe_speed*60);
$gcode .= $self->_writer->extrude_to_xy( $gcode .= $self->writer->extrude_to_xy(
$self->point_to_gcode($line->b), $self->point_to_gcode($line->b),
-$dE, -$dE,
'retract;_WIPE', 'retract' . ($self->enable_cooling_markers ? ';_WIPE' : ''),
); );
$retracted += $dE; $retracted += $dE;
} }
$self->_writer->extruder->set_retracted($self->_writer->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 parent class will decide whether we need to perform an actual retraction
# (the extruder might be already retracted fully or partially). We call these # (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 # methods even if we performed wipe, since this will ensure the entire retraction
# length is honored in case wipe path was too short.p # length is honored in case wipe path was too short.p
$gcode .= $toolchange ? $self->_writer->retract_for_toolchange : $self->_writer->retract; $gcode .= $toolchange ? $self->writer->retract_for_toolchange : $self->writer->retract;
$gcode .= $self->_writer->reset_e; $gcode .= $self->writer->reset_e;
$gcode .= $self->_writer->lift; $gcode .= $self->writer->lift;
return $gcode; return $gcode;
} }
@ -472,8 +473,8 @@ sub unretract {
my ($self) = @_; my ($self) = @_;
my $gcode = ""; my $gcode = "";
$gcode .= $self->_writer->unlift; $gcode .= $self->writer->unlift;
$gcode .= $self->_writer->unretract('compensate retraction'); $gcode .= $self->writer->unretract('compensate retraction');
return $gcode; return $gcode;
} }
@ -481,7 +482,7 @@ sub unretract {
sub point_to_gcode { sub point_to_gcode {
my ($self, $point) = @_; my ($self, $point) = @_;
my $extruder_offset = $self->config->get_at('extruder_offset', $self->_writer->extruder->id); my $extruder_offset = $self->config->get_at('extruder_offset', $self->writer->extruder->id);
return Slic3r::Pointf->new( return Slic3r::Pointf->new(
($point->x * &Slic3r::SCALING_FACTOR) + $self->origin->x - $extruder_offset->x, ($point->x * &Slic3r::SCALING_FACTOR) + $self->origin->x - $extruder_offset->x,
($point->y * &Slic3r::SCALING_FACTOR) + $self->origin->y - $extruder_offset->y, #** ($point->y * &Slic3r::SCALING_FACTOR) + $self->origin->y - $extruder_offset->y, #**
@ -491,20 +492,20 @@ sub point_to_gcode {
sub set_extruder { sub set_extruder {
my ($self, $extruder_id) = @_; my ($self, $extruder_id) = @_;
return "" if !$self->_writer->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 we are running a single-extruder setup, just set the extruder and return nothing
if (!$self->_writer->multiple_extruders) { if (!$self->writer->multiple_extruders) {
return $self->_writer->toolchange($extruder_id); return $self->writer->toolchange($extruder_id);
} }
# prepend retraction on the current extruder # prepend retraction on the current extruder
my $gcode = $self->retract(1); my $gcode = $self->retract(1);
# append custom toolchange G-code # append custom toolchange G-code
if (defined $self->_writer->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, { $gcode .= sprintf "%s\n", $self->placeholder_parser->process($self->config->toolchange_gcode, {
previous_extruder => $self->_writer->extruder->id, previous_extruder => $self->writer->extruder->id,
next_extruder => $extruder_id, next_extruder => $extruder_id,
}); });
} }
@ -512,10 +513,10 @@ sub set_extruder {
# if ooze prevention is enabled, park current extruder in the nearest # if ooze prevention is enabled, park current extruder in the nearest
# standby point and set it to the standby temperature # standby point and set it to the standby temperature
$gcode .= $self->ooze_prevention->pre_toolchange($self) $gcode .= $self->ooze_prevention->pre_toolchange($self)
if $self->ooze_prevention && defined $self->_writer->extruder; if $self->ooze_prevention && defined $self->writer->extruder;
# append the toolchange command # append the toolchange command
$gcode .= $self->_writer->toolchange($extruder_id); $gcode .= $self->writer->toolchange($extruder_id);
# set the new extruder to the operating temperature # set the new extruder to the operating temperature
$gcode .= $self->ooze_prevention->post_toolchange($self) $gcode .= $self->ooze_prevention->post_toolchange($self)

View File

@ -63,14 +63,14 @@ sub flush {
} }
} }
$fan_speed = 0 if $self->layer_id < $self->config->disable_fan_first_layers; $fan_speed = 0 if $self->layer_id < $self->config->disable_fan_first_layers;
$gcode = $self->gcodegen->set_fan($fan_speed) . $gcode; $gcode = $self->gcodegen->writer->set_fan($fan_speed) . $gcode;
# bridge fan speed # bridge fan speed
if (!$self->config->cooling || $self->config->bridge_fan_speed == 0 || $self->layer_id < $self->config->disable_fan_first_layers) { if (!$self->config->cooling || $self->config->bridge_fan_speed == 0 || $self->layer_id < $self->config->disable_fan_first_layers) {
$gcode =~ s/^;_BRIDGE_FAN_(?:START|END)\n//gm; $gcode =~ s/^;_BRIDGE_FAN_(?:START|END)\n//gm;
} else { } else {
$gcode =~ s/^;_BRIDGE_FAN_START\n/ $self->gcodegen->set_fan($self->config->bridge_fan_speed, 1) /gmex; $gcode =~ s/^;_BRIDGE_FAN_START\n/ $self->gcodegen->writer->set_fan($self->config->bridge_fan_speed, 1) /gmex;
$gcode =~ s/^;_BRIDGE_FAN_END\n/ $self->gcodegen->set_fan($fan_speed, 1) /gmex; $gcode =~ s/^;_BRIDGE_FAN_END\n/ $self->gcodegen->writer->set_fan($fan_speed, 1) /gmex;
} }
$gcode =~ s/;_WIPE//g; $gcode =~ s/;_WIPE//g;

View File

@ -5,7 +5,7 @@ use List::Util qw(first);
use Slic3r::Geometry qw(X Y unscale); use Slic3r::Geometry qw(X Y unscale);
has 'print' => (is => 'ro', required => 1, handles => [qw(config)]); has 'print' => (is => 'ro', required => 1, handles => [qw(config)]);
has 'gcodegen' => (is => 'ro', required => 1, handles => [qw(extruders)]); has 'gcodegen' => (is => 'ro', required => 1);
has 'origin' => (is => 'ro', default => sub { Slic3r::Pointf->new(0,0) }); has 'origin' => (is => 'ro', default => sub { Slic3r::Pointf->new(0,0) });
has 'spiralvase' => (is => 'lazy'); has 'spiralvase' => (is => 'lazy');
@ -63,12 +63,12 @@ sub process_layer {
$self->gcodegen->enable_loop_clipping(!defined $self->spiralvase || !$self->spiralvase->enable); $self->gcodegen->enable_loop_clipping(!defined $self->spiralvase || !$self->spiralvase->enable);
if (!$self->second_layer_things_done && $layer->id == 1) { if (!$self->second_layer_things_done && $layer->id == 1) {
for my $extruder (@{$self->extruders}) { for my $extruder (@{$self->gcodegen->writer->extruders}) {
my $temperature = $self->config->get_at('temperature', $extruder->id); my $temperature = $self->config->get_at('temperature', $extruder->id);
$gcode .= $self->gcodegen->set_temperature($temperature, 0, $extruder->id) $gcode .= $self->gcodegen->writer->set_temperature($temperature, 0, $extruder->id)
if $temperature && $temperature != $self->config->get_at('first_layer_temperature', $extruder->id); if $temperature && $temperature != $self->config->get_at('first_layer_temperature', $extruder->id);
} }
$gcode .= $self->gcodegen->set_bed_temperature($self->print->config->bed_temperature) $gcode .= $self->gcodegen->writer->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; if $self->print->config->bed_temperature && $self->print->config->bed_temperature != $self->print->config->first_layer_bed_temperature;
$self->second_layer_things_done(1); $self->second_layer_things_done(1);
} }
@ -83,7 +83,7 @@ sub process_layer {
if (((values %{$self->skirt_done}) < $self->print->config->skirt_height || $self->print->config->skirt_height == -1) if (((values %{$self->skirt_done}) < $self->print->config->skirt_height || $self->print->config->skirt_height == -1)
&& !$self->skirt_done->{$layer->print_z}) { && !$self->skirt_done->{$layer->print_z}) {
$self->gcodegen->set_origin($self->origin); $self->gcodegen->set_origin($self->origin);
my @extruder_ids = map { $_->id } @{$self->extruders}; my @extruder_ids = map { $_->id } @{$self->gcodegen->writer->extruders};
$gcode .= $self->gcodegen->set_extruder($extruder_ids[0]); $gcode .= $self->gcodegen->set_extruder($extruder_ids[0]);
# skip skirt if we have a large brim # skip skirt if we have a large brim
if ($layer->id < $self->print->config->skirt_height || $self->print->config->skirt_height == -1) { if ($layer->id < $self->print->config->skirt_height || $self->print->config->skirt_height == -1) {
@ -136,8 +136,8 @@ sub process_layer {
# tweak region ordering to save toolchanges # tweak region ordering to save toolchanges
my @region_ids = 0 .. ($self->print->region_count-1); my @region_ids = 0 .. ($self->print->region_count-1);
if ($self->gcodegen->multiple_extruders) { if ($self->gcodegen->writer->multiple_extruders) {
my $last_extruder_id = $self->gcodegen->extruder->id; my $last_extruder_id = $self->gcodegen->writer->extruder->id;
my $best_region_id = first { $self->print->regions->[$_]->config->perimeter_extruder-1 == $last_extruder_id } @region_ids; my $best_region_id = first { $self->print->regions->[$_]->config->perimeter_extruder-1 == $last_extruder_id } @region_ids;
@region_ids = ($best_region_id, grep $_ != $best_region_id, @region_ids) if $best_region_id; @region_ids = ($best_region_id, grep $_ != $best_region_id, @region_ids) if $best_region_id;
} }
@ -176,7 +176,7 @@ sub process_layer {
# give priority to infill if we were already using its extruder and it wouldn't # give priority to infill if we were already using its extruder and it wouldn't
# be good for perimeters # be good for perimeters
if ($self->print->config->infill_first 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)) { || ($self->gcodegen->writer->multiple_extruders && $region->config->infill_extruder-1 == $self->gcodegen->writer->extruder->id && $region->config->infill_extruder != $region->config->perimeter_extruder)) {
$gcode .= $self->_extrude_infill($infill_by_island[$i], $region); $gcode .= $self->_extrude_infill($infill_by_island[$i], $region);
$gcode .= $self->_extrude_perimeters($perimeters_by_island[$i], $region); $gcode .= $self->_extrude_perimeters($perimeters_by_island[$i], $region);
} else { } else {

View File

@ -21,10 +21,10 @@ sub pre_toolchange {
if ($gcodegen->config->standby_temperature_delta != 0) { if ($gcodegen->config->standby_temperature_delta != 0) {
my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0 my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0
? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->extruder->id) ? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->writer->extruder->id)
: $gcodegen->config->get_at('temperature', $gcodegen->extruder->id); : $gcodegen->config->get_at('temperature', $gcodegen->writer->extruder->id);
# we assume that heating is always slower than cooling, so no need to block # we assume that heating is always slower than cooling, so no need to block
$gcode .= $gcodegen->set_temperature($temp + $gcodegen->config->standby_temperature_delta, 0); $gcode .= $gcodegen->writer->set_temperature($temp + $gcodegen->config->standby_temperature_delta, 0);
} }
return $gcode; return $gcode;
@ -37,9 +37,9 @@ sub post_toolchange {
if ($gcodegen->config->standby_temperature_delta != 0) { if ($gcodegen->config->standby_temperature_delta != 0) {
my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0 my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0
? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->extruder->id) ? $gcodegen->config->get_at('first_layer_temperature', $gcodegen->writer->extruder->id)
: $gcodegen->config->get_at('temperature', $gcodegen->extruder->id); : $gcodegen->config->get_at('temperature', $gcodegen->writer->extruder->id);
$gcode .= $gcodegen->set_temperature($temp, 1); $gcode .= $gcodegen->writer->set_temperature($temp, 1);
} }
return $gcode; return $gcode;

View File

@ -41,6 +41,26 @@ sub set_extruders {
$self->multiple_extruders(max(@$extruder_ids) > 0); $self->multiple_extruders(max(@$extruder_ids) > 0);
} }
sub preamble {
my ($self) = @_;
my $gcode = "";
if ($self->config->gcode_flavor ne 'makerware') {
$gcode .= "G21 ; set units to millimeters\n";
$gcode .= "G90 ; use absolute coordinates\n";
}
if ($self->config->gcode_flavor =~ /^(?:reprap|teacup)$/) {
if ($self->config->use_relative_e_distances) {
$gcode .= "M83 ; use relative distances for extrusion\n";
} else {
$gcode .= "M82 ; use absolute distances for extrusion\n";
}
$gcode .= $self->reset_e(1);
}
return $gcode;
}
sub set_temperature { sub set_temperature {
my ($self, $temperature, $wait, $tool) = @_; my ($self, $temperature, $wait, $tool) = @_;

View File

@ -772,16 +772,16 @@ sub write_gcode {
my $gcodegen = Slic3r::GCode->new( my $gcodegen = Slic3r::GCode->new(
placeholder_parser => $self->placeholder_parser, placeholder_parser => $self->placeholder_parser,
layer_count => $layer_count, layer_count => $layer_count,
enable_cooling_markers => 1,
); );
$gcodegen->apply_print_config($self->config); $gcodegen->apply_print_config($self->config);
$gcodegen->set_extruders($self->extruders); $gcodegen->set_extruders($self->extruders);
print $fh "G21 ; set units to millimeters\n" if $self->config->gcode_flavor ne 'makerware'; print $fh $gcodegen->writer->set_fan(0, 1) if $self->config->cooling && $self->config->disable_fan_first_layers;
print $fh $gcodegen->set_fan(0, 1) if $self->config->cooling && $self->config->disable_fan_first_layers;
# set bed temperature # set bed temperature
if ((my $temp = $self->config->first_layer_bed_temperature) && $self->config->start_gcode !~ /M(?:190|140)/i) { if ((my $temp = $self->config->first_layer_bed_temperature) && $self->config->start_gcode !~ /M(?:190|140)/i) {
printf $fh $gcodegen->set_bed_temperature($temp, 1); printf $fh $gcodegen->writer->set_bed_temperature($temp, 1);
} }
# set extruder(s) temperature before and after start G-code # set extruder(s) temperature before and after start G-code
@ -792,7 +792,7 @@ sub write_gcode {
for my $t (@{$self->extruders}) { for my $t (@{$self->extruders}) {
my $temp = $self->config->get_at('first_layer_temperature', $t); my $temp = $self->config->get_at('first_layer_temperature', $t);
$temp += $self->config->standby_temperature_delta if $self->config->ooze_prevention; $temp += $self->config->standby_temperature_delta if $self->config->ooze_prevention;
printf $fh $gcodegen->set_temperature($temp, $wait, $t) if $temp > 0; printf $fh $gcodegen->writer->set_temperature($temp, $wait, $t) if $temp > 0;
} }
}; };
$print_first_layer_temperature->(0); $print_first_layer_temperature->(0);
@ -800,15 +800,7 @@ sub write_gcode {
$print_first_layer_temperature->(1); $print_first_layer_temperature->(1);
# set other general things # set other general things
print $fh "G90 ; use absolute coordinates\n" if $self->config->gcode_flavor ne 'makerware'; print $fh $gcodegen->writer->preamble;
if ($self->config->gcode_flavor =~ /^(?:reprap|teacup)$/) {
printf $fh $gcodegen->reset_e;
if ($self->config->use_relative_e_distances) {
print $fh "M83 ; use relative distances for extrusion\n";
} else {
print $fh "M82 ; use absolute distances for extrusion\n";
}
}
# initialize a motion planner for object-to-object travel moves # initialize a motion planner for object-to-object travel moves
if ($self->config->avoid_crossing_perimeters) { if ($self->config->avoid_crossing_perimeters) {
@ -893,7 +885,7 @@ sub write_gcode {
# another one, set first layer temperatures. this happens before the Z move # another one, set first layer temperatures. this happens before the Z move
# is triggered, so machine has more time to reach such temperatures # is triggered, so machine has more time to reach such temperatures
if ($layer->id == 0 && $finished_objects > 0) { if ($layer->id == 0 && $finished_objects > 0) {
printf $fh $gcodegen->set_bed_temperature($self->config->first_layer_bed_temperature), printf $fh $gcodegen->writer->set_bed_temperature($self->config->first_layer_bed_temperature),
if $self->config->first_layer_bed_temperature; if $self->config->first_layer_bed_temperature;
$print_first_layer_temperature->(); $print_first_layer_temperature->();
} }
@ -944,12 +936,12 @@ sub write_gcode {
# write end commands to file # write end commands to file
print $fh $gcodegen->retract; print $fh $gcodegen->retract;
print $fh $gcodegen->set_fan(0); print $fh $gcodegen->writer->set_fan(0);
printf $fh "%s\n", $gcodegen->placeholder_parser->process($self->config->end_gcode); printf $fh "%s\n", $gcodegen->placeholder_parser->process($self->config->end_gcode);
$self->total_used_filament(0); $self->total_used_filament(0);
$self->total_extruded_volume(0); $self->total_extruded_volume(0);
foreach my $extruder (@{$gcodegen->extruders}) { foreach my $extruder (@{$gcodegen->writer->extruders}) {
my $used_filament = $extruder->used_filament; my $used_filament = $extruder->used_filament;
my $extruded_volume = $extruder->extruded_volume; my $extruded_volume = $extruder->extruded_volume;