Inserted dynamic block from VBA loses visibility state but block still has visibility parameter

Inserted dynamic block from VBA loses visibility state but block still has visibility parameter

SWS77
Contributor Contributor
1,557 Views
9 Replies
Message 1 of 10

Inserted dynamic block from VBA loses visibility state but block still has visibility parameter

SWS77
Contributor
Contributor

I have VBA code that inserts a drawing as a block, consisting of (3) unique blocks, into a drawing.  One of the blocks contains a visibility state.  If I insert the block from AutoCAD using insert command it works fine - insert, explode into (3) separate blocks, and then I can select the visibility state of the block with the visibility parameter.  If I insert the block from my VBA code, the drawing block does get insert into the current drawing, and exploded so the (3) separate blocks are separated - but still each is its own block - however the selector for the visibility states does not show up.  Once inserted from VBA, where I can not see the visibility selector, I can go to the block editor and the action parameter is shown and I can change from one state to the other just fine using the selector drop down in the ribbon bar.  The visibility parameter properties do show YES for show properties and number of grips is 1.

 

Another quirk, in my VBA code I select which scale to use when inserting the drawing block.  If the scale is 1 then the block comes in fine and I can select the visibility state.  Any other scale causes the visibility grip to not show up.

 

Also, the properties of the block with visibility issues, after inserting from VBA, do not show the "custom" options in the properties where you can select the visibility state.

 

If I insert the block from VBA, and then insert it from CAD (with or without scaling the CAD inserted block) the one inserted from CAD works.  If I edit the block inserted from CAD insert, say draw a line, and save it, the block inserted from VBA, that does not show the visibility options, also updates to show the line I added.  But the visibility still does not show up on the VBA inserted block - but the CAD inserted block visibility does work as expected.

0 Likes
Accepted solutions (1)
1,558 Views
9 Replies
Replies (9)
Message 2 of 10

Ed__Jobe
Mentor
Mentor

We can't help you much if you don't share your code and sample dwg, blocks. When you post your code, use the </> button to post it to a code window.

 

Also, why are you exploding your blocks??? If you don't want a dynamic block, then create separate blocks for each state.

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

0 Likes
Message 3 of 10

SWS77
Contributor
Contributor

I've attached the block drawing which has 3 separate blocks where one is a dynamic block with two visibility states.  The VB code is shown below.  When I change the scale from this (48x scale)

