From 5ea64864ef5f409c3422237263894e8ba1ea165e Mon Sep 17 00:00:00 2001 From: Clifford Date: Thu, 29 Oct 2026 16:50:57 -0400 Subject: [PATCH] Widen the post-slice extruder masks to 64 bits (#17002) The post-slice check stored the extruders that can't print each filament as bits in an int. From the 32nd extruder on, the shift overflowed, and on x86 extruder 33's bit landed on extruder 1. limit_filament_maps and the printable area mask are now 64-bit, which covers MAXIMUM_EXTRUDER_NUMBER. Printers with up to 31 extruders get the same values as before. The tests check the area, height and filament type limits on nozzles 2, 32, 33 and 64, and slice a cube on nozzle 64 of a 64-nozzle printer. --- src/libslic3r/Format/bbs_3mf.cpp | 2 +- src/libslic3r/Format/bbs_3mf.hpp | 9 +- src/libslic3r/GCode/GCodeProcessor.cpp | 13 +-- src/libslic3r/GCode/GCodeProcessor.hpp | 3 +- src/libslic3r/Print.cpp | 3 +- tests/fff_print/test_gcodeprocessor.cpp | 106 +++++++++++++++++++++--- 6 files changed, 110 insertions(+), 26 deletions(-) diff --git a/src/libslic3r/Format/bbs_3mf.cpp b/src/libslic3r/Format/bbs_3mf.cpp index 1c809910e0..f693ac2d5a 100644 --- a/src/libslic3r/Format/bbs_3mf.cpp +++ b/src/libslic3r/Format/bbs_3mf.cpp @@ -8808,7 +8808,7 @@ bool _BBS_3MF_Exporter::_add_model_config_file_to_archive(mz_zip_archive& archiv if (plate_data->limit_filament_maps.size() > 0) { stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << LIMIT_FILAMENT_MAP_ATTR << "\" " << VALUE_ATTR << "=\""; - add_vector(stream, plate_data->limit_filament_maps); + add_vector(stream, plate_data->limit_filament_maps); stream << "\"/>\n"; } diff --git a/src/libslic3r/Format/bbs_3mf.hpp b/src/libslic3r/Format/bbs_3mf.hpp index b9eea3251c..ca1819324a 100644 --- a/src/libslic3r/Format/bbs_3mf.hpp +++ b/src/libslic3r/Format/bbs_3mf.hpp @@ -8,6 +8,7 @@ #include "libslic3r/ProjectTask.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp" #include +#include #include #include #include @@ -140,12 +141,8 @@ struct PlateData std::vector nozzles_info; std::optional nozzle_group_result; - // Hexadecimal number, - // the 0th digit corresponds to extruder 1 - // the 1th digit corresponds to extruder 2 - // ... and so on. - // 0 means can be print on this extruder, 1 means cannot - std::vector limit_filament_maps; + // Per filament, bit i is set when extruder i + 1 can't print it. + std::vector limit_filament_maps; std::vector warnings; diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 791d106da2..0157c8cdb4 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -3022,11 +3023,11 @@ bool GCodeProcessor::check_multi_extruder_gcode_valid(const int auto unprintable_extruders = [&unprintable_areas, &unprintable_bboxes, &plate_offset](const BoundingBox &path_bbox) { BoundingBox bbox = path_bbox.inflated(SCALED_EPSILON); bbox.translate(-plate_offset); - int mask = 0; + uint64_t mask = 0; for (size_t i = 0; i < unprintable_areas.size(); ++i) if (!unprintable_areas[i].empty() && unprintable_bboxes[i].overlap(bbox) && !intersection(unprintable_areas[i], bbox.polygon()).empty()) - mask |= 1 << i; + mask |= uint64_t(1) << i; return mask; }; @@ -3084,9 +3085,9 @@ bool GCodeProcessor::check_multi_extruder_gcode_valid(const int //BoundingBox bbox_custom = path_poly_custom.bounding_box(); //bbox_custom.offset(-scale_(1.0)); // Narrow the range to provide a tolerance for the custom gcode //bbox.merge(bbox_custom); // merge the custom gcode pos with other pos*/ - const int unprintable_mask = unprintable_extruders(bbox); + const uint64_t unprintable_mask = unprintable_extruders(bbox); // check printable area - if ((extruder_id < unprintable_areas.size()) && (unprintable_mask & (1 << extruder_id))) { + if ((extruder_id < unprintable_areas.size()) && (unprintable_mask & (uint64_t(1) << extruder_id))) { m_result.gcode_check_result.error_code |= 1; std::pair filament_to_object_id; filament_to_object_id.first = iter->first; @@ -3102,7 +3103,7 @@ bool GCodeProcessor::check_multi_extruder_gcode_valid(const int filament_to_object_id.first = iter->first; filament_to_object_id.second = object_label_id; m_result.gcode_check_result.print_height_error_infos[extruder_id].push_back(filament_to_object_id); - m_result.limit_filament_maps[iter->first] |= (1 << extruder_id); + m_result.limit_filament_maps[iter->first] |= uint64_t(1) << extruder_id; valid = false; } @@ -3115,7 +3116,7 @@ bool GCodeProcessor::check_multi_extruder_gcode_valid(const int for (int extruder_id = 0; extruder_id < unprintable_filament_types.size(); ++extruder_id) { const std::set &filament_ids = unprintable_filament_types[extruder_id]; for (int filament_id : filament_ids) { - m_result.limit_filament_maps[filament_id] |= (1 << extruder_id); + m_result.limit_filament_maps[filament_id] |= uint64_t(1) << extruder_id; } }; diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index b99a5f2e86..8a53739b32 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -389,7 +389,8 @@ inline constexpr float DEFAULT_FILAMENT_DENSITY = 1.245f; std::vector filament_costs; std::vector filament_vitrification_temperature; std::vector filament_maps; - std::vector limit_filament_maps; + // Per filament, bit i is set when extruder i + 1 can't print it. + std::vector limit_filament_maps; PrintEstimatedStatistics print_statistics; std::vector custom_gcode_per_print_z; bool spiral_vase_mode; diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 59ede45165..a2e289a985 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -4951,7 +4951,8 @@ std::vector> Print::get_physical_unprintable_filaments(const std:: auto get_unprintable_extruder_id = [&](unsigned int filament_idx) -> int { // filament_printable may be shorter than the filament count; get_at() clamps. int status = m_config.filament_printable.get_at(filament_idx); - for (int i = 0; i < extruder_num; ++i) { + // An int has bits for the first 32 extruders only. + for (int i = 0; i < std::min(extruder_num, 32); ++i) { if (!(status >> i & 1)) { return i; } diff --git a/tests/fff_print/test_gcodeprocessor.cpp b/tests/fff_print/test_gcodeprocessor.cpp index 0bb0b33e3b..9653ef07d2 100644 --- a/tests/fff_print/test_gcodeprocessor.cpp +++ b/tests/fff_print/test_gcodeprocessor.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "libslic3r/libslic3r.h" #include "libslic3r/BeltTransform.hpp" #include "libslic3r/BoundingBox.hpp" @@ -24,10 +25,12 @@ #include #include +#include #include #include #include "libslic3r/PrintConfig.hpp" #include "libslic3r/Point.hpp" +#include #include #include #include @@ -658,36 +661,57 @@ TEST_CASE("Each extrusion weighs its filament's density", "[GCodeProcessor]") namespace { -// Two nozzles over a 200 mm square bed: nozzle 1 reaches only `nozzle_1_area`, nozzle 2 the whole bed. -// A 20 mm cube centered on `center` prints with filament 1, mapped to nozzle 1. -void slice_cube_in_nozzle_area(const Vec2ds &nozzle_1_area, const Vec2d ¢er, GCodeProcessorResult &result) +// 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. +void slice_cube_in_nozzle_area(const Vec2ds &nozzle_area, const Vec2d ¢er, GCodeProcessorResult &result, int nozzles = 2, + int filament_1_nozzle = 1) { + const auto per_nozzle = [nozzles](const std::string &value) { + std::string values = value; + for (int i = 1; i < nozzles; ++i) + values += "," + value; + return values; + }; + std::string extruder_ids = "1"; + for (int i = 2; i <= nozzles; ++i) + extruder_ids += "," + std::to_string(i); DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); config.set_deserialize_strict({ - { "nozzle_diameter", "0.4,0.4" }, - { "printer_extruder_id", "1,2" }, - { "printer_extruder_variant", "Direct Drive Standard,Direct Drive Standard" }, - { "extruder_offset", "0x0,0x0" }, + { "nozzle_diameter", per_nozzle("0.4") }, + { "printer_extruder_id", extruder_ids }, + { "printer_extruder_variant", per_nozzle("Direct Drive Standard") }, + { "extruder_offset", per_nozzle("0x0") }, { "printable_area", "0x0,200x0,200x200,0x200" }, { "printable_height", 100 }, - { "extruder_printable_height", "100,100" }, + { "extruder_printable_height", per_nozzle("100") }, + // One filament, so one purge volume per nozzle, as the GUI and the CLI size it. + { "flush_volumes_matrix", per_nozzle("0") }, { "skirt_loops", 0 }, { "brim_type", "no_brim" }, + { "layer_change_gcode", "G92 E0\n" }, // validate() relative-E reset { "filament_map_mode", "Manual" }, - { "filament_map", "1" }, + { "filament_map", filament_1_nozzle }, + { "filament_printable", -1 }, }); + // Other per-nozzle printer options, at their defaults. + for (const char *key : { "extruder_type", "nozzle_volume_type", "min_layer_height", "max_layer_height", "extruder_max_nozzle_count" }) + dynamic_cast(config.option(key))->resize(nozzles); const Vec2ds bed { Vec2d(0., 0.), Vec2d(200., 0.), Vec2d(200., 200.), Vec2d(0., 200.) }; - config.set_key_value("extruder_printable_area", new ConfigOptionPointsGroups(std::vector{ nozzle_1_area, bed })); + std::vector areas(nozzles, bed); + areas[filament_1_nozzle - 1] = nozzle_area; + config.set_key_value("extruder_printable_area", new ConfigOptionPointsGroups(areas)); TriangleMesh mesh = Test::cube(20); mesh.translate(float(center.x() - 10.), float(center.y() - 10.), 0.f); Print print; Model model; Test::init_print(std::vector{ std::move(mesh) }, print, model, config, nullptr, false); + REQUIRE_THAT(print.validate().string, Catch::Matchers::Equals(std::string())); Test::gcode(print, &result); } -// Nozzle 1's area inset from the bed on one side, on two adjacent sides (leaving an L-shaped +// A nozzle's area inset from the bed on one side, on two adjacent sides (leaving an L-shaped // unprintable region), on three (a U) and on all four (a frame). Each contains the bed center and // has an X 150 edge; all but the first also have a Y 50 edge, meeting it in an inner corner. Vec2ds inset_nozzle_area(int sides) @@ -802,3 +826,63 @@ TEST_CASE("A filament mapped to no nozzle has no errors listed under a nozzle", CHECK(result.gcode_check_result.print_area_error_infos.count(-1) == 0); } + +TEST_CASE("Nozzles up to the 64th are marked as unable to print a filament they can't", "[GCodeProcessor][Regression]") +{ + // 64 nozzles over a 200 mm square bed. Filament 1 maps to one of them and prints a 20 mm square + // at X 160-180, Y 100-120, Z 10, which that nozzle alone can't print. + const int nozzle = GENERATE(1, 31, 32, 63); // 0-based + CAPTURE(nozzle); + const Vec2ds bed { Vec2d(0., 0.), Vec2d(200., 0.), Vec2d(200., 200.), Vec2d(0., 200.) }; + std::vector unprintable_areas(MAXIMUM_EXTRUDER_NUMBER); + std::vector printable_heights(MAXIMUM_EXTRUDER_NUMBER, 100.); + std::vector> unprintable_filament_types(MAXIMUM_EXTRUDER_NUMBER); + int expected_error = 0; + SECTION("it can't reach the square") + { + const Vec2ds nozzle_area { Vec2d(0., 0.), Vec2d(150., 0.), Vec2d(150., 200.), Vec2d(0., 200.) }; + unprintable_areas[nozzle] = diff(Polygon::new_scale(bed), Polygon::new_scale(nozzle_area)); + expected_error = 1; + } + SECTION("the square is above its printable height") + { + printable_heights[nozzle] = 5.; + expected_error = 1 << 1; + } + SECTION("it can't print the filament's type") { unprintable_filament_types[nozzle] = { 0 }; } + + GCodeProcessor processor; + for (const Vec2d &point : { Vec2d(160., 100.), Vec2d(180., 100.), Vec2d(180., 120.), Vec2d(160., 120.) }) { + GCodeProcessorResult::MoveVertex move; + move.type = EMoveType::Extrude; + move.extrusion_role = erExternalPerimeter; + move.extruder_id = 0; // filament 1 + move.object_label_id = 1; + move.position = Vec3f(float(point.x()), float(point.y()), 10.f); + move.print_z = 10.f; + processor.result().moves.emplace_back(move); + } + processor.check_multi_extruder_gcode_valid(int(MAXIMUM_EXTRUDER_NUMBER), bed, 100., {}, unprintable_areas, printable_heights, + { nozzle + 1 }, unprintable_filament_types); + const GCodeProcessorResult &result = processor.result(); + + CHECK(result.gcode_check_result.error_code == expected_error); + REQUIRE(result.limit_filament_maps.size() == 1); + CHECK(result.limit_filament_maps[0] == uint64_t(1) << nozzle); +} + +TEST_CASE("A cube on the 64th nozzle of a 64-nozzle printer slices, and fails the area check only outside that nozzle's area", "[GCodeProcessor][Regression]") +{ + // Filament 1 maps to nozzle 64, which reaches X 0-150. The cube sits at the bed center, or at X 165-185. + const int nozzles = 64; + const bool outside = GENERATE(false, true); + INFO("cube " << (outside ? "outside" : "inside") << " nozzle 64's area"); + GCodeProcessorResult result; + slice_cube_in_nozzle_area(inset_nozzle_area(1), outside ? Vec2d(175., 100.) : Vec2d(100., 100.), result, nozzles, nozzles); + + const int expected = outside ? 1 : 0; + CHECK((result.gcode_check_result.error_code & 1) == expected); + CHECK(result.gcode_check_result.print_area_error_infos.count(nozzles - 1) == size_t(expected)); + REQUIRE(result.limit_filament_maps.size() == 1); + CHECK(result.limit_filament_maps[0] == uint64_t(expected) << (nozzles - 1)); +}