Accessing Blocks Inside Blocks

Accessing Blocks Inside Blocks

felix.cortes5K3Y2
Advocate Advocate
491 Views
4 Replies
Message 1 of 5

Accessing Blocks Inside Blocks

felix.cortes5K3Y2
Advocate
Advocate

Hi all,

 

I am writing a program that analyzes the attributes of blocks in an layout. However, I cannot find the way to access AcadBlockReference's of blocks inside a block. Anyone have an idea how to approach this? Here's my lines of code: 

 

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System.Runtime.InteropServices

 

Sub Main()
Dim acad As AcadApplication = Marshal.GetActiveObject("Autocad.Application")
Dim acadDoc As AcadDocument = acad.ActiveDocument
Dim layout As AcadLayout = acadDoc.Layouts.Item("Model")
End Sub

Sub RunProgram(Lay As AcadLayout)
Dim Ent As AcadEntity
Dim Blk As AcadBlockReference
For i As Integer = 0 To Lay.Block.Count
Ent = Lay.Block.Item(i)
If TypeOf Ent Is AcadBlockReference Then
Call ProcessBlock(Ent)
End If
Next
End Sub

Sub ProcessBlock(Blk As AcadBlockReference)
Debug.Print("Name: {0}", Blk.EffectiveName)
'if block has blocks inside, ProcessBlock(New block)
End Sub

 

 

Thanks!

0 Likes
492 Views
4 Replies
Replies (4)
Message 2 of 5

hippe013
Advisor
Advisor

Here is a quick example of navigating through nested blocks. 

 Dim block As BlockTableRecord
            Using tr As Transaction = db.TransactionManager.StartTransaction
                Dim blk As BlockReference = tr.GetObject(res.ObjectId, OpenMode.ForRead)

                If blk.IsDynamicBlock Then
                    block = tr.GetObject(blk.DynamicBlockTableRecord, OpenMode.ForRead)
                Else
                    block = tr.GetObject(blk.BlockTableRecord, OpenMode.ForRead)
                End If

                ed.WriteMessage(vbCrLf & "Block Name: " & block.Name)

                PrintNestedBlocks(block, tr, 0)

                tr.Commit()
            End Using

 

Public Sub PrintNestedBlocks(block As BlockTableRecord, tr As Transaction, nestLevel As Integer)
            For Each id As ObjectId In block
                Dim ent As Entity = tr.GetObject(id, OpenMode.ForRead)
                If TypeOf ent Is BlockReference Then
                    Dim blkRef As BlockReference = TryCast(ent, BlockReference)
                    If blkRef.IsDynamicBlock Then
                        block = tr.GetObject(blkRef.DynamicBlockTableRecord, OpenMode.ForRead)
                    Else
                        block = tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead)
                    End If

                    Dim pad As String = ""
                    For i As Integer = 0 To nestLevel
                        pad += vbTab
                    Next

                    ed.WriteMessage(vbCrLf & pad & "Block Name: " & block.Name)

                    PrintNestedBlocks(block, tr, nestLevel + 1)

                End If
            Next
        End Sub

 

 

0 Likes
Message 3 of 5

felix.cortes5K3Y2
Advocate
Advocate

Is this method using different dll's ? I have to keep the program under the Autodesk.AutoCAD.Interop and Interop.Common dll's

0 Likes
Message 4 of 5

hippe013
Advisor
Advisor

Are you looking for something like this? 

 

Private Sub PrintNestedNames(blk As AcadBlock, acdb As AcadDatabase, ed As Editor)
            ed.WriteMessage(vbCrLf & "Block Name: " & blk.Name)
            For Each ent As AcadEntity In blk
                If TypeOf ent Is AcadBlockReference Then
                    Dim bRef As AcadBlockReference = ent
                    Dim b As AcadBlock = GetAcadBlock(bRef.EffectiveName, acdb)
                    If b IsNot Nothing Then
                        PrintNestedNames(b, acdb, ed)
                    End If
                End If
            Next
        End Sub

        Private Function GetAcadBlock(name As String, acdb As AcadDatabase) As AcadBlock
            For Each blk As AcadBlock In acdb.Blocks
                If blk.Name = name Then
                    Return blk
                End If
            Next
            Return Nothing
        End Function

 

 

0 Likes
Message 5 of 5

felix.cortes5K3Y2
Advocate
Advocate

Doesn't seem to work, I need to filter through each AcadBlockReference inside a layout to search for specific blocks and inside nested blocks as well

0 Likes