Author Topic: ACADApp_BeginQuit Event  (Read 3715 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
ACADApp_BeginQuit Event
« on: January 18, 2008, 01:35:06 PM »

Hi
I am trying to switch my current active workspace when I close ACAD
However, I am getting an error "Failed to get the document object"
Does anyone know a workaround?

Thanks
Mark

Code: [Select]
Private Sub ACADApp_BeginQuit(Cancel As Boolean)
 Dim CurrSysVarData As Variant
 Dim SysVarName As String
 Dim NewSysVarData As String
 
 SysVarName = "WSCURRENT"
 CurrSysVarData = ThisDrawing.GetVariable(SysVarName)
 NewSysVarData = "Exit"
 
  If CurrSysVarData <> "Exit" Then
    ThisDrawing.SetVariable SysVarName, NewSysVarData
    If MsgBox("Would you like to save this drawing?", vbYesNo) = vbYes Then
     ThisDrawing.Save
     Application.Quit
    Else
     Application.Quit
     Exit Sub
    End If
  End If
End Sub

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: ACADApp_BeginQuit Event
« Reply #1 on: January 18, 2008, 02:19:03 PM »
The problem is you can close all your drawings without closing the app, and you cant make changes w/o a document being open.  What you need to do is check to see how many documents are open and if =1 set your var then close.  If doc.count > 1, do nothing
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

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #2 on: January 18, 2008, 03:07:55 PM »

Cool CM!

I would not have thought of that but it makes a whole lot of sense :)
Yes, how can it do something to a document (drawing) if there is no drawing (object) to work on

Thanks!
Mark

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #3 on: January 21, 2008, 10:45:19 AM »
CM

This method:
Code: [Select]
If Documents.Count > 0 Then
  GoTo Continue
Else
  Exit Sub
End If
 
Continue:
MsgBox "Do Something"

Is working in its own subroutine but it is not working along with my ACADApp_BeginQuit code.
Any idea?

If it works alone, then in theory it should get the job done. ?

Thanks!
Mark

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #4 on: January 21, 2008, 11:57:56 AM »
CM

When I put that code in the app event; it still says, can not get the document object.
I wonder if it is because I am in an object level event?

Mark

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #5 on: January 21, 2008, 03:17:54 PM »
OK

I think the problem here is that on the before close event (even though it says BEFORE CLOSE) I think it is closing all drawings before running the event; therefore I am getting a failed to get document error.

This is defientely an interesting one.

May be a Do While Application.documents > 0 would work.

I don't know.

Mark

Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #6 on: January 21, 2008, 04:45:51 PM »
I ran into this a long time ago and was given a workaround by Randall.  Unfortunately for now, the program he gave it to me for evolved away from using it so the code is no longer there.  I'll dig through my archive when I get a chance and see if I can find it.

Just a thought though, if you are closing the drawing without saving it, it makes no difference what space you are in, correct?  The flip side of that coin is that what you really want to do is ensure that it is in the proper space at save time.  So then, you might use the BeginSave event instead.

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #7 on: January 21, 2008, 05:59:09 PM »

Hey Bob

That is a very good idea, but I can not honestly say that I will be saving the drawing every time I close.

Right now I am using the ACADApp_SysVarChanged event which works fine.
I switch workspaces and that does every thing I need from there, it is just that don't I don't often remember to do that.

So, I thought it would be nice to ACAD make the switch for me on closing.

Mark



Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #8 on: January 21, 2008, 06:08:37 PM »
That is a very good idea, but I can not honestly say that I will be saving the drawing every time I close.
That was exactly my point.  If you don't save the drawing, it doesn't matter what space it is in when the drawing closes because, well, you didn't save.

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #9 on: January 21, 2008, 06:22:24 PM »

Oh yes, sorry I missed that.
I'm not sure (at this point) what space has to do with it, but the answer is, no it does not matter.

Mark

Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #10 on: January 21, 2008, 07:09:16 PM »
Sorry, just scanned and misread the active workspace in the OP.  Give this a shot.

