diff --git a/src/Mod/Part/App/TopoShapePy.xml b/src/Mod/Part/App/TopoShapePy.xml index 357a7be4cf..96c9ef7aa0 100644 --- a/src/Mod/Part/App/TopoShapePy.xml +++ b/src/Mod/Part/App/TopoShapePy.xml @@ -813,8 +813,12 @@ infos contains additional info on the solutions. It is a list of tuples: - Returns a SubElement -getElement(elementName) -> Face | Edge | Vertex + +Returns a SubElement +getElement(elementName, [silent = False]) -> Face | Edge | Vertex +elementName: SubElement name - i.e. 'Edge1', 'Face3' etc. + Accepts TNP mitigation mapped names as well +silent: True to suppress the exception throw if the shape isn't found. diff --git a/src/Mod/Part/App/TopoShapePyImp.cpp b/src/Mod/Part/App/TopoShapePyImp.cpp index a556600cf5..a6678b9d6a 100644 --- a/src/Mod/Part/App/TopoShapePyImp.cpp +++ b/src/Mod/Part/App/TopoShapePyImp.cpp @@ -2602,35 +2602,18 @@ PyObject* TopoShapePy::removeSplitter(PyObject *args) PyObject* TopoShapePy::getElement(PyObject *args) { char* input; - if (!PyArg_ParseTuple(args, "s", &input)) + PyObject* silent = Py_False; + if (!PyArg_ParseTuple(args, "s|O", &input, &silent)) { return nullptr; - - boost::regex ex("^(Face|Edge|Vertex)[1-9][0-9]*$"); - + } try { - 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; - } + PyObject* res = getTopoShapePtr()->getPySubShape(input, PyObject_IsTrue(silent)); + if (!res) { + Py_Return; } - - PyErr_SetString(PyExc_ValueError, "Invalid subelement name"); - return nullptr; - } - catch (Standard_Failure& e) { - PyErr_SetString(PartExceptionOCCError, e.GetMessageString()); - return nullptr; + return res; } + PY_CATCH_OCC } PyObject* TopoShapePy::countElement(PyObject *args) diff --git a/src/Mod/PartDesign/PartDesignTests/TestTopologicalNamingProblem.py b/src/Mod/PartDesign/PartDesignTests/TestTopologicalNamingProblem.py index 4d1d2acdc5..4e628ace44 100644 --- a/src/Mod/PartDesign/PartDesignTests/TestTopologicalNamingProblem.py +++ b/src/Mod/PartDesign/PartDesignTests/TestTopologicalNamingProblem.py @@ -1772,6 +1772,28 @@ class TestTopologicalNamingProblem(unittest.TestCase): else: self.assertEqual(App.Gui.Selection.getSelectionEx("", 0)[0].SubElementNames[0][-8:],",F.Face2") + def testGetElementFunctionality(self): + # Arrange + body = self.Doc.addObject('PartDesign::Body', 'Body') + padSketch = self.Doc.addObject('Sketcher::SketchObject', 'SketchPad') + pad = self.Doc.addObject("PartDesign::Pad", "Pad") + body.addObject(padSketch) + body.addObject(pad) + TestSketcherApp.CreateRectangleSketch(padSketch, (0, 0), (1, 1)) + pad.Profile = padSketch + pad.Length = 1 + # Act + self.Doc.recompute() + if pad.Shape.ElementMapVersion == "": # Should be '4' as of Mar 2023. + return + map = pad.Shape.ElementMap + # Assert + self.assertGreater(pad.Shape.ElementMapSize,0) + for tnpName in map.keys(): + element1 = pad.Shape.getElement(tnpName) + element2 = pad.Shape.getElement(map[tnpName]) + self.assertTrue(element1.isSame(element2)) + def testFileSaveRestore(self): # Arrange self.Body = self.Doc.addObject('PartDesign::Body', 'Body')