|
[code] static int ads_getcentroid(void)
{
Acad::ErrorStatus es;
ads_point pt;
ads_name ename;
// pick a polyline if not exit
if (acedEntSel ("\nSelect polyline: ", ename, pt) != RTNORM) return RSERR;
// get the object id if not exit
AcDbObjectId objId;
if (acdbGetObjectId (objId, ename) != Acad::eOk) return RSERR;
// open our object for read
AcDbObjectPointer<AcDbCurve> pCurve(objId, AcDb::kForRead);
// try to opened if not exit with error
if ((es = pCurve.openStatus()) != Acad::eOk) {
acutPrintf("\npCurve.openStatus()=%s", acadErrorStatusText(es));
return RSERR;
}
// cast our object into the polyline
AcDbPolyline *pPoly = AcDbPolyline::cast(pCurve.object());
// if not closed exit
if (!pPoly->isClosed()) return RSERR;
AcDbVoidPtrArray array;
AcDbVoidPtrArray regions;
array.append(pPoly);
AcDbRegion::createFromCurves(array, regions);
assert(regions.length() == 1);
AcDbRegion *pRegion = AcDbRegion::cast((AcRxObject*)regions[0]);
assert(pRegion != NULL);
AcGePoint3d origin;
AcGeVector3d xAxis;
AcGeVector3d yAxis;
double perimeter;
double area;
AcGePoint2d centroid;
double momInertia[2];
double prodInertia;
double prinMoments[2];
AcGeVector2d prinAxes[2];
double radiiGyration[2];
AcGePoint2d extentsLow;
AcGePoint2d extentsHigh;
origin += xAxis*centroid[0];
origin += yAxis*centroid[1];
centroid.x = origin.x;
centroid.y = origin.y;
AcGePlane plane;
pRegion->getPlane(plane);
plane.getCoordSystem(origin, xAxis, yAxis);
if (pRegion->getAreaProp(origin,xAxis,yAxis,perimeter,area,centroid,momInertia,prodInertia,prinMoments,prinAxes,radiiGyration,extentsLow,extentsHigh) == Acad::eOk) {
acedRetPoint(asDblArray(centroid));
}
for (int i = 0; i < regions.length(); i++) {
delete (AcRxObject*)regions[i];
}
return (RSRSLT) ;
}[/code] |
|