Block Visibility Issues

Block Visibility Issues

Anonymous
Not applicable
1,541 Views
2 Replies
Message 1 of 3

Block Visibility Issues

Anonymous
Not applicable

Hi, I've been using VBA to automate AutoCAD for a little while now, but this issue is baffling me. I have a function that looks through the blocks in paperspace to find a specific block containing one attribute, and then makes that block visible/invisible based on a value from the Custom Drawing Properties.

 

Sub TexasFirm()
'This is a function that checks the state of the project and, if not in Texas, removes the Texas Firm block
Dim entity As AcadEntity
Dim bref As AcadBlockReference

Dim objLayout As AcadLayout
Set objLayout = ThisDrawing.ActiveLayout
Dim Value As String ThisDrawing.SummaryInfo.GetCustomByKey "Project City State Zip Code", Value
If objLayout.name <> "Model" Then For j = 0 To objLayout.block.Count - 1 Set entity = objLayout.block.Item(j) If TypeOf entity Is AcadBlockReference Then Set bref = entity If bref.EffectiveName = "TEXAS FIRM BLOCK" Then If UCase(Value) Like "*TEXAS*" Or Value Like "*TX*" Or Value = "" Then bref.Visible = True
bref.Update Exit For Else bref.Visible = False bref.Update Exit For End If End If End If Next End If End Sub

This function worked perfectly until I updated to AutoCAD 2018. Now, it still finds the block and checks through my conditions as expected, but the "bref.visible = false" line executes and makes no change. The block is a simple block with a single attribute that contains a number that doesn't need to be shown if the job is not in Texas. I also tried changing the visibility from the attribute level (as opposed to the block reference level), but the HasAttributes() method is not returning the one attribute that comprises the block.

 

For now, my solution has just been to delete the block, but I don't really like doing that, and everything worked before updating AutoCAD... Any ideas?

 

0 Likes
Accepted solutions (1)
1,542 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

Well, if all your other code than "bref.Visible = True/False" is correct as intended, then you are effectively saying that 

AcadBlockreference.Visible = True/False does not work in AutoCAD 2018. It is hard to believe Autodesk introduced a bug at such a basic level after all these years. I can certainly not reproduce this by simplifying the code as this:

Public Sub ToggleBlockVisibility()

    Dim hide As Boolean
    Dim ent As AcadEntity
    Dim an As String
    
    an = ThisDrawing.Utility.GetString(0, vbCr & "Enter H for Hide, S for Show:")
    If UCase(an) = "H" Then
        hide = True
    Else
        hide = False
    End If
    
    For Each ent In ThisDrawing.PaperSpace ''Or ModelSoace
        If TypeOf ent Is AcadBlockReference Then
            ent.Visible = Not hide
            ent.Update
        End If
    Next
    
End Sub

I tried a few different blocks in modelspace or paperspace. They are hidden/shown correctly, with my AutoCAD 2018.

 

So, can you try the simplest code similarly to what I showed to verify AcadBlockReference.Visible somehow still does not work with your AutoCAD 2018?

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

Anonymous
Not applicable

I used this code and, sure enough, it turned all of the blocks (including my problematic block) visible/invisible. I'm still not sure why my previous code did not work anymore, but I tailored this code for the specifics of my case and it works just like it should!

Sub TexasFirm()
'This is a function that checks the state of the project and, if not in Texas, removes the Texas Firm block

Dim Value As String
ThisDrawing.SummaryInfo.GetCustomByKey "Project City State Zip Code", Value

For Each entity In ThisDrawing.PaperSpace
    If TypeOf entity Is AcadBlockReference Then
        Set bref = entity
        If bref.EffectiveName = "TEXAS FIRM BLOCK" Then
            If UCase(Value) Like "*TEXAS*" Or Value Like "*TX*" Or Value = "" Then
                entity.Visible = True
            Else
                entity.Visible = False
            End If
            entity.Update
        End If
    End If
Next

End Sub

 

Thank you so much for the help!

0 Likes