From 2dd9e67887880494a518f530a6705ef4a4482760 Mon Sep 17 00:00:00 2001 From: wandererfan Date: Tue, 7 Aug 2018 08:19:16 -0400 Subject: [PATCH] Expose findCentroid to Python --- src/Mod/TechDraw/App/AppTechDrawPy.cpp | 35 ++++++++++++++++++++++++- src/Mod/TechDraw/App/GeometryObject.cpp | 10 +++++++ src/Mod/TechDraw/App/GeometryObject.h | 2 ++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Mod/TechDraw/App/AppTechDrawPy.cpp b/src/Mod/TechDraw/App/AppTechDrawPy.cpp index e0e11c8a42..38590e8f78 100644 --- a/src/Mod/TechDraw/App/AppTechDrawPy.cpp +++ b/src/Mod/TechDraw/App/AppTechDrawPy.cpp @@ -110,6 +110,9 @@ public: add_varargs_method("writeDXFPage",&Module::writeDXFPage, "writeDXFPage(page,filename): Exports a DrawPage to a DXF file." ); + add_varargs_method("findCentroid",&Module::findCentroid, + "vector = findCentroid(shape,direction): finds geometric centroid of shape looking in direction." + ); initialize("This is a module for making drawings"); // register with Python } virtual ~Module() {} @@ -693,8 +696,38 @@ private: } return Py::None(); - } + } + Py::Object findCentroid(const Py::Tuple& args) + { + PyObject *pcObjShape; + PyObject *pcObjDir; + if (!PyArg_ParseTuple(args.ptr(), "OO", &pcObjShape, + &pcObjDir)) { + throw Py::TypeError("expected (shape,direction"); + } + + if (!PyObject_TypeCheck(pcObjShape, &(TopoShapePy::Type))) { + throw Py::TypeError("expected arg1 to be 'Shape'"); + } + + if (!PyObject_TypeCheck(pcObjDir, &(Base::VectorPy::Type))) { + throw Py::TypeError("expected arg2 to be 'Vector'"); + } + + TopoShapePy* pShape = static_cast(pcObjShape); + if (!pShape) { + Base::Console().Error("TechDraw::findCentroid - input shape is null\n"); + return Py::None(); + } + + const TopoDS_Shape& shape = pShape->getTopoShapePtr()->getShape(); + Base::Vector3d dir = static_cast(pcObjDir)->value(); + Base::Vector3d c = TechDrawGeometry::findCentroidVec(shape,dir); + PyObject* result = nullptr; + result = new Base::VectorPy(new Base::Vector3d(c)); + return Py::asObject(result); + } }; diff --git a/src/Mod/TechDraw/App/GeometryObject.cpp b/src/Mod/TechDraw/App/GeometryObject.cpp index 5b34f0a21e..acbcd449d2 100644 --- a/src/Mod/TechDraw/App/GeometryObject.cpp +++ b/src/Mod/TechDraw/App/GeometryObject.cpp @@ -589,6 +589,7 @@ gp_Ax2 TechDrawGeometry::getViewAxis(const Base::Vector3d origin, return viewAxis; } +//TODO: cardinal directions don't seem to affect result. is this right? //! Returns the centroid of shape, as viewed according to direction gp_Pnt TechDrawGeometry::findCentroid(const TopoDS_Shape &shape, const Base::Vector3d &direction) @@ -617,6 +618,15 @@ gp_Pnt TechDrawGeometry::findCentroid(const TopoDS_Shape &shape, return gp_Pnt(x, y, z); } +Base::Vector3d TechDrawGeometry::findCentroidVec(const TopoDS_Shape &shape, + const Base::Vector3d &direction) +{ + gp_Pnt p = TechDrawGeometry::findCentroid(shape,direction); + Base::Vector3d result(p.X(),p.Y(),p.Z()); + return result; +} + + //!scales & mirrors a shape about a center TopoDS_Shape TechDrawGeometry::mirrorShape(const TopoDS_Shape &input, const gp_Pnt& inputCenter, diff --git a/src/Mod/TechDraw/App/GeometryObject.h b/src/Mod/TechDraw/App/GeometryObject.h index 193023b10f..3d979e595f 100644 --- a/src/Mod/TechDraw/App/GeometryObject.h +++ b/src/Mod/TechDraw/App/GeometryObject.h @@ -66,6 +66,8 @@ TopoDS_Shape TechDrawExport moveShape(const TopoDS_Shape &input, //! Returns the centroid of shape, as viewed according to direction gp_Pnt TechDrawExport findCentroid(const TopoDS_Shape &shape, const Base::Vector3d &direction); +Base::Vector3d TechDrawExport findCentroidVec(const TopoDS_Shape &shape, + const Base::Vector3d &direction); gp_Ax2 TechDrawExport getViewAxis(const Base::Vector3d origin, const Base::Vector3d& direction,