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

0 Members and 1 Guest are viewing this topic.

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 »