Code: [Select]
Private Sub AcadDocument_BeginClose()
If Documents.Count = 1 Then
 SysVarName = "WSCURRENT"
 CurrSysVarData = ThisDrawing.GetVariable(SysVarName)
 NewSysVarData = "Exit"
 
  If CurrSysVarData <> "Exit" Then
    ThisDrawing.SetVariable SysVarName, NewSysVarData
    If MsgBox("Would you like to save this drawing?", vbYesNo) = vbYes Then
     ThisDrawing.Save
     Application.Quit
    Else
     Application.Quit
     Exit Sub
    End If
  End If
End If
End Sub

And yes, I did screw up my workspace testing it.  Doh!

Bryco

  • Water Moccasin
  • Posts: 1849
Re: ACADApp_BeginQuit Event
« Reply #11 on: January 21, 2008, 08:33:59 PM »
Bob I heard you swearing from La.

Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #12 on: January 21, 2008, 09:06:49 PM »
I don't doubt it for a second.

ML

  • Guest
Re: AcadDocument_BeginClose Event
« Reply #13 on: January 22, 2008, 10:17:54 AM »
Yes sir!

On that event (AcadDocument_BeginClose) it did work
Thanks Bob!

You said that it did mess up your workspace when closing ACAD?
It is a known issue but AutoDesk; if 2 menus (cui files) are sharing the same toolbars, the vertical alignment of two adjacent toolbars will get thrown off on one of the cui files (workspaces)
In my case, The toolbars in my Land.cui file (in Land Desktop are fine), it was in Map Enabled (acmap.cui) that the toolbar alignment was getting screwed up.

AutoDesk said to create a start up routine that will set your wscurrent, so I did that but quickly realized that this was not the solution. Apparently it is the current workspace upon exiting acad that gets whacked out.

Hence this work around. Let the exit workspace get screwed up, it is a dummy workspace with nothing (the minimal requirements) in it.

On the flip side, I do have a set workspace current module that gets ran on start up.
I can post that after this post, if you are interested.
It does looks like this did the job though

Thanks again
Mark

 

ML

  • Guest
Workspace current on startup
« Reply #14 on: January 22, 2008, 10:23:24 AM »
Bob
If you or anyone is interested, here is my start up code for wscurrent
It checks for the active profile, menufile (main cui) and lastly it will sets your wscurrent
Mark

Code: [Select]
Sub Acadstartup()

Dim Username As Variant
Dim WshNetwork As Variant
Dim Preferences As AcadPreferences
Dim ActProfile As String
Dim CurrMenuPath As String
Dim CurrMenuFile As String

Set WshNetwork = CreateObject("WScript.Network")
Set Preferences = ThisDrawing.Application.Preferences

Username = WshNetwork.Username
ActProfile = Preferences.Profiles.ActiveProfile
CurrMenuPath = "C:\Documents and Settings\" & Username & "\Application Data\Autodesk\Autodesk Land Desktop 2006\R16.2\enu\Support\"
CurrMenuFile = Preferences.Files.MenuFile

If ActProfile = "Land Desktop" Then
 CurrMenuFile = CurrMenuPath & "land"
 ThisDrawing.SetVariable "wscurrent", "Land Destop Complete"
Else
 If ActProfile = "MapLandDevelopmentDesktop" Then
 CurrMenuFile = CurrMenuPath & "acmap"
 ThisDrawing.SetVariable "wscurrent", "Map Classic"
End If
End If
 
End Sub

Bob Wahr

  • Guest
Re: AcadDocument_BeginClose Event
« Reply #15 on: January 22, 2008, 10:44:22 AM »
You said that it did mess up your workspace when closing ACAD?
I just use vanilla acad and don't change workspaces.  Didn't really give it any thought before I did it but assumed that since my configuration was maintained that the current workspace was saved that way.  When I changed it and changed it back, everything went to default.  I just need to close the useless crap like dashboard, and redock what I need.

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #16 on: January 22, 2008, 11:05:14 AM »

Yeah, I see
It really is not that big of a deal to fix my toolbars after start up; it just became a pain in the *
So, I decided to write something to address it.
Not to mention that I can draw from this code (as we all do) for future things as well.

Apparently, the workspace definition is written to the registry but not retained.
I'm not sure (at least) in my case, why it affects only the last current workspace and only in the one profile but apparently that was the problem and this seemed to have fixed it.

