diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index db5ce284c..14080cb17 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -296,42 +296,4 @@ sub _extrude_path { return $gcode; } -# This method accepts $point in print coordinates. -sub travel_to { - my ($self, $point, $role, $comment) = @_; - - # Define the travel move as a line between current position and the taget point. - # This is expressed in print coordinates, so it will need to be translated by - # $self->origin in order to get G-code coordinates. - my $travel = Slic3r::Polyline->new($self->last_pos, $point); - - # check whether a straight travel move would need retraction - my $needs_retraction = $self->needs_retraction($travel, $role // EXTR_ROLE_NONE); - - # if a retraction would be needed, try to use avoid_crossing_perimeters to plan a - # multi-hop travel path inside the configuration space - if ($needs_retraction - && $self->config->avoid_crossing_perimeters - && !$self->avoid_crossing_perimeters->disable_once) { - $travel = $self->avoid_crossing_perimeters->travel_to($self, $point); - - # check again whether the new travel path still needs a retraction - $needs_retraction = $self->needs_retraction($travel, $role // EXTR_ROLE_NONE); - } - - # Re-allow avoid_crossing_perimeters for the next travel moves - $self->avoid_crossing_perimeters->set_disable_once(0); - $self->avoid_crossing_perimeters->set_use_external_mp_once(0); - - # generate G-code for the travel move - my $gcode = ""; - $gcode .= $self->retract if $needs_retraction; - - # use G1 because we rely on paths being straight (G0 may make round paths) - $gcode .= $self->writer->travel_to_xy($self->point_to_gcode($_->b), $comment) - for @{$travel->lines}; - - return $gcode; -} - 1; diff --git a/xs/src/libslic3r/GCode.cpp b/xs/src/libslic3r/GCode.cpp index fe6e3089e..1c3a6dce0 100644 --- a/xs/src/libslic3r/GCode.cpp +++ b/xs/src/libslic3r/GCode.cpp @@ -267,6 +267,47 @@ GCode::preamble() return gcode; } +// This method accepts &point in print coordinates. +std::string +GCode::travel_to(const Point &point, ExtrusionRole role, std::string comment) +{ + /* Define the travel move as a line between current position and the taget point. + This is expressed in print coordinates, so it will need to be translated by + $self->origin in order to get G-code coordinates. */ + Polyline travel; + travel.append(this->last_pos()); + travel.append(point); + + // check whether a straight travel move would need retraction + bool needs_retraction = this->needs_retraction(travel, role); + + // if a retraction would be needed, try to use avoid_crossing_perimeters to plan a + // multi-hop travel path inside the configuration space + if (needs_retraction + && this->config.avoid_crossing_perimeters + && !this->avoid_crossing_perimeters.disable_once) { + travel = this->avoid_crossing_perimeters.travel_to(*this, point); + + // check again whether the new travel path still needs a retraction + needs_retraction = this->needs_retraction(travel, role); + } + + // Re-allow avoid_crossing_perimeters for the next travel moves + this->avoid_crossing_perimeters.disable_once = false; + this->avoid_crossing_perimeters.use_external_mp_once = false; + + // generate G-code for the travel move + std::string gcode; + if (needs_retraction) gcode += this->retract(); + + // use G1 because we rely on paths being straight (G0 may make round paths) + Lines lines = travel.lines(); + for (Lines::const_iterator line = lines.begin(); line != lines.end(); ++line) + gcode += this->writer.travel_to_xy(this->point_to_gcode(line->b), comment); + + return gcode; +} + bool GCode::needs_retraction(const Polyline &travel, ExtrusionRole role) { diff --git a/xs/src/libslic3r/GCode.hpp b/xs/src/libslic3r/GCode.hpp index ee6f78c57..d71069c92 100644 --- a/xs/src/libslic3r/GCode.hpp +++ b/xs/src/libslic3r/GCode.hpp @@ -92,6 +92,7 @@ class GCode { void apply_print_config(const PrintConfig &print_config); void set_origin(const Pointf &pointf); std::string preamble(); + std::string travel_to(const Point &point, ExtrusionRole role, std::string comment); bool needs_retraction(const Polyline &travel, ExtrusionRole role = erNone); std::string retract(bool toolchange = false); std::string unretract(); diff --git a/xs/xsp/GCode.xsp b/xs/xsp/GCode.xsp index 3465c3c2c..7595e829a 100644 --- a/xs/xsp/GCode.xsp +++ b/xs/xsp/GCode.xsp @@ -157,6 +157,8 @@ void set_origin(Pointf* pointf) %code{% THIS->set_origin(*pointf); %}; std::string preamble(); + std::string travel_to(Point* point, ExtrusionRole role, std::string comment) + %code{% RETVAL = THIS->travel_to(*point, role, comment); %}; bool needs_retraction(Polyline* travel, ExtrusionRole role = erNone) %code{% RETVAL = THIS->needs_retraction(*travel, role); %}; std::string retract(bool toolchange = false);