It is probably trivial question but what is different between property DBObject.ObjectId and DBObject.Id
Thanks,
Richard
Solved! Go to Solution.
Solved by khoa.ho. Go to Solution.
Both DBObject.ObjectId and DBObject.Id return to the same struct ObjectId, and have the same value.
The difference is how they get called unmanaged code behind .NET framework. If we decompile acdbmgd.dll, we will see the difference:
Decompiled code of DBObject.ObjectId:
// Autodesk.AutoCAD.DatabaseServices.DBObject class public unsafe ObjectId ObjectId { get { AcDbObjectId acDbObjectId; AcDbObjectId* ptr = <Module>.AcDbObject.objectId(this.GetImpObj(), &acDbObjectId); ObjectId result; result.m_id = *(long*)ptr; return result; } } // <Module> class [SuppressUnmanagedCodeSecurity] [DllImport("", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] [MethodImpl(MethodImplOptions.Unmanaged)] public unsafe static extern AcDbObjectId* objectId(AcDbObject*, AcDbObjectId*);
Decompiled code of DBObject.Id:
// Autodesk.AutoCAD.DatabaseServices.DBObject class public unsafe override ObjectId Id { get { AcDbObject* impObj = this.GetImpObj(); AcDbObjectId acDbObjectId; AcDbObjectId* ptr = calli(AcDbObjectId* modreq(System.Runtime.CompilerServices.IsUdtReturn) modopt(System.Runtime.CompilerServices.CallConvCdecl)(System.IntPtr,AcDbObjectId*), impObj, ref acDbObjectId, *(*(long*)impObj + 80L)); ObjectId result; result.m_id = *(long*)ptr; return result; } }
A managed code DBObject.ObjectId will call another unmanaged/unsafe code AcDbObject.objectId(). AcDbObject is a class of ObjectARX, which has method objectId() to return the pointer of memory address of the database-resident object.
A managed code DBObject.Id will call another unmanaged/unsafe code calli() which is I don't know, and is also not found on ObjectARX SDK. I guess it may be legacy code, even it returns same value of DBObject.ObjectId.
ObjectId is not value that AutoCAD saves to DWG file, it saves handles instead. A handle is a long number to identify an entity, so handle is unique inside one drawing. But AutoCAD is a MDI application. That means it can work with multiple drawing files at the same time. Then a handle is not unique among multiple opened drawings. To solve that, AutoCAD uses memory address to identify an entity, which is ObjectId. So every time we open a DWG file, an ObjectId of any entity will change, except the handle of this entity is still remained from reading file.
The benefit of ObjectId is to work for multiple drawings at the same time, as each entity has its own unique memory address. For that reason, we can copy any entity from one drawing to another with Ctrl-C and Ctrl-V inside AutoCAD.
Anyway, DBObject.ObjectId is official to identify an entity inside an AutoCAD host which may handle many opened drawings at the same time.
I still don't know if it is a need to use DBObject.Id, or it is redundant. As DBObject.ObjectId is good enough to represent an entity.
-Khoa