Once an object is opened using either the GetObject or Open methods, you can change the current open mode of an object with the UpgradeOpen and DowngradeOpen methods. The UpgradeOpen method changes an object open for read to write mode, while DowngradeOpen changes an object open for write to read mode. You do not need to pair a call to DowngradeOpen with each UpgradeOpen call, since closing of an object or disposing of a transaction will sufficiently cleanup the open state of an entity.
一旦对象使用 GetObject 或 Open 方法中的任一个被打开,就可以用 UpgradeOpen 和 DowngradeOpen 方法修改当前对象的打开模式。UpgradeOpen 方法将对象的打开模式由只读变为可写,而 DowngradeOpen 将对象的打开模式由可写变为只读。不需要成对调用 DowngradeOpen 和 UpgradeOpen,因为对象的关闭和事务的销毁将充分地清除打开图元的状态。
When you go to open an object, open the object in the mode that you will use the object in. Do not just open an object for write when you might only need to query an object. It is more efficent to open an object for read and query the object’s properties than it is to open the object for write and query the object’s properties.
当用户确定打开一个对象时,使用你想用于对象的模式打开对象。当你仅需要查询对象时不应以可写方式打开对象。以只读方式打开对象并查询对象的属性比以可写方式打开对象并查询对象的属性更有效率。
Opening an object for write causes undo filing to start for the object. Undo filing is used to track changes to an object, so any changes made can be rolled back. If you are uncertain if you need to modify an object, it is best to open an object for read and then upgrade it for write. This will help to reduce the overhead of your program.
以可写方式打开一个对象将为对象启动一个撤销编档。撤销编档用于跟踪对象的修改,因此任何已做修改都可以回滚。如果不确定是否需要修改对象,最好的方法是以只读方式打开对象然后需要时升级为可写模式。这样将帮助你的程序减小开销。
An example of when you might use UpgradeOpen is when you might be querying objects to see if they match a specific condition, and if the condition is met then you would upgrade the object from read to write mode to make modifications to it.
一个当用户可能使用 UpgradeOpen 例子是,当用户查询对象看它是否符合特定条件,而且如果条件满足,那么你可以将对象模式从只读升级为可写模式以便修改它。
Similarly, if an object is open for notify and you receive a notification, you use UpgradeFromNotify to upgrade the object for write. Then you would use DowngradeToNotify to downgrade the object back to notify. UpgradeFromNotify and DowngradeFromNotify are reserved for use in methods that are intended to be used by an object to change its own open status so that it can safely modify itself.
同样的,如果一个对象是以通知的方式打开的并且获得了一个通知,就应该使用 UpgradeFromNotify 升级对象为可写模式。然后你应该使用 DowngradeToNotify 降级对象为通知模式。UpgradeFromNotify 和 DowngradeFromNotify 故意被保留是为了通过对象修改它自己的打开状态,所以它可以安全的修改它自己。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("FreezeDoorLayer")> _
Public Sub FreezeDoorLayer()
'' 获得当前文档和数据库 Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' 以只读方式打开图层表 Open the Layer table for read
Dim acLyrTbl As LayerTable
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
'' 遍历每个图层并更些那些以"Door"开始的图层 Step through each layer and update those that start with 'Door'
For Each acObjId As ObjectId In acLyrTbl
'' 以只读模式打开图层表记录 Open the Layer table record for read
Dim acLyrTblRec As LayerTableRecord
acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead)
'' 检查图层名是否以"Door"开头 Check to see if the layer's name starts with 'Door'
If (acLyrTblRec.Name.StartsWith("Door", _
StringComparison.OrdinalIgnoreCase) = True) Then
'' 检查图层是否是当前图层,如果是这样的话,不要冻结它 Check to see if the layer is current, if so then do not freeze it
If acLyrTblRec.ObjectId <> acCurDb.Clayer Then
'' 将模式从只读变为可写 Change from read to write mode
acLyrTblRec.UpgradeOpen()
'' 冻结图层 Freeze the layer
acLyrTblRec.IsFrozen = True
End If
End If
Next
'' 提交修改并销毁事务 Commit the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("FreezeDoorLayer")]
public static void FreezeDoorLayer()
{
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只读方式打开图层表 Open the Layer table for read
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;
// 遍历每个图层并更些那些以"Door"开始的图层 Step through each layer and update those that start with 'Door'
foreach (ObjectId acObjId in acLyrTbl)
{
// 以只读模式打开图层表记录 Open the Layer table record for read
LayerTableRecord acLyrTblRec;
acLyrTblRec = acTrans.GetObject(acObjId,
OpenMode.ForRead) as LayerTableRecord;
// 检查图层名是否以"Door"开头 Check to see if the layer's name starts with 'Door'
if (acLyrTblRec.Name.StartsWith("Door",
StringComparison.OrdinalIgnoreCase) == true)
{
// 检查图层是否是当前图层,如果是这样的话,不要冻结它 Check to see if the layer is current, if so then do not freeze it
if (acLyrTblRec.ObjectId != acCurDb.Clayer)
{
// 将模式从只读变为可写 Change from read to write mode
acLyrTblRec.UpgradeOpen();
// 冻结图层 Freeze the layer
acLyrTblRec.IsFrozen = true;
}
}
}
// 提交修改并销毁事务 Commit the changes and dispose of the transaction
acTrans.Commit();
}
}