Document GUID

Document GUID

drobertsY4GL3
Enthusiast Enthusiast
1,030 Views
3 Replies
Message 1 of 4

Document GUID

drobertsY4GL3
Enthusiast
Enthusiast

My plugin currently uses the document name + object handle as an identifier when saving records to an SQL database. I would like to use a more robust document id than the name, so that the document is seen as the same, even if it is moved to a different location, or if the file is renamed. 

 

I have tried using the document hash, but this changes based on where the document is located. Same with the document.Database.FingerprintGuid.

 

Would creating a custom document property like myDocumentGUID be the best way to go?

 

0 Likes
Accepted solutions (2)
1,031 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

You might consider embed an GUID value in the drawing's named dictionary (of course create your own named Dictionary underneath the drawing's named dictionary).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

daniel_cadext
Advisor
Advisor
Accepted solution

DWGPROPS, summary section, keywords or comments, or custom area is easy to read
XDATA attached to the 0 layer would be easy to access as well

 

In ARX FingerprintGuid has a set and get, maybe you can assign the GUID, but then it might be a race between who sets it first, you or autocad.

 

I always thought FingerprintGuid never changed... odd

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
Message 4 of 4

drobertsY4GL3
Enthusiast
Enthusiast
Accepted solution

Thank you daniel and norman. Your suggestions worked great!

Below is the implementation I used.

 

ViewModel:

 

 

 

 // Check document for GUID custom propery and assign if missing.
documentGUID = Utilities.GetDocumentGUID();
if(documentGUID == null)
{
    Utilities.SetDocumentGUID();
    var customizationSection = new Autodesk.AutoCAD.Customization.CustomizationSection();
    // Save the document after assigning a custom GUID property.
    customizationSection.Save();
    documentGUID = Utilities.GetDocumentGUID();
}

 

 

 

Helper Methods:

 

 

 

 public static string GetDocumentGUID()
{
    // Check if custom property exists.
    var document = Application.DocumentManager.MdiActiveDocument;

    System.Collections.IDictionaryEnumerator customProperties = document.Database.SummaryInfo.CustomProperties;

    while(customProperties.MoveNext())
    {
        var key = customProperties.Key.ToString();
        if(key == "DOCUMENT_GUID")
        {
            return customProperties.Value.ToString();
        }
    }

    return null;
}
 public static void SetDocumentGUID()
{
    // Create custom propery
    var document = Application.DocumentManager.MdiActiveDocument;
    var infoBuilder = new DatabaseSummaryInfoBuilder();
    var GUID = System.Guid.NewGuid().ToString();
    infoBuilder.CustomPropertyTable.Add("DOCUMENT_GUID", GUID);

    using (DocumentLock docLock = document.LockDocument())
    {
        document.Database.SummaryInfo = infoBuilder.ToDatabaseSummaryInfo(); 
    }
}

 

 

 

 

More info on creating custom properties:
https://forums.autodesk.com/t5/net/create-document-custom-property/td-p/11763555