diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 65d83f96e..911df206d 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -52,6 +52,7 @@ use Slic3r::GCode::ArcFitting; use Slic3r::GCode::CoolingBuffer; use Slic3r::GCode::Layer; use Slic3r::GCode::MotionPlanner; +use Slic3r::GCode::OozePrevention; use Slic3r::GCode::PlaceholderParser; use Slic3r::GCode::Reader; use Slic3r::GCode::SpiralVase; diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm index fab693b12..d1aeb38cf 100644 --- a/lib/Slic3r/Config.pm +++ b/lib/Slic3r/Config.pm @@ -432,6 +432,9 @@ sub read_ini { return $ini; } +package Slic3r::Config::GCode; +use parent 'Slic3r::Config'; + package Slic3r::Config::Print; use parent 'Slic3r::Config'; diff --git a/lib/Slic3r/Extruder.pm b/lib/Slic3r/Extruder.pm index 51e7ad2a6..404dc14bc 100644 --- a/lib/Slic3r/Extruder.pm +++ b/lib/Slic3r/Extruder.pm @@ -29,16 +29,6 @@ sub retract_speed_mm_min { return $self->retract_speed * 60; } -sub scaled_wipe_distance { - my ($self, $travel_speed) = @_; - - # how far do we move in XY at travel_speed for the time needed to consume - # retract_length at retract_speed? - # 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 - return scale($self->retract_length / $self->retract_speed * $travel_speed * 0.8); -} - sub extruded_volume { my ($self, $E) = @_; return $E * ($self->filament_diameter**2) * PI/4; diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index e5d60c383..9b8ed1622 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -9,12 +9,14 @@ 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 'placeholder_parser' => (is => 'rw', default => sub { Slic3r::GCode::PlaceholderParser->new }); -has 'standby_points' => (is => 'rw'); +has 'ooze_prevention' => (is => 'rw'); 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 'layer_count' => (is => 'ro', required => 1 ); +has 'layer_count' => (is => 'ro'); has '_layer_index' => (is => 'rw', default => sub {-1}); # just a counter has 'layer' => (is => 'rw'); has '_layer_islands' => (is => 'rw'); @@ -23,32 +25,28 @@ has '_seam_position' => (is => 'ro', default => sub { {} }); # $object => p has 'shift_x' => (is => 'rw', default => sub {0} ); has 'shift_y' => (is => 'rw', default => sub {0} ); has 'z' => (is => 'rw'); -has 'extruders' => (is => 'ro', default => sub {{}}); -has 'multiple_extruders' => (is => 'rw', default => sub {0}); -has 'extruder' => (is => 'rw'); 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}); has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds -has 'lifted' => (is => 'rw', default => sub {0} ); has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } ); -has 'last_fan_speed' => (is => 'rw', default => sub {0}); -has 'last_acceleration' => (is => 'rw', default => sub {0}); has 'wipe_path' => (is => 'rw'); +sub apply_print_config { + my ($self, $print_config) = @_; + + $self->SUPER::apply_print_config($print_config); + $self->config->apply_print_config($print_config); +} + sub set_extruders { - my ($self, $extruder_ids, $print_config) = @_; + my ($self, $extruder_ids) = @_; - foreach my $i (@$extruder_ids) { - $self->extruders->{$i} = my $e = Slic3r::Extruder->new($i, $print_config); - $self->enable_wipe(1) if $e->wipe; - } + $self->SUPER::set_extruders($extruder_ids); - # we enable support for multiple extruder if any extruder greater than 0 is used - # (even if prints only uses that one) since we need to output Tx commands - # first extruder has index 0 - $self->multiple_extruders(max(@$extruder_ids) > 0); + # enable wipe path generation if any extruder has wipe enabled + $self->enable_wipe(defined first { $_->wipe } values %{$self->_extruders}); } sub set_shift { @@ -82,49 +80,17 @@ sub change_layer { } my $gcode = ""; - if ($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/) { + if (defined $self->layer_count) { # TODO: cap this to 99% and add an explicit M73 P100 in the end G-code - $gcode .= sprintf "M73 P%s%s\n", - int(99 * ($self->_layer_index / ($self->layer_count - 1))), - ($self->config->gcode_comments ? ' ; update progress' : ''); + $gcode .= $self->update_progress(int(99 * ($self->_layer_index / ($self->layer_count - 1)))); } - $gcode .= $self->move_z($layer->print_z); - return $gcode; -} - -# this method accepts Z in unscaled coordinates -sub move_z { - my ($self, $z, $comment) = @_; - - my $gcode = ""; - - $z += $self->config->z_offset; - my $current_z = $self->z; - my $nominal_z = defined $current_z ? ($current_z - $self->lifted) : undef; - - if (!defined $current_z || $z > $current_z || $z < $nominal_z) { - # we're moving above the current actual Z (so above the lift height of the current - # layer if any) or below the current nominal layer - - # in both cases, we're going to the nominal Z of the next layer - $self->lifted(0); - - if ($self->extruder->retract_layer_change) { - # this retraction may alter $self->z - $gcode .= $self->retract(move_z => $z); - $current_z = $self->z; # update current z in case retract() changed it - $nominal_z = defined $current_z ? ($current_z - $self->lifted) : undef; - } - $gcode .= $self->G0(undef, $z, 0, $self->config->travel_speed*60, $comment || ('move to next layer (' . $self->layer->id . ')')) - if !defined $current_z || abs($z - $nominal_z) > epsilon; - } elsif ($z < $current_z) { - # we're moving above the current nominal layer height and below the current actual one. - # we're basically advancing to next layer, whose nominal Z is still lower than the previous - # layer Z with lift. - $self->lifted($current_z - $z); + my $z = $layer->print_z + $self->config->z_offset; # in unscaled coordinates + if ($self->_extruder->retract_layer_change && $self->will_move_z($z)) { + $gcode .= $self->retract; + $gcode .= $self->lift; } - + $gcode .= $self->travel_to_z($z, 'move to next layer (' . $self->layer->id . ')'); return $gcode; } @@ -154,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->_extruder->nozzle_diameter/2)}; # concave vertices have priority my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified; @@ -200,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->_extruder->nozzle_diameter) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER : 0; # get paths @@ -234,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->_extruder->nozzle_diameter), $first_segment->length); my $point = $first_segment->point_at($distance); $point->rotate($angle, $last_path_polyline->first_point); @@ -290,8 +256,8 @@ sub _extrude_path { } # calculate extrusion length per distance unit - my $e = $self->extruder->e_per_mm3 * $path->mm3_per_mm; - $e = 0 if !$self->config->get_extrusion_axis; + my $e_per_mm = $self->_extruder->e_per_mm3 * $path->mm3_per_mm; + $e_per_mm = 0 if !$self->_extrusion_axis; # set speed my $F; @@ -323,10 +289,10 @@ sub _extrude_path { $gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge; my $path_length = unscale $path->length; { - $gcode .= $path->gcode($self->extruder, $e, $F, - $self->shift_x - $self->extruder->extruder_offset->x, - $self->shift_y - $self->extruder->extruder_offset->y, #,, - $self->config->get_extrusion_axis, + $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, $self->config->gcode_comments ? " ; $description" : ""); if ($self->enable_wipe) { @@ -358,7 +324,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->_extruder->retract_before_travel || ($self->config->only_retract_when_crossing_perimeters && $self->config->fill_density > 0 && (first { $_->contains_line($travel) } @{$self->_upper_layer_islands}) @@ -366,11 +332,11 @@ sub travel_to { || (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && (first { $_->contains_line($travel) } @{$self->layer->support_islands})) ) { $self->straight_once(0); - $gcode .= $self->G0($point, undef, 0, $self->config->travel_speed*60, $comment || ""); + $gcode .= $self->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->G0($point, undef, 0, $self->config->travel_speed*60, $comment || ""); + $gcode .= $self->travel_to_xy($self->point_to_gcode($point), $comment); } else { if ($self->new_object) { $self->new_object(0); @@ -411,83 +377,75 @@ sub _plan { } } - # do the retract (the travel_to argument is broken) + # perform the retraction $gcode .= $self->retract if $need_retract; # append the actual path and return # use G1 because we rely on paths being straight (G0 may make round paths) - $gcode .= join '', map $self->G1($_->b, undef, 0, $self->config->travel_speed*60, $comment || ""), @travel; + $gcode .= join '', + map $self->travel_to_xy($self->point_to_gcode($_->b), $comment), + @travel; return $gcode; } sub retract { - my ($self, %params) = @_; + my ($self, $toolchange) = @_; - # 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"); + return "" if !defined $self->_extruder; - # if we already retracted, reduce the required amount of retraction - $length -= $self->extruder->retracted; - return "" unless $length > 0; my $gcode = ""; - # wipe - my $wipe_path; - if ($self->extruder->wipe && $self->wipe_path) { - my @points = @{$self->wipe_path}; - $wipe_path = Slic3r::Polyline->new($self->last_pos, @{$self->wipe_path}[1..$#{$self->wipe_path}]); - $wipe_path->clip_end($wipe_path->length - $self->extruder->scaled_wipe_distance($self->config->travel_speed)); - } - - # prepare moves - my $retract = [undef, undef, -$length, $self->extruder->retract_speed_mm_min, $comment]; - my $lift = ($self->config->retract_lift->[0] == 0 || defined $params{move_z}) && !$self->lifted - ? undef - : [undef, $self->z + $self->config->retract_lift->[0], 0, $self->config->travel_speed*60, 'lift plate during travel']; - - # check that we have a positive wipe length - if ($wipe_path) { - # subdivide the retraction + # wipe (if it's enabled for this extruder and we have a stored wipe path) + if ($self->_extruder->wipe && $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; + + # get the retraction length + my $length = $toolchange + ? $self->_extruder->retract_length + : $self->_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); + + # 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). + my $wipe_path = Slic3r::Polyline->new( + $self->last_pos, + @{$self->wipe_path}[1..$#{$self->wipe_path}], + ); + # + $wipe_path->clip_end($wipe_path->length - $wipe_dist); + + # subdivide the retraction in segments my $retracted = 0; foreach my $line (@{$wipe_path->lines}) { my $segment_length = $line->length; - # reduce retraction length a bit to avoid effective retraction speed to be greater than the configured one - # due to rounding - my $e = $retract->[2] * ($segment_length / $self->extruder->scaled_wipe_distance($self->config->travel_speed)) * 0.95; - $retracted += $e; - $gcode .= $self->G1($line->b, undef, $e, $self->config->travel_speed*60*0.8, $retract->[3] . ";_WIPE"); + # 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( + $self->point_to_gcode($line->b), + -$dE, + 'retract;_WIPE', + ); + $retracted += $dE; } - if ($retracted > $retract->[2]) { - # if we retracted less than we had to, retract the remainder - # TODO: add regression test - $gcode .= $self->G1(undef, undef, $retract->[2] - $retracted, $self->extruder->retract_speed_mm_min, $comment); - } - $gcode .= $self->reset_e; - } elsif ($self->config->use_firmware_retraction) { - $gcode .= "G10 ; retract\n"; - } else { - $gcode .= $self->G1(@$retract); - - # reset extrusion distance during retracts - # this makes sure we leave sufficient precision in the firmware - $gcode .= $self->reset_e; + $self->_extruder->set_retracted($self->_extruder->retracted + $retracted); } - if (!$self->lifted) { - if (defined $params{move_z} && $self->config->retract_lift->[0] > 0) { - my $travel = [undef, $params{move_z} + $self->config->retract_lift->[0], 0, $self->config->travel_speed*60, 'move to next layer (' . $self->layer->id . ') and lift']; - $gcode .= $self->G0(@$travel); - $self->lifted($self->config->retract_lift->[0]); - } elsif ($lift) { - $gcode .= $self->G1(@$lift); - } - } - $self->extruder->set_retracted($self->extruder->retracted + $length); - $self->extruder->set_restart_extra($restart_extra); - $self->lifted($self->config->retract_lift->[0]) if $lift; - $gcode .= "M103 ; extruder off\n" if $self->config->gcode_flavor eq 'makerware'; + # 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 .= $self->reset_e; + $gcode .= $self->lift; return $gcode; } @@ -496,53 +454,11 @@ sub unretract { my ($self) = @_; my $gcode = ""; - $gcode .= "M101 ; extruder on\n" if $self->config->gcode_flavor eq 'makerware'; - - if ($self->lifted) { - $gcode .= $self->G0(undef, $self->z - $self->lifted, 0, $self->config->travel_speed*60, 'restore layer Z'); - $self->lifted(0); - } - - my $to_unretract = $self->extruder->retracted + $self->extruder->restart_extra; - if ($to_unretract) { - if ($self->config->use_firmware_retraction) { - $gcode .= "G11 ; unretract\n"; - $gcode .= $self->reset_e; - } elsif ($self->config->get_extrusion_axis) { - # use G1 instead of G0 because G0 will blend the restart with the previous travel move - $gcode .= sprintf "G1 %s%.5f F%.3f", - $self->config->get_extrusion_axis, - $self->extruder->extrude($to_unretract), - $self->extruder->retract_speed_mm_min; - $gcode .= " ; compensate retraction" if $self->config->gcode_comments; - $gcode .= "\n"; - } - $self->extruder->set_retracted(0); - $self->extruder->set_restart_extra(0); - } - + $gcode .= $self->unlift; + $gcode .= $self->SUPER::unretract('compensate retraction'); return $gcode; } -sub reset_e { - my ($self) = @_; - return "" if $self->config->gcode_flavor =~ /^(?:mach3|makerware|sailfish)$/; - - $self->extruder->set_E(0) if $self->extruder; - return sprintf "G92 %s0%s\n", $self->config->get_extrusion_axis, ($self->config->gcode_comments ? ' ; reset extrusion distance' : '') - if $self->config->get_extrusion_axis && !$self->config->use_relative_e_distances; -} - -sub set_acceleration { - my ($self, $acceleration) = @_; - - return "" if !$acceleration || $acceleration == $self->last_acceleration; - - $self->last_acceleration($acceleration); - return sprintf "M204 S%s%s\n", - $acceleration, ($self->config->gcode_comments ? ' ; adjust acceleration' : ''); -} - sub G0 { my $self = shift; return $self->G1(@_) if !($self->config->g0 || $self->config->gcode_flavor eq 'mach3'); @@ -559,8 +475,8 @@ sub _G0_G1 { if ($point) { $gcode .= sprintf " X%.3f Y%.3f", - ($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 - $self->_extruder->extruder_offset->x, + ($point->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $self->_extruder->extruder_offset->y; #** $self->last_pos($point->clone); } if (defined $z && (!defined $self->z || $z != $self->z)) { @@ -571,14 +487,24 @@ sub _G0_G1 { return $self->_Gx($gcode, $e, $F, $comment); } +# convert a model-space scaled point into G-code coordinates +sub point_to_gcode { + my ($self, $point) = @_; + + 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, #** + ); +} + sub _Gx { my ($self, $gcode, $e, $F, $comment) = @_; $gcode .= sprintf " F%.3f", $F; # output extrusion distance - if ($e && $self->config->get_extrusion_axis) { - $gcode .= sprintf " %s%.5f", $self->config->get_extrusion_axis, $self->extruder->extrude($e); + if ($e && $self->_extrusion_axis) { + $gcode .= sprintf " %s%.5f", $self->_extrusion_axis, $self->_extruder->extrude($e); } $gcode .= " ; $comment" if $comment && $self->config->gcode_comments; @@ -588,124 +514,35 @@ sub _Gx { sub set_extruder { my ($self, $extruder_id) = @_; - # return nothing if this extruder was already selected - return "" if (defined $self->extruder) && ($self->extruder->id == $extruder_id); + return "" if !$self->need_toolchange($extruder_id); # if we are running a single-extruder setup, just set the extruder and return nothing - if (!$self->multiple_extruders) { - $self->extruder($self->extruders->{$extruder_id}); - return ""; + if (!$self->_multiple_extruders) { + return $self->_toolchange($extruder_id); } - # trigger retraction on the current extruder (if any) - my $gcode = ""; - $gcode .= $self->retract(toolchange => 1) if defined $self->extruder; + # 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->_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->_extruder->id, next_extruder => $extruder_id, }); } - # set the current extruder to the standby temperature - if ($self->standby_points && defined $self->extruder) { - # move to the nearest standby point - { - my $last_pos = $self->last_pos->clone; - $last_pos->translate(scale +$self->shift_x, scale +$self->shift_y); - my $standby_point = $last_pos->nearest_point($self->standby_points); - $standby_point->translate(scale -$self->shift_x, scale -$self->shift_y); - $gcode .= $self->travel_to($standby_point); - } - - if ($self->config->standby_temperature_delta != 0) { - my $temp = defined $self->layer && $self->layer->id == 0 - ? $self->extruder->first_layer_temperature - : $self->extruder->temperature; - # we assume that heating is always slower than cooling, so no need to block - $gcode .= $self->set_temperature($temp + $self->config->standby_temperature_delta, 0); - } - } + # 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; - # set the new extruder - $self->extruder($self->extruders->{$extruder_id}); - $gcode .= sprintf "%s%d%s\n", - ($self->config->gcode_flavor eq 'makerware' - ? 'M135 T' - : $self->config->gcode_flavor eq 'sailfish' - ? 'M108 T' - : 'T'), - $extruder_id, - ($self->config->gcode_comments ? ' ; change extruder' : ''); - - $gcode .= $self->reset_e; + # append the toolchange command + $gcode .= $self->_toolchange($extruder_id); # set the new extruder to the operating temperature - if ($self->config->ooze_prevention && $self->config->standby_temperature_delta != 0) { - my $temp = defined $self->layer && $self->layer->id == 0 - ? $self->extruder->first_layer_temperature - : $self->extruder->temperature; - $gcode .= $self->set_temperature($temp, 1); - } - - return $gcode; -} - -sub set_fan { - my ($self, $speed, $dont_save) = @_; - - if ($self->last_fan_speed != $speed || $dont_save) { - $self->last_fan_speed($speed) if !$dont_save; - if ($speed == 0) { - my $code = $self->config->gcode_flavor eq 'teacup' - ? 'M106 S0' - : $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/ - ? 'M127' - : 'M107'; - return sprintf "$code%s\n", ($self->config->gcode_comments ? ' ; disable fan' : ''); - } else { - if ($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/) { - return sprintf "M126%s\n", ($self->config->gcode_comments ? ' ; enable fan' : ''); - } else { - return sprintf "M106 %s%d%s\n", ($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), - (255 * $speed / 100), ($self->config->gcode_comments ? ' ; enable fan' : ''); - } - } - } - return ""; -} - -sub set_temperature { - my ($self, $temperature, $wait, $tool) = @_; - - return "" if $wait && $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/; - - my ($code, $comment) = ($wait && $self->config->gcode_flavor ne 'teacup') - ? ('M109', 'wait for temperature to be reached') - : ('M104', 'set temperature'); - my $gcode = sprintf "$code %s%d %s; $comment\n", - ($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature, - (defined $tool && ($self->multiple_extruders || $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/)) ? "T$tool " : ""; - - $gcode .= "M116 ; wait for temperature to be reached\n" - if $self->config->gcode_flavor eq 'teacup' && $wait; - - return $gcode; -} - -sub set_bed_temperature { - my ($self, $temperature, $wait) = @_; - - my ($code, $comment) = ($wait && $self->config->gcode_flavor ne 'teacup') - ? (($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/ ? 'M109' : 'M190'), 'wait for bed temperature to be reached') - : ('M140', 'set bed temperature'); - my $gcode = sprintf "$code %s%d ; $comment\n", - ($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature; - - $gcode .= "M116 ; wait for bed temperature to be reached\n" - if $self->config->gcode_flavor eq 'teacup' && $wait; + $gcode .= $self->ooze_prevention->post_toolchange($self) + if $self->ooze_prevention; return $gcode; } diff --git a/lib/Slic3r/GCode/Base.pm b/lib/Slic3r/GCode/Base.pm new file mode 100644 index 000000000..78a06d88f --- /dev/null +++ b/lib/Slic3r/GCode/Base.pm @@ -0,0 +1,408 @@ +package Slic3r::GCode::Base; +use Moo; + +use List::Util qw(min max first); +use Slic3r::Geometry qw(X Y epsilon); + +has 'gcode_config' => (is => 'ro', default => sub { Slic3r::Config::GCode->new }); +has '_extrusion_axis' => (is => 'rw', default => sub { 'E' }); +has '_extruders' => (is => 'ro', default => sub {{}}); +has '_extruder' => (is => 'rw'); +has '_multiple_extruders' => (is => 'rw', default => sub { 0 }); +has '_last_acceleration' => (is => 'rw', default => sub { 0 }); +has '_last_fan_speed' => (is => 'rw', default => sub { 0 }); +has '_lifted' => (is => 'rw', default => sub { 0 }); +has '_pos' => (is => 'rw', default => sub { Slic3r::Pointf3->new }); + +sub apply_print_config { + my ($self, $print_config) = @_; + + $self->gcode_config->apply_print_config($print_config); + + if ($self->gcode_config->gcode_flavor eq 'mach3') { + $self->_extrusion_axis('A'); + } elsif ($self->gcode_config->gcode_flavor eq 'no-extrusion') { + $self->_extrusion_axis(''); + } else { + $self->_extrusion_axis($self->gcode_config->extrusion_axis); + } +} + +sub set_extruders { + my ($self, $extruder_ids) = @_; + + foreach my $i (@$extruder_ids) { + $self->_extruders->{$i} = my $e = Slic3r::Extruder->new($i, $self->config); + } + + # we enable support for multiple extruder if any extruder greater than 0 is used + # (even if prints only uses that one) since we need to output Tx commands + # first extruder has index 0 + $self->_multiple_extruders(max(@$extruder_ids) > 0); +} + +sub set_temperature { + my ($self, $temperature, $wait, $tool) = @_; + + return "" if $wait && $self->gcode_config->gcode_flavor =~ /^(?:makerware|sailfish)$/; + + my ($code, $comment) = ($wait && $self->gcode_config->gcode_flavor ne 'teacup') + ? ('M109', 'wait for temperature to be reached') + : ('M104', 'set temperature'); + my $gcode = sprintf "$code %s%d %s; $comment\n", + ($self->gcode_config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature, + (defined $tool && ($self->_multiple_extruders || $self->gcode_config->gcode_flavor =~ /^(?:makerware|sailfish)$/)) ? "T$tool " : ""; + + $gcode .= "M116 ; wait for temperature to be reached\n" + if $self->gcode_config->gcode_flavor eq 'teacup' && $wait; + + return $gcode; +} + +sub set_bed_temperature { + my ($self, $temperature, $wait) = @_; + + my ($code, $comment) = ($wait && $self->gcode_config->gcode_flavor ne 'teacup') + ? (($self->gcode_config->gcode_flavor =~ /^(?:makerware|sailfish)$/ ? 'M109' : 'M190'), 'wait for bed temperature to be reached') + : ('M140', 'set bed temperature'); + my $gcode = sprintf "$code %s%d ; $comment\n", + ($self->gcode_config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature; + + $gcode .= "M116 ; wait for bed temperature to be reached\n" + if $self->gcode_config->gcode_flavor eq 'teacup' && $wait; + + return $gcode; +} + +sub set_fan { + my ($self, $speed, $dont_save) = @_; + + if ($self->_last_fan_speed != $speed || $dont_save) { + $self->_last_fan_speed($speed) if !$dont_save; + if ($speed == 0) { + my $code = $self->gcode_config->gcode_flavor eq 'teacup' + ? 'M106 S0' + : $self->gcode_config->gcode_flavor =~ /^(?:makerware|sailfish)$/ + ? 'M127' + : 'M107'; + return sprintf "$code%s\n", ($self->gcode_config->gcode_comments ? ' ; disable fan' : ''); + } else { + if ($self->gcode_config->gcode_flavor =~ /^(?:makerware|sailfish)$/) { + return sprintf "M126%s\n", ($self->gcode_config->gcode_comments ? ' ; enable fan' : ''); + } else { + return sprintf "M106 %s%d%s\n", ($self->gcode_config->gcode_flavor eq 'mach3' ? 'P' : 'S'), + (255 * $speed / 100), ($self->gcode_config->gcode_comments ? ' ; enable fan' : ''); + } + } + } + return ""; +} + +sub set_acceleration { + my ($self, $acceleration) = @_; + + return "" if !$acceleration || $acceleration == $self->_last_acceleration; + + $self->_last_acceleration($acceleration); + return sprintf "M204 S%s%s\n", + $acceleration, ($self->gcode_config->gcode_comments ? ' ; adjust acceleration' : ''); +} + +sub update_progress { + my ($self, $percent) = @_; + + return "" if $self->gcode_config->gcode_flavor !~ /^(?:makerware|sailfish)$/; + return sprintf "M73 P%s%s\n", + int($percent), + $self->_comment('update progress'); +} + +sub need_toolchange { + my ($self, $extruder_id) = @_; + + # return false if this extruder was already selected + return (!defined $self->_extruder) || ($self->_extruder->id != $extruder_id); +} + +sub set_extruder { + my ($self, $extruder_id) = @_; + + return "" if !$self->need_toolchange; + return $self->_toolchange($extruder_id); +} + +sub _toolchange { + my ($self, $extruder_id) = @_; + + # set the new extruder + $self->_extruder($self->_extruders->{$extruder_id}); + + # return the toolchange command + # if we are running a single-extruder setup, just set the extruder and return nothing + my $gcode = ""; + if ($self->_multiple_extruders) { + $gcode .= sprintf "%s%d%s\n", + ($self->gcode_config->gcode_flavor eq 'makerware' + ? 'M135 T' + : $self->gcode_config->gcode_flavor eq 'sailfish' + ? 'M108 T' + : 'T'), + $extruder_id, + ($self->gcode_config->gcode_comments ? ' ; change extruder' : ''); + + $gcode .= $self->reset_e(1); + } + return $gcode; +} + +sub reset_e { + my ($self, $force) = @_; + + return "" if $self->config->gcode_flavor =~ /^(?:mach3|makerware|sailfish)$/; + + if (defined $self->_extruder) { + return "" if $self->_extruder->E == 0 && !$force; + $self->_extruder->set_E(0) if $self->_extruder; + } + + if ($self->_extrusion_axis ne '' && !$self->gcode_config->use_relative_e_distances) { + return sprintf "G92 %s0%s\n", $self->gcode_config->extrusion_axis, ($self->config->gcode_comments ? ' ; reset extrusion distance' : ''); + } else { + return ""; + } +} + +sub _comment { + my ($self, $comment) = @_; + + return "" if (!defined $comment) || ($comment eq '') || !$self->config->gcode_comments; + return " ; $comment"; +} + +sub set_speed { + my ($self, $F, $comment) = @_; + + return sprintf "G1 F%.3f%s\n", + $F, + $self->_comment($comment); +} + +sub travel_to_xy { + my ($self, $pointf, $comment) = @_; + + $self->_pos->set_x($pointf->x); + $self->_pos->set_y($pointf->y); # )) + return sprintf "G1 X%.3f Y%.3f F%.3f%s\n", + @$pointf, + $self->gcode_config->travel_speed*60, + $self->_comment($comment); +} + +sub travel_to_xyz { + my ($self, $pointf3, $comment) = @_; + + # If target Z is lower than current Z but higher than nominal Z we + # don't perform the Z move but we only move in the XY plane and + # adjust the nominal Z by reducing the lift amount that will be + # used for unlift. + if (!$self->will_move_z($pointf3->z)) { + my $nominal_z = $self->_pos->z - $self->_lifted; + $self->_lifted($self->_lifted - ($pointf3->z - $nominal_z)); + return $self->travel_to_xy(Slic3r::Pointf->new(@$pointf3[X,Y])); + } + + # In all the other cases, we perform an actual XYZ move and cancel + # the lift. + $self->_lifted(0); + return $self->_travel_to_xyz($pointf3, $comment); +} + +sub _travel_to_xyz { + my ($self, $pointf3, $comment) = @_; + + $self->_pos($pointf3); + return sprintf "G1 X%.3f Y%.3f Z%.3f F%.3f%s\n", + @$pointf3, + $self->gcode_config->travel_speed*60, + $self->_comment($comment); +} + +sub travel_to_z { + my ($self, $z, $comment) = @_; + + # If target Z is lower than current Z but higher than nominal Z + # we don't perform the move but we only adjust the nominal Z by + # reducing the lift amount that will be used for unlift. + if (!$self->will_move_z($z)) { + my $nominal_z = $self->_pos->z - $self->_lifted; + $self->_lifted($self->_lifted - ($z - $nominal_z)); + return ""; + } + + # In all the other cases, we perform an actual Z move and cancel + # the lift. + $self->_lifted(0); + return $self->_travel_to_z($z, $comment); +} + +sub _travel_to_z { + my ($self, $z, $comment) = @_; + + $self->_pos->set_z($z); + return sprintf "G1 Z%.3f F%.3f%s\n", + $z, + $self->gcode_config->travel_speed*60, + $self->_comment($comment); +} + +sub will_move_z { + my ($self, $z) = @_; + + # If target Z is lower than current Z but higher than nominal Z + # we don't perform an actual Z move. + if ($self->_lifted > 0) { + my $nominal_z = $self->_pos->z - $self->_lifted; + if ($z >= $nominal_z && $z <= $self->_pos->z) { + return 0; + } + } + return 1; +} + +sub extrude_to_xy { + my ($self, $pointf, $dE, $comment) = @_; + + $self->_pos->set_x($pointf->x); + $self->_pos->set_y($pointf->y); # )) + $self->_extruder->extrude($dE); + return sprintf "G1 X%.3f Y%.3f %s%.5f%s\n", + @$pointf, + $self->_extrusion_axis, + $self->_extruder->E, + $self->_comment($comment); +} + +sub extrude_to_xyz { + my ($self, $pointf3, $dE, $comment) = @_; + + $self->_pos($pointf3); + $self->_lifted(0); + $self->_extruder->extrude($dE); + return sprintf "G1 X%.3f Y%.3f Z%.3f %s%.5f%s\n", + @$pointf3, + $self->_extrusion_axis, + $self->_extruder->E, + $self->_comment($comment); +} + +sub retract { + my ($self) = @_; + + return $self->_retract( + $self->_extruder->retract_length, + $self->_extruder->retract_restart_extra, + 'retract', + ); +} + +sub retract_for_toolchange { + my ($self) = @_; + + return $self->_retract( + $self->_extruder->retract_length_toolchange, + $self->_extruder->retract_restart_extra_toolchange, + 'retract for toolchange', + ); +} + +sub _retract { + my ($self, $length, $restart_extra, $comment) = @_; + + if ($self->gcode_config->use_firmware_retraction) { + return "G10 ; retract\n"; + } + + my $gcode = ""; + my $dE = $self->_extruder->retract($length, $restart_extra); + if ($dE != 0) { + $gcode = sprintf "G1 %s%.5f F%.3f%s\n", + $self->_extrusion_axis, + $self->_extruder->E, + $self->_extruder->retract_speed_mm_min, + $self->_comment($comment . " dE = $dE"); + } + + $gcode .= "M103 ; extruder off\n" + if $self->gcode_config->gcode_flavor eq 'makerware'; + + return $gcode; +} + +sub unretract { + my ($self, $comment) = @_; + + my $gcode = ""; + + $gcode .= "M101 ; extruder on\n" + if $self->gcode_config->gcode_flavor eq 'makerware'; + + if ($self->gcode_config->use_firmware_retraction) { + $gcode .= "G11 ; unretract\n"; + $gcode .= $self->reset_e; + return $gcode; + } + + my $dE = $self->_extruder->unretract; + if ($dE != 0) { + # use G1 instead of G0 because G0 will blend the restart with the previous travel move + $gcode .= sprintf "G1 %s%.5f F%.3f%s\n", + $self->_extrusion_axis, + $self->_extruder->E, + $self->_extruder->retract_speed_mm_min, + $self->_comment($comment); + } + + return $gcode; +} + +# If this method is called more than once before calling unlift(), +# it will not perform subsequent lifts, even if Z was raised manually +# (i.e. with travel_to_z()) and thus _lifted was reduced. +sub lift { + my ($self) = @_; + + if ($self->_lifted == 0 && $self->gcode_config->retract_lift->[0] > 0) { + my $to_lift = $self->gcode_config->retract_lift->[0]; + $self->_lifted($to_lift); + return $self->_travel_to_z($self->_pos->z + $to_lift, 'lift Z'); + } + return ""; +} + +sub unlift { + my ($self) = @_; + + my $gcode = ""; + if ($self->_lifted > 0) { + $gcode .= $self->_travel_to_z($self->_pos->z - $self->_lifted, 'restore layer Z'); + $self->_lifted(0); + } + return $gcode; +} + +sub has_multiple_extruders { + my ($self) = @_; + return $self->_multiple_extruders; +} + +sub extruder { + my ($self) = @_; + return $self->_extruder; +} + +sub extruders { + my ($self) = @_; + return [ sort { $a->id <=> $b->id } values %{$self->_extruders} ]; +} + +1; diff --git a/lib/Slic3r/GCode/Layer.pm b/lib/Slic3r/GCode/Layer.pm index 5f3531d6d..2f70a4a4b 100644 --- a/lib/Slic3r/GCode/Layer.pm +++ b/lib/Slic3r/GCode/Layer.pm @@ -63,8 +63,7 @@ sub process_layer { $self->gcodegen->enable_loop_clipping(!defined $self->spiralvase || !$self->spiralvase->enable); if (!$self->second_layer_things_done && $layer->id == 1) { - for my $extruder_id (sort keys %{$self->extruders}) { - my $extruder = $self->extruders->{$extruder_id}; + for my $extruder (@{$self->extruders}) { $gcode .= $self->gcodegen->set_temperature($extruder->temperature, 0, $extruder->id) if $extruder->temperature && $extruder->temperature != $extruder->first_layer_temperature; } @@ -83,7 +82,7 @@ sub process_layer { if (((values %{$self->skirt_done}) < $self->print->config->skirt_height || $self->print->config->skirt_height == -1) && !$self->skirt_done->{$layer->print_z}) { $self->gcodegen->set_shift(@{$self->shift}); - my @extruder_ids = sort keys %{$self->extruders}; + my @extruder_ids = map { $_->id } @{$self->extruders}; $gcode .= $self->gcodegen->set_extruder($extruder_ids[0]); # skip skirt if we have a large brim if ($layer->id < $self->print->config->skirt_height || $self->print->config->skirt_height == -1) { @@ -136,7 +135,7 @@ sub process_layer { # tweak region ordering to save toolchanges my @region_ids = 0 .. ($self->print->region_count-1); - if ($self->gcodegen->multiple_extruders) { + if ($self->gcodegen->has_multiple_extruders) { my $last_extruder = $self->gcodegen->extruder; my $best_region_id = first { $self->print->regions->[$_]->config->perimeter_extruder-1 eq $last_extruder } @region_ids; @region_ids = ($best_region_id, grep $_ != $best_region_id, @region_ids) if $best_region_id; @@ -176,7 +175,7 @@ sub process_layer { # give priority to infill if we were already using its extruder and it wouldn't # be good for perimeters 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->has_multiple_extruders && $region->config->infill_extruder-1 == $self->gcodegen->extruder->id && $region->config->infill_extruder != $region->config->perimeter_extruder)) { $gcode .= $self->_extrude_infill($infill_by_island[$i], $region); $gcode .= $self->_extrude_perimeters($perimeters_by_island[$i], $region); } else { diff --git a/lib/Slic3r/GCode/OozePrevention.pm b/lib/Slic3r/GCode/OozePrevention.pm new file mode 100644 index 000000000..5533d2ea0 --- /dev/null +++ b/lib/Slic3r/GCode/OozePrevention.pm @@ -0,0 +1,48 @@ +package Slic3r::GCode::OozePrevention; +use Moo; + +use Slic3r::Geometry qw(scale); + +has 'standby_points' => (is => 'rw', required => 1); + +sub pre_toolchange { + my ($self, $gcodegen) = @_; + + my $gcode = ""; + + # move to the nearest standby point + if (@{$self->standby_points}) { + my $last_pos = $gcodegen->last_pos->clone; + $last_pos->translate(scale +$gcodegen->shift_x, scale +$gcodegen->shift_y); + my $standby_point = $last_pos->nearest_point($self->standby_points); + $standby_point->translate(scale -$gcodegen->shift_x, scale -$gcodegen->shift_y); + $gcode .= $gcodegen->travel_to($standby_point); + } + + if ($gcodegen->config->standby_temperature_delta != 0) { + my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0 + ? $gcodegen->_extruder->first_layer_temperature + : $gcodegen->_extruder->temperature; + # 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); + } + + return $gcode; +} + +sub post_toolchange { + my ($self, $gcodegen) = @_; + + my $gcode = ""; + + if ($gcodegen->config->standby_temperature_delta != 0) { + my $temp = defined $gcodegen->layer && $gcodegen->layer->id == 0 + ? $gcodegen->_extruder->first_layer_temperature + : $gcodegen->_extruder->temperature; + $gcode .= $gcodegen->set_temperature($temp, 1); + } + + return $gcode; +} + +1; diff --git a/lib/Slic3r/Point.pm b/lib/Slic3r/Point.pm index 6ec4e60bc..abb7a5edf 100644 --- a/lib/Slic3r/Point.pm +++ b/lib/Slic3r/Point.pm @@ -7,4 +7,13 @@ sub new_scale { return $class->new(map Slic3r::Geometry::scale($_), @_); } +package Slic3r::Pointf; +use strict; +use warnings; + +sub new_unscale { + my $class = shift; + return $class->new(map Slic3r::Geometry::unscale($_), @_); +} + 1; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 4c3a04107..4234f57df 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -773,8 +773,8 @@ sub write_gcode { placeholder_parser => $self->placeholder_parser, layer_count => $layer_count, ); - $gcodegen->config->apply_print_config($self->config); - $gcodegen->set_extruders($self->extruders, $self->config); + $gcodegen->apply_print_config($self->config); + $gcodegen->set_extruders($self->extruders); print $fh "G21 ; set units to millimeters\n" if $self->config->gcode_flavor ne 'makerware'; print $fh $gcodegen->set_fan(0, 1) if $self->config->cooling && $self->config->disable_fan_first_layers; @@ -842,7 +842,11 @@ sub write_gcode { $s->translate(map scale($_), @{$self->config->get_at('extruder_offset', $extruder_id)}); } my $convex_hull = convex_hull([ map @$_, @skirts ]); - $gcodegen->standby_points([ map $_->clone, map @$_, map $_->subdivide(scale 10), @{offset([$convex_hull], scale 3)} ]); + + my $oozeprev = Slic3r::GCode::OozePrevention->new( + standby_points => [ map $_->clone, map @$_, map $_->subdivide(scale 10), @{offset([$convex_hull], scale 3)} ], + ); + $gcodegen->ooze_prevention($oozeprev); } } @@ -935,14 +939,13 @@ sub write_gcode { } # write end commands to file - print $fh $gcodegen->retract if $gcodegen->extruder; # empty prints don't even set an extruder + print $fh $gcodegen->retract; print $fh $gcodegen->set_fan(0); printf $fh "%s\n", $gcodegen->placeholder_parser->process($self->config->end_gcode); $self->total_used_filament(0); $self->total_extruded_volume(0); - foreach my $extruder_id (@{$self->extruders}) { - my $extruder = $gcodegen->extruders->{$extruder_id}; + foreach my $extruder (@{$gcodegen->extruders}) { # the final retraction doesn't really count as "used filament" my $used_filament = $extruder->absolute_E + $extruder->retract_length; my $extruded_volume = $extruder->extruded_volume($used_filament); diff --git a/t/retraction.t b/t/retraction.t index b86255dfc..6c96abd62 100644 --- a/t/retraction.t +++ b/t/retraction.t @@ -17,7 +17,7 @@ use Slic3r::Test qw(_eq); my $test = sub { my ($conf) = @_; $conf ||= $config; - + my $print = Slic3r::Test::init_print('20mm_cube', config => $conf, duplicate => $duplicate); my $tool = 0; @@ -78,7 +78,7 @@ use Slic3r::Test qw(_eq); $expected_amount = $print->print->config->get_at('retract_length_toolchange', $tool) + $print->print->config->get_at('retract_restart_extra_toolchange', $tool); $changed_tool = 0; } - fail 'unretracted by the correct amount' + fail 'unretracted by the correct amount' && exit if !_eq($info->{dist_E}, $expected_amount); $retracted[$tool] = 0; $retracted_length[$tool] = 0; diff --git a/xs/src/libslic3r/Extruder.cpp b/xs/src/libslic3r/Extruder.cpp index d327276f8..d3919e5e4 100644 --- a/xs/src/libslic3r/Extruder.cpp +++ b/xs/src/libslic3r/Extruder.cpp @@ -21,13 +21,49 @@ Extruder::reset() double Extruder::extrude(double dE) { - if (this->config->use_relative_e_distances) { + // in case of relative E distances we always reset to 0 before any output + if (this->config->use_relative_e_distances) this->E = 0; - } this->E += dE; this->absolute_E += dE; - return this->E; + return dE; +} + +/* This method makes sure the extruder is retracted by the specified amount + of filament and returns the amount of filament retracted. + If the extruder is already retracted by the same or a greater amount, + this method is a no-op. + The restart_extra argument sets the extra length to be used for + unretraction. If we're actually performing a retraction, any restart_extra + value supplied will overwrite the previous one if any. */ +double +Extruder::retract(double length, double restart_extra) +{ + // in case of relative E distances we always reset to 0 before any output + if (this->config->use_relative_e_distances) + this->E = 0; + + double to_retract = length - this->retracted; + if (to_retract > 0) { + this->E -= to_retract; + this->absolute_E -= to_retract; + this->retracted += to_retract; + this->restart_extra = restart_extra; + return to_retract; + } else { + return 0; + } +} + +double +Extruder::unretract() +{ + double dE = this->retracted + this->restart_extra; + this->extrude(dE); + this->retracted = 0; + this->restart_extra = 0; + return dE; } Pointf diff --git a/xs/src/libslic3r/Extruder.hpp b/xs/src/libslic3r/Extruder.hpp index 9caa38c89..99182c58a 100644 --- a/xs/src/libslic3r/Extruder.hpp +++ b/xs/src/libslic3r/Extruder.hpp @@ -14,7 +14,8 @@ class Extruder virtual ~Extruder() {} void reset(); double extrude(double dE); - + double retract(double length, double restart_extra); + double unretract(); Pointf extruder_offset() const; double nozzle_diameter() const; diff --git a/xs/src/libslic3r/ExtrusionEntity.cpp b/xs/src/libslic3r/ExtrusionEntity.cpp index fd39fdc9f..725693fae 100644 --- a/xs/src/libslic3r/ExtrusionEntity.cpp +++ b/xs/src/libslic3r/ExtrusionEntity.cpp @@ -123,7 +123,11 @@ ExtrusionPath::gcode(Extruder* extruder, double e, double F, const double line_length = line_it->length() * SCALING_FACTOR; // calculate extrusion length for this line - double E = (e == 0) ? 0 : extruder->extrude(e * line_length); + double E = 0; + if (e > 0) { + extruder->extrude(e * line_length); + E = extruder->E; + } // compose G-code line diff --git a/xs/src/libslic3r/PrintConfig.cpp b/xs/src/libslic3r/PrintConfig.cpp index 3486afc22..f5b277134 100644 --- a/xs/src/libslic3r/PrintConfig.cpp +++ b/xs/src/libslic3r/PrintConfig.cpp @@ -960,6 +960,7 @@ t_optiondef_map PrintConfigDef::def = PrintConfigDef::build_def(); REGISTER_CLASS(DynamicPrintConfig, "Config"); REGISTER_CLASS(PrintObjectConfig, "Config::PrintObject"); REGISTER_CLASS(PrintRegionConfig, "Config::PrintRegion"); +REGISTER_CLASS(GCodeConfig, "Config::GCode"); REGISTER_CLASS(PrintConfig, "Config::Print"); REGISTER_CLASS(FullPrintConfig, "Config::Full"); #endif diff --git a/xs/src/libslic3r/PrintConfig.hpp b/xs/src/libslic3r/PrintConfig.hpp index cdc3d7173..29bfe6646 100644 --- a/xs/src/libslic3r/PrintConfig.hpp +++ b/xs/src/libslic3r/PrintConfig.hpp @@ -306,7 +306,62 @@ class PrintRegionConfig : public virtual StaticPrintConfig }; }; -class PrintConfig : public virtual StaticPrintConfig +class GCodeConfig : public virtual StaticPrintConfig +{ + public: + ConfigOptionString extrusion_axis; + ConfigOptionBool gcode_comments; + ConfigOptionEnum gcode_flavor; + ConfigOptionFloats retract_length; + ConfigOptionFloats retract_length_toolchange; + ConfigOptionFloats retract_lift; + ConfigOptionFloats retract_restart_extra; + ConfigOptionFloats retract_restart_extra_toolchange; + ConfigOptionInts retract_speed; + ConfigOptionFloat travel_speed; + ConfigOptionBool use_firmware_retraction; + ConfigOptionBool use_relative_e_distances; + + GCodeConfig() : StaticPrintConfig() { + this->extrusion_axis.value = "E"; + this->gcode_comments.value = false; + this->gcode_flavor.value = gcfRepRap; + this->retract_length.values.resize(1); + this->retract_length.values[0] = 1; + this->retract_length_toolchange.values.resize(1); + this->retract_length_toolchange.values[0] = 10; + this->retract_lift.values.resize(1); + this->retract_lift.values[0] = 0; + this->retract_restart_extra.values.resize(1); + this->retract_restart_extra.values[0] = 0; + this->retract_restart_extra_toolchange.values.resize(1); + this->retract_restart_extra_toolchange.values[0] = 0; + this->retract_speed.values.resize(1); + this->retract_speed.values[0] = 30; + this->travel_speed.value = 130; + this->use_firmware_retraction.value = false; + this->use_relative_e_distances.value = false; + }; + + ConfigOption* option(const t_config_option_key opt_key, bool create = false) { + if (opt_key == "extrusion_axis") return &this->extrusion_axis; + if (opt_key == "gcode_comments") return &this->gcode_comments; + if (opt_key == "gcode_flavor") return &this->gcode_flavor; + if (opt_key == "retract_length") return &this->retract_length; + if (opt_key == "retract_length_toolchange") return &this->retract_length_toolchange; + if (opt_key == "retract_lift") return &this->retract_lift; + if (opt_key == "retract_restart_extra") return &this->retract_restart_extra; + if (opt_key == "retract_restart_extra_toolchange") return &this->retract_restart_extra_toolchange; + if (opt_key == "retract_speed") return &this->retract_speed; + if (opt_key == "travel_speed") return &this->travel_speed; + if (opt_key == "use_firmware_retraction") return &this->use_firmware_retraction; + if (opt_key == "use_relative_e_distances") return &this->use_relative_e_distances; + + return NULL; + }; +}; + +class PrintConfig : public GCodeConfig { public: ConfigOptionBool avoid_crossing_perimeters; @@ -324,7 +379,6 @@ class PrintConfig : public virtual StaticPrintConfig ConfigOptionFloat extruder_clearance_height; ConfigOptionFloat extruder_clearance_radius; ConfigOptionPoints extruder_offset; - ConfigOptionString extrusion_axis; ConfigOptionFloats extrusion_multiplier; ConfigOptionBool fan_always_on; ConfigOptionInt fan_below_layer_time; @@ -336,8 +390,6 @@ class PrintConfig : public virtual StaticPrintConfig ConfigOptionInts first_layer_temperature; ConfigOptionBool g0; ConfigOptionBool gcode_arcs; - ConfigOptionBool gcode_comments; - ConfigOptionEnum gcode_flavor; ConfigOptionFloat infill_acceleration; ConfigOptionBool infill_first; ConfigOptionString layer_gcode; @@ -355,12 +407,6 @@ class PrintConfig : public virtual StaticPrintConfig ConfigOptionFloat resolution; ConfigOptionFloats retract_before_travel; ConfigOptionBools retract_layer_change; - ConfigOptionFloats retract_length; - ConfigOptionFloats retract_length_toolchange; - ConfigOptionFloats retract_lift; - ConfigOptionFloats retract_restart_extra; - ConfigOptionFloats retract_restart_extra_toolchange; - ConfigOptionInts retract_speed; ConfigOptionFloat skirt_distance; ConfigOptionInt skirt_height; ConfigOptionInt skirts; @@ -371,14 +417,11 @@ class PrintConfig : public virtual StaticPrintConfig ConfigOptionInts temperature; ConfigOptionInt threads; ConfigOptionString toolchange_gcode; - ConfigOptionFloat travel_speed; - ConfigOptionBool use_firmware_retraction; - ConfigOptionBool use_relative_e_distances; ConfigOptionFloat vibration_limit; ConfigOptionBools wipe; ConfigOptionFloat z_offset; - PrintConfig() : StaticPrintConfig() { + PrintConfig() : GCodeConfig() { this->avoid_crossing_perimeters.value = false; this->bed_shape.values.push_back(Pointf(0,0)); this->bed_shape.values.push_back(Pointf(200,0)); @@ -398,7 +441,6 @@ class PrintConfig : public virtual StaticPrintConfig this->extruder_clearance_radius.value = 20; this->extruder_offset.values.resize(1); this->extruder_offset.values[0] = Pointf(0,0); - this->extrusion_axis.value = "E"; this->extrusion_multiplier.values.resize(1); this->extrusion_multiplier.values[0] = 1; this->fan_always_on.value = false; @@ -415,8 +457,6 @@ class PrintConfig : public virtual StaticPrintConfig this->first_layer_temperature.values[0] = 200; this->g0.value = false; this->gcode_arcs.value = false; - this->gcode_comments.value = false; - this->gcode_flavor.value = gcfRepRap; this->infill_acceleration.value = 0; this->infill_first.value = false; this->layer_gcode.value = ""; @@ -436,18 +476,6 @@ class PrintConfig : public virtual StaticPrintConfig this->retract_before_travel.values[0] = 2; this->retract_layer_change.values.resize(1); this->retract_layer_change.values[0] = true; - this->retract_length.values.resize(1); - this->retract_length.values[0] = 1; - this->retract_length_toolchange.values.resize(1); - this->retract_length_toolchange.values[0] = 10; - this->retract_lift.values.resize(1); - this->retract_lift.values[0] = 0; - this->retract_restart_extra.values.resize(1); - this->retract_restart_extra.values[0] = 0; - this->retract_restart_extra_toolchange.values.resize(1); - this->retract_restart_extra_toolchange.values[0] = 0; - this->retract_speed.values.resize(1); - this->retract_speed.values[0] = 30; this->skirt_distance.value = 6; this->skirt_height.value = 1; this->skirts.value = 1; @@ -459,9 +487,6 @@ class PrintConfig : public virtual StaticPrintConfig this->temperature.values[0] = 200; this->threads.value = 2; this->toolchange_gcode.value = ""; - this->travel_speed.value = 130; - this->use_firmware_retraction.value = false; - this->use_relative_e_distances.value = false; this->vibration_limit.value = 0; this->wipe.values.resize(1); this->wipe.values[0] = false; @@ -484,7 +509,6 @@ class PrintConfig : public virtual StaticPrintConfig if (opt_key == "extruder_clearance_height") return &this->extruder_clearance_height; if (opt_key == "extruder_clearance_radius") return &this->extruder_clearance_radius; if (opt_key == "extruder_offset") return &this->extruder_offset; - if (opt_key == "extrusion_axis") return &this->extrusion_axis; if (opt_key == "extrusion_multiplier") return &this->extrusion_multiplier; if (opt_key == "fan_always_on") return &this->fan_always_on; if (opt_key == "fan_below_layer_time") return &this->fan_below_layer_time; @@ -496,8 +520,6 @@ class PrintConfig : public virtual StaticPrintConfig if (opt_key == "first_layer_temperature") return &this->first_layer_temperature; if (opt_key == "g0") return &this->g0; if (opt_key == "gcode_arcs") return &this->gcode_arcs; - if (opt_key == "gcode_comments") return &this->gcode_comments; - if (opt_key == "gcode_flavor") return &this->gcode_flavor; if (opt_key == "infill_acceleration") return &this->infill_acceleration; if (opt_key == "infill_first") return &this->infill_first; if (opt_key == "layer_gcode") return &this->layer_gcode; @@ -515,12 +537,6 @@ class PrintConfig : public virtual StaticPrintConfig if (opt_key == "resolution") return &this->resolution; if (opt_key == "retract_before_travel") return &this->retract_before_travel; if (opt_key == "retract_layer_change") return &this->retract_layer_change; - if (opt_key == "retract_length") return &this->retract_length; - if (opt_key == "retract_length_toolchange") return &this->retract_length_toolchange; - if (opt_key == "retract_lift") return &this->retract_lift; - if (opt_key == "retract_restart_extra") return &this->retract_restart_extra; - if (opt_key == "retract_restart_extra_toolchange") return &this->retract_restart_extra_toolchange; - if (opt_key == "retract_speed") return &this->retract_speed; if (opt_key == "skirt_distance") return &this->skirt_distance; if (opt_key == "skirt_height") return &this->skirt_height; if (opt_key == "skirts") return &this->skirts; @@ -531,18 +547,20 @@ class PrintConfig : public virtual StaticPrintConfig if (opt_key == "temperature") return &this->temperature; if (opt_key == "threads") return &this->threads; if (opt_key == "toolchange_gcode") return &this->toolchange_gcode; - if (opt_key == "travel_speed") return &this->travel_speed; - if (opt_key == "use_firmware_retraction") return &this->use_firmware_retraction; - if (opt_key == "use_relative_e_distances") return &this->use_relative_e_distances; if (opt_key == "vibration_limit") return &this->vibration_limit; if (opt_key == "wipe") return &this->wipe; if (opt_key == "z_offset") return &this->z_offset; + // look in parent class + ConfigOption* opt; + if ((opt = GCodeConfig::option(opt_key, create)) != NULL) return opt; + return NULL; }; }; -class FullPrintConfig : public PrintObjectConfig, public PrintRegionConfig, public PrintConfig { +class FullPrintConfig : public PrintObjectConfig, public PrintRegionConfig, public PrintConfig +{ public: ConfigOption* option(const t_config_option_key opt_key, bool create = false) { ConfigOption* opt; @@ -551,15 +569,6 @@ class FullPrintConfig : public PrintObjectConfig, public PrintRegionConfig, publ if ((opt = PrintConfig::option(opt_key, create)) != NULL) return opt; return NULL; }; - - std::string get_extrusion_axis() { - if (this->gcode_flavor == gcfMach3) { - return std::string("A"); - } else if (this->gcode_flavor == gcfNoExtrusion) { - return std::string(""); - } - return this->extrusion_axis; - } }; } diff --git a/xs/xsp/Config.xsp b/xs/xsp/Config.xsp index 4c56e10c8..b11533e36 100644 --- a/xs/xsp/Config.xsp +++ b/xs/xsp/Config.xsp @@ -28,6 +28,27 @@ void normalize(); }; +%name{Slic3r::Config::GCode} class GCodeConfig { + GCodeConfig(); + ~GCodeConfig(); + bool has(t_config_option_key opt_key); + SV* as_hash(); + SV* get(t_config_option_key opt_key); + SV* get_at(t_config_option_key opt_key, int i); + bool set(t_config_option_key opt_key, SV* value); + bool set_deserialize(t_config_option_key opt_key, SV* str); + std::string serialize(t_config_option_key opt_key); + double get_abs_value(t_config_option_key opt_key); + %name{get_abs_value_over} + double get_abs_value(t_config_option_key opt_key, double ratio_over); + void apply_print_config(PrintConfig* other) + %code{% THIS->apply(*other, true); %}; + void apply_dynamic(DynamicPrintConfig* other) + %code{% THIS->apply(*other, true); %}; + std::vector get_keys() + %code{% THIS->keys(&RETVAL); %}; +}; + %name{Slic3r::Config::Print} class PrintConfig { PrintConfig(); ~PrintConfig(); @@ -112,7 +133,6 @@ %code{% THIS->apply(*other, true); %}; std::vector get_keys() %code{% THIS->keys(&RETVAL); %}; - std::string get_extrusion_axis(); }; %package{Slic3r::Config}; diff --git a/xs/xsp/Extruder.xsp b/xs/xsp/Extruder.xsp index 582b39b99..43bdbfabc 100644 --- a/xs/xsp/Extruder.xsp +++ b/xs/xsp/Extruder.xsp @@ -6,10 +6,14 @@ %} %name{Slic3r::Extruder} class Extruder { - Extruder(int id, PrintConfig *config); + // We don't need a Perl constructor with PrintConfig at the moment + //Extruder(int id, PrintConfig *config); + Extruder(int id, FullPrintConfig *config); ~Extruder(); void reset(); double extrude(double dE); + double retract(double length, double restart_extra); + double unretract(); int id() const %code%{ RETVAL = THIS->id; %}; diff --git a/xs/xsp/Point.xsp b/xs/xsp/Point.xsp index 774b2fc1a..e1163ea5c 100644 --- a/xs/xsp/Point.xsp +++ b/xs/xsp/Point.xsp @@ -110,4 +110,10 @@ Point::coincides_with(point_sv) %code{% RETVAL = THIS->y; %}; double z() %code{% RETVAL = THIS->z; %}; + void set_x(double val) + %code{% THIS->x = val; %}; + void set_y(double val) + %code{% THIS->y = val; %}; + void set_z(double val) + %code{% THIS->z = val; %}; }; diff --git a/xs/xsp/my.map b/xs/xsp/my.map index a7264c62e..41476ca2c 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -29,6 +29,9 @@ Ref O_OBJECT_SLIC3R_T PrintRegionConfig* O_OBJECT_SLIC3R Ref O_OBJECT_SLIC3R_T +GCodeConfig* O_OBJECT_SLIC3R +Ref O_OBJECT_SLIC3R_T + PrintConfig* O_OBJECT_SLIC3R Ref O_OBJECT_SLIC3R_T diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 57c321bb4..87283debf 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -40,6 +40,8 @@ %typemap{Ref}{simple}; %typemap{PrintRegionConfig*}; %typemap{Ref}{simple}; +%typemap{GCodeConfig*}; +%typemap{Ref}{simple}; %typemap{PrintConfig*}; %typemap{Ref}{simple}; %typemap{FullPrintConfig*};