AutoCad

Site with dynamic tree menu (recommended).

Autocad mappings and menus made with AutoHotKey scripts

I've worked in Autocad for a few years and I accumulated a number of scripts and mappings that I did via AutoHotKey automation program that could be useful to others, hence this page.

It may not be true at all times, but at often you want to use AutoCad really quickly, and I mean really quickly. It may be a deadline or it may be that you're just playing with an idea and you don't want to spend a whole day on it, but just do a quick mockup. In this scenario, it's important to be able to draw, in a way of speaking, "directly", without thinking about how; pouring the mental picture to the screen as straightforwardly as possible. If you look at AutoCad commands you'll see that some of them are used so often that they should be cut down to, ideally, one keystroke. AutoCad makes this possible, but with the catch that you still need to hit the space bar to signify the end of the command entry. This may not seem to be a big deal, but if you run a command once, let's say, in two seconds, this could mean a difference of hitting 30 keys vs. 60 keys in 2 minutes, or, alternatively, of running 30 or 60 commands in a minute.

AutoHotKey makes it possible to assign a single key to a shortcut. To make things quicker, I create a keyboard shortcut in AutoCad for each command I wish to use, for instance, Ctrl-Alt-F2 for 'erase', and then have AutoHotKey issue this shortcut when a certain key is pressed, i.e. 'e'. Obviously, this would lead to a problem when you need to type this letter, for instance in a text block or in an autocad command. There are a few ways to get around this: in AutoHotKey, you can set up a shortcut which will enable/disable a given script, you can type an uppercase letter which will usually be taken to mean the same thing by AutoCad (e.g. typing EXTRUDE is the same as extrude), and finally, you can set AutoCad to use an external program for text blocks.

AutoHotKey shortcuts are easy to define; here is an example:

q:: Send ^+{f11} ; fillet

Text after the semicolon is just a comment; it's not necessary, but it can be very useful if you're using one-key autocad shortcuts like I did.

Here's another example that issues several commands to autocad:

x:: SetKeyDelay, 20 Send close{Enter} ; close return

First line here will set a delay between keypresses that are sent to AutoCad, because AutoCad does not always keep up, and AutoHotKey has no way of knowing if AutoCad is ready for the next key or not. Then it sends command "close", then it sends an Enter keypress (it has to be enclosed in figure brackets, otherwise it would send 'Enter' as plain text); the last line ends the definition of the shortcut. In the previous example we did not need that because was all defined on one line, but as soon as you go to 2 lines and over, you need that 'return' marker to indicate that you're done.

That all seems easy enough, but the issue is that there's not enough keys on the left side of the keyboard to define all the commands you might need. If you put them on the right side, you'd have to constantly move your hand between mouse and the keyboard, or move all over the keyboard with your left hand, having to look down and foregoing all the advantages of touch typing. AutoHotKey, fortunately, permits us to avoid this hurdle by letting you set up some keys as 'modifiers'. Once the modifier key is pressed, you can hit one or two other keys to complete the command. In addition, you can hit modifier key two times and then hit another key to complete a different command. I chose keys 'g' and 'w' to be modifier keys. Here is the code that will do that:

g:: ; when 'g' is pressed If (wprefix != true) ; if 'w' was pressed right before 'g', this is a 'wg' command that will be handled separately { If (gprefix = true) ; we already pressed 'g' once before this time { ggprefix := true ; variable called ggprefix is set gprefix := false ; variable gprefix is unset } Else gprefix := true ; var gprefix is set.. } return

The idea here is that we will have a number of definitions later, one for each key, that will do different things depending on whether 'g' was hit previously or not. This is a bit of a roundabout way to do this, I'd prefer if it was possible to define 'gf', then 'gb', etc, but that's not possible in AutoHotKey right now. Instead, you have to set a variable when 'g' is hit. When 'b' is hit, the code will check for variables that are set. If no vars are set, it will do an action corresponding to 'b'; if 'gprefix' is set, it will do an action corresponding ot 'gb', and so forth. Note that variable is only set immediately after 'g' is hit. If you hit 'g' and then hit 'b', the command is run and then 'gprefix' is unset.

