Author Topic: whats the best way to set a variable  (Read 5076 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