使用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.

大多数公共方法都是通过 ObjectID 访问对象的。如果你的工程使用了COm Interop 和托管 .NET API,ObjectID 也可以很好的工作。如果创建了自定义的 AutoLISP 函数,就可以需要使用图元句柄了。

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.

句柄在 AutoCAD 进程之间是不变的,如果需要输出图形信息到外部文件,可能稍后又需要用它更新图形,那么使用句柄是访问对象最好的方式。数据库中对象的 ObjectID 仅当数据库被加载到内存中后才存在。一旦数据库关闭了,对象的 ObjectID 就不再存在并且下次数据库再打开时,同一对象的 ObjectID 可能会不同了。

获取对象 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:

当使用对象时,打开对象用于查询和编辑它之前,首先必须获得对象的ID。当图形打开后,一个对象 ID 被赋值给数据库中的一个现有对象,一个新对象在它们首次创建时被赋值对象 ID。对象 ID 通常用于获取数据库中的现有对象:

打开对象

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:

只要获得了对象 ID,GetObject 函数就可以根据特定的对象 ID 打开指定对象。一个对象可以用以下几种模式打开:

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.

用户应该以这一种模式打开一个对象,这种模式使访问这个对象有最好的状态。以可写方式打开对象比创建一个可撤消记录需要更多额外的开销。如果你不确定对象是否是你想打开后用于操作的对象,就应该以只读方式打开,然后使用 UpgradeOpen 方法把模式从只读模式升级为可写模式。更多关于使用 UpgradeOpen 函数的详细信息,请参见 升级和降级打开的对象

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:

GetObjectOpen 函数都返回一个对象。在一些程序语言中,需要根据被赋值的变量值转换返回值。如果使用的是 VB.NET,就不需要担心转换返回值,因为它自己做这么做。下面示例展示为当前数据库的零层获得 LayerTableRecord。

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.

下面的示例使用 Using 语句在事务不再需要后销毁。Using 语句是首选的代码样式。

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.

下面的示例使用 Using 语句在事务不再需要后销毁。Using 语句是首选的代码样式。

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