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.
This commit is contained in:
Benjamin Bræstrup Sayoc
2025-02-03 12:38:15 +01:00
parent 6184ed19aa
commit ee11eb188f
6 changed files with 30 additions and 29 deletions

View File

@@ -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;

View File

@@ -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<std::string>& subElements)
RefType DrawViewDimension::getRefTypeSubElements(const std::vector<std::string>& 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<std::string>& 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;
}

View File

@@ -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<std::string>&); // Vertex-Vertex, Edge, Edge-Edge
void setReferences2d(const ReferenceVector& refs);

View File

@@ -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;

View File

@@ -57,7 +57,7 @@ public:
return "TechDrawGui::ViewProviderDimension"; }
DrawViewPart* getViewPart() const override;
int getRefType() const override;
RefType getRefType() const override;
gp_Ax2 getProjAxis() const;

View File

@@ -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<TechDraw::DrawViewDimension>()) {
auto* dim = static_cast<TechDraw::DrawViewDimension*>(view);
int dimRefType = dim->getRefType();
TechDraw::DrawViewDimension::RefType dimRefType = dim->getRefType();
if (dimRefType == selRefType) { //potential matches
// found++;
if (dim->has3DReferences()) {