From 3e8664b40e74a3ec188d7298c91781ce904a9a41 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Fri, 6 Aug 2021 14:23:17 +0200 Subject: [PATCH] workplane draft --- libsrc/occ/python_occ.cpp | 124 +++++++++++++++++++++++++++++++- libsrc/occ/python_occ_basic.cpp | 4 +- 2 files changed, 125 insertions(+), 3 deletions(-) diff --git a/libsrc/occ/python_occ.cpp b/libsrc/occ/python_occ.cpp index 2faf3dc6..f5396aad 100644 --- a/libsrc/occ/python_occ.cpp +++ b/libsrc/occ/python_occ.cpp @@ -4,6 +4,7 @@ #include <../general/ngpython.hpp> #include #include "../meshing/python_mesh.hpp" +#include #include #include @@ -24,9 +25,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -88,18 +91,137 @@ extern py::object CastShape(const TopoDS_Shape & s); DLL_HEADER void ExportNgOCCBasic(py::module &m); DLL_HEADER void ExportNgOCCShapes(py::module &m); + +class WorkPlane : public enable_shared_from_this +{ + gp_Ax3 axis; + gp_Ax2d localpos; + gp_Pnt2d startpnt; + Handle(Geom_Surface) surf; + // Geom_Plane surf; + + BRepBuilderAPI_MakeWire wire_builder; + std::vector wires; + +public: + + WorkPlane (const gp_Ax3 & _axis, const gp_Ax2d _localpos = gp_Ax2d()) + : axis(_axis), localpos(_localpos) // , surf(_axis) + { + // surf = GC_MakePlane (gp_Ax1(axis.Location(), axis.Direction())); + surf = new Geom_Plane(axis); + } + + + auto MoveTo (double h, double v) + { + startpnt = gp_Pnt2d(h,v); + localpos.SetLocation(startpnt); + return shared_from_this(); + } + + auto Direction (double h, double v) + { + localpos.SetDirection(gp_Dir2d(h,v)); + return shared_from_this(); + } + + auto LineTo (double h, double v) + { + gp_Pnt2d old2d = localpos.Location(); + gp_Pnt oldp = axis.Location() . Translated(old2d.X() * axis.XDirection() + old2d.Y() * axis.YDirection()); + + // localpos.Translate (gp_Vec2d(h,v)); + localpos.SetLocation (gp_Pnt2d(h,v)); + gp_Pnt2d new2d = localpos.Location(); + gp_Pnt newp = axis.Location() . Translated(new2d.X() * axis.XDirection() + new2d.Y() * axis.YDirection()); + + cout << "lineto, newp = " << occ2ng(newp) << endl; + gp_Pnt pfromsurf; + surf->D0(new2d.X(), new2d.Y(), pfromsurf); + cout << "p from plane = " << occ2ng(pfromsurf) << endl; + + + Handle(Geom_TrimmedCurve) curve = GC_MakeSegment(oldp, newp); + auto edge = BRepBuilderAPI_MakeEdge(curve).Edge(); + wire_builder.Add(edge); + return shared_from_this(); + } + + auto Line(double h, double v) + { + gp_Pnt2d oldp = localpos.Location(); + oldp.Translate(gp_Vec2d(h,v)); + return LineTo (oldp.X(), oldp.Y()); + } + + auto Line(double len) + { + gp_Dir2d dir = localpos.Direction(); + cout << "dir = " << dir.X() << ", " << dir.Y() << endl; + gp_Pnt2d oldp = localpos.Location(); + oldp.Translate(len*dir); + return LineTo (oldp.X(), oldp.Y()); + } + + auto Rotate (double angle) + { + localpos.Rotate(localpos.Location(), angle*M_PI/180); + return shared_from_this(); + } + + auto Close () + { + LineTo (startpnt.X(), startpnt.Y()); + wires.push_back (wire_builder.Wire()); + wire_builder = BRepBuilderAPI_MakeWire(); + } + + TopoDS_Wire Last() + { + return wires.back(); + } + + TopoDS_Face Face() + { + // crashes ???? + BRepBuilderAPI_MakeFace builder(surf, 1e-8); + for (auto w : wires) + builder.Add(w); + return builder.Face(); + + // only one wire, for now: + // return BRepBuilderAPI_MakeFace(wires.back()).Face(); + } +}; + + DLL_HEADER void ExportNgOCC(py::module &m) { m.attr("occ_version") = OCC_VERSION_COMPLETE; ExportNgOCCBasic(m); ExportNgOCCShapes(m); + + py::class_> (m, "WorkPlane") + .def(py::init(), py::arg("axis"), py::arg("pos")=gp_Ax2d()) + .def("MoveTo", &WorkPlane::MoveTo) + .def("Direction", &WorkPlane::Direction) + .def("LineTo", &WorkPlane::LineTo) + .def("Rotate", &WorkPlane::Rotate) + .def("Line", [](WorkPlane&wp,double l) { return wp.Line(l); }) + .def("Line", [](WorkPlane&wp,double h,double v) { return wp.Line(h,v); }) + .def("Close", &WorkPlane::Close) + .def("Last", &WorkPlane::Last) + .def("Face", &WorkPlane::Face) + ; + + // not working, since occ - exceptions don't derive from std::exception // py::register_exception(m, "OCC-Exception"); py::class_, NetgenGeometry> (m, "OCCGeometry", R"raw_string(Use LoadOCCGeometry to load the geometry from a *.step file.)raw_string") - .def(py::init<>()) /* .def(py::init(), py::arg("shape"), "Create Netgen OCCGeometry from existing TopoDS_Shape") diff --git a/libsrc/occ/python_occ_basic.cpp b/libsrc/occ/python_occ_basic.cpp index d9745a10..32c4d97e 100644 --- a/libsrc/occ/python_occ_basic.cpp +++ b/libsrc/occ/python_occ_basic.cpp @@ -142,7 +142,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m) 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")) + }), py::arg("p")=gp_Pnt(0,0,0), py::arg("n")=gp_Vec(0,0,1), py::arg("h")=gp_Vec(1,0,0)) .def(py::init()) .def_property("p", [](gp_Ax3 & ax) { return ax.Location(); }, [](gp_Ax3&ax, gp_Pnt p) { ax.SetLocation(p); }) ; @@ -189,7 +189,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m) py::class_(m, "gp_Ax2d") .def(py::init([](gp_Pnt2d p, gp_Dir2d d) { return gp_Ax2d(p,d); - })) + }), py::arg("p")=gp_Pnt2d(0,0), py::arg("d")=gp_Dir2d(1,0)) ;