Here is the code you would use to set up a shortcuts that use a few prefixes:

q:: If (gprefix = true) { Send ^+{f11} ; fillet gprefix := false } else if (wwprefix = true) { SetKeyDelay, 20 Send close{Enter} ; close Send n wwprefix := false } else if (wprefix = true) { Send ^+e ; offset wprefix := false } Else Send ^+z ; qleader Return

In order to get this to work, you need to install AutoHotKey (around 2 meg download). Here's the AutoHotKey download page.

Here's my AutoCad shortcuts script. Note that the list of shortcuts listed in comments at the top is a little out of date. NOTE: one shortcut in the file can be dangerous. 'wwq' will close current file without saving. Make sure that's what you really mean to do when using it. If you're uncomfortable having such a destructive shortcut, please disable it. I'm not responsible for any and all damage... By the way, I wish AutoCad wouldn't bug you to save the drawing if you only opened it and zoomed around. That's just annoying when you're looking at a dozen of drawings and then closing them. What if you're quickly looking at a hundred or so?

Sometimes I like to work at a slower, more relaxed pace and using the mouse is more conductive to that. However, when you use AutoCad toolbars, you have to drag your mouse all over the place, and that quickly gets tiring, especially with such commands as copy, move, etc. Wouldn't you wish to have them always close to your cursor, wherever it is? Well, now you can! This neat AutoHotKey script will create a menu of buttons right under your cursor. You can change and add new buttons, of course, although you will need to edit the code directly in order to do that. I tried a few different ways of doing it before I finally settled on having right mouse button bring up the menu and pressing buttons of the menu with the right button, as well. After a bit of practice it's very natural to hit right button, move the cursor a bit and hit it again over the command you need. However, due to a little AutoHotKey idiosyncracy, and my lack of experience, I wasn't able to make it work properly when you press a button with a left button instead. Not that anything terrible will happen - but next time you hit right key to bring up the menu, it won't work. You'll just have to hit it again to bring up the menu. I found that I quickly adjusted to having to use right button at all times & this wasn't an issue for me.

You might think that this will disable a very useful command - right clicking to end a command which is a feature that can be set in AutoCad options screen. However, you can still have that functionality by hitting right button twice in succession, because the 'Enter' button is always located right under the cursor when the menu appears. 'Enter' will end current command, and hitting right button twice is almost as quick as hitting it once.

Here is a screenshot of my current menu in Windows 2000 (in WinXP it looks much nicer because of rounded buttons):

AutoCad menu screenshot

Here's an example definition of a command in the menu script:

Gui, 3: Add, Button, x6 y0 w32 h20 +BackgroundTrans gacadAction, Mv

This line will create a gui # 3 (because I have gui's under different numbers for other programs, managed by a single master script); it will create a button at a given spot with given width and height, in pixels, and set the background to be transparent (although that doesn't seem to work right now in AHK), then it runs a function called 'acadAction', and then the label to appear on the button is given. I don't remember what that 'g' at the start of function name stands for, but it has to be there.

AcadInit: Mv = ^+h ... return

At the end of file we define an AcadInit code block, where one of the lines will be our move command.

Here you will find acad menu script and master menus script. Strictly speaking, it could all be in one file, but, since I have menus for several programs, I prefer to have a master script that loads all of them and appears in the taskbar as one icon. You only need to run the master script file, it will load the other file by itself.

Finally, you may do a menu bar for a different program. All you have to do is copy the file, change shortcuts, change the gui number for each button and the one other place where it's called up, and add it to the master file.

NOTE: in order to use most of these shortcuts and menu buttons, you will need to set up shorcuts in AutoCad to correspond with what I have them set to in the scripts, e.g. ctrl-alt-e for offset, etc. In AHK, control is '^', alt is '!' and shift is '+'. To set shortcuts in Autocad, you need to go to Tools | Customize | Interface and then to keyboard shortcuts section, but this could be a little different in older versions of AutoCad (or in newer versions.. I have AutoCad 2006).


Prev   Main