在 VB.NET 中,可以使用以下代码将实体的 ObjectId 和对应的实体坐标点保存到一个数组中:

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

Public Class MyCommands
    <CommandMethod("SaveEntityData")>
    Public Sub SaveEntityData()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor

        ' 提示用户选择实体
        Dim peo As New PromptEntityOptions("请选择一个实体")
        peo.SetRejectMessage("请选择有效的实体")
        peo.AddAllowedClass(GetType(Entity), False)
        Dim per As PromptEntityResult = ed.GetEntity(peo)

        If per.Status = PromptStatus.OK Then
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim ent As Entity = TryCast(tr.GetObject(per.ObjectId, OpenMode.ForRead), Entity)

                If ent IsNot Nothing Then
                    ' 获取实体的坐标点
                    Dim points As New List(Of Point3d)()

                    If TypeOf ent Is Polyline Then
                        Dim polyline As Polyline = TryCast(ent, Polyline)

                        For i As Integer = 0 To polyline.NumberOfVertices - 1
                            points.Add(polyline.GetPoint3dAt(i))
                        Next
                    ElseIf TypeOf ent Is Circle Then
                        Dim circle As Circle = TryCast(ent, Circle)
                        points.Add(circle.Center)
                    End If

                    ' 保存实体的 ObjectId 和坐标点到数组
                    Dim entityData As New List(Of EntityData)()
                    entityData.Add(New EntityData(ent.ObjectId, points.ToArray()))

                    ' 输出实体数据
                    For Each data As EntityData In entityData
                        ed.WriteMessage('实体 ObjectId: ' + data.ObjectId + '; 坐标点: ' + String.Join(",", data.Points))
                    Next
                End If

                tr.Commit()
            End Using
        End If
    End Sub

    Private Class EntityData
        Public ReadOnly Property ObjectId As ObjectId
        Public ReadOnly Property Points As Point3d()

        Public Sub New(objectId As ObjectId, points As Point3d())
            Me.ObjectId = objectId
            Me.Points = points
        End Sub
    End Class
End Class

使用上述代码,用户可以选择 Polyline 或 Circle 等实体,然后将其 ObjectId 和坐标点保存到一个数组中。在上述示例中,我们只支持 Polyline 和 Circle 实体类型,如果需要支持其他类型的实体,请根据实际需求进行修改。

VB.NET 在 AutoCAD 中获取实体 ObjectId 和坐标点

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

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