From 29e1899571a0c45abfbc1130b978a22ec6f6bbcd Mon Sep 17 00:00:00 2001 From: Roman Romantsov <81668770+alternativniy@users.noreply.github.com> Date: Fri, 30 Oct 2026 02:06:26 +0500 Subject: [PATCH] fix: fix no sparse layers during a toolchange (#15441) Co-authored-by: Claude Opus 5.5 Co-authored-by: Rodrigo Faselli <162915171+RF47@users.noreply.github.com> --- src/libslic3r/GCode.cpp | 23 +++++++ src/libslic3r/GCode/WipeTower.cpp | 2 + src/libslic3r/GCode/WipeTower2.cpp | 2 + tests/fff_print/test_wipe_tower.cpp | 100 ++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 33621f5b0b..78a322e3f6 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1260,6 +1260,17 @@ static std::vector get_path_of_change_filament(const Print& print) end_filament_gcode_str = toolchange_retract_str + object_end_label_temp + end_filament_gcode_str; + // Lift to the layer Z so the toolchange can't hit taller printed parts. Only a custom macro or + // a switch to another physical extruder (whose firmware Tn may dock) can travel. The + // compacted_below_object descent below brings Z back down once over the tower. + const bool tool_may_travel = !change_filament_gcode.empty() + || (gcodegen.writer().filament() != nullptr && gcodegen.writer().filament()->extruder_id() != unsigned(new_extruder_id)); + std::string restore_layer_z_str; + if (compacted_below_object && tool_may_travel && gcodegen.writer().get_position().z() < current_z - EPSILON) { + restore_layer_z_str += gcodegen.writer().travel_to_z(current_z, "Restore layer Z before toolchange"); + check_add_eol(restore_layer_z_str); + } + std::string wipe_next_start_point_str; bool need_travel_after_change_filament_gcode = false; // travel need be after the filament changed to get the correct "m_curr_extruder_id" if (! change_filament_gcode.empty()) { @@ -1614,6 +1625,7 @@ static std::vector get_path_of_change_filament(const Print& print) // Insert the end filament, toolchange, and start filament gcode into the generated gcode. DynamicConfig config; config.set_key_value("filament_end_gcode", new ConfigOptionString(end_filament_gcode_str)); + config.set_key_value("restore_layer_z_before_toolchange", new ConfigOptionString(restore_layer_z_str)); config.set_key_value("change_filament_gcode", new ConfigOptionString(toolchange_gcode_str)); config.set_key_value("filament_start_gcode", new ConfigOptionString(start_filament_gcode_str)); std::string tcr_gcode, tcr_escaped_gcode = gcodegen.placeholder_parser_process("tcr_rotated_gcode", tcr_rotated_gcode, new_filament_id, &config); @@ -1747,6 +1759,7 @@ static std::vector get_path_of_change_filament(const Print& print) std::string toolchange_gcode_str; std::string deretraction_str; + std::string restore_layer_z_str; int toolchange_temp_override = -1; int interface_temp = -1; if (tcr.priming || (new_extruder_id >= 0 && needs_toolchange)) { @@ -1758,6 +1771,15 @@ static std::vector get_path_of_change_filament(const Print& print) interface_temp = gcodegen.config().nozzle_temperature_range_high.get_at(gcodegen.get_filament_config_index(new_extruder_id)); toolchange_temp_override = interface_temp; } + // Lift to the layer Z so the toolchange can't hit taller printed parts. Only a custom macro + // or a switch to another physical extruder (whose firmware Tn may dock) can travel. + const bool tool_may_travel = !gcodegen.config().change_filament_gcode.value.empty() + || (gcodegen.writer().filament() != nullptr + && gcodegen.writer().filament()->extruder_id() != get_extruder_index(*m_print_config, new_extruder_id)); + if (will_go_down && tool_may_travel && gcodegen.config().enable_prime_tower) { + restore_layer_z_str += gcodegen.writer().travel_to_z(current_z, "Restore layer Z before toolchange"); + check_add_eol(restore_layer_z_str); + } toolchange_gcode_str = gcodegen.set_extruder(new_extruder_id, tcr.print_z, false, toolchange_temp_override, WipeTower2::wait_for_temp_enabled(gcodegen.m_config)); // TODO: toolchange_z vs print_z if (!travel_to_tower_now && !tcr.priming && WipeTower2::use_gap_wall(gcodegen.m_config)) { @@ -1915,6 +1937,7 @@ static std::vector get_path_of_change_filament(const Print& print) DynamicConfig config; config.set_key_value("change_filament_gcode", new ConfigOptionString(toolchange_gcode_str)); config.set_key_value("deretraction_from_wipe_tower_generator", new ConfigOptionString(deretraction_str)); + config.set_key_value("restore_layer_z_before_toolchange", new ConfigOptionString(restore_layer_z_str)); config.set_key_value("layer_num", new ConfigOptionInt(gcodegen.m_layer_index)); config.set_key_value("layer_z", new ConfigOptionFloat(tcr.print_z)); config.set_key_value("toolchange_z", new ConfigOptionFloat(z)); diff --git a/src/libslic3r/GCode/WipeTower.cpp b/src/libslic3r/GCode/WipeTower.cpp index 9b29ba0b53..135600d6d7 100644 --- a/src/libslic3r/GCode/WipeTower.cpp +++ b/src/libslic3r/GCode/WipeTower.cpp @@ -2723,6 +2723,8 @@ void WipeTower::toolchange_Change( // This is where we want to place the custom gcodes. We will use placeholders for this. // These will be substituted by the actual gcodes when the gcode is generated. writer.append("[filament_end_gcode]\n"); + // Lift to the layer Z so change_filament_gcode can't hit taller printed parts. + writer.append("[restore_layer_z_before_toolchange]\n"); writer.append("[change_filament_gcode]\n"); // BBS: do travel in GCode::append_tcr() for lazy_lift diff --git a/src/libslic3r/GCode/WipeTower2.cpp b/src/libslic3r/GCode/WipeTower2.cpp index 83729a72a1..5e310e3bdd 100644 --- a/src/libslic3r/GCode/WipeTower2.cpp +++ b/src/libslic3r/GCode/WipeTower2.cpp @@ -1734,6 +1734,8 @@ void WipeTower2::toolchange_Change( // This is where we want to place the custom gcodes. We will use placeholders for this. // These will be substituted by the actual gcodes when the gcode is generated. //writer.append("[end_filament_gcode]\n"); + // Lift to the layer Z so change_filament_gcode can't hit taller printed parts. + writer.append("[restore_layer_z_before_toolchange]\n"); writer.append("[change_filament_gcode]\n"); if (m_is_mk4mmu3) diff --git a/tests/fff_print/test_wipe_tower.cpp b/tests/fff_print/test_wipe_tower.cpp index 20b6ef373c..86d6f2a6dc 100644 --- a/tests/fff_print/test_wipe_tower.cpp +++ b/tests/fff_print/test_wipe_tower.cpp @@ -10,6 +10,7 @@ #include "libslic3r/Print.hpp" #include #include +#include #include #include @@ -23,6 +24,7 @@ #include "libslic3r/BoundingBox.hpp" #include "libslic3r/ClipperUtils.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp" +#include "libslic3r/GCodeReader.hpp" #include "libslic3r/GCode/WipeTower.hpp" #include "libslic3r/LocalesUtils.hpp" #include "libslic3r/PrintConfig.hpp" @@ -573,3 +575,101 @@ TEST_CASE("Only a cone wall gives the tower a stabilization cone", "[WipeTower]" CHECK_FALSE(print.get_conflict_result().has_value()); } } + +TEST_CASE("Without sparse layers the filament change starts at the tallest printed layer", "[WipeTower]") +{ + // Filament 2 on top surfaces only, so the layers between the short cube's top and the tall + // cube's top have no toolchange and the tower skips them: at the tall cube's top it is still + // about 2 mm high. A custom change_filament_gcode may travel anywhere, and so may the firmware + // Tn of a switch to another physical extruder, so neither may start that low. + const std::string tower_type = GENERATE("type1", "type2"); + const bool two_nozzles = GENERATE(false, true); + const bool custom_macro = GENERATE(true, false); + DynamicPrintConfig config = multifilament_config(2, { + { "top_surface_filament_id", 2 }, + { "enable_prime_tower", true }, + { "wipe_tower_type", tower_type }, + { "wipe_tower_no_sparse_layers", true }, + { "wipe_tower_x", 30 }, + { "wipe_tower_y", 30 }, + { "layer_height", 0.2 }, + { "change_filament_gcode", custom_macro ? "; TOOLCHANGE_MARK\n" : "" }, + { "gcode_comments", true }, + { "timelapse_type", "0" }, + { "enable_wrapping_detection", false }, + { "raft_layers", "0" } }); + if (two_nozzles) { + config.set_num_extruders(2); + config.set_deserialize_strict({ + { "single_extruder_multi_material", false }, + { "nozzle_diameter", "0.4,0.4" }, + { "printer_extruder_id", "1,2" }, + { "printer_extruder_variant", "Direct Drive Standard,Direct Drive Standard" }, + { "extruder_printable_height", "0,0" }, + { "filament_map_mode", "Manual" }, + { "filament_map", "1,2" } }); + size_flush_to_nozzles(config); + } + + const double short_height = 2., tall_height = 10.; + TriangleMesh tall = make_cube(10., 10., tall_height); + tall.translate(120.f, 120.f, 0.f); + TriangleMesh low = make_cube(10., 10., short_height); + low.translate(150.f, 120.f, 0.f); + + Print print; + Model model; + init_print({ std::move(tall), std::move(low) }, print, model, config, nullptr, false); + print.apply(model, config); + const std::string gcode = Slic3r::Test::gcode(print); + + // Z where each change_filament_gcode or Tn starts, next to the highest Z of the objects + // extruded before it; and the Z of each tower extrusion once the objects pass the short cube. + std::vector> changes; + std::vector> tower_extrusions; + double object_z = 0.; + bool in_tower = false; + size_t lifts = 0; + GCodeReader parser; + parser.parse_buffer(gcode, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + const std::string_view raw = line.raw(); + // Objects are tagged with the processor's ;TYPE:, the tower with its own ; FEATURE:. + if (raw.rfind(";TYPE:", 0) == 0 || raw.rfind("; FEATURE: ", 0) == 0) + in_tower = raw.find("Prime tower") != std::string_view::npos; + else if (line.extruding(self) && (line.has_x() || line.has_y())) { // not an unretract + if (!in_tower) + object_z = std::max(object_z, double(self.z())); + // Above the tall cube is the final unload, printed over the finished parts on purpose. + else if (object_z > short_height + EPSILON && self.z() <= tall_height + EPSILON) + tower_extrusions.emplace_back(self.z(), object_z); + } else if (raw.find("; TOOLCHANGE_MARK") != std::string_view::npos + || (raw.size() > 1 && raw[0] == 'T' && raw[1] >= '0' && raw[1] <= '9')) + changes.emplace_back(self.z(), object_z); + if (raw.find("Restore layer Z before toolchange") != std::string_view::npos) + ++lifts; + }); + INFO(tower_type << (two_nozzles ? ", two nozzles" : ", one nozzle") << (custom_macro ? ", custom macro" : ", empty macro")); + + if (!custom_macro && !two_nozzles) { + // Nothing travels during the change, so the toolhead stays down at the tower. + CHECK(lifts == 0); + } else { + size_t above_short = 0; + for (const auto &[change_z, printed_z] : changes) { + if (printed_z <= short_height + EPSILON) + continue; + ++above_short; + INFO("toolchange at Z " << change_z << ", printed up to Z " << printed_z); + CHECK(change_z >= printed_z - EPSILON); + } + // The tall cube's top surface needs at least one change above the short cube. + CHECK(above_short > 0); + } + + // Every lift has to come back down: the tower keeps printing at its compacted height. + REQUIRE_FALSE(tower_extrusions.empty()); + for (const auto &[tower_z, printed_z] : tower_extrusions) { + INFO("tower extrusion at Z " << tower_z << ", printed up to Z " << printed_z); + CHECK(tower_z < printed_z - EPSILON); + } +}