From 135f383eb22b4c88e730bdf36772789fb7d5a91c Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Wed, 4 Nov 2026 13:14:01 +0800 Subject: [PATCH] Join Precise-Z Sliver Layers to the Prime Tower Layer Below (#17204) * Join Precise-Z Sliver Layers to the Prime Tower Layer Below Precise Z height ends each object at its exact top, so another object's layers can print a few microns above these. Each sliver became its own wipe tower layer, and a tool change on it was sized for that height: a 0.0009 mm layer asked for a 230 mm ramming band, which grew the tower past the bed ("Prime tower is partially outside the printable area"). Once sized correctly, G-code export lost step with the tower and threw "append_tcr was asked to do a toolchange it didn't expect". A tower layer thinner than half the printer's minimum layer height now joins the tower layer below: ToolOrdering records the tower layer each layer prints on and whether it finishes it, the tower plans the joined tool changes on that layer's z and height, and the G-code neither advances the tower on a joined layer nor finishes the layer before it. * Finish a Joined Prime Tower Layer on Its Last Tool Change The joined layer always finished the tower layer, so a joined layer that changed no tools asked for a result after the layer below had taken them all and threw "Wipe tower generation failed". The layer that finishes is now the last of the group that changes tools, or the last one when none does, matching how a single layer merges its finish into a tool change. A joined layer also reset the Type 1 tower's first print, so smooth timelapse replayed the tower layer's wall and threw the same error. Layers are now joined below the printer's minimum layer height instead of half of it: precise Z height can move a layer off by up to a layer height, and the 0.04-0.08 mm layers left on their own still grew the tower off the bed. * Print Joined Prime Tower Purges at the Tower Layer's Z A joined layer printed its purges at its own nozzle height, up to just under the minimum layer height above the tower layer, so the beads were not pressed onto it. The emitter now descends to the tower layer's z for a joined layer, travelling over the tower first and climbing back after, the way it already does for a tower compacted below the objects. Layers sliced at exactly the minimum layer height sum to gaps a hair under it and were joined to the layer below; an EPSILON margin keeps them apart. * Travel Over the Prime Tower Before a Joined Layer Descends With no change_filament_gcode and no nozzle change, the Type 1 emitter descended to the joined tower layer's z while the head was still over the part, then travelled to the tower at that z. It now travels over the tower first. The tests check that every descent below the object layer happens over the tower. (cherry picked from commit 1c9f6f931811feccdabc091dd157256259777840) --- docs/HLSD/prime-tower-sparse-layers.md | 39 +++++- src/libslic3r/GCode.cpp | 37 ++++-- src/libslic3r/GCode.hpp | 7 +- src/libslic3r/GCode/ToolOrdering.cpp | 53 +++++++- src/libslic3r/GCode/ToolOrdering.hpp | 7 ++ src/libslic3r/Print.cpp | 8 +- tests/fff_print/test_wipe_tower.cpp | 163 +++++++++++++++++++++++++ 7 files changed, 290 insertions(+), 24 deletions(-) diff --git a/docs/HLSD/prime-tower-sparse-layers.md b/docs/HLSD/prime-tower-sparse-layers.md index 19802c9169..47a0ff4163 100644 --- a/docs/HLSD/prime-tower-sparse-layers.md +++ b/docs/HLSD/prime-tower-sparse-layers.md @@ -29,7 +29,32 @@ the tower is continuous from the bed to its last purge. `wipe_tower_layer_height is the distance from the previous tower layer, which is the object's layer height whenever the tower prints on every layer. -`Print::_make_wipe_tower` plans one tower layer per such object layer. A layer +A tower layer thinner than the printer's minimum layer height (by more than +`EPSILON`, so layers sliced at exactly the minimum stay apart; never less than the +towers' own 1e-3 mm merge) is not printed on its own: it joins the tower layer +below. Precise Z height leaves such layers when it ends each object at its exact +top, moving one object's last layers off another's by anything up to a layer +height; a purge sized for a layer a few microns thick would run hundreds of mm. +A joined layer takes `wipe_tower_print_z` and `wipe_tower_layer_height` from the +tower layer below, and its purges print down at that tower layer's z: the emitter +travels over the tower first, descends, and climbs back to the object layer, the +same sequence it uses for a tower compacted below the objects. After a custom +`change_filament_gcode` the descent happens wherever the macro leaves the head, so +a macro has to end over the tower, as it already must for the compacted tower. +Layers from the minimum layer height up stay tower layers of their own, sized for +their height. + +The emitter takes one tower result per toolchange, and a layer without one takes +a result only to finish the tower layer. So `wipe_tower_layer_ends` marks the last +of the tower layer and its joined layers that changes tools, or the last of them +when none does: a joined layer that changes nothing after the results are all +taken has nothing left to finish with. A joined layer also does not reset the +Type 1 tower's first print, so smooth timelapse prints each tower layer's wall +once. + +`Print::_make_wipe_tower` plans every such object layer at its +`wipe_tower_print_z`: one tower layer per object layer, except that a joined +layer's toolchanges land in the tower layer below. A layer whose only call keeps the current filament leaves no toolchange in the plan, and the layer it generates is a single result whose initial and new tool are equal. That is what `wipe_tower_layer_is_sparse` recognises, and it is the unit both @@ -37,8 +62,8 @@ settings work on. The plan stays one entry per tower layer in every case. The G-code emitter walks `WipeTowerData::tool_changes` by layer index, advancing once per object layer -that carries a tower, so a planner that removed entries would silently shift -every later layer onto the wrong tower geometry. Layers that print nothing are +that carries a tower and does not join the one below, so a planner that removed +entries would silently shift every later layer onto the wrong tower geometry. Layers that print nothing are therefore still planned and still generated; they are marked, and the emitter drops them. @@ -193,5 +218,9 @@ read as off rather than compacting or merging in one place and not another. and the clearance geometry the plater draws. - [Slicing tests](../../tests/fff_print/test_wipe_tower.cpp) slice a real print and check that a run folds, that the tower still covers the object exactly - once, that a run too thin for the cap is left alone, and that a merged layer - declares its height in the tag the printer's processor reads. + once, that a run too thin for the cap is left alone, that a merged layer + declares its height in the tag the printer's processor reads, and that layers + precise Z height moves less than a minimum layer height off another object's + join the tower layer below and print at its z, with and without their own + toolchange and with smooth timelapse, while layers sliced at exactly the + minimum height do not join. diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 66dfa88f37..e11602a259 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1166,7 +1166,9 @@ static std::vector get_path_of_change_filament(const Print& print) // over the tower - which is what the is_finish_first travel above does. Otherwise the nozzle // is still over the model and this descent would drive it into the print, so defer it to the // re-descents below, which run after the travel to the tower. - const bool defer_compacted_descend = m_sparse_layers_skipped + // A layer joined to the tower layer below descends to it the same way, by less than a layer. + const bool joined_below_layer = m_layer_joined && !tcr.priming && (current_z - z) > EPSILON; + const bool defer_compacted_descend = (m_sparse_layers_skipped || joined_below_layer) && !tcr.priming && !tcr.is_finish_first && (current_z - z) > EPSILON; if (!is_approx(z, current_z) && !defer_compacted_descend) { gcode += gcodegen.writer().retract(); @@ -1175,7 +1177,7 @@ static std::vector get_path_of_change_filament(const Print& print) } // Tower compacted below the object, so any extrusion emitted without an explicit z has to be // pulled back down to it first. - const bool compacted_below_object = m_sparse_layers_skipped && z >= 0. && (tcr.print_z - z) > EPSILON; + const bool compacted_below_object = (m_sparse_layers_skipped && z >= 0. && (tcr.print_z - z) > EPSILON) || joined_below_layer; // Process the end filament gcode. bool add_change_filament_624 = false; @@ -1510,6 +1512,14 @@ static std::vector get_path_of_change_filament(const Print& print) if (position_before_toolchange) gcodegen.synchronize_toolchange_position(*position_before_toolchange, toolchange_gcode_str); + // A joined layer descends to the tower layer below it. With no macro and no nozzle change + // nothing has moved the head off the part yet, so travel over the tower before that descent. + if (joined_below_layer && !need_travel_after_change_filament_gcode && !is_nozzle_change) { + std::string start_pos_str = gcodegen.travel_to(wipe_tower_point_to_object_point(gcodegen, tool_change_start_pos + plate_origin_2d), erMixed, "Move to start pos"); + check_add_eol(start_pos_str); + wipe_next_start_point_str = start_pos_str; + } + if (need_travel_after_change_filament_gcode) { // move to start_pos for wiping after toolchange if (!is_used_travel_avoid_perimeter) { @@ -2102,7 +2112,8 @@ static std::vector get_path_of_change_filament(const Print& print) ignore_sparse = wipe_tower_layer_is_sparse(m_tool_changes[m_layer_idx]) && m_layer_idx != 0; if (m_tool_change_idx == 0 && !ignore_sparse) wipe_tower_z = m_last_wipe_tower_print_z + m_tool_changes[m_layer_idx].front().layer_height; - } + } else if (m_layer_joined) + wipe_tower_z = joined_tower_z(); if (!ignore_sparse) { gcode += append_tcr2(gcodegen, m_tool_changes[m_layer_idx][m_tool_change_idx++], extruder_id, wipe_tower_z); @@ -2119,7 +2130,8 @@ static std::vector get_path_of_change_filament(const Print& print) if (m_sparse_layers_skipped) { ignore_sparse = wipe_tower_layer_is_sparse(m_tool_changes[m_layer_idx]); wipe_tower_z = m_compacted_tower_z[m_layer_idx]; - } + } else if (m_layer_joined) + wipe_tower_z = joined_tower_z(); if ((m_enable_timelapse_print || m_enable_wrapping_detection) && m_is_first_print) { gcode += append_tcr(gcodegen, m_tool_changes[m_layer_idx][0], m_tool_changes[m_layer_idx][0].new_tool, wipe_tower_z); @@ -5175,8 +5187,13 @@ void GCode::process_layers( const std::pair>& layer = layers_to_print[precomputed.index]; const LayerTools& layer_tools = tool_ordering.tools_for_layer(layer.first); print.set_status(80, Slic3r::format(_(L("Generating G-code: layer %1%")), std::to_string(precomputed.index + 1))); - if (m_wipe_tower && layer_tools.has_wipe_tower) - m_wipe_tower->next_layer(); + // A layer that joined the tower layer below continues it. + if (m_wipe_tower && layer_tools.has_wipe_tower) { + if (layer_tools.joins_wipe_tower_layer_below()) + m_wipe_tower->join_layer(); + else + m_wipe_tower->next_layer(); + } //BBS check_placeholder_parser_failed(); print.throw_if_canceled(); @@ -7419,7 +7436,8 @@ LayerResult GCode::process_layer( gcode += insert_timelapse_gcode(); } - if (m_wipe_tower) + // A joined layer continues a tower layer whose first print is already done. + if (m_wipe_tower && ! layer_tools.joins_wipe_tower_layer_below()) m_wipe_tower->set_is_first_print(true); auto insert_wrapping_detection_gcode = [this, &print, &print_z, &most_used_extruder]() -> std::string { @@ -7508,7 +7526,8 @@ LayerResult GCode::process_layer( std::string gcode_toolchange; if (has_wipe_tower) { - if (!m_wipe_tower->is_empty_wipe_tower_gcode(*this, extruder_id, extruder_id == layer_tools.extruders.back())) { + const bool finish_tower_layer = extruder_id == layer_tools.extruders.back() && layer_tools.wipe_tower_layer_ends; + if (!m_wipe_tower->is_empty_wipe_tower_gcode(*this, extruder_id, finish_tower_layer)) { if (need_insert_timelapse_gcode_for_traditional && !has_insert_timelapse_gcode && !(m_farthest_point_timelapse.enabled && m_farthest_point_timelapse.farthest_is_photo_head)) { bool should_insert = true; @@ -7536,7 +7555,7 @@ LayerResult GCode::process_layer( gcode += insert_wrapping_detection_gcode(); has_insert_wrapping_detection_gcode = true; } - gcode_toolchange = m_wipe_tower->tool_change(*this, extruder_id, extruder_id == layer_tools.extruders.back()); + gcode_toolchange = m_wipe_tower->tool_change(*this, extruder_id, finish_tower_layer); } } else { // Same case-A/case-B split for the non-wipe-tower path. With the subsystem off, diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 03c5325953..9d9f12877b 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -142,7 +142,9 @@ public: } std::string prime(GCode &gcodegen); - void next_layer() { ++ m_layer_idx; m_tool_change_idx = 0; } + void next_layer() { ++ m_layer_idx; m_tool_change_idx = 0; m_layer_joined = false; } + // The next object layer joined the current tower layer, so its purges print down at that layer's z. + void join_layer() { m_layer_joined = true; } std::string tool_change(GCode &gcodegen, int extruder_id, bool finish_layer); bool is_empty_wipe_tower_gcode(GCode &gcodegen, int extruder_id, bool finish_layer); std::string finalize(GCode &gcodegen); @@ -164,6 +166,8 @@ private: std::string append_tcr2(GCode &gcodegen, const WipeTower::ToolChangeResult &tcr, int new_extruder_id, double z = -1.) const; std::string travel_to_tower_gap(GCode &gcodegen, const Point &route_start, const Point &start_wipe_pos) const; Vec2f transform_wt2_pt(const Vec2f &pt) const; + // The current tower layer's z in the G-code frame, where a joined layer prints its purges. + double joined_tower_z() const { return m_tool_changes[m_layer_idx].front().print_z + m_print_config->z_offset.value; } Polygons shared_printable_area(GCode &gcodegen) const; // Postprocesses gcode: rotates and moves G1 extrusions and returns result @@ -183,6 +187,7 @@ private: int m_layer_idx; int m_tool_change_idx; double m_last_wipe_tower_print_z; + bool m_layer_joined = false; // BBS Vec3d m_plate_origin; diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index 75fcace426..e5c36d34c7 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -192,6 +192,14 @@ static double calc_max_layer_height(const PrintConfig &config, double max_object return std::max(max_layer_height, max_object_layer_height); } +static double calc_min_layer_height(const PrintConfig &config) +{ + double min_layer_height = std::numeric_limits::max(); + for (size_t i = 0; i < config.nozzle_diameter.values.size(); ++ i) + min_layer_height = std::min(min_layer_height, config.min_layer_height.get_at(i)); + return config.nozzle_diameter.values.empty() ? 0. : min_layer_height; +} + //calculate the flush weight (first value) and filament change count(second value) // Nozzle-aware flush-stat calculator. Resolves each // filament in the print sequence to its physical nozzle via the grouping result and tracks a @@ -1233,13 +1241,48 @@ void ToolOrdering::fill_wipe_tower_partitions(const PrintConfig &config, coordf_ } } - // Calculate the wipe_tower_layer_height values. - coordf_t wipe_tower_print_z_last = 0.; - for (LayerTools < : m_layer_tools) - if (lt.has_wipe_tower) { + // Calculate the wipe_tower_layer_height values. Precise Z height ends each object at its exact + // top, so another object's layers can sit above these by less than the printer's minimum layer + // height. A tower layer that thin cannot be printed, and a purge sized for it runs up to hundreds + // of mm long, so it joins the tower layer below. The floor matches the towers' own WT_EPSILON + // merge, which would otherwise size the joined tool changes with the sliver's height. The EPSILON + // margin keeps layers sliced at exactly the minimum height, whose sums round below it, apart. + const double min_tower_layer_height = std::max(calc_min_layer_height(config), 1e-3); + coordf_t wipe_tower_print_z_last = 0.; + // The layer that finishes the current tower layer, and whether any layer of it changes tools. + LayerTools *tower_layer_end = nullptr; + bool tower_layer_changes = false; + unsigned int current_extruder = (unsigned int) -1; + for (LayerTools < : m_layer_tools) { + lt.wipe_tower_layer_ends = true; + // The G-code takes one tower result per tool change, so a layer that changes tools always + // takes one; a layer that does not takes one only when it finishes the tower layer. + const bool changes = ! lt.extruders.empty() && + (current_extruder == (unsigned int) -1 || lt.extruders.size() > 1 || lt.extruders.front() != current_extruder); + if (! lt.extruders.empty()) + current_extruder = lt.extruders.back(); + if (! lt.has_wipe_tower) + continue; + // Only a layer that prints joins, so the tower layer is always finished by a printing layer. + if (tower_layer_end != nullptr && ! lt.extruders.empty() && lt.print_z - wipe_tower_print_z_last + EPSILON < min_tower_layer_height) { + lt.wipe_tower_print_z = tower_layer_end->wipe_tower_print_z; + lt.wipe_tower_layer_height = tower_layer_end->wipe_tower_layer_height; + // The finish rides on the last tool change of the joined layers, as it does on its own + // layer: one that takes over after the last result is gone has nothing left to print. + if (changes || ! tower_layer_changes) { + tower_layer_end->wipe_tower_layer_ends = false; + tower_layer_end = < + } else + lt.wipe_tower_layer_ends = false; + tower_layer_changes |= changes; + } else { + lt.wipe_tower_print_z = lt.print_z; lt.wipe_tower_layer_height = lt.print_z - wipe_tower_print_z_last; - wipe_tower_print_z_last = lt.print_z; + wipe_tower_print_z_last = lt.print_z; + tower_layer_end = < + tower_layer_changes = changes; } + } } void ToolOrdering::collect_extruder_statistics(bool prime_multi_material) diff --git a/src/libslic3r/GCode/ToolOrdering.hpp b/src/libslic3r/GCode/ToolOrdering.hpp index b7aca7dd30..e38cae4fb9 100644 --- a/src/libslic3r/GCode/ToolOrdering.hpp +++ b/src/libslic3r/GCode/ToolOrdering.hpp @@ -187,6 +187,13 @@ public: // and to support the wipe tower partitions above this one. size_t wipe_tower_partitions = 0; coordf_t wipe_tower_layer_height = 0.; + // print_z of the wipe tower layer this layer's tool changes print on. A layer too thin to be + // its own tower layer joins the one below, keeping that layer's z and height. + coordf_t wipe_tower_print_z = 0.; + // Whether this layer finishes its wipe tower layer. Of a tower layer and the layers joined to + // it, that is the last one changing tools, or the last one when none does. + bool wipe_tower_layer_ends = true; + bool joins_wipe_tower_layer_below() const { return has_wipe_tower && wipe_tower_print_z != print_z; } // Custom G-code (color change, extruder switch, pause) to be performed before this layer starts to print. const CustomGCode::Item *custom_gcode = nullptr; diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index cfc7f5752e..0a3427cc8d 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -5369,7 +5369,7 @@ void Print::_make_wipe_tower() if (!layer_tools.has_wipe_tower) continue; bool first_layer = &layer_tools == &m_wipe_tower_data.tool_ordering.front(); - wipe_tower.plan_toolchange((float)layer_tools.print_z, (float)layer_tools.wipe_tower_layer_height, current_filament_id, current_filament_id); + wipe_tower.plan_toolchange((float)layer_tools.wipe_tower_print_z, (float)layer_tools.wipe_tower_layer_height, current_filament_id, current_filament_id); used_filament_ids.insert(layer_tools.extruders.begin(), layer_tools.extruders.end()); @@ -5420,7 +5420,7 @@ void Print::_make_wipe_tower() wipe_volume_nc = 15.f; } - wipe_tower.plan_toolchange((float)layer_tools.print_z, (float)layer_tools.wipe_tower_layer_height, current_filament_id, filament_id, + wipe_tower.plan_toolchange((float)layer_tools.wipe_tower_print_z, (float)layer_tools.wipe_tower_layer_height, current_filament_id, filament_id, wipe_volume_ec, wipe_volume_nc, volume_to_purge); current_filament_id = filament_id; } @@ -5527,7 +5527,7 @@ void Print::_make_wipe_tower() if (!layer_tools.has_wipe_tower) continue; bool first_layer = &layer_tools == &m_wipe_tower_data.tool_ordering.front(); - wipe_tower.plan_toolchange((float) layer_tools.print_z, (float) layer_tools.wipe_tower_layer_height, current_extruder_id, + wipe_tower.plan_toolchange((float) layer_tools.wipe_tower_print_z, (float) layer_tools.wipe_tower_layer_height, current_extruder_id, current_extruder_id, false); for (const auto extruder_id : layer_tools.extruders) { if ((first_layer && extruder_id == m_wipe_tower_data.tool_ordering.all_extruders().back()) || extruder_id != @@ -5548,7 +5548,7 @@ void Print::_make_wipe_tower() } // request a toolchange at the wipe tower with at least volume_to_wipe purging amount - wipe_tower.plan_toolchange((float) layer_tools.print_z, (float) layer_tools.wipe_tower_layer_height, + wipe_tower.plan_toolchange((float) layer_tools.wipe_tower_print_z, (float) layer_tools.wipe_tower_layer_height, current_extruder_id, extruder_id, volume_to_wipe); current_extruder_id = extruder_id; } diff --git a/tests/fff_print/test_wipe_tower.cpp b/tests/fff_print/test_wipe_tower.cpp index 815eb2d97c..80d00bb483 100644 --- a/tests/fff_print/test_wipe_tower.cpp +++ b/tests/fff_print/test_wipe_tower.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "libslic3r/Config.hpp" @@ -25,6 +26,7 @@ #include "libslic3r/ClipperUtils.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/GCodeReader.hpp" +#include "libslic3r/GCode/ToolOrdering.hpp" #include "libslic3r/GCode/WipeTower.hpp" #include "libslic3r/LocalesUtils.hpp" #include "libslic3r/PrintConfig.hpp" @@ -753,3 +755,164 @@ TEST_CASE("Without sparse layers the filament change starts at the tallest print CHECK(tower_z < printed_z - EPSILON); } } + +// Two cubes at 0.2 mm layers with precise Z height on: the 20 mm cube stays on the 0.2 mm grid, and +// precise Z height spreads the other's gap to `short_height` over its last five layers, moving them +// off the grid. Slices and exports the plate; the emitter throws if it loses step with the tower. +static std::string slice_precise_z_pair(DynamicPrintConfig config, const char *tower_type, double short_height, Print &print, + const std::vector> *overrides = nullptr) +{ + // The default position puts the 50 mm tower plus its 3 mm brim past the 200 mm test bed. + config.set_deserialize_strict({ { "precise_z_height", "1" }, { "wipe_tower_type", tower_type }, + { "wipe_tower_x", "130" }, { "wipe_tower_y", "140" } }); + Model model; + init_print({ cube(20), make_cube(20, 20, short_height) }, print, model, config, overrides); + print.apply(model, config); // see slice_with_prime_tower + REQUIRE(print.has_wipe_tower()); + std::string out; + REQUIRE_NOTHROW(out = gcode(print)); + CHECK_FALSE(out.empty()); + return out; +} + +// Z of every extruding move under the tower's feature tag. +static std::vector tower_extrusion_z(const std::string &gcode) +{ + std::vector zs; + bool in_tower = false; + GCodeReader parser; + parser.parse_buffer(gcode, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + const std::string_view raw = line.raw(); + if (raw.rfind(";TYPE:", 0) == 0 || raw.rfind("; FEATURE: ", 0) == 0) + in_tower = raw.find("Prime tower") != std::string_view::npos; + else if (in_tower && line.extruding(self) && (line.has_x() || line.has_y())) + zs.emplace_back(self.z()); + }); + return zs; +} + +static size_t count_joined_layers(const Print &print) +{ + size_t joined = 0; + for (const LayerTools < : print.wipe_tower_data().tool_ordering.layer_tools()) + joined += lt.joins_wipe_tower_layer_below() ? 1 : 0; + return joined; +} + +TEST_CASE("A layer less than the minimum layer height above another prints its tool changes on the tower layer below", "[WipeTower]") +{ + // Both filaments print on every layer of both cubes, so every off-grid layer changes tools. At + // 10.004 mm the last five layers grow by 0.0008 mm each and sit 0.0008 to 0.004 mm above the 20 mm + // cube's; at 10.15 mm they shrink by 0.01 mm each, leaving the 20 mm cube's layers 0.01 to 0.05 mm + // above them. All of that is under the 0.07 mm minimum layer height. A purge sized for a 0.0008 mm + // layer is 100 / (0.0008 * 50) = 2500 mm deep, for a 0.05 mm one 40 mm; on a 0.19 mm layer it is + // about 10.5 mm, and a joined layer holds two. + const char *tower_type = GENERATE("type1", "type2"); + const double short_height = GENERATE(10.004, 10.15); + DynamicPrintConfig config = tower_estimate_config("rectangle"); + config.set_deserialize_strict({ { "min_layer_height", "0.07" } }); + + Print print; + const std::string out = slice_precise_z_pair(config, tower_type, short_height, print); + + CHECK(count_joined_layers(print) > 0); + std::vector tower_layer_z; + for (const LayerTools < : print.wipe_tower_data().tool_ordering.layer_tools()) + if (lt.joins_wipe_tower_layer_below()) + CHECK(lt.print_z - lt.wipe_tower_print_z < 0.07); + else if (lt.has_wipe_tower) + tower_layer_z.emplace_back(lt.print_z); + CHECK(print.wipe_tower_data().depth < 30.f); + + // A joined layer's purges go down to the tower layer they join, so the tower is only ever + // extruded at its own layers' z, never up to 0.05 mm above one (G-code Z has 3 decimals). + const std::vector zs = tower_extrusion_z(out); + REQUIRE_FALSE(zs.empty()); + size_t off_layer = 0; + for (double z : zs) + if (std::none_of(tower_layer_z.begin(), tower_layer_z.end(), [z](double t) { return std::abs(z - t) < 0.002; })) + ++off_layer; + CHECK(off_layer == 0); + + // The nozzle goes below the object layer only over the tower: descending anywhere else drags it + // through the layer just printed. + BoundingBox tower_box(print.first_layer_wipe_tower_corners()); + tower_box.offset(scaled(5.)); + size_t descents = 0, off_tower = 0; + double layer_z = 0.; + GCodeReader parser; + parser.parse_buffer(out, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + const std::string_view raw = line.raw(); + if (raw.rfind(";Z:", 0) == 0) + layer_z = std::stod(std::string(raw.substr(3))); + else if (line.has_z() && ! line.has_x() && ! line.has_y() && line.z() < layer_z - 1e-3) { + ++descents; + if (! tower_box.contains(Point(scaled(self.x()), scaled(self.y())))) { + INFO("descent to Z " << line.z() << " at X " << self.x() << " Y " << self.y() << ", layer Z " << layer_z); + ++off_tower; + } + } + }); + CHECK(descents > 0); + CHECK(off_tower == 0); +} + +TEST_CASE("Layers sliced at the printer's minimum layer height keep their own tower layers", "[WipeTower]") +{ + // 0.08 mm layers summed in floating point sit 0.0799999 mm apart now and then, just under the + // 0.08 mm minimum. Those are whole layers, not off-grid ones: none may join the layer below. + const char *tower_type = GENERATE("type1", "type2"); + DynamicPrintConfig config = tower_estimate_config("rectangle"); + config.set_deserialize_strict({ { "layer_height", "0.08" }, { "min_layer_height", "0.08" }, { "wipe_tower_type", tower_type }, + { "wipe_tower_x", "130" }, { "wipe_tower_y", "140" } }); + + Print print; + Model model; + init_print({ cube(20) }, print, model, config); + print.apply(model, config); // see slice_with_prime_tower + REQUIRE(print.has_wipe_tower()); + REQUIRE_NOTHROW(gcode(print)); + CHECK(count_joined_layers(print) == 0); +} + +TEST_CASE("A joined layer that keeps its filament leaves finishing the tower layer to the one that changed", "[WipeTower]") +{ + // The 20 mm cube prints both filaments on every layer, the short one only `filament`. Whenever the + // 20 mm cube's layer ends on that filament, the off-grid layer joined to it changes nothing, and the + // tower layer's results are all taken by the time it would finish the layer. + const char *tower_type = GENERATE("type1", "type2"); + const int filament = GENERATE(1, 2); + // An unset feature filament reads as filament 1, so the short cube names its filament for each. + const std::vector> overrides = { + {}, + { { "extruder", filament }, { "outer_wall_filament_id", filament }, { "inner_wall_filament_id", filament }, + { "sparse_infill_filament_id", filament }, { "internal_solid_filament_id", filament }, + { "top_surface_filament_id", filament }, { "bottom_surface_filament_id", filament } } }; + const DynamicPrintConfig config = tower_estimate_config("rectangle"); + + Print print; + slice_precise_z_pair(config, tower_type, 10.004, print, &overrides); + + // At least one joined layer must change no tools, or this does not test the case. + size_t keeps_filament = 0; + unsigned int last = (unsigned int) -1; + for (const LayerTools < : print.wipe_tower_data().tool_ordering.layer_tools()) { + if (lt.joins_wipe_tower_layer_below() && lt.extruders.size() == 1 && lt.extruders.front() == last) + ++keeps_filament; + if (! lt.extruders.empty()) + last = lt.extruders.back(); + } + CHECK(keeps_filament > 0); +} + +TEST_CASE("Smooth timelapse prints each tower layer's wall once when a layer joins it", "[WipeTower]") +{ + // Smooth timelapse prints a tower on every layer even with one filament, and the Type 1 tower opens + // each of its layers with the timelapse wall. A layer joined to the one below must not print it again. + DynamicPrintConfig config = tower_estimate_config("rectangle", 1); + config.set_deserialize_strict({ { "timelapse_type", "1" } }); + + Print print; + slice_precise_z_pair(config, "type1", 10.004, print); + CHECK(count_joined_layers(print) > 0); +}