TheSwamp

Code Red => VB(A) => Topic started by: David Hall on September 12, 2007, 04:56:52 PM

Title: whats the best way to set a variable
Post 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?

Code: [Select]
Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
If UCase(CommandName) = "OPEN" Then
ThisDrawing.SetVariable "INSUNITS", 1
End If
End Sub
Title: Re: whats the best way to set a variable
Post by: Cathy on September 12, 2007, 05:14:27 PM
Have you tried it in the EndOpen event instead of the EndCommand event? 
Title: Re: whats the best way to set a variable
Post by: David Hall on September 12, 2007, 05:22:07 PM
I looked for that event, and it wasn't listed.  Is that one of the undocumented features?
Title: Re: whats the best way to set a variable
Post by: Bryco on September 13, 2007, 12:57:46 AM
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
Code: [Select]

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
Title: Re: whats the best way to set a variable
Post by: Guest on September 13, 2007, 08:27:38 AM
Just tried this and it worked fine.

Code: [Select]
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).
Title: Re: whats the best way to set a variable
Post by: Keith™ on September 13, 2007, 10:31:15 AM
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:
Code: [Select]
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.
Title: Re: whats the best way to set a variable
Post by: Bryco on September 13, 2007, 09:25:58 PM
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.
Title: Re: whats the best way to set a variable
Post by: David Hall on September 14, 2007, 09:59:20 AM
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
Code: [Select]

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?
Title: Re: whats the best way to set a variable
Post by: David Hall on September 14, 2007, 10:43:38 AM
I tried it in the thisdrawng mod and it never runs, so I guess itdoesn't go there
Title: Re: whats the best way to set a variable
Post by: David Hall on September 14, 2007, 11:32:56 AM
OK, I guess I needed to close Autocad.  All of a sudden, everything I open is firing the event.  Thanks guys
Title: Re: whats the best way to set a variable
Post by: Guest on September 14, 2007, 01:12:03 PM
Glad you got it working!
Title: Re: whats the best way to set a variable
Post by: Spageddie on October 26, 2007, 01:07:39 AM
 :| 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:
Title: Re: whats the best way to set a variable
Post by: Bob Wahr on October 26, 2007, 08:52:48 AM
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.
Code: [Select]
'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
Title: Re: whats the best way to set a variable
Post by: Bryco on October 26, 2007, 10:05:08 AM
That's interesting Bob
Code: [Select]
Private Sub AcadDocument_EndLisp()
    MsgBox "DOCLISP"
End Sub
the document level endlisp fires (6 times for me) before
Code: [Select]
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
Code: [Select]
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.
Title: Re: whats the best way to set a variable
Post by: Bryco on October 26, 2007, 10:39:23 AM
Code: [Select]
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
Title: Re: whats the best way to set a variable
Post by: Bob Wahr on October 26, 2007, 11:06:02 AM
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.

Code: [Select]
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
Title: Re: whats the best way to set a variable
Post by: ML on October 29, 2007, 09:48:57 AM
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

Code: [Select]
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
Title: Re: whats the best way to set a variable
Post by: David Hall on October 29, 2007, 10:29:30 AM
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.
Title: Re: whats the best way to set a variable
Post by: ML on October 29, 2007, 11:05:45 AM

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
Title: Re: whats the best way to set a variable
Post by: ML on October 29, 2007, 11:06:52 AM

Although, I am sure there is no harm (I think) in doing it the way you are.

Mark
Title: Re: whats the best way to set a variable
Post by: ML on October 31, 2007, 10:40:44 AM
CM

After you start ACAD and hit F2
Do you see this:   ?

Loading AEC Detail Base...Initializing VBA System...
Loading VBA startup file...

Mark
Title: Re: whats the best way to set a variable
Post by: David Hall on October 31, 2007, 03:25:14 PM
yes, but not b/c why you think.  I am loading VBA from my acad.rx file
Title: Re: whats the best way to set a variable
Post by: ML on October 31, 2007, 03:26:51 PM
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  :-(