Author Topic: whats the best way to set a variable  (Read 5074 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
whats the best way to set a variable
« 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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Cathy

  • Guest
Re: whats the best way to set a variable
« Reply #1 on: September 12, 2007, 05:14:27 PM »
Have you tried it in the EndOpen event instead of the EndCommand event? 

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: whats the best way to set a variable
« Reply #2 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?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: whats the best way to set a variable
« Reply #3 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

Guest

  • Guest
Re: whats the best way to set a variable
« Reply #4 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).

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: whats the best way to set a variable
« Reply #5 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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Bryco

  • Water Moccasin
  • Posts: 1882
Re: whats the best way to set a variable
« Reply #6 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.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: whats the best way to set a variable
« Reply #7 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?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: whats the best way to set a variable
« Reply #8 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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: whats the best way to set a variable
« Reply #9 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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: whats the best way to set a variable
« Reply #10 on: September 14, 2007, 01:12:03 PM »
Glad you got it working!

Spageddie

  • Guest
Re: whats the best way to set a variable
« Reply #11 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:

Bob Wahr

  • Guest
Re: whats the best way to set a variable
« Reply #12 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

Bryco

  • Water Moccasin
  • Posts: 1882
Re: whats the best way to set a variable
« Reply #13 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.

Bryco

  • Water Moccasin
  • Posts: 1882
Re: whats the best way to set a variable
« Reply #14 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

Bob Wahr

  • Guest
Re: whats the best way to set a variable
« Reply #15 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

ML

  • Guest
Re: whats the best way to set a variable
« Reply #16 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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: whats the best way to set a variable
« Reply #17 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.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ML

  • Guest
Re: whats the best way to set a variable
« Reply #18 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

ML

  • Guest
Re: whats the best way to set a variable
« Reply #19 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

ML

  • Guest
Re: whats the best way to set a variable
« Reply #20 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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: whats the best way to set a variable
« Reply #21 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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ML

  • Guest
Re: whats the best way to set a variable
« Reply #22 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  :-(