diff --git a/src/slic3r/GUI/CAD/DesignSketchTool.cpp b/src/slic3r/GUI/CAD/DesignSketchTool.cpp index 48bb56bb31..6551b6c608 100644 --- a/src/slic3r/GUI/CAD/DesignSketchTool.cpp +++ b/src/slic3r/GUI/CAD/DesignSketchTool.cpp @@ -10677,8 +10677,11 @@ DesignSketchTool::LoopReport DesignSketchTool::loop_report(const std::vector 2) a += r.poly.back().x() * r.poly.front().y() - r.poly.front().x() * r.poly.back().y(); - li.area = 0.5 * a; + li.area = std::abs(0.5 * a); } } out.loops.push_back(std::move(li)); diff --git a/src/slic3r/GUI/CAD/DesignSketchTool.hpp b/src/slic3r/GUI/CAD/DesignSketchTool.hpp index 0400b141bf..d1a2c56f3d 100644 --- a/src/slic3r/GUI/CAD/DesignSketchTool.hpp +++ b/src/slic3r/GUI/CAD/DesignSketchTool.hpp @@ -632,7 +632,7 @@ public: std::vector ents; // entities of this loop, in chain order std::vector holes; // indices into LoopReport::loops that this loop encloses bool closed{false}; - double area{0.0}; // signed shoelace area of the loop polyline + double area{0.0}; // area the loop encloses (mm^2), its holes not taken out bool defect{false}; // crosses or folds back on itself (see RegionLoop) Vec2d defect_at{0, 0}; }; diff --git a/tests/slic3rutils/test_mcp_cad.cpp b/tests/slic3rutils/test_mcp_cad.cpp index cefe1a1641..979a6bb331 100644 --- a/tests/slic3rutils/test_mcp_cad.cpp +++ b/tests/slic3rutils/test_mcp_cad.cpp @@ -2213,7 +2213,7 @@ double only_loop_area(const GUI::DesignSketchTool& tool) const GUI::DesignSketchTool::LoopReport rep = tool.loop_report(); REQUIRE(rep.loops.size() == 1); CHECK(rep.open_ends.empty()); - return std::abs(rep.loops[0].area); + return rep.loops[0].area; } BoundingBoxf entities_box(const std::vector& ents, size_t first, size_t count) @@ -2273,7 +2273,7 @@ TEST_CASE("sketch_add draws polygons, slots, ellipses and splines as their tools for (const auto& l : rep.loops) if (l.ents.size() == 4) { slot_found = true; - CHECK_THAT(std::abs(l.area), WithinAbs(20.0 * 5.0 + PI * 2.5 * 2.5, 1e-6)); + CHECK_THAT(l.area, WithinAbs(20.0 * 5.0 + PI * 2.5 * 2.5, 1e-6)); } CHECK(slot_found); const SketchEntity& el = ents[5]; @@ -2488,6 +2488,27 @@ TEST_CASE("sketch_transform moves, rotates, scales and arrays as the transform t CHECK(ents.size() == n); } +TEST_CASE("a loop reports the area it encloses whichever way it runs", "[Mcp][CAD]") +{ + GUI::DesignSketchTool tool; + begin_sketch(tool); + // A 10 mm square drawn clockwise (entities 0-3), and a slot, which the slot tool lays out + // clockwise whichever way its centres are given (4-7). + sketch(tool, "sketch_add", {{"entities", + {{{"type", "line"}, {"p0", {0, 0}}, {"p1", {0, 10}}}, + {{"type", "line"}, {"p0", {0, 10}}, {"p1", {10, 10}}}, + {{"type", "line"}, {"p0", {10, 10}}, {"p1", {10, 0}}}, + {{"type", "line"}, {"p0", {10, 0}}, {"p1", {0, 0}}}, + {{"type", "slot"}, {"c0", {30, 0}}, {"c1", {50, 0}}, {"width", 6}}}}}); + // The report sketch_validate and sketch_describe print as closed_loops. + const GUI::DesignSketchTool::LoopReport rep = tool.loop_report(); + REQUIRE(rep.loops.size() == 2); + for (const auto& l : rep.loops) { + const bool square = l.ents.at(0) < 4; + CHECK_THAT(l.area, WithinAbs(square ? 10.0 * 10.0 : 20.0 * 6.0 + PI * 3.0 * 3.0, 1e-6)); + } +} + TEST_CASE("constraints are planned from picks as the CONSTRAIN bar does, or taken from refs as written", "[Mcp][CAD]") { using T = SketchConstraintType;