What it does
Count Selected Blocks with the COUNTBLOCKS AutoLISP command
A compact quantity-checking helper for fixtures, devices, symbols, and repeated components. It counts INSERT entities in a deliberate selection and prints an alphabetical summary.
- AutoLISP command
- COUNTBLOCKS
- Difficulty
- Intermediate
Applicable disciplines
CAD workflows that use this routine
Requirements and compatibility
What it needs
Software rows marked “Not tested” are not compatibility claims. Test the routine in a copy of your drawing and CAD version.
Downloadable source code
Review the count-blocks.lsp AutoLISP code
;; CADLisp Tools - Count Selected Blocks
(vl-load-com)
(defun ct-cb:bump (key items / hit)
(if (setq hit (assoc key items))
(subst (cons key (1+ (cdr hit))) hit items)
(cons (cons key 1) items)))
(defun ct-cb:name (ent / data obj result)
(setq data (and ent (entget ent)))
(if data
(progn
(setq obj (vl-catch-all-apply 'vlax-ename->vla-object (list ent)))
(if (not (vl-catch-all-error-p obj))
(progn
(if (vlax-property-available-p obj 'EffectiveName)
(setq result (vl-catch-all-apply 'vla-get-EffectiveName (list obj))))
(if (or (null result) (vl-catch-all-error-p result))
(setq result (vl-catch-all-apply 'vla-get-Name (list obj))))))
(if (or (null result) (vl-catch-all-error-p result))
(setq result (cdr (assoc 2 data))))
result)))
(defun c:COUNTBLOCKS (/ counts ent index name pair set skipped)
(prompt "\nSelect block references to count: ")
(if (setq set (ssget '((0 . "INSERT"))))
(progn
(setq index 0 counts nil skipped 0)
(repeat (sslength set)
(setq ent (ssname set index) name (ct-cb:name ent))
(if name
(setq counts (ct-cb:bump name counts))
(setq skipped (1+ skipped)))
(setq index (1+ index)))
(setq counts (vl-sort counts '(lambda (a b) (< (strcase (car a)) (strcase (car b))))))
(prompt "\n--- Block count (dynamic effective names) ---")
(foreach pair counts (prompt (strcat "\n" (car pair) ": " (itoa (cdr pair)))))
(prompt (strcat "\nInvalid references skipped: " (itoa skipped))))
(prompt "\nNo block references selected."))
(princ))
Setup
How to install the count-blocks.lsp file
- 1Download and review the source.
- 2Load it with APPLOAD.
- 3Open command history to copy the summary.
Command workflow
How to run the COUNTBLOCKS command
- 1Run COUNTBLOCKS.
- 2Select block references.
- 3Press Enter and read the alphabetical count.
Worked example
Example: Check the quantity of selected fixture symbols.
- Run COUNTBLOCKS.
- Select the plan area.
- Review the block-name totals.
A block-by-block quantity summary appears in command history.
Read before use
Notes and limitations
- Uses ActiveX to resolve dynamic-block effective names.
- Does not inspect nested blocks.
- Does not export a BOM file.
- Not manually CAD-tested.
Version history