Results 1 to 9 of 9

Thread: Counting Block references

  1. #1
    100 Club amaser's Avatar
    Join Date
    2006-05
    Location
    Joliet, IL
    Posts
    105
    Login to Give a bone
    0

    Default Counting Block references

    I have a quick form set up with some common commands.
    I am trying to get one to count blocks by name on a particular layer.

    (IE count all the proposed fire hydrants, proposed manholes, etc)

    if i use a for each block in thisdocument.blocks
    then layer isnt a valid choice to filter on.

    if i try to count block references, i cant get anything to run at all.

    if i use the block.count property, i get all of them, regardless of layer

    can anyone help?

    let me know if more information is needed.



    Dim intHyd As Integer
    Dim intBbox As Integer
    Dim cadBlock As AcadBlock
    Dim cadBlocks As AcadBlocks
    Set cadBlocks = ThisDrawing.Blocks
    For Each cadBlock In cadBlocks
    If cadBlock.Name = "HYD" Then
    intHyd = intHyd + 1
    End If
    If cadBlock.Name = "BBOX" Then
    intBbox = cadBlock.Count
    End If
    Next cadBlock

    is what i have so far, then i use a msgbox to display it, but it doesnt work

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,416
    Login to Give a bone
    0

    Default Re: Counting Block references

    That's a mistake made by many. For some reason, when it comes to programming, people tend to forget everything they know about blocks. The Blocks collection holds block definitions (AcadBlock). You have to search ps or ms for block insertions (AcadBlockReference) to get a count. There are two methods. You can either use a filtered selectionset for specific block names or iterate ms/ps searching for AcadBlockReference objects. Search this forum for Blocks and you should find plenty of code samples.
    C:> ED WORKING....


    LinkedIn

  3. #3
    100 Club amaser's Avatar
    Join Date
    2006-05
    Location
    Joliet, IL
    Posts
    105
    Login to Give a bone
    0

    Default Re: Counting Block references

    i made a slight modification to some code that i found here in the forums, but how do i specify which blk name to count? I assume it has something to do with the grpCode and dataCode, but i dont know how to modify that section.

    Code:
    Public Function cntBlocks(layName As String, blkName As String) As Integer
      'this counts the number of blocks with the given name and on the given layer
      
      'declare variables
      Dim blkSelSet As AcadSelectionSet
      Dim blkRef As AcadEntity
      Dim grpCode(0 To 3) As Integer
      Dim dataCode(0 To 3) As Variant
      
      'set initial values
      grpCode(0) = -4
      grpCode(1) = 0
      grpCode(2) = 8
      grpCode(3) = -4
      dataCode(0) = "<AND"
      dataCode(1) = "INSERT"
      dataCode(2) = layName
      dataCode(3) = "AND>"
      
      'clear existing selection sets
      For Each blkSelSet In ThisDrawing.SelectionSets
        If blkSelSet.Name = "Sel_Blocks" Then
          blkSelSet.Delete
          Exit For
        End If
      Next blkSelSet
      
      'get new selection set
      Set blkSelSet = ThisDrawing.SelectionSets.Add("Sel_Blocks")
      blkSelSet.Select acSelectionSetAll, , , grpCode, dataCode
      
      'return results
      cntBlocks = blkSelSet.Count
      
    End Function
    Last edited by Ed Jobe; 2007-05-21 at 07:26 PM. Reason: code tags added

  4. #4
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,416
    Login to Give a bone
    0

    Default Re: Counting Block references

    Correct. You need to expand the array to include a filter item for names. That's dxf group code 2.

    ps. It helps to use code tags in your posts. They are available from the advanced editor. See the difference in your last post from the first one. It doesn't show as well in this case, because you already lost it, but code formatting is better preserved.
    Last edited by Ed Jobe; 2007-05-21 at 07:28 PM.
    C:> ED WORKING....


    LinkedIn

  5. #5
    100 Club amaser's Avatar
    Join Date
    2006-05
    Location
    Joliet, IL
    Posts
    105
    Login to Give a bone
    0

    Default Re: Counting Block references

    it seems to be mostly working, but now i have a different but related problem:

    one of my block references has a name of *U535 instead of "HYD" which its supposed to have, and i cant figure out why. i removed the object, and used the insert command to do it, but i still cant figure out why its not using the correct name.
    any ideas?

    (The count worked for one of the other blocks, so it apears to work okay, not the problem is in the block insertion in LDD)

  6. #6
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,416
    Login to Give a bone
    0

    Default Re: Counting Block references

    Is "HYD" a dynamic block? If it is, search this forum for EffectiveName.
    C:> ED WORKING....


    LinkedIn

  7. #7
    100 Club amaser's Avatar
    Join Date
    2006-05
    Location
    Joliet, IL
    Posts
    105
    Login to Give a bone
    0

    Default Re: Counting Block references

    it is a dynamic block, but the 3 other blocks where the count is working are also dynamic.
    is there a different function for counting dynamic blocks?

  8. #8
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,416
    Login to Give a bone
    0

    Default Re: Counting Block references

    DB's work like dims. They rename component blocks using unamed block convention "*Uxxx" and then use the EffectiveName property to tie them together. Just use the filter to find any block on that layer. Once you have your ss, examine each block's EffectiveName prop to increment a count.
    C:> ED WORKING....


    LinkedIn

  9. #9
    All AUGI, all the time zoomharis's Avatar
    Join Date
    2005-02
    Location
    Abu Dhabi (Native-India)
    Posts
    506
    Login to Give a bone
    0

    Default Re: Counting Block references

    Once you finished experimenting with your code, follow the 'Block Counter' link in my signature area. You can compare the code with yours and develop a better application. Remember...., the more you code yourself, the more you learn about the subject.

Similar Threads

  1. Replies: 0
    Last Post: 2015-03-20, 10:57 AM
  2. Replies: 2
    Last Post: 2011-05-23, 12:03 PM
  3. Counting ROWS in a Note Block/Schedule
    By Joshua Kohl in forum Revit Architecture - General
    Replies: 1
    Last Post: 2010-08-17, 08:26 PM
  4. Block Counting and Quick Select
    By Grumpy in forum Dynamic Blocks - Technical
    Replies: 3
    Last Post: 2006-02-08, 04:38 PM
  5. Block Counting Tool
    By luis.93102 in forum AutoCAD LT - General
    Replies: 2
    Last Post: 2005-08-25, 03:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •