From 7fbacdfe7aef1e3620d8c4ee37fd59675cc026d7 Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Fri, 27 Nov 2026 11:13:56 +0800 Subject: [PATCH] Stop Reading an Uninitialised Position When Routing Back to the Type 1 Wipe Tower (#17930) After a filament change on a Type 1 wipe tower with "Skip points" on, the travel back to the tower started from `gcode_last_pos`, which was never initialised. An axis the change G-code doesn't move kept whatever was on the stack, so the route could cut across the tower or go the long way round, and the same slice could give different G-code between runs. That axis now starts at the head's last tracked position from before the change G-code. This changes slices on the Bambu Lab A2L, H2C, P2S and X2D and the Qidi Q2 and Q2C, and adds a regression test to `test_wipe_tower.cpp`. --- src/libslic3r/GCode.cpp | 8 ++- tests/fff_print/test_wipe_tower.cpp | 94 +++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 78c4524266..1549ccafa5 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1482,6 +1482,11 @@ static std::vector get_path_of_change_filament(const Print& print) need_travel_after_change_filament_gcode = true; } + // The last position tracked for the head before the change G-code, in the coordinates the G-code is + // written in. A wall the tower prints first is not tracked, so then it is where that wall started. + // Taken while the old filament is still the active one, as its extruder offset is part of it. + const Vec2d position_before_change_gcode = gcodegen.point_to_gcode(gcodegen.last_pos()) - plate_origin_2d.cast(); + std::string toolchange_command; if (tcr.priming || (new_filament_id >= 0 && gcodegen.writer().need_toolchange(new_filament_id))) // Orca: null-safe, layer-aware nozzle lookup — group_result may be null on @@ -1517,7 +1522,8 @@ static std::vector get_path_of_change_filament(const Print& print) wipe_next_start_point_str = start_pos_str; } else { // BBS:change travel_path - Vec3f gcode_last_pos; + // An axis the change G-code does not name in a G0 to G3 move is taken to be where it was. + Vec3f gcode_last_pos(static_cast(position_before_change_gcode.x()), static_cast(position_before_change_gcode.y()), 0.f); GCodeProcessor::get_last_position_from_gcode(toolchange_gcode_str, gcode_last_pos); Vec2f gcode_last_pos2d{gcode_last_pos[0], gcode_last_pos[1]}; Point gcode_last_pos2d_object = gcodegen.gcode_to_point(gcode_last_pos2d.cast() + plate_origin_2d.cast()); diff --git a/tests/fff_print/test_wipe_tower.cpp b/tests/fff_print/test_wipe_tower.cpp index 558091b867..bf566e30f3 100644 --- a/tests/fff_print/test_wipe_tower.cpp +++ b/tests/fff_print/test_wipe_tower.cpp @@ -1379,3 +1379,97 @@ TEST_CASE("Smooth timelapse prints each tower layer's wall once when a layer joi slice_precise_z_pair(config, "type1", 10.004, print); CHECK(count_joined_layers(print) > 0); } + +TEST_CASE("The travel to the Type 1 tower keeps the axis the filament change G-code does not move", "[WipeTower][Regression]") +{ + // The change G-code moves Y only, as some printers' does, so afterwards the head is still at the X + // it had before. Both cubes sit behind the tower, within its width, so the head is always above the + // tower then, and the travel to the wipe start on the tower's left or right side has to go round a + // corner of the box kept clear around the tower. + const std::string change_gcode = "G1 Y190 F18000"; + const double change_y = 190.; + DynamicPrintConfig config = multifilament_config(2, { + { "enable_prime_tower", true }, + { "wipe_tower_type", "type1" }, + { "wipe_tower_wall_type", "rectangle" }, + { "prime_tower_width", 40 }, + { "prime_tower_brim_width", 3 }, + { "prime_volume", 45 }, + { "prime_tower_skip_points", true }, + { "wipe_tower_x", 80 }, + { "wipe_tower_y", 60 }, + { "layer_height", 0.2 }, + { "change_filament_gcode", change_gcode + "\n" }, + { "enable_arc_fitting", false }, + { "timelapse_type", "0" }, + { "enable_wrapping_detection", false }, + { "raft_layers", "0" } }); + const std::vector> filaments = { { { "extruder", "1" } }, { { "extruder", "2" } } }; + + TriangleMesh near_cube = make_cube(10., 10., 1.); + near_cube.translate(87.f, 105.f, 0.f); + TriangleMesh far_cube = make_cube(10., 10., 1.); + far_cube.translate(87.f, 125.f, 0.f); + + Print print; + Model model; + init_print({ std::move(near_cube), std::move(far_cube) }, print, model, config, &filaments, false); + print.apply(model, config); + const std::string gcode = Slic3r::Test::gcode(print); + + // For each filament change on the tower: where the head is after the change G-code, and the + // travel moves from there to the tower's first extrusion. + struct Change + { + Vec2d head; + std::vector route; + }; + std::vector changes; + Vec2d position = Vec2d::Zero(); + bool in_route = false; + GCodeReader parser; + parser.parse_buffer(gcode, [&](GCodeReader &, const GCodeReader::GCodeLine &line) { + const std::string_view raw = line.raw(); + if (raw == change_gcode) { + position.y() = change_y; + changes.push_back({ position, {} }); + in_route = true; + } else if (raw.rfind("; CP_TOOLCHANGE", 0) == 0) { + in_route = false; + } else if (raw.rfind(";LAYER_CHANGE", 0) == 0 || raw.rfind(";TYPE:", 0) == 0) { + // Not a change on the tower: the first filament is loaded before anything is printed. + if (in_route) + changes.pop_back(); + in_route = false; + } else if ((line.cmd_is("G0") || line.cmd_is("G1")) && (line.has_x() || line.has_y())) { + if (line.has_x()) + position.x() = line.x(); + if (line.has_y()) + position.y() = line.y(); + if (in_route) + changes.back().route.push_back(position); + } + }); + REQUIRE(changes.size() > 1); + + for (const Change &change : changes) { + CAPTURE(change.head.x(), change.route.size()); + REQUIRE(change.route.size() >= 2); + // The route ends with a move along X into the tower. That move starts on the side of the box. + size_t on_side = change.route.size() - 1; + while (on_side > 0 && std::abs(change.route[on_side - 1].y() - change.route.back().y()) < EPSILON) + --on_side; + const Vec2d &first = change.route.front(); + const Vec2d &on_box = change.route[on_side]; + CAPTURE(first.x(), first.y(), on_box.x(), on_box.y()); + // From above the tower the side of the box is out of sight, so the route goes round a corner. + REQUIRE(on_side >= 2); + // Its first move heads straight for that point on the side, and stops where it meets the box. + // The route starts from the last position tracked for the head, which can be a few millimetres + // from the head when the tower printed its wall first. That moves the first point by under 1 mm; + // a start taken from anywhere else moves it by far more. + const Vec2d to_side = on_box - change.head; + const double off_line = std::abs(cross2(to_side, Vec2d(first - change.head))) / to_side.norm(); + CHECK(off_line < 1.); + } +}