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
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
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
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
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
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
Lee Mac Programming
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Just another Swamper
Bookmarks