Merge pull request #15177 from shaise/Fix-Python-getElement

[TNP] Fix python get element. partially fixes #15095
This commit is contained in:
Chris Hennes
2024-07-13 20:53:03 -05:00
committed by GitHub
3 changed files with 36 additions and 27 deletions

View File

@@ -813,8 +813,12 @@ infos contains additional info on the solutions. It is a list of tuples:
</Methode>
<Methode Name="getElement" Const="true">
<Documentation>
<UserDocu>Returns a SubElement
getElement(elementName) -> Face | Edge | Vertex
<UserDocu>
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.
</UserDocu>
</Documentation>
</Methode>

View File

@@ -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<Part::ShapeSegment> s(static_cast<Part::ShapeSegment*>
(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)

View File

@@ -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')