.NET

.NET

Reply
Active Contributor
aknigin
Posts: 27
Registered: ‎04-15-2010
Message 1 of 5 (138 Views)
Accepted Solution

Database.SaveComplete event is not fired in Civil3D 2014 and 2015

138 Views, 4 Replies
07-21-2014 09:59 AM

In my code as shown below I try to cath the SaveComplete event:

       public void Request()
        {
            try
            {
                Database db = HostApplicationServices.WorkingDatabase;
                Document current_doc = Application.DocumentManager.GetDocument(db);
                current_doc.Database.BeginSave += OnBeginSave;
                current_doc.Database.SaveAs(drawing_path, DwgVersion.Current);
            }
            catch (System.Exception)
            {
            }
        }

        void OnSaveComplete(object sender, DatabaseIOEventArgs e)
        {
            try
            {
                Database db = sender as Database;
                if (db != null)
                {
                    db.SaveComplete -= OnSaveComplete;
                }
            }
            catch (System.Exception)
            {
            }
        }

        void OnBeginSave(object sender, DatabaseIOEventArgs e)
        {
            try
            {
                Database db = sender as Database;
                if (db != null)
                {
                    db.BeginSave -= OnBeginSave;
                    db.SaveComplete += OnSaveComplete;
                }
            }
            catch (System.Exception)
            {
            }
        }

 But it looks as it works fine in Civil3D up to version 2013. In later versions the SaveComplete event is not fired or something. Any suggestions?

Your post gave me some idea, I made investigations and it seems that the Database.SaveAs method should be called from the main thread to the events work properly.

*Expert Elite*
norman.yuan
Posts: 1,062
Registered: ‎04-27-2009
Message 2 of 5 (132 Views)

Re: Database.SaveComplete event is not fired in Civil3D 2014 and 2015

07-21-2014 10:56 AM in reply to: aknigin

If the code you showed here is the same in your project for Acad (or Civil3d, not different here) 2014/15, and 2013, the code in the OnSaveComplete() would never run, regardless AutoCAD's version: you did not hook the event handler (OnSaveComplete()) to any event that might be fired by Document/Database. That is, whatever event AutoCAD exposed ALWAYS fires, your code only responds to a event when your code hooks an event handler to the event. Just as you did with Database.BeginSave event. You need to add

 

current_Doc.Database.SaveComplete+=OnSaveComplete;

 

Isn't it quite obvious?

Active Contributor
aknigin
Posts: 27
Registered: ‎04-15-2010
Message 3 of 5 (104 Views)

Re: Database.SaveComplete event is not fired in Civil3D 2014 and 2015

07-22-2014 12:27 AM in reply to: norman.yuan

Norman,

I do subscribe to the SaveComplete event - in OnBeginSave handler, which you can see in the code snippet. But whatever - I tried to subscribe to both events in the Request() method and got the same results, of course: it does work in Civil3D up to version 2013, and in the versions 2014 and 2015 only BeginSave event is fired.

Distinguished Contributor
mcicognani
Posts: 131
Registered: ‎12-01-2004
Message 4 of 5 (100 Views)

Re: Database.SaveComplete event is not fired in Civil3D 2014 and 2015

07-22-2014 01:34 AM in reply to: aknigin
I'm not sure you can add the SaveComplete handler inside the OnBeginSave event... isn't too close a call?
I cannot debug your code, but I can say you what I normally do and works well:
- in the plugin init I attach an event handler to the Application.DocumentManager.DocumentCreated event;
- in the DocumentCreated handler I add alla the handler needed, in my case both BeginSave and SaveComplete

Only drawback, the plugin may start after the first document load, so you also need to cycle through the DocumentManager and attach the handlers to the already loaded documents.
I assure you this works.
Active Contributor
aknigin
Posts: 27
Registered: ‎04-15-2010
Message 5 of 5 (92 Views)

Re: Database.SaveComplete event is not fired in Civil3D 2014 and 2015

07-22-2014 04:16 AM in reply to: mcicognani

Your post gave me some idea, I made investigations and it seems that the Database.SaveAs method should be called from the main thread to the events work properly.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.