From 4629bccd728ab5ce07fc6822cf079d38dee83335 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Fri, 6 Aug 2021 10:45:14 +0200 Subject: [PATCH] forgot file --- libsrc/occ/python_occ_basic.cpp | 248 ++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 libsrc/occ/python_occ_basic.cpp diff --git a/libsrc/occ/python_occ_basic.cpp b/libsrc/occ/python_occ_basic.cpp new file mode 100644 index 00000000..d9745a10 --- /dev/null +++ b/libsrc/occ/python_occ_basic.cpp @@ -0,0 +1,248 @@ +#ifdef NG_PYTHON +#ifdef OCCGEOMETRY + +#include <../general/ngpython.hpp> +#include +#include "../meshing/python_mesh.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#if OCC_VERSION_MAJOR>=7 && OCC_VERSION_MINOR>=4 +#define OCC_HAVE_DUMP_JSON +#endif + + + + + +DLL_HEADER void ExportNgOCCBasic(py::module &m) +{ + cout << "export basics" << endl; + + py::class_(m, "gp_Pnt") + .def(py::init([] (py::tuple pnt) + { + if (py::len(pnt) != 3) + throw Exception("need 3-tuple to create gp_Pnt"); + + return gp_Pnt(py::cast(pnt[0]), + py::cast(pnt[1]), + py::cast(pnt[2])); + })) + .def(py::init([] (double x, double y, double z) { + return gp_Pnt(x, y, z); + })) + .def_property("x", [](gp_Pnt&p) { return p.X(); }, [](gp_Pnt&p,double x) { p.SetX(x); }) + .def_property("y", [](gp_Pnt&p) { return p.Y(); }, [](gp_Pnt&p,double y) { p.SetY(y); }) + .def_property("z", [](gp_Pnt&p) { return p.Z(); }, [](gp_Pnt&p,double z) { p.SetZ(z); }) + .def("__str__", [] (const gp_Pnt & p) { + stringstream str; + str << "(" << p.X() << ", " << p.Y() << ", " << p.Z() << ")"; + return str.str(); + }) + // .def(py::self - py::self) + .def("__sub__", [](gp_Pnt p1, gp_Pnt p2) { return gp_Vec(p1.X()-p2.X(), p1.Y()-p2.Y(), p1.Z()-p2.Z()); }) + .def("__add__", [](gp_Pnt p, gp_Vec v) { return gp_Pnt(p.X()+v.X(), p.Y()+v.Y(), p.Z()+v.Z()); }) + .def("__sub__", [](gp_Pnt p, gp_Vec v) { return gp_Pnt(p.X()-v.X(), p.Y()-v.Y(), p.Z()-v.Z()); }) + ; + + py::class_(m, "gp_Vec") + .def(py::init([] (py::tuple vec) + { + return gp_Vec(py::cast(vec[0]), + py::cast(vec[1]), + py::cast(vec[2])); + })) + .def(py::init([] (double x, double y, double z) { + return gp_Vec(x, y, z); + })) + .def_property("x", [](gp_Vec&p) { return p.X(); }, [](gp_Vec&p,double x) { p.SetX(x); }) + .def_property("y", [](gp_Vec&p) { return p.Y(); }, [](gp_Vec&p,double y) { p.SetY(y); }) + .def_property("z", [](gp_Vec&p) { return p.Z(); }, [](gp_Vec&p,double z) { p.SetZ(z); }) + .def("__str__", [] (const gp_Vec & p) { + stringstream str; + str << "(" << p.X() << ", " << p.Y() << ", " << p.Z() << ")"; + return str.str(); + }) + .def("__add__", [](gp_Vec v1, gp_Vec v2) { return v1+v2; }) // return gp_Vec(v1.X()+v2.X(), v1.Y()+v2.Y(), v1.Z()+v2.Z()); }) + .def("__sub__", [](gp_Vec v1, gp_Vec v2) { return v1-v2; }) // gp_Vec(v1.X()-v2.X(), v1.Y()-v2.Y(), v1.Z()-v2.Z()); }) + .def("__rmul__", [](gp_Vec v, double s) { return s*v; }) // gp_Vec(s*v.X(), s*v.Y(), s*v.Z()); }) + .def("__neg__", [](gp_Vec v) { return -v; }) // gp_Vec(-v.X(), -v.Y(), -v.Z()); }) + ; + + py::class_(m, "gp_Dir") + .def(py::init([] (py::tuple dir) + { + return gp_Dir(py::cast(dir[0]), + py::cast(dir[1]), + py::cast(dir[2])); + })) + .def(py::init([] (double x, double y, double z) { + return gp_Dir(x, y, z); + })) + .def(py::init()) + .def("__str__", [] (const gp_Dir & p) { + stringstream str; + str << "(" << p.X() << ", " << p.Y() << ", " << p.Z() << ")"; + return str.str(); + }) + ; + + py::class_(m, "gp_Ax1") + .def(py::init([](gp_Pnt p, gp_Dir d) { + return gp_Ax1(p,d); + })) + ; + py::class_(m, "gp_Ax2") + .def(py::init([](gp_Pnt p, gp_Dir d) { + return gp_Ax2(p,d); + })) + .def(py::init([](const gp_Ax3 & ax3) { + return gp_Ax2(ax3.Ax2()); + })) + ; + + py::class_(m, "gp_Ax3") + .def(py::init([](gp_Pnt p, gp_Dir N, gp_Dir Vx) { + return gp_Ax3(p,N, Vx); + }), py::arg("p"), py::arg("n"), py::arg("x")) + .def(py::init()) + .def_property("p", [](gp_Ax3 & ax) { return ax.Location(); }, [](gp_Ax3&ax, gp_Pnt p) { ax.SetLocation(p); }) + ; + + + py::class_(m, "gp_Pnt2d") + .def(py::init([] (py::tuple pnt) + { + if (py::len(pnt) != 2) + throw Exception("need 2-tuple to create gp_Pnt2d"); + return gp_Pnt2d(py::cast(pnt[0]), + py::cast(pnt[1])); + })) + .def(py::init([] (double x, double y) { + return gp_Pnt2d(x, y); + })) + ; + py::class_(m, "gp_Vec2d") + .def(py::init([] (py::tuple vec) + { + if (py::len(vec) != 2) + throw Exception("need 2-tuple to create gp_Vec2d"); + return gp_Vec2d(py::cast(vec[0]), + py::cast(vec[1])); + })) + .def(py::init([] (double x, double y) { + return gp_Vec2d(x, y); + })) + ; + + py::class_(m, "gp_Dir2d") + .def(py::init([] (py::tuple dir) + { + if (py::len(dir) != 2) + throw Exception("need 2-tuple to create gp_Dir2d"); + return gp_Dir2d(py::cast(dir[0]), + py::cast(dir[1])); + })) + .def(py::init([] (double x, double y) { + return gp_Dir2d(x, y); + })) + ; + + py::class_(m, "gp_Ax2d") + .def(py::init([](gp_Pnt2d p, gp_Dir2d d) { + return gp_Ax2d(p,d); + })) + ; + + + + py::class_(m, "gp_Trsf") + .def(py::init<>()) + .def("SetMirror", [] (gp_Trsf & trafo, const gp_Ax1 & ax) { trafo.SetMirror(ax); return trafo; }) + .def_static("Translation", [] (const gp_Vec & v) { gp_Trsf trafo; trafo.SetTranslation(v); return trafo; }) + .def_static("Scale", [] (const gp_Pnt & p, double s) { gp_Trsf trafo; trafo.SetScale(p,s); return trafo; }) + .def_static("Mirror", [] (const gp_Ax1 & ax) { gp_Trsf trafo; trafo.SetMirror(ax); return trafo; }) + .def_static("Rotation", [] (const gp_Ax1 & ax, double ang) { gp_Trsf trafo; trafo.SetRotation(ax, ang*M_PI/180); return trafo; }) + .def_static("Rotation", [] (const gp_Pnt & p, const gp_Dir & d, double ang) + { gp_Trsf trafo; trafo.SetRotation(gp_Ax1(p,d), ang*M_PI/180); return trafo; }) + .def_static("Transformation", [] (const gp_Ax3 & ax) { gp_Trsf trafo; trafo.SetTransformation(ax); return trafo; }) + .def_static("Transformation", [] (const gp_Ax3 & from, const gp_Ax3 to) + { gp_Trsf trafo; trafo.SetTransformation(from, to); return trafo; }) + .def(py::self * py::self) + .def("__call__", [] (gp_Trsf & trafo, const TopoDS_Shape & shape) { + return BRepBuilderAPI_Transform(shape, trafo).Shape(); + }) + .def("__str__", [](gp_Trsf & trafo) + { + stringstream str; + gp_XYZ xyz = trafo.TranslationPart(); + str << xyz.X() << ", " << xyz.Y() << ", " << xyz.Z(); + return str.str(); + }) + ; + + py::class_(m, "TopLoc_Location") + .def(py::init()) + .def("Transformation", [](const TopLoc_Location & loc) { return loc.Transformation(); }) + ; + + py::implicitly_convertible(); + py::implicitly_convertible(); + py::implicitly_convertible(); + py::implicitly_convertible(); + py::implicitly_convertible(); + py::implicitly_convertible(); + py::implicitly_convertible(); + + + py::implicitly_convertible(); + + static gp_Vec ex(1,0,0), ey(0,1,0), ez(0,0,1); + m.attr("X") = py::cast(&ex); + m.attr("Y") = py::cast(&ey); + m.attr("Z") = py::cast(&ez); + +} + + +#endif // OCCGEOMETRY +#endif // NG_PYTHON +