Author Topic: Event for when closing AutoCAD  (Read 9519 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

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.