Wednesday, December 2, 2015

Really really simple AutoLISP

For those who can't wait for more of this blog to play out, I will show you my really, really simple AutoLISP macro system. Essentially these macros imitate typing commands on the command line. For example, if I wanted to ZOOM ALL, I would type at the command prompt:

> Command : zoom
Zoom:  In/oUt/All/.../Scale (nX/nXP)>: all

This can be captured in the AutoLISP command function as (command "zoom" "all"). I have shortened this to (command "z" "a") in my macros. If you are using AutoCAD internationally, you should include an underscore in front of each keyword to get the English version as (command "_zoom" "_all").

This doesn't look shorter. Typing (command "_zoom" "_all") is much slower than typing the original commands themselves.

The secret is to turn this form into a callable command. To do this place the form inside a defun form and prefix your new function name with a c:. I have added the form (prin1) to make the function exit silently with no return value. The ;; zoom all is a comment only.

Here is my macro as I have it:

;; zoom all
(defun c:za ( / )
   (command "_zoom" "_a")
   (prin1))

Now it is easy to copy this and add more macros:

;; zoom window
(defun c:zw ( / )
   (command "_zoom" "_w")
   (prin1))
   
Save these in a text file ending in .lsp on your drawing search path. For example "macros.lsp". Now to load these into your drawing session, just type (open "macros.lsp") or add it to your APPLOAD startup briefcase.

Now to use your new command just type za

> Command : za

Remember to select command names you can remember easily. Enjoy!

No comments:

Post a Comment