Search code examples
c#handleautocadobjectarx

How to deal with serialization and Handles in AutoCAD


I want to serialize blockreference handle in an xml file and (its properties). So I store this value at the initialization :

blockReference.ObjectId.Handle.Value; // decimal value = 10658

But when I select the blockReference in AutoCAD the handle has changed.

private void database_ObjectModified(object sender, ObjectEventArgs e)
{
    long currentId = e.DBObject.ObjectId.Handle.Value;  // Now it's 10659 !!!!

    ...
}

Do I use handle no correctly ?


Solution

  • When you use the Handle in the XML file you need to get the ObjectId by its .Handle and then use ObjectId that is valid only in the current session to read/write the object. See http://through-the-interface.typepad.com/through_the_interface/2007/02/getting_access_.html

    Look for the Database.GetObjectId() method in the SDK docs (the managed interface CHM file). This is it in a nutshell...

    public ObjectId GetObjectId(
    [MarshalAs(UnmanagedType.U1)] bool createIfNotFound, 
    Handle objHandle, 
    int identifier
    );
    
    • [MarshalAs(UnmanagedType.U1)] bool createIfNotFound Input Boolean indicating to create a objectId stub if input handle is not found
    • Handle objHandle Input Handle object containing the handle being passed in
    • int identifier Reserved for future use