Today I will be showing some basics of lisp. You might be surprised how easy Lisp is and how powerful you become with just a little information.
What is AutoLISP?
AutoLISP is a subset language of a high-level programming language called LISP. It allows users to write their own code to suit their own needs, and run it seamlessly inside of AutoCAD.
Use Note pad, Word pad, or some other editing software. Do not use Word. It formats incorrectly and your lisps won't run.
Use indentations to clearly convey the meaning of your code. Even if the code is only for you, chances are you will revisit it infrequently. Clearly written code is much easier to understand and modify.
Do not reinvent the wheel. Start with similar code when ever possible and modify to suit your current needs. You can find lisp all over the internet.
Stick to your standards. Do not code similar tasks differently. Management becomes more difficult when you can’t remember how it was done in each individual instance.
Look for opportunities to create modular code. Write it once, and be done with it. I have a friend that wrote a small error handler module that would reset any varibles before it will allow an escape from the lisp it is run in. He attaches it to everything.
Create a “Test LISP” location. Debug enhancements to your current LISP routines before going live.
Think it through. Consider what the final output is, decide the best way to reach it, then code it.
AutoLISP code basics
Defun - Every AutoLISP routine defines a function using the defun command.
Parentheses - The number of open parentheses must match the number of closed parentheses.
Remarks - Use semicolons to indentify remarks in your code.
Here is a sample format for a simple lisp
; Name of LISP
; Date: x/x/x
; Written by: Cad Manager
; Description of what the lisp does
(defun Function Name (global variable /local variable)
(Body of lisp)
Ways to load LISP
Use the CUI command to automatically load your lisp file by right clicking on the word LISP Files
Or just drag the lisp file from Windows Explorer into your AutoCAD drawing window. It will load on the fly, works great for debugging.
; Zooms out by a factor of .5x
(defun C:zoomout ()
(command “zoom” “.5x”)
In this example the word defun tells AutoCAD a function is about to be defined.
The C: is telling AutoCAD what ever follows invokes the function when typed on the command line. So in this case, if you type zoomout, the lisp is executed.
The word command tells lisp to execute command line items that follow in quotes.
That's it. What can you write just knowing that?
; resets the running osnaps to my favorites
(defun C:mysnaps ()
(command “’osmode” “183”)
Osmode is a system variable that defines what your running object snaps are set to. To find out what your osmode number is. Set your snaps and type osmode on the command line.
; Invokes the arc command with the End and Distance modifiers
(defun C: ArcEndDistance ()
(command “arc” pause “end” pause “d”)
I introduced the pause command here which pauses the lisp for user input.
; Changes the linetype of selected objects to centerline
(defun C: centerline ( / cl)
(princ “select objects to change to centerline linetype…\n”)
(setq cl (ssget))
(command “change” cl “” “prop” “LT” “center” “”)
This example introduces a variables and the ssget and setq functions. The ssget function is short for selection set get. The variable stores the selection set for future use. And setq sets the two equal. The princ command is also introduced, it will print text at the command line. The \n at the end is an enter to give the command line cursor a new line. Otherwise the cursor will sit at the end of the printed text.
; accepts number in degrees and returns equivalent radians
(defun D2R (a) This variable must be passed with the command D2R
(* pi (/ a 180.0))
Here is a little math in action.
; Change current space to model, zooms extents, saves, then closes.
(defun myclose ()
(command “ctab” “model” “”)
(command “zoom” “e”)
Ctab is a system variable for current tab.
Use the Help function to fill in the blanks where you need help and modify these as you need to. There is a ton of Lisp on the internet for the taking. If you are lucky, you will never need to draw anything from scratch ever.