CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

使用事务访问和创建对象 (.NET)

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

事务管理器是从当前数据库的属性访问的。引用事务管理器后,可以使用下列方法之一启动或获取事务:TransactionManager

  • StartTransaction– 通过创建对象的新实例来启动新事务。当您需要多次写入对象并控制如何使用不同的嵌套级别回滚更改时,请使用此方法。Transaction
  • StartOpenCloseTransation– 创建一个对象,其行为类似于对象,它包装对象的 and 方法,从而更容易关闭所有打开的对象,而不必显式关闭每个打开的对象。建议在可能调用未知次数的支持或实用工具函数中使用,并在使用大多数事件处理程序时使用。OpenCloseTransactionTransactionOpenClose

拥有主动脉后,使用该方法打开存储在数据库中的对象以进行读取或写入。该方法返回可以强制转换为它所表示的实际对象类型。TransactionOpenCloseTransactionGetObjectGetObjectDBObject

在事务期间打开的所有打开的对象都将在事务结束时关闭。若要结束事务,请调用事务对象的方法。如果使用 theand关键字来指示事务的开始和结束,则无需调用该方法。DisposeUsingEnd UsingDispose

在释放事务之前,您应该提交使用该方法所做的任何更改。如果在释放事务之前未提交更改,则所做的任何更改都将回滚到事务开始之前的状态。Commit

可以启动多个事务。活动事务的数量可以使用对象的属性检索,而最顶层或最新的事务可以使用属性检索。NumberOfActiveTransactionsTransactionManagerTopTransaction

事务可以一个嵌套在另一个内部,以便回滚在例程执行期间所做的一些更改。

查询对象

下面的示例演示如何使用事务打开和读取 中的对象。使用该方法首先打开 和 然后打开模型空间记录。GetObjectBlockTable

VB.NET

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

C#

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
    }
}

Add a new object to the database

The following example demonstrates how to add a circle object to the database with in a transaction. You use the 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 and function to append the new Circle object to Model space as well as the transaction. GetObjectAppendEntityAddNewlyCreatedDBObject

VB.NET

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

C#

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

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 12:26

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部