2012-06-28 12:44:54 +00:00
|
|
|
package Slic3r::GCode;
|
|
|
|
use Moo;
|
|
|
|
|
2012-12-09 17:33:25 +00:00
|
|
|
use List::Util qw(max first);
|
2012-06-28 12:44:54 +00:00
|
|
|
use Slic3r::ExtrusionPath ':roles';
|
2012-11-29 18:16:07 +00:00
|
|
|
use Slic3r::Geometry qw(scale unscale scaled_epsilon points_coincide PI X Y B);
|
2012-08-23 13:42:58 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(union_ex);
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2012-09-23 00:40:25 +00:00
|
|
|
has 'multiple_extruders' => (is => 'ro', default => sub {0} );
|
2013-01-17 13:56:31 +00:00
|
|
|
has 'layer_count' => (is => 'ro', required => 1 );
|
2012-06-28 12:44:54 +00:00
|
|
|
has 'layer' => (is => 'rw');
|
2012-12-20 16:01:01 +00:00
|
|
|
has 'move_z_callback' => (is => 'rw');
|
2012-06-28 12:44:54 +00:00
|
|
|
has 'shift_x' => (is => 'rw', default => sub {0} );
|
|
|
|
has 'shift_y' => (is => 'rw', default => sub {0} );
|
2012-10-28 18:24:24 +00:00
|
|
|
has 'z' => (is => 'rw');
|
2012-06-28 12:44:54 +00:00
|
|
|
has 'speed' => (is => 'rw');
|
|
|
|
|
2012-08-23 19:10:04 +00:00
|
|
|
has 'external_mp' => (is => 'rw');
|
|
|
|
has 'layer_mp' => (is => 'rw');
|
|
|
|
has 'new_object' => (is => 'rw', default => sub {0});
|
|
|
|
has 'straight_once' => (is => 'rw', default => sub {1});
|
2012-09-23 00:40:25 +00:00
|
|
|
has 'extruder' => (is => 'rw');
|
2012-06-28 12:44:54 +00:00
|
|
|
has 'extrusion_distance' => (is => 'rw', default => sub {0} );
|
|
|
|
has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
|
|
|
|
has 'total_extrusion_length' => (is => 'rw', default => sub {0} );
|
|
|
|
has 'lifted' => (is => 'rw', default => sub {0} );
|
|
|
|
has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
|
|
|
|
has 'last_speed' => (is => 'rw', default => sub {""});
|
2012-11-18 14:28:13 +00:00
|
|
|
has 'last_f' => (is => 'rw', default => sub {""});
|
2012-06-28 12:44:54 +00:00
|
|
|
has 'last_fan_speed' => (is => 'rw', default => sub {0});
|
|
|
|
has 'dec' => (is => 'ro', default => sub { 3 } );
|
|
|
|
|
2012-11-18 14:28:13 +00:00
|
|
|
# used for vibration limit:
|
|
|
|
has 'last_dir' => (is => 'ro', default => sub { [0,0] });
|
2012-12-09 17:33:25 +00:00
|
|
|
has 'dir_time' => (is => 'ro', default => sub { [0,0] });
|
2012-11-18 14:28:13 +00:00
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
# calculate speeds (mm/min)
|
|
|
|
has 'speeds' => (
|
|
|
|
is => 'ro',
|
2012-10-30 14:53:01 +00:00
|
|
|
default => sub {+{
|
|
|
|
map { $_ => 60 * $Slic3r::Config->get_value("${_}_speed") }
|
|
|
|
qw(travel perimeter small_perimeter external_perimeter infill
|
2013-03-07 15:24:25 +00:00
|
|
|
solid_infill top_solid_infill support_material bridge gap_fill retract),
|
2012-06-28 12:44:54 +00:00
|
|
|
}},
|
|
|
|
);
|
|
|
|
|
2012-10-30 14:53:01 +00:00
|
|
|
# assign speeds to roles
|
2012-06-28 12:44:54 +00:00
|
|
|
my %role_speeds = (
|
|
|
|
&EXTR_ROLE_PERIMETER => 'perimeter',
|
|
|
|
&EXTR_ROLE_EXTERNAL_PERIMETER => 'external_perimeter',
|
|
|
|
&EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER => 'perimeter',
|
|
|
|
&EXTR_ROLE_FILL => 'infill',
|
|
|
|
&EXTR_ROLE_SOLIDFILL => 'solid_infill',
|
|
|
|
&EXTR_ROLE_TOPSOLIDFILL => 'top_solid_infill',
|
|
|
|
&EXTR_ROLE_BRIDGE => 'bridge',
|
|
|
|
&EXTR_ROLE_SKIRT => 'perimeter',
|
2012-11-23 16:20:26 +00:00
|
|
|
&EXTR_ROLE_SUPPORTMATERIAL => 'support_material',
|
2012-11-23 16:41:15 +00:00
|
|
|
&EXTR_ROLE_GAPFILL => 'gap_fill',
|
2012-06-28 12:44:54 +00:00
|
|
|
);
|
|
|
|
|
2012-08-23 13:42:58 +00:00
|
|
|
sub set_shift {
|
|
|
|
my $self = shift;
|
|
|
|
my @shift = @_;
|
|
|
|
|
2013-01-27 12:06:45 +00:00
|
|
|
# if shift increases (goes towards right), last_pos decreases because it goes towards left
|
2012-12-09 17:33:25 +00:00
|
|
|
$self->last_pos->translate(
|
2013-01-27 11:48:16 +00:00
|
|
|
scale ($self->shift_x - $shift[X]),
|
|
|
|
scale ($self->shift_y - $shift[Y]),
|
2012-12-09 17:33:25 +00:00
|
|
|
);
|
2012-08-23 13:42:58 +00:00
|
|
|
|
|
|
|
$self->shift_x($shift[X]);
|
|
|
|
$self->shift_y($shift[Y]);
|
|
|
|
}
|
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
sub change_layer {
|
|
|
|
my $self = shift;
|
|
|
|
my ($layer) = @_;
|
|
|
|
|
|
|
|
$self->layer($layer);
|
2012-08-23 13:42:58 +00:00
|
|
|
if ($Slic3r::Config->avoid_crossing_perimeters) {
|
2012-08-23 19:10:04 +00:00
|
|
|
$self->layer_mp(Slic3r::GCode::MotionPlanner->new(
|
2012-10-24 09:49:31 +00:00
|
|
|
islands => union_ex([ map @$_, @{$layer->slices} ], undef, 1),
|
2012-08-23 13:42:58 +00:00
|
|
|
));
|
|
|
|
}
|
2013-01-17 13:56:31 +00:00
|
|
|
|
|
|
|
my $gcode = "";
|
|
|
|
if ($Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/) {
|
|
|
|
$gcode .= sprintf "M73 P%s%s\n",
|
2013-02-07 04:32:37 +00:00
|
|
|
int(99 * ($layer->id / ($self->layer_count - 1))),
|
2013-01-17 13:56:31 +00:00
|
|
|
($Slic3r::Config->gcode_comments ? ' ; update progress' : '');
|
|
|
|
}
|
|
|
|
return $gcode;
|
2013-01-12 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 15:59:20 +00:00
|
|
|
# this method accepts Z in scaled coordinates
|
|
|
|
sub move_z {
|
|
|
|
my $self = shift;
|
|
|
|
my ($z, $comment) = @_;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2012-10-30 14:53:01 +00:00
|
|
|
$z *= &Slic3r::SCALING_FACTOR;
|
2012-11-06 19:04:44 +00:00
|
|
|
$z += $Slic3r::Config->z_offset;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2012-10-28 15:59:20 +00:00
|
|
|
my $gcode = "";
|
2012-10-30 09:45:55 +00:00
|
|
|
my $current_z = $self->z;
|
|
|
|
if (!defined $current_z || $current_z != ($z + $self->lifted)) {
|
|
|
|
$gcode .= $self->retract(move_z => $z);
|
2012-11-18 14:28:13 +00:00
|
|
|
$self->speed('travel');
|
2012-10-30 13:11:32 +00:00
|
|
|
$gcode .= $self->G0(undef, $z, 0, $comment || ('move to next layer (' . $self->layer->id . ')'))
|
2012-10-30 09:45:55 +00:00
|
|
|
unless ($current_z // -1) != ($self->z // -1);
|
2012-12-20 16:01:01 +00:00
|
|
|
$gcode .= $self->move_z_callback->() if defined $self->move_z_callback;
|
2012-10-30 09:45:55 +00:00
|
|
|
}
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub extrude {
|
|
|
|
my $self = shift;
|
|
|
|
|
2012-07-23 15:51:04 +00:00
|
|
|
($_[0]->isa('Slic3r::ExtrusionLoop') || $_[0]->isa('Slic3r::ExtrusionLoop::Packed'))
|
2012-07-20 12:39:07 +00:00
|
|
|
? $self->extrude_loop(@_)
|
|
|
|
: $self->extrude_path(@_);
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub extrude_loop {
|
|
|
|
my $self = shift;
|
|
|
|
my ($loop, $description) = @_;
|
|
|
|
|
|
|
|
# extrude all loops ccw
|
2012-07-23 15:56:20 +00:00
|
|
|
$loop = $loop->unpack if $loop->isa('Slic3r::ExtrusionLoop::Packed');
|
2012-12-05 10:31:35 +00:00
|
|
|
my $was_clockwise = $loop->polygon->make_counter_clockwise;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# find the point of the loop that is closest to the current extruder position
|
|
|
|
# or randomize if requested
|
|
|
|
my $last_pos = $self->last_pos;
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->randomize_start && $loop->role == EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER) {
|
|
|
|
$last_pos = Slic3r::Point->new(scale $Slic3r::Config->print_center->[X], scale $Slic3r::Config->bed_size->[Y]);
|
|
|
|
$last_pos->rotate(rand(2*PI), $Slic3r::Config->print_center);
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
2012-07-02 19:26:56 +00:00
|
|
|
my $start_index = $loop->nearest_point_index_to($last_pos);
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# split the loop at the starting point and make a path
|
2012-07-02 19:26:56 +00:00
|
|
|
my $extrusion_path = $loop->split_at_index($start_index);
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# clip the path to avoid the extruder to get exactly on the first point of the loop;
|
|
|
|
# if polyline was shorter than the clipping distance we'd get a null polyline, so
|
|
|
|
# we discard it in that case
|
2012-10-30 13:07:01 +00:00
|
|
|
$extrusion_path->clip_end(scale $extrusion_path->flow_spacing * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_SPACING);
|
2012-06-28 12:44:54 +00:00
|
|
|
return '' if !@{$extrusion_path->polyline};
|
|
|
|
|
|
|
|
# extrude along the path
|
2012-12-05 10:31:35 +00:00
|
|
|
my $gcode = $self->extrude_path($extrusion_path, $description);
|
|
|
|
|
|
|
|
# make a little move inwards before leaving loop
|
|
|
|
if ($loop->role == EXTR_ROLE_EXTERNAL_PERIMETER) {
|
|
|
|
# detect angle between last and first segment
|
|
|
|
# the side depends on the original winding order of the polygon (left for contours, right for holes)
|
|
|
|
my @points = $was_clockwise ? (-2, 1) : (1, -2);
|
|
|
|
my $angle = Slic3r::Geometry::angle3points(@{$extrusion_path->polyline}[0, @points]) / 3;
|
|
|
|
$angle *= -1 if $was_clockwise;
|
|
|
|
|
|
|
|
# create the destination point along the first segment and rotate it
|
|
|
|
my $point = Slic3r::Geometry::point_along_segment(@{$extrusion_path->polyline}[0,1], scale $extrusion_path->flow_spacing);
|
|
|
|
bless $point, 'Slic3r::Point';
|
|
|
|
$point->rotate($angle, $extrusion_path->polyline->[0]);
|
|
|
|
|
|
|
|
# generate the travel move
|
2013-03-08 12:50:50 +00:00
|
|
|
$gcode .= $self->travel_to($point, $loop->role, "move inwards before travel");
|
2012-12-05 10:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $gcode;
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub extrude_path {
|
|
|
|
my $self = shift;
|
|
|
|
my ($path, $description, $recursive) = @_;
|
|
|
|
|
2012-07-20 12:39:07 +00:00
|
|
|
$path = $path->unpack if $path->isa('Slic3r::ExtrusionPath::Packed');
|
2012-11-18 16:38:08 +00:00
|
|
|
$path->simplify(&Slic3r::SCALED_RESOLUTION);
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# detect arcs
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->gcode_arcs && !$recursive) {
|
2012-06-28 12:44:54 +00:00
|
|
|
my $gcode = "";
|
|
|
|
foreach my $arc_path ($path->detect_arcs) {
|
|
|
|
$gcode .= $self->extrude_path($arc_path, $description, 1);
|
|
|
|
}
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
# go to first point of extrusion path
|
2013-03-05 16:30:27 +00:00
|
|
|
my $gcode = "";
|
|
|
|
$gcode .= $self->travel_to($path->points->[0], $path->role, "move to first $description point");
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# compensate retraction
|
2012-12-21 14:14:44 +00:00
|
|
|
$gcode .= $self->unretract;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2012-07-03 16:05:31 +00:00
|
|
|
my $area; # mm^3 of extrudate per mm of tool movement
|
2012-06-28 12:44:54 +00:00
|
|
|
if ($path->role == EXTR_ROLE_BRIDGE) {
|
2012-10-30 13:07:01 +00:00
|
|
|
my $s = $path->flow_spacing;
|
2012-06-28 12:44:54 +00:00
|
|
|
$area = ($s**2) * PI/4;
|
|
|
|
} else {
|
2012-10-30 13:07:01 +00:00
|
|
|
my $s = $path->flow_spacing;
|
2012-10-28 13:22:51 +00:00
|
|
|
my $h = $path->height // $self->layer->height;
|
2012-07-03 16:05:31 +00:00
|
|
|
$area = $self->extruder->mm3_per_mm($s, $h);
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 16:05:31 +00:00
|
|
|
# calculate extrusion length per distance unit
|
2012-07-02 15:09:09 +00:00
|
|
|
my $e = $self->extruder->e_per_mm3 * $area;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2012-12-05 09:47:41 +00:00
|
|
|
# set speed
|
2012-06-28 12:44:54 +00:00
|
|
|
$self->speed( $role_speeds{$path->role} || die "Unknown role: " . $path->role );
|
2012-12-05 09:47:41 +00:00
|
|
|
if ($path->role == EXTR_ROLE_PERIMETER || $path->role == EXTR_ROLE_EXTERNAL_PERIMETER || $path->role == EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER) {
|
|
|
|
if (abs($path->length) <= &Slic3r::SMALL_PERIMETER_LENGTH) {
|
|
|
|
$self->speed('small_perimeter');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# extrude arc or line
|
2012-06-28 12:44:54 +00:00
|
|
|
my $path_length = 0;
|
|
|
|
if ($path->isa('Slic3r::ExtrusionPath::Arc')) {
|
2012-11-17 11:08:19 +00:00
|
|
|
$path_length = unscale $path->length;
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $self->G2_G3($path->points->[-1], $path->orientation,
|
2012-07-03 16:05:31 +00:00
|
|
|
$path->center, $e * unscale $path_length, $description);
|
2012-06-28 12:44:54 +00:00
|
|
|
} else {
|
|
|
|
foreach my $line ($path->lines) {
|
2012-11-17 11:08:19 +00:00
|
|
|
my $line_length = unscale $line->length;
|
2012-06-28 12:44:54 +00:00
|
|
|
$path_length += $line_length;
|
2012-11-17 17:07:13 +00:00
|
|
|
$gcode .= $self->G1($line->[B], undef, $e * $line_length, $description);
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->cooling) {
|
2012-11-17 11:08:19 +00:00
|
|
|
my $path_time = $path_length / $self->speeds->{$self->last_speed} * 60;
|
2012-06-28 12:44:54 +00:00
|
|
|
if ($self->layer->id == 0) {
|
2012-07-27 19:13:03 +00:00
|
|
|
$path_time = $Slic3r::Config->first_layer_speed =~ /^(\d+(?:\.\d+)?)%$/
|
2012-06-28 12:44:54 +00:00
|
|
|
? $path_time / ($1/100)
|
2012-11-17 11:08:19 +00:00
|
|
|
: $path_length / $Slic3r::Config->first_layer_speed * 60;
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
$self->elapsed_time($self->elapsed_time + $path_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
2012-08-23 13:42:58 +00:00
|
|
|
sub travel_to {
|
|
|
|
my $self = shift;
|
2013-03-05 16:30:27 +00:00
|
|
|
my ($point, $role, $comment) = @_;
|
2012-08-23 13:42:58 +00:00
|
|
|
|
2013-01-26 22:55:47 +00:00
|
|
|
$self->speed('travel');
|
2012-08-23 19:10:04 +00:00
|
|
|
my $gcode = "";
|
2013-03-05 16:30:27 +00:00
|
|
|
|
|
|
|
my $travel = Slic3r::Line->new($self->last_pos->clone, $point->clone);
|
|
|
|
|
|
|
|
# move travel back to original layer coordinates for the island check.
|
|
|
|
# note that we're only considering the current object's islands, while we should
|
|
|
|
# build a more complete configuration space
|
|
|
|
$travel->translate(-$self->shift_x, -$self->shift_y);
|
|
|
|
|
|
|
|
if ($travel->length < scale $self->extruder->retract_before_travel
|
|
|
|
|| ($Slic3r::Config->only_retract_when_crossing_perimeters && first { $_->encloses_line($travel, scaled_epsilon) } @{$self->layer->slices})
|
|
|
|
|| ($role == EXTR_ROLE_SUPPORTMATERIAL && $self->layer->support_islands_enclose_line($travel))
|
|
|
|
) {
|
|
|
|
$self->straight_once(0);
|
|
|
|
$gcode .= $self->G0($point, undef, 0, $comment || "");
|
|
|
|
} elsif (!$Slic3r::Config->avoid_crossing_perimeters || $self->straight_once) {
|
|
|
|
$self->straight_once(0);
|
|
|
|
$gcode .= $self->retract(travel_to => $point);
|
|
|
|
$gcode .= $self->G0($point, undef, 0, $comment || "");
|
|
|
|
} else {
|
2012-08-23 19:10:04 +00:00
|
|
|
my $plan = sub {
|
|
|
|
my $mp = shift;
|
2013-03-05 16:30:27 +00:00
|
|
|
|
|
|
|
my $gcode = "";
|
|
|
|
my @travel = $mp->shortest_path($self->last_pos, $point)->lines;
|
|
|
|
|
|
|
|
# if the path is not contained in a single island we need to retract
|
|
|
|
my $need_retract = !$Slic3r::Config->only_retract_when_crossing_perimeters;
|
|
|
|
if (!$need_retract) {
|
|
|
|
$need_retract = 1;
|
|
|
|
foreach my $slice (@{$self->layer->slices}) {
|
|
|
|
# discard the island if at any line is not enclosed in it
|
|
|
|
next if first { !$slice->encloses_line($_, scaled_epsilon) } @travel;
|
|
|
|
# okay, this island encloses the full travel path
|
|
|
|
$need_retract = 0;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# do the retract (the travel_to argument is broken)
|
|
|
|
$gcode .= $self->retract(travel_to => $point) if $need_retract;
|
|
|
|
|
|
|
|
# append the actual path and return
|
|
|
|
$gcode .= join '', map $self->G0($_->[B], undef, 0, $comment || ""), @travel;
|
|
|
|
return $gcode;
|
2012-08-23 19:10:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($self->new_object) {
|
2013-01-27 11:48:16 +00:00
|
|
|
$self->new_object(0);
|
|
|
|
|
|
|
|
# represent $point in G-code coordinates
|
|
|
|
$point = $point->clone;
|
2012-08-23 19:10:04 +00:00
|
|
|
my @shift = ($self->shift_x, $self->shift_y);
|
|
|
|
$point->translate(map scale $_, @shift);
|
2013-01-27 11:48:16 +00:00
|
|
|
|
|
|
|
# calculate path (external_mp uses G-code coordinates so we temporary need a null shift)
|
|
|
|
$self->set_shift(0,0);
|
2012-08-23 19:10:04 +00:00
|
|
|
$gcode .= $plan->($self->external_mp);
|
|
|
|
$self->set_shift(@shift);
|
|
|
|
} else {
|
|
|
|
$gcode .= $plan->($self->layer_mp);
|
|
|
|
}
|
2012-08-23 13:42:58 +00:00
|
|
|
}
|
2012-08-23 19:10:04 +00:00
|
|
|
|
|
|
|
return $gcode;
|
2012-08-23 13:42:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
sub retract {
|
|
|
|
my $self = shift;
|
|
|
|
my %params = @_;
|
|
|
|
|
2012-08-22 18:31:03 +00:00
|
|
|
# get the retraction length and abort if none
|
|
|
|
my ($length, $restart_extra, $comment) = $params{toolchange}
|
|
|
|
? ($self->extruder->retract_length_toolchange, $self->extruder->retract_restart_extra_toolchange, "retract for tool change")
|
|
|
|
: ($self->extruder->retract_length, $self->extruder->retract_restart_extra, "retract");
|
2012-08-22 17:11:45 +00:00
|
|
|
|
2012-08-22 18:31:03 +00:00
|
|
|
# if we already retracted, reduce the required amount of retraction
|
|
|
|
$length -= $self->extruder->retracted;
|
|
|
|
return "" unless $length > 0;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# prepare moves
|
|
|
|
$self->speed('retract');
|
2012-08-22 18:31:03 +00:00
|
|
|
my $retract = [undef, undef, -$length, $comment];
|
2012-08-07 19:08:56 +00:00
|
|
|
my $lift = ($self->extruder->retract_lift == 0 || defined $params{move_z})
|
2012-06-28 12:44:54 +00:00
|
|
|
? undef
|
2012-10-30 09:45:55 +00:00
|
|
|
: [undef, $self->z + $self->extruder->retract_lift, 0, 'lift plate during travel'];
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
my $gcode = "";
|
2012-07-27 19:13:03 +00:00
|
|
|
if (($Slic3r::Config->g0 || $Slic3r::Config->gcode_flavor eq 'mach3') && $params{travel_to}) {
|
2012-06-28 12:44:54 +00:00
|
|
|
if ($lift) {
|
|
|
|
# combine lift and retract
|
|
|
|
$lift->[2] = $retract->[2];
|
|
|
|
$gcode .= $self->G0(@$lift);
|
|
|
|
} else {
|
|
|
|
# combine travel and retract
|
2012-08-22 18:31:03 +00:00
|
|
|
my $travel = [$params{travel_to}, undef, $retract->[2], "travel and $comment"];
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $self->G0(@$travel);
|
|
|
|
}
|
2012-07-27 19:13:03 +00:00
|
|
|
} elsif (($Slic3r::Config->g0 || $Slic3r::Config->gcode_flavor eq 'mach3') && defined $params{move_z}) {
|
2012-06-28 12:44:54 +00:00
|
|
|
# combine Z change and retraction
|
2012-08-22 18:31:03 +00:00
|
|
|
my $travel = [undef, $params{move_z}, $retract->[2], "change layer and $comment"];
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $self->G0(@$travel);
|
|
|
|
} else {
|
|
|
|
$gcode .= $self->G1(@$retract);
|
2012-08-07 19:08:56 +00:00
|
|
|
if (defined $params{move_z} && $self->extruder->retract_lift > 0) {
|
|
|
|
my $travel = [undef, $params{move_z} + $self->extruder->retract_lift, 0, 'move to next layer (' . $self->layer->id . ') and lift'];
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $self->G0(@$travel);
|
2012-08-07 19:08:56 +00:00
|
|
|
$self->lifted($self->extruder->retract_lift);
|
2012-06-28 12:44:54 +00:00
|
|
|
} elsif ($lift) {
|
|
|
|
$gcode .= $self->G1(@$lift);
|
|
|
|
}
|
|
|
|
}
|
2012-12-21 14:14:44 +00:00
|
|
|
$self->extruder->retracted($self->extruder->retracted + $length);
|
|
|
|
$self->extruder->restart_extra($restart_extra);
|
2012-08-07 19:08:56 +00:00
|
|
|
$self->lifted($self->extruder->retract_lift) if $lift;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# reset extrusion distance during retracts
|
|
|
|
# this makes sure we leave sufficient precision in the firmware
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= $self->reset_e if $Slic3r::Config->gcode_flavor !~ /^(?:mach3|makerbot)$/;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unretract {
|
|
|
|
my $self = shift;
|
2012-08-22 16:57:03 +00:00
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
my $gcode = "";
|
|
|
|
|
|
|
|
if ($self->lifted) {
|
2012-11-18 14:28:13 +00:00
|
|
|
$self->speed('travel');
|
2012-08-07 19:08:56 +00:00
|
|
|
$gcode .= $self->G0(undef, $self->z - $self->lifted, 0, 'restore layer Z');
|
2012-06-28 12:44:54 +00:00
|
|
|
$self->lifted(0);
|
|
|
|
}
|
|
|
|
|
2012-12-21 14:14:44 +00:00
|
|
|
my $to_unretract = $self->extruder->retracted + $self->extruder->restart_extra;
|
|
|
|
if ($to_unretract) {
|
|
|
|
$self->speed('retract');
|
|
|
|
$gcode .= $self->G0(undef, undef, $to_unretract, "compensate retraction");
|
|
|
|
$self->extruder->retracted(0);
|
|
|
|
$self->extruder->restart_extra(0);
|
|
|
|
}
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
2012-07-06 17:57:58 +00:00
|
|
|
sub reset_e {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->extrusion_distance(0);
|
2012-07-27 19:13:03 +00:00
|
|
|
return sprintf "G92 %s0%s\n", $Slic3r::Config->extrusion_axis, ($Slic3r::Config->gcode_comments ? ' ; reset extrusion distance' : '')
|
|
|
|
if $Slic3r::Config->extrusion_axis && !$Slic3r::Config->use_relative_e_distances;
|
2012-07-06 17:57:58 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
sub set_acceleration {
|
|
|
|
my $self = shift;
|
|
|
|
my ($acceleration) = @_;
|
2013-01-10 14:29:40 +00:00
|
|
|
return "" if !$acceleration;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2013-01-10 14:29:40 +00:00
|
|
|
return sprintf "M204 S%s%s\n",
|
2012-07-27 19:13:03 +00:00
|
|
|
$acceleration, ($Slic3r::Config->gcode_comments ? ' ; adjust acceleration' : '');
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub G0 {
|
|
|
|
my $self = shift;
|
2012-07-27 19:13:03 +00:00
|
|
|
return $self->G1(@_) if !($Slic3r::Config->g0 || $Slic3r::Config->gcode_flavor eq 'mach3');
|
2012-06-28 12:44:54 +00:00
|
|
|
return $self->_G0_G1("G0", @_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub G1 {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->_G0_G1("G1", @_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _G0_G1 {
|
|
|
|
my $self = shift;
|
|
|
|
my ($gcode, $point, $z, $e, $comment) = @_;
|
|
|
|
my $dec = $self->dec;
|
|
|
|
|
|
|
|
if ($point) {
|
|
|
|
$gcode .= sprintf " X%.${dec}f Y%.${dec}f",
|
2012-08-07 19:39:45 +00:00
|
|
|
($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]; #**
|
2012-12-09 17:33:25 +00:00
|
|
|
$gcode = $self->_limit_frequency($point) . $gcode;
|
2012-11-16 10:05:45 +00:00
|
|
|
$self->last_pos($point->clone);
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
2012-10-28 18:24:24 +00:00
|
|
|
if (defined $z && (!defined $self->z || $z != $self->z)) {
|
2012-06-28 12:44:54 +00:00
|
|
|
$self->z($z);
|
|
|
|
$gcode .= sprintf " Z%.${dec}f", $z;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->_Gx($gcode, $e, $comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub G2_G3 {
|
|
|
|
my $self = shift;
|
|
|
|
my ($point, $orientation, $center, $e, $comment) = @_;
|
|
|
|
my $dec = $self->dec;
|
|
|
|
|
|
|
|
my $gcode = $orientation eq 'cw' ? "G2" : "G3";
|
|
|
|
|
|
|
|
$gcode .= sprintf " X%.${dec}f Y%.${dec}f",
|
2012-08-07 19:39:45 +00:00
|
|
|
($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]; #**
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# XY distance of the center from the start position
|
|
|
|
$gcode .= sprintf " I%.${dec}f J%.${dec}f",
|
2012-07-27 19:13:03 +00:00
|
|
|
($center->[X] - $self->last_pos->[X]) * &Slic3r::SCALING_FACTOR,
|
|
|
|
($center->[Y] - $self->last_pos->[Y]) * &Slic3r::SCALING_FACTOR;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
$self->last_pos($point);
|
|
|
|
return $self->_Gx($gcode, $e, $comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _Gx {
|
|
|
|
my $self = shift;
|
|
|
|
my ($gcode, $e, $comment) = @_;
|
|
|
|
my $dec = $self->dec;
|
|
|
|
|
|
|
|
# output speed if it's different from last one used
|
|
|
|
# (goal: reduce gcode size)
|
|
|
|
my $append_bridge_off = 0;
|
2012-11-18 14:28:13 +00:00
|
|
|
my $F;
|
|
|
|
if ($self->speed ne $self->last_speed) {
|
|
|
|
if ($self->speed eq 'bridge') {
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode = ";_BRIDGE_FAN_START\n$gcode";
|
|
|
|
} elsif ($self->last_speed eq 'bridge') {
|
|
|
|
$append_bridge_off = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# apply the speed reduction for print moves on bottom layer
|
2012-11-18 14:28:13 +00:00
|
|
|
$F = $self->speed eq 'retract'
|
2012-08-07 19:08:56 +00:00
|
|
|
? ($self->extruder->retract_speed_mm_min)
|
2012-11-18 14:28:13 +00:00
|
|
|
: $self->speeds->{$self->speed} // $self->speed;
|
2012-08-22 16:57:03 +00:00
|
|
|
if ($e && $self->layer && $self->layer->id == 0 && $comment !~ /retract/) {
|
2012-11-18 14:28:13 +00:00
|
|
|
$F = $Slic3r::Config->first_layer_speed =~ /^(\d+(?:\.\d+)?)%$/
|
|
|
|
? ($F * $1/100)
|
2012-07-27 19:13:03 +00:00
|
|
|
: $Slic3r::Config->first_layer_speed * 60;
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
2012-11-18 14:28:13 +00:00
|
|
|
$self->last_speed($self->speed);
|
|
|
|
$self->last_f($F);
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
2012-11-18 14:28:13 +00:00
|
|
|
$gcode .= sprintf " F%.${dec}f", $F if defined $F;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
|
|
|
# output extrusion distance
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($e && $Slic3r::Config->extrusion_axis) {
|
|
|
|
$self->extrusion_distance(0) if $Slic3r::Config->use_relative_e_distances;
|
2012-06-28 12:44:54 +00:00
|
|
|
$self->extrusion_distance($self->extrusion_distance + $e);
|
|
|
|
$self->total_extrusion_length($self->total_extrusion_length + $e);
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= sprintf " %s%.5f", $Slic3r::Config->extrusion_axis, $self->extrusion_distance;
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= sprintf " ; %s", $comment if $comment && $Slic3r::Config->gcode_comments;
|
2012-06-28 12:44:54 +00:00
|
|
|
if ($append_bridge_off) {
|
|
|
|
$gcode .= "\n;_BRIDGE_FAN_END";
|
|
|
|
}
|
|
|
|
return "$gcode\n";
|
|
|
|
}
|
|
|
|
|
2012-09-23 00:40:25 +00:00
|
|
|
sub set_extruder {
|
2012-06-28 12:44:54 +00:00
|
|
|
my $self = shift;
|
2012-09-23 00:40:25 +00:00
|
|
|
my ($extruder) = @_;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2012-09-23 00:40:25 +00:00
|
|
|
# return nothing if this extruder was already selected
|
2012-10-25 16:49:59 +00:00
|
|
|
return "" if (defined $self->extruder) && ($self->extruder->id == $extruder->id);
|
2012-08-22 17:47:59 +00:00
|
|
|
|
|
|
|
# if we are running a single-extruder setup, just set the extruder and return nothing
|
2012-09-23 00:40:25 +00:00
|
|
|
if (!$self->multiple_extruders) {
|
|
|
|
$self->extruder($extruder);
|
2012-08-22 17:47:59 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2012-09-23 00:40:25 +00:00
|
|
|
# trigger retraction on the current extruder (if any)
|
2012-08-22 17:47:59 +00:00
|
|
|
my $gcode = "";
|
2012-09-23 00:40:25 +00:00
|
|
|
$gcode .= $self->retract(toolchange => 1) if defined $self->extruder;
|
2012-08-22 17:47:59 +00:00
|
|
|
|
2012-12-23 15:29:08 +00:00
|
|
|
# append custom toolchange G-code
|
|
|
|
if (defined $self->extruder && $Slic3r::Config->toolchange_gcode) {
|
|
|
|
$gcode .= sprintf "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->toolchange_gcode, {
|
|
|
|
previous_extruder => $self->extruder->id,
|
|
|
|
next_extruder => $extruder->id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-09-23 00:40:25 +00:00
|
|
|
# set the new extruder
|
|
|
|
$self->extruder($extruder);
|
2013-01-17 14:02:40 +00:00
|
|
|
my $toolchange_gcode = sprintf "%s%d%s\n",
|
2013-01-17 13:56:31 +00:00
|
|
|
($Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/ ? 'M108 T' : 'T'),
|
|
|
|
$extruder->id,
|
|
|
|
($Slic3r::Config->gcode_comments ? ' ; change extruder' : '');
|
2013-01-17 14:02:40 +00:00
|
|
|
|
|
|
|
if ($Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/) {
|
|
|
|
$gcode .= $self->reset_e;
|
|
|
|
$gcode .= $toolchange_gcode;
|
|
|
|
} else {
|
|
|
|
$gcode .= $toolchange_gcode;
|
|
|
|
$gcode .= $self->reset_e;
|
|
|
|
}
|
2012-08-22 17:20:34 +00:00
|
|
|
|
2012-08-22 17:47:59 +00:00
|
|
|
return $gcode;
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub set_fan {
|
|
|
|
my $self = shift;
|
|
|
|
my ($speed, $dont_save) = @_;
|
|
|
|
|
|
|
|
if ($self->last_fan_speed != $speed || $dont_save) {
|
|
|
|
$self->last_fan_speed($speed) if !$dont_save;
|
|
|
|
if ($speed == 0) {
|
2012-11-19 14:30:55 +00:00
|
|
|
my $code = $Slic3r::Config->gcode_flavor eq 'teacup'
|
|
|
|
? 'M106 S0'
|
2013-01-17 13:56:31 +00:00
|
|
|
: $Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/
|
|
|
|
? 'M127'
|
|
|
|
: 'M107';
|
2012-11-19 14:30:55 +00:00
|
|
|
return sprintf "$code%s\n", ($Slic3r::Config->gcode_comments ? ' ; disable fan' : '');
|
2012-06-28 12:44:54 +00:00
|
|
|
} else {
|
2013-01-17 13:56:31 +00:00
|
|
|
if ($Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/) {
|
|
|
|
return sprintf "M126%s\n", ($Slic3r::Config->gcode_comments ? ' ; enable fan' : '');
|
|
|
|
} else {
|
|
|
|
return sprintf "M106 %s%d%s\n", ($Slic3r::Config->gcode_flavor eq 'mach3' ? 'P' : 'S'),
|
|
|
|
(255 * $speed / 100), ($Slic3r::Config->gcode_comments ? ' ; enable fan' : '');
|
|
|
|
}
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set_temperature {
|
|
|
|
my $self = shift;
|
2012-06-28 14:22:11 +00:00
|
|
|
my ($temperature, $wait, $tool) = @_;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2013-01-17 13:56:31 +00:00
|
|
|
return "" if $wait && $Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/;
|
2012-06-28 12:44:54 +00:00
|
|
|
|
2012-08-30 21:13:28 +00:00
|
|
|
my ($code, $comment) = ($wait && $Slic3r::Config->gcode_flavor ne 'teacup')
|
2012-06-28 12:44:54 +00:00
|
|
|
? ('M109', 'wait for temperature to be reached')
|
|
|
|
: ('M104', 'set temperature');
|
2012-08-30 21:13:28 +00:00
|
|
|
my $gcode = sprintf "$code %s%d %s; $comment\n",
|
2012-07-27 19:13:03 +00:00
|
|
|
($Slic3r::Config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature,
|
2013-01-17 13:56:31 +00:00
|
|
|
(defined $tool && ($self->multiple_extruders || $Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/)) ? "T$tool " : "";
|
2012-08-30 21:13:28 +00:00
|
|
|
|
|
|
|
$gcode .= "M116 ; wait for temperature to be reached\n"
|
|
|
|
if $Slic3r::Config->gcode_flavor eq 'teacup' && $wait;
|
|
|
|
|
|
|
|
return $gcode;
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub set_bed_temperature {
|
|
|
|
my $self = shift;
|
|
|
|
my ($temperature, $wait) = @_;
|
|
|
|
|
2012-08-30 21:13:28 +00:00
|
|
|
my ($code, $comment) = ($wait && $Slic3r::Config->gcode_flavor ne 'teacup')
|
2013-01-17 13:56:31 +00:00
|
|
|
? (($Slic3r::Config->gcode_flavor =~ /^(?:makerbot|sailfish)$/ ? 'M109' : 'M190'), 'wait for bed temperature to be reached')
|
2012-06-28 12:44:54 +00:00
|
|
|
: ('M140', 'set bed temperature');
|
2012-08-30 21:13:28 +00:00
|
|
|
my $gcode = sprintf "$code %s%d ; $comment\n",
|
2012-07-27 19:13:03 +00:00
|
|
|
($Slic3r::Config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature;
|
2012-08-30 21:13:28 +00:00
|
|
|
|
|
|
|
$gcode .= "M116 ; wait for bed temperature to be reached\n"
|
|
|
|
if $Slic3r::Config->gcode_flavor eq 'teacup' && $wait;
|
|
|
|
|
|
|
|
return $gcode;
|
2012-06-28 12:44:54 +00:00
|
|
|
}
|
|
|
|
|
2012-11-17 17:07:13 +00:00
|
|
|
# http://hydraraptor.blogspot.it/2010/12/frequency-limit.html
|
2012-11-18 14:28:13 +00:00
|
|
|
sub _limit_frequency {
|
2012-11-17 17:07:13 +00:00
|
|
|
my $self = shift;
|
2012-11-18 14:28:13 +00:00
|
|
|
my ($point) = @_;
|
2012-11-17 17:07:13 +00:00
|
|
|
|
2012-12-06 09:54:28 +00:00
|
|
|
return '' if $Slic3r::Config->vibration_limit == 0;
|
2012-12-05 14:03:36 +00:00
|
|
|
my $min_time = 1 / ($Slic3r::Config->vibration_limit * 60); # in minutes
|
2012-11-17 17:07:13 +00:00
|
|
|
|
2012-11-18 14:28:13 +00:00
|
|
|
# calculate the move vector and move direction
|
2012-12-09 17:33:25 +00:00
|
|
|
my $vector = Slic3r::Line->new($self->last_pos, $point)->vector;
|
|
|
|
my @dir = map { $vector->[B][$_] <=> 0 } X,Y;
|
2012-11-18 14:28:13 +00:00
|
|
|
|
2012-12-09 17:33:25 +00:00
|
|
|
my $time = (unscale $vector->length) / $self->speeds->{$self->speed}; # in minutes
|
|
|
|
if ($time > 0) {
|
|
|
|
my @pause = ();
|
2012-11-18 14:28:13 +00:00
|
|
|
foreach my $axis (X,Y) {
|
2012-12-09 17:33:25 +00:00
|
|
|
if ($dir[$axis] != 0 && $self->last_dir->[$axis] != $dir[$axis]) {
|
|
|
|
if ($self->last_dir->[$axis] != 0) {
|
|
|
|
# this axis is changing direction: check whether we need to pause
|
|
|
|
if ($self->dir_time->[$axis] < $min_time) {
|
|
|
|
push @pause, ($min_time - $self->dir_time->[$axis]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$self->last_dir->[$axis] = $dir[$axis];
|
|
|
|
$self->dir_time->[$axis] = 0;
|
2012-11-18 10:33:53 +00:00
|
|
|
}
|
2012-12-09 17:33:25 +00:00
|
|
|
$self->dir_time->[$axis] += $time;
|
2012-11-18 14:28:13 +00:00
|
|
|
}
|
|
|
|
|
2012-12-09 17:33:25 +00:00
|
|
|
if (@pause) {
|
|
|
|
return sprintf "G4 P%d\n", max(@pause) * 60 * 1000;
|
2012-11-17 17:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 14:03:36 +00:00
|
|
|
return '';
|
2012-11-17 17:07:13 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
1;
|