The Transaction Manager is accessed from the TransactionManager property of the current database. Once a reference to the Transaction Manager is made, you use the StartTransaction method to start a new transaction. StartTransaction creates an instance of a Transaction object and allows you to open objects with the GetObject method.
事务管理器是从当前 Database 的 TransactionManager 属性访问的。一旦对事务管理器进行了引用,就可以使用 StartTransaction 方法启动一个新事务。StartTransaction 会创建一个 Transaction 对象的实例并允许用户使用 GetObject 方法打开对象。
All open objects opened during a transaction are closed at the end of the transaction. To end a transaction, call the Dispose method of a transaction object. If you use the Using and End Using keywords to indicate the start and end of a transaction, you do not need to call the Dispose method.
在事务结束的时候,所有在事务打开期间打开的对象都会被关闭。若要结束事务,请调用 Transaction 对象的 Dispose 方法。如果利用关键字 Using 和 End 表示事务的启动和结束,就不需要调用 Dispose 方法。
Prior to disposing of a transaction, you should commit any changes made with the Commit method. If the changes are not committed before a transaction is disposed, any changes made are rolled back to the state their were in prior to the start of the transaction. For more information on committing or rolling back changes made in a transaction, see Commit and Rollback Changes.
在 Transaction 销毁前,应用使用 Commit 方法提交所有的更改。如果 Transaction 在销毁前更改没有被提交,所有的修改都会回滚到先前 Transaction 启动时状态。更多关于提交和回滚修改的详细信息,请参见 提交与回滚更改 。
More than one transaction can be started. The number of active transactions can be retrieved with the NumberOfActiveTransactions property of the TransactionManager object while the top most or latest transaction can be retrieved with the TopTransaction property.
同时可以启动多个事务。活动事务的数量可以通过检索 TransactionManager 对象的 NumberOfActiveTransactions 属性得到,而最近创建的事务可以通过 TopTransaction 属性检索到。
Transactions can be nested one inside of another in order to rollback some of the changes made during the execution of a routine. For more information on working with multiple transactions or nesting transactions, see Nest Transactions.
事务可以由一个嵌套到另一个中,以回滚某些程序执行期间所做的修改。更多关于使用多个事务或嵌套事务的详细信息,请参见 嵌套事务。
The following example demonstrates how to open and read objects within using a transaction. You use the GetObject method to first open the BlockTable and then the Model space record.
下面示例演示如何在事务内部打开和读取对象。用户使用 GetObject 方法首先打开 BlockTable ,然后打模型空间记录。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("OpenTransactionManager")> _
Public Sub OpenTransactionManager()
'' 获得当前文档和数据库 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
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("OpenTransactionManager")]
public static void OpenTransactionManager()
{
// 获得当前文档和数据库 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
}
}
The following example demonstrates how to add a circle object to the database with in a transaction. You use the GetObject method to first open the BlockTable for read and then the Model space record for write. After Model space is opened for write, you use the AppendEntity and AddNewlyCreatedDBObject function to append the new Circle object to Model space as well as the transaction.
下面的示例演示如何在一个事务中向数据库中添加一个圆对象。用户使用 GetObject 方法首先以只读方式打开块表,然后以可写方式打开模型空间块表记录。以可写方式打开模型空间后,然后利用 AppendEntity 和 AddNewlyCreatedDBObject 追加一个新的圆对象到模型空间以及事务中。
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.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 write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 创建一个半径为3圆心在5,5的圆 Create a circle with a radius of 3 at 5,5
Dim acCirc As Circle = New Circle()
acCirc.SetDatabaseDefaults()
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()
End Using
End Sub
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.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 write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 创建一个半径为3圆心在5,5的圆 Create a circle with a radius of 3 at 5,5
Circle acCirc = new Circle();
acCirc.SetDatabaseDefaults();
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();
}
}