トランザクションを使用すると複数のオブジェクトを開いて作業することが簡単になりますが、オブジェクトを開いて編集する方法はこれだけではありません。トランザクションを使用する以外にも、Open および Close メソッドを使ってオブジェクトを開き、閉じることができます。ただし、Open メソッドを使用するには、オブジェクト ID を取得する必要があります。トランザクションで使用される GetObject メソッドと同様にオープン モードを指定する必要があり、戻り値はオブジェクトになります。
Open メソッドを使って開いた後にオブジェクトに変更を加えた場合、Cancel メソッドを使用すると、オブジェクトを開いてから加えたすべての変更をロールバックすることができます。ロールバックする各オブジェクトで、Cancel を呼び出す必要があります。また、オブジェクトを閉じた後、Dispose メソッドを使用してオブジェクトを正しく破棄する必要があります。Using ステートメントを使用して、オブジェクトを閉じて破棄することもできます。
1 つのオブジェクトを操作する必要がある場合は、Open および Close メソッドを使用すると、トランザクション マネージャを使用する場合と比較して、記述する必要があるコードの行数を減らすことができます。ただし、オブジェクトを開いて閉じるには、トランザクションを使用する方法をお勧めします。
次の例では、トランザクションと GetObject メソッドを使用せずにオブジェクトを開き、閉じる方法について説明します。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("OpenCloseObjectId")]
public static void OpenCloseObjectId()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Open the Block table for read
BlockTable acBlkTbl = null;
try
{
acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for read
BlockTableRecord acBlkTblRec = null;
try
{
acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForRead) as BlockTableRecord;
// Step through the Block table record
foreach (ObjectId acObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage("\nDXF name: " + acObjId.ObjectClass.DxfName);
acDoc.Editor.WriteMessage("\nObjectID: " + acObjId.ToString());
acDoc.Editor.WriteMessage("\nHandle: " + acObjId.Handle.ToString());
acDoc.Editor.WriteMessage("\n");
}
}
catch (Autodesk.AutoCAD.Runtime.Exception es)
{
System.Windows.Forms.MessageBox.Show(es.Message);
}
finally
{
// Close the Block table
if (!acBlkTblRec.ObjectId.IsNull)
{
// Close the Block table record
acBlkTblRec.Close();
acBlkTblRec.Dispose();
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception es)
{
System.Windows.Forms.MessageBox.Show(es.Message);
}
finally
{
// Close the Block table
if (!acBlkTbl.ObjectId.IsNull)
{
acBlkTbl.Close();
acBlkTbl.Dispose();
}
}
}
次の例では、手動でオブジェクトを閉じて破棄する代わりに、Using ステートメントを使用してオブジェクトを開いて閉じる方法を示します。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("OpenCloseObjectIdWithUsing")]
public static void OpenCloseObjectIdWithUsing()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Open the Block table for read
using (BlockTable acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
{
// Open the Block table record Model space for read
using (BlockTableRecord acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForRead)
as BlockTableRecord)
{
// Step through the Block table record
foreach (ObjectId acObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage("\nDXF name: " + acObjId.ObjectClass.DxfName);
acDoc.Editor.WriteMessage("\nObjectID: " + acObjId.ToString());
acDoc.Editor.WriteMessage("\nHandle: " + acObjId.Handle.ToString());
acDoc.Editor.WriteMessage("\n");
}
// Close the Block table record
}
// Close the Block table
}
}
この例では、新しいオブジェクトを作成し、トランザクション マネージャを使用せずにモデル空間に追加する方法を示します。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("AddNewCircleOpenClose")]
public static void AddNewCircleOpenClose()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Open the Block table for read
using (BlockTable acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
{
// Open the Block table record Model space for write
using (BlockTableRecord acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForWrite)
as BlockTableRecord)
{
// Create a circle with a radius of 3 at 5,5
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(5, 5, 0);
acCirc.Radius = 3;
// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acCirc);
// Close and dispose the circle object
}
// Close the Block table record
}
// Close the Block table
}
}