more docstrings, don't catch exceptions individually

This commit is contained in:
Joachim Schoeberl 2021-09-08 06:45:56 +02:00
parent 1087e961ca
commit 6bd9d83fce
2 changed files with 77 additions and 76 deletions

View File

@ -57,7 +57,7 @@
DLL_HEADER void ExportNgOCCBasic(py::module &m) DLL_HEADER void ExportNgOCCBasic(py::module &m)
{ {
py::class_<gp_Pnt>(m, "gp_Pnt") py::class_<gp_Pnt>(m, "gp_Pnt", "3d OCC point")
.def(py::init([] (py::tuple pnt) .def(py::init([] (py::tuple pnt)
{ {
if (py::len(pnt) != 3) if (py::len(pnt) != 3)
@ -69,7 +69,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
})) }))
.def(py::init([] (double x, double y, double z) { .def(py::init([] (double x, double y, double z) {
return gp_Pnt(x, y, z); return gp_Pnt(x, y, z);
})) }), py::arg("x"), py::arg("y"), py::arg("z"))
.def_property("x", [](gp_Pnt&p) { return p.X(); }, [](gp_Pnt&p,double x) { p.SetX(x); }) .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("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_property("z", [](gp_Pnt&p) { return p.Z(); }, [](gp_Pnt&p,double z) { p.SetZ(z); })
@ -89,7 +89,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
.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()); })
; ;
py::class_<gp_Vec>(m, "gp_Vec") py::class_<gp_Vec>(m, "gp_Vec", "3d OCC vector")
.def(py::init([] (py::tuple vec) .def(py::init([] (py::tuple vec)
{ {
return gp_Vec(py::cast<double>(vec[0]), return gp_Vec(py::cast<double>(vec[0]),
@ -98,7 +98,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
})) }))
.def(py::init([] (double x, double y, double z) { .def(py::init([] (double x, double y, double z) {
return gp_Vec(x, y, z); return gp_Vec(x, y, z);
})) }), py::arg("x"), py::arg("y"), py::arg("z"))
.def_property("x", [](gp_Vec&p) { return p.X(); }, [](gp_Vec&p,double x) { p.SetX(x); }) .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("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_property("z", [](gp_Vec&p) { return p.Z(); }, [](gp_Vec&p,double z) { p.SetZ(z); })
@ -120,17 +120,17 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
.def("__lt__", [](gp_Vec v, double val) .def("__lt__", [](gp_Vec v, double val)
{ {
cout << "vec, lt v - " << netgen::occ2ng(v) << ", val = " << val << endl; cout << IM(6) << "vec, lt v - " << netgen::occ2ng(v) << ", val = " << val << endl;
return DirectionalInterval(v) < val; return DirectionalInterval(v) < val;
}) })
.def("__gt__", [](gp_Vec v, double val) .def("__gt__", [](gp_Vec v, double val)
{ {
cout << "vec, gt v - " << netgen::occ2ng(v) << ", val = " << val << endl; cout << IM(6) << "vec, gt v - " << netgen::occ2ng(v) << ", val = " << val << endl;
return DirectionalInterval(v) > val; return DirectionalInterval(v) > val;
}) })
; ;
py::class_<gp_Dir>(m, "gp_Dir") py::class_<gp_Dir>(m, "gp_Dir", "3d OCC direction")
.def(py::init([] (py::tuple dir) .def(py::init([] (py::tuple dir)
{ {
return gp_Dir(py::cast<double>(dir[0]), return gp_Dir(py::cast<double>(dir[0]),
@ -139,7 +139,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
})) }))
.def(py::init([] (double x, double y, double z) { .def(py::init([] (double x, double y, double z) {
return gp_Dir(x, y, z); return gp_Dir(x, y, z);
})) }), py::arg("x"), py::arg("y"), py::arg("z"))
.def(py::init<gp_Vec>()) .def(py::init<gp_Vec>())
.def("__str__", [] (const gp_Dir & p) { .def("__str__", [] (const gp_Dir & p) {
stringstream str; stringstream str;
@ -148,10 +148,10 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
}) })
; ;
py::class_<gp_Ax1>(m, "Axis") // "gp_Ax1") py::class_<gp_Ax1>(m, "Axis", "an OCC axis in 3d")
.def(py::init([](gp_Pnt p, gp_Dir d) { .def(py::init([](gp_Pnt p, gp_Dir d) {
return gp_Ax1(p,d); return gp_Ax1(p,d);
})) }), py::arg("p"), py::arg("d"))
; ;
py::class_<gp_Ax2>(m, "gp_Ax2") py::class_<gp_Ax2>(m, "gp_Ax2")
.def(py::init([](gp_Pnt p, gp_Dir d) { .def(py::init([](gp_Pnt p, gp_Dir d) {
@ -162,7 +162,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
})) }))
; ;
py::class_<gp_Ax3>(m, "Axes") // "gp_Ax3") py::class_<gp_Ax3>(m, "Axes", "an OCC coordinate system in 3d")
.def(py::init([](gp_Pnt p, gp_Dir N, gp_Dir Vx) { .def(py::init([](gp_Pnt p, gp_Dir N, gp_Dir Vx) {
return gp_Ax3(p,N, Vx); return gp_Ax3(p,N, Vx);
}), py::arg("p")=gp_Pnt(0,0,0), py::arg("n")=gp_Vec(0,0,1), py::arg("h")=gp_Vec(1,0,0)) }), py::arg("p")=gp_Pnt(0,0,0), py::arg("n")=gp_Vec(0,0,1), py::arg("h")=gp_Vec(1,0,0))
@ -171,7 +171,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
; ;
py::class_<gp_Pnt2d>(m, "gp_Pnt2d") py::class_<gp_Pnt2d>(m, "gp_Pnt2d", "2d OCC point")
.def(py::init([] (py::tuple pnt) .def(py::init([] (py::tuple pnt)
{ {
if (py::len(pnt) != 2) if (py::len(pnt) != 2)
@ -181,7 +181,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
})) }))
.def(py::init([] (double x, double y) { .def(py::init([] (double x, double y) {
return gp_Pnt2d(x, y); return gp_Pnt2d(x, y);
})) }), py::arg("x"), py::arg("y"))
.def_property("x", [](gp_Pnt2d&p) { return p.X(); }, [](gp_Pnt2d&p,double x) { p.SetX(x); }) .def_property("x", [](gp_Pnt2d&p) { return p.X(); }, [](gp_Pnt2d&p,double x) { p.SetX(x); })
.def_property("y", [](gp_Pnt2d&p) { return p.Y(); }, [](gp_Pnt2d&p,double y) { p.SetY(y); }) .def_property("y", [](gp_Pnt2d&p) { return p.Y(); }, [](gp_Pnt2d&p,double y) { p.SetY(y); })
.def("__str__", [] (const gp_Pnt2d & p) { .def("__str__", [] (const gp_Pnt2d & p) {
@ -200,7 +200,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
.def("__sub__", [](gp_Pnt2d p, gp_Vec2d v) { return p.Translated(-v); }) .def("__sub__", [](gp_Pnt2d p, gp_Vec2d v) { return p.Translated(-v); })
; ;
py::class_<gp_Vec2d>(m, "gp_Vec2d") py::class_<gp_Vec2d>(m, "gp_Vec2d", "2d OCC vector")
.def(py::init([] (py::tuple vec) .def(py::init([] (py::tuple vec)
{ {
if (py::len(vec) != 2) if (py::len(vec) != 2)
@ -210,7 +210,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
})) }))
.def(py::init([] (double x, double y) { .def(py::init([] (double x, double y) {
return gp_Vec2d(x, y); return gp_Vec2d(x, y);
})) }), py::arg("x"), py::arg("y"))
.def_property("x", [](gp_Vec2d&p) { return p.X(); }, [](gp_Vec2d&p,double x) { p.SetX(x); }) .def_property("x", [](gp_Vec2d&p) { return p.X(); }, [](gp_Vec2d&p,double x) { p.SetX(x); })
.def_property("y", [](gp_Vec2d&p) { return p.Y(); }, [](gp_Vec2d&p,double y) { p.SetY(y); }) .def_property("y", [](gp_Vec2d&p) { return p.Y(); }, [](gp_Vec2d&p,double y) { p.SetY(y); })
.def("__str__", [] (const gp_Vec & p) { .def("__str__", [] (const gp_Vec & p) {
@ -230,7 +230,7 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
.def("__xor__", [](gp_Vec2d v1, gp_Vec2d v2) { return v1^v2; }) .def("__xor__", [](gp_Vec2d v1, gp_Vec2d v2) { return v1^v2; })
; ;
py::class_<gp_Dir2d>(m, "gp_Dir2d") py::class_<gp_Dir2d>(m, "gp_Dir2d", "2d OCC direction")
.def(py::init([] (py::tuple dir) .def(py::init([] (py::tuple dir)
{ {
if (py::len(dir) != 2) if (py::len(dir) != 2)
@ -240,11 +240,13 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
})) }))
.def(py::init([] (double x, double y) { .def(py::init([] (double x, double y) {
return gp_Dir2d(x, y); return gp_Dir2d(x, y);
})) }), py::arg("x"), py::arg("y"))
; ;
m.def("Pnt", [](double x, double y) { return gp_Pnt2d(x,y); }); m.def("Pnt", [](double x, double y) { return gp_Pnt2d(x,y); },
m.def("Pnt", [](double x, double y, double z) { return gp_Pnt(x,y,z); }); py::arg("x"), py::arg("y"), "create 2d OCC point");
m.def("Pnt", [](double x, double y, double z) { return gp_Pnt(x,y,z); },
py::arg("x"), py::arg("y"), py::arg("z"), "create 3d OCC point");
m.def("Pnt", [](std::vector<double> p) m.def("Pnt", [](std::vector<double> p)
{ {
if (p.size() == 2) if (p.size() == 2)
@ -252,10 +254,12 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
if (p.size() == 3) if (p.size() == 3)
return py::cast(gp_Pnt(p[0], p[1], p[2])); return py::cast(gp_Pnt(p[0], p[1], p[2]));
throw Exception("OCC-Points only in 2D or 3D"); throw Exception("OCC-Points only in 2D or 3D");
}); }, py::arg("p"), "create 2d or 3d OCC point");
m.def("Vec", [](double x, double y) { return gp_Vec2d(x,y); }); m.def("Vec", [](double x, double y) { return gp_Vec2d(x,y); },
m.def("Vec", [](double x, double y, double z) { return gp_Vec(x,y,z); }); py::arg("x"), py::arg("y"), "create 2d OCC point");
m.def("Vec", [](double x, double y, double z) { return gp_Vec(x,y,z); },
py::arg("x"), py::arg("y"), py::arg("z"), "create 3d OCC point");
m.def("Vec", [](std::vector<double> p) m.def("Vec", [](std::vector<double> p)
{ {
if (p.size() == 2) if (p.size() == 2)
@ -263,10 +267,12 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
if (p.size() == 3) if (p.size() == 3)
return py::cast(gp_Vec(p[0], p[1], p[2])); return py::cast(gp_Vec(p[0], p[1], p[2]));
throw Exception("OCC-Vecs only in 2D or 3D"); throw Exception("OCC-Vecs only in 2D or 3D");
}); }, py::arg("v"), "create 2d or 3d OCC vector");
m.def("Dir", [](double x, double y) { return gp_Dir2d(x,y); }); m.def("Dir", [](double x, double y) { return gp_Dir2d(x,y); },
m.def("Dir", [](double x, double y, double z) { return gp_Dir(x,y,z); }); py::arg("x"), py::arg("y"), "create 2d OCC direction");
m.def("Dir", [](double x, double y, double z) { return gp_Dir(x,y,z); },
py::arg("x"), py::arg("y"), py::arg("z"), "create 3d OCC direction");
m.def("Dir", [](std::vector<double> p) m.def("Dir", [](std::vector<double> p)
{ {
if (p.size() == 2) if (p.size() == 2)
@ -274,12 +280,12 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
if (p.size() == 3) if (p.size() == 3)
return py::cast(gp_Dir(p[0], p[1], p[2])); return py::cast(gp_Dir(p[0], p[1], p[2]));
throw Exception("OCC-Dirs only in 2D or 3D"); throw Exception("OCC-Dirs only in 2D or 3D");
}); }, py::arg("d"), "create 2d or 3d OCC direction");
py::class_<gp_Ax2d>(m, "gp_Ax2d") py::class_<gp_Ax2d>(m, "gp_Ax2d", "2d OCC coordinate system")
.def(py::init([](gp_Pnt2d p, gp_Dir2d d) { .def(py::init([](gp_Pnt2d p, gp_Dir2d d) {
return gp_Ax2d(p,d); return gp_Ax2d(p,d);
}), py::arg("p")=gp_Pnt2d(0,0), py::arg("d")=gp_Dir2d(1,0)) }), py::arg("p")=gp_Pnt2d(0,0), py::arg("d")=gp_Dir2d(1,0))
@ -353,11 +359,9 @@ DLL_HEADER void ExportNgOCCBasic(py::module &m)
py::implicitly_convertible<gp_Ax3, gp_Ax2>(); py::implicitly_convertible<gp_Ax3, gp_Ax2>();
static gp_Vec ex(1,0,0), ey(0,1,0), ez(0,0,1); m.attr("X") = py::cast(gp_Vec(1,0,0));
m.attr("X") = py::cast(&ex); m.attr("Y") = py::cast(gp_Vec(0,1,0));
m.attr("Y") = py::cast(&ey); m.attr("Z") = py::cast(gp_Vec(0,0,1));
m.attr("Z") = py::cast(&ez);
} }

View File

@ -737,6 +737,14 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m)
}, py::arg("axes"), }, py::arg("axes"),
"copy shape, and mirror over plane defined by 'axes'") "copy shape, and mirror over plane defined by 'axes'")
.def("Mirror", [] (const TopoDS_Shape & shape, const gp_Ax1 & ax)
{
gp_Trsf trafo;
trafo.SetMirror(ax);
return BRepBuilderAPI_Transform(shape, trafo).Shape();
}, py::arg("axes"),
"copy shape, and mirror around axis 'axis'")
.def("Scale", [](const TopoDS_Shape & shape, const gp_Pnt p, double s) .def("Scale", [](const TopoDS_Shape & shape, const gp_Pnt p, double s)
{ {
gp_Trsf trafo; gp_Trsf trafo;
@ -1623,21 +1631,23 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m)
m.def("Ellipse", [] (const gp_Ax2d & ax, double major, double minor) -> Handle(Geom2d_Curve) m.def("Ellipse", [] (const gp_Ax2d & ax, double major, double minor) -> Handle(Geom2d_Curve)
{ {
return new Geom2d_Ellipse(ax, major, minor); return new Geom2d_Ellipse(ax, major, minor);
}); }, py::arg("axes"), py::arg("major"), py::arg("minor"), "create 2d ellipse curve");
m.def("Segment", [](gp_Pnt2d p1, gp_Pnt2d p2) -> Handle(Geom2d_Curve) { m.def("Segment", [](gp_Pnt2d p1, gp_Pnt2d p2) -> Handle(Geom2d_Curve) {
return Handle(Geom2d_TrimmedCurve)(GCE2d_MakeSegment(p1, p2));
/*
Handle(Geom2d_TrimmedCurve) curve = GCE2d_MakeSegment(p1, p2); Handle(Geom2d_TrimmedCurve) curve = GCE2d_MakeSegment(p1, p2);
return curve; return curve;
// return BRepBuilderAPI_MakeEdge(curve).Edge(); */
// return GCE2d_MakeSegment(p1, p2); }, py::arg("p1"), py::arg("p2"), "create 2d line curve");
});
m.def("Circle", [](gp_Pnt2d p1, double r) -> Handle(Geom2d_Curve) { m.def("Circle", [](gp_Pnt2d p1, double r) -> Handle(Geom2d_Curve) {
return Handle(Geom2d_Circle)(GCE2d_MakeCircle(p1, r));
/*
Handle(Geom2d_Circle) curve = GCE2d_MakeCircle(p1, r); Handle(Geom2d_Circle) curve = GCE2d_MakeCircle(p1, r);
return curve; return curve;
// gp_Ax2d ax; ax.SetLocation(p1); */
// return new Geom2d_Circle(ax, r); }, py::arg("c"), py::arg("r"), "create 2d circle curve");
});
m.def("Glue", [] (const std::vector<TopoDS_Shape> shapes) -> TopoDS_Shape m.def("Glue", [] (const std::vector<TopoDS_Shape> shapes) -> TopoDS_Shape
{ {
@ -1743,12 +1753,14 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m)
m.def("ArcOfCircle", [](gp_Pnt p1, gp_Pnt p2, gp_Pnt p3) { m.def("ArcOfCircle", [](gp_Pnt p1, gp_Pnt p2, gp_Pnt p3) {
Handle(Geom_TrimmedCurve) curve = GC_MakeArcOfCircle(p1, p2, p3); Handle(Geom_TrimmedCurve) curve = GC_MakeArcOfCircle(p1, p2, p3);
return BRepBuilderAPI_MakeEdge(curve).Edge(); return BRepBuilderAPI_MakeEdge(curve).Edge();
}, py::arg("p1"), py::arg("p2"), py::arg("p3")); }, py::arg("p1"), py::arg("p2"), py::arg("p3"),
"create arc from p1 through p2 to p3");
m.def("ArcOfCircle", [](gp_Pnt p1, gp_Vec v, gp_Pnt p2) { m.def("ArcOfCircle", [](gp_Pnt p1, gp_Vec v, gp_Pnt p2) {
Handle(Geom_TrimmedCurve) curve = GC_MakeArcOfCircle(p1, v, p2); Handle(Geom_TrimmedCurve) curve = GC_MakeArcOfCircle(p1, v, p2);
return BRepBuilderAPI_MakeEdge(curve).Edge(); return BRepBuilderAPI_MakeEdge(curve).Edge();
}, py::arg("p1"), py::arg("v"), py::arg("p2")); }, py::arg("p1"), py::arg("v"), py::arg("p2"),
"create arc from p1, with tangent vector v, to point p2");
m.def("BSplineCurve", [](std::vector<gp_Pnt> vpoles, int degree) { m.def("BSplineCurve", [](std::vector<gp_Pnt> vpoles, int degree) {
@ -1757,48 +1769,32 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m)
TColStd_Array1OfReal knots(0, vpoles.size()+degree); TColStd_Array1OfReal knots(0, vpoles.size()+degree);
TColStd_Array1OfInteger mult(0, vpoles.size()+degree); TColStd_Array1OfInteger mult(0, vpoles.size()+degree);
int cnt = 0; int cnt = 0;
try
for (int i = 0; i < vpoles.size(); i++)
{
poles.SetValue(i, vpoles[i]);
knots.SetValue(i, i);
mult.SetValue(i,1);
}
for (int i = vpoles.size(); i < vpoles.size()+degree+1; i++)
{ {
for (int i = 0; i < vpoles.size(); i++)
{
poles.SetValue(i, vpoles[i]);
knots.SetValue(i, i);
mult.SetValue(i,1);
}
for (int i = vpoles.size(); i < vpoles.size()+degree+1; i++)
{
knots.SetValue(i, i); knots.SetValue(i, i);
mult.SetValue(i, 1); mult.SetValue(i, 1);
}
Handle(Geom_Curve) curve = new Geom_BSplineCurve(poles, knots, mult, degree);
return BRepBuilderAPI_MakeEdge(curve).Edge();
}
catch (Standard_Failure & e)
{
stringstream errstr;
e.Print(errstr);
throw NgException("cannot create spline: "+errstr.str());
} }
Handle(Geom_Curve) curve = new Geom_BSplineCurve(poles, knots, mult, degree);
return BRepBuilderAPI_MakeEdge(curve).Edge();
}); });
m.def("BezierCurve", [](std::vector<gp_Pnt> vpoles) { m.def("BezierCurve", [](std::vector<gp_Pnt> vpoles) {
TColgp_Array1OfPnt poles(0, vpoles.size()-1); TColgp_Array1OfPnt poles(0, vpoles.size()-1);
try
{
for (int i = 0; i < vpoles.size(); i++)
poles.SetValue(i, vpoles[i]);
Handle(Geom_Curve) curve = new Geom_BezierCurve(poles); for (int i = 0; i < vpoles.size(); i++)
return BRepBuilderAPI_MakeEdge(curve).Edge(); poles.SetValue(i, vpoles[i]);
}
catch (Standard_Failure & e) Handle(Geom_Curve) curve = new Geom_BezierCurve(poles);
{ return BRepBuilderAPI_MakeEdge(curve).Edge();
stringstream errstr; }, py::arg("points"), "create Bezier curve");
e.Print(errstr);
throw NgException("cannot create Bezier-spline: "+errstr.str());
}
});
m.def("SplineApproximation", [](std::vector<gp_Pnt> pnts, double tol) { m.def("SplineApproximation", [](std::vector<gp_Pnt> pnts, double tol) {
TColgp_Array1OfPnt points(0, pnts.size()-1); TColgp_Array1OfPnt points(0, pnts.size()-1);
@ -1806,7 +1802,8 @@ DLL_HEADER void ExportNgOCCShapes(py::module &m)
points.SetValue(i, pnts[i]); points.SetValue(i, pnts[i]);
GeomAPI_PointsToBSpline builder(points); GeomAPI_PointsToBSpline builder(points);
return BRepBuilderAPI_MakeEdge(builder.Curve()).Edge(); return BRepBuilderAPI_MakeEdge(builder.Curve()).Edge();
}, py::arg("points"), py::arg("tol"), "Generate spline-curve approximating list of points up to tolerance tol"); }, py::arg("points"), py::arg("tol"),
"Generate spline-curve approximating list of points up to tolerance tol");
m.def("MakeFillet", [](TopoDS_Shape shape, std::vector<TopoDS_Shape> edges, double r) { m.def("MakeFillet", [](TopoDS_Shape shape, std::vector<TopoDS_Shape> edges, double r) {