以下是一个示例代码,用于在 AutoCAD 中提取所有 'dmx' 图层的交点坐标,并将每条多段线的 ID 和坐标保存到数组中,并输出到文本文件。

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

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

        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Dim ed As Editor = doc.Editor

            ' 提示用户选择 'dmx' 图层
            Dim filter As New SelectionFilter(New TypedValue() {New TypedValue(CInt(DxfCode.LayerName), 'dmx')})
            Dim selOpts As New PromptSelectionOptions With {
                .MessageForAdding = '选择 'dmx' 图层的多段线:'
            }
            Dim selRes As PromptSelectionResult = ed.GetSelection(selOpts, filter)
            If selRes.Status <> PromptStatus.OK Then
                Exit Sub
            End If

            Dim selSet As SelectionSet = selRes.Value
            Dim intersectionPoints As New List(Of Point3d())
            Dim polylineIds As New List(Of ObjectId())

            ' 遍历选择集中的每个多段线
            For Each id As ObjectId In selSet.GetObjectIds()
                Dim polyline As Polyline = TryCast(trans.GetObject(id, OpenMode.ForRead), Polyline)
                If polyline IsNot Nothing Then
                    polylineIds.Add(polyline.ObjectId)

                    ' 检查多段线是否封闭
                    If polyline.Closed Then
                        ' 获取多段线的交点
                        Dim intersections As Point3dCollection = New Point3dCollection()
                        For i As Integer = 0 To polyline.NumberOfVertices - 2
                            For j As Integer = i + 1 To polyline.NumberOfVertices - 1
                                Dim intersectPoint As Point3d = New Point3d()
                                If polyline.IntersectWith(polyline, Intersect.OnBothOperands, intersectPoint, IntPtr.Zero, IntPtr.Zero) <> False Then
                                    intersectionPoints.Add(intersectPoint)
                                End If
                            Next
                        Next
                    End If
                End If
            Next

            ' 输出到文本文件
            Dim filePath As String = 'C:\temp\dmx_intersections.txt'
            Using sw As StreamWriter = New StreamWriter(filePath)
                For i As Integer = 0 To intersectionPoints.Count - 1
                    sw.WriteLine($'多段线 ID: {polylineIds(i).ToString()}, 坐标: {intersectionPoints(i).ToString()}')
                Next
            End Using

            trans.Commit()
        End Using
    End Sub
End Class

请确保已添加对 Autodesk.AutoCAD.ApplicationServicesAutodesk.AutoCAD.DatabaseServicesAutodesk.AutoCAD.EditorInputAutodesk.AutoCAD.Geometry 命名空间的引用,并将代码放置在 AutoCAD 插件项目中。

运行插件后,它将提示用户选择 'dmx' 图层中的多段线。然后,它将提取每条多段线的交点坐标,并将 ID 和坐标保存到数组中。最后,它将将数组中的内容输出到名为 dmx_intersections.txt 的文本文件中。请根据需要根据实际情况更改文件路径。

AutoCAD VBA 代码:提取 DMX 图层多段线交点坐标

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

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