事务可以嵌套在另一个事务中。您可能有一个外部事务来撤消例程所做的所有更改,而内部事务则仅撤消所做的部分更改。使用嵌套事务时,从顶部事务开始,该事务也是最外层的事务。 当您开始新事务时,它们将添加到以前的事务中。嵌套事务的提交或中止顺序必须与创建它们的顺序相反。因此,如果您有三笔交易,则必须在第二笔交易之前关闭第三笔或最里面的一笔交易,最后是第一笔交易。如果中止第一个事务,则撤消所有三个事务所做的更改。 下图显示了事务在嵌套时的显示方式。 ![]() 使用嵌套事务创建和修改对象下面的示例演示如何使用三个事务创建一个 and 对象,然后更改它们的颜色。圆圈的颜色在第二个和第三个事务中更改,但由于第三个事务被中止,因此只有在第一和第二个事务中所做的更改保存到数据库中。此外,活动事务的数量在创建和关闭时会打印在命令行窗口中。CircleLine VB.NETImports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
<CommandMethod("NestedTransactions")> _
Public Sub NestedTransactions()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Create a reference to the Transaction Manager
Dim acTransMgr As Autodesk.AutoCAD.DatabaseServices.TransactionManager
acTransMgr = acCurDb.TransactionManager
'' Create a new transaction
Using acTrans1 As Transaction = acTransMgr.StartTransaction()
'' Print the current number of active transactions
acDoc.Editor.WriteMessage(vbLf & "Number of transactions active: " & _
acTransMgr.NumberOfActiveTransactions.ToString())
'' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans1.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans1.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)
acTrans1.AddNewlyCreatedDBObject(acCirc, True)
'' Create the second transaction
Using acTrans2 As Transaction = acTransMgr.StartTransaction()
acDoc.Editor.WriteMessage(vbLf & "Number of transactions active: " & _
acTransMgr.NumberOfActiveTransactions.ToString())
'' Change the circle's color
acCirc.ColorIndex = 5
'' Get the object that was added to Transaction 1 and set it to the color 5
Using acLine As Line = New Line(New Point3d(2, 5, 0), New Point3d(10, 7, 0))
acLine.ColorIndex = 3
'' Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acLine)
acTrans2.AddNewlyCreatedDBObject(acLine, True)
End Using
'' Create the third transaction
Using acTrans3 As Transaction = acTransMgr.StartTransaction()
acDoc.Editor.WriteMessage(vbLf & "Number of transactions active: " & _
acTransMgr.NumberOfActiveTransactions.ToString())
'' Change the circle's color
acCirc.ColorIndex = 3
'' Update the display of the drawing
acDoc.Editor.WriteMessage(vbLf)
acDoc.Editor.Regen()
'' Request to keep or discard the changes in the third transaction
Dim pKeyOpts As PromptKeywordOptions = New PromptKeywordOptions("")
pKeyOpts.Message = vbLf & "Keep color change "
pKeyOpts.Keywords.Add("Yes")
pKeyOpts.Keywords.Add("No")
pKeyOpts.Keywords.Default = "No"
pKeyOpts.AllowNone = True
Dim pKeyRes As PromptResult = acDoc.Editor.GetKeywords(pKeyOpts)
If pKeyRes.StringResult = "No" Then
'' Discard the changes in transaction 3
acTrans3.Abort()
Else
'' Save the changes in transaction 3
acTrans3.Commit()
End If
'' Dispose the transaction
End Using
acDoc.Editor.WriteMessage(vbLf & "Number of transactions active: " & _
acTransMgr.NumberOfActiveTransactions.ToString())
'' Keep the changes to transaction 2
acTrans2.Commit()
End Using
End Using
acDoc.Editor.WriteMessage(vbLf & "Number of transactions active: " & _
acTransMgr.NumberOfActiveTransactions.ToString())
'' Keep the changes to transaction 1
acTrans1.Commit()
End Using
End Sub
C#using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[CommandMethod("NestedTransactions")]
public static void NestedTransactions()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Create a reference to the Transaction Manager
Autodesk.AutoCAD.DatabaseServices.TransactionManager acTransMgr;
acTransMgr = acCurDb.TransactionManager;
// Create a new transaction
using (Transaction acTrans1 = acTransMgr.StartTransaction())
{
// Print the current number of active transactions
acDoc.Editor.WriteMessage("\nNumber of transactions active: " +
acTransMgr.NumberOfActiveTransactions.ToString());
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans1.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans1.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);
acTrans1.AddNewlyCreatedDBObject(acCirc, true);
// Create the second transaction
using (Transaction acTrans2 = acTransMgr.StartTransaction())
{
acDoc.Editor.WriteMessage("\nNumber of transactions active: " +
acTransMgr.NumberOfActiveTransactions.ToString());
// Change the circle's color
acCirc.ColorIndex = 5;
// Get the object that was added to Transaction 1 and set it to the color 5
using (Line acLine = new Line(new Point3d(2, 5, 0), new Point3d(10, 7, 0)))
{
acLine.ColorIndex = 3;
// Add the new object to Model space and the transaction
acBlkTblRec.AppendEntity(acLine);
acTrans2.AddNewlyCreatedDBObject(acLine, true);
}
// Create the third transaction
using (Transaction acTrans3 = acTransMgr.StartTransaction())
{
acDoc.Editor.WriteMessage("\nNumber of transactions active: " +
acTransMgr.NumberOfActiveTransactions.ToString());
// Change the circle's color
acCirc.ColorIndex = 3;
// Update the display of the drawing
acDoc.Editor.WriteMessage("\n");
acDoc.Editor.Regen();
// Request to keep or discard the changes in the third transaction
PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
pKeyOpts.Message = "\nKeep color change ";
pKeyOpts.Keywords.Add("Yes");
pKeyOpts.Keywords.Add("No");
pKeyOpts.Keywords.Default = "No";
pKeyOpts.AllowNone = true;
PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);
if (pKeyRes.StringResult == "No")
{
// Discard the changes in transaction 3
acTrans3.Abort();
}
else
{
// Save the changes in transaction 3
acTrans3.Commit();
}
// Dispose the transaction
}
acDoc.Editor.WriteMessage("\nNumber of transactions active: " +
acTransMgr.NumberOfActiveTransactions.ToString());
// Keep the changes to transaction 2
acTrans2.Commit();
}
}
acDoc.Editor.WriteMessage("\nNumber of transactions active: " +
acTransMgr.NumberOfActiveTransactions.ToString());
// Keep the changes to transaction 1
acTrans1.Commit();
}
}
父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-29 17:10
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.