Try this one Luke
(defun c:recount (/ block_list cnt env_name)
;; The intent of this app is to count the number of blocks inserted into
;; a given dwg and set environment variables based on the block name so
;; one can use the diesel expression '$(getenv, <name>)' in a table cell
;; to track the number of block insertitions therein
;;
;; Typical output
;;
;; Set BLOCK_COUNT_ELM to 6
;; Set BLOCK_COUNT_CITRUS to 5
;; Set BLOCK_COUNT_OAK to 11
;; Set BLOCK_COUNT_PALM to 5
;;
;; Given the above output you would use '$(getenv, BLOCK_COUNT_OAK)' in
;; your table cell
;;
;; version 2.0
;; Wed May 02, 2007
;; Mark S. Thomas ( mark@theswamp.org )
;
; list of blocks you wish to count
;
(setq block_list
(list
;
; add a line for each block you wish to count
; the following are just samples
;
(cons '2 "ELM")
(cons '2 "CITRUS")
(cons '2 "OAK")
(cons '2 "PALM")
)
)
(foreach i block_list
(setq ss (ssget "X" (list '(0 . "INSERT") i)))
(if ss
(progn
(setq
;
; count the number of blocks inserted
; create the environment name, i.e. "BLOCK_COUNT_ELM"
;
cnt (itoa (sslength ss))
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
; finally we set the environment variable
(setenv env_name cnt)
; show the user what was set and how many blocks were counted
(princ (strcat "\nSet " env_name " to " cnt))
)
;; else if ss = nil then no blocks were found and
;; we set 'env_name' to zero
(progn
(setq
;
; create the environment name, i.e. "BLOCK_COUNT_ELM"
;
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
; finally we set the environment variable to zero
(setenv env_name "0")
; show the user what was set and how many blocks were counted
(princ (strcat "\nSet " env_name " to 0"))
)
)
)
(princ)
)