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

发布于 2024-11-24 09:48:10 字数 1698 浏览 1 评论 0原文

我想得到 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 中做到这一点?

I want to get the centroid of polyline2d. I have a polyline from TraceBoundary like so:

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***

I was doing that before with autolisp:

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

I have a mathematical solution, but it's not valid when the polyline comes with some curves.

I won't use this function:

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

I don't want to use AutoLisp. Is there a way to do this in .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-12-01 09:48:10

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

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

I found this solution and it's valid for me:

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;
}
朮生 2024-12-01 09:48:10

对于 AutoCAD <= 2013,使用 .NET (Region.CreateFromCurves) 创建一个区域,并使用 COM 和 HandleToObject 方法获取对其的引用。在区域 ActiveX 对象上,有一个 Centroid 属性。

对于 AutoCAD > 2013 年,Region 类上有一个 AreaProperties 方法,它返回带有 Centroid 属性的 RegionAreaProperties 结构。

For AutoCAD <= 2013, create a region with .NET (Region.CreateFromCurves), and get a reference to it with COM and the HandleToObject method. On the region ActiveX object, there is a Centroid property.

For AutoCAD > 2013, there is a AreaProperties method on the Region class that returns a RegionAreaProperties structure with a Centroid property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文