CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

嵌套事务 (.NET)

2023-1-1 15:15| 发布者: admin| 查看: 349| 评论: 0|来自: AutoCAD

事务可以一个嵌套在另一个事务中。您可能有一个外部事务来撤消例程所做的所有更改,而内部事务则只能撤消所做的部分更改。当您使用嵌套事务时,您从一个顶级事务开始,该事务也是最外层的事务。

当您开始新交易记录时,它们将添加到上一个交易记录中。嵌套事务必须按照与创建顺序相反的顺序提交或中止。因此,如果您有三笔交易,则必须在第二笔交易之前关闭第三笔或最里面的一笔,最后关闭第一笔。如果中止第一个事务,则所有三个事务所做的更改都将撤消。

下图显示了事务在嵌套时的显示方式。

使用嵌套事务创建和修改对象

下面的示例演示如何使用三个事务创建 aandobject,然后更改其颜色。圆圈的颜色在第二个和第三个事务中更改,但由于第三个事务中止,因此只有第一个和第二个事务中所做的更改将保存到数据库中。此外,在创建和关闭活动事务时,活动事务数将打印在“命令行”窗口中。CircleLine

VB.NET

Imports 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();
    }
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 13:20

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部