(defun c:block_accum ( / blocklst flag blocknam f ss x fnam cnt) ; First, determine if running list fnam is already in memory. ; If not, then it means that this is ; being run for the first time during this AutoCAD session ; and need to prompt for output file name. (setq fnam nil) ; File name held in "blackboard" namespace. This means ; that it is accessible from any document (i.e. can be ; in multi-document mode with multiple drawings open on ; the screen). (if (OR (not (setq fnam (vl-bb-ref 'block_accum_fnam))) (not (findfile fnam))) ; file doesn't exist (progn ; looks like this is first run (if (setq fnam (getfiled "Enter csv file name" "" "CSV" 1)) (progn ; Save file name in "black board" memory (vl-bb-set 'block_accum_fnam fnam) ) ) ) ) (setq blocklst nil) (if (AND fnam (setq f (open fnam "a"))) ; open for append (progn (setq flag T) ; flag to rewind to beginning of BLOCK table (while (/= (setq x (tblnext "BLOCK" flag)) nil) (setq flag nil) (setq blocknam (cdr (assoc 2 x))) (setq blocklst (cons blocknam blocklst)) ) ; Now sort sort block name list (setq blocklst (acad_strlsort blocklst)) ; Now output the list to a csv format file (foreach blocknam blocklst ; Figure out how many instances of this block ; found in the drawing (setq ss (ssget "_X" (list (cons 2 blocknam)))) (setq cnt 0) (if (/= ss nil) (progn (setq cnt (sslength ss)) ; length of set (setq ss nil) ; release selection set ) ) ; Format the active drawing name, block name ; and count of block instances ; into line of comma-delimited text and then ; write it to the output text file. (write-line (strcat "\"" (getvar "DWGNAME") "\",\"" blocknam "\"," (itoa cnt)) f) ; Also write to command window (princ (strcat blocknam " " (itoa cnt) "\n")) ) (close f) (setq f nil) ) ) ; remind user of the output file name (if fnam (princ (strcat "\n" fnam))) (princ) )