Author Topic: Event for when closing AutoCAD  (Read 5213 times)

0 Members and 1 Guest are viewing this topic.

iliekater

  • Guest
Event for when closing AutoCAD
« on: October 23, 2007, 05:02:33 PM »
When someone wants to initiate something upon closing a drawing , he can use the corespondent event in the "This drawing" module . However , I'd lile to write something for when the whole program closes . So , is there any similar event for when closing AutoCAD ?

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #1 on: October 23, 2007, 07:30:05 PM »
All the way from the acad activex help
Code: [Select]
Public WithEvents ACADApp As AcadApplication    ' Use with Application Event Examples
Sub Example_AcadApplication_Events()
    ' This example intializes the public variable (ACADApp) which will be used
    ' to intercept AcadApplication Events
    '
    ' The VBA WithEvents statement makes it possible to intercept an generic object
    ' with the events associated with that object.
    '
    ' Before you will be able to trigger any of the AcadApplication events,
    ' you will first need to run this procedure.
   
    ' We could get the application from the ThisDocument object, but that would
    ' require having a drawing open, so we grab it from the system.
    Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
    ' This example intercepts an Application BeginQuit event.
    '
    ' This event is triggered when AutoCAD receives a request to shut down.
    '
    ' To trigger this example event:
    '     1) Make sure to run the example that initializes
    '     the public variable (named ACADApp) linked to this event.
    '
    '     2) Close the AutoCAD application

    ' Use the "Cancel" variable to stop the shut down of the application
    If MsgBox("AutoCAD is about to shut down.  Do you want to continue with the shutdown?", vbYesNoCancel + vbQuestion) <> vbYes Then
        Cancel = True
    End If
End Sub

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #2 on: October 24, 2007, 01:04:48 AM »
I confess I didn't understand the MsgBox use , but thanks ; I am going to give it a try !

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Event for when closing AutoCAD
« Reply #3 on: October 24, 2007, 01:52:27 AM »

I confess I didn't understand the MsgBox use , but thanks ;............

I Imagine just to demonstrate that the event can be trapped and that the return value from the trap determines if the Application closes or not.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #4 on: October 24, 2007, 11:07:00 AM »
We have the same imagination Kerry.

Kerry will now disappear for the next three hours because he's curled up in the fetal position, in a corner, with his wubbie, asking the universe, "Why?"

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #5 on: October 28, 2007, 04:15:05 PM »
Could you plese help me a little bit more ? I used the code , but I didn't manage to do anything . By the way , I realized that
Code: [Select]
Public WithEvents ACADApp As AcadApplicationhas to be placed in a class module .
I also added the other two subs , but when I close AutoCAD nothing happens ...
Actually what I am trying to do is to delete a temporary file when AutoCAD shuts down . Can you help me (again) ?

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #6 on: October 28, 2007, 11:20:45 PM »
You an drop all of the code into the This Drawing module which is IIRC a class module.

My first suggestion would be to set a break point on you firt line of cod. close autocad.  If the event is triggered, yousub will run and stop on the first line.  You can the use F8 to step through your code, line by line, paying attention to the results to see if you can pin down the problem.

My zero level supposition is that you need to get rid on On Error Resume Nexts so your routine can break when and how it's supposed to.

Second, if you can't figure out your problem, post your code so we can play the home game.

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #7 on: October 29, 2007, 01:35:29 AM »
I put all of the code in ThisDrawing module , placed a mark on the first line (Set ACADApp =.......) and still nothing ... Here is my whole code :
Code: [Select]
Public WithEvents ACADApp As AcadApplication
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Example_AcadApplication_Events()
    Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
    '
    MySubmarine
    '
    If MsgBox("AutoCAD is about to shut down.  Do you want to continue with the shutdown?", vbYesNoCancel + vbQuestion) <> vbYes Then
        Cancel = True
    End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MySubmarine()
    Kill "c:\MyDamnStupidFile.txt"
End Sub

By the way , what does the .16 code does ?
« Last Edit: October 29, 2007, 01:37:47 AM by iliekater »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Event for when closing AutoCAD
« Reply #8 on: October 29, 2007, 04:49:53 AM »
<snip>
By the way , what does the .16 code does ?

.16 is AC2004

There are several ways to error trap this in the case where yoy are using a different version.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Fatty

  • Guest
