Loop through all Dynamic blocks and set the Block property “Allow exploding” to “Yes”

Loop through all Dynamic blocks and set the Block property “Allow exploding” to “Yes”

Mike_Y2
Advocate Advocate
354 Views
2 Replies
Message 1 of 3

Loop through all Dynamic blocks and set the Block property “Allow exploding” to “Yes”

Mike_Y2
Advocate
Advocate

Hi.

I regularly receive drawings from a client that contain a dozen or so Dynamic blocks. Each of the Dynamic blocks has the Block property “Allow exploding” set to “No”.

 

I was wondering if anyone has (or can write) a macro that will loop through all Dynamic blocks in the current drawing and set the Block property “Allow exploding” to “Yes”?

 

Any help would be greatly appreciated

0 Likes
355 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

I feel you did not ask your question in full context that matters to you. 

 

It is simple to set AcadBlock (block definition) property "Explodable" to True/False in code, but it is not exactly the same as you set it in the AutoCAD's Block Editor, in terms of dynamic block.

 

When doing it in AutoCAD, with Block Editor, if you change "Allow Exploding" of the dynamic block definition to "Yes", in the Properties window, then all the dynamic block's references become explodable.

 

However, if you use code to change the block definition's Explodable property, like this:

 

    Dim blk As AcadBlock
    For Each blk In ThisDrawing.Blocks
        If UCase(blk.Name) = "TESTBLOCK" Then
            If Not blk.Explodable Then
                blk.Explodable = True
            End If
            Exit For
        End If
    Next

 

Then, only the newly created block references to this dynamic block definition is explodable, but older block references are still remain unexplodable, that is because the real block definition for these block references are anonymous block definitions created by AutoCAD based on the original dynamic block definition when it was unexplodable. 

 

So, if your real intention is to not only make the dynamic block definition to allow exploding, but also make existing references to the dynamic block definition explodable, you need go after each block reference to reach its anonymous block definition and set it explodable. The whole code would be something like:

 

Public Sub Test()
    '' First change the dynamic block definition
    Dim blk As AcadBlock
    For Each blk In ThisDrawing.Blocks
        If UCase(blk.Name) = "TESTBLOCK" Then
            If Not blk.Explodable Then
                blk.Explodable = True
            End If
            Exit For
        End If
    Next
    
    '' then go after the block reference's anonymous block definition
    Dim ent  As AcadEntity
    Dim bref As AcadBlockReference
    For Each ent In ThisDrawing.ModelSpace
        If TypeOf ent Is AcadBlockReference Then
            Set bref = ent
            If UCase(bref.EffectiveName) = "TESTBLOCK" Then
                Set blk = ThisDrawing.Blocks(bref.Name)
                blk.Explodable = True
            End If
        End If
    Next
End Sub

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

Mike_Y2
Advocate
Advocate

@norman.yuan  Thankyou for your detailed reply and for explaining the issues relating to this task. It has helped my understanding. I wasn't sure which was the best options... to use VBA or Lisp so I asked the question in both forums. I have used VBA in Excel a lot (but never used it in AutoCAD) so my preference was to use VBA so I can understand the code. However I didn't realise that VBA isn't installed in AutoCAD by default (therefore I haven't tested it). I can however run .Lsp without installing anything on the end users PC's. So I am going to opt for lisp this time. Thanks again for your help. If I have issues with Lisp I will revert to installing VBA and testing/using this code

0 Likes