From 3eb2d3ca35aec0754c86b90e3e6883770b18fd57 Mon Sep 17 00:00:00 2001 From: Gazi Faysal Jubayer Date: Wed, 4 Nov 2026 17:27:10 +0600 Subject: [PATCH] Travel back from a lift at the lifted height, then descend (#16933) * Travel back from a lift at the lifted height, then descend After a tool change on the Bambu Lab prime tower, the nozzle is lifted at the tower and then travels to the next object. That travel is forced to set the layer Z, which sent it down the final branch of travel_to_xyz(): it cancelled the lift and emitted one combined XYZ move, so the nozzle sank over the print and left color streaks. When the nozzle is lifted and the target is lower, move in XY first and descend at the target, as the branch already does after a filament change. Fixes #14968 Co-Authored-By: Claude Opus 5.5 * Keep a lifted nozzle up over every travel that descends to the print A multi-point avoid-crossing or exclusion-volume travel now stays at the lifted height and descends only at its last point. The split descent also covers printers with an axis remap, takes its Z move from the target position, and is skipped when X and Y do not change. The test parses with GCodeReader and runs with arc fitting both off and on. Co-Authored-By: Claude Opus 5.5 * Check exclusion volumes from the height a lifted nozzle travels at Co-Authored-By: Claude Opus 5.5 * Merge the lifted and multi-point travel branches into one loop The 2-point travel, the lifted detour and the avoid-crossing detour now share one loop; a detour only moves to the layer height at its first point when the nozzle is not staying lifted. A new test lifts at each layer change of a concave part with avoid crossing walls on, so lifted detours of more than two points are checked for a nozzle that comes down before the end. Co-Authored-By: Claude Opus 5.5 * Keep an exclusion-volume detour lifted when its lift is still pending A retraction leaves its lift pending until the first leg of the travel. The interpolated-Z detour only checked for a lift already made, so it lifted on the first leg and then lowered at each later waypoint. A lift the first leg will make now skips the interpolation, so the detour stays lifted and descends at its last point. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: Kris Austin (cherry picked from commit 99fada6aab6df08e022382f29e6fb692e306fb5a) --- src/libslic3r/GCode.cpp | 48 ++++---- src/libslic3r/GCodeWriter.cpp | 25 ++-- tests/fff_print/test_gcodewriter.cpp | 164 +++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 35 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 7e4f5b94ed..66dfa88f37 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -8051,7 +8051,7 @@ LayerResult GCode::process_layer( m_nominal_z = entry.sub_z; // Use the same lazy-Z mechanism as change_layer(): set the flag so travel_to // fires even when m_last_pos coincides with the first extrusion point, - // ensuring Z reaches sub_z via the combined XY+Z move. + // ensuring Z reaches sub_z on the travel to it. m_need_change_layer_lift_z = true; for (ObjectByExtruder::Island &island : instance_to_print.object_by_extruder.islands) { @@ -10483,9 +10483,6 @@ std::string GCode::travel_to(const Point& point, ExtrusionRole role, std::string result.append(to_local_point(gcode_point)); return result; }; - const auto nominal_writer_z = [this]() { - return m_writer.get_position().z() - m_writer.get_zhop(); - }; const auto route_around_exclusion_volumes = [&]( Polyline &path, const std::optional &start_z_override, @@ -10494,7 +10491,8 @@ std::string GCode::travel_to(const Point& point, ExtrusionRole role, std::string return false; const double target_z = z == DBL_MAX ? m_nominal_z : z; - double route_start_z = start_z_override.value_or(nominal_writer_z()); + // A lifted nozzle travels at its lifted height, so the route is checked from there. + double route_start_z = start_z_override.value_or(m_writer.get_position().z()); double route_end_z = target_z; if (include_planned_lift) { const Vec2d destination_xy = this->point_to_gcode(path.back()); @@ -10643,13 +10641,15 @@ std::string GCode::travel_to(const Point& point, ExtrusionRole role, std::string gcode += m_writer.travel_to_xy(this->point_to_gcode(travel.points[i]), comment); } } else { - if (travel.size() == 2) { - // No extra movements emitted by avoid_crossing_perimeters, simply move to the end point with z change - const auto& dest2d = this->point_to_gcode(travel.points.back()); - Vec3d dest3d(dest2d(0), dest2d(1), z == DBL_MAX ? m_nominal_z : z); - gcode += m_writer.travel_to_xyz(dest3d, comment, m_need_change_layer_lift_z); - m_need_change_layer_lift_z = false; - } else if (exclusion_volume_travel_rerouted && z != DBL_MAX) { + const double target_z = z == DBL_MAX ? m_nominal_z : z; + // A lifted nozzle keeps its height over the whole detour and descends only at the last + // point, so it never sinks over the print. + const bool stay_lifted = m_writer.get_zhop() > EPSILON && m_writer.get_position().z() > target_z + EPSILON; + // A lift left pending by the retraction happens on the first leg, so that detour is lifted too. + const Vec2d first2d = this->point_to_gcode(travel.points[1]); + const bool lift_pending = + m_writer.planned_travel_z(Vec3d(first2d.x(), first2d.y(), target_z), m_need_change_layer_lift_z).second > target_z + EPSILON; + if (exclusion_volume_travel_rerouted && z != DBL_MAX && !stay_lifted && !lift_pending) { // Preserve the original sloped-Z move over the longer detour. // The router activates every volume touched by the complete Z // range, so this interpolation remains conservative. @@ -10668,23 +10668,19 @@ std::string GCode::travel_to(const Point& point, ExtrusionRole role, std::string m_need_change_layer_lift_z = false; } } else { - // Extra movements emitted by avoid_crossing_perimeters, lift the z to normal height at the beginning, then apply the z - // ratio at the last point + // Extra movements emitted by avoid_crossing_perimeters move to the layer height at the first point, + // unless the nozzle stays lifted. The target Z is applied at the last point. + const bool layer_z_first = travel.size() > 2 && !stay_lifted; for (size_t i = 1; i < travel.size(); ++i) { - if (i == 1) { - // Lift to normal z at beginning - Vec2d dest2d = this->point_to_gcode(travel.points[i]); - Vec3d dest3d(dest2d(0), dest2d(1), m_nominal_z); - gcode += m_writer.travel_to_xyz(dest3d, comment, m_need_change_layer_lift_z); + const Vec2d dest2d = this->point_to_gcode(travel.points[i]); + if (i == 1 && layer_z_first) { + gcode += m_writer.travel_to_xyz(Vec3d(dest2d.x(), dest2d.y(), m_nominal_z), comment, m_need_change_layer_lift_z); + m_need_change_layer_lift_z = false; + } else if (i == travel.size() - 1 && (z != DBL_MAX || !layer_z_first)) { + gcode += m_writer.travel_to_xyz(Vec3d(dest2d.x(), dest2d.y(), target_z), comment, m_need_change_layer_lift_z); m_need_change_layer_lift_z = false; - } else if (z != DBL_MAX && i == travel.size() - 1) { - // Apply z_ratio for the very last point - Vec2d dest2d = this->point_to_gcode(travel.points[i]); - Vec3d dest3d(dest2d(0), dest2d(1), z); - gcode += m_writer.travel_to_xyz(dest3d, comment); } else { - // For all points in between, no z change - gcode += m_writer.travel_to_xy(this->point_to_gcode(travel.points[i]), comment); + gcode += m_writer.travel_to_xy(dest2d, comment); } } } diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index 761e89293d..20c2e45691 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -1104,28 +1104,35 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co this->set_current_position_clear(true); return this->travel_to_xy(to_2d(point)); } - else { - /* In all the other cases, we perform an actual XYZ move and cancel - the lift. */ - m_lifted = 0; - } + // A lifted nozzle that has to end lower travels at the lifted height and descends at the + // target, so it never sinks while crossing the print. + const bool descend_after_xy = m_lifted > EPSILON && point(2) < m_pos(2) - EPSILON && this->is_current_position_clear() && + (to_2d(point) - to_2d(m_pos)).norm() > EPSILON; + /* In all the other cases, we move to the target Z and cancel the lift: in a single XYZ move, + or XY first and then Z after a filament change or when coming down from a lift. */ + m_lifted = 0; //BBS: take plate offset into consider Vec3d point_on_plate = { dest_point(0) - m_x_offset, dest_point(1) - m_y_offset, dest_point(2) }; std::string out_string; GCodeG1Formatter w(config.high_precision_gcode.value); - if (has_axis_remap()) { + if (has_axis_remap() && !descend_after_xy) { // Remap may couple XY with Z; emit full XYZ in machine coordinates. w.emit_xyz(apply_axis_remap(point_on_plate)); w.emit_f(final_travel_speed * 60.0); w.emit_comment(GCodeWriter::full_gcode_comment, comment); out_string = w.string(); - } else if (!this->is_current_position_clear()) + } else if (!this->is_current_position_clear() || descend_after_xy) { - //force to move xy first then z after filament change - w.emit_xy(Vec2d(point_on_plate.x(), point_on_plate.y())); + //force to move xy first then z after filament change, or when coming down from a lift + if (has_axis_remap()) + w.emit_xyz(apply_axis_remap(Vec3d(point_on_plate.x(), point_on_plate.y(), m_pos(2)))); + else + w.emit_xy(Vec2d(point_on_plate.x(), point_on_plate.y())); w.emit_f(final_travel_speed * 60.0); w.emit_comment(GCodeWriter::full_gcode_comment, comment); + // The Z move happens at the target, so it is emitted and timed from there. + m_pos = Vec3d(dest_point.x(), dest_point.y(), m_pos(2)); out_string = w.string() + _travel_to_z(point_on_plate.z(), comment); } else { GCodeG1Formatter w(config.high_precision_gcode.value); diff --git a/tests/fff_print/test_gcodewriter.cpp b/tests/fff_print/test_gcodewriter.cpp index 4183db735f..a42cd2474d 100644 --- a/tests/fff_print/test_gcodewriter.cpp +++ b/tests/fff_print/test_gcodewriter.cpp @@ -7,6 +7,7 @@ #include "libslic3r/PrintConfig.hpp" #include "libslic3r/BoundingBox.hpp" #include "libslic3r/Point.hpp" +#include "libslic3r/TriangleMesh.hpp" #include "libslic3r/Config.hpp" #include "libslic3r/Extruder.hpp" #include "libslic3r/Flow.hpp" @@ -2041,3 +2042,166 @@ TEST_CASE("Initial filament start G-code tolerates a list shorter than the filam REQUIRE_NOTHROW(Slic3r::Test::gcode(print)); } + +// Counts the travels (no E) that move in X or Y while lowering Z: each drags the nozzle down over the print. +static int count_descending_travels(const std::string &gcode) +{ + int descending_travels = 0; + GCodeReader reader; + reader.parse_buffer(gcode, [&descending_travels](GCodeReader &self, const GCodeReader::GCodeLine &line) { + if (line.travel() && line.dist_Z(self) < -EPSILON && (line.dist_X(self) != 0.f || line.dist_Y(self) != 0.f)) { + UNSCOPED_INFO(line.raw()); + ++descending_travels; + } + }); + return descending_travels; +} + +// Counts the travels made after the nozzle came straight down, before it printed or rose again: +// the nozzle dropped from its lift short of the destination and crosses the print low. Moves before +// the first extrusion come down from the start G-code and are not counted. +static int count_travels_after_descent(const std::string &gcode) +{ + int travels = 0; + bool printed = false; + bool descended = false; + GCodeReader reader; + reader.parse_buffer(gcode, [&travels, &printed, &descended](GCodeReader &self, const GCodeReader::GCodeLine &line) { + if (!line.cmd_is("G0") && !line.cmd_is("G1") && !line.cmd_is("G2") && !line.cmd_is("G3")) + return; + const bool xy = line.dist_X(self) != 0.f || line.dist_Y(self) != 0.f; + if (line.has(E) || line.dist_Z(self) > EPSILON) { + printed |= xy && line.extruding(self); + descended = false; + } else if (xy) { + if (descended) { + UNSCOPED_INFO(line.raw()); + ++travels; + } + descended = false; + } else if (line.dist_Z(self) < -EPSILON) { + descended = printed; + } + }); + return travels; +} + +TEST_CASE("A lifted nozzle reaches its target height only after the travel back to the print", "[GCodeWriter]") { + // Two filaments on one nozzle with a prime tower: after each tool change the nozzle is + // lifted at the tower and travels back to the object. + const int lift = GENERATE(int(zhtNormal), int(zhtSlope), int(zhtSpiral), int(zhtAuto)); + const std::string arc_fitting = GENERATE("disabled", "enabled"); + CAPTURE(lift, arc_fitting); + DynamicPrintConfig config = Test::multifilament_config(2, { + {"filament_self_index", "1,2"}, + {"filament_extruder_variant", "Direct Drive Standard;Direct Drive Standard"}, + {"z_hop", "0.4"}, + {"retract_lift_below", "0"}, + {"enable_prime_tower", 1}, + {"wipe_tower_x", "50"}, + {"wipe_tower_y", "50"}, + {"skirt_loops", 0}, + {"brim_type", "no_brim"}, + // A spiral lift is written as G2/G3 when arc fitting is on. + {"enable_arc_fitting", arc_fitting}, + }); + config.option("z_hop_types", true)->values = {lift}; + Print print; + Model model; + print.is_BBL_printer() = true; + const std::vector> overrides{{{"extruder", "1"}}, {{"extruder", "2"}}}; + Test::init_print({Test::cube(20), Test::cube(20)}, print, model, config, &overrides); + + CHECK(count_descending_travels(Test::gcode(print)) == 0); +} + +TEST_CASE("A lifted nozzle keeps its height over a detour around a concave part", "[GCodeWriter]") { + // A Bambu Lab printer lifts at each layer change, and avoiding crossing walls detours the travel + // to the next layer's start around the notch of the L. + const int lift = GENERATE(int(zhtNormal), int(zhtSlope), int(zhtSpiral)); + CAPTURE(lift); + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + {"z_hop", "0.4"}, + {"retract_lift_below", "0"}, + {"retract_when_changing_layer", "1"}, + {"reduce_crossing_wall", 1}, + {"seam_position", "random"}, + {"enable_arc_fitting", "disabled"}, + {"skirt_loops", 0}, + {"brim_type", "no_brim"}, + }); + config.option("z_hop_types", true)->values = {lift}; + Print print; + Model model; + print.is_BBL_printer() = true; + Test::init_print({TestMesh::L}, print, model, config); + + const std::string gcode = Test::gcode(print); + CHECK(count_descending_travels(gcode) == 0); + CHECK(count_travels_after_descent(gcode) == 0); +} + +TEST_CASE("A lifted nozzle keeps its height over an exclusion-volume detour", "[GCodeWriter]") { + // A collision volume stands between two parts, so the travel between them is rerouted around it. + // The retraction before that travel leaves its lift pending, and the scarf seam of the outer wall, + // printed first, gives the travel an explicit Z below the layer. + const int lift = GENERATE(int(zhtNormal), int(zhtSlope), int(zhtSpiral)); + CAPTURE(lift); + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + {"bed_exclude_volumes", "0..10;45x10,55x10,55x90,45x90"}, + {"z_hop", "0.4"}, + {"retract_lift_below", "0"}, + {"seam_slope_type", "all"}, + {"seam_slope_conditional", 0}, + {"wall_sequence", "outer wall/inner wall"}, + {"enable_arc_fitting", "disabled"}, + {"skirt_loops", 0}, + {"brim_type", "no_brim"}, + }); + config.option("z_hop_types", true)->values = {lift}; + std::vector meshes; + for (const float x : {20.f, 70.f}) { + TriangleMesh mesh = make_cube(10., 10., 2.); + mesh.translate(x, 45.f, 0.f); + meshes.push_back(std::move(mesh)); + } + Print print; + Model model; + Test::init_print(std::move(meshes), print, model, config, nullptr, false); + + const std::string gcode = Test::gcode(print); + // Short unlifted travels still slope down to a scarf start, so only travels after a descent are checked. + CHECK(count_travels_after_descent(gcode) == 0); +} + +TEST_CASE("A lifted travel that ends lower descends only at its destination", "[GCodeWriter]") { + // Reverse X maps the X and Z of a move together, so the descent re-emits X and Y. + const bool remap = GENERATE(false, true); + // Forced: a layer change. Not forced: a destination below the layer, like a scarf seam start. + const bool force_z = GENERATE(false, true); + CAPTURE(remap, force_z); + GCodeWriter writer; + configure_lift_writer(writer); + if (remap) { + writer.set_axis_remap(6, 1, 2); + writer.set_build_volume_max(Vec3d(250.0, 250.0, 250.0)); + } + // The whole output is parsed, so the reader starts from the position the writer is at. + std::string gcode = writer.travel_to_xyz(Vec3d(20.0, 20.0, 5.0)); + gcode += writer.lazy_lift(LiftType::NormalLift); + gcode += writer.travel_to_xyz(Vec3d(30.0, 30.0, 5.0)); + REQUIRE_THAT(writer.get_position().z(), Catch::Matchers::WithinAbs(5.4, EPSILON)); + + const double target_z = force_z ? 5.0 : 4.9; + gcode += writer.travel_to_xyz(Vec3d(40.0, 40.0, target_z), "", force_z); + INFO(gcode); + CHECK(count_descending_travels(gcode) == 0); + CHECK_THAT(writer.get_position().z(), Catch::Matchers::WithinAbs(target_z, EPSILON)); + + // Already above the destination, the descent is a single move. + writer.lazy_lift(LiftType::NormalLift); + writer.travel_to_xyz(Vec3d(50.0, 50.0, target_z)); + CHECK(count_g1(writer.travel_to_xyz(Vec3d(50.0, 50.0, target_z), "", true)) == 1); +}