From fbb75de58688b9f4129110d0aca002eb01f68907 Mon Sep 17 00:00:00 2001 From: Ian Bassi Date: Wed, 18 Nov 2026 21:52:07 -0300 Subject: [PATCH] Retract for a toolchange on Hilbert curve surfaces The rule that skips the retraction on Hilbert curve surfaces also skipped the retraction for a toolchange, which GCode::set_extruder() asks for without a role. On a first layer with Hilbert curve bottom surfaces and no wipe, a toolchange then happened without any retraction. Keep the skip for travels only. --- src/libslic3r/GCode.cpp | 4 ++-- tests/fff_print/test_wipe.cpp | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 449e2cd617..a535f253cd 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -10983,8 +10983,8 @@ std::string GCode::retract(bool toolchange, bool is_last_retraction, LiftType li (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. */ - // Hilbert curve surfaces skip the retraction, but not the rest of one a wipe has started. - if (wiped || + // Hilbert curve surfaces skip the retraction of a travel, but not a toolchange's, nor the rest of one a wipe has started. + if (wiped || toolchange || ((!this->on_first_layer() || this->config().bottom_surface_pattern != InfillPattern::ipHilbertCurve) && (role != erTopSolidInfill || this->config().top_surface_pattern != InfillPattern::ipHilbertCurve))) gcode += toolchange ? m_writer.retract_for_toolchange() : m_writer.retract(); diff --git a/tests/fff_print/test_wipe.cpp b/tests/fff_print/test_wipe.cpp index fe5cb43d74..09650ca579 100644 --- a/tests/fff_print/test_wipe.cpp +++ b/tests/fff_print/test_wipe.cpp @@ -844,3 +844,41 @@ TEST_CASE("A wipe retracts the full length on a first layer with Hilbert curve b REQUIRE(first_layer_wipes > 0); REQUIRE(wipes > first_layer_wipes); } + +TEST_CASE("A toolchange retracts on a first layer with Hilbert curve bottom surfaces", "[Wipe][Regression]") +{ + // Without a wipe or a prime tower, GCode::set_extruder() retracts for the toolchange. + DynamicPrintConfig config = multifilament_config(2, { + { "gcode_flavor", "marlin2" }, + { "use_relative_e_distances", 1 }, + { "bottom_surface_pattern", "hilbertcurve" }, + { "wipe", 0 }, + { "retraction_length", 0.8 }, + { "retract_length_toolchange", 5 }, + { "enable_prime_tower", 0 }, + { "skirt_loops", 0 }, + { "brim_type", "no_brim" }, + }); + // The second cube prints with the second filament, so the first layer changes filaments. + const std::string gcode = slice_with_object_overrides({ make_cube(10., 10., 1.), make_cube(10., 10., 1.) }, config, + { {}, { { "extruder", "2" } } }); + double retracted = 0.; + bool extruded = false; + bool changed = false; + GCodeReader parser; + parser.apply_config(config); + parser.parse_buffer(gcode, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + const std::string_view cmd = line.cmd(); + if (extruded && cmd.size() > 1 && cmd[0] == 'T') { + changed = true; + self.quit_parsing(); + } else if (line.retracting(self)) { + retracted -= line.dist_E(self); + } else if (line.extruding(self) && line.dist_XY(self) > EPSILON) { + extruded = true; + retracted = 0.; + } + }); + REQUIRE(changed); + CHECK_THAT(retracted, Catch::Matchers::WithinAbs(5., 1e-4)); +}