Write data to the NOD before document closes?

Write data to the NOD before document closes?

nshupeFMPE3
Advocate Advocate
1,630 Views
5 Replies
Message 1 of 6

Write data to the NOD before document closes?

nshupeFMPE3
Advocate
Advocate

I'm trying to find what event would be the best to react to in in order to write data to the NOD before the document closes. I've tried reacting to 

Application.DocumentManager.DocumentDestroyed += new DocumentDestroyedEventHandler(docColDocDestroyed)


but get an error when trying to create a transaction. 

And when I try to react to

Document.BeginDocumentClose

 

I get an eLockedViolation. The violation comes from an external assembly that I cant change, and I need the assembly for backwards compatibility (I'm only doing this part for backwards compatibility.)

Any help would be greatly appreciated!


0 Likes
Accepted solutions (1)
1,631 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

Try to handle the DocumentToBeDestroyed event and, in the event handler, use an OpenCloseTransaction instead of a standard Transaction.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

nshupeFMPE3
Advocate
Advocate
that appears to have worked, thank you Sir!
0 Likes
Message 4 of 6

nshupeFMPE3
Advocate
Advocate

it appears that the Transaction completes without failing, but that the changes are not saved to the NOD as the changes do not appear when opening the file again.

0 Likes
Message 5 of 6

_gile
Consultant
Consultant

You have to save the document.

This works for me:

        private void Docs_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
        {
            var db = e.Document.Database;
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary dict;
                if (nod.Contains("FOO"))
                {
                    dict = (DBDictionary)tr.GetObject(nod.GetAt("FOO"), OpenMode.ForRead);
                }
                else
                {
                    dict = new DBDictionary();
                    nod.UpgradeOpen();
                    nod.SetAt("FOO", dict);
                    tr.AddNewlyCreatedDBObject(dict, true);
                }
                Xrecord xrec;
                if (dict.Contains("Bar"))
                {
                    xrec = (Xrecord)tr.GetObject(dict.GetAt("Bar"), OpenMode.ForWrite);
                }
                else
                {
                    xrec = new Xrecord();
                    if (!dict.IsWriteEnabled)
                        dict.UpgradeOpen();
                    dict.SetAt("Bar", xrec);
                    tr.AddNewlyCreatedDBObject(xrec, true);
                }
                xrec.Data = new ResultBuffer(new TypedValue(1, "baz"));
                tr.Commit();
            }
            dynamic acadDoc = e.Document.GetAcadDocument();
            acadDoc.Save();
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 6

SRSDS
Advisor
Advisor

Does this save the document on closing regardless of whether or not the user has chosen not to?

Your solution here is another which doesn't. https://forums.autodesk.com/t5/net/how-to-catch-save-quit-document/m-p/8321676#M60415

0 Likes