Get the constant attributes of a blockref through VBA

<?xml encoding=”UTF-8″>By Augusto Goncalves

How can we get the block name, the attribute values, and find how many times the block is inserted and put this information into a spreadsheet using VBA? Getting the block information from the block definition in the block table.

The following sample demonstrates this by getting all the attributes of all the block references found in model space. You need to create a simple form which contains a listbox named ListBox1 and a button that starts the following procedure:

<font color="#0000a0">Private</font> <font color="#0000a0">Sub</font> CommandButton1_Click()
<font color="#0000a0">Dim</font> elem <font color="#0000a0">As</font> <font color="#0000a0">Object</font>
<font color="#0000a0">Dim</font> block <font color="#0000a0">As</font> AcadBlock
<font color="#0000a0">Dim</font> item <font color="#0000a0">As</font> <font color="#0000a0">Object</font>
<font color="#0000a0">Dim</font> Array1 <font color="#0000a0">As</font> <font color="#0000a0">Variant</font>
<font color="#0000a0">Dim</font> count <font color="#0000a0">As</font> <font color="#0000a0">Integer</font>
<font color="#0000a0">Dim</font> MBtest1 <font color="#0000a0">As</font> <font color="#0000a0">String</font>
<font color="#0000a0">Dim</font> str <font color="#0000a0">As</font> <font color="#0000a0">String</font>
<font color="#0000a0">For</font> <font color="#0000a0">Each</font> elem <font color="#0000a0">In</font> ThisDrawing.ModelSpace
<font color="#0000a0">If</font> elem.EntityName = "AcDbBlockReference" <font color="#0000a0">Then</font>
<font color="#0000a0">If</font> elem.HasAttributes <font color="#0000a0">Then</font>
Array1 = elem.GetAttributes
<font color="#0000a0">For</font> count = <font color="#0000a0">LBound</font>(Array1) <font color="#0000a0">To</font> <font color="#0000a0">UBound</font>(Array1)
<font color="#0000a0">If</font> (Array1(count).EntityName) = "AcDbAttribute" <font color="#0000a0">Then</font>
MBtest1 = Array1(count).TagString & _
" - " & Array1(count).TextString
ListBox1.AddItem MBtest1
<font color="#0000a0">End</font> <font color="#0000a0">If</font>
<font color="#0000a0">Next</font> count
<font color="#008000">      'Get the block definition from the block table</font>
str = elem.Name
<font color="#0000a0">Set</font> block = ThisDrawing.Blocks.item(str)
<font color="#0000a0">For</font> <font color="#0000a0">Each</font> item <font color="#0000a0">In</font> block
str = item.EntityName
<font color="#008000">        'Get the Constant attributes</font>
<font color="#0000a0">If</font> item.EntityName = "AcDbAttributeDefinition" <font color="#0000a0">Then</font>
<font color="#0000a0">If</font> item.Mode = acAttributeModeConstant <font color="#0000a0">Then</font>
ListBox1.AddItem item.TagString & " - " _
& item.TextString
<font color="#0000a0">End</font> <font color="#0000a0">If</font>
<font color="#0000a0">End</font> <font color="#0000a0">If</font>
<font color="#0000a0">Next</font> item
<font color="#0000a0">End</font> <font color="#0000a0">If</font>
<font color="#0000a0">End</font> <font color="#0000a0">If</font>
<font color="#0000a0">Next</font> elem
<font color="#0000a0">End</font> <font color="#0000a0">Sub</font>


Comments

7 responses to “Get the constant attributes of a blockref through VBA”

  1. Zakaria Avatar
    Zakaria

    I need to know how many is repeated a block in the model space In AutoCad through a VBA Code
    Please HELP

  2. Hi Zakaria,
    Not sure if I’m understood your question…can you clarify?
    Thanks,
    Augusto Goncalves

  3. Zakaria Avatar
    Zakaria

    Hi Augusto,
    I have an autocad drawing contains many blocks and blocks under the same name,
    My question is how can I count the blocks that have a certain name? For example I need to know how many blocks in my drawing have the name: “Block1” through a VBA Code.
    Thank you a lot for your help
    Zakaria

  4. Zakaria Avatar
    Zakaria

    Or how many times the “Block1” is used in my drawing?
    Thanks a lot & Best Regards
    Zakaria

  5. Zakaria,
    You may need to open the model space, then run through the collection of entities, check if is a AcadBlockReference, if so, check the Name property, which points to the block name defined for it.
    Hope this help.
    Regards,
    Augusto Goncalves

  6. keunsung Avatar
    keunsung

    Hi Augusto.
    How to get just one text of tag(specified) in 1 block(specified) what i want?
    I need to put one value of a tag into textbox1
    ex)
    I have “GAD” block which has “OH” tag
    and I want to put value of tag”OH” in textbox1
    Can you help me?

  7. Hi,
    Please take a look at this help topic: http://docs.autodesk.com/ACD/2014/PLK/files/GUID-BA69D85A-2AED-43C2-B5B7-73022B5F28F8.htm (at the bottom you’ll find a VBA sample code).
    Regards
    Augusto Goncalves

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading