Customizing AutoCAD with Autolisp:
If you are still using AutoCAD "out-of-the-box" then you are only working at half speed and half efficiency. Let me share with you the effects of twenty five years of customizing AutoCAD with Autolisp.
This article has been written for beginners who want to plan ahead and for intermediate users who want to do something constructive now. It is a personal point of view about improving the User Interface of AutoCAD arising from a very early start with AutoCAD and twenty-five years continuous use. Use what you can from it but don’t isolate yourself by ignoring what the other less efficient 90% of drafters are doing. Put yourself in the top 10% but keep up your experience with dinosaur menus.
Customizing AutoCAD is a very extensive subject; mainly because there are so many ways you can do it and so many reasons why one would want do it. Let me confess, however, that in spite of over twenty five years of customizing AutoCAD using Autolisp, I am neither an expert on Autolisp nor on customization but I would like to pass on some very useful stuff that I have done with AutoCAD – So here goes!.
There are two basic reasons why one would want to customize AutoCAD; the first being to improve the command interface and the second to create new or smarter commands. I have done both, very extensively, using Autolisp but would not venture to suggest that I could teach you to become an Autolisp programmer from a programmer’s perspective.
What I can show you is how you can do some grass-roots thinking and simple programming to do some very smart things with AutoCAD that will revolutionize the way you work. If however you want the easy route now (but the harder route later) stick with the way AutoCAD works out-of-the-box because this is the way almost all tutorials are presented and, disappointingly, the way that most operators work. If though; you want to get ahead of the field read on.
Much of what you will read about customization is to do with menus; pull downs, pop-outs, right-click shortcuts, ribbons, icons, you name it, it’s there. D’y’know what? You can spend hours trying to understand it all and change it (the screen menus I mean) but it’s very difficult to improve on what AutoCAD provides out-of-the-box. I am asking you to entirely re-think the way you think about and work with AutoCAD. The real bonuses that AutoCAD (and now several other programs such as Intellicad and BricsCAD) provide are:
- A Command: line that allows you to communicate with ten fingers instead of one mouse,
- Facilities that allow you to substitute short aliases (keyboard-shortcuts) instead of long command names and
- AUTOLISP.
You can change or add to AutoCAD’s command aliases by editing a file called ACAD.PGP but a more flexible and powerful way is to use AUTOLISP to do the same thing and better. If you are happy with the concept of issuing command aliases AT THE KEYBOARD, instead of clicking about in menus with a mouse, read on, but first let me say; don’t forget the menus, they are a great way to learn about the commands. Use them in parallel with the Command: line, just don’t use them for your regular work.
What comes next is a very personal way of working with AutoCAD and it came about when I started using AutoCAD at Release 1 when the Command: line was the only real option. As, over time, the different menus appeared I used them for familiarization with new commands but always found the Command: line faster and more accessible. It is always there in the same place and ALL of the commands are instantly available, all the time and always work. Faster, better and more complete; what more can you ask? Well there is another significant bonus; with all but the main strip menu turned-off I have access to the whole screen for drawing.
Enough justification, let’s get going!
Have a look at this bit of Autolisp:
(defun C:M () (command “.-MOVE”))
What this is saying is: Define the function (or alias) “M” to mean “MOVE” when entered at the Command: prompt.
- “defun” is an Autolisp function (instruction) to define a new function (or command).
- “C” means that the new function will be recognized when entered at the Command: prompt.
- “command” is another Autolisp function that says “issue the command” that follows,
- and of course “MOVE” is the command we want to issue.
- Don’t worry (for the moment) about the dot and dash. The dot means use the generic AutoCAD command in case it has been re-defined and the dash means use the command-line (text version) rather than the dialogue box version if one exists.
- Now; what about all the brackets? You will notice that (command “.-MOVE”) is a complete instruction within itself. Each Autolisp function and its parameters must be separated from others by an enclosing set of brackets.
- Note that “defun” also has a starting bracket (defun C:M , , , and two closing brackets . . . “.-MOVE”)) . The first one belongs to the defun function and the second to (command “MOVE”).
- The empty pair of brackets in the middle () is for parameters, if any, that the function needs for execution. I will come back to that later.
- What about all the quote marks? Text strings require the quotes to identify the beginning and end of the text and also to indicate that it is text. In this case “MOVE” is the text that is transferred to the AutoCAD Command line.
What is the purpose of this?
If I typed the full instruction (defun C: M () (command “.-MOVE”)) at the Command: prompt and pressed <enter> AutoCAD would recognize that it is an Autolisp definition meaning when M is entered at the Command: prompt execute the command MOVE.
I could thereafter for the duration of that session enter M to start the MOVE command. Not much good by itself though, so I would create a whole lot more defuns for the common commands that I use, list them all in a file called say MYFUNS.LSP and then at the Command prompt enter (load “myfuns”) (“load” is another Autolisp function) and guess what; Autolisp will load a whole bunch of aliases into memory. I could go one better and automatically load them at start-up of the AutoCAD program itself.
Now I said at the outset that I was not a qualified teacher of Autolisp so I will refer you to sites on my LINKS page and the AutoCAD help file under Customization and get on with the really useful stuff of creating a superior USER INTERFACE.
I will suggest an alternative set of aliases to those provided by Autodesk. (I’m happy to give you a predefined file and tell you how to load it automatically). Alternatively you may wish to stick to your own modifications to generic AutoCAD aliases either by creating your own Autolisp file or by editing AutoCAD’s PGP file. But whatever you do; put some real effort into aliases and the Command: line. Typing MOVE and pressing <enter> is quicker than mouse clicking. Typing M <enter> is a whole lot quicker. Later on I will show you how you can dispense with pressing <enter> at the end of every input.
Don’t sit on your Aliases:
AutoCAD provides an extensive list of about 450 command aliases. It is not too difficult to guess that there will be difficulties in devising meaningful short aliases for so many complicated command names. This gets even worse when you add a few hundred of your own. I could go into a long dissertation about the merits of the AutoCAD aliases relative to the ones that I have created but let’s just have a look at an extract from MYCADKEYS
MYCADKEYS – superior to AutoCAD Aliases:
- Is easier to learn and use than AutoCAD Aliases,
- Uses less keystrokes,
- Prioritizes the use of single character aliases for the most common commands
- Makes use of the entire keyboard and mouse keys,
- Logically groups commands by function,
- Avoids alias naming conflicts,
- Provides shortcuts for virtually all AutoCAD commands and custom MYCADKEYS commands,
- Flexible enough to provide for MYCADKEYS' 1025+ command combinations.
MYCADKEYS consist of:
- Command-Aliases,
- Accelerator-keys,
- Instant-Keys and
- Customized mouse clicks.
Naming of MYCADKEYS Aliases:
- One of MYCADKEYS' main techniques is to group related commands into sets e.g. PA Place Arc, PL Place Line, PP Place Pline etc. Related commands such as Extend, Trim, chamFer are grouped under the concept of Joining e.g. JE JT JF A frequently used group is Q Query, thus: QA Query Area, QB Block-name, QC Colour, QD Distance, QG Group, QK thicKness, QL Length, QO Object, QT Text, QL Layer.
- “File” commands are too numerous to mention here but examples such as: FA saveAs, FG purGe, FP Print, FU Utilities, FH Hyperlink, FML Menuload are a good illustration of the general concept. Note that one can also use the AutoCAD Alt+chr method of accessing menus such as SAVEAS Alt+FA).
- MYCADKEYS’ other main technique is to use extended keyboard characters and mnemonic and visual relationships to take full advantage of the keyboard, e.g. @ @ttedit, # cal, | |inetype, $ Style, % scale, & group,
- The number of characters in an Alias is related to its frequency of use. Common editing commands such as Copy and Move all have single character names. All remaining common commands require only two and hardly any but the most complex of command concepts exceeds three. A complete listing of MYCADKEYS commands is available in Commands - complete list.
A fuller explanation is available in MYCADKEYS when you are ready for it but I won’t go into more detail here – I’m just trying to demonstrate two principles:
- The command line and aliases is the way to go.
- Autolisp customization is a must for professional operators.
So if you would like to experiment further keep reading.
Let’s have another look at “defun” and “command” and add a little extra.
(defun C:L0 () (command “.-LAYER” “T” “0” “ON” “0” “S” “0” “”))
This will start the LAYER command, thaw, turn ON and set layer zero to be the current layer.
“L0” is an alias for “Set Layer 0 (zero) to be the current layer but make sure that is Thawed and turned ON first.
Notice that Autolisp is used to execute the command EXACTLY as you would enter it at the AutoCAD keyboard. (Try it NOW). Read the bit between the first and last quote marks and notice that each space is a SINGLE space that in Autolisp is equivalent to pressing <enter> after each text input and the last pair of quotes (a null entry) is equivalent to pressing the final <enter> to terminate the LAYER command. I could have used another space but this way it is clearer what is intended.
What have we got? A very convenient command alias to set Layer zero as the current Layer i.e. Command: L0 <enter>
Now it gets really interesting. I have a predefined custom function called “#getyn”(that I will show you later) that will find the name of a layer of an object with a left mouse pick and a nested layer name from a BLOCK by using a right-mouse-pick. (Don't worry about the "yn" in "getyn" - it should have been "ln" but that's historical and won't affect the user).
I can now define a function “L-“ to turn a Layer OFF by picking it.
(defun C:L-(/ln)(#getyn)(command “.-LAYER” “OFF” yn “”))
Some stuff has appeared in the previously empty pair of brackets. The “yn” after the slash declares the variable set by (#getyn) to be temporary i.e. it will be discarded from memory at the completion of the command.
Thus with the assistance of #getyn I can have a set of Layer commands such as LF, L-, LS that freeze, turn OFF, set current a Layer just by picking on it. With the assistance of an extra custom function #ssget to collect all the objects with the relevant Layer name I can define an alias EL to erase everything “on” the layer yn just by picking on a relevant object.
Thus: (defun C:EL(/yn ss)(#getyn)(#ssget yn)(command “.-ERASE” ss “”))
Is that worth a bit of midnight oil to achieve?
I’m not suggesting that you are ready to design the functions #getyn and #ssget but clearly you can start to improve your user interface and learn a little Autolisp along the way. Looks confusing? Well yes it is a little at first but you quickly get used to it and LISP (List Processing language) is one of the easiest to learn. Others might call it Lost in Stupid Parentheses but that's the beauty of it; you can see clearly what you are doing. Try some Autolisp tutorials soon.