From 36c9201ffc74dc652312a38dcbcfc1c151217f46 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 17 Mar 2025 11:20:19 +0100 Subject: [PATCH] add From_PyOCC function to convert swig pyocc shape to netgen.occ --- libsrc/occ/python_occ_shapes.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/libsrc/occ/python_occ_shapes.cpp b/libsrc/occ/python_occ_shapes.cpp index 77119135..4b6fa7b0 100644 --- a/libsrc/occ/python_occ_shapes.cpp +++ b/libsrc/occ/python_occ_shapes.cpp @@ -689,6 +689,33 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m) OCCGeometry::global_shape_properties.clear(); OCCGeometry::global_shape_property_indices.Clear(); }); + + struct SwigTypeInfo + { + const char* name; // SWIG's type name string + // Other fields... + }; + + struct SwigPyObject{ + PyObject_HEAD + void *ptr; + SwigTypeInfo* ty; // SWIG type information + int own; // ownership flag + }; + + m.def("From_PyOCC", [](py::object shape) + { + py::object py_this = shape.attr("this"); + PyObject* obj = py_this.ptr(); + SwigPyObject* swig_obj = reinterpret_cast(obj); + cout << "swig type = " << swig_obj->ty->name << endl; + if (!swig_obj->ptr || !swig_obj->ty || !swig_obj->ty->name) { + throw std::runtime_error("SWIG object does not contain a valid pointer"); + } + if(strcmp(swig_obj->ty->name, "_p_TopoDS_Shape") != 0) + throw std::runtime_error("Does not contain TopoDS_Shape from pyocc!"); + return py::cast(static_cast(swig_obj->ptr)); + }, py::return_value_policy::reference, py::keep_alive<0,1>()); py::class_ (m, "TopoDS_Shape") .def("__str__", [] (const TopoDS_Shape & shape)