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/libsrc/stlgeom/python_stl.cpp b/libsrc/stlgeom/python_stl.cpp new file mode 100644 index 00000000..1220f133 --- /dev/null +++ b/libsrc/stlgeom/python_stl.cpp @@ -0,0 +1,56 @@ + +#ifdef NG_PYTHON + +#include <../general/ngpython.hpp> +#include + +#ifdef WIN32 + #define DLL_HEADER __declspec(dllexport) +#else + #define DLL_HEADER +#endif + +using namespace netgen; +namespace netgen +{ + //extern shared_ptr mesh; + extern shared_ptr ng_geometry; +} + + +DLL_HEADER void ExportSTL(py::module & m) +{ + py::class_> (m,"STLGeometry") + .def(py::init<>()) + ; + 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(); + 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/libsrc/stlgeom/vsstl.cpp b/libsrc/stlgeom/vsstl.cpp index 42d5db1c..6e550b0f 100644 --- a/libsrc/stlgeom/vsstl.cpp +++ b/libsrc/stlgeom/vsstl.cpp @@ -1210,3 +1210,44 @@ 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) +{ + 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(libstlvis) { + py::module m("stlvis", "pybind stl vis"); + ExportSTLVis(m); + return m.ptr(); +} +#endif diff --git a/ng/netgenpy.cpp b/ng/netgenpy.cpp index 32d9c694..3a91e373 100644 --- a/ng/netgenpy.cpp +++ b/ng/netgenpy.cpp @@ -15,6 +15,8 @@ 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); +void DLL_HEADER ExportSTLVis(py::module &m); PYBIND11_PLUGIN(libngpy) { @@ -29,6 +31,10 @@ 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); + py::module stlvis = ngpy.def_submodule("stlvis", "pybind stlvis module"); + ExportSTLVis(stlvis); return ngpy.ptr(); } 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