Re: Event for when closing AutoCAD
« Reply #9 on: October 29, 2007, 07:32:14 AM »
Take a look at the dxf system variable ACADVER
For version 16. they should:
AC1018 AutoCAD 2004/2005/2006

~'J'~

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #10 on: October 29, 2007, 08:54:01 AM »
Oh , OK . But hey ! How did you know I am using AutoCAD 2004 eitherway ? ? ? ! ! ! !  :lol:

Fatty

  • Guest
Re: Event for when closing AutoCAD
« Reply #11 on: October 29, 2007, 09:26:17 AM »
I have one from I wrote earlier on lisp
You can to translate it on VBA

Code: [Select]
(defun dwv (/ fso path readstr result source sourcepath)
  (vl-load-com)
  (setq path (getstring "\nEnter path name <C:\\> : "))
; change default folder name
  (if (eq "" path)
    (setq path "C:\\")
  ) ; change default folder name
  (setq sourcepath (getfiled "Select a Drawing File" path "dwg" 0))

  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (if (zerop (vlax-invoke fso 'FileExists (findfile sourcepath)))
    (progn
      (alert "File does not exist")
      (exit)
      (princ)
    )
  )
  (setq source (vlax-invoke fso 'OpenTextFile sourcepath 1 :vlax-false))
  (setq readstr (vlax-invoke source 'ReadLine))
  (vlax-invoke source 'Close)
  (vlax-release-object source)
  (vlax-release-object fso)
  (setq readstr (substr readstr 1 6))
  (setq result
(cond
   ((eq "AC1012" readstr) "R12")
   ((eq "AC1013" readstr) "R13")
   ((eq "AC1014" readstr) "R14")
   ((eq "AC1015" readstr) "R15")
   ((eq "AC1018" readstr) "R16")
   ((eq "AC1021" readstr) "R17")
)
  )
)
;;; TesT :
;;; (setq answer (dwv))

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #12 on: October 29, 2007, 02:20:34 PM »
Oh , OK . But hey ! How did you know I am using AutoCAD 2004 eitherway ? ? ? ! ! ! !  :lol:
Again, I posted sample code from ACAD Help.  It was written for that version.  Try this:
Code: [Select]
Sub test()
Debug.Print ThisDrawing.Application.Version
End Sub
  In your Immediate window you'll get something like "17.1s (LMS Tech)"  In this example, you would use the 17 part, like:

Code: [Select]
Public WithEvents ACADApp As AcadApplication
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Example_AcadApplication_Events()
    Set ACADApp = GetObject(, "AutoCAD.Application.17")
End Sub

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #13 on: October 30, 2007, 10:02:50 AM »
OK , but how can I delete that damn bloody stupid cursed and annoying file when I close AutoCAD ? Please  :cry:

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #14 on: October 30, 2007, 10:47:19 AM »
Welp, looking at the comments in the example I posted
Quote
    ' This example intializes the public variable (ACADApp) which will be used
    ' to intercept AcadApplication Events
    '
    ' The VBA WithEvents statement makes it possible to intercept an generic object
    ' with the events associated with that object.
    '
    ' Before you will be able to trigger any of the AcadApplication events,
    ' you will first need to run this procedure.
   
    ' We could get the application from the ThisDocument object, but that would
    ' require having a drawing open, so we grab it from the system.

So ACADApp needs to initialize before Application level events will trigger.  Since it seems like you are at the "give me a solution, not a lesson" stage, I'm not going to go into a lot of detail that you might not even read, so try this.
Code: [Select]
Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
  Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
  Kill "c:\MyDamnStupidFile.txt"
End Sub
If you want to discuss whys, hows, and improvements we can.  If you're happy with it working, enjoy.

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #15 on: October 30, 2007, 05:47:38 PM »
Well I would thankfully accept a lesson instead of a raw solution , but I don't dare to ask it Bob . I am afraid I made you angry ... : So I'll simply shut up and shut down .

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #16 on: October 30, 2007, 06:30:38 PM »
That wasn't anger at all.  I just started out typing what would have been a pretty long explanation and decided that I would hold off on the effort until I knew if you actually wanted it.  I've been to the point in things before where my brain is complete and total mush, I absolutely can't think anymore, and all I really wanted was for someone to give me what I needed to move forward so I could get on with my project and come back for the explanation later.  It sounded to me like that was where you were.  I don't have time to write it up at the moment but will try to get you something this evening or in the morning.

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #17 on: October 31, 2007, 07:37:33 PM »
So, let's start with what you had and take a look at it.  Starting off with the first sub...
Code: [Select]
Public WithEvents ACADApp As AcadApplication
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Example_AcadApplication_Events()
    Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
  In order to use an application level event, like BeginQuit, you have set the application.  The sub that you have there does set the application.  The problem is that before it is set, the sub has to be run and you have done nothing here to cause it to run.  I went with the assumption that before you quit autocad, you would close at least one drawing.  With that assumption, we can use BeginClose to set ACADApp because BeginClose is a Document level event which initializes automatically when you start.  So...
Code: [Select]
Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
  Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
Now on to the rest of it.
Code: [Select]
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
    '
    MySubmarine
    '
    If MsgBox("AutoCAD is about to shut down.  Do you want to continue with the shutdown?", vbYesNoCancel + vbQuestion) <> vbYes Then
        Cancel = True
    End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MySubmarine()
    Kill "c:\MyDamnStupidFile.txt"
End Sub
First off, when you want to run a sub from another sub, you need to call it so instead of the line
Code: [Select]
   MySubmarine you should use
Code: [Select]
   Call MySubmarine  The next thing I've got is the message box.  Do you really want AutoCAD to prompt you on whether you want to quit or not every time you close it?  Assuming you have users, they will kill you for something like that.  The message box was in the sample code just to show that you can capture the event and cancel it if you need to.  You don't have to have it.  So, taking that into consideration, we have this.
Code: [Select]
Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
  Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
    '
    Call MySubmarine
    '
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MySubmarine()
    Kill "c:\MyDamnStupidFile.txt"
End Sub
Next, why did you separate it out into two subs?  I don't see any reason for it but if you had one, I would be interested in hearing it.  In the mean time, let's combine those to into one.
Code: [Select]
Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
  Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
    Kill "c:\MyDamnStupidFile.txt"
End Sub
The last thing I've got for you right now is, When you change versions, will you have only one version in use?  Personally, when I upgrade, I install the new version but leave the old version installed while I make sure everything is functioning correctly.  I also usually use the new version for production for a while before installing for others, and often install in phases.  If there will only ever be one version of autocad using this application at the same time, you can safely leave it as is, and edit the version number as necessary.  If there's a chance of multiple versions, then you need some way to differentiate between them.  There are a few ways that you can do this.  Let's look at the line that sets it.

Set ACADApp = GetObject(, "AutoCAD.Application.16")

We can tell by looking at the application name that what we are passing is a string because it's in quotes.  So we need to figure out what the "16" part of it needs to be for it to work with the current version.  As we saw earlier in this thread,

Debug.Print ThisDrawing.Application.Version

will give you the version number but it gives more than you need.  All we really need to set the application are the two leftmost digits.  Now to set the version, we could go a few ways.  First off, we could use an If/Then check

If Left(thisDrawing.application.Version,2) = "16" then
  Set ACADApp = GetObect(, "AutoCAD.Application.16")
Elseif left(thisdrawing.applicaton.version, 2) = "17" then
  Set ACADApp = GetObect(, "AutoCAD.Application.17")
End if

You can see where that would get pretty clunky, pretty fast.  A slightly better way would be to use Cases

Select Case Left(ThisDrawing.Application.Version, 2)
  Case "16"
    Set ACADApp = GetObect(, "AutoCAD.Application.16")
  Case "17"
    Set ACADApp = GetObect(, "AutoCAD.Application.17")
  Case else
    Msgbox "the application version wasn't set to " & Left(ThisDrawing.Application.Version, 2)
end Select

Again, requires maintenance and starts to get unwieldy.  What we can do is just set the string correctly to begin with.

  Set ACADApp = GetObect(, "AutoCAD.Application." & Left(ThisDrawing.Application.Version, 2))

Which overall would give us
Code: [Select]
[code]Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
  Set ACADApp = GetObject(, "AutoCAD.Application." & Left(ThisDrawing.Application.Version, 2))
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
    Kill "c:\MyDamnStupidFile.txt"
End Sub
Are you still with me?  Does it make any sense?  Do you have any questions?[/code]

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: Event for when closing AutoCAD
« Reply #18 on: November 01, 2007, 10:32:29 AM »
Damn Bob, I learned 2 things today.  Thx for the tips
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

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: Event for when closing AutoCAD
« Reply #19 on: November 01, 2007, 10:33:21 AM »
I never could figure out an easy way to tell Autocad what it was, and
Code: [Select]
Set ACADApp = GetObject(, "AutoCAD.Application." & Left(ThisDrawing.Application.Version, 2)) was pretty damn easy
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

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #20 on: November 01, 2007, 10:56:36 AM »
Good, glad it will be useful.  What was the other thing?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Event for when closing AutoCAD
« Reply #21 on: November 01, 2007, 11:01:45 AM »
Good, glad it will be useful.  What was the other thing?

Something about cats legs ?
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #22 on: November 01, 2007, 11:07:04 AM »
Didn't figure that was it.  That seemed like a fairly straightforward math/logic problem to me.

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: Event for when closing AutoCAD
« Reply #23 on: November 01, 2007, 11:16:27 AM »
Good, glad it will be useful.  What was the other thing?
I never use app level events, so I learned how to set them here
Quote from: Bob Wahr
  In order to use an application level event, like BeginQuit, you have set the application.  The sub that you have there does set the application.  The problem is that before it is set, the sub has to be run and you have done nothing here to cause it to run.  I went with the assumption that before you quit autocad, you would close at least one drawing.  With that assumption, we can use BeginClose to set ACADApp because BeginClose is a Document level event which initializes automatically when you start.  So...
Code:

Public WithEvents ACADApp As AcadApplication
Sub AcadDocument_BeginClose()
  Set ACADApp = GetObject(, "AutoCAD.Application.16")
End Sub

Now on to the rest of 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

Bob Wahr

  • Guest
Re: Event for when closing AutoCAD
« Reply #24 on: November 01, 2007, 11:18:15 AM »
Cool.  I'm taking the rest of the day off then.

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #25 on: March 10, 2008, 05:32:02 AM »
Thanks , Bob Wahr . I know it's four and a half month since my last post in this topic , but at least I remebered to thank you  :-) . And , oh , your lesson was great . I surely learned more than only two thinks (this actually shows I don't know a lot  :-D . There are still plenty for me out there to learn) !

iliekater

  • Guest
Re: Event for when closing AutoCAD
« Reply #26 on: March 11, 2008, 03:50:43 PM »
I am sorry to bring this topic back , but I found that there is something tragic going on ...
I used the code Bob Wahr explained to me and actually it worked . However , this only was going perfectly until I open a second file (*.dwg) . You see, if I start AutoCAD , I can only load a single file . If I go on and try opening a second file , I get an AutoCAD's message form saying : Invalid file name ... I tried opening the files by doublecklicking (instead of opening them via the Open dialog box) , but I laso get the same error message .
I noticed that if don't load the VBA files that make use of the code that erases something during AuroCAD's shut down , everything goes back to normal . I tried loading the problematic VBA files with the APPLOAD command (up to now I used an ACAD.lsp file) , but once again when they are loeded I can't open any drawing file ...
I know I must be doing something wrong , but I can't figure it out ...
Here's the code that's in the "ThisDrawing" module of each of the VBA files :
Code: [Select]
Public WithEvents ACADApp As AcadApplication

Private Sub AcadDocument_BeginClose()
  Set ACADApp = GetObject(, "AutoCAD.Application." & Left(ThisDrawing.Application.Version, 2))
End Sub

Private Sub ACADApp_BeginQuit(Cancel As Boolean)
  On Error Resume Next 'That is in case the file does not exists or has been deleted
  Open "c:\RemindersPath" For Input As #1
  Input #1, RemindersPath
  Close #1
  Kill RemindersPath & "\AIRCONDITIONBLOCKS_ArrowFactor"
  Kill "c:\RemindersPath"
End Sub

Do you have any idea ? ... Let me specify here that I am using AutoCAD 2004 .

Lett me add omething else too :
First I simply created a new project and I added the above code in the "ThisDrawing" module . I didn't add any other code anywhere . It proved that even this small piece of code caused the same error . Then I tried deleting and moving the lines around in hope that I might somehow make it work (I know this a cheap method , but I was desperat) and at the end I found out that the problem is caused from only a line the :
Code: [Select]
Set ACADApp = GetObject(, "AutoCAD.Application." & Left(ThisDrawing.Application.Version, 2)
« Last Edit: March 11, 2008, 04:35:24 PM by iliekater »