
AutoLISP Pt 5 - AutoCount Part 2
How can you add Command Line options? How can you use Blocks instead of Text? We will also cover creating helper functions, and a whole lot more!
AUTOCADAUTOLISP

My post content

Greetings, fellow coding enthusiasts! We’re excited to bring you the latest installment in our AutoLISP Programming Series. In this deep dive, we eadd on to our AutoCount dynamic text placement routine. We took viewer questions about extra options, and implemented them into a new and improved routine!
Want to learn more about AutoCAD? Check out our AutoCAD class options!
Below you will find the code that was shown in the video.
To run it, copy the code below and paste it into a text file called AutoCount.lsp
To load it into AutoCAD, use the APPLOAD command,
Start the command by typing AUTOCOUNT
;
;
; AutoCount2.LISP
;
; Davin Atkins 2024 - youtube.com/@atccad
;
; This LISP routine, asks the user for a starting number
; then a count interval. Then for each click, a new text or
; block object is placed with the next number in line
;
;
;-------------------------------------------------------------
;-------------------------------------------------------------
(defun c:AUTOCOUNT ()
; If you do not have your block library path setup in the AutoCAD Options
; Uncomment the code and add your path (don't forget double backspaces)
; (setq BLOCKPATH "d:ATCSupportProgramminglisp")
; Initialize Variables at First Start
(if (= BLOCKNAME nil)(setq BLOCKNAME "autocounttag")) ; Set this to your preferred counting block name
(if (= COUNTTYPE nil)(setq COUNTTYPE "Text")) ; Default to text counting
(if (= STARTNUM nil)(setq STARTNUM 1))
(if (= COUNTBY nil)(setq COUNTBY 1))
(if (= TEXTHEIGHT nil)(setq TEXTHEIGHT 6)) ; Why did I pick 6? Because I had to pick something!
; Check to see if the Text Style has a height defined - This will return 0.0 if NOT defined
(setq STYLEHEIGHT (cdr (assoc 40 (tblsearch "style" (getvar "textstyle")))))
; Display Current Settings
(princ (strcat "nCurrent Settings: Count Mode = " COUNTTYPE
", Start at = " (rtos STARTNUM 2 0)
", Count by = " (rtos COUNTBY 2 0)
", Height = " (rtos TEXTHEIGHT 2 3)
))
(while
(progn
(initget "Block Text Start Interval Height")
(setq INSERTPOINT (getpoint "nClick the location of the next text element [Block/Text/Start/Interval/Height]"))
)
(cond
((= "Block" INSERTPOINT)
(progn
(setq COUNTTYPE "Block")
(setq TMPBLOCKNAME (getstring (strcat "nCurrent Block Name Is <" BLOCKNAME ">")))
(BLOCKFINDER BLOCKNAME)
(if (not (= TMPBLOCKNAME ""))
(progn
(BLOCKFINDER TMPBLOCKNAME)
(setq BLOCKNAME TMPBLOCKNAME)
)
)
)
)
((= "Text" INSERTPOINT)
(setq COUNTTYPE "Text")
)
((= "Start" INSERTPOINT) ; If Start is selected - Change STARTNUM
(setq TMPSTARTNUM (getint (strcat "nCounting from <" (rtos STARTNUM 2 0) ">: ")))
(if TMPSTARTNUM (setq STARTNUM TMPSTARTNUM))
)
((= "Interval" INSERTPOINT) ; If Interval is selected - Change COUNTBY
(setq TMPCOUNTBY (getint (strcat "nCounting interval <" (rtos COUNTBY 2 0) ">: ")))
(if TMPCOUNTBY (setq COUNTBY TMPCOUNTBY))
)
((= "Height" INSERTPOINT) ; If Height is selected - Change TEXTHEIGHT
(setq TMPTEXTHEIGHT (getreal (strcat "nText Height <" (rtos TEXTHEIGHT 2 3) ">: ")))
(if TMPTEXTHEIGHT (setq TEXTHEIGHT TMPTEXTHEIGHT))
)
(t
(if (= COUNTTYPE "Text") ; Place as text if COUNTTYPE is Text
(progn
(if (= STYLEHEIGHT 0.0)
(command "text" INSERTPOINT TEXTHEIGHT 0 STARTNUM)
(command "text" INSERTPOINT 0 STARTNUM)
)
)
)
(if (= COUNTTYPE "Block") ; Place as text if COUNTTYPE is Block
(progn
(setvar "attdia" 0)
(command "-insert" BLOCKNAME INSERTPOINT 1 0 STARTNUM)
(setvar "attdia" 1)
)
)
(setq STARTNUM (+ STARTNUM COUNTBY))
)
)
)
(princ)
)
(defun BLOCKFINDER (CHECKBLOCKNAME)
(if (not (tblsearch "BLOCK" CHECKBLOCKNAME))
(progn
(if (not (= BLOCKPATH nil)) ; If BLOCKPATH veriable set, include path in FULLNAME
(setq FULLNAME (strcat BLOCKPATH CHECKBLOCKNAME ".dwg"))
(setq FULLNAME (strcat CHECKBLOCKNAME ".dwg")) ; else
)
(if (findfile FULLNAME)
(progn
; Insert the block into the drawing at 0,0 and then erase it.
; This will load the block into the drawing
(setvar "attdia" 0)
(command "-insert" FULLNAME 0,0 1 0 1)
(setvar "attdia" 1)
(command "erase" L)
)
(progn
(alert (strcat "nCannot locate block.n" FULLNAME "nThis routine will be cancelled. Please insert the block and try again"))
(quit)
)
)
)
)
)
