GetPolyLineCentroid
public static Point2d GetPolyLineCentroid(Polyline polyline)
{
double area = polyline.Area;
double cx = 0.0;
double cy = 0.0;
double X0 = 0.0;
double Y0 = 0.0;
double X1 = 0.0;
double Y1 = 0.0;
for (int i = 0; i < polyline.NumberOfVertices - 1; i++)
{
X0 = polyline.GetPoint2dAt(i).X;
Y0 = polyline.GetPoint2dAt(i).Y;
X1 = polyline.GetPoint2dAt(i + 1).X;
Y1 = polyline.GetPoint2dAt(i + 1).Y;
cx += (X0 + X1) * (X0 * Y1 - X1 * Y0);
cy += (Y0 + Y1) * (X0 * Y1 - X1 * Y0);
}
// last Point
int Lv = polyline.NumberOfVertices - 1;
X0 = polyline.GetPoint2dAt(Lv).X;
Y0 = polyline.GetPoint2dAt(Lv).Y;
X1 = polyline.GetPoint2dAt(0).X;
Y1 = polyline.GetPoint2dAt(0).Y;
cx += (X0 + X1) * (X0 * Y1 - X1 * Y0);
cy += (Y0 + Y1) * (X0 * Y1 - X1 * Y0);
cx /= 6 * area;
cy /= 6 * area;
return new Point2d(cx, cy);
}
public static Point2d GetPolyLineCentroid(DBObjectCollection objs)
{
Solid3d Solid = new Solid3d();
Solid.Extrude(((Region)Region.CreateFromCurves(objs)[0]), 1, 0);
Point2d centroid = new Point2d(Solid.MassProperties.Centroid.X, Solid.MassProperties.Centroid.Y);
Solid.Dispose();
return centroid;
}
Python AutoCAD 系统设置
欢迎关注微信公众账号ByCAD