How to deal with serialization and Handles in AutoCAD

807 Views Asked by At

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 ?

2

There are 2 best solutions below

0
On BEST ANSWER

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
11
On

Use theEntityObj.Handle.Value

If you get if from the ObjectId, it will change when you close/open a drawing.

Note a Handle can change in some cases, for instance, if you have a LINE inside a block, if you BEDIT the block, the HANDLES will change.