From ee11eb188fbe22c6fa43c2ec1ae9ef12249a4d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Br=C3=A6strup=20Sayoc?= Date: Mon, 3 Feb 2025 12:38:15 +0100 Subject: [PATCH] Remove magic number and hard type enums in DrawViewDimension.h - Remove currently present magic numbers - Hard type enums, so magic numbers can no longer be introduced. We don't want people to introduce magic numbers. --- src/Mod/TechDraw/App/DrawViewDimExtent.h | 2 +- src/Mod/TechDraw/App/DrawViewDimension.cpp | 43 +++++++++++----------- src/Mod/TechDraw/App/DrawViewDimension.h | 6 +-- src/Mod/TechDraw/App/LandmarkDimension.cpp | 2 +- src/Mod/TechDraw/App/LandmarkDimension.h | 2 +- src/Mod/TechDraw/Gui/TaskLinkDim.cpp | 4 +- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawViewDimExtent.h b/src/Mod/TechDraw/App/DrawViewDimExtent.h index 962b3e41cc..bb30a1ac49 100644 --- a/src/Mod/TechDraw/App/DrawViewDimExtent.h +++ b/src/Mod/TechDraw/App/DrawViewDimExtent.h @@ -51,7 +51,7 @@ public: App::DocumentObjectExecReturn *execute() override; - int getRefType() const override { return extent; } + RefType getRefType() const override { return RefType::extent; } PyObject *getPyObject() override; diff --git a/src/Mod/TechDraw/App/DrawViewDimension.cpp b/src/Mod/TechDraw/App/DrawViewDimension.cpp index a2e5244955..911bc62cf5 100644 --- a/src/Mod/TechDraw/App/DrawViewDimension.cpp +++ b/src/Mod/TechDraw/App/DrawViewDimension.cpp @@ -82,6 +82,7 @@ using namespace TechDraw; using namespace Part; using DU = DrawUtil; +using RefType = DrawViewDimension::RefType; //=========================================================================== // DrawViewDimension @@ -490,16 +491,16 @@ App::DocumentObjectExecReturn* DrawViewDimension::execute() ReferenceVector references = getEffectiveReferences(); if (Type.isValue("Distance") || Type.isValue("DistanceX") || Type.isValue("DistanceY")) { - if (getRefType() == oneEdge) { + if (getRefType() == RefType::oneEdge) { m_linearPoints = getPointsOneEdge(references); } - else if (getRefType() == twoEdge) { + else if (getRefType() == RefType::twoEdge) { m_linearPoints = getPointsTwoEdges(references); } - else if (getRefType() == twoVertex) { + else if (getRefType() == RefType::twoVertex) { m_linearPoints = getPointsTwoVerts(references); } - else if (getRefType() == vertexEdge) { + else if (getRefType() == RefType::vertexEdge) { m_linearPoints = getPointsEdgeVert(references); } m_hasGeometry = true; @@ -510,21 +511,21 @@ App::DocumentObjectExecReturn* DrawViewDimension::execute() m_hasGeometry = true; } else if (Type.isValue("Angle")) { - if (getRefType() != twoEdge) { + if (getRefType() != RefType::twoEdge) { throw Base::RuntimeError("Angle dimension has non-edge references"); } m_anglePoints = getAnglePointsTwoEdges(references); m_hasGeometry = true; } else if (Type.isValue("Angle3Pt")) { - if (getRefType() != threeVertex) { + if (getRefType() != RefType::threeVertex) { throw Base::RuntimeError("3 point angle dimension has non-vertex references"); } m_anglePoints = getAnglePointsThreeVerts(references); m_hasGeometry = true; } else if (Type.isValue("Area")) { - if (getRefType() != oneFace) { + if (getRefType() != RefType::oneFace) { throw Base::RuntimeError("area dimension has non-face references"); } m_areaPoint = getAreaParameters(references); @@ -1512,7 +1513,7 @@ ReferenceVector DrawViewDimension::getEffectiveReferences() const // what configuration of references do we have - Vertex-Vertex, Edge-Vertex, Edge, ... -int DrawViewDimension::getRefType() const +RefType DrawViewDimension::getRefType() const { if (isExtentDim()) { return RefType::extent; @@ -1534,7 +1535,7 @@ int DrawViewDimension::getRefType() const // something went wrong, there were no subNames. Base::Console().Message("DVD::getRefType - %s - there are no subNames.\n", getNameInDocument()); - return 0; + return RefType::invalidRef; } return getRefTypeSubElements(subNames); @@ -1542,9 +1543,9 @@ int DrawViewDimension::getRefType() const // TODO: Gui/DimensionValidators.cpp has almost the same code // decide what the reference configuration is by examining the names of the sub elements -int DrawViewDimension::getRefTypeSubElements(const std::vector& subElements) +RefType DrawViewDimension::getRefTypeSubElements(const std::vector& subElements) { - int refType{invalidRef}; + RefType refType{RefType::invalidRef}; int refEdges{0}; int refVertices{0}; int refFaces{0}; @@ -1562,22 +1563,22 @@ int DrawViewDimension::getRefTypeSubElements(const std::vector& sub } if (refEdges == 0 && refVertices == 2 && refFaces == 0) { - refType = twoVertex; + refType = RefType::twoVertex; } if (refEdges == 0 && refVertices == 3 && refFaces == 0) { - refType = threeVertex; + refType = RefType::threeVertex; } if (refEdges == 1 && refVertices == 0 && refFaces == 0) { - refType = oneEdge; + refType = RefType::oneEdge; } if (refEdges == 1 && refVertices == 1 && refFaces == 0) { - refType = vertexEdge; + refType = RefType::vertexEdge; } if (refEdges == 2 && refVertices == 0 && refFaces == 0) { - refType = twoEdge; + refType = RefType::twoEdge; } if (refEdges == 0 && refVertices == 0 && refFaces == 1) { - refType = oneFace; + refType = RefType::oneFace; } return refType; @@ -1830,14 +1831,14 @@ bool DrawViewDimension::validateReferenceForm() const } if (Type.isValue("Distance") || Type.isValue("DistanceX") || Type.isValue("DistanceY")) { - if (getRefType() == oneEdge) { + if (getRefType() == RefType::oneEdge) { if (references.size() != 1) { return false; } std::string subGeom = DrawUtil::getGeomTypeFromName(references.front().getSubName()); return subGeom == "Edge"; } - if (getRefType() == twoEdge) { + if (getRefType() == RefType::twoEdge) { if (references.size() != 2) { return false; } @@ -1846,7 +1847,7 @@ bool DrawViewDimension::validateReferenceForm() const return (subGeom0 == "Edge" && subGeom1 == "Edge"); } - if (getRefType() == twoVertex) { + if (getRefType() == RefType::twoVertex) { if (references.size() != 2) { return false; } @@ -1855,7 +1856,7 @@ bool DrawViewDimension::validateReferenceForm() const return (subGeom0 == "Vertex" && subGeom1 == "Vertex"); } - if (getRefType() == vertexEdge) { + if (getRefType() == RefType::vertexEdge) { if (references.size() != 2) { return false; } diff --git a/src/Mod/TechDraw/App/DrawViewDimension.h b/src/Mod/TechDraw/App/DrawViewDimension.h index 1493a9039d..a3f66bece5 100644 --- a/src/Mod/TechDraw/App/DrawViewDimension.h +++ b/src/Mod/TechDraw/App/DrawViewDimension.h @@ -99,7 +99,7 @@ public: App::PropertyBool ShowUnits; - enum RefType + enum class RefType { invalidRef, oneEdge, @@ -145,8 +145,8 @@ public: { return {0, 0, 1, 1}; } // pretend dimensions always fit! - virtual int getRefType() const; // Vertex-Vertex, Edge, Edge-Edge - static int + virtual RefType getRefType() const; // Vertex-Vertex, Edge, Edge-Edge + static RefType getRefTypeSubElements(const std::vector&); // Vertex-Vertex, Edge, Edge-Edge void setReferences2d(const ReferenceVector& refs); diff --git a/src/Mod/TechDraw/App/LandmarkDimension.cpp b/src/Mod/TechDraw/App/LandmarkDimension.cpp index d18d28f08c..152918bdd7 100644 --- a/src/Mod/TechDraw/App/LandmarkDimension.cpp +++ b/src/Mod/TechDraw/App/LandmarkDimension.cpp @@ -155,7 +155,7 @@ Base::Vector3d LandmarkDimension::projectPoint(const Base::Vector3d& pt, DrawVie return DrawUtil::invertY(result); } -int LandmarkDimension::getRefType() const +DrawViewDimension::RefType LandmarkDimension::getRefType() const { //TODO: need changes here when other reference dim types added return DrawViewDimension::RefType::twoVertex; diff --git a/src/Mod/TechDraw/App/LandmarkDimension.h b/src/Mod/TechDraw/App/LandmarkDimension.h index bf276412d2..54716f1ab5 100644 --- a/src/Mod/TechDraw/App/LandmarkDimension.h +++ b/src/Mod/TechDraw/App/LandmarkDimension.h @@ -57,7 +57,7 @@ public: return "TechDrawGui::ViewProviderDimension"; } DrawViewPart* getViewPart() const override; - int getRefType() const override; + RefType getRefType() const override; gp_Ax2 getProjAxis() const; diff --git a/src/Mod/TechDraw/Gui/TaskLinkDim.cpp b/src/Mod/TechDraw/Gui/TaskLinkDim.cpp index 78a4553b0f..6f3aede92a 100644 --- a/src/Mod/TechDraw/Gui/TaskLinkDim.cpp +++ b/src/Mod/TechDraw/Gui/TaskLinkDim.cpp @@ -90,12 +90,12 @@ void TaskLinkDim::loadAvailDims() return; std::string result; - int selRefType = TechDraw::DrawViewDimension::getRefTypeSubElements(m_subs); + TechDraw::DrawViewDimension::RefType selRefType = TechDraw::DrawViewDimension::getRefTypeSubElements(m_subs); //int found = 0; for (auto* view : m_page->getViews()) { if (view->isDerivedFrom()) { auto* dim = static_cast(view); - int dimRefType = dim->getRefType(); + TechDraw::DrawViewDimension::RefType dimRefType = dim->getRefType(); if (dimRefType == selRefType) { //potential matches // found++; if (dim->has3DReferences()) {