トランザクション マネージャには、現在のデータベースの TransactionManager プロパティからアクセスします。トランザクション マネージャへの参照が完了したら、次のいずれかのメソッドを使用してトランザクションを開始または取得できます。
Transaction オブジェクトまたは OpenCloseTransaction オブジェクトを作成したら、GetObject メソッドを使用して、読み取りまたは書き込みのためにデータベースに格納されているオブジェクトを開くことができます。GetObject メソッドは、オブジェクトが表す実際のオブジェクト型にキャストできる DBObject を返します。
トランザクション中に開かれたすべての開いたオブジェクトは、トランザクションの終了時に閉じられます。トランザクションを終了するには、トランザクション オブジェクトの Dispose メソッドを呼び出します。Using キーワードと End Using キーワードを使用してトランザクションの開始と終了を示す場合、Dispose メソッドを呼び出す必要はありません。
トランザクションを破棄する前に、Commit メソッドを使用して変更内容をコミットする必要があります。トランザクションを破棄する前に変更内容をコミットしないと、すべての変更内容はトランザクションの開始前の状態にロールバックされます。
複数のトランザクションを開始できます。アクティブなトランザクション数は TransactionManager オブジェクトの NumberOfActiveTransactions プロパティで取得できます。また、最上位または最新のトランザクションは TopTransaction プロパティで取得できます。
ルーチンの実行中に加えられた変更内容の一部をロールバックするために、トランザクションを別のトランザクション内にネストすることができます。
次の例は、トランザクションの使用時にオブジェクトを開いて読み取る方法を示します。GetObject メソッドを使用してまず BlockTable を開き、次にモデル空間レコードを開きます。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("StartTransactionManager")]
public static void StartTransactionManager()
{
// 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 Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for read
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
// Step through the Block table record
foreach (ObjectId asObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage("\nDXF name: " + asObjId.ObjectClass.DxfName);
acDoc.Editor.WriteMessage("\nObjectID: " + asObjId.ToString());
acDoc.Editor.WriteMessage("\nHandle: " + asObjId.Handle.ToString());
acDoc.Editor.WriteMessage("\n");
}
// Dispose of the transaction
}
}
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("StartTransactionManager")> _
Public Sub StartTransactionManager()
'' 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 Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' Open the Block table record Model space for read
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForRead)
'' Step through the Block table record
For Each acObjId As ObjectId In acBlkTblRec
acDoc.Editor.WriteMessage(vbLf & "DXF name: " & acObjId.ObjectClass().DxfName)
acDoc.Editor.WriteMessage(vbLf & "ObjectID: " & acObjId.ToString())
acDoc.Editor.WriteMessage(vbLf & "Handle: " & acObjId.Handle.ToString())
acDoc.Editor.WriteMessage(vbLf)
Next
'' Dispose of the transaction
End Using
End Sub
次の例は、トランザクション内のデータベースに Circle オブジェクトを追加する方法を示します。GetObject メソッドを使用してまず読み取り用に BlockTable を開き、次に書き込み用にモデル空間レコードを開きます。モデル空間を書き込みのために開いた後に、AppendEntity 関数と AddNewlyCreatedDBObject 関数を使用して、新しい Circle オブジェクトをモデル空間とトランザクションに追加します。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("AddNewCircleTransaction")]
public static void AddNewCircleTransaction()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartOpenCloseTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
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);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
}
// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("AddNewCircleTransaction")> _
Public Sub AddNewCircleTransaction()
'' 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.StartOpenCloseTransaction()
'' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' Create a circle with a radius of 3 at 5,5
Using acCirc As Circle = 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)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
End Using
'' Commit the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub