事务管理器是从当前数据库的属性访问的。引用事务管理器后,可以使用下列方法之一启动或获取事务:TransactionManager
获得 or 对象后,使用该方法打开存储在数据库中的对象以进行读取或写入。该方法返回一个可以强制转换为它所表示的实际对象类型。TransactionOpenCloseTransactionGetObjectGetObjectDBObject 在事务结束时打开的所有打开对象都将关闭。若要结束事务,请调用事务对象的方法。如果使用 和 关键字来指示事务的开始和结束,则无需调用该方法。DisposeUsingEnd UsingDispose 在处理事务之前,应提交使用该方法所做的任何更改。如果在释放事务之前未提交更改,则所做的任何更改都将回滚到事务开始前的状态。Commit 可以启动多个事务。可以使用对象的属性检索活动事务的数量,而可以使用该属性检索最顶部或最新的事务。NumberOfActiveTransactionsTransactionManagerTopTransaction 事务可以嵌套在另一个事务中,以便回滚在例程执行期间所做的某些更改。 查询对象下面的示例演示如何使用事务打开和读取其中的对象。使用该方法首先打开模型空间记录,然后打开模型空间记录。GetObjectBlockTable VB.NETImports 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 } } 向数据库添加新对象下面的示例演示如何在事务中向数据库添加 circle 对象。使用该方法首先打开 BlockTable 进行读取,然后打开模型空间记录进行写入。打开模型空间进行写入后,使用 and 函数将新的 Circle 对象追加到模型空间以及事务中。GetObjectAppendEntityAddNewlyCreatedDBObject VB.NETImports 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(); } } 父主题: |
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-1-8 19:35
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.