Edit Nested Block Attributes in VBA

Edit Nested Block Attributes in VBA

Anonymous
Not applicable
1,385 Views
1 Reply
Message 1 of 2

Edit Nested Block Attributes in VBA

Anonymous
Not applicable

Good Day to everyone,

 

I'm running into a bit of an issue here.

 

My purpose is to write a VBA code that will edit a particular attribute of all the nested blocks within the selected parent block. I have so far achieved a code that can change the particular attribute of a non nested block my desired value.

 

I figure with this code, all I would need to do is add "For each Entity (child blocks) within the Entity ( parent that i have chosen)". But this is seemingly wrong, maybe because i need to look for entities within the Block Reference and not the block itself,I'm really not too sure.

 

I've attached below the code that works for a single non nested block, would anyone point me in the right direction to make this work by selecting a parent block, and having all ASSYNO attributes changed to the parent blocks name?

 

Public Sub TestGetAttributes()

Dim varPick As Variant

Dim objEnt As AcadEntity

Dim objBRef As AcadBlockReference

Dim varAttribs As Variant
Dim strAttribs As String
Dim intI As Integer
On Error Resume Next

With ThisDrawing.Utility

 

'' get an entity from user
GetEntity objEnt, varPick, vbCr & "Pick a block with attributes: If Err Then Exit Sub"

'Set Ref
Set objBRef = objEnt


'' exit if not a block
If objBRef Is Nothing Then
MsgBox ("That wasn't a block.")
End If

'' exit if it has no attributes
If Not objBRef.HasAttributes Then
MsgBox ("That block doesn't have attributes.")
End If

'' get the attributerefs
varAttribs = objBRef.GetAttributes

'' Change ASSYNO to the blocks name
strAttribs = objBRef.Name
For intI = LBound(varAttribs) To UBound(varAttribs)
If varAttribs(intI).TagString = "ASSYNO" Then
varAttribs(intI).TextString = strAttribs
End If

Next

MsgBox strAttribs

End With

 


End Sub

0 Likes
1,386 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor

Firstly, I'd say having a block reference with attributes added in a block definition (thus, the nested block) isn't a very good practice in most cases. But this is the fact you have not control and have to deal with it using your code...

 

So, let's assume you have a block definition named "MyNestBlock", which has a block reference with attributes (AcadAttributeReference) in it. Following code demonstrates how to edit the attributes of nested block (AcadBlockreference) in a block definition (of course you know all the block reference to this block definition would show the same changes of nested block's attributes):

 

Dim blkDef As AcadBlock

Set blkDef=ThisDrawing.Blocks("MyNestBlock") '' Assume the block definition does exist in ThisDrawing

Dim ent As AcadEntity

Dim blkRef As AcadBlockReference

Dim att As AcadAttributeReference

Dim atts As Variant

Dim i As Integer

For Each ent in blkDef

    If TypeOf ent Is AcadBlockReference Then

        Set blkRef = ent

        If blkRef.HasAttributes Then

            atts=blkRef.GetAttributes()

            For i=0 To UBound(atts)

                Set att = atts(i)

                Select UCase(att.TagString)

                    Case "AAAA"

                       att.TextString="xxxxxxxxxx"

                   Case "BBBBB"

                      att.TextString="yyyyyyyyyy"

                  ... ...

               End Select

            Next

        End If

        Exit For '' Assume you know there is only one nested block reference in the definition

    End If

Next

 

'' After updating the block definition, you may want to Regen the drawing

ThisDrawing.Regen acAllViewports

... ...

 

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes