TheSwamp
Code Red => VB(A) => Topic started by: David Hall on September 12, 2007, 04:56:52 PM
-
OK, I thought this was a good idea, but it locks my machine up and then fatals and closes. So what is the best way to ensure that this variable is always set?
Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
If UCase(CommandName) = "OPEN" Then
ThisDrawing.SetVariable "INSUNITS", 1
End If
End Sub
-
Have you tried it in the EndOpen event instead of the EndCommand event?
-
I looked for that event, and it wasn't listed. Is that one of the undocumented features?
-
It's not available until to you set the application as it is an application level event.
Your crash is probably caused by the timing problem of a command being started in one "thisdrawing" then ending in another "thisdrawing", below is the best way I've found to get the app level events working after first opening cad
Public WithEvents AutoCAD As AcadApplication
Sub App_StartMacro()
Set AutoCAD = ThisDrawing.Application
End Sub
Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
If CommandName = "COMMANDLINE" Then
Set AutoCAD = ThisDrawing.Application
End If
End Sub
Private Sub AutoCAD_EndOpen(ByVal FileName As String)
ThisDrawing.SetVariable "INSUNITS", 1
MsgBox "ins"
End Sub
-
Just tried this and it worked fine.
Private Sub AcadDocument_Activate()
ThisDrawing.SetVariable "insunits", 1
End Sub
But I prefer to set them using my ACAD.LSP file (but that's just me).
-
Your crash is probably caused by the timing problem of a command being started in one "thisdrawing" then ending in another "thisdrawing"
This is one of the problems inherent with using the "ThisDrawing" class. "ThisDrawing" is generally equivalent to "Application.ActiveDocument", and since OPEN, sets the last opened document as ActiveDocument, the problem will persist.
To illustrate this in action use this code:
Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
If UCase(CommandName) = "OPEN" Then
Dim NewDoc As AcadDocument
Set NewDoc = Application.ActiveDocument
NewDoc.SetVariable "INSUNITS", 1
End If
End Sub
It will set INSUNITS to 1 in the already open drawing, not the newly opened drawing.
The best solution is using the code Matt posted, using the Activate event.
-
Just be wary with the AcadDocument_Activate() method.
If you only want something done once per dwg. this is a bit dubious as with 2 drawings open ctrl+tab will both activate the second dwg and run the sub again.
It sure seems fine for this application though.
-
It's not available until to you set the application as it is an application level event.
Your crash is probably caused by the timing problem of a command being started in one "thisdrawing" then ending in another "thisdrawing", below is the best way I've found to get the app level events working after first opening cad
Public WithEvents AutoCAD As AcadApplication
Sub App_StartMacro()
Set AutoCAD = ThisDrawing.Application
End Sub
Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
If CommandName = "COMMANDLINE" Then
Set AutoCAD = ThisDrawing.Application
End If
End Sub
Private Sub AutoCAD_EndOpen(ByVal FileName As String)
ThisDrawing.SetVariable "INSUNITS", 1
MsgBox "ins"
End Sub
The one thing I didn't see is wether or not to put this code in the ThisDrawing Module?
-
I tried it in the thisdrawng mod and it never runs, so I guess itdoesn't go there
-
OK, I guess I needed to close Autocad. All of a sudden, everything I open is firing the event. Thanks guys
-
Glad you got it working!
-
:| OK, I've read many different articles on this, and still can't work out how to get this to work up on Autocad finishing it's start up.
I can get the EndOpen to work once Autocad is running and I open another drawing ect.
But
I can't get the EndOpen event to trigger if the enduser 2x clicks on a drawing from windows explorer, any ideas would be helpfull ? :kewl:
-
It's too early to fire autocad up right now so you'll either have to wait till I get to the orifice or twist my fake code into something that actually works. The best luck I've had in firing code when a drawring opens is with end lisp. Since acad.lsp runs when autocad starts, there ya go.
'not even too very close to actual code
Private Sub AcadDocument_EndLisp(LispyFileName as String)
If LispyFileName = "acad.lsp" then
'crap you want to do here
end if
end sub
-
That's interesting Bob
Private Sub AcadDocument_EndLisp()
MsgBox "DOCLISP"
End Sub
the document level endlisp fires (6 times for me) before
Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
If CommandName = "COMMANDLINE" Then
Set AutoCAD = ThisDrawing.Application
End If
End Sub
but the app level lisp
Private Sub AutoCAD_EndLisp()
MsgBox "LISPING:"
End Sub
doesn't fire at all.
The "COMMANDLINE" always fires for me, including opening from explorer but the app endopen has already finished at this point.
-
Public WithEvents AutoCAD As AcadApplication
Private bApp As Boolean
Sub App_StartMacro()
Set AutoCAD = ThisDrawing.Application
End Sub
Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
If CommandName = "COMMANDLINE" Then
Set AutoCAD = ThisDrawing.Application
If Not bApp Then
bApp = True
AppStuff
End If
End If
End Sub
Private Sub AutoCAD_EndOpen(ByVal FileName As String)
AppStuff
End Sub
Private sub Appstuff
msgbox "appstuff"
End Sub
-
Eh, I was shooting from the hip. No caffeine yet, hadn't even left the bedroom yet actually. This is actually more what I had blodged together. Your commandline solution is much better, far more generic, and much less likely to get broken. What I did was the first way I came up with to solve a temporary problem I had.
Public booStartage As Boolean
Private Sub AcadDocument_BeginLisp(ByVal FirstLine As String)
If FirstLine = "first line of your acad lisp" Then
booStartage = True
End If
End Sub
Private Sub AcadDocument_EndLisp()
If booStartage Then
'do your shiznizzle here
booStartage = False
End If
End Sub
-
CM,
Isn't this a duplication of efforts?
If looks like you Set The AutoCAD Object to ThisDrawing on Startup, so why again if (command) COMMANDLINE is true? Just an observation
Mark
Sub App_StartMacro()
Set AutoCAD = ThisDrawing.Application
End Sub
Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
If CommandName = "COMMANDLINE" Then
Set AutoCAD = ThisDrawing.Application
End If
End Sub
-
Thats a really good question, one that must wait for Bryco to show up, as he gave me that. It works, so I didn't question it.
-
No man, no disrespect to Bryco but I already gave you the answer my friend.
Look at the acad.dvb file that I sent to you "and" that you helped me with
I set it once with the acadstartup macro (in your case with the app_startmacro) and that was all that was needed.
It is working fine for me; I guess once you set the reference to Thisdrawing, it is retained from drawing to drawing until you close ACAD at which point there is no drawing :)
Mark
-
Although, I am sure there is no harm (I think) in doing it the way you are.
Mark
-
CM
After you start ACAD and hit F2
Do you see this: ?
Loading AEC Detail Base...Initializing VBA System...
Loading VBA startup file...
Mark
-
yes, but not b/c why you think. I am loading VBA from my acad.rx file
-
Which is exactly what I am doing LOL
calling up the acvba.arx file
Great minds think a like I guess :)
Or in my case, cooked minds :-(