Tuesday, May 16, 2006

Basic Lisp

There comes a time when you have done about all you can with AutoCAD menus and macros and you want to do more. I think we all know that means Lisp. Now just because you are great at design and understand computers, it does not necessarily mean you are a programmer.

That's OK.

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.

Programming Notes
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
; Revised:
; 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.

Code Examples
Example One
; Zoomout
; 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?

Example Two
; Mysnaps
; 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.


Example Three
; ArcEndDistance
; 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.


Example Four
; Centerline
; 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.

Example Five
; D2R
; 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.

Example Six
; MyClose
; Change current space to model, zooms extents, saves, then closes.
;
(defun myclose ()
(command “ctab” “model” “”)
(command “zoom” “e”)
(command “qsave”)
(command “close”)
)

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.

3 comments:

Anonymous said...

No mention of VLIDE?

Anonymous said...

A couple of other useful LISP programs that everyone should have!

Most CAD users know how to modify their PGP file to customize their command aliases, but sometimes you think of a good key sequence that won't fit into the PGP. For instance, you can save one key stroke by defining a new lisp program ZE for "zoom" "extents".

(defun c:ze ()(command "zoom""e")(princ))

It's simple, and everyone knows saving even one keystroke speeds up your drafting. Want a few others?

(defun C:zd ()(COMMAND "ZOOM" "D")(princ))

(defun c:zw ()(command "zoom" "w")(princ))

(defun c:zp()(command "zoom" "p")(princ))

This type of file is simple to make and works with any nested command that you may want to use.

(defun c:tri ()(command ".POLYGON" "3" pause "I" pause)(princ))

(defun c:squ ()(command ".POLYGON" "4" pause "I" pause)(princ))

Play around with it and see what you can come up with.

Unknown said...

This is very helpful thanks, any chance you have an example of LISP's to edit blocks? I need to replace the year in about a 900 drawings.