Block count based on attribute text

Discussion in 'AutoCAD' started by Matt W, Sep 9, 2004.

  1. Matt W

    Matt W Guest

    Maybe I'm just having a mental block, but I can't for the life of me figure
    this one out...

    I've got a block called "INFO". Within that block are a half-dozen
    attributes, one of which is called "ID".
    What I would like to do is query the drawing for all blocks called INFO and
    count them based on the ID.
    In other words, if a drawing has 20 (there could be more/less) INFO blocks
    and 5 of them have an ID of "A-A" and 7 have an ID of "B-B" and the
    remaining 8 have an ID of "C-C", I would like to be able to spit out a
    MSGBOX that says something like this...

    20 instances of INFO were found.
    5: A-A
    7: B-B
    8: C-C

    I can get the block count and the attributes, but I can't seem to put 2 and
    2 together to end up with 4. I keep ending up with the square root of pi.

    Thanks in advance!
     
    Matt W, Sep 9, 2004
    #1
  2. Use a collection. Make the key = to the att value [A-A] and the value your
    count. Use an error trap to catch the existance of the key and increment
    your value:

    Sub Test()
    Dim v(3) As String
    v(0) = "aplha"
    v(1) = "beta"
    v(2) = "omega"
    v(3) = "beta"
    Dim iCntr As Integer
    Dim iTmp As Integer
    Dim cAtts As Collection
    Set cAtts = New Collection
    On Error Resume Next
    For iCntr = 0 To 3
    'add to collection
    cAtts.Add iCntr, v(iCntr)
    'test for error
    If Err Then
    'yep so clear the error
    Err.Clear
    'grab collection item's value
    iTmp = cAtts(v(iCntr))
    'delete the item
    cAtts.Remove v(iCntr)
    're-add it with the adjusted value
    cAtts.Add iTmp + iCntr, v(iCntr)
    End If
    Next
    End Sub


    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 9, 2004
    #2
  3. Matt W

    Matt W Guest

    Thanks Mike.
    I've been doing a little research on the MS Dictionary. I'm assuming this
    could also be done with that, right??
    Is an MS Dictionary faster than a collection and how would it be done using
    the MS Dictionary?

    Thanks again!

    --
    Matt W

    I love deadlines
    I like the whooshing sound they make as they fly by.

    | On Thu, 9 Sep 2004 11:11:23 -0400, Matt W wrote:
    |
    | > Maybe I'm just having a mental block, but I can't for the life of me
    figure
    | > this one out...
    | >
    | > I've got a block called "INFO". Within that block are a half-dozen
    | > attributes, one of which is called "ID".
    | > What I would like to do is query the drawing for all blocks called INFO
    and
    | > count them based on the ID.
    | > In other words, if a drawing has 20 (there could be more/less) INFO
    blocks
    | > and 5 of them have an ID of "A-A" and 7 have an ID of "B-B" and the
    | > remaining 8 have an ID of "C-C", I would like to be able to spit out a
    | > MSGBOX that says something like this...
    | >
    | > 20 instances of INFO were found.
    | > 5: A-A
    | > 7: B-B
    | > 8: C-C
    | Use a collection. Make the key = to the att value [A-A] and the value your
    | count. Use an error trap to catch the existance of the key and increment
    | your value:
    |
    | Sub Test()
    | Dim v(3) As String
    | v(0) = "aplha"
    | v(1) = "beta"
    | v(2) = "omega"
    | v(3) = "beta"
    | Dim iCntr As Integer
    | Dim iTmp As Integer
    | Dim cAtts As Collection
    | Set cAtts = New Collection
    | On Error Resume Next
    | For iCntr = 0 To 3
    | 'add to collection
    | cAtts.Add iCntr, v(iCntr)
    | 'test for error
    | If Err Then
    | 'yep so clear the error
    | Err.Clear
    | 'grab collection item's value
    | iTmp = cAtts(v(iCntr))
    | 'delete the item
    | cAtts.Remove v(iCntr)
    | 're-add it with the adjusted value
    | cAtts.Add iTmp + iCntr, v(iCntr)
    | End If
    | Next
    | End Sub
    |
    |
    | -- Mike
    | ___________________________
    | Mike Tuersley
    | CADalyst's CAD Clinic
    | Rand IMAGINiT Technologies
    | ___________________________
    | the trick is to realize that there is no spoon...
     
    Matt W, Sep 9, 2004
    #3
  4. Matt W

    MP Guest

    Regarding the use of On Error Resume Next in that kind of context,
    would you put an
    On Error GOTO 0
    before the end Sub?
    In order to reset error control for subsequent subs or enclosing subs that
    may be calling this one?

    do you need to reset Error control here?
    On Error GoTo 0 ?
    Or does the on error resume next "Go out of scope"(so to speak) when the sub
    ends.????
    if the sub were longer and other actions were taken after the fornext loop,
    would the goto 0 follow the fornext loop so as to reset within the current
    sub???

    I've been following many posts on error control over time and am trying to
    absorb the correct methods.
    I've seen the problems of masking errors that on error resume next can
    create and am trying to avoid them
    while taking advantage of the simplicity of using that construct to do
    things like you show in this example

    Tia
    Mark Propst
     
    MP, Sep 9, 2004
    #4
  5. Yes, Mark, I would issue the Goto 0 after the Next IF there was more to do
    AND nothing else was performed within the original For loop. If there were
    calls to other Subs/Functions, I'd move the Resume inside the For loop and
    Goto 0 right after the check. I don't have a spiffy answer ready for the
    'out of scope' question other than to say that I have had issues where the
    Resume overrides the subsequent call. I typically only have error control
    at the lowest levels - never the highest. Someone else may have a better
    more definite answer - I've kinda moved on to .NET's Try...Catch looping
    and don't want to look back =)

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 9, 2004
    #5
  6. Yes, generally the dictionary would be better IMHO. With it, you could use
    the Exists method to see if the item already exists and bypass the whole
    Error manipulation. I didn't bother suggesting it because it requires an
    additional reference and kinda seemed overkill just to handle this
    question.

    -- Mike
    ___________________________
    Mike Tuersley
    CADalyst's CAD Clinic
    Rand IMAGINiT Technologies
    ___________________________
    the trick is to realize that there is no spoon...
     
    Mike Tuersley, Sep 9, 2004
    #6
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.