From c01dfad1309b9affdc0178ff29e93ffb1d6233f7 Mon Sep 17 00:00:00 2001 From: marioalexis Date: Mon, 11 Apr 2022 02:01:39 -0300 Subject: [PATCH] Part: Fix getElement method in Python TopoShape class --- src/Mod/Part/App/PreCompiled.h | 1 + src/Mod/Part/App/TopoShapePyImp.cpp | 43 +++++++++++++++-------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/Mod/Part/App/PreCompiled.h b/src/Mod/Part/App/PreCompiled.h index e6cf1ef526..a411fe9bca 100644 --- a/src/Mod/Part/App/PreCompiled.h +++ b/src/Mod/Part/App/PreCompiled.h @@ -80,6 +80,7 @@ #include #include +#include #include #include "OpenCascadeAll.h" diff --git a/src/Mod/Part/App/TopoShapePyImp.cpp b/src/Mod/Part/App/TopoShapePyImp.cpp index d44665c68e..b19c8ac1c2 100644 --- a/src/Mod/Part/App/TopoShapePyImp.cpp +++ b/src/Mod/Part/App/TopoShapePyImp.cpp @@ -24,6 +24,7 @@ #include "PreCompiled.h" #ifndef _PreComp_ # include +# include # include # include # include @@ -2268,34 +2269,34 @@ PyObject* TopoShapePy::getElement(PyObject *args) char* input; if (!PyArg_ParseTuple(args, "s", &input)) return nullptr; - std::string name(input); - + + boost::regex ex("^(Face|Edge|Vertex)[1-9][0-9]*$"); + try { - if (name.size() > 4 && name.substr(0,4) == "Face" && name[4]>=48 && name[4]<=57) { - std::unique_ptr s(static_cast - (getTopoShapePtr()->getSubElementByName(input))); - TopoDS_Shape Shape = s->Shape; - return new TopoShapeFacePy(new TopoShape(Shape)); - } - else if (name.size() > 4 && name.substr(0,4) == "Edge" && name[4]>=48 && name[4]<=57) { - std::unique_ptr s(static_cast - (getTopoShapePtr()->getSubElementByName(input))); - TopoDS_Shape Shape = s->Shape; - return new TopoShapeEdgePy(new TopoShape(Shape)); - } - else if (name.size() > 6 && name.substr(0,6) == "Vertex" && name[6]>=48 && name[6]<=57) { - std::unique_ptr s(static_cast - (getTopoShapePtr()->getSubElementByName(input))); - TopoDS_Shape Shape = s->Shape; - return new TopoShapeVertexPy(new TopoShape(Shape)); + if (boost::regex_match(input, ex)) { + std::unique_ptr s(static_cast + (getTopoShapePtr()->getSubElementByName(input))); + TopoDS_Shape shape = s->Shape; + switch (shape.ShapeType()) { + case TopAbs_FACE: + return new TopoShapeFacePy(new TopoShape(shape)); + case TopAbs_EDGE: + return new TopoShapeEdgePy(new TopoShape(shape)); + case TopAbs_VERTEX: + return new TopoShapeVertexPy(new TopoShape(shape)); + default: + break; + } } } catch (Standard_Failure& e) { - PyErr_SetString(PartExceptionOCCError, e.GetMessageString()); return nullptr; } - return nullptr; + + PyErr_SetString(PyExc_ValueError, "Invalid subelement name"); + + return nullptr; } PyObject* TopoShapePy::countElement(PyObject *args)