開いたオブジェクトをアップグレード、ダウングレードする(.NET)

オブジェクトの現在のオープン モードは、オブジェクトをアップグレードまたはダウングレードすることによって、読み込みから書き込み、または書き込みから読み込みに変更することができます。

次のメソッドを使用して、以前に開いたオブジェクトをアップグレードまたはダウングレードできます。

オブジェクトの使用方法に最適なモードを使用してオブジェクトを開くことをお勧めします。たとえば、オブジェクトを書き込み用に開いてオブジェクトのプロパティを照会するよりも、オブジェクトを読み込み用に開いてオブジェクトのプロパティを照会する方が効率的です。オブジェクトを修正する必要があるかどうかが不明な場合は、オブジェクトを読み込み用に開いてから書き込み用にアップグレードすることをお勧めします。これによって、プログラムのオーバーヘッドを軽減できます。

C# の例

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;

        // 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;

            // 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
                    acTrans.GetObject(acObjId, OpenMode.ForWrite);

                    // Freeze the layer
                    acLyrTblRec.IsFrozen = true;
                }
            }
        }

        // Commit the changes and dispose of the transaction
        acTrans.Commit();
    }
}