Author Topic: Block Count based on block name, attribute value  (Read 1886 times)

0 Members and 1 Guest are viewing this topic.

Guest

  • Guest
Block Count based on block name, attribute value
« on: September 07, 2007, 09:46:09 AM »
I've got a program that counts various blocks using different selection types.  Right now, I've got it so that it will count the blocks based on the value of an attribute tag.  And now I want to add the block name to the mix because some blocks may have the same attribute value as others.

So right now the code spits out something like this...

Quote
A  3
B  6
C  12
I want to add the block name so that the count looks more like this to show which block is associated with the attribute value.

Quote
Block 1
 A  3

Block 2
 B  3

Block C
 B  3

Block D
 C  9

Block E
 C  3


Here's my code thus far.  I've got a reference to the MS Scripting Runtime for creating the dictionaries.
Code: [Select]
Private Sub GetAttributeCount()
    Dim objAttKeys As Variant
    Dim objAttItems As Variant
    Dim objAttDict As Dictionary
    Dim varAtts() As AcadAttributeReference
    Dim objBlock As AcadBlockReference
    Dim obj As AcadEntity
    Dim i, x As Integer
   
    Set objAttDict = New Dictionary
   
    x = 1
    For Each obj In SSet
        Set objBlock = obj
        If obj.HasAttributes Then
            varAtts = obj.GetAttributes
            For i = LBound(varAtts) To UBound(varAtts)
                If UCase$(varAtts(i).TagString) = "DATATYPE" Then
                    If objAttDict.Exists(varAtts(i).TextString) = False Then
                        objAttDict.Add varAtts(i).TextString, 1
                    Else
                        objAttDict.item(varAtts(i).TextString) = objAttDict.item(varAtts(i).TextString) + 1
                    End If
                End If
                On Error GoTo 0
            Next i
        End If
    Next obj
   
    i = 0
   
    objAttKeys = objAttDict.Keys
    objAttItems = objAttDict.Items
    For x = 0 To UBound(objAttKeys)
        Debug.Print objAttKeys(x) & vbTab & objAttItems(x)
        i = i + objAttItems(x)
    Next
End Sub

Fatty

  • Guest
Re: Block Count based on block name, attribute value
« Reply #1 on: September 09, 2007, 09:43:14 AM »
Hi Matt
Look at this thread
http://discussion.autodesk.com/thread.jspa?threadID=606029
Hope it may helps

~'J'~


Bryco

  • Water Moccasin
  • Posts: 1882
Re: Block Count based on block name, attribute value
« Reply #2 on: September 09, 2007, 12:00:45 PM »
The dictionary may not be the best container for this.
It seems to me that you can have
Block1 A 3 and Block1 B 2.
By parsing the comma you could achieve this with
"Block1,A" 3  and "Block1,B" 2
but it's usually easier to use a variant array in a collection.
Dim Bc(2)
BC(0)=block.name:Bc(1)=Att.textstring:Bc(2)=count
Now you make a selset for each blockref by name,
Sort the Bcs for that block
 add to the collection

Guest

  • Guest
Re: Block Count based on block name, attribute value
« Reply #3 on: September 10, 2007, 08:33:23 AM »
Thanks guys!

This will give me something to think about.