Set insertedblock = ThisDrawing.ModelSpace.InsertBlock(ip, path, 48#, 48#, 1#, 0)

To this (1x scale)

Set insertedblock = ThisDrawing.ModelSpace.InsertBlock(ip, path, 1#, 1#, 1#, 0)

Then it will work as expected:

  • Insert drawing as block
  • Explode into 3 separate blocks
  • dynamic block is able to change visibility state

When I have a scale set (other than 1, as shown below) I can comment out the explode and delete line.  The code will still insert the block, but then I am not able to manually explode it in the drawing after it's inserted.

 

Option Explicit

Sub insertMyBlock()
    'Block Path
        Dim path As String
        path = "YOUR FILE PATH\block drawing.dwg"
    
    'Insertion point
        Dim ip(0 To 2) As Double
        ip(0) = 0
        ip(1) = 0
        ip(2) = 0
    
    Dim insertedblock As AcadBlockReference
    
    Set insertedblock = ThisDrawing.ModelSpace.InsertBlock(ip, path, 48#, 48#, 1#, 0)
    insertedblock.Update
    
    'The below explode errors when x and y scale set to 48, but it works when set to 1.0
        'insertedblock.Explode
        'insertedblock.Delete
End Sub

 

0 Likes
Message 4 of 10

SWS77
Contributor
Contributor

I explode the block after insertion since I have (3) unique blocks in my block drawing.  One which is a dynamic block.  When you insert a drawing as a block the entire drawing is inserted as a single block.  I then explode after insertion, so it separates it into 3 separate blocks.

0 Likes
Message 5 of 10

SWS77
Contributor
Contributor

If I set all three scale factors to the same value, then it seems to work as expected.  My blocks are 2D only so I originally just set the Z scale to 1.0. Seems weird that setting the Z scale the same as the X and Y would solve this issue, but for now that seems to work a couple times in a row for me.

 

So here is my working version:

 

Option Explicit

Sub insertMyBlock()
    'Block Path
        Dim path As String
        path = "YOUR FILE PATH HERE\block drawing.dwg"
    
    'Inserted scale
        Dim myScale As Double
        myScale = 48
        
    'Insertion point
        Dim ip(0 To 2) As Double
        ip(0) = 0
        ip(1) = 0
        ip(2) = 0
    
    Dim insertedblock As AcadBlockReference
    
    Set insertedblock = ThisDrawing.ModelSpace.InsertBlock(ip, path, myScale, myScale, myScale, 0)
    insertedblock.Update
    
    insertedblock.Explode
    insertedblock.Delete

End Sub

 

 

0 Likes
Message 6 of 10

Ed__Jobe
Mentor
Mentor
Accepted solution

AutoCAD calls that a uniformly scaled block. It used to be that non-uniformly scaled blocks could not be exploded.

 

It looks like this is either a test sub or part of something larger. I would suggest making a sub that inserts one dwg and use a For loop to insert each block dwg, rather than inserting a container dwg and exploding it. If you stick to the latter method, you should purge it as well.

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

0 Likes
Message 7 of 10

SWS77
Contributor
Contributor

Thanks Ed.  I didn't know about uniform vs. non-uniform blocks.  But that sounds like my issue.  Yes, it's a small excerpt from a larger VBA program I'm working on.

 

How do you insert a drawing to use the method you describe, where I then loop to insert the blocks?  Or do you mean to separate my single .dwg file with 3 blocks into 3 different .dwg files and use the insert block code that I have in my example? 

0 Likes
Message 8 of 10

Ed__Jobe
Mentor
Mentor

You either should insert 3 separate blocks so you don't have to explode them or purge the parent block after exploding it. This keeps your drawing clean of unused blocks. For the first scenario, process an array of paths. This can be a function that accepts a string array, such as if you prompt a user to select a folder and you return all the files in the folder.

Dim paths(2) As String
paths(0) = "path1\block1.dwg"
paths(1) = "path2\block2.dwg"
paths(2) = "path3\block3.dwg"
Dim i As Integer

For i = 0 to UBound(paths)
   'insert paths(i)
Next i

 

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

0 Likes
Message 9 of 10

SWS77
Contributor
Contributor
Got it. Thanks for the help. I've used a single file with multiple blocks, but looks like it may be better to use a single dwg for a single block. That should work for me. I was thinking you would still have all the blocks in one dwg, and then insert them some different way. But this solution will do just fine.
0 Likes
Message 10 of 10

Ed__Jobe
Mentor
Mentor

With that method, you would also need to supply info unique to each insert, e.g. InsertionPoint.For that, I would create a Struct for storing all the data for a block insert. Then pass a collection of them to the insert routine.

 

Public Type blkRef
   path As String
   insPoint As Variant
   sc As Double
   rot As Double
End Type

 

 

Your first method works too, you just need to follow with deleting the block def once all references are deleted. See my additions below.

 

 

Option Explicit

Sub insertMyBlock()
    'Block Path
        Dim path As String
        path = "YOUR FILE PATH HERE\block drawing.dwg"
    
    'Inserted scale
        Dim myScale As Double
        myScale = 48
        
    'Insertion point
        Dim ip(0 To 2) As Double
        ip(0) = 0
        ip(1) = 0
        ip(2) = 0
    
    Dim insertedblock As AcadBlockReference
    Dim blk As AcadBlock
    Dim blkName As String
    
    Set insertedblock = ThisDrawing.ModelSpace.InsertBlock(ip, path, myScale, myScale, myScale, 0)
    insertedblock.Update
    
    blkName = insertedblock.Name
    set blk = ThisDrawing.Blocks(blkName)
    insertedblock.Explode
    insertedblock.Delete
    blk.Delete

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