From 0c611c339cfc51cfca0f4de06556f030641b6a23 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Wed, 31 May 2017 19:38:17 +0200 Subject: [PATCH] Python opencascade interface (by Gerhard Kitzler) --- libsrc/occ/CMakeLists.txt | 2 +- libsrc/occ/occgenmesh.cpp | 13 +++++++ libsrc/occ/occgeom.cpp | 77 ++++++++++++++++++++++++++++++++++++++- libsrc/occ/occgeom.hpp | 2 +- libsrc/occ/python_occ.cpp | 56 ++++++++++++++++++++++++++++ ng/netgenpy.cpp | 3 ++ python/CMakeLists.txt | 2 +- python/NgOCC.py | 11 ++++++ 8 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 libsrc/occ/python_occ.cpp create mode 100644 python/NgOCC.py diff --git a/libsrc/occ/CMakeLists.txt b/libsrc/occ/CMakeLists.txt index 831f2e6b..2f10e54e 100644 --- a/libsrc/occ/CMakeLists.txt +++ b/libsrc/occ/CMakeLists.txt @@ -2,7 +2,7 @@ add_definitions(-DNGINTERFACE_EXPORTS) add_library(occ ${NG_LIB_TYPE} Partition_Inter2d.cxx Partition_Inter3d.cxx Partition_Loop.cxx Partition_Loop2d.cxx Partition_Loop3d.cxx Partition_Spliter.cxx - occconstruction.cpp occgenmesh.cpp occgeom.cpp occmeshsurf.cpp + occconstruction.cpp occgenmesh.cpp occgeom.cpp occmeshsurf.cpp python_occ.cpp ) add_library(occvis ${NG_LIB_TYPE} vsocc.cpp) diff --git a/libsrc/occ/occgenmesh.cpp b/libsrc/occ/occgenmesh.cpp index 1530a31e..2ce29d7b 100644 --- a/libsrc/occ/occgenmesh.cpp +++ b/libsrc/occ/occgenmesh.cpp @@ -417,10 +417,14 @@ namespace netgen && (geom.face_colours->GetColor(face,XCAFDoc_ColorSurf,face_colour))) { mesh.GetFaceDescriptor(facenr).SetSurfColour(Vec3d(face_colour.Red(),face_colour.Green(),face_colour.Blue())); + mesh.GetFaceDescriptor(facenr).SetBCName(&geom.fnames[facenr-1]); + mesh.GetFaceDescriptor(facenr).SetBCProperty(facenr); } else { mesh.GetFaceDescriptor(facenr).SetSurfColour(Vec3d(0.0,1.0,0.0)); + mesh.GetFaceDescriptor(facenr).SetBCName(&geom.fnames[facenr-1]); + mesh.GetFaceDescriptor(facenr).SetBCProperty(facenr); } // ACHTUNG! STIMMT NICHT ALLGEMEIN (RG) @@ -974,6 +978,13 @@ namespace netgen NgProfiler::StopTimer (timer_opt2d); multithread.task = savetask; + + // Gerhard BEGIN + for(int i = 0; iGetNSeg(); i++) (*testout) << mesh->LineSegment(i) << endl; + for (int i = 0; i < mesh->GetNDomains(); i++) + mesh->SetMaterial (i+1, geom.snames[i]); return TCL_OK; } } diff --git a/libsrc/occ/occgeom.cpp b/libsrc/occ/occgeom.cpp index 3ffaf376..1d8e86d8 100644 --- a/libsrc/occ/occgeom.cpp +++ b/libsrc/occ/occgeom.cpp @@ -17,6 +17,10 @@ #include "Partition_Spliter.hxx" #include "BRepAlgoAPI_Fuse.hxx" +#include "XSControl_WorkSession.hxx" +#include "XSControl_TransferReader.hxx" +#include "StepRepr_RepresentationItem.hxx" + #ifndef _Standard_Version_HeaderFile #include #endif @@ -29,6 +33,44 @@ namespace netgen { +void STEP_GetEntityName(const TopoDS_Shape & theShape, STEPCAFControl_Reader * aReader, char * acName) +{ + const Handle(XSControl_WorkSession)& theSession = aReader->Reader().WS(); + const Handle(XSControl_TransferReader)& aTransferReader = + theSession->TransferReader(); + + Handle(Standard_Transient) anEntity = + aTransferReader->EntityFromShapeResult(theShape, 1); + + if (anEntity.IsNull()) { + // as just mapped + anEntity = aTransferReader->EntityFromShapeResult (theShape,-1); + } + + if (anEntity.IsNull()) { + // as anything + anEntity = aTransferReader->EntityFromShapeResult (theShape,4); + } + + if (anEntity.IsNull()) { + cout<<"Warning: XSInterVertex_STEPReader::ReadAttributes()\nentity not found"<Name()->ToCString()); + } +} + + void OCCGeometry :: PrintNrShapes () { TopExp_Explorer e; @@ -1169,7 +1211,7 @@ namespace netgen // Enable transfer of colours reader.SetColorMode(Standard_True); - + reader.SetNameMode(Standard_True); Standard_Integer stat = reader.ReadFile((char*)filename); if(stat != IFSelect_RetDone) @@ -1210,7 +1252,38 @@ namespace netgen occgeo->CalcBoundingBox(); PrintContents (occgeo); - + char * name = new char(50); + //string name; + STEP_GetEntityName(occgeo->shape,&reader,name); + occgeo->snames.Append(name); + TopExp_Explorer exp0,exp1; + + for (exp0.Init(occgeo->shape, TopAbs_FACE); exp0.More(); exp0.Next()) + { + TopoDS_Face face = TopoDS::Face(exp0.Current()); + STEP_GetEntityName(face,&reader,name); + occgeo->fnames.Append(name); + for (exp1.Init(face, TopAbs_EDGE); exp1.More(); exp1.Next()) + { + TopoDS_Edge edge = TopoDS::Edge(exp1.Current()); + STEP_GetEntityName(edge,&reader,name); + occgeo->enames.Append(name); + } + } + // Gerhard BEGIN +// cout << "Solid Names: "<snames.Size();i++) +// cout << occgeo->snames[i] << endl; +// cout << " " <fnames.Size();i++) +// cout << occgeo->fnames[i] << endl; +// cout << " " <enames.Size();i++) +// cout << occgeo->enames[i] << endl; +// cout << " " < fsingular, esingular, vsingular; Box<3> boundingbox; - + Array fnames, enames, snames; // Philippose - 29/01/2009 // OpenCascade XDE Support // XCAF Handle to make the face colours available to the rest of diff --git a/libsrc/occ/python_occ.cpp b/libsrc/occ/python_occ.cpp new file mode 100644 index 00000000..0cb2454a --- /dev/null +++ b/libsrc/occ/python_occ.cpp @@ -0,0 +1,56 @@ +#ifdef NG_PYTHON +#ifdef OCCGEOMETRY + +#include <../general/ngpython.hpp> + +#include +#include + +using namespace netgen; + +namespace netgen +{ + extern std::shared_ptr ng_geometry; +} + + +DLL_HEADER void ExportNgOCC(py::module &m) +{ + py::class_> (m, "OCCGeometry") + .def(py::init<>()) + ; + m.def("LoadOCCGeometry",FunctionPointer([] (const string & filename) + { + cout << "load OCC geometry"; + ifstream ist(filename); + OCCGeometry * instance = new OCCGeometry(); + instance = LoadOCC_STEP(filename.c_str()); + return shared_ptr(instance, NOOP_Deleter); + })); + m.def("GenerateMesh", FunctionPointer([] (shared_ptr geo, MeshingParameters ¶m) + { + auto mesh = make_shared(); + SetGlobalMesh(mesh); + mesh->SetGeometry(geo); + ng_geometry = geo; + try + { + geo->GenerateMesh(mesh,param); + } + catch (NgException ex) + { + cout << "Caught NgException: " << ex.What() << endl; + } + return mesh; + })) + ; +} + +PYBIND11_PLUGIN(libNgOCC) { + py::module m("NgOCC", "pybind NgOCC"); + ExportNgOCC(m); + return m.ptr(); +} + +#endif // OCCGEOMETRY +#endif // NG_PYTHON diff --git a/ng/netgenpy.cpp b/ng/netgenpy.cpp index e4ea7aac..e18e1f4d 100644 --- a/ng/netgenpy.cpp +++ b/ng/netgenpy.cpp @@ -17,6 +17,7 @@ void DLL_HEADER ExportCSGVis(py::module &m); void DLL_HEADER ExportGeom2d(py::module &m); void DLL_HEADER ExportSTL(py::module &m); void DLL_HEADER ExportSTLVis(py::module &m); +void DLL_HEADER ExportNgOCC(py::module &m); PYBIND11_PLUGIN(libngpy) { @@ -29,6 +30,8 @@ PYBIND11_PLUGIN(libngpy) ExportGeom2d(geom2d); py::module stl = ngpy.def_submodule("_stl", "pybind stl module"); ExportSTL(stl); + py::module NgOCC = ngpy.def_submodule("_NgOCC", "pybind NgOCC module"); + ExportNgOCC(NgOCC); #ifdef OPENGL py::module meshvis = ngpy.def_submodule("meshvis", "pybind meshvis module"); ExportMeshVis(meshvis); diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 5f9c785a..72912b24 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -2,7 +2,7 @@ configure_file(__init__.py ${CMAKE_CURRENT_BINARY_DIR}/__init__.py @ONLY) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/__init__.py - meshing.py csg.py geom2d.py stl.py gui.py + meshing.py csg.py geom2d.py stl.py gui.py NgOCC.py DESTINATION ${NG_INSTALL_DIR_PYTHON}/${NG_INSTALL_SUFFIX} COMPONENT netgen ) diff --git a/python/NgOCC.py b/python/NgOCC.py new file mode 100644 index 00000000..118bc1e4 --- /dev/null +++ b/python/NgOCC.py @@ -0,0 +1,11 @@ +import libngpy +from libngpy._NgOCC import * +from libngpy._meshing import MeshingParameters + +def NgOCC_meshing_func (geom, **args): + if "mp" in args: + return GenerateMesh (geom, args["mp"]) + else: + return GenerateMesh (geom, MeshingParameters (**args)) + +OCCGeometry.GenerateMesh = NgOCC_meshing_func