From 561cf4e75f4add489a9cbff7508b0c3ab7032a04 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Fri, 9 Dec 2016 16:34:22 +0100 Subject: [PATCH 1/5] add files --- libsrc/stlgeom/python_stl.cpp | 49 +++++++++++++++++++++++++++++++++++ python/stl.py | 13 ++++++++++ 2 files changed, 62 insertions(+) create mode 100644 libsrc/stlgeom/python_stl.cpp create mode 100644 python/stl.py diff --git a/libsrc/stlgeom/python_stl.cpp b/libsrc/stlgeom/python_stl.cpp new file mode 100644 index 00000000..6a0f38a0 --- /dev/null +++ b/libsrc/stlgeom/python_stl.cpp @@ -0,0 +1,49 @@ + +#ifdef NG_PYTHON + +#include <../general/ngpython.hpp> +#include + +using namespace netgen; +namespace netgen +{ + extern shared_ptr ng_geometry; +} + + +DLL_HEADER void ExportSTL(py::module & m) +{ + py::class_> (m,"STLGeometry") + .def(py::init<>()) + .def("Load", FunctionPointer([] (shared_ptr self, const string & filename) + { + ifstream ist(filename); + self->Load(ist); + + })) + ; + 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(libstl) { + py::module m("stl", "pybind stl"); + ExportSTL(m); + return m.ptr(); +} +#endif diff --git a/python/stl.py b/python/stl.py new file mode 100644 index 00000000..9ef75a57 --- /dev/null +++ b/python/stl.py @@ -0,0 +1,13 @@ +import libngpy +from libngpy._stl import * +from libngpy._meshing import MeshingParameters + + +def stl_meshing_func (geom, **args): + if "mp" in args: + return GenerateMesh (geom, args["mp"]) + else: + return GenerateMesh (geom, MeshingParameters (**args)) +# return GenerateMesh (geom, MeshingParameters (**args)) + +STLGeometry.GenerateMesh = stl_meshing_func From 25d2e497b1940ba9b7a8bff325361274c1e173f3 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 12 Dec 2016 10:47:05 +0100 Subject: [PATCH 2/5] add python stl submodule --- libsrc/stlgeom/CMakeLists.txt | 2 +- ng/netgenpy.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/libsrc/stlgeom/CMakeLists.txt b/libsrc/stlgeom/CMakeLists.txt index a78658ae..436cf3e4 100644 --- a/libsrc/stlgeom/CMakeLists.txt +++ b/libsrc/stlgeom/CMakeLists.txt @@ -1,6 +1,6 @@ add_library(stl ${NG_LIB_TYPE} meshstlsurface.cpp stlgeom.cpp stlgeomchart.cpp - stlgeommesh.cpp stlline.cpp stltool.cpp stltopology.cpp + stlgeommesh.cpp stlline.cpp stltool.cpp stltopology.cpp python_stl.cpp ) if(NOT WIN32) diff --git a/ng/netgenpy.cpp b/ng/netgenpy.cpp index 32d9c694..e47b1f90 100644 --- a/ng/netgenpy.cpp +++ b/ng/netgenpy.cpp @@ -15,6 +15,7 @@ void DLL_HEADER ExportMeshVis(py::module &m); void DLL_HEADER ExportCSG(py::module &m); void DLL_HEADER ExportCSGVis(py::module &m); void DLL_HEADER ExportGeom2d(py::module &m); +void DLL_HEADER ExportSTL(py::module &m); PYBIND11_PLUGIN(libngpy) { @@ -29,6 +30,8 @@ PYBIND11_PLUGIN(libngpy) ExportMeshVis(meshvis); py::module geom2d = ngpy.def_submodule("_geom2d", "pybind geom2d module"); ExportGeom2d(geom2d); + py::module stl = ngpy.def_submodule("_stl", "pybind stl module"); + ExportSTL(stl); return ngpy.ptr(); } From a5e411d0089a04a33071036593038f4d90ed1e96 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 12 Dec 2016 13:14:47 +0100 Subject: [PATCH 3/5] load stl fix and some vis --- libsrc/stlgeom/python_stl.cpp | 13 +++++++------ libsrc/stlgeom/vsstl.cpp | 33 +++++++++++++++++++++++++++++++++ ng/netgenpy.cpp | 3 +++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/libsrc/stlgeom/python_stl.cpp b/libsrc/stlgeom/python_stl.cpp index 6a0f38a0..0ab54822 100644 --- a/libsrc/stlgeom/python_stl.cpp +++ b/libsrc/stlgeom/python_stl.cpp @@ -7,6 +7,7 @@ using namespace netgen; namespace netgen { + //extern shared_ptr mesh; extern shared_ptr ng_geometry; } @@ -15,13 +16,12 @@ DLL_HEADER void ExportSTL(py::module & m) { py::class_> (m,"STLGeometry") .def(py::init<>()) - .def("Load", FunctionPointer([] (shared_ptr self, const string & filename) - { - ifstream ist(filename); - self->Load(ist); - - })) ; + m.def("LoadSTLGeometry", FunctionPointer([] (const string & filename) + { + ifstream ist(filename); + return shared_ptr(STLGeometry::Load(ist)); + })); m.def("GenerateMesh", FunctionPointer([] (shared_ptr geo, MeshingParameters ¶m) { auto mesh = make_shared(); @@ -46,4 +46,5 @@ PYBIND11_PLUGIN(libstl) { ExportSTL(m); return m.ptr(); } + #endif diff --git a/libsrc/stlgeom/vsstl.cpp b/libsrc/stlgeom/vsstl.cpp index 42d5db1c..60011f61 100644 --- a/libsrc/stlgeom/vsstl.cpp +++ b/libsrc/stlgeom/vsstl.cpp @@ -1210,3 +1210,36 @@ void VisualSceneSTLMeshing :: MouseDblClick (int px, int py) } } + + + +#ifdef NG_PYTHON +#include <../general/ngpython.hpp> + +DLL_HEADER void ExportSTLVis(py::module &m) +{ + using namespace netgen; + + py::class_> + (m, "VisualSceneSTLGeometry") + .def("Draw", &VisualSceneSTLGeometry::DrawScene) + ; + + m.def("SetBackGroundColor", &VisualSceneSTLGeometry::SetBackGroundColor); + + m.def("VS", + [](STLGeometry & geom) + { + auto vs = make_shared(); + + vs->SetGeometry(&geom); + return vs; + }); +} + +PYBIND11_PLUGIN(libcsgvis) { + py::module m("csg", "pybind csg"); + ExportSTLVis(m); + return m.ptr(); +} +#endif diff --git a/ng/netgenpy.cpp b/ng/netgenpy.cpp index e47b1f90..3a91e373 100644 --- a/ng/netgenpy.cpp +++ b/ng/netgenpy.cpp @@ -16,6 +16,7 @@ void DLL_HEADER ExportCSG(py::module &m); 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); PYBIND11_PLUGIN(libngpy) { @@ -32,6 +33,8 @@ PYBIND11_PLUGIN(libngpy) ExportGeom2d(geom2d); py::module stl = ngpy.def_submodule("_stl", "pybind stl module"); ExportSTL(stl); + py::module stlvis = ngpy.def_submodule("stlvis", "pybind stlvis module"); + ExportSTLVis(stlvis); return ngpy.ptr(); } From 2649726692a8308ab6ef68806c0ce82f93946834 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 12 Dec 2016 14:22:24 +0100 Subject: [PATCH 4/5] fix DLL_HEADER export --- libsrc/stlgeom/python_stl.cpp | 6 ++++++ libsrc/stlgeom/vsstl.cpp | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/libsrc/stlgeom/python_stl.cpp b/libsrc/stlgeom/python_stl.cpp index 0ab54822..1220f133 100644 --- a/libsrc/stlgeom/python_stl.cpp +++ b/libsrc/stlgeom/python_stl.cpp @@ -4,6 +4,12 @@ #include <../general/ngpython.hpp> #include +#ifdef WIN32 + #define DLL_HEADER __declspec(dllexport) +#else + #define DLL_HEADER +#endif + using namespace netgen; namespace netgen { diff --git a/libsrc/stlgeom/vsstl.cpp b/libsrc/stlgeom/vsstl.cpp index 60011f61..2cb7b9e4 100644 --- a/libsrc/stlgeom/vsstl.cpp +++ b/libsrc/stlgeom/vsstl.cpp @@ -1214,6 +1214,14 @@ void VisualSceneSTLMeshing :: MouseDblClick (int px, int py) #ifdef NG_PYTHON + + +#ifdef WIN32 + #define DLL_HEADER __declspec(dllexport) +#else + #define DLL_HEADER +#endif + #include <../general/ngpython.hpp> DLL_HEADER void ExportSTLVis(py::module &m) From e16dfde5e754bf9522361aad385fe7c9ae1a2cb4 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Mon, 12 Dec 2016 14:26:49 +0100 Subject: [PATCH 5/5] fix libstlvis --- libsrc/stlgeom/vsstl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libsrc/stlgeom/vsstl.cpp b/libsrc/stlgeom/vsstl.cpp index 2cb7b9e4..6e550b0f 100644 --- a/libsrc/stlgeom/vsstl.cpp +++ b/libsrc/stlgeom/vsstl.cpp @@ -1245,8 +1245,8 @@ DLL_HEADER void ExportSTLVis(py::module &m) }); } -PYBIND11_PLUGIN(libcsgvis) { - py::module m("csg", "pybind csg"); +PYBIND11_PLUGIN(libstlvis) { + py::module m("stlvis", "pybind stl vis"); ExportSTLVis(m); return m.ptr(); }