diff --git a/docs/HLSD/overhang-seam-preview.md b/docs/HLSD/overhang-seam-preview.md new file mode 100644 index 0000000000..3c34f9c6e1 --- /dev/null +++ b/docs/HLSD/overhang-seam-preview.md @@ -0,0 +1,49 @@ +# Overhang seam preview — High Level Design + +## Problem + +The preview draws a seam marker where each outer wall loop starts and ends. The +marker comes from the seam detector of `GCodeProcessor`, which reads the G-code +text: it opens a candidate on the first "Outer wall" extrusion and closes it on +the first move that no longer continues the wall. + +An outer wall that starts on an overhang is labelled "Overhang wall", exactly +like the overhanging part of an inner wall, so the detector never opens a +candidate for it, although the printed seam is there. Whether a loop is an outer +wall is known only to the G-code generator. + +## Design + +`GCode::extrude_loop` flags a loop that is not in spiral vase mode, is an outer +wall (`inset_idx` 0, or any of its paths before the seam gap clipping is an +external perimeter) and whose first path is an overhang. `GCode::_extrude_piece` +then writes one comment, `; OUTER_OVERHANG_WALL`, next to the feature tag of the +first path, after the travel and before the `PA_CHANGE` tag. + +`GCodeProcessor` keeps two flags. The tag sets "pending". The next printing move +clears it and, if that move is an "Overhang wall", sets "candidate". While +"candidate" is set, "Overhang wall" moves count as "Outer wall" for the detector +only; the flag is cleared when the detector closes the candidate. Continuation +and closing already treat both roles alike, so the marker's position, threshold +and timing come from the existing detector, and the moves keep their real role. + +The tag is in the file, so G-code opened from disk, stored plate G-code and the +BBL post-processing reload get the same markers as a fresh slice. + +## Limits + +- G-code files written by earlier versions have no tag and show no marker for + these loops, as before. +- The tag adds one comment line per such loop. `PressureEqualizer` counts it in + its 128-line look-back, so with extrusion rate smoothing a feedrate near that + limit can differ slightly. Other filters pass the line through. +- Spiral vase prints are excluded. +- The existing detector's limits stay: loops printed with no move between them + share one candidate, and a gap of 0.25 mm or more gives no marker. + +## Tests + +`tests/fff_print/test_gcodeprocessor.cpp`, tag `[OverhangSeam]`: markers against +the same stream labelled "Outer wall" in each detector state; a following wall +stays unmarked; and an exported sphere against a replay of its G-code relabelled +"Outer wall". diff --git a/src/libslic3r/ExtrusionEntity.hpp b/src/libslic3r/ExtrusionEntity.hpp index 73e06d7d45..9df77a0cc7 100644 --- a/src/libslic3r/ExtrusionEntity.hpp +++ b/src/libslic3r/ExtrusionEntity.hpp @@ -6,6 +6,7 @@ #include "Polygon.hpp" #include "Polyline.hpp" +#include #include #include #include @@ -555,6 +556,14 @@ private: ExtrusionLoopRole m_loop_role; }; +// Orca: A fully overhanging outer loop has no outer wall paths but keeps inset index 0. Not the negation of +// is_inner_wall(), which goes by the first path while the inset index is unset. +inline bool is_outer_wall(const ExtrusionLoop &loop) +{ + return loop.inset_idx == 0 || + std::any_of(loop.paths.begin(), loop.paths.end(), [](const ExtrusionPath &path) { return path.role() == erExternalPerimeter; }); +} + class ExtrusionLoopSloped : public ExtrusionLoop { public: diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index f9532718c4..9441e71e50 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -8706,6 +8706,11 @@ std::string GCode::extrude_loop(const ExtrusionLoop& loop_ m_multi_flow_segment_path_average_mm3_per_mm = weighted_sum_mm3_per_mm / total_multipath_length; // Orca: end of multipath average mm3_per_mm value calculation + // Orca: the G-code labels the overhanging part of any wall "Overhang wall", so the preview cannot + // tell an outer wall that starts on an overhang from an inner one. Only outer walls need the tag. + m_outer_overhang_tag_pending = description == "perimeter" && !m_config.spiral_mode && + paths.front().role() == erOverhangPerimeter && is_outer_wall(loop_ref); + if (!enable_seam_slope) { for (const ExtrusionPath& path : paths) { gcode += this->_extrude(path, description, speed_for_path(path)); @@ -9873,6 +9878,12 @@ std::string GCode::_extrude_piece(const ExtrusionPath &path, const std::string & gcode += buf; } + // Orca: must stay before the PA_CHANGE tag: Adaptive PA reads the line right after that tag. + if (m_outer_overhang_tag_pending) { + m_outer_overhang_tag_pending = false; + gcode += ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Outer_Overhang_Wall) + "\n"; + } + if (last_was_wipe_tower || m_last_width != path.width) { m_last_width = path.width; sprintf(buf, ";%s%g\n", GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Width).c_str(), m_last_width); diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 9a68c3a429..1b43c82b17 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -828,6 +828,8 @@ protected: bool m_enable_extrusion_role_markers; // Keeps track of the last extrusion role passed to the processor ExtrusionRole m_last_processor_extrusion_role; + // Orca: the next extruded path starts an outer wall on an overhang; tag it for the preview. + bool m_outer_overhang_tag_pending{false}; // How many times will change_layer() be called? // change_layer() will update the progress bar. unsigned int m_layer_count; diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index f306100afd..97fba7c27f 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -130,7 +130,8 @@ const std::vector GCodeProcessor::Reserved_Tags = { "@TOTAL_WEIGHT@", "@TOTAL_COST@", "@TOTAL_WIPE_TOWER_COST@", - "@TOTAL_WIPE_TOWER_FILAMENT@" + "@TOTAL_WIPE_TOWER_FILAMENT@", + " OUTER_OVERHANG_WALL" }; const std::vector GCodeProcessor::Reserved_Tags_compatible = { @@ -163,7 +164,8 @@ const std::vector GCodeProcessor::Reserved_Tags_compatible = { "@TOTAL_WEIGHT@", "@TOTAL_COST@", "@TOTAL_WIPE_TOWER_COST@", - "@TOTAL_WIPE_TOWER_FILAMENT@" + "@TOTAL_WIPE_TOWER_FILAMENT@", + " OUTER_OVERHANG_WALL" }; @@ -3814,6 +3816,9 @@ void GCodeProcessor::reset() m_layer_id = 0; m_cp_color.reset(); m_mass_locator = nullptr; + m_seams_detector.reset(); + m_outer_overhang_pending = false; + m_outer_overhang_candidate = false; m_producer = EProducer::Unknown; @@ -4695,6 +4700,12 @@ void GCodeProcessor::process_tags(const std::string_view comment, bool producers return; } + // Orca: the next printing move starts an outer wall, whatever its role + if (comment == reserved_tag(ETags::Outer_Overhang_Wall)) { + m_outer_overhang_pending = true; + return; + } + // layer change tag if (comment == reserved_tag(ETags::Layer_Change)) { ++m_layer_id; @@ -5700,9 +5711,17 @@ void GCodeProcessor::process_G1(const std::array, 4>& axes const Vec3f plate_offset = {(float) m_x_offset, (float) m_y_offset, 0.0f}; + // Orca: a tagged wall that starts on an overhang is an outer wall for the seam detector only. + if (type == EMoveType::Extrude && m_outer_overhang_pending) { + m_outer_overhang_candidate = m_extrusion_role == erOverhangPerimeter; + m_outer_overhang_pending = false; + } + const bool external_for_seam = m_extrusion_role == erExternalPerimeter || + (m_extrusion_role == erOverhangPerimeter && m_outer_overhang_candidate); + if (m_seams_detector.is_active()) { // check for seam starting vertex - if (type == EMoveType::Extrude && m_extrusion_role == erExternalPerimeter) { + if (type == EMoveType::Extrude && external_for_seam) { //BBS: m_result.moves.back().position has plate offset, must minus plate offset before calculate the real seam position const Vec3f new_pos = m_result.moves.back().position - m_extruder_offsets[filament_id] - plate_offset; if (!m_seams_detector.has_first_vertex()) { @@ -5734,9 +5753,10 @@ void GCodeProcessor::process_G1(const std::array, 4>& axes } m_seams_detector.activate(false); + m_outer_overhang_candidate = false; } } - else if (type == EMoveType::Extrude && m_extrusion_role == erExternalPerimeter) { + else if (type == EMoveType::Extrude && external_for_seam) { m_seams_detector.activate(true); m_seams_detector.set_first_vertex(m_result.moves.back().position - m_extruder_offsets[filament_id] - plate_offset); } diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index 8a53739b32..22c2d45a99 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -652,6 +652,7 @@ inline constexpr float DEFAULT_FILAMENT_DENSITY = 1.245f; Total_Cost_Placeholder, Total_Wipe_Tower_Cost_Placeholder, Total_Wipe_Tower_Filament_Placeholder, + Outer_Overhang_Wall, }; static const std::string& reserved_tag(ETags tag) { return s_IsBBLPrinter ? Reserved_Tags[static_cast(tag)] : Reserved_Tags_compatible[static_cast(tag)]; } @@ -1102,6 +1103,8 @@ inline constexpr float DEFAULT_FILAMENT_DENSITY = 1.245f; bool is_active() const { return m_active; } bool has_first_vertex() const { return m_first_vertex.has_value(); } + // Orca: activate(false) keeps the first vertex; a new export must not inherit it. + void reset() { m_active = false; m_first_vertex.reset(); } }; // Helper class used to fix the z for color change, pause print and @@ -1364,6 +1367,11 @@ inline constexpr float DEFAULT_FILAMENT_DENSITY = 1.245f; unsigned int m_layer_id; CpColor m_cp_color; SeamsDetector m_seams_detector; + // Orca: an outer wall tagged in the G-code starts on the next printing move + // (docs/HLSD/overhang-seam-preview.md). + bool m_outer_overhang_pending{false}; + // Orca: the seam detector candidate is such a wall, so "Overhang wall" counts as "Outer wall". + bool m_outer_overhang_candidate{false}; OptionsZCorrector m_options_z_corrector; size_t m_last_default_color_id; bool m_detect_layer_based_on_tag {false}; diff --git a/src/libslic3r/GCode/SeamPlacer.cpp b/src/libslic3r/GCode/SeamPlacer.cpp index 2de6be1df4..ad3cc1db9c 100644 --- a/src/libslic3r/GCode/SeamPlacer.cpp +++ b/src/libslic3r/GCode/SeamPlacer.cpp @@ -485,16 +485,8 @@ Polygons extract_perimeter_polygons(const Layer *layer, std::vector(ex_entity)->entities) { ExtrusionRole role = perimeter->role(); - if (perimeter->is_loop()) { - for (const ExtrusionPath &path : static_cast(perimeter)->paths) { - if (path.role() == ExtrusionRole::erExternalPerimeter) { - role = ExtrusionRole::erExternalPerimeter; - } - } - // A fully overhanging outer loop has no external paths but keeps inset_idx 0. - if (perimeter->inset_idx == 0) - role = ExtrusionRole::erExternalPerimeter; - } + if (perimeter->is_loop() && is_outer_wall(*static_cast(perimeter))) + role = ExtrusionRole::erExternalPerimeter; if (role == ExtrusionRole::erExternalPerimeter) { Points p; diff --git a/tests/fff_print/test_gcodeprocessor.cpp b/tests/fff_print/test_gcodeprocessor.cpp index 9653ef07d2..479ff2d106 100644 --- a/tests/fff_print/test_gcodeprocessor.cpp +++ b/tests/fff_print/test_gcodeprocessor.cpp @@ -24,10 +24,13 @@ #include "test_utils.hpp" #include +#include #include #include #include #include +#include +#include #include "libslic3r/PrintConfig.hpp" #include "libslic3r/Point.hpp" #include @@ -661,6 +664,172 @@ TEST_CASE("Each extrusion weighs its filament's density", "[GCodeProcessor]") namespace { +const std::string OUTER_OVERHANG_TAG = "; OUTER_OVERHANG_WALL\n"; + +// A closed square at X/Y 10..50, printed from where the nozzle stands. +std::string square_moves() { return "G1 X10.1 Y10 E0.01 F3000\nG1 X50 Y10 E2\nG1 X50 Y50 E2\nG1 X10 Y50 E2\nG1 X10 Y10 E2\n"; } + +std::string layer_start() { return "M83\nG90\n; CHANGE_LAYER\nG1 X10 Y10 Z0.2 F12000\n"; } +std::string feature(ExtrusionRole role) { return "; FEATURE: " + ExtrusionEntity::role_to_string(role) + "\n"; } + +// Closes the seam candidate with an inner-wall move. +std::string inner_wall_end() { return feature(erPerimeter) + "G1 X12 Y12 E0.1\nG1 X30 Y12 E1\n"; } + +// Streams G-code through the processor as an export does and returns its moves. +std::vector process_stream(const std::string &gcode) +{ + FullPrintConfig config; + config.gcode_flavor.value = gcfMarlinFirmware; + // s_IsBBLPrinter selects the "; FEATURE: " tags these fixtures use. + const bool was_bbl_printer = GCodeProcessor::s_IsBBLPrinter; + const ScopeGuard restore_bbl_printer([was_bbl_printer] { GCodeProcessor::s_IsBBLPrinter = was_bbl_printer; }); + GCodeProcessor::s_IsBBLPrinter = true; + GCodeProcessor processor; + processor.reset(); + processor.initialize("stream.gcode"); + processor.initialize_result_moves(); + processor.apply_config(config); + processor.process_buffer(gcode); + processor.finalize(false); + return std::move(processor.result().moves); +} + +std::vector seams_of(const std::vector &moves) +{ + std::vector seams; + std::copy_if(moves.begin(), moves.end(), std::back_inserter(seams), + [](const GCodeProcessorResult::MoveVertex &move) { return move.type == EMoveType::Seam; }); + return seams; +} + +void check_same_seams(const std::vector &actual, const std::vector &expected, + float tolerance) +{ + REQUIRE_FALSE(expected.empty()); + REQUIRE(actual.size() == expected.size()); + for (size_t i = 0; i < actual.size(); ++i) { + INFO("seam " << i); + CHECK(actual[i].gcode_id == expected[i].gcode_id); + CHECK_THAT(actual[i].position.x(), Catch::Matchers::WithinAbs(expected[i].position.x(), tolerance)); + CHECK_THAT(actual[i].position.y(), Catch::Matchers::WithinAbs(expected[i].position.y(), tolerance)); + CHECK_THAT(actual[i].position.z(), Catch::Matchers::WithinAbs(expected[i].position.z(), tolerance)); + CHECK_THAT(actual[i].actual_feedrate, Catch::Matchers::WithinAbs(expected[i].actual_feedrate, 1e-4)); + } +} + +} // namespace + +TEST_CASE("A tagged overhang wall gets the seam an outer wall would get", "[GCodeProcessor][OverhangSeam]") +{ + // An "Outer wall" tag with no move leaves the seam detector active without a first vertex. + const bool outer_tag_first = GENERATE(false, true); + INFO("outer tag first " << outer_tag_first); + const auto wall = [&](ExtrusionRole role) { + return layer_start() + (outer_tag_first ? feature(erExternalPerimeter) : "") + feature(role) + OUTER_OVERHANG_TAG + + square_moves() + inner_wall_end(); + }; + const auto overhang = process_stream(wall(erOverhangPerimeter)); + check_same_seams(seams_of(overhang), seams_of(process_stream(wall(erExternalPerimeter))), 1e-5f); + REQUIRE(seams_of(overhang).size() == 1); + + // The wall keeps its real role. + CHECK(std::any_of(overhang.begin(), overhang.end(), [](const GCodeProcessorResult::MoveVertex &move) { + return move.type == EMoveType::Extrude && move.extrusion_role == erOverhangPerimeter; + })); +} + +TEST_CASE("An overhang wall without the tag gets no seam", "[GCodeProcessor][OverhangSeam]") +{ + CHECK(seams_of(process_stream(layer_start() + feature(erOverhangPerimeter) + square_moves() + inner_wall_end())).empty()); +} + +TEST_CASE("The tag marks only the wall that follows it", "[GCodeProcessor][OverhangSeam]") +{ + const std::string travel = "G1 X80 Y80 F12000\nG1 X10 Y10 F12000\n"; + + SECTION("an overhang wall after a tagged one") + { + const std::string gcode = layer_start() + feature(erOverhangPerimeter) + OUTER_OVERHANG_TAG + square_moves() + travel + square_moves() + travel; + CHECK(seams_of(process_stream(gcode)).size() == 1); + } + SECTION("an overhang wall after a tagged wall that started as an outer wall") + { + const std::string gcode = layer_start() + feature(erExternalPerimeter) + OUTER_OVERHANG_TAG + square_moves() + travel + + feature(erOverhangPerimeter) + square_moves() + travel; + CHECK(seams_of(process_stream(gcode)).size() == 1); + } +} + +TEST_CASE("The tag waits for the printing move while the previous candidate closes", "[GCodeProcessor][OverhangSeam]") +{ + // No travel between the walls; the unretract after the tag closes the first wall's candidate. + const std::string gcode = layer_start() + feature(erExternalPerimeter) + square_moves() + feature(erOverhangPerimeter) + OUTER_OVERHANG_TAG + + "G1 F3000\nG1 E0.8 F1800\n" + square_moves() + inner_wall_end(); + CHECK(seams_of(process_stream(gcode)).size() == 2); +} + +TEST_CASE("A tagged wall with no move before it merges like an outer wall", "[GCodeProcessor][OverhangSeam]") +{ + // Two walls with no move between them: the detector keeps the first wall's candidate. + const auto two_walls = [](ExtrusionRole second) { + return layer_start() + feature(erExternalPerimeter) + square_moves() + feature(second) + OUTER_OVERHANG_TAG + + "G1 X30 Y10 E1\nG1 X30 Y30 E1\nG1 X10 Y30 E1\nG1 X10 Y10 E1\n" + inner_wall_end(); + }; + check_same_seams(seams_of(process_stream(two_walls(erOverhangPerimeter))), seams_of(process_stream(two_walls(erExternalPerimeter))), 1e-5f); +} + +TEST_CASE("Exported overhang outer walls get the seams of outer walls", "[GCodeProcessor][OverhangSeam]") +{ + // One wall, so every wall is an outer wall. Replaying the same G-code with "Overhang wall" + // relabelled "Outer wall" gives the reference seams. + const std::string seam_slope = GENERATE("none", "all"); + INFO("seam slope " << seam_slope); + Print print; + Model model; + Test::init_print({ Test::TestMesh::sphere_50mm }, print, model, + { { "detect_overhang_wall", true }, { "layer_height", 0.4 }, { "initial_layer_print_height", 0.4 }, + { "wall_loops", 1 }, { "sparse_infill_density", "0%" }, { "top_shell_layers", 0 }, { "bottom_shell_layers", 0 }, + { "skirt_loops", 0 }, { "brim_type", "no_brim" }, { "seam_slope_type", seam_slope }, { "seam_gap", "10%" }, + { "z_offset", 0 } }); + GCodeProcessorResult exported; + std::string gcode = Test::gcode(print, &exported); + REQUIRE(gcode.find("OUTER_OVERHANG_WALL") != std::string::npos); + + const std::string overhang = ExtrusionEntity::role_to_string(erOverhangPerimeter); + const std::string outer = ExtrusionEntity::role_to_string(erExternalPerimeter); + for (size_t pos = gcode.find(overhang); pos != std::string::npos; pos = gcode.find(overhang, pos + outer.size())) + gcode.replace(pos, overhang.size(), outer); + ScopedTemporaryFile temp(".gcode"); + // Preserve exported line endings: Windows text mode can split a CRLF at a reader buffer + // boundary and introduce an extra parsed line, invalidating the gcode_id comparison. + std::ofstream(temp.string(), std::ios::binary) << gcode; + GCodeProcessor processor; + processor.process_file(temp.string()); + + check_same_seams(seams_of(exported.moves), seams_of(processor.get_result().moves), 1e-4f); +} + +TEST_CASE("Inner walls of an exported sphere are not tagged", "[GCodeProcessor][OverhangSeam]") +{ + // A single island per layer has one outer wall, so one seam at most per layer. + Print print; + Model model; + Test::init_print({ Test::TestMesh::sphere_50mm }, print, model, + { { "detect_overhang_wall", true }, { "layer_height", 0.4 }, { "initial_layer_print_height", 0.4 }, + { "wall_loops", 2 }, { "sparse_infill_density", "0%" }, { "top_shell_layers", 0 }, { "bottom_shell_layers", 0 } }); + GCodeProcessorResult result; + const std::string gcode = Test::gcode(print, &result); + REQUIRE(gcode.find("OUTER_OVERHANG_WALL") != std::string::npos); + + std::set layers; + const auto seams = seams_of(result.moves); + for (const GCodeProcessorResult::MoveVertex &seam : seams) + layers.insert(int(std::lround(seam.position.z() * 10.f))); + CHECK(layers.size() == seams.size()); +} + +namespace { + // A printer with `nozzles` nozzles over a 200 mm square bed: nozzle `filament_1_nozzle` reaches only // `nozzle_area`, the others the whole bed. A 20 mm cube centered on `center` prints with filament 1, // mapped to that nozzle and printable on every nozzle.