autocad - .net中有一种方法可以获取折线的质心吗?

标签 autocad autocad-plugin

我想得到polyline2d的质心。我有一条来自 TraceBoundary 的折线,如下所示:

DBObjectCollection objs = Application.DocumentManager.MdiActiveDocument.Editor.TraceBoundary(Point, false);
Polyline polyline = (Polyline)objs[0];
Point2d centroid = ***The missing part i need any way to get center of gravity of the above polyline as point2d***

我之前用 autolisp 这样做过:

(setq arob (entlast))
(setq enpol (vlax-ename->vla-object arob))
(setq cgpoint (vlax-get-property enpol 'Centroid))

我有一个数学解决方案,但当折线带有一些曲线时,它无效。

我不会使用这个功能:

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);
    }

我不想使用 AutoLisp。有没有办法在 .NET 中做到这一点?

最佳答案

我找到了这个解决方案,它对我有效:

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;
}

关于autocad - .net中有一种方法可以获取折线的质心吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6722630/

相关文章:

C# 无法绑定(bind)到目标方法

.net - 加载AutoCAD插件的顺序是什么?

c# - 在运行时加载程序集,Autocad 插件示例

c# - 在AutoCAD中使用Editor类执行命令

C# 绘制 Oracle Spatial 几何图形

c# - 导入多个 DXF 文件时实体错误

c# - 获取运行 Windows 窗体控件的应用程序

autocad - 测量距离时启用 "dynamic input"

dialog - 如何正确地将 DCL 链接到 AutoLisp?

c# - 在 C# 中将 RGB 颜色转换为最接近的 ACI 颜色