.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to catch Save/Quit Document?

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
JanetDavidson
880 Views, 9 Replies

How to catch Save/Quit Document?

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.

 

 

9 REPLIES 9
Message 2 of 10
_gile
in reply to: JanetDavidson

Hi,

 

You should try to handle the Document.BeginDocumentClose envent.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 10
JanetDavidson
in reply to: _gile

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

 

Message 4 of 10
_gile
in reply to: JanetDavidson

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()
        { }
    }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 10

 

Thanks for your time and expertise.

 

Cheers,

Janet.

 

 

Message 6 of 10
mhillis
in reply to: _gile

@_gile

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?

Message 7 of 10
_gile
in reply to: mhillis

As explained in the docs, if you handle Document.DocumentBeginClose event, you "can call DocumentBeginCloseEventArgs.Veto() during this notification to stop AutoCAD from shutting down the document."



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 10
mhillis
in reply to: _gile

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. 

Message 9 of 10
JanetDavidson
in reply to: _gile

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
Message 10 of 10
_gile
in reply to: JanetDavidson

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();
                }
            }
        }
    } 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


AutoCAD Beta