Work with ObjectIds
 
 
 

Each object contained with in the Database object is assigned several unique ids. The unique ways you can access objects are:

The most common method is to access an object by its Object Id. Object Ids work well if your projects utilize both COM interop and the managed .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 Object Ids assigned to an object no longer exist and maybe different the next time the database is opened.

Obtain an Object Id

As you work with objects, you will need to obtain an Object Id first before you can open the object to query or edit it. An Object Id is assigned to an existing object in the database when the drawing file is opened, and new objects are assigned Object Ids when they are first created. An Object ID 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 use the UpgradeOpen function to change from read to write mode. For more information on using the UpgradeOpen function, see Upgrade and Downgrade Open Objects.

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 returned value as it is done for you. The following examples show obtaining 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;
}