From 04491c32edd7cdcdef0bffd1000065d14b439af1 Mon Sep 17 00:00:00 2001 From: bgbsww Date: Thu, 15 Feb 2024 10:41:45 -0500 Subject: [PATCH] Toponaming/Part: Clean up linearize, add tests, and some delinting --- src/Mod/Part/App/TopoShape.h | 36 ++++++++++---- src/Mod/Part/App/TopoShapeExpansion.cpp | 7 +-- tests/src/Mod/Part/App/TopoShapeExpansion.cpp | 47 +++++++++++++++++++ 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/Mod/Part/App/TopoShape.h b/src/Mod/Part/App/TopoShape.h index 25b6790111..09e1e72723 100644 --- a/src/Mod/Part/App/TopoShape.h +++ b/src/Mod/Part/App/TopoShape.h @@ -59,10 +59,11 @@ namespace Part struct ShapeHasher; class TopoShape; class TopoShapeCache; -typedef std::unordered_map TopoShapeMap; +using TopoShapeMap = std::unordered_map; /* A special sub-class to indicate null shapes */ +// NOLINTNEXTLINE cppcoreguidelines-special-member-functions class PartExport NullShapeException: public Base::ValueError { public: @@ -76,6 +77,7 @@ public: /* A special sub-class to indicate boolean failures */ +// NOLINTNEXTLINE cppcoreguidelines-special-member-functions class PartExport BooleanException: public Base::CADKernelError { public: @@ -89,6 +91,7 @@ public: class PartExport ShapeSegment: public Data::Segment { + // NOLINTNEXTLINE cppcoreguidelines-avoid-non-const-global-variables TYPESYSTEM_HEADER_WITH_OVERRIDE(); public: @@ -124,17 +127,32 @@ enum class CheckGeometry ignoreGeometry, checkGeometry }; + +enum class LinearizeFace +{ + noFaces, + linearizeFaces +}; + +enum class LinearizeEdge +{ + noEdges, + linearizeEdges +}; + /** The representation for a CAD Shape */ +// NOLINTNEXTLINE cppcoreguidelines-special-member-functions class PartExport TopoShape: public Data::ComplexGeoData { + // NOLINTNEXTLINE cppcoreguidelines-avoid-non-const-global-variables TYPESYSTEM_HEADER_WITH_OVERRIDE(); public: - TopoShape(long Tag=0, + TopoShape(long Tag=0, // NOLINT google-explicit-constructor App::StringHasherRef hasher=App::StringHasherRef(), const TopoDS_Shape &shape=TopoDS_Shape()); // Cannot be made explicit - TopoShape(const TopoDS_Shape&, + TopoShape(const TopoDS_Shape&, // NOLINT google-explicit-constructor long Tag=0, App::StringHasherRef hasher=App::StringHasherRef()); // Cannot be made explicit TopoShape(const TopoShape&); @@ -203,7 +221,7 @@ public: uint16_t flags = 0) const override; void setFaces(const std::vector& Points, const std::vector& faces, - double tolerance = 1.0e-06); + double tolerance = 1.0e-06); // NOLINT void getDomains(std::vector&) const; //@} @@ -219,13 +237,13 @@ public: std::vector getElementTypes() const override; unsigned long countSubElements(const char* Type) const override; /// get the subelement by type and number - Data::Segment* getSubElement(const char* Type, unsigned long) const override; + Data::Segment* getSubElement(const char* Type, unsigned long index) const override; /** Get lines from segment */ - void getLinesFromSubElement(const Data::Segment*, + void getLinesFromSubElement(const Data::Segment* segment, std::vector& Points, std::vector& lines) const override; /** Get faces from segment */ - void getFacesFromSubElement(const Data::Segment*, + void getFacesFromSubElement(const Data::Segment* segment, std::vector& Points, std::vector& PointNormals, std::vector& faces) const override; @@ -273,7 +291,7 @@ public: /// get the Topo"sub"Shape with the given name PyObject* getPySubShape(const char* Type, bool silent = false) const; PyObject* getPyObject() override; - void setPyObject(PyObject*) override; + void setPyObject(PyObject* obj) override; /** @name Save/restore */ //@{ @@ -688,7 +706,7 @@ public: * * @return Return true if the shape is modified */ - bool linearize(bool face, bool edge); + bool linearize(LinearizeFace face, LinearizeEdge edge); static TopAbs_ShapeEnum shapeType(const char* type, bool silent = false); static TopAbs_ShapeEnum shapeType(char type, bool silent = false); diff --git a/src/Mod/Part/App/TopoShapeExpansion.cpp b/src/Mod/Part/App/TopoShapeExpansion.cpp index 4da7445a77..f77357c39f 100644 --- a/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -2521,13 +2521,14 @@ bool TopoShape::isPlanarFace(double tol) const tol); } -bool TopoShape::linearize(bool face, bool edge) +// TODO: Refactor this into two methods. Totally separate concerns here. +bool TopoShape::linearize(LinearizeFace do_face, LinearizeEdge do_edge) { bool touched = false; BRep_Builder builder; // Note: changing edge geometry seems to mess up with face (or shell, or solid) // Probably need to do some fix afterwards. - if (edge) { + if (do_edge == LinearizeEdge::linearizeEdges) { for (auto& edge : getSubTopoShapes(TopAbs_EDGE)) { TopoDS_Edge e = TopoDS::Edge(edge.getShape()); BRepAdaptor_Curve curve(e); @@ -2546,7 +2547,7 @@ bool TopoShape::linearize(bool face, bool edge) } } } - if (face) { + if (do_face == LinearizeFace::linearizeFaces) { for (auto& face : getSubTopoShapes(TopAbs_FACE)) { TopoDS_Face f = TopoDS::Face(face.getShape()); BRepAdaptor_Surface surf(f); diff --git a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp index 84279ed41a..25f81a5b6a 100644 --- a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -7,12 +7,17 @@ #include "PartTestHelpers.h" +#include +#include #include #include #include #include #include #include +#include +#include +#include #include #include #include @@ -1111,4 +1116,46 @@ TEST_F(TopoShapeExpansionTest, makeElementDraftTopoShapes) EXPECT_EQ(result3.getElementMap().size(), 0); // No element map in non reference call. } +TEST_F(TopoShapeExpansionTest, makeElementLinearizeEdge) +{ + // Arrange + TColgp_Array1OfPnt points {1, 2}; + points.SetValue(1, gp_Pnt(0.0, 0.0, 0.0)); + points.SetValue(2, gp_Pnt(1.0, 0.0, 0.0)); + auto line1 = new Geom_BezierCurve(points); + auto edge1 = BRepBuilderAPI_MakeEdge(line1).Edge(); + TopoShape topoShape1 {edge1, 1L}; + // Act + auto edges = topoShape1.getSubTopoShapes(TopAbs_EDGE); + BRepAdaptor_Curve curve(TopoDS::Edge(edges.front().getShape())); + topoShape1.linearize(LinearizeFace::noFaces, LinearizeEdge::linearizeEdges); + auto edges2 = topoShape1.getSubTopoShapes(TopAbs_EDGE); + BRepAdaptor_Curve curve2(TopoDS::Edge(edges2.front().getShape())); + // Assert + EXPECT_EQ(curve.GetType(), GeomAbs_BezierCurve); + EXPECT_EQ(curve2.GetType(), GeomAbs_Line); +} + +TEST_F(TopoShapeExpansionTest, makeElementLinearizeFace) +{ + TColgp_Array2OfPnt points2 {1, 2, 1, 2}; + points2.SetValue(1, 1, gp_Pnt(0.0, 0.0, 0.0)); + points2.SetValue(2, 1, gp_Pnt(1.0, 0.0, 0.0)); + points2.SetValue(1, 2, gp_Pnt(0.0, 1.0, 0.0)); + points2.SetValue(2, 2, gp_Pnt(1.0, 1.0, 0.0)); + auto face1 = new Geom_BezierSurface(points2); + auto surf1 = BRepBuilderAPI_MakeFace(face1, 0.1).Face(); + TopoShape topoShape2 {surf1, 2L}; + // Act + auto faces = topoShape2.getSubTopoShapes(TopAbs_FACE); + BRepAdaptor_Surface surface(TopoDS::Face(faces.front().getShape())); + topoShape2.linearize(LinearizeFace::linearizeFaces, LinearizeEdge::noEdges); + auto faces2 = topoShape2.getSubTopoShapes(TopAbs_FACE); + BRepAdaptor_Surface surface2(TopoDS::Face(faces.front().getShape())); + // Assert + EXPECT_EQ(surface.GetType(), GeomAbs_BezierSurface); + EXPECT_EQ(surface2.GetType(), GeomAbs_Plane); +} + + // NOLINTEND(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers)