loop through block attributes the proper method using VBA?

Discussion in 'AutoCAD' started by stephenlecompte, Feb 7, 2005.

  1. I've got the following looping through all the blocks on my drawings?
    I also want it to loop through each attribute to every block - I know that GetAttribute should get me what I want but not sure on the correct format for it?

    Also -
    I also have a second question but its the most important.
    I am really just interested in one particular block name called door_TAG. My drawing has a lot of blocks with the similiar name door_TAG. What makes one door_TAG different from all the other door_TAG blocks is that its attribute ROOM_NUMBER is different for all! I am going to eventually try to extract all of those ROOM_NUMBERs from each door_TAG. I am worried that if I use the below code it won't help me at all because it seems to loop through only one instance of door_TAG or am I wrong? Thanks for all your past help!


    Private Sub cmdBlock_Click()
    On Error GoTo Err_cmdBlock_Click

    Dim acadApp As AutoCAD.AcadApplication
    Dim acadDoc As AutoCAD.AcadDocument
    Dim acadBlk As AutoCAD.AcadBlock
    Dim acadAtt As AutoCAD.AcadAttribute

    Dim strPath As String

    strPath = "C:\ZZZ EC WORK\DRTAG-TEST.dwg"
    Set acadDoc = GetObject(strPath)
    Set acadApp = acadDoc.Application

    For Each acadBlk In acadDoc.Blocks
    MsgBox "Block: " & acadBlk.Name
    Next
    Set acadApp = Nothing
    Set acadDoc = Nothing

    MsgBox "Finished finding all blocks..."

    Err_cmdBlock_Click:
    End Sub
     
    stephenlecompte, Feb 7, 2005
    #1
  2. stephenlecompte

    MP Guest

    Sounds like you want to get the attribute references in the blockreferences,
    not the attribute definition in the block definition.
    Your code is just looking at the blocks table (definitions) not model or
    paperspace insertions of the blocks (the things you see on the screen)
    get a selection set of all "door_Tag" insertions(blockreferences) (search
    for filtered selection sets if you don't know how)
    get their attribute references via GetAttributes
    step through those to find the tag you want
    hth
    Mark
    do a google search on GetAttributes and you're sure to get tons of samples

    GetAttribute should get me what I want but not sure on the correct format
    for it?
    'once you have the block refs - either iterating model or paper spaces or by
    a filtered selection set
    Dim oBlkRef As AutoCAD.AcadBlockReference
    Dim vAtts As Variant
    Dim I As Long
    If oBlkRef.HasAttributes Then
    vAtts = oBlkRef.GetAttributes
    For I = LBound(vAtts) To UBound(vAtts)
    If vAtts(I).TagString = "RoomNumber" Then
    vAtts(I).TextString = <---------------room number here
    Exit For'assuming there's only one roomnumber tag per block
    reference
    End If
    Next
    End if
     
    MP, Feb 8, 2005
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.