It is bizarre that we have to do these things to fix ACADs inefficiencies but there are quite a few bugs still lingering in the cui files. I was fine with .mns and .mnc files; they couldn't leave good enough alone? :) Although, workspaces are a great and very useful feature.

Bob:
While I have you here, can you please help me with one other thing?

If the drawing name is drawing#.dwg, on close, I want to initiate a save as if I choose yes to save via the message box.

I have this:
Code: [Select]
If ThisDrawing.Name = "Drawing" & "I need help here" & ".dwg" Then

I'm not sure how to capture the numeric value after Drawing.

My guess is it would be something like
Code: [Select]
For i = 0 to "Not sure"
 If ThisDrawing.Name = "Drawing" & (i) & ".dwg" Then
  I have the rest of the code
 End if
Next i

I'd appreciate it!

Mark











Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #17 on: January 22, 2008, 12:45:37 PM »
If Left(thisdrawing.name,7) = "Drawing" Then

?

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #18 on: January 22, 2008, 01:30:52 PM »

Yes! Of course
Duh! Why do I over think things so much

You are basically just filtering for the word "drawing".
Excellent!

Thanks Bob!

Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #19 on: January 22, 2008, 01:35:08 PM »
Actually, for the word "Drawing"  almost through a UCASE in there, but in this instance, a new drawing should always be named "Drawing"#.dwg.

No problem.

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #20 on: January 22, 2008, 01:40:35 PM »

Yes, that is true
You could use Proper in that case
Proper (Thisdrawing.name)
As you said, ACAD by deafult uses proper case Drawing.

That is cool
I always used Left(Thisdrawing.name Len(Thisdrawing.name)-4 which would achieve the same result
I like you way you are doing it; are counting 7 characters from the left as opposed to the way I way doing it, which was to trim 4 characters (.dwg) from the right.

Mark



ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #21 on: January 22, 2008, 01:41:49 PM »

WOW!
Excuse my spelling in the last post LOL

Thought I did a spell check :)

Mark

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #22 on: January 22, 2008, 01:55:46 PM »

Bob,

I think we want to change the 7 to an 8 so that we grab the drawing number as well or we may need an Instr function.
Am I wrong here?

Mark

Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #23 on: January 22, 2008, 02:27:03 PM »
Code: [Select]
For i = 0 to "Not sure"
 If ThisDrawing.Name = "Drawing" & (i) & ".dwg" Then
  I have the rest of the code
 End if
Next i

I don't know what "I have the rest of the code" is doing so I don't know.  If you need the number I would do something like this
Major psuedo-code not even trying much warning

Code: [Select]
dim strDwgNo as String 'or int dbl whatever you need from it, I assume string for new dwg name
dim strDwgNm as String
dim intDecPlc as Integer
strdwgnm = thisdrawing.name
if left(strdwgnm, 7) = "Drawing" then
  intdecplc = len(strdwgnm)-11 ' 7 for Drawing, 4 for .dwg.  The remainder of the name should be #, ##, ###
  strdwgno = mid(strdwgname,8,2)
  '"I have the rest of the code" goes here
end if

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #24 on: January 22, 2008, 02:40:43 PM »

LOL

Bob!
You're going to kick my butt!  :pissed:

I was running the code with more then one drawing open; therefore your original suggestion will work just fine :)

That is:
Code: [Select]
Left(ThisDrawing.Name, 7)

All I am doing in this section is checking for (as your code does) if Drawing is in the drawing name.
If so, prompt for a save as.

First of all, most drawings are going to be saved with a real name (not drawing#), second I can not have AutoCAD prompt me for a save as dialog box; therefore, I would need to create a variable for an inputbox and type in the filename in etc. etc.
Not worth LOL
It is overkill for what I am doing

Still, your code is definitely worth keeping, I will be able to use it again.

Thanks again!
Mark

Bob Wahr

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #25 on: January 22, 2008, 03:18:09 PM »
second I can not have AutoCAD prompt me for a save as dialog box; therefore,
que?

thisdrawing.sendcommand "saveas"

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #26 on: January 22, 2008, 03:28:45 PM »

Dalp!

This is true :)

Thank you sir!

Mark

ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #27 on: January 24, 2008, 11:13:43 AM »

Bob,
This way all I really needed
Code: [Select]
Dwgname = ThisDrawing.Name

