Have you ever had to do a repetitive task in AutoCAD? Things like changing your snap settings to your preferred or adding layers to a drawing. If you answered yes then LISP could make a huge difference to your day-to-day CADDing. Now assuming that your familiar with AutoCAD you’ve probably at least heard of LISP, but learning it can sometimes be daunting. You open up the file and there all of this “stuff” in it; then you promptly close it. That is usually the first experience that everyone has with LISP so don’t feel alone, while you can make some really complicated routines with it you can also make some really nice and short time savers without learning everything there is to know about it. Now I can just imagine some of you going “ha, I have a CAD Manager that’s their job” but let’s face it, if you’re lucky enough to have one; a lot of places don’t, then they probably have a ton of other responsibilities their juggling around and might just not have the time to write something just for you.
Now for the LISP 101 you have been waiting for. First, all you need is Notepad. There is no compiling needed to write LISP. There are editors out there and even in AutoCAD but Notepad is a tried and true favorite of mine.
Secondly remember LISP is not magic, it cannot read your mind. If you don’t tell it to do something, it won’t do it even if it seems very obvious. Third do yourself a favor and kind of write the steps that you do when making a new layer or a new setting, this will make it easier to write the LISP. Finally and this is the most important save you work before test running any new LISP you make, sometimes even the most well thought out LISP will have a unforseen hickup in the testing stage, and you’d hate to lose your current drawing.
Note: AutoCAD LT does not support LISP, there are add-on’s that can allow it to but I haven’t tried them. Also there are other CAD software programs that support LISP like progeCAD and other intelliCAD based CAD products.
Now the fun stuff: the code and an explanation of what it actually does. The example is a super sort LISP that adds the command “ET” to the command line and that command sends an instruction to AutoCAD to invoke the “ddedit” command.
(defun C:ET (/)
First, notice the () around everything. If you open one then you need to close it as well. Now the “defun C:ET” tells AutoCAD that you want to define a new function (or overwrite one that already exists) and the Command will be “ET”. The “(/)” next to it is a place to put variables but were not going to get into that right now, but it’s good to get in the habit of it when you start. After that the (command “._ddedit”), this is saying that you want to use a command just like if you were typing in the command bar. We’re using “ddedit” here and the “._” before it makes sure that no other LISP has changed it on you. Lastly there is the ending “)” to close the LISP, now save you file with the .lsp extension and go into AutoCAD. In the command line type “APPLOAD”, this will allow you to load the LISP, browse to your file and load it. You can also add it to the startup suite if you want it loaded with every drawing. Now for this LISP make sure you have text in the drawing and then type “ET” into the command line and hit enter.
Congratulations you have just written and run your first LISP!
That wasn’t too hard, right? Let’s look into a little bit more complicated parts of LISP without getting off the 101 aspect of this.
SETQ: This allows you to set variables. For those who don’t know, variables let you store numbers or text for later use.
GETVAR & SETVAR: These two allow you to get and set system varibles, good stuff like your snap settings, dimscale, etc. Here is a good tip in your AutoCAD type "setvar" and enter the ? and * to list all your setting values. The help file also is a great source for this information.
GETPOINT: lets you have the user pick a point on the screen.
GETCORNER: This is a neat command, after using getpoint this will let you have a displayed rectangle that the user can see while selecting an additional point for a rectangle or selection set.
PRINC displays info on to the command line. Example: (princ "\nHello World")notice the "\n” it tells AutoCAD that you want a new line on the command line.
; Means ignore after symbol to LISP, it’s great for blocking out sections of code during debugging and documenting your code. On that note, do yourself a favor and document your code, it will help you figure out what you were trying to do later when you revisit it in a year or two.
So using all that we have learned so far we can write this:
(defun C:NREC (/ OLD_PLINE OLD_LAYER OLD_SNAP PT1 PT2)
;Get the settings of the drawing before you change them
(setq OLD_SNAP (getvar "OSMODE"))
(setq OLD_LAYER (getvar "clayer"))
;Now change your Osnap's, 0 on OSMODE turns the osnaps off
(setvar "OSMODE" 0)
;Here's a neat trick even if you have the layer have LISP create it again
;this ensures that you have it
(command "._-layer" "n" "Rectangle" "S" "Rectangle" "c" "31" "" "")
;Make the current color ByLayer
(setvar "cecolor" "BYLAYER")
;Get the two corners of the rectangle
(setq PT1 (getpoint "\nFirst corner point:"))
(setq PT2 (getcorner PT1 "\nOther corner point:"))
;Draw the rectangle
(command "rectangle" PT1 PT2)
;Now let's put the settings back to where we found them
(setvar "OSMODE" OLD_SNAP)
(setvar "clayer" OLD_LAYER)
So now you know the ropes of LISP. There is a bunch of info out there from sites like Afralisp, The Swamp and CADTutor just to name a few places where you can find more information on LISP as well as more advanced tutorials.
- DS -
~ Dan has been drafting everything from house plans to water mains with CADD for over ten years. Currently he drafting indoors, because it's hot outside. If you would like to reach Dan, please email him. ~