From d9ea13ddec67c6fbcfcdec4acf98ff6549d52d58 Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Mon, 15 Jan 2024 18:03:30 -0600 Subject: [PATCH] Part/Toponaming: Refactor to eliminate boolean blindness --- src/Mod/Part/App/TopoShape.h | 20 +++++++++++------- src/Mod/Part/App/TopoShapeExpansion.cpp | 28 +++++++++++++------------ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/Mod/Part/App/TopoShape.h b/src/Mod/Part/App/TopoShape.h index 1ec4890584..eaba85f014 100644 --- a/src/Mod/Part/App/TopoShape.h +++ b/src/Mod/Part/App/TopoShape.h @@ -616,15 +616,21 @@ public: void mapSubElementsTo(std::vector &shapes, const char *op=nullptr) const; bool hasPendingElementMap() const; + /** + * When given a single shape to create a compound, two results are possible: either to simply + * return the shape as given, or to force it to be placed in a Compound. + */ + enum class SingleShapeCompoundCreationPolicy { + RETURN_SHAPE, + FORCE_COMPOUND + }; /** Make a compound shape * * @param shapes: input shapes * @param op: optional string to be encoded into topo naming for indicating * the operation - * @param force: if true and there is only one input shape, then return - * that shape instead. If false, then always return a - * compound, even if there is no input shape. + * @param policy: set behavior when only a single shape is given * * @return The original content of this TopoShape is discarded and replaced * with the new shape. The function returns the TopoShape itself as @@ -633,7 +639,7 @@ public: */ TopoShape& makeElementCompound(const std::vector& shapes, const char* op = nullptr, - bool force = true); + SingleShapeCompoundCreationPolicy policy = SingleShapeCompoundCreationPolicy::FORCE_COMPOUND); enum class ConnectionPolicy { @@ -666,7 +672,7 @@ public: TopoShape& makeElementWires(const std::vector& shapes, const char* op = nullptr, double tol = 0.0, - bool shared = false, + ConnectionPolicy policy = ConnectionPolicy::MERGE_WITH_TOLERANCE, TopoShapeMap* output = nullptr); @@ -743,10 +749,10 @@ public: */ TopoShape makeElementWires(const char* op = nullptr, double tol = 0.0, - bool shared = false, + ConnectionPolicy policy = ConnectionPolicy::MERGE_WITH_TOLERANCE, TopoShapeMap* output = nullptr) const { - return TopoShape(0, Hasher).makeElementWires(*this, op, tol, shared, output); + return TopoShape(0, Hasher).makeElementWires(*this, op, tol, policy, output); } friend class TopoShapeCache; diff --git a/src/Mod/Part/App/TopoShapeExpansion.cpp b/src/Mod/Part/App/TopoShapeExpansion.cpp index 9e9bfa4e64..24dd9e1792 100644 --- a/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -25,7 +25,7 @@ #include "PreCompiled.h" #ifndef _PreComp_ -#include +#include #include #include @@ -533,9 +533,11 @@ void addShapesToBuilder(const std::vector& shapes, } // namespace TopoShape& -TopoShape::makeElementCompound(const std::vector& shapes, const char* op, bool force) +TopoShape::makeElementCompound(const std::vector& shapes, + const char* op, + SingleShapeCompoundCreationPolicy policy) { - if (!force && shapes.size() == 1) { + if (policy == SingleShapeCompoundCreationPolicy::RETURN_SHAPE && shapes.size() == 1) { *this = shapes[0]; return *this; } @@ -559,23 +561,23 @@ TopoShape::makeElementCompound(const std::vector& shapes, const char* TopoShape& TopoShape::makeElementWires(const std::vector& shapes, const char* op, double tol, - bool shared, + ConnectionPolicy policy, TopoShapeMap* output) { if (shapes.empty()) { FC_THROWM(NullShapeException, "Null shape"); } if (shapes.size() == 1) { - return makeElementWires(shapes[0], op, tol, shared, output); + return makeElementWires(shapes[0], op, tol, policy, output); } - return makeElementWires(TopoShape(Tag).makeElementCompound(shapes), op, tol, shared, output); + return makeElementWires(TopoShape(Tag).makeElementCompound(shapes), op, tol, policy, output); } TopoShape& TopoShape::makeElementWires(const TopoShape& shape, const char* op, double tol, - bool shared, + ConnectionPolicy policy, TopoShapeMap* output) { if (!op) { @@ -585,7 +587,7 @@ TopoShape& TopoShape::makeElementWires(const TopoShape& shape, tol = Precision::Confusion(); } - if (shared) { + if (policy == ConnectionPolicy::REQUIRE_SHARED_VERTEX) { // Can't use ShapeAnalysis_FreeBounds if not shared. It seems the output // edges are modified somehow, and it is not obvious how to map the // resulting edges. @@ -605,10 +607,10 @@ TopoShape& TopoShape::makeElementWires(const TopoShape& shape, std::vector wires; for (int i = 1; i <= hWires->Length(); i++) { auto wire = hWires->Value(i); - wires.push_back(TopoShape(Tag, Hasher, wire)); + wires.emplace_back(Tag, Hasher, wire); } shape.mapSubElementsTo(wires, op); - return makeElementCompound(wires, "", false); + return makeElementCompound(wires, "", SingleShapeCompoundCreationPolicy::RETURN_SHAPE); } std::vector wires; @@ -666,7 +668,7 @@ TopoShape& TopoShape::makeElementWires(const TopoShape& shape, wires.back().mapSubElement(edges, op); wires.back().fix(); } - return makeElementCompound(wires, nullptr, false); + return makeElementCompound(wires, nullptr, SingleShapeCompoundCreationPolicy::RETURN_SHAPE); } @@ -833,7 +835,7 @@ TopoShape& TopoShape::makeElementOrderedWires(const std::vector& shap std::vector wires; std::list edgeList; - auto shape = TopoShape().makeElementCompound(shapes, "", false); + auto shape = TopoShape().makeElementCompound(shapes, "", SingleShapeCompoundCreationPolicy::RETURN_SHAPE); for (auto& edge : shape.getSubTopoShapes(TopAbs_EDGE)) { edgeList.push_back(edge); } @@ -855,7 +857,7 @@ TopoShape& TopoShape::makeElementOrderedWires(const std::vector& shap wires.emplace_back(mkWire.Wire()); wires.back().mapSubElement(edges, op); } - return makeElementCompound(wires, nullptr, false); + return makeElementCompound(wires, nullptr, SingleShapeCompoundCreationPolicy::RETURN_SHAPE); } } // namespace Part