From 7481fdcdaed9c5c4138178acbd66b9ece6c4dc90 Mon Sep 17 00:00:00 2001 From: Harry Loud Date: Thu, 24 Sep 2026 13:54:26 +0100 Subject: [PATCH] Fix exclusion conversion and legacy brim clipping --- src/libslic3r/Brim.cpp | 10 ++-- src/libslic3r/ExclusionVolumeGeometry.cpp | 17 ++++++ src/libslic3r/ExclusionVolumeGeometry.hpp | 4 ++ src/libslic3r/Model.cpp | 5 +- src/libslic3r/PrintConfig.cpp | 53 +++++++++++++++++-- src/libslic3r/PrintConfig.hpp | 8 +++ src/slic3r/GUI/PartPlate.cpp | 3 +- src/slic3r/GUI/Tab.cpp | 24 ++++----- tests/fff_print/test_exclusion_volumes.cpp | 19 ++++++- tests/libslic3r/test_exclusion_volumes.cpp | 60 ++++++++++++++++++++++ 10 files changed, 179 insertions(+), 24 deletions(-) diff --git a/src/libslic3r/Brim.cpp b/src/libslic3r/Brim.cpp index f1bbfa67de..a40fdd13f2 100644 --- a/src/libslic3r/Brim.cpp +++ b/src/libslic3r/Brim.cpp @@ -440,8 +440,8 @@ static ExPolygons outer_inner_brim_area(const Print& print, std::vector> exclusion_regions_by_extruder; if (has_bed_exclude_volumes(print.config())) { exclusion_regions_by_extruder = get_bed_excluded_regions_by_extruder(print.config()); - // Legacy areas historically constrain placement, not generated - // geometry. Only explicit collision volumes may trim a brim. + // The established bed polygon below still clips the bed edge and + // legacy areas. Only explicit volumes use the new Z-aware trimming. for (std::vector ®ions : exclusion_regions_by_extruder) regions.erase(std::remove_if(regions.begin(), regions.end(), [](const BedExcludeRegion ®ion) { return !region.is_collision_volume(); }), regions.end()); @@ -646,6 +646,8 @@ static ExPolygons outer_inner_brim_area(const Print& print, if (extruder_unprintable_area.empty()) { extruder_unprintable_area.resize(extruder_nums, Polygons{Model::getBedPolygon()}); } + const size_t physical_extruder_count = std::max( + print.config().nozzle_diameter.size(), extruder_unprintable_area.size()); if (print.has_wipe_tower() && !print.get_fake_wipe_tower().outer_wall.empty()) { ExPolygons expolyFromLines{}; for (auto polyline : print.get_fake_wipe_tower().outer_wall.begin()->second) { @@ -665,7 +667,7 @@ static ExPolygons outer_inner_brim_area(const Print& print, if (iter != objPrintVec.end() && iter->second > 0) { object_physical_extruders = bed_exclusion_physical_extruders( - print, { iter->second - 1 }, exclusion_regions_by_extruder.size(), 0); + print, { iter->second - 1 }, physical_extruder_count, 0); for (const size_t extruder_id : object_physical_extruders) { if (extruder_id >= extruder_unprintable_area.size()) continue; @@ -678,7 +680,7 @@ static ExPolygons outer_inner_brim_area(const Print& print, } if (object_physical_extruders.empty()) { - object_physical_extruders.resize(exclusion_regions_by_extruder.size()); + object_physical_extruders.resize(physical_extruder_count); std::iota(object_physical_extruders.begin(), object_physical_extruders.end(), size_t(0)); } const ExPolygons exclusions = active_bed_exclusion_footprints( diff --git a/src/libslic3r/ExclusionVolumeGeometry.cpp b/src/libslic3r/ExclusionVolumeGeometry.cpp index af1bf25091..b7bae0bb30 100644 --- a/src/libslic3r/ExclusionVolumeGeometry.cpp +++ b/src/libslic3r/ExclusionVolumeGeometry.cpp @@ -1,6 +1,8 @@ #include "ExclusionVolumeGeometry.hpp" +#include "BoundingBox.hpp" #include "ClipperUtils.hpp" +#include "Geometry/ConvexHull.hpp" #include "Print.hpp" #include @@ -57,6 +59,21 @@ bool bed_exclusion_z_ranges_overlap( return first_max >= second_min - Z_EPSILON_MM && first_min <= second_max + Z_EPSILON_MM; } +Polygon bed_exclusion_arrange_polygon(const Polygon &polygon) +{ + if (polygon.points.size() < 3) + return polygon; + + const Polygon convex_hull = Geometry::convex_hull(polygon.points); + const double polygon_area = std::abs(polygon.area()); + const double hull_area = std::abs(convex_hull.area()); + const double tolerance = std::max(1.0, hull_area) * 1e-12; + if (std::abs(hull_area - polygon_area) <= tolerance) + return polygon; + + return get_extents(polygon).polygon(); +} + ExPolygons active_bed_exclusion_footprints( const std::vector ®ions, const double z_min, diff --git a/src/libslic3r/ExclusionVolumeGeometry.hpp b/src/libslic3r/ExclusionVolumeGeometry.hpp index cbf7140b85..5bcaaaa5aa 100644 --- a/src/libslic3r/ExclusionVolumeGeometry.hpp +++ b/src/libslic3r/ExclusionVolumeGeometry.hpp @@ -14,6 +14,10 @@ class PrintObject; bool bed_exclusion_z_ranges_overlap(double first_min, double first_max, double second_min, double second_max); +// libnest2d currently accepts only convex fixed obstacles. Keep convex +// exclusions exact and conservatively use the bounding box for concave ones. +Polygon bed_exclusion_arrange_polygon(const Polygon &polygon); + // Return the union of exclusion footprints intersecting the requested extrusion // slab. Regions are already resolved into physical-nozzle/model coordinates by // PrintConfig; callers only provide the physical nozzles that may emit the diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 135004821a..55fbae6bda 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -3108,7 +3108,10 @@ void Model::setPrintSpeedTable(const DynamicPrintConfig& config, const PrintConf //auto print_config = print.config(); //printSpeedMap.bed_poly.points = get_bed_shape(*(wxGetApp().plater()->config())); printSpeedMap.bed_poly.points = get_bed_shape(config); - Pointfs excluse_area_points = has_bed_exclude_volumes(print_config) ? Pointfs{} : print_config.bed_exclude_area.values; + // Collision volumes are additive to the legacy bed keep-out. Preserve the + // established printable-bed polygon so bed-edge and legacy-area brim + // clipping continue to work when collision volumes are also configured. + Pointfs excluse_area_points = print_config.bed_exclude_area.values; Polygons exclude_polys; Polygon exclude_poly; for (int i = 0; i < excluse_area_points.size(); i++) { diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 862955d192..e1dd457bce 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -900,14 +900,15 @@ void PrintConfigDef::init_common_params() def->label = L("Exclusion volumes"); def->tooltip = L("Collision volumes used for model and generated-material exclusion, G-code checking, and travel avoidance. " "Use \"XxY, XxY, ...\" for a full-height polygon or \"ZMIN..ZMAX;XxY, XxY, ...\" for a Z-limited volume. " - "Separate multiple volumes with \"|\". These volumes are additional to any legacy excluded bed area."); + "Separate multiple volumes with \"|\". These volumes are additional to any legacy excluded bed area. " + "Automatic arrangement conservatively uses the bounding box of a non-convex volume."); def->mode = comAdvanced; def->gui_type = ConfigOptionDef::GUIType::one_string; def->set_default_value(new ConfigOptionString()); def = this->add("bed_exclude_volume_mode", coEnum); def->label = L("Exclusion volume behaviour"); - def->tooltip = L("Choose whether all nozzles use the same exclusion volumes, derive toolhead-relative volumes from each nozzle's XY offset, or configure independent volumes for every extruder."); + def->tooltip = L("Choose whether all nozzles use the same exclusion volumes, derive toolhead-relative volumes from each nozzle's XY offset, or configure independent volumes for every extruder. In individual mode, configure the volumes on each Extruder page."); def->enum_keys_map = &ConfigOptionEnum::get_enum_values(); def->enum_values = { "shared", "toolhead_offset", "per_extruder" }; def->enum_labels = { L("Shared"), L("Toolhead-relative (nozzle offsets)"), L("Individual per extruder") }; @@ -916,7 +917,7 @@ void PrintConfigDef::init_common_params() def = this->add("extruder_bed_exclude_volumes", coStrings); def->label = L("Extruder exclusion volumes"); - def->tooltip = L("Collision volumes used only by this extruder in individual mode. Use the same polygon and optional Z-range format as the shared exclusion volumes. Leave empty for no exclusion volumes on this extruder."); + def->tooltip = L("Collision volumes used only by this extruder in individual mode. Use the same polygon and optional Z-range format as the shared exclusion volumes. Leave empty for no exclusion volumes on this extruder. Automatic arrangement conservatively uses the bounding box of a non-convex volume."); def->mode = comAdvanced; def->gui_type = ConfigOptionDef::GUIType::one_string; def->set_default_value(new ConfigOptionStrings{ "" }); @@ -13355,6 +13356,52 @@ bool is_valid_bed_exclude_volumes_string(const std::string &value, const double return found_nonempty; } +std::string legacy_bed_exclude_area_to_volumes(const Pointfs &points) +{ + // The legacy option has always interpreted each consecutive group of four + // points as a separate rectangular keep-out. Preserve that meaning instead + // of joining several rectangles into one invalid concave polygon. + if (points.size() < 4 || points.size() % 4 != 0) + return {}; + + std::string result; + for (size_t offset = 0; offset < points.size(); offset += 4) { + const Pointfs rectangle(points.begin() + offset, points.begin() + offset + 4); + const std::string serialized = ConfigOptionPoints(rectangle).serialize(); + if (!result.empty()) + result += '|'; + result += serialized; + } + return result; +} + +void append_bed_exclude_volumes(DynamicPrintConfig &config, const std::string &definition) +{ + if (definition.empty()) + return; + + const auto append = [&definition](const std::string ¤t) { + return current.empty() ? definition : current + '|' + definition; + }; + const BedExcludeVolumeMode mode = config.has("bed_exclude_volume_mode") ? + config.opt_enum("bed_exclude_volume_mode") : BedExcludeVolumeMode::Shared; + if (mode == BedExcludeVolumeMode::PerExtruder) { + const ConfigOptionStrings *current = config.option("extruder_bed_exclude_volumes"); + std::vector definitions = current != nullptr ? current->values : std::vector{}; + const ConfigOptionFloats *nozzles = config.option("nozzle_diameter"); + const size_t extruder_count = std::max(1, nozzles != nullptr ? nozzles->size() : 0); + definitions.resize(std::max(definitions.size(), extruder_count)); + for (size_t extruder_id = 0; extruder_id < extruder_count; ++extruder_id) + definitions[extruder_id] = append(definitions[extruder_id]); + config.set_key_value("extruder_bed_exclude_volumes", new ConfigOptionStrings(std::move(definitions))); + return; + } + + const ConfigOptionString *current = config.option("bed_exclude_volumes"); + config.set_key_value("bed_exclude_volumes", new ConfigOptionString( + append(current != nullptr ? current->value : std::string{}))); +} + bool has_bed_exclude_volumes(const DynamicPrintConfig &cfg) { const ConfigOptionString *shared = cfg.opt("bed_exclude_volumes"); diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 32b5279758..35cc6f8cfd 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -2348,6 +2348,14 @@ int bed_exclusion_extruder_for_filament(size_t filament_id, const std::vector(BedExcludeVolumeMode::Shared)); + append_bed_exclude_volumes(converted, *definition); converted.set_key_value("bed_exclude_area", new ConfigOptionPoints()); load_config(converted); on_presets_changed(); @@ -5138,29 +5136,29 @@ void TabPrinter::build_fff() }); optgroup->append_single_option_line("parallel_printheads_count"); create_line_with_widget(optgroup.get(), "bed_exclude_area", "printer_basic_information_printable_space#excluded-bed-area", [this](wxWindow *parent) { - Button *button = new Button(parent, _L("Convert area to collision volume")); - button->SetToolTip(_L("Convert the legacy excluded bed area into a collision volume so Orca can check G-code moves and reroute travel around it.")); + Button *button = new Button(parent, _L("Convert") + " " + dots); + button->SetStyle(ButtonStyle::Regular, ButtonType::Parameter); + button->SetToolTip(_L("Convert each legacy excluded-bed rectangle into a full-height collision volume. Existing collision volumes and the selected multi-nozzle behaviour are preserved.")); wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); - sizer->Add(button); + sizer->Add(button, 0, wxALIGN_CENTER_VERTICAL); button->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) { const ConfigOptionPoints *legacy = m_config->option("bed_exclude_area"); - if (legacy == nullptr || legacy->values.size() < 3) { - show_error(wxGetApp().plater(), _L("Configure a valid excluded bed area before converting it.")); + const std::string definition = legacy != nullptr ? + legacy_bed_exclude_area_to_volumes(legacy->values) : std::string{}; + if (definition.empty()) { + show_error(wxGetApp().plater(), _L("Configure a valid legacy excluded bed area containing one or more four-corner rectangles before converting it.")); return; } - const std::string definition = legacy->serialize(); MessageDialog dialog( wxGetApp().plater(), - _L("Convert this legacy excluded bed area into a full-height collision volume? This enables G-code checking and travel avoidance."), + _L("Convert the legacy excluded bed area into full-height collision volumes? They will be added to any existing collision volumes, the current multi-nozzle behaviour will be preserved, and the legacy area will be cleared."), _L("Convert exclusion volume"), wxICON_QUESTION | wxYES | wxNO); if (dialog.ShowModal() != wxID_YES) return; DynamicPrintConfig converted = *m_config; - converted.set_key_value("bed_exclude_volumes", new ConfigOptionString(definition)); - converted.set_key_value("bed_exclude_volume_mode", - new ConfigOptionEnum(BedExcludeVolumeMode::Shared)); + append_bed_exclude_volumes(converted, definition); converted.set_key_value("bed_exclude_area", new ConfigOptionPoints()); load_config(converted); on_presets_changed(); diff --git a/tests/fff_print/test_exclusion_volumes.cpp b/tests/fff_print/test_exclusion_volumes.cpp index 5a530b0f48..d37e12e59d 100644 --- a/tests/fff_print/test_exclusion_volumes.cpp +++ b/tests/fff_print/test_exclusion_volumes.cpp @@ -231,9 +231,10 @@ struct BrimRun { double total_length {0.0}; bool intersects_test_region {false}; + bool leaves_bed {false}; }; -BrimRun generate_brim(const std::string &exclusion_definition) +BrimRun generate_brim(const std::string &exclusion_definition, const Vec2d &object_position = Vec2d(40.0, 40.0)) { DynamicPrintConfig config = exclusion_config(BedExcludeVolumeMode::Shared, exclusion_definition); config.set_deserialize_strict({ @@ -244,7 +245,7 @@ BrimRun generate_brim(const std::string &exclusion_definition) }); TriangleMesh mesh = make_cube(20.0, 20.0, 20.0); - mesh.translate(40.0f, 40.0f, 0.0f); + mesh.translate(float(object_position.x()), float(object_position.y()), 0.0f); Print print; Model model; Test::init_print(std::vector{std::move(mesh)}, print, model, config, nullptr, false); @@ -262,6 +263,13 @@ BrimRun generate_brim(const std::string &exclusion_definition) result.total_length += path.length(); result.intersects_test_region = !intersection_pl(paths, Polygons{rectangle(32.0, 35.0, 38.0, 65.0)}).empty(); + result.leaves_bed = std::any_of(paths.begin(), paths.end(), [](const Polyline &path) { + return std::any_of(path.points.begin(), path.points.end(), [](const Point &point) { + const Vec2d position = to_mm(point); + return position.x() < -EPSILON || position.x() > 100.0 + EPSILON || + position.y() < -EPSILON || position.y() > 100.0 + EPSILON; + }); + }); return result; } @@ -640,6 +648,13 @@ TEST_CASE("Brim paths are clipped only by exclusions active on the first layer", CHECK(active.total_length < raised.total_length); } +TEST_CASE("Brim paths remain clipped to the bed without collision volumes", "[ExclusionVolume][Brim][Regression]") +{ + const BrimRun brim = generate_brim("", Vec2d(21.0, 5.0)); + REQUIRE(brim.total_length > 0.0); + CHECK_FALSE(brim.leaves_bed); +} + TEST_CASE("Support exclusion masks follow explicit and current-filament roles", "[ExclusionVolume][Support][MultiNozzle]") { SECTION("explicit support roles select one physical nozzle each") { diff --git a/tests/libslic3r/test_exclusion_volumes.cpp b/tests/libslic3r/test_exclusion_volumes.cpp index e5f07ad64a..9bb01ff91b 100644 --- a/tests/libslic3r/test_exclusion_volumes.cpp +++ b/tests/libslic3r/test_exclusion_volumes.cpp @@ -100,6 +100,48 @@ TEST_CASE("Legacy point options reject collision-volume syntax", "[ExclusionVolu CHECK(is_bed_exclusion_volume_syntax(definition)); } +TEST_CASE("Legacy rectangle groups convert into separate collision volumes", "[ExclusionVolume][PrintConfig]") +{ + ConfigOptionPoints legacy; + REQUIRE(legacy.deserialize("0x0,10x0,10x10,0x10,20x20,30x20,30x30,20x30")); + CHECK(legacy_bed_exclude_area_to_volumes(legacy.values) == + "0x0,10x0,10x10,0x10|20x20,30x20,30x30,20x30"); + + legacy.values.pop_back(); + CHECK(legacy_bed_exclude_area_to_volumes(legacy.values).empty()); +} + +TEST_CASE("Converted volumes preserve existing definitions and nozzle mode", "[ExclusionVolume][PrintConfig][MultiNozzle]") +{ + const std::string converted = "40x40,50x40,50x50,40x50"; + + SECTION("toolhead-relative definitions remain shared") { + DynamicPrintConfig config = two_extruder_config(); + config.set_key_value("bed_exclude_volume_mode", + new ConfigOptionEnum(BedExcludeVolumeMode::ToolheadOffset)); + append_bed_exclude_volumes(config, converted); + + CHECK(config.opt_enum("bed_exclude_volume_mode") == BedExcludeVolumeMode::ToolheadOffset); + CHECK(config.opt_string("bed_exclude_volumes") == + "10..30;0x0,10x0,10x10,0x10|40x40,50x40,50x50,40x50"); + } + + SECTION("individual definitions receive the former shared keep-out") { + DynamicPrintConfig config = two_extruder_config(); + config.set_key_value("bed_exclude_volume_mode", + new ConfigOptionEnum(BedExcludeVolumeMode::PerExtruder)); + config.set_key_value("extruder_bed_exclude_volumes", new ConfigOptionStrings{"", "60x60,70x60,70x70,60x70"}); + append_bed_exclude_volumes(config, converted); + + CHECK(config.opt_enum("bed_exclude_volume_mode") == BedExcludeVolumeMode::PerExtruder); + const ConfigOptionStrings *per_extruder = config.option("extruder_bed_exclude_volumes"); + REQUIRE(per_extruder != nullptr); + REQUIRE(per_extruder->values.size() == 2); + CHECK(per_extruder->values[0] == converted); + CHECK(per_extruder->values[1] == "60x60,70x60,70x70,60x70|40x40,50x40,50x50,40x50"); + } +} + TEST_CASE("Exclusion syntax validation accepts supported forms and rejects malformed regions", "[ExclusionVolume][PrintConfig]") { const auto [definition, valid] = GENERATE(table({ @@ -379,6 +421,24 @@ TEST_CASE("Exclusion Z overlap treats touching slabs conservatively", "[Exclusio CHECK_FALSE(bed_exclusion_z_ranges_overlap(0.0, 9.0, 10.0, 20.0)); } +TEST_CASE("Arrange keeps convex exclusions exact and bounds concave exclusions", "[ExclusionVolume][Geometry][Arrange]") +{ + const Polygon convex = rectangle(0.0, 0.0, 10.0, 10.0); + CHECK(bed_exclusion_arrange_polygon(convex).points == convex.points); + + Polygon concave({ + Point::new_scale(0.0, 0.0), Point::new_scale(10.0, 0.0), + Point::new_scale(10.0, 4.0), Point::new_scale(4.0, 4.0), + Point::new_scale(4.0, 10.0), Point::new_scale(0.0, 10.0), + }); + concave.make_counter_clockwise(); + REQUIRE_FALSE(concave.contains(Point::new_scale(8.0, 8.0))); + + const Polygon blocker = bed_exclusion_arrange_polygon(concave); + CHECK(blocker.points.size() == 4); + CHECK(blocker.contains(Point::new_scale(8.0, 8.0))); +} + TEST_CASE("Active exclusion footprints select Z range nozzle translation and clearance", "[ExclusionVolume][Geometry][MultiNozzle]") { const std::vector> regions_by_extruder{