Hello Experts,
I have a routine which listen to Save Document command and then write some Data to Document Dictionary.
The problem is when user click on top right 'X' to quit and then when Auto-CAD prompts for saving , my routine crashes.
What is the best approach to catch if user clicks the 'X' and if user wants to save the document before document closes data to be written to Dictionary?
Thanks in Advance.
Janet.
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Solved by _gile. Go to Solution.
Hello Gile and Thank you for replying back to me.
If I use that Event how would I know if user is pressing Yes or No when program asking " Save Change to C:\BlahBlah.dwg ?"
I don't want to save database if user pressing No.
I summarized the code below for your review.
Public Class AllCommandAndInitialize Implements Autodesk.AutoCAD.Runtime.IExtensionApplication Public Sub Initialize() Implements IExtensionApplication.Initialize AddHandler Application.DocumentManager.DocumentCreated, AddressOf StartUpEvents.On_Document_Created End Sub Public Sub Terminate() Implements IExtensionApplication.Terminate End Sub Class StartUpEvents Shared Sub On_Document_Created(sender As Object, e As DocumentCollectionEventArgs) AddHandler Application.DocumentManager.CurrentDocument.BeginDocumentClose, AddressOf StartUpEvents.OnBeginDocumentClose End Sub Shared Sub OnBeginDocumentClose(sender As Object, e As DocumentBeginCloseEventArgs) 'If user presses 'Yes' to "Save c:\blahblah.dwg" then ' draw a line and save database. 'else ' do nothing. ' end if End Sub End Class End Class
You can handle the Database.BeginSave or the Database.SaveComplete event
public class Initialization : IExtensionApplication { public void Initialize() { var docMan = Application.DocumentManager; docMan.DocumentCreated += DocumentManager_DocumentCreated; foreach (Document doc in docMan) { doc.Database.SaveComplete += Database_SaveComplete; } } private void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e) { e.Document.Database.SaveComplete += Database_SaveComplete; } private void Database_SaveComplete(object sender, DatabaseIOEventArgs e) { // do your stuff here } public void Terminate() { } }
I apologize for hijacking, but this is 'very' close in topic to a thread I was going to make.
So lets say a user is Closing/Saving a DWG, we can catch that with the event handlers. But utilizing those event handlers, is there anyway to stop the DWG from closing if we wanted to within that Event Handler?
Thanks for the quick reply! I should have specified this further in my initial post, but I do want to be able to veto the close after the save has been performed. I'll need to look into it and mess around with it.
Hello Gile
I am not sure why this don't work. I followed what you suggested.
still I don't see what I expect to see. I click on 'X' and when get prompt to save, I say save. after opening the DWG file I don't see the line in there. Would you mind please.
Public Class AllCommandAndInitialize Implements Autodesk.AutoCAD.Runtime.IExtensionApplication Public Sub Initialize() Implements IExtensionApplication.Initialize AddHandler Application.DocumentManager.DocumentCreated, AddressOf StartUpEvents.On_Document_Created End Sub Public Sub Terminate() Implements IExtensionApplication.Terminate End Sub Class StartUpEvents Shared Sub On_Document_Created(sender As Object, e As DocumentCollectionEventArgs) AddHandler Application.DocumentManager.CurrentDocument.BeginDocumentClose, AddressOf StartUpEvents.OnBeginDocumentClose End Sub Shared Sub OnBeginDocumentClose(sender As Object, e As DocumentBeginCloseEventArgs) AddHandler Application.DocumentManager.MdiActiveDocument.Database.SaveComplete, AddressOf OnSaveComplete End Sub Private Shared Sub OnSaveComplete(sender As Object, e As DatabaseIOEventArgs) Dim Mydwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim MyDb As Database = Mydwg.Database Using MyDb Using tr As Transaction = MyDb.TransactionManager.StartTransaction Try Dim mybtr As BlockTableRecord = CType(tr.GetObject(MyDb.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord) Dim myline As Line = New Line(New Point3d(0, 0, 0), New Point3d(10000, 10000, 0)) mybtr.AppendEntity(myline) tr.AddNewlyCreatedDBObject(myline, True) tr.Commit() Autodesk.AutoCAD.ApplicationServices.DocumentExtension.CloseAndSave(Mydwg, Mydwg.Name) Catch ex As Autodesk.AutoCAD.Runtime.Exception MsgBox(ex.StackTrace) End Try End Using End Using End Sub End Class End Class
Hi,
It looks like the Document.BeginDocumentClose event fires after the database have been saved.
You can try handling the Database.BeginSave event and check if the current command is "CLOSE".
public class Initialization : IExtensionApplication { public void Initialize() { var docs = Application.DocumentManager; foreach (Document doc in docs) { doc.Database.BeginSave += Database_BeginSave; } docs.DocumentCreated += Docs_DocumentCreated; } public void Terminate() { } private void Docs_DocumentCreated(object sender, DocumentCollectionEventArgs e) { e.Document.Database.BeginSave += Database_BeginSave; } private void Database_BeginSave(object sender, DatabaseIOEventArgs e) { var db = (Database)sender; var doc = Application.DocumentManager.GetDocument(db); if (doc.CommandInProgress == "CLOSE") { using (var tr = db.TransactionManager.StartOpenCloseTransaction()) { var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite); var line = new Line(Point3d.Origin, new Point3d(1000.0, 1000.0, 0.0)); ms.AppendEntity(line); tr.AddNewlyCreatedDBObject(line, true); tr.Commit(); } } } }
Can't find what you're looking for? Ask the community or share your knowledge.