Jump to content

A LISP that can count and Name the blocks being Selected [help]


Recommended Posts

Hello dear Masters,


My name is Sean and I'm very new to this Lisp programming. it's like 2 weeks now and I begin to frustrate that I can't get my code work. first thing I was able to make the lisp to count the blocks being selected and i was very happy to it. then I decided to name the blocks also that is being selected. I know about the command "BCOUNT" but older versions of AutoCAD has no bcount command yet (Autocad 2010) I really wanted to learn LISP stuffs but gets stuck every time. Please help me with this code. I've been to many sites and still can't figure out what to do next. Thank you in advance masters. :D



so here's my code


(defun c:BL (/ ssblks ssblks2 en ctr blk ent enlist) ;define funcntion

(if (setq ssblks (ssget))) ;|selects blocks|;


		(setq blk (sslength ssblks)) ;|get number of blocks|;

		(setq ct 0) ;|set counter to zero|;

		(repeat (ssget ssblks2)) ;|repeat the selecting blocks|;

		(ssadd (ssname ssblks2 ct) ssblks) ;|add ssname froms ssblks selection|;

		(setq en (car(entsel ssblks2)) ;|gets entity from ssblks2|;

		(setq enlist(cdr (assoc 2 (entget en)))) ;|get the dxf group codes of the entity;|

		(alert (strcat "\n There are " (itoa blk) " blocks and these are: " (vla-get-effectivename (vlax-ename->vla-object enlist)) )) ;|display result|;

	) ;end prog


) ;end if


);end defun


Link to comment
Share on other sites

Welcome to the Forum, Sean!





At a glance:

  • there is no PROG statement, maybe PROGN.
  • please check the REPEAT function in help; it will require the number of cycles and an action.
  • SSGET will return a selection set which should be retrieved into a variable, it doesn't support assignment by reference.
  • the ct counter is indexed nowhere.
  • ENTSEL require a string (the prompt) as argument, not a selection set.
  • VLAX-ENAME->VLA-OBJECT is expecting an entity name (DXF code -1).

Link to comment
Share on other sites

Just for your information, BCOUNT has been around for quite some time, it is an Express Tool. Maybe you do not have Express Tools Loaded?



Counts, itemizes, and displays in tabular form, the number of insertions of each block in the selected objects or in the entire drawing.

Link to comment
Share on other sites


Thank you so much for that information. helped me a lot. for now i will break down the codes first. Autolisp is the first programming i've ever learn. very different in web making. haha.



Yes I know of that already. it's a great tool but for those who have lower version of cad like 2010. Bcount is not yet of the program. that's why I'm gonna make one. :D

Link to comment
Share on other sites

Reposting according to guidelines...I hope.


Hello Sean,

A company I worked for a decade ago used the following code to display each block name and the number of instances each appear. It was probably found/copied via forums like this one. There's no documentation as to who wrote this code, nor how old it really is. If anyone recognizes it as theirs, my sincere gratitude for the use!


When you run BLKLST and it asks you to Select Objects, type ALL. F2 to show the list. Hope it works how you need it...


                  BNAME1 BNAME2 TEMP1 TEMP2
  (princ "\nUse standard selection methods to ")
  (setq BLKS (ssget (list (cons 0 "INSERT"))))
  (setq L (sslength BLKS))
  (setq L (- L 1))
  (setq I 0)
  (setq BLIST (list "BLOCK NAMES"))
  (setq BNUM (list "INSTANCES"))
  (while (<= I L)
     (setq ENAME (ssname BLKS I))
     (setq EDATA (entget ENAME))
     (setq BNAME (assoc 2 EDATA))
     (setq BNAME (cdr BNAME))
     (setq CHK (member BNAME BLIST))
     (if (eq CHK NIL)
        (setq BLIST (cons BNAME BLIST))
     ) ;_ end of if
     (setq I (+ I 1))
  ) ; end while
  (setq L1 (length BLIST))
  (setq L1 (- L1 2))
  (setq J 0)
  (setq N 0)
  (setq K 0)
  (while (<= K L1)
     (setq N 0)
     (setq BNAME1 (nth K BLIST))
     (setq J 0)
     (while (<= J L)
        (setq ENAME (ssname BLKS J))
        (setq EDATA (entget ENAME))
        (setq BNAME2 (assoc 2 EDATA))
        (setq BNAME2 (cdr BNAME2))
        (if (eq BNAME1 BNAME2)
           (setq N (+ N 1))
        ) ;_ end of if
        (setq J (+ J 1))
     ) ; end while j
     (setq BNUM (cons N BNUM))
     (setq K (+ K 1))
  ) ; end while k
;;; Formatting
  (setq K 0)
  (command "TEXTSCR")
  (princ "\nBlock Name     Instance")
  (while (<= K L1)
     (setq TEMP1 (nth K BLIST))
     (setq TEMP2 (nth (- L1 K) BNUM))
     (princ "\n")
     (princ TEMP1)
     (princ "\t\t\t")
     (princ TEMP2)
     (setq K (+ K 1))
  ) ; end while k
  (princ "\n\nPress the <F2> key to hide the text screen after viewing results... ")
) ;_ end of defun

;   "\n         Blocklst loaded...  Start command by typing   BLOCKLST "


Link to comment
Share on other sites

Thank You so much Sir @Lee mac and Sir ORgrown for providing me LISPs. I will study those lisp that you've shared to me. as what i've seen on Sir @Lee Mac's programs, my expression was like "ahhhH! how the???? how does he do this codes?" :? how do you really? I wanna know. haha. Sir @ORgrown there's still many things for me to learn in order to understand that lisp you showed, some are familiar codes but still, i gotta learn more. This will be my first step in creating a LISP. Thank you so much Masters! :excited:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...