Work With ObjectIds (.NET)

Each object contained within a database is assigned several unique ids. The unique ways you can access objects are:

The most common way to access an object is by its ObjectId. ObjectIds work well if your projects utilize both COM interop and the managed AutoCAD .NET API. If you create custom AutoLISP functions, you may need to work with entity handles.

Handles are persistent between AutoCAD sessions, so they are the best way of accessing objects if you need to export drawing information to an external file which might later need to be used to update the drawing. The ObjectId of an object in a database exists only while the database is loaded into memory. Once the database is closed, the ObjectId assigned to an object no longer exist and maybe different the next time the database is opened.

Obtain an ObjectId

As you work with objects, you will need to obtain an ObjectId first before you can open the object to query or edit it. An ObjectId is assigned to an existing object in the database when the drawing file is opened, and new objects are assigned an ObjectId when they are first created. An ObjectId is commonly obtained for an existing object in the database by:

Open an Object

Once an Object Id is obtained, the GetObject function is used to open the object assigned the given Object Id. An object can be opened in one of the following modes:

You should open an object in the mode that is best for the situation in which the object will be accessed. Opening an object for write introduces additional overhead than you might need due to the creation of undo records. If you are unsure if the object you are opening is the one you want to work with, you should open it for read and then upgrade the object from read to write mode. For more information on upgrading an object, see "Upgrade and Downgrade Open Objects (.NET)."

Both the GetObject and Open functions return an object. When working with some programming languages, you will need to cast the returned value based on the variable the value is being assigned to. If you are using VB.NET, you do not need to worry about casting the return value as it is done for you.

When working with the Dynamic Runtime Language (DLR), you do not need to worry about opening an object for read or write. The opening of an object is handled transparently for you, and so is the process of committing the changes made to an object without using transactions.

The following examples show how to obtain the LayerTableRecord for Layer Zero of the current database.

VB.NET

The following example manually disposes of the transaction after it is no longer needed.

Dim acCurDb As Document = Application.DocumentManager.MdiActiveDocument.Database
Dim acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
 
Dim acLyrTblRec As LayerTableRecord
acLyrTblRec = acTrans.GetObject(acCurDb.LayerZero, OpenMode.ForRead)
 
acTrans.Dispose()

The following example uses the Using statement to dispose of the transaction after it is no longer needed. The Using statement is the preferred coding style.

Dim acCurDb As Document = Application.DocumentManager.MdiActiveDocument.Database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
    Dim acLyrTblRec As LayerTableRecord
    acLyrTblRec = acTrans.GetObject(acCurDb.LayerZero, OpenMode.ForRead)
End Using

C#

The following example manually disposes of the transaction after it is no longer needed.

Document acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
Transaction acTrans = acCurDb.TransactionManager.StartTransaction();
 
LayerTableRecord acLyrTblRec;
acLyrTblRec = acTrans.GetObject(acCurDb.LayerZero,
                                OpenMode.ForRead) as LayerTableRecord;
 
acTrans.Dispose();

The following example uses the Using statement to dispose of the transaction after it is no longer needed. The Using statement is the preferred coding style.

Document acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
    LayerTableRecord acLyrTblRec;
    acLyrTblRec = acTrans.GetObject(acCurDb.LayerZero,
                                    OpenMode.ForRead) as LayerTableRecord;
}