[occ] Specify extrusion direction, add getitem to gp_Pnt to iterate

This commit is contained in:
Christopher Lackner 2021-12-01 14:38:35 +01:00
parent ed0f8b8a53
commit c1d768a5b3
2 changed files with 24 additions and 6 deletions

View File

@ -49,6 +49,16 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
.def("__sub__", [](gp_Pnt p1, gp_Pnt p2) { return gp_Vec(p2, p1); }) .def("__sub__", [](gp_Pnt p1, gp_Pnt p2) { return gp_Vec(p2, p1); })
.def("__add__", [](gp_Pnt p, gp_Vec v) { return p.Translated(v); }) // gp_Pnt(p.X()+v.X(), p.Y()+v.Y(), p.Z()+v.Z()); }) .def("__add__", [](gp_Pnt p, gp_Vec v) { return p.Translated(v); }) // gp_Pnt(p.X()+v.X(), p.Y()+v.Y(), p.Z()+v.Z()); })
.def("__sub__", [](gp_Pnt p, gp_Vec v) { return p.Translated(-v); }) // gp_Pnt(p.X()-v.X(), p.Y()-v.Y(), p.Z()-v.Z()); }) .def("__sub__", [](gp_Pnt p, gp_Vec v) { return p.Translated(-v); }) // gp_Pnt(p.X()-v.X(), p.Y()-v.Y(), p.Z()-v.Z()); })
.def("__getitem__", [](const gp_Pnt& p, int index)
{
if(index == 0)
return p.X();
if(index == 1)
return p.Y();
if(index == 2)
return p.Z();
throw std::out_of_range("Point index must be in range [0,3)!");
})
; ;
py::class_<gp_Vec>(m, "gp_Vec", "3d OCC vector") py::class_<gp_Vec>(m, "gp_Vec", "3d OCC vector")

View File

@ -972,14 +972,22 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m)
.def("Reversed", [](const TopoDS_Shape & shape) { .def("Reversed", [](const TopoDS_Shape & shape) {
return CastShape(shape.Reversed()); }) return CastShape(shape.Reversed()); })
.def("Extrude", [](const TopoDS_Shape & shape, double h) { .def("Extrude", [](const TopoDS_Shape & shape, double h,
optional<gp_Vec> dir) {
for (TopExp_Explorer e(shape, TopAbs_FACE); e.More(); e.Next()) for (TopExp_Explorer e(shape, TopAbs_FACE); e.More(); e.Next())
{ {
Handle(Geom_Surface) surf = BRep_Tool::Surface (TopoDS::Face(e.Current())); Handle(Geom_Surface) surf = BRep_Tool::Surface (TopoDS::Face(e.Current()));
gp_Vec edir;
if(dir.has_value())
edir = *dir;
else
{
gp_Vec du, dv; gp_Vec du, dv;
gp_Pnt p; gp_Pnt p;
surf->D1 (0,0,p,du,dv); surf->D1 (0,0,p,du,dv);
BRepPrimAPI_MakePrism builder(shape, h*du^dv, true); edir = du^dv;
}
BRepPrimAPI_MakePrism builder(shape, h*edir, true);
for (auto typ : { TopAbs_EDGE, TopAbs_VERTEX }) for (auto typ : { TopAbs_EDGE, TopAbs_VERTEX })
for (TopExp_Explorer e(shape, typ); e.More(); e.Next()) for (TopExp_Explorer e(shape, typ); e.More(); e.Next())
@ -992,7 +1000,7 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m)
return builder.Shape(); return builder.Shape();
} }
throw Exception("no face found for extrusion"); throw Exception("no face found for extrusion");
}, py::arg("h"), "extrude shape to thickness 'h', shape must contain a plane surface") }, py::arg("h"), py::arg("dir")=nullopt, "extrude shape to thickness 'h', shape must contain a plane surface, optionally give an extrusion direction")
.def("Extrude", [] (const TopoDS_Shape & face, gp_Vec vec) { .def("Extrude", [] (const TopoDS_Shape & face, gp_Vec vec) {
return BRepPrimAPI_MakePrism (face, vec).Shape(); return BRepPrimAPI_MakePrism (face, vec).Shape();