Expose findCentroid to Python

This commit is contained in:
wandererfan
2018-08-07 08:19:16 -04:00
committed by wmayer
parent 603c294d47
commit 7028da17b5
3 changed files with 46 additions and 1 deletions

View File

@@ -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<TopoShapePy*>(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<Base::VectorPy*>(pcObjDir)->value();
Base::Vector3d c = TechDrawGeometry::findCentroidVec(shape,dir);
PyObject* result = nullptr;
result = new Base::VectorPy(new Base::Vector3d(c));
return Py::asObject(result);
}
};

View File

@@ -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,

View File

@@ -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,