Count blocks add to listbox

Discussion in 'AutoCAD' started by gizmowiebe, Dec 25, 2004.

  1. gizmowiebe

    gizmowiebe Guest

    Hi,

    What I would like to do is to count all block(with the same name) in the acad modelspace. and then put those values in a listbox with 2 columns. So It would look something like this.

    Rear 3
    Front 6
    Bold 9
    screw 10

    does anyone have any sugestions.

    Thx

    Wiebe
     
    gizmowiebe, Dec 25, 2004
    #1
  2. gizmowiebe

    Nava Ran Guest

    Nava Ran, Dec 26, 2004
    #2
  3. gizmowiebe

    gizmowiebe Guest

    Thx but I would like to create my own program....
     
    gizmowiebe, Dec 26, 2004
    #3
  4. gizmowiebe

    Wade Guest

    Here is something that will at least get you started. This assumes you have
    a Form with a Command Button (named cmdListBlocks) and a List Box (named
    ListBoxBlocks). To be more useful, you'll have to set up some filters.

    Private Sub cmdListBlocks_Click()
    Dim blk As AcadBlock
    Dim MyBlkArray() As Variant
    Dim i As Long
    i = 0
    For Each blk In ThisDrawing.Blocks
    i = i + 1
    ReDim Preserve MyBlkArray(2, i) '2 rows by i columns
    MyBlkArray(0, i) = blk.Name
    MyBlkArray(1, i) = blk.Count
    Next blk
    Me.ListBoxBlocks.ColumnCount = 2
    Me.ListBoxBlocks.ColumnWidths = "36;36" ' each 0.5" wide
    Me.ListBoxBlocks.Column() = MyBlkArray

    End Sub


    acad modelspace. and then put those values in a listbox with 2 columns. So
    It would look something like this.
     
    Wade, Dec 27, 2004
    #4
  5. gizmowiebe

    Wade Guest

    This one works better...

    Private Sub cmdListBlocks_Click()
    Dim MyBlkArray() As Variant
    Dim i As Long, j As Long
    Dim count As Integer
    Dim index As Integer
    Dim FoundBlock As Boolean

    count = ThisDrawing.ModelSpace.count
    i = 0
    For index = 0 To count - 1
    If ThisDrawing.ModelSpace.Item(index).ObjectName =
    "AcDbBlockReference" Then
    j = 1
    FoundBlock = False
    While j <= i
    If MyBlkArray(0, j) = ThisDrawing.ModelSpace.Item(index).Name
    Then
    MyBlkArray(1, j) = MyBlkArray(1, j) + 1
    j = i
    FoundBlock = True
    End If
    j = j + 1
    Wend
    If Not FoundBlock Then
    i = i + 1
    ReDim Preserve MyBlkArray(2, i)
    MyBlkArray(0, i) = ThisDrawing.ModelSpace.Item(index).Name
    MyBlkArray(1, i) = 1
    End If
    End If
    Next index
    Me.ListBoxBlocks.ColumnCount = 2
    Me.ListBoxBlocks.ColumnWidths = "72;36"
    Me.ListBoxBlocks.Column() = MyBlkArray

    End Sub


    acad modelspace. and then put those values in a listbox with 2 columns. So
    It would look something like this.
     
    Wade, Dec 27, 2004
    #5
  6. gizmowiebe

    gizmowiebe Guest

    Thx Wade,

    That codes works, but do you know how I can change it to add blocks nested in another block
     
    gizmowiebe, Dec 30, 2004
    #6
  7. gizmowiebe

    Wade Guest

    Unfortuately, no, at least not yet.

    Does anyone else know how?

    in another block
     
    Wade, Dec 30, 2004
    #7
  8. gizmowiebe

    Ed Jobe Guest

    Do you need to physically count nested blocks? Shouldn't it just be a
    multiple of how many times its inserted? e.g. if block A contains 2 of block
    B and block A is inserted 30 times, then there will be 60 of block B.
     
    Ed Jobe, Dec 30, 2004
    #8
  9. gizmowiebe

    Wade Guest

    Ed,

    Thanks for you input.

    I think (just my opinion) it would be easier on the user if the program
    would count the nested blocks inside each block. What if the user didn't
    know how many blocks were in block "A" (like a new employee or if you
    received a drawing from someone outside the company). What if your drawing
    consisted of blocks A-Z, and blocks 1-99 each of which had anywhere from 0
    to 2 or 3 nested blocks (and maybe a couple had blocks that were nested 3 or
    4 levels deep - i.e. a faucet block inside a sink block inside a building
    block) - of course a drawing this complicated is very unlikely. What you
    suggest would work, it just might be harder if the drawing were very
    complicated or the user was unfamilar with the blocks in the drawing.
     
    Wade, Dec 30, 2004
    #9
  10. gizmowiebe

    Ed Jobe Guest

    When I said, "you", I meant you as a programmer. Your code needs to examine
    each block def for nested blocks, then multiply by the parent's count.
     
    Ed Jobe, Dec 30, 2004
    #10
  11. gizmowiebe

    Ed Jobe Guest

    BTW, I didn't examine Wade's code closely...until now. It uses the Block's
    Count prop. This does not return the number of times the block was inserted,
    but the Count of Items in the block. To get the number of insertions, you
    have to iterate MS and/or PS looking for AcadBlockRef objects or use a
    filtered selection set. There's lots of examples on this ng of how to do
    that.
     
    Ed Jobe, Dec 30, 2004
    #11
  12. gizmowiebe

    Wade Guest

    OK, thanks.

    Being relatively new to Autocad vba, I'm not sure how to do this (refering
    to objects parent and getting parent info). Do you have an example? (always
    looking to learn new stuff)
    Thanks.
     
    Wade, Dec 30, 2004
    #12
  13. gizmowiebe

    Wade Guest

    I think one example is under the post "Re: Count block add to listbox
    (Better)", and I'm sure there are even better ways to accomplish this.
     
    Wade, Dec 30, 2004
    #13
  14. gizmowiebe

    Ed Jobe Guest

    I just did search in this ng for "count block" and got all kinds of samples.
    Looking at the names of the posters, I'm sure you'll find something useful
    right away.
     
    Ed Jobe, Dec 30, 2004
    #14
  15. gizmowiebe

    Wade Guest

    Must be my ng search isn't working, because the only "hits" I receive is
    this thread. It doesn't matter if I set either of the "Received before" or
    "Recieved after" dates or leave them blank (uncheck), if I put the "count
    block" in the Subject Field or the Message Field or both, I still only get
    this thread I even changed the "Look in" field to
    "discussion.autodesk.com". I'll try googles search....

    Thanks anyway.
     
    Wade, Dec 30, 2004
    #15
  16. gizmowiebe

    Ed Jobe Guest

    Ed Jobe, Dec 30, 2004
    #16
  17. gizmowiebe

    Anne Brown Guest

    As Ed said, you are searching only the messages on your harddrive
    from using OE.

    The discussion group search engine provides flexibility for
    searching through existing message content. To search:

    1. Go to http://discussion.autodesk.com/

    2. Select a product (Example: AutoCAD which includes VBA)

    3. Type your keywords into the search box and click Search. Note:
    You can further refine your query by selecting a specific
    discussion group from the dropdown provided. Your group choices
    will vary in the drop down, based on where you have navigated on
    the discussion group site.

    Search results are sorted by relevance (a complex query of all
    relevant content based on your keywords and group choices).
    Clicking on the message result will display the entire thread
    (the series of messages and replies). Clicking on the Post date
    will sort the results by date with most recent first.
     
    Anne Brown, Dec 30, 2004
    #17
  18. gizmowiebe

    Matt W Guest

    This snippet will count the blocks nested within xrefs.
    Examine the code and you should be able to figure out how to change it to
    search for block within blocks.

    Code:
    Dim MyBlkArray() As Variant
    Dim i As Long, j As Long
    Dim count As Integer
    Dim index As Integer
    Dim FoundBlock As Boolean
    Dim entXref As AcadExternalReference
    Dim entBlock As AcadBlock
    Dim entNestedBlock As AcadBlockReference
    Dim ent1, ent2 As AcadEntity
    
    count = ThisDrawing.ModelSpace.count
    i = 0
    For index = 0 To count - 1
    If ThisDrawing.ModelSpace.Item(index).ObjectName =
    "AcDbBlockReference" Then
    j = 1
    FoundBlock = False
    While j <= i
    If MyBlkArray(0, j) =
    ThisDrawing.ModelSpace.Item(index).Name Then
    MyBlkArray(1, j) = MyBlkArray(1, j) + 1
    j = i
    FoundBlock = True
    End If
    j = j + 1
    Wend
    If Not FoundBlock Then
    i = i + 1
    ReDim Preserve MyBlkArray(2, i)
    MyBlkArray(0, i) = ThisDrawing.ModelSpace.Item(index).Name
    MyBlkArray(1, i) = 1
    End If
    End If
    Next index
    
    For Each ent1 In ThisDrawing.ModelSpace
    If TypeOf ent1 Is AcadExternalReference Then
    Set entXref = ent1
    Set entBlock = ThisDrawing.Blocks(entXref.Name)
    For Each ent2 In entBlock
    If TypeOf ent2 Is AcadBlockReference Then
    Set entNestedBlock = ent2
    j = 1
    FoundBlock = False
    While j <= i
    If MyBlkArray(0, j) = entNestedBlock.Name Then
    MyBlkArray(1, j) = MyBlkArray(1, j) + 1
    j = i
    FoundBlock = True
    End If
    j = j + 1
    Wend
    If Not FoundBlock Then
    i = i + 1
    ReDim Preserve MyBlkArray(2, i)
    MyBlkArray(0, i) = entNestedBlock.Name
    MyBlkArray(1, i) = 1
    End If
    j = j + 1
    End If
    Next ent2
    End If
    Next
    
    Me.ListBoxBlocks.ColumnCount = 2
    Me.ListBoxBlocks.ColumnWidths = "150;36"
    Me.ListBoxBlocks.Column() = MyBlkArray
    
     
    Matt W, Jan 3, 2005
    #18
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.