要获取Polyline2D的坐标,您可以使用以下代码示例:

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry

Public Sub GetPolylineCoordinates()
    ' 获取当前文档和数据库
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database

    ' 创建一个编辑器
    Dim ed As Editor = doc.Editor

    ' 提示用户选择一个Polyline2D
    Dim peo As New PromptEntityOptions("请选择一个Polyline2D")
    peo.SetRejectMessage("请选择一个有效的Polyline2D")
    peo.AddAllowedClass(GetType(Polyline2d), False)

    Dim per As PromptEntityResult = ed.GetEntity(peo)

    If per.Status = PromptStatus.OK Then
        ' 打开Polyline2D的事务读取
        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Dim polyline2d As Polyline2d = tr.GetObject(per.ObjectId, OpenMode.ForRead)

            ' 遍历Polyline2D的所有顶点
            For Each vertexId As ObjectId In polyline2d
                Dim vertex As Vertex2d = tr.GetObject(vertexId, OpenMode.ForRead)

                ' 获取顶点坐标并输出
                ed.WriteMessage("坐标:{0}", vertex.Position)
            Next
        End Using
    End If
End Sub

请注意,在使用此代码之前,您需要将对AutoCAD和AutoCAD的参考添加到您的项目中。


原文地址: https://www.cveoy.top/t/topic/i7AQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录