catch events about saving a document.

catch events about saving a document.

joantopo
Mentor Mentor
2,468 Views
7 Replies
Message 1 of 8

catch events about saving a document.

joantopo
Mentor
Mentor

Hi.

Is there any API event about "saving a document"? I refer about the 3 types: quick save, save and "save as".

 

Maybe, I could catch them when if it is typed the command to save the document, but.. what I do with AUTO-SAVE?

 

 

The issue in my app, is that my app throws messagebox when an object is modified and when the document is saved the object modified event is fired, so I get those messagebox every time that I save the drawing file.

 

Thanks in advance.

 

 

Autocad C3D 2019 SP3, 2020 & 2021
Intel I9 9900K with frontal watercooler alphacool eisbaer 360 (original fans mounted in pull)- 3 fans Corsair 120 ML PRO in push.
MOBO Gygabyte Z390 Aorus Master- Corsair RGB Vengeance 64GB RAM (4x16) CL16
Nvidia Quadro RTX 4000
Samsung 970 EVO PLUS 1TB (unit C). Samsung 970 PRO 512GB (for data)
Power Supply: Corsair TX850M PLUS


Descubre mi programa VisorNET para Civil 3D:
https://apps.autodesk.com/CIV3D/es/Detail/Index?id=appstore.exchange.autodesk.com%3avisornet_windows32and64%3aes
0 Likes
Accepted solutions (1)
2,469 Views
7 Replies
Replies (7)
Message 2 of 8

joantopo
Mentor
Mentor
Accepted solution

OK, with this works.

 

  doc.CommandWillStart += OnCommandWillStart;
            doc.CommandEnded += OnCommandEnded;
            doc.CommandCancelled += OnCommandEnded;
            doc.CommandFailed += OnCommandEnded;

  static void OnCommandWillStart(object sender, CommandEventArgs e)
        {
            string comando = e.GlobalCommandName.ToLower();

           if (comando.Contains("save"))
            {
                variables_globales.GuardadoDibujo = true;
            }
        }

       static void OnCommandEnded(object sender, CommandEventArgs e)
       {
           string comando = e.GlobalCommandName.ToLower();
           if (e.GlobalCommandName.Contains("save"))
           {
               variables_globales.GuardadoDibujo = false;
           }
       }

I have a bool global var ( GuardadoDibujo) which is true or false (if the command is not a command related to Save operation), this way, I reject the ObjectModified event if this bool var is false.

 

However, I have to add a Document closed event because if you press the button (YES) in the dialog box when you close a drawing file, any command about save is called.

Autocad C3D 2019 SP3, 2020 & 2021
Intel I9 9900K with frontal watercooler alphacool eisbaer 360 (original fans mounted in pull)- 3 fans Corsair 120 ML PRO in push.
MOBO Gygabyte Z390 Aorus Master- Corsair RGB Vengeance 64GB RAM (4x16) CL16
Nvidia Quadro RTX 4000
Samsung 970 EVO PLUS 1TB (unit C). Samsung 970 PRO 512GB (for data)
Power Supply: Corsair TX850M PLUS


Descubre mi programa VisorNET para Civil 3D:
https://apps.autodesk.com/CIV3D/es/Detail/Index?id=appstore.exchange.autodesk.com%3avisornet_windows32and64%3aes
0 Likes
Message 3 of 8

Anonymous
Not applicable

You can also work exclusively with event handlers...

 

As a general guide, the scheme may be:

 

When your application starts, you can attach an handler when documents are added to the DocumentManager (new or open) with

 

Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(DocumentManager_DocumentCreated);

Inside this handler, you can attach other specific document events handler, as for your needs, like:

 

void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
{
    if (e.Document != null)
    {
        e.Document.Database.BeginSave += new DatabaseIOEventHandler(Database_BeginSave);

        e.Document.Database.SaveComplete += new DatabaseIOEventHandler(Database_SaveComplete);

        e.Document.BeginDocumentClose += new DocumentBeginCloseEventHandler(doc_BeginDocumentClose);
     }
}

These events will be called for user commands, user interaction (closing application), system shutdown or auto-saving...

Inside these events you may check the application variable CMDNAMES to check if a user command has run and which, or you may check the filename if contains '.sv$', meaning it's an autosave...

 

 

 

 

 

Message 4 of 8

koan.stevenson
Explorer
Explorer

i'm trying to find out when the REFEDIT has finished (that is when the user edits the reference and then the changes are saved). the reason is we would like to pop up a message from our software to prompt the user to check in the file after the save.

 

i've used the below snippet to catch the event

reference.Database.SaveComplete += new AcDB.DatabaseIOEventHandler(Xref_SaveComplete);

 

but it takes a long time for my handler to get hit (the last time i tried it was 10 minutes after i clicked save changes). is there a quicker way i can get this information back?

0 Likes
Message 5 of 8

m.cicognani
Contributor
Contributor

10 minutes you say? maybe that's an autosave call...

I'd go for monitoring the CommandStart/CommandEnd event, and maybe check the external xref datetime to check if it's been actually saved...

Message 6 of 8

koan.stevenson
Explorer
Explorer

thanks for the quick reply. it sounds like a good way to try it, i'll put your suggestions in our ticket. i think we're going to shelve this one anyway. the guidelines say not to attempt 'interactive functions within an event handler' so we'll try to stick to stuff we have better control over for now.

0 Likes
Message 7 of 8

m.cicognani
Contributor
Contributor

yes, maybe it's not allowed to execute lengthy procedures inside an event, but AutoCAD is quite tolerant to multithreading and you may open a modeless form, or maybe you can run a custom command of your own through sending a string to the command window... both ways are non-blocking and the event handler will end smoothly...

There are quite a number of options indeed...

Message 8 of 8

koan.stevenson
Explorer
Explorer

good to know!

0 Likes