+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19
  1. #1
    Senior Member JoeC's Avatar
    Using
    AutoCAD LT 2011
    Join Date
    Aug 2008
    Location
    Georgia, USA
    Posts
    180

    Default Best block counting method?

    Registered forum members do not see this ad.

    I have a large quantity of the same block that I need an accurate count of. Some of them are "lone" blocks and some of them are nested in another block.

    The BCOUNT command will not count the nested blocks. QSELECT worked the first time, but now it will not count the nested blocks either!?!. (Is there a setting for this?)

    What would you all recommend as the quickest / easiest way to achieve this?

    Thanks!
    Last edited by JoeC; 21st Aug 2009 at 08:53 pm.
    "You really don’t know how much you know until you know how much you don’t know." - Rev. Darrell W. Boswell

    The Mighty Mother Frogs - SGFL Fantasy Football champions 2006, 2008

  2. #2
    Forum Deity rkent's Avatar
    Computer Details
    rkent's Computer Details
    Operating System:
    WIN7 PRO 64bit
    Computer:
    Dell Precision T7500
    CPU:
    Xeon Dual Core E5503 2.0Ghz
    RAM:
    6GB DDR3 ECC SDRAM, 1333MHz, 6x1GB
    Graphics:
    nVidia Quadro 3800
    Primary Storage:
    300Gb, 10K rpm
    Monitor:
    Dell 24" FS (2x)
    Discipline
    Mechanical
    rkent's Discipline Details
    Occupation
    Facilities Design - Semi-conductor manufacturing
    Discipline
    Mechanical
    Details
    Designer: Process Piping, P&ID's, Machine Part Design, Planning / General Arrangement. Facilities Management. Drafting: Electrical, Controls, Architectural.
    Using
    Mechanical 2014
    Join Date
    Nov 2007
    Location
    Lat:35, Long:-106, Elev: 5164'
    Posts
    3,371

    Default

    Quote Originally Posted by JoeC View Post
    I have a large quantity of the same block that I need an accurate count of. Some of them are "lone" blocks and some of them are nested in another block.

    The BCOUNT command will not count the nested blocks. QSELECT worked the first time, but now it will not count the nested blocks either!?!. (Is there a setting for this?)

    What would you all recommend as the quickest / easiest way to achieve this?

    Thanks!
    Maybe try this one? Not my own.
    Attached Files
    "You are entitled to your own opinion but you are not entitled to your own facts." Daniel Patrick Moynihan

  3. #3
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,711

    Default

    Try this:

    Code:
    ;; Block Counter by Lee McDonnell (Lee Mac) ~ 22.08.2009
    ;; Copyright © August 2009
    
    ;; Will Count all instances of a block, including nested.
    
    (defun BlkCount (Blk / i j ss *blk)
      (vl-load-com)
      (setq i 0 Blk (strcase Blk) j -1)
    
      (defun GetNest (Obj Nme)
        (and (eq (strcase (vla-get-Name Obj)) Nme)
             (setq i (1+ i)))
        (vlax-for Sub Obj
          (if (eq "AcDbBlockReference" (vla-get-ObjectName Sub))
            (GetNest (vla-item *blk (vla-get-Name Sub)) Nme))))
    
      (if (setq ss (ssget "_X" '((0 . "INSERT"))))
        (while (setq ent (ssname ss (setq j (1+ j))))
          (GetNest
            (vla-item
              (setq *blk
                (vla-get-blocks
                  (vla-get-ActiveDocument
                    (vlax-get-acad-object))))
              (cdr (assoc 2 (entget ent)))) Blk)))
    
      i)
    
    (defun c:test (/ str)
      (if (setq str (getstring t "\nSpecify Block Name: "))
        (princ
          (strcat
            "\nInstances: " (itoa (BlkCount str)))))
      (princ))
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  4. #4
    Senior Member JoeC's Avatar
    Using
    AutoCAD LT 2011
    Join Date
    Aug 2008
    Location
    Georgia, USA
    Posts
    180

    Default

    Thanks for the responses:

    Rkent...the lisp worked o.k., but it did not count the nested blocks.

    Lee...I figured out that the "test" counts a specified block (and it did work well), but "blkcount" was not recognized as a command. What does it do? Did I do something wrong?

    Thanks for the help & regards,
    "You really don’t know how much you know until you know how much you don’t know." - Rev. Darrell W. Boswell

    The Mighty Mother Frogs - SGFL Fantasy Football champions 2006, 2008

  5. #5
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,711

    Default

    Joe...

    BlkCount is a sub-function, meaning that it is a function to be called with arguments and is a sort of "helper function" to the main routine.

    The syntax call for this program is indeed test.

    I have updated this routine somewhat:

    Code:
    ;; Block Counter by Lee McDonnell (Lee Mac) ~ 22.08.2009
    ;; Copyright © August 2009
    
    ;; Will Count all instances of a block, including nested.
    
    (defun BlkCount (Blk / i j ss *blk)
      (vl-load-com)
      (setq i 0 Blk (strcase Blk) j -1
            *blk (vla-get-Blocks
                   (vla-get-ActiveDocument
                     (vlax-get-acad-object))))
    
      (defun GetNest (Obj Nme)
        (and (eq (strcase (vla-get-Name Obj)) Nme)
             (setq i (1+ i)))
        (vlax-for Sub Obj
          (if (eq "AcDbBlockReference" (vla-get-ObjectName Sub))
            (GetNest (vla-item *blk (vla-get-Name Sub)) Nme))))
    
      (if (setq ss (ssget "_X" '((0 . "INSERT"))))
        (while (setq ent (ssname ss (setq j (1+ j))))
          (GetNest
            (vla-item *blk (cdr (assoc 2 (entget ent)))) Blk)))
      
      i)
    
    (defun c:test (/ str lst tdef)
      (while
        (progn
          (setq str (getstring t "\nSpecify Block Name <All> : "))
          (cond ((eq "" str)
                 (while (setq tdef (tblnext "BLOCK" (null tdef)))
                   (setq lst (cons (cdr (assoc 2 tdef)) lst))) nil)
                ((and (snvalid str)
                      (tblsearch "BLOCK" str))
                 (setq lst (list str)) nil)
                (t (princ "\n** Block not Found **")))))
    
      (setq mstr (+ 5 (apply 'max (mapcar 'strlen lst))))             
      (princ (strcat (Pad "\n Block" 32 mstr) "| Count"))
      (princ (strcat (Pad "\n " 45 mstr) (Pad "|" 45 10)))
    
      (foreach x lst
        (setq i (Blkcount x))
        (princ
          (strcat
            (Pad (strcat "\n " x) 46 mstr)
            (Pad "|" 46 (- 10 (strlen (itoa i)))) (itoa i))))        
    
      (princ))
    
    (defun Pad (Str Chc Len)
      (while (< (strlen Str) Len)
        (setq Str (strcat Str (chr Chc))))
      Str)
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  6. #6
    Luminous Being alanjt's Avatar
    Using
    Civil 3D 2011
    Join Date
    Apr 2008
    Posts
    5,993

    Default

    Quote Originally Posted by JoeC View Post
    Thanks for the responses:

    Rkent...the lisp worked o.k., but it did not count the nested blocks.

    Lee...I figured out that the "test" counts a specified block (and it did work well), but "blkcount" was not recognized as a command. What does it do? Did I do something wrong?

    Thanks for the help & regards,
    blcount is a subroutine. if you type (blkcount "DETAIL") it will count every occurance of a block named "DETAIL".

  7. #7
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,711

    Default

    Joe,

    Take a read of this article - it will explain about sub-functions:

    http://www.afralisp.net/lispa/lisp5.htm
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  8. #8
    Senior Member JoeC's Avatar
    Using
    AutoCAD LT 2011
    Join Date
    Aug 2008
    Location
    Georgia, USA
    Posts
    180

    Default

    Thanks for the lisp, Lee. It is EXACTLY what I needed.
    "You really don’t know how much you know until you know how much you don’t know." - Rev. Darrell W. Boswell

    The Mighty Mother Frogs - SGFL Fantasy Football champions 2006, 2008

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,711

    Default

    Quote Originally Posted by JoeC View Post
    Thanks for the lisp, Lee. It is EXACTLY what I needed.
    You're very welcome Joe, happy to be of assistance.

    Lee
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  10. #10
    Luminous Being alanjt's Avatar
    Using
    Civil 3D 2011
    Join Date
    Apr 2008
    Posts
    5,993

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by Lee Mac View Post
    You're very welcome Joe, happy to be of assistance.

    Lee
    nice work btw lee.

Similar Threads

  1. block/attribute counting
    By SydneyCB in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 9th Jun 2009, 09:08 pm
  2. Replies: 2
    Last Post: 17th Dec 2008, 03:50 am
  3. Load method with VBA
    By Ndape in forum AutoLISP, Visual LISP & DCL
    Replies: 43
    Last Post: 20th Jun 2008, 04:42 pm
  4. Counting the number of block references in a drawing
    By Autodesk in forum AutoCAD RSS Feeds
    Replies: 0
    Last Post: 14th Sep 2006, 03:30 am
  5. Block or attribute counting
    By T9PMK in forum AutoCAD Drawing Management & Output
    Replies: 6
    Last Post: 21st Mar 2005, 12:00 pm

Bookmarks

Posting Permissions

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