List of blocks

Discussion in 'AutoCAD' started by KingCAD, Sep 17, 2003.

  1. KingCAD

    KingCAD Guest

    Hi guys;
    Can someone tell me (in simple terms) how I can get a list of the blocks contained in the current drawing and how many instances have been inserted? I believe that I can put them in a list box with "additem", but I just don't seem to be able to wrap my little brain around how to get the names and quantity in the first place.

    Thanks

    Dave. K
     
    KingCAD, Sep 17, 2003
    #1
  2. Here's a quick solution. There are lots of things you can do to improve it but it should get you going in the right direction:

    Type BlockRecord   Name As String   Count As Integer End Type

    Public Sub CountBlocks()   Dim Entity As AcadEntity   Dim BlockRecords() As BlockRecord   Dim Block As AcadBlock   Dim BlockRef As AcadBlockReference   Dim I As Integer

    ReDim BlockRecords(0)   For Each Block In ThisDrawing.Blocks     ReDim Preserve BlockRecords(UBound(BlockRecords) + 1) As BlockRecord     BlockRecords(UBound(BlockRecords)).Name = Block.Name     BlockRecords(UBound(BlockRecords)).Count = 0   Next Block

    For Each Entity In ThisDrawing.ModelSpace     If TypeOf Entity Is AcadBlockReference Then       Set BlockRef = Entity       For I = LBound(BlockRecords) To UBound(BlockRecords)         If BlockRef.Name = BlockRecords(I).Name Then           BlockRecords(I).Count = BlockRecords(I).Count + 1         End If       Next I     End If   Next Entity

    For Each Entity In ThisDrawing.PaperSpace     If TypeOf Entity Is AcadBlockReference Then       Set BlockRef = Entity       For I = LBound(BlockRecords) To UBound(BlockRecords)         If BlockRef.Name = BlockRecords(I).Name Then           BlockRecords(I).Count = BlockRecords(I).Count + 1         End If       Next I     End If   Next Entity

    For Each Block In ThisDrawing.Blocks     For Each Entity In Block       If TypeOf Entity Is AcadBlockReference Then         Set BlockRef = Entity         For I = LBound(BlockRecords) To UBound(BlockRecords)           If BlockRef.Name = BlockRecords(I).Name Then             BlockRecords(I).Count = BlockRecords(I).Count + 1           End If         Next I       End If     Next Entity   Next Block

    For I = LBound(BlockRecords) To UBound(BlockRecords)     Debug.Print BlockRecords(I).Name & vbTab & BlockRecords(I).Count   Next I End Sub
     
    Mark_Abercrombie, Sep 17, 2003
    #2
  3. KingCAD

    KingCAD Guest

    Thanks Mark (I think), That's sure on the right track unfortunately a little over my head so I haven't really figured out hoe to modify it for me, and the biggest trouble is that it gives me a count of 2 times the actual number of blocks inserted, as well as modelspace and paperspace. I figured out how to get rid of modelspace and paper space, but I'm stuck on how to fix the count. It looks like you're doing more than I need - All I really need is to populate a list box with the name(s) and count of the blocks in the drawing.
    Thanks again and I'll keep poking it around to see if I can make it work for me.

    Dave. K
     
    KingCAD, Sep 17, 2003
    #3
  4. KingCAD

    Mark Propst Guest

    dimensions are blocks!



    is that part of it?



    "KingCAD" <> wrote in message news:...

    Thanks Mark (I think), That's sure on the right track unfortunately a little over my head so I haven't really figured out hoe to modify it for me, and the biggest trouble is that it gives me a count of 2 times the actual number of blocks inserted, as well as modelspace and paperspace. I figured out how to get rid of modelspace and paper space, but I'm stuck on how to fix the count. It looks like you're doing more than I need - All I really need is to populate a list box with the name(s) and count of the blocks in the drawing.
    Thanks again and I'll keep poking it around to see if I can make it work for me.

    Dave. K
     
    Mark Propst, Sep 17, 2003
    #4
  5. KingCAD

    KingCAD Guest

    Hey Mark,
    No, that is not it (YET). It's listing the block by name "Ralph" and giving a variety of quantities, I was getting 2 times the number so I said [LstBlockNames.AddItem BlockRecords(I).Name & " ............ " & (BlockRecords(I).Count / 2)] and that worked fine on my test drawing, BUT when I opened an actual drawing, Now I get 1.5 (or 5.5, 2.5...)as a quantity for blocks I can't even locate in the AutoCAD insert blocks dialog box. I purge the drawing and no change. What I'm trying to end up with is a listbox that lists the block(s) by name and shows how many are inserted into the active drawing. You know "ralph .....6", Tommy ......5", "Sally ...... 0", and so on. I thought that would be an easy one - OOPPS!-
    Thanks again for you efforts.
    Dave. K
     
    KingCAD, Sep 17, 2003
    #5
  6. KingCAD

    Mark Propst Guest

    if you just want a count of inserts don't count the block table



    get a filtered selection set and use the count



    it could have blocks defined but not inserted?



     



    "KingCAD" <> wrote in message news:...

    Hey Mark,
    No, that is not it (YET). It's listing the block by name "Ralph" and giving a variety of quantities, I was getting 2 times the number so I said [LstBlockNames.AddItem BlockRecords(I).Name & " ............ " & (BlockRecords(I).Count / 2)] and that worked fine on my test drawing, BUT when I opened an actual drawing, Now I get 1.5 (or 5.5, 2.5...)as a quantity for blocks I can't even locate in the AutoCAD insert blocks dialog box. I purge the drawing and no change. What I'm trying to end up with is a listbox that lists the block(s) by name and shows how many are inserted into the active drawing. You know "ralph .....6", Tommy ......5", "Sally ...... 0", and so on. I thought that would be an easy one - OOPPS!-
    Thanks again for you efforts.
    Dave. K
     
    Mark Propst, Sep 17, 2003
    #6
  7. KingCAD

    KingCAD Guest

    Got it! I took out [For Each Block In ThisDrawing.Blocks
        For Each Entity In Block
          If TypeOf Entity Is AcadBlockReference Then
            Set BlockRef = Entity
            For I = LBound(BlockRecords) To UBound(BlockRecords)
              If BlockRef.Name = BlockRecords(I).Name Then
                BlockRecords(I).Count = BlockRecords(I).Count + 1
              End If
            Next I
          End If
        Next Entity
      Next Block]
    And now it works fine. Thank you very much for your help.
    Dave. K
     
    KingCAD, Sep 18, 2003
    #7
  8. I would advise against iterating the entities in model space. For starters, you'd ignore all block references in all paper space layouts except the current one. Second, it would be much simpler to plug the block name into a selection set filter then read the SelectionSet's Count property.




    --
    There are 10 kinds of people. Those who understand binary and those who don't.



     



    http://code.acadx.com




    "Mark_Abercrombie" <> wrote in message news:...

    Here's a quick solution. There are lots of things you can do to improve it but it should get you going in the right direction:

    Type BlockRecord   Name As String   Count As Integer End Type

    Public Sub CountBlocks()   Dim Entity As AcadEntity   Dim BlockRecords() As BlockRecord   Dim Block As AcadBlock   Dim BlockRef As AcadBlockReference   Dim I As Integer

    ReDim BlockRecords(0)   For Each Block In ThisDrawing.Blocks     ReDim Preserve BlockRecords(UBound(BlockRecords) + 1) As BlockRecord     BlockRecords(UBound(BlockRecords)).Name = Block.Name     BlockRecords(UBound(BlockRecords)).Count = 0   Next Block

    For Each Entity In ThisDrawing.ModelSpace     If TypeOf Entity Is AcadBlockReference Then       Set BlockRef = Entity       For I = LBound(BlockRecords) To UBound(BlockRecords)         If BlockRef.Name = BlockRecords(I).Name Then           BlockRecords(I).Count = BlockRecords(I).Count + 1         End If       Next I     End If   Next Entity

    For Each Entity In ThisDrawing.PaperSpace     If TypeOf Entity Is AcadBlockReference Then       Set BlockRef = Entity       For I = LBound(BlockRecords) To UBound(BlockRecords)         If BlockRef.Name = BlockRecords(I).Name Then           BlockRecords(I).Count = BlockRecords(I).Count + 1         End If       Next I     End If   Next Entity

    For Each Block In ThisDrawing.Blocks     For Each Entity In Block       If TypeOf Entity Is AcadBlockReference Then         Set BlockRef = Entity         For I = LBound(BlockRecords) To UBound(BlockRecords)           If BlockRef.Name = BlockRecords(I).Name Then             BlockRecords(I).Count = BlockRecords(I).Count + 1           End If         Next I       End If     Next Entity   Next Block

    For I = LBound(BlockRecords) To UBound(BlockRecords)     Debug.Print BlockRecords(I).Name & vbTab & BlockRecords(I).Count   Next I End Sub
     
    Frank Oquendo, Sep 18, 2003
    #8
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.