From c01ac8ff4477d4b20efe9b0dc9a5cb8550a2c63e Mon Sep 17 00:00:00 2001 From: Kiss Lorand <50251547+kisslorand@users.noreply.github.com> Date: Sat, 7 Nov 2026 03:39:38 +0200 Subject: [PATCH] Validate excluded bed area (#17209) --- src/libslic3r/PrintConfig.cpp | 37 +++++++++++++++++++--- src/libslic3r/PrintConfig.hpp | 2 ++ src/slic3r/GUI/Field.cpp | 9 ++++++ src/slic3r/GUI/Plater.cpp | 11 +++++++ tests/libslic3r/test_exclusion_volumes.cpp | 25 +++++++++++++++ 5 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index e894529fa9..42dbe730b3 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -13853,6 +13853,37 @@ Points get_bed_shape(const PrintConfig &cfg, bool use_share) Points get_bed_shape(const SLAPrinterConfig &cfg) { return to_points(make_counter_clockwise(cfg.printable_area.values)); } +bool is_valid_bed_exclude_area(const Pointfs &points) +{ + if (points.size() < 3) + return false; + + for (const Vec2d &point : points) { + if (!std::isfinite(point.x()) || !std::isfinite(point.y())) + return false; + } + + const Polygon polygon = Polygon::new_scale(points); + if (has_duplicate_points(polygon)) + return false; + + const Points &vertices = polygon.points; + if (polygon.area() == 0.0) + return false; + + const size_t count = vertices.size(); + for (size_t idx = 0; idx + 2 < count; ++idx) { + const size_t end = idx == 0 ? count - 1 : count; + for (size_t other = idx + 2; other < end; ++other) { + if (Geometry::segments_intersect( + vertices[idx], vertices[idx + 1], vertices[other], vertices[(other + 1) % count])) + return false; + } + } + + return true; +} + namespace { static bool looks_like_bed_exclusion_volume_syntax(const std::string &value) @@ -13928,15 +13959,13 @@ static std::optional parse_exclude_polygon_points(const std::string &po return std::nullopt; points.emplace_back(x, y); } - if (points.size() < 3) + if (!is_valid_bed_exclude_area(points)) return std::nullopt; Polygon polygon; polygon.points.reserve(points.size()); for (const Vec2d &pt : points) polygon.points.emplace_back(scale_(pt.x()), scale_(pt.y())); - if (polygon.area() == 0.0) - return std::nullopt; polygon.make_counter_clockwise(); return polygon; } @@ -13946,7 +13975,7 @@ static void append_legacy_bed_exclude_regions( const Pointfs &legacy_points, const double printable_height) { - if (legacy_points.size() < 3) + if (!is_valid_bed_exclude_area(legacy_points)) return; Polygon polygon; diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 9196a3bbf3..56e1b39334 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -2576,6 +2576,8 @@ int bed_exclusion_extruder_for_filament(size_t filament_id, const std::vector 1 && !is_valid_bed_exclude_area(out_values)) { + wxString text_value; + if (const auto *previous_values = boost::any_cast>(&m_value); previous_values != nullptr) + text_value = from_u8(ConfigOptionPoints(*previous_values).serialize()); + set_value(text_value, true); + show_error(m_parent, _L("The excluded bed area is invalid. It must be a simple, non-self-intersecting polygon.")); + break; + } + m_value = out_values; break; } diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index e393877f70..def1e30f1c 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -8978,6 +8978,7 @@ std::vector Plater::priv::load_files(const std::vector& input_ bool dlg_cont = true; bool is_user_cancel = false; bool translate_old = false; + bool invalid_bed_exclude_area = false; int current_width = 0, current_depth = 0; double current_height = 0; @@ -9679,6 +9680,12 @@ std::vector Plater::priv::load_files(const std::vector& input_ config.apply(static_cast(FullPrintConfig::defaults())); // and place the loaded config over the base. config += std::move(config_loaded); + if (ConfigOptionPoints *bed_exclude_area = config.option("bed_exclude_area"); + bed_exclude_area != nullptr && bed_exclude_area->values.size() > 1 && + !is_valid_bed_exclude_area(bed_exclude_area->values)) { + bed_exclude_area->values.clear(); + invalid_bed_exclude_area = true; + } std::map validity = config.validate(); if (!validity.empty()) { BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ":" << __LINE__ << " " << boost::format("Param values in 3mf error: "); @@ -10568,6 +10575,10 @@ std::vector Plater::priv::load_files(const std::vector& input_ } q->schedule_background_process(true); q->mark_plate_toolbar_image_dirty(); + if (invalid_bed_exclude_area) { + GUI::show_error(q, _L("The excluded bed area is invalid. It must be a simple, non-self-intersecting polygon.") + + "\n" + _L("The invalid area has been removed, so please define it again using a valid polygon.")); + } return obj_idxs; } diff --git a/tests/libslic3r/test_exclusion_volumes.cpp b/tests/libslic3r/test_exclusion_volumes.cpp index d006e88b54..f4a170f464 100644 --- a/tests/libslic3r/test_exclusion_volumes.cpp +++ b/tests/libslic3r/test_exclusion_volumes.cpp @@ -102,6 +102,31 @@ TEST_CASE("Legacy exclusion polygons keep their point representation", "[Exclusi CHECK_FALSE(is_bed_exclusion_volume_syntax(option.serialize())); } +TEST_CASE("Excluded bed areas require simple polygons", "[ExclusionVolume][PrintConfig][Regression]") +{ + ConfigOptionPoints self_intersecting; + REQUIRE(self_intersecting.deserialize("0x0,6x0,1x220,6x220")); + CHECK_FALSE(is_valid_bed_exclude_area(self_intersecting.values)); + + DynamicPrintConfig invalid_config = DynamicPrintConfig::full_print_config(); + invalid_config.set_key_value("bed_exclude_area", self_intersecting.clone()); + CHECK(get_bed_excluded_regions(invalid_config).empty()); + + ConfigOptionPoints zero_area; + REQUIRE(zero_area.deserialize("0x0,6x0,0x220,6x220")); + CHECK_FALSE(is_valid_bed_exclude_area(zero_area.values)); + + ConfigOptionPoints valid; + REQUIRE(valid.deserialize("0x0,6x0,6x220,0x220")); + CHECK(is_valid_bed_exclude_area(valid.values)); + + DynamicPrintConfig valid_config = DynamicPrintConfig::full_print_config(); + valid_config.set_key_value("bed_exclude_area", valid.clone()); + const std::vector regions = get_bed_excluded_regions(valid_config); + REQUIRE(regions.size() == 1); + CHECK(regions.front().polygon.points.size() == valid.values.size()); +} + TEST_CASE("Belt material keep-outs are projected once without projecting collision volumes", "[ExclusionVolume][PrintConfig][Belt][Regression]") { const auto [belt, axis, angle, scale_x, scale_y] = GENERATE(table({