Autocad and Autohotkey – How to speed up your workflow

Many of us still use Autocad for editing old drawings, charted drawings and layouts. The Autocad visual interface can be time consuming to click through the menus even for something simple like changing an object to a different layer. Repetitive actions like blocking every part in a new layout or constantly switching between layers 0 and DIM while editing a drawing make this pain evident.

Turns out you can use a free little scripting program called Autohotkey to make your life a lot easier. It has a simple and well documented syntax. You can fairly easily learn to automate just about any part of Autocad (or any Microsoft Application among others). I have taken the time to learn some of the more advanced features (using COM objects to automate programs) that you should consider if you would benefit from using. The scripts I have written below can simply be copy pasted into a text file and saved with a .ahk extension in order to be used. Once you install Autohotkey you can simply double click on your .ahk text file to run it. I have put several of these scripts in my Startup folder so I always have them running. You don’t need to learn any Autohotkey language in order to take advantage of what I have already written as the parts that you probably need to change can use the exact same commands I have used just with different words for what you need.

Here is an example of using Autohotkey to help generate Stub Acme thread notes.

Here is a great example of setting a shortcut to change the current selections layers to the DIM layer. With shortcuts like this you can always work on layer 0 and simply hit the shortcut to send whatever new objects you make to their respective layer easily.

^+d::
;send objects to dim layer
acad_handle := ComObjActive(“AutoCAD.Application.18”) ;get a com object handle to an existing Autocad Session
acad_handle.ActiveDocument.sendcommand(“amlaymove ” “dim “)
return

^ stands for control key
+ stands for shift key
:: means the start of a hotkey code block.
; designates a line as a comment.
acad_handle is just the variable name I chose to control Autocad with

ActiveDocument.sendcommand lets you programmatically call Autocad commands just like you would type in normally in the program. Note the space at the end of each command and the space in between each command (just like you would type in Autocad).

Paste the line below at the top of your text file in order to make your hotkeys only active when an Autocad window is active.
#ifwinactive, ahk_class AfxMDIFrame90u

Since I use Autocad Mechanical (like most of us here) I use the amlaymove command and then “dim” to send the currently selected objects to the dim layer. Instead of having to remember and type amlaymove dim each time I just press ctrl+shift+d. This only uses one hand which frees me up to always have my one hand on my mouse.

That was a simple example and shows how anyone can set any Autocad command or series of commands they know how to type to a single shortcut. Since Authotkey is a program independent language (unlike Lisp for Autocad) you can also bring data into or out of Autocad easily from other programs like Excel! If you have a charted drawing where you have a bunch of calculated values in Excel that you want to write to a column of Autocad text objects you can use the hotkey below.

# stands for the Windows key so you activate this hotkey by pressing win+e.
It then asks you to select a column of values in Excel to write to Autocad
It assumes that you have a group of objects selected that includes each text object (in a vertical order ie. Column) that you want to overwrite with the selected column of values in excel. I have used a more complicated way to get the Excel application object since I want to make sure that it was the last active window. Using scripts like this saves any chance of human error during typing or copy pasting and is of course way faster than typing each value in by hand (pretty much instant).

#e::
msgbox Select a column of values to write to acad.
winactivate ahk_class XLMAIN
ControlGet, hwnd, hwnd, , Excel71, ahk_class XLMAIN
window := Acc_ObjectFromWindow(hwnd, -16)
x_win := window.application
sendinput {esc}
x_win.CutCopyMode := False

loop % x_win.selection.rows.count {
value%a_index% := x_win.selection.cells(a_index,1).value
StringReplace, value%a_index%, value%a_index%, `n,, All
StringReplace, value%a_index%, value%a_index%, “,, All
StringReplace, value%a_index%, value%a_index%, `r,, All
}

acad_handle := ComObjActive(“AutoCAD.Application.18″) ;get a com object handle to an existing Autocad Session
active_selection := acad_handle.ActiveDocument.PickfirstSelectionSet
object_list := Object()

for object in active_selection
{
if !(object.objectname=”AcDbText”)
continue
;build list of objects with the key as their vertical location
simple_array := object.InsertionPoint
object_list[simple_array[1]] := object
}

;make new simple array now that it is sorted by key
simple_list := []
for each, object in object_list {
simple_list.insert(object)
}

;set text
max := simple_list.MaxIndex()
index := 0
loop % max {
index := max+1-a_index
simple_list[index].TextString := value%a_index%
}

acad_handle=
return

Those two examples should get you started! If you are brave enough to try and look up the Autocad object model you may find it on your computer here C:\Program Files\Common Files\Autodesk Shared\acadauto.chm

If you want to look up a Microsoft Application object model you may look somewhere like here
https://msdn.microsoft.com/en-us/library/office/ff846392(v=office.14).aspx

All the Microsoft ones can be found similarly. I always start with the Application object and see what functions and properties it has that sound like what I want (object functions are called methods).

Here is one more awesome hotkey I use all the time. win+h hides all layers other than the layers of the selected objects.

#h::
;hide all layers other than the layers of the selected objects
acad_handle :=  ComObjActive(“AutoCAD.Application.18”) ;get a com object handle to an existing Autocad Session
selection := acad_handle.ActiveDocument.PickfirstSelectionSet
chosen_layers  :=  []
for object in selection {
chosen_layers.insert(object.layer)
}
for layer in acad_handle.ActiveDocument.layers {
if inArray(layer.name , chosen_layers)
goto skip
layer.layeron := 0
skip:
}
return

Let me know if you have any questions!

Steve

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s