Change height attribute definition

jplujan
Advocate

Change height attribute definition

jplujan
Advocate
Advocate

Hi,

I have a drawing with hundreds of blocks with the same attribute names and I would like to change the height of these attributes in all blocks globally.

The code attached on the screen changes the height, but when I edit a block it returns to the previous height.

 

 

 Sub ChangeHeightAttr()
    Dim nombreSeleccion As String
    Dim Seleccion As AcadSelectionSet
    Dim ListaAtr As Variant
    Dim item As Variant
    Dim i As Variant
  
    Set Seleccion = ThisDrawing.SelectionSets.Add("Sleccion1")
    Seleccion.SelectOnScreen
    For Each item In Seleccion
             If item.HasAttributes Then
                 ListaAtr = item.GetAttributes()
                 For i = LBound(ListaAtr) To UBound(ListaAtr)
                     ListaAtr(i).Height = 5
                     ListaAtr(i).Update
                 Next
         End If
     Next
         
End sub

 

 

 

 

Thank you very much in advance
Best regards

0 Likes
Reply
Accepted solutions (2)
302 Views
10 Replies
Replies (10)

norman.yuan
Mentor
Mentor

It seems that you may need to fully understand the relationships between block definition/block reference and attribute definition/attribute reference, especially the latter.

 

Relationship between Attribute definition (AcadAttribute) and attribute reference (AcadAttributeReference) is different from that of between AcadBlock and AcadBlockReference. AcadAttribute is only used as template when a block reference's AcadAttributeReference is created. So, changes to AcadAttribute would not affact any existing AcadAttributeReferences and vice versa.

 Your code changes AcadAttributeReference's height, not AcadAttribute inside the block definition, which is what you see in the Block Editor that the attribute high is not changed. If you change attribute's height in the block editor, it is the AcadAttribute in the block definition being changed, which would not change any AcadAttributeReference in all the existing block references.

 

In your case, what you need to do is to use the block editor to change the AcadAttribute's height as needed and then run command from Express Tool "AttSync", which would reset the existing AcadAttributeReferences with updated AcadAttribute as template (thus, all the AcadAttributeReferences you see with the existing block reference get changes). Of course, if you wish, you can code your own "AttSync" command to achieve the same goal.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

tim11_manhhieu
Participant
Participant

try this

 

 

Sub Main()

Dim blockRef As AcadBlockReference
Dim attrib As Variant
Dim entity As acadEntity

For Each entity In ThisDrawing.ModelSpace

    If TypeOf entity Is AcadBlockReference Then

        Set blockRef = entity

        For Each attrib In blockRef.GetAttributes
    
            If attrib.TagString = "YOUR ATTRIBUTE NAME" Then
                attrib.TextString = "sfsdf"
                attrib.ScaleFactor = 0.8
                attrib.Height = 5
            End If

        Next attrib

    End If

Next entity

ThisDrawing.Regen (acActiveViewport)

End Sub

 

0 Likes

jplujan
Advocate
Advocate

Hello,
Thank you both for your time.
I have tried tim11_manhhieu code and the same thing happens to me, I can't get it to change in the block definition. According to what Normando Yuan explained, I'm afraid the solution is to create my own "AttSync", which I really don't know how to access the entity's Acadatribute.

Thanks again

0 Likes

tim11_manhhieu
Participant
Participant

can you share the DWG file?

0 Likes

jplujan
Advocate
Advocate

Here is the file, I want to change all the attributes to a different height

 

Thanks

0 Likes

jplujan
Advocate
Advocate
Accepted solution

 

I just generated this code and I think it works.

 

Sub Main()
   Dim elem As Object
    Dim block As AcadBlock
    Dim item As Object
    Dim Array1 As Variant
    Dim count As Integer
    Dim MBtest1 As String
    Dim str As String
    
    For Each elem In ThisDrawing.ModelSpace
        If elem.EntityName = "AcDbBlockReference" Then
            If elem.HasAttributes Then
                Array1 = elem.GetAttributes
                    str = elem.Name
                    Set block = ThisDrawing.Blocks.item(str)
                    For Each item In block
                        str = item.EntityName
                       If item.EntityName = "AcDbAttributeDefinition" Then
                          item.Height = 1.5
                                
                         
                        End If
                    Next item
            End If
        End If
    Next

End Sub

 

Thanks

 

 

 

 

 

0 Likes

tim11_manhhieu
Participant
Participant

The request at the beginning of the post, as I understand it, is to change the height of attribute A in block A, attribute A in block B, attribute A in block C...

If you want change height all attribute in all block, the code more easier.

tim11_manhhieu_0-1737072920588.png

 

Sub Main()

Dim blockRef As AcadBlockReference
Dim attrib As Variant
Dim entity As acadEntity

For Each entity In ThisDrawing.ModelSpace

    If TypeOf entity Is AcadBlockReference Then

        Set blockRef = entity

        For Each attrib In blockRef.GetAttributes   
                attrib.Height = 1.5
        Next attrib

    End If

Next entity

ThisDrawing.Regen (acActiveViewport)

End Sub

 

 

0 Likes

jplujan
Advocate
Advocate

Hello

The idea is to change the height of the attributes in the block definition.

Thank you very much for your code.

Thanks

0 Likes

norman.yuan
Mentor
Mentor
Accepted solution

If the goal is only change the attribute's Height in the block definition, then you do not need to loop through the ModelSpace/PaperSpace for each AcadBlockReference, especially, if there are many block references that are from the same block definition: your code modify the same block definition multiple times again and again with the same change.

 

You can simply loop through AcadBlocks collection:

 

Dim blk as AcadBlock

Dim ent As AcadEntity

Dim att As AcadAttribute

For Each blk in ThisDrawingBlock

   '' You may also test block name to narrow the loop here

   For Each ent in blk

     If TypeOf ent Is AcadAttribute Then

        Set att = Ent

       att.Height=10.0

     End If

   Next

Next

 

This way, each block definition is only modified once.

 

Of course, you do realize that all the attributes (AcadAttributeReferences) of existing block references (you said there could hundreds of then) remain unchanged with their Height, unless you run "ATTSYNC" command (or you have other code to also change AcadReferences' Height).

 

Norman Yuan

Drive CAD With Code

EESignature

jplujan
Advocate
Advocate

Hello,
Thank you for your dedication

Best regards

0 Likes