I want to run a custom command after a save (QSAVE or SAVEAS) occurs. The following code works but it also gets run when an Autosave occurs as well. Does anyone know of a way to ignore the Autosave and have it only run when QSAVE or SAVEAS is run. Any help would be greatly appreciated. Thanks!
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.ApplicationServices
<Assembly: ExtensionApplication(GetType(Autodesk.AutoCAD.ACADAutoSaveDWF.AutoSave))>
Namespace Autodesk.AutoCAD.ACADAutoSaveDWF
Public Class AutoSave
Implements IExtensionApplication
Public WithEvents ThisDrawing As AcadDocument = Application.DocumentManager.MdiActiveDocument.AcadDocument
Public WithEvents docs As DocumentCollection = Application.DocumentManager
Private Sub docs_DocumentActivated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs) Handles docs.DocumentActivated
ThisDrawing = e.Document.AcadDocument
End Sub
Private Sub AcadDocument_QSave() Handles ThisDrawing.EndSave
ThisDrawing.Utility.Prompt("Save has occurred")
End Sub
Public Sub Initialize() Implements IExtensionApplication.Initialize
End Sub
Public Sub Terminate() Implements IExtensionApplication.Terminate
End Sub
End Class
End Namespace
You can't tell AutoCAD to not fire the event for AutoSaves, so the
only option is to look at the extension of the file being saved to, and
if it is "sv$" then do nothing.
Also, if you are making changes to the drawing in the event,
then it may be appropriate to not ignore AuotSaves, because
that creates a backup of the DWG file in case something goes
wrong, and in that case, if the autosaved file is restored, then
whatever you are doing in the event will be missing from the
file.
Thanks. I'll have to try looking for the sv$ or try something else. Maybe like undefining the QSAVE or the SAVEAS commands and redefined to a custom save. No sure what's the best way yet.
BTW, the goals is to generate a dwf upon save. So I don't want it to run when the autosave occurs otherwise a dwf is always being created every 10 or 15 mins. That's unnecessary steps that ACAD has to do.
Thanks!
I got my answer....... I think. Well it works. Instead of watching the EndSave. I am watching for EndCommand. Then filtering it out for only QSAVE and SAVEAS. Now how do I filter it out if they click Close and choose Yes to save the drawing? hummmmm.
~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.ApplicationServices
<Assembly: ExtensionApplication(GetType(Autodesk.AutoCAD.ACAD AutoSaveDWF.AutoSave))>
Namespace Autodesk.AutoCAD.ACADAutoSaveDWF
Public Class AutoSave
Implements IExtensionApplication
Public WithEvents ThisDrawing As AcadDocument = Application.DocumentManager.MdiActiveDocument.AcadDocument
Public WithEvents docs As DocumentCollection = Application.DocumentManager
Private Sub docs_DocumentActivated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs) Handles docs.DocumentActivated
ThisDrawing = e.Document.AcadDocument
End Sub
Private Sub AcadDocument_QSave(ByVal CommandName as String) Handles ThisDrawing.EndCommand
If CommandName = "QSAVE" or CommandName = "SAVEAS" then
ThisDrawing.Utility.Prompt("Save has occurred")
End If
End Sub
Public Sub Initialize() Implements IExtensionApplication.Initialize
End Sub
Public Sub Terminate() Implements IExtensionApplication.Terminate
End Sub
End Class
End Namespace
Sorry, that's a bad idea.
Saves can be done without issuing either of those commands,
as is the case of CLOSE or QUIT, as well as other means.
It's not easy to tell how the user answers the prompt and there
is no reason to jump through that hoop because you can just
check the file extension in the EndSave event.
You should be able to get the filename being saved to from
either of these sysvars:
SAVEFILE
SAVEFILEPATH
With that you only need to check the extension to
see if its an autosave or a user-save.
Thanks for the input! It is appreciated.
I didn't get a chance to work on this until now but here is my code. It seems to work. This just the sub
~~~~~~~~~~~~~~~~
Private Sub AcadDocument_QSave(ByVal FileName as String) Handles ThisDrawing.EndSave
Dim intExtLoc As Integer = FileName.Count - 3
Dim strExtension as String = FileName.Substring(intExtLoc,3).ToUpper
If strExtension = "DWG" then
ThisDrawing.Utility.Prompt("Save has occurred")
End If
End Sub
~~~~~~~~~~~~~~~
The only issue I am running into, which is minor I think, is when I run the code to plot the DWG to generate the dwf. At the end looks like a temporary save occurs. The saved file has a DWG extension as well but has ".0." before the extension. For example, Drawing1.0.dwg, Floorplan.0.dwg, etc. So I will have to do another check which will be simple enough. But now i am wondering how many commands will trigger a temporay save like the plot.
I create the commandEnded event within the DocumentCreated event.
doc.CommandEnded += OnCommandEnded;
Then, function is:
static void OnCommandEnded(object sender, CommandEventArgs e) { string comando = e.GlobalCommandName.ToLower(); if (comando=="qsave" || comando=="saveas") { //call your command } }
Hi @caddzone
Mi situation is the opposite case.
I want to catch the "QSAVE", "SAVEAS" and AUTO_SAVE, but AUTO_SAVE is not a command.
I need to catch them before saving the document, so I use the CommandWillStart event for (QSAVE, SAVEAS) commands.
But... how can I catch the "AUTO_SAVE" routine before saving the document?
I read this (waiting for any response)
http://adndevblog.typepad.com/autocad/2012/08/about-autosave-and-notificationsevents.html
Thanks.