如何使用 VBA 从 Autocad 块中获取所有属性?

问题描述 投票:0回答:1

所以,我一直在尝试遍历 Autocad 绘图中的所有块以找到名为“FL01”的块,我可以做到,但我也想获取他的所有属性以将其中一些属性连接成一个字符串。这是代码:

Dim cellValue As String
cellValue = ActiveSheet.Range("B5").Value
' Converte o valor em uma string
cellValue = CStr(cellValue)
Dim acadApp As Object
Set acadApp = GetObject(, "AutoCAD.Application")

' Abre um documento DWG
Dim acadDoc As Object
Set acadDoc = acadApp.Documents.Open(cellValue)
' Itera sobre os blocos do documento
Dim minhaColecao As Collection
Set minhaColecao = New Collection
Dim block As Object
Dim atts() As Variant
For Each block In acadDoc.Blocks
    If block.Name = "FL01" Then
    atts = block.GetAttributes ' The error is right Here
    Dim i As Long
    Dim aux As String
    For i = LBound(atts) To UBound(atts)
        If atts(i).TagString = "TIPO" Then
            aux = atts(i).TextString
        ElseIf atts(i).TagString = "SEQ-1" Then
            aux = aux & "-" & atts(i).TextString
        ElseIf atts(i).TagString = "SEQ-2" Then
            aux = aux & "-" & atts(i).TextString
            Exit For
        End If
    Next i
    minhaColecao.Add aux
    End If
    ' Faça algo com o bloco
    Next block
Range("A1").Select
Dim item As Variant
For Each item In minhaColecao
    ActiveCell.Value = item
    ActiveCell.Offset(1, 0).Select ' Move para a próxima linha
Next item
' Fecha o documento
acadDoc.Close False 

顺便说一下,单元格 B5 是 .dwg 文件的路径。整个计划是连接特定的属性,将它们添加到一个集合中,然后将该集合写入一个 excel 文件。

当我运行代码时,它声称“块”不接受“GetAttributes”方法。

excel vba autocad
1个回答
0
投票

您的代码正在遍历 AutoCAD Blocks Collection,其中包含 Layouts 和 Block Definitions 等容器。

相反,您需要遍历布局容器的内容(如果您对嵌套属性块感兴趣,则需要遍历块定义容器),直到遇到属性块 references 可以使用

GetAttributes
方法。

类似的东西:

For Each block In acadDoc.Blocks
    If block.IsLayout Then
        For Each obj In block
            If obj.ObjectName = "AcDbBlockReference" Then
                If obj.Name = "FL01" Then
                    atts = obj.GetAttributes
                    ...
© www.soinside.com 2019 - 2024. All rights reserved.