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

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

aknigin
Enthusiast Enthusiast
616 Views
4 Replies
Message 1 of 5

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

aknigin
Enthusiast
Enthusiast

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?

0 Likes
Accepted solutions (1)
617 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

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?

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

aknigin
Enthusiast
Enthusiast

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.

0 Likes
Message 4 of 5

Anonymous
Not applicable
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.
0 Likes
Message 5 of 5

aknigin
Enthusiast
Enthusiast
Accepted solution

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.

0 Likes