If Left(Dwgname, 7) = "Drawing" Then
'Rest of code
End

I do that all of the time; I think about the big picture and miss the smallest things.
I guess we all do that sometimes.

I did save all of your code for future needs to filter out drawing names.

The bizarre thing here is that now that I resolved the problem with Map Enabled; now it is the last workspace (on exit) in Land Desktop that is getting thrown out of sorts. So, I can either have an exit workspace on both profiles are just simply (better idea) switch to a workspace that I do not use much on exit.

Or better yet, manually switch the workspace before exit.
Or even better then that, just say F-it! :)
What a pain in the butt.

Thanks Bob!


ML

  • Guest
Re: ACADApp_BeginQuit Event
« Reply #28 on: January 24, 2008, 01:03:23 PM »
OK, here is what I found;
I did not need to filter for the drawing name at all.
If the drawing = drawing & # and there is nothing drawn in there; then there is nothing to save.
I know, No S**t

What I wasn't realizing before was that if you haven't named the drawing yet but have drawn something, then you will automatically be prompted for a save as, so that code was not necessary to begin with; or that problem may have been a result of The BeginClose Event.Also I still had The Application.quit code in there which was forcing ACAD to close (possibly) prior to asking me if I want to save.

Also after reading over the help screen,
Quote
You should use the BeginDocClose event rather than the BeginClose event because the BeginDocClose event enables you to keep a drawing from being closed.

No events occur while a modal dialog is displayed.

So after realizing that I am now using a BeginDocClose event; I will inherently be closing ACAD (duh) so that will force the save-saveas dialog boxes to appear as normal.

I think the drawing may have been closing prior to some events firing; so it looks like BeginDocClose is the winner here.

Lastly, I am able to check for Documents.Count >= 1 and ACAD will prompt for each drawing (save-saves) in MDI Mode prior to closing out of the app.

So, the end result was that (for now) I simply created a workspace called Exit in both Land Desktop and Map Enabled so that I don't have to check for Profile, Menufile etc. etc.; this is just much easier (less code) and very easy to create. Plus, all of your (in use) workspace's toolbars should all remain just how they were left off when closing, as acad will only mess with the Exit (BS) workspaces.

The only code I needed in the end was:
Code: [Select]
Private Sub AcadDocument_BeginDocClose(Cancel As Boolean)
Dim Dwgname As String
Dim CurrSysVarData As Variant
Dim SysVarName As String
Dim NewSysVarData As String
 
Dwgname = ThisDrawing.Name
SysVarName = "WSCURRENT"

On Error GoTo Done
 If Documents.Count >= 1 Then
 'Get Current Workspace setting
  CurrSysVarData = ThisDrawing.GetVariable(SysVarName)
 End If
 If CurrSysVarData <> "Exit" Then
  NewSysVarData = "Exit"
 'Set system variable current workspace
  ThisDrawing.SetVariable SysVarName, NewSysVarData
 End If
End Sub

However, I posted the (below) code in it's entirety (using Case Statements to check for drawing name and save method) if anyone would like to refer to it.

Thanks again for the help Bob!
Mark

Code: [Select]
Private Sub AcadDocument_BeginDocClose(Cancel As Boolean)
Dim Dwgname As String
Dim CurrSysVarData As Variant
Dim SysVarName As String
Dim NewSysVarData As String
 
Dwgname = ThisDrawing.Name
SysVarName = "WSCURRENT"

On Error GoTo Done
 If Documents.Count >= 1 Then
 'Get Current Workspace setting
  CurrSysVarData = ThisDrawing.GetVariable(SysVarName)
 End If
 If CurrSysVarData <> "Exit" Then
  NewSysVarData = "Exit"
 'Set system variable current workspace
  ThisDrawing.SetVariable SysVarName, NewSysVarData
 End If
   
 Select Case Left(Dwgname, 7)
  Case Is = "Drawing"
   If MsgBox("Would you like to save this drawing?", vbYesNo) = vbYes Then
    ThisDrawing.SendCommand "saveas" & vbCr
   End If
  Case Is > "Drawing"
   If MsgBox("Would you like to save this drawing?", vbYesNo) = vbYes Then
    ThisDrawing.Save
   End If
  End Select

Done:
End Sub