Basic Lisp Macros for Architectural Desktop

Contents:
Why Macros? ---- Macro Writing in LISP ---- LISP Loading

1Why Macros?
Macros for Speed and Pleasure

Now that you have mastered the myriad of Architectural Desktop dialogue boxes haven't you had enough?  I mean, one of the best features of AutoCAD that has kept me going for all these years is speed speed and more speed and the fastest thing around is still keyboard entry.  It's the fastest because of many factors: the keyboard usually beats other external devices, words and numbers are more specific than clicks and picks and pointing devices usually cause the user to move the cursor away from the area of focus.  This is not to say that I love the keyboard but that I like to use what I have to the best of its capabilities.

lisp_macro_notepad.gif (7252 bytes)

The problem with Architectural Desktop's commands is that they are really long and no one added ADT Aliases to the ACAD.pgp file so we are left to our own creative solutions.  You can modify the ACAD.pgp file and do a lot for yourself but you can also write a simple set of LISP Macros and take advantage of the ability to encode values and Enters to make really fast routines.

Below, we will look at a few LISP routines that we at ARCHIdigm use and hope that you will soon create your own list and improve your productivity so much that you'll get a raise this year.

2Macro Writing in LISP
Write the Macros - Explanation

There are two parts to the concept of writing custom LISP Macros that should be stated before we look at writing and using them.   The first part that needs to be understood is that these routines, though simple, need to be written with extreme attention to syntax ( particularly Parentheses ) and they need to be written with a plain ASCII text editor like NotePad.  The file extension must be ".lsp" and not ".txt" or ".doc".  When you are ready to save from Notepad, it will automatically want to create a .txt file so you have to type the whole file name out; something like "custom_macros.lsp".

The second part that needs to be understood is that this file must be loaded into AutoCAD or Architectural Desktop before it works so we will take a look at one of the simplest ways of loading a custom LISP routine in Section 3.

Now all we need to do is look at a sample LISP Macro and break every part of it down so you can write your own.

Illustrated to the right is a typical routine that works great for Architectural Desktop Walls.   The first line starts with a left parenthesis "(" followed by the LISP statement "defun" which declares that we want to define a function.  The function is defined as a command "c:" invoked by typing the "w5" on the command line.  This whole statement must be placed within parentheses as shown.  The odd double "()" at the end are blank because this function will not be using any variables ( in other words no calculations or other fancy stuff ).  Notice that this leaves us with an imbalance in parentheses so we will need to add one later at the end of this bit of code. ->

Write the Macros - Example

(defun c:w5 ()
   (command "-walladd" "st" "Standard" "wi" "5" "h" "96" )
)

Here are some other possible candidates for your personal set of macros.

;;this adds a 3'-0"x6'x8" Standard Door - notice the pause for wall selection
(defun c:d3 ()
(command "-dooradd" pause "st" "Standard" "wi" "36" "he" "80" )
)
;;this unloads all Xrefs
(defun c:xru ()
(command "-Xref" "u" "*" )
)
;;this reloads all Xrefs
(defun c:xrr ()
(command "-Xref" "r" "*" )
)
;;this sets an instant perspective
(defun c:per ()
(command "dview" "all" "" "d" "600" "z" "35" "" )
)

Since the list of options is extensive, we can take advantage of as many of them as we like and program them all in.  Two other important options to specify would be the WIdth "wi" with a specified value of "5" and a Height "h" with a specified value of "96".  These values are in inches but you can, of course, use millimeters too. 

On the second line of this routine, we state exactly what we want Architectural Desktop to do when "w5" or "W5" is typed in on the command line.  This represents the actual "function" we are defining.   Since this will be a complete statement it will be enclosed by parentheses on both sides and thus we start with "(".  To explain to the command line interpreter that we want an AutoCAD or Architectural Desktop "command", we use the LISP statement: "command".  At this point we can use quotation marks to write out every statement that we would write if we were in Architectural Desktop.

To prevent a dialogue from popping up and blowing out the macro, we have to use a dash "-" before any command that usually invokes a dialogue box.  This is why the statement "-walladd" is not written as "walladd".  When you write your own macros, always check the whole process on the command line first and make sure you have the full sequence working without dialogue boxes.

If we read the options on the command line in Architectural Desktop after typing "-addwall" this is what is stated:

Start point or [STyle/ Group/ WIdth/ Height/ OFfset/ Justify/ Match/ Arc]:

We can now use any of these options, one a time, and specify exactly what we want this function to do.  We can specify a Wall STyle for example, by writing "st" and then we can specify the Wall Style Name "standard" so long as it will exist in the file where our Macro will be used.  If you typically use a Stud Wall Style, then you can specify its Style Name here.

Once we have added all of the options and values we want, then we end the statement with a right parenthesis ")".

For the last line we have to match all of the parentheses in the code; for every left parenthesis there must be an equal number of right parentheses: ")".  The very first bit of code was a left parenthesis and now we close the whole mini-program up with a final right parenthesis.

Save the Macro as .lsp File

lisp_macro_save.gif (14284 bytes)

That's it, a fully functioning LISP Macro in no time at all.  Now we need to save it and test it in Architectural Desktop.

3LISP Loading
Load and Use the Macros

Once you have created your first or one-hundredth LISP Macro, you will probably want to test it.  In order to do this you can use one of two approaches that I regularly use.  The first approach that I use when testing routines is a drag-n-drop method where I use Explorer and simply drag the .lsp file right into AutoCAD or Architectural Desktop.  This should trigger a load and you will see the results on the command line.

The second approach I use to test routines is also an approach you can use to make your LISP file load automatically for use every day.   Illustrated to the right is the Load/Unload Applications dialogue box that you can access from the Tools pull-down menu or by typing "Appload" on the command line.

On the Load/Unload Applications dialogue box, you can use the Look in drop-down list to find your custom LISP file and then use the Load button to use the Macros immediately and for the duration of the current AutoCAD or Architectural Desktop session.  To make the loading of your custom LISP file more permanent, you can use the Contents... button to access the Startup Suite dialogue box, illustrated lower right, and Add... it to the regular startup process.  This is keyed to the current User Profile so if you swap Profiles a lot, keep this in mind.

lisp_macro_appload.gif (24451 bytes)

Copyright 2002 ARCHIdigm. All rights reserved.