Iterate thru block and it's nested blocks

Iterate thru block and it's nested blocks

mohandis2
Advocate Advocate
451 Views
1 Reply
Message 1 of 2

Iterate thru block and it's nested blocks

mohandis2
Advocate
Advocate

I want to iterate thru a selected block reference, change the layer and color of it's entities, if a nested blocks found, iterate thru the same way and change the layer and color of it's entities.

Can anyone help?

0 Likes
Accepted solutions (1)
452 Views
1 Reply
Reply (1)
Message 2 of 2

Ed__Jobe
Mentor
Mentor
Accepted solution

Here's my sub:

Public Sub BlockEntsByLayer()
    Dim oblk As AcadBlock
    Dim oBlk1 As AcadBlock
    Dim oBlkRef As AcadBlockReference
    Dim oBlkRef1 As AcadBlockReference
    Dim oEnt As AcadEntity
    Dim oEnt1 As AcadEntity
    Dim SS As AcadSelectionSet

    Set SS = GetSS_BlockFilter 'search this forum for this sub.
    For Each oBlkRef In SS
        Set oblk = ThisDrawing.Blocks(oBlkRef.Name)
        If Not oblk.IsXRef Then
            For Each oEnt In oblk
                If TypeOf oEnt Is AcadBlockReference Then
                    Set oBlkRef1 = oEnt
                    Set oBlk1 = ThisDrawing.Blocks(oBlkRef1.Name)
                    For Each oEnt1 In oBlk1
                        With oEnt1
                            If Not ThisDrawing.Layers(.Layer).Lock Then
                                .Layer = "0"
                                .color = acByLayer
                            End If
                        End With
                    Next oEnt1
                Else
                    With oEnt
                        If Not ThisDrawing.Layers(.Layer).Lock Then
                            .Layer = "0"
                            .color = acByLayer
                        End If
                    End With
                End If
            Next oEnt
        End If
    Next oBlkRef
    ThisDrawing.Regen acAllViewports
End Sub

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature