Toposhape/Part: Bring in element methods in FeaturePart, TopoShapePy::Init and TopoShape::GetPyObject

This commit is contained in:
Zheng, Lei
2024-02-27 15:12:25 -05:00
committed by bgbsww
parent 2e96599903
commit ae5c0a6d9c
4 changed files with 445 additions and 20 deletions

View File

@@ -67,6 +67,7 @@
#endif
#include <App/PropertyStandard.h>
#include <App/StringHasherPy.h>
#include <Base/FileInfo.h>
#include <Base/GeometryPyCXX.h>
#include <Base/MatrixPy.h>
@@ -85,6 +86,7 @@
#include <Mod/Part/App/TopoShapeCompSolidPy.h>
#include <Mod/Part/App/TopoShapeEdgePy.h>
#include <Mod/Part/App/TopoShapeFacePy.h>
#include <Mod/Part/App/TopoShapeOpCode.h>
#include <Mod/Part/App/TopoShapeShellPy.h>
#include <Mod/Part/App/TopoShapeSolidPy.h>
#include <Mod/Part/App/TopoShapeVertexPy.h>
@@ -105,6 +107,24 @@ using namespace Part;
#define M_PI_2 1.57079632679489661923 /* pi/2 */
#endif
static Py_hash_t _TopoShapeHash(PyObject *self) {
if (!self) {
PyErr_SetString(PyExc_TypeError, "descriptor 'hash' of 'Part.TopoShape' object needs an argument");
return 0;
}
if (!static_cast<Base::PyObjectBase*>(self)->isValid()) {
PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
return 0;
}
return static_cast<TopoShapePy*>(self)->getTopoShapePtr()->getShape().HashCode(INT_MAX);
}
struct TopoShapePyInit {
TopoShapePyInit() {
TopoShapePy::Type.tp_hash = _TopoShapeHash;
}
} _TopoShapePyInit;
// returns a string which represents the object e.g. when printed in python
std::string TopoShapePy::representation() const
{
@@ -120,18 +140,68 @@ PyObject *TopoShapePy::PyMake(struct _typeobject *, PyObject *, PyObject *) //
return new TopoShapePy(new TopoShape);
}
int TopoShapePy::PyInit(PyObject* args, PyObject*)
int TopoShapePy::PyInit(PyObject* args, PyObject* keywds)
{
PyObject *pcObj=nullptr;
if (!PyArg_ParseTuple(args, "|O", &pcObj))
#ifdef FC_USE_TNP_FIX
static char* kwlist[] = {"shape", "op", "tag", "hasher", nullptr};
long tag = 0;
PyObject* pyHasher = nullptr;
const char* op = nullptr;
PyObject* pcObj = nullptr;
if (!PyArg_ParseTupleAndKeywords(args,
keywds,
"|OsiO!",
kwlist,
&pcObj,
&op,
&tag,
&App::StringHasherPy::Type,
&pyHasher)) {
return -1;
}
auto& self = *getTopoShapePtr();
self.Tag = tag;
if (pyHasher) {
self.Hasher = static_cast<App::StringHasherPy*>(pyHasher)->getStringHasherPtr();
}
auto shapes = getPyShapes(pcObj);
PY_TRY
{
if (shapes.size() == 1 && !op) {
auto s = shapes.front();
if (self.Tag) {
if ((s.Tag && self.Tag != s.Tag)
|| (self.Hasher && s.getElementMapSize() && self.Hasher != s.Hasher)) {
s.reTagElementMap(self.Tag, self.Hasher);
}
else {
s.Tag = self.Tag;
s.Hasher = self.Hasher;
}
}
self = s;
}
else if (shapes.size()) {
if (!op) {
op = Part::OpCodes::Fuse;
}
self.makeElementBoolean(op, shapes);
}
}
_PY_CATCH_OCC(return (-1))
#else
PyObject* pcObj = nullptr;
if (!PyArg_ParseTuple(args, "|O", &pcObj)) {
return -1;
}
auto shapes = getPyShapes(pcObj);
if (pcObj) {
TopoShape shape;
PY_TRY {
if (PyObject_TypeCheck(pcObj,&TopoShapePy::Type)) {
PY_TRY
{
if (PyObject_TypeCheck(pcObj, &TopoShapePy::Type)) {
shape = *static_cast<TopoShapePy*>(pcObj)->getTopoShapePtr();
}
else {
@@ -139,8 +209,8 @@ int TopoShapePy::PyInit(PyObject* args, PyObject*)
bool first = true;
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(Part::GeometryPy::Type))) {
TopoDS_Shape sh = static_cast<GeometryPy*>((*it).ptr())->
getGeometryPtr()->toShape();
TopoDS_Shape sh =
static_cast<GeometryPy*>((*it).ptr())->getGeometryPtr()->toShape();
if (first) {
first = false;
shape.setShape(sh);
@@ -152,11 +222,11 @@ int TopoShapePy::PyInit(PyObject* args, PyObject*)
}
}
}
_PY_CATCH_OCC(return(-1))
_PY_CATCH_OCC(return (-1))
getTopoShapePtr()->setShape(shape.getShape());
}
#endif
return 0;
}