锁定和解锁文档 (.NET)
修改对象或访问 AutoCAD 的请求可以在任何上下文中发生,并且来自任意数量的应用程序。为防止与其他请求发生冲突,您有责任在修改文档之前锁定文档。在某些上下文中未能锁定文档将导致在修改数据库期间发生锁定冲突。您希望在应用程序出现以下情况时锁定文档:
例如,将实体添加到当前文档以外的文档中的模型或图纸空间时,需要锁定文档。使用要锁定的对象的方法。调用该方法时,将返回一个对象。LockDocumentDatabaseLockDocumentDocumentLock 修改完锁定的数据库后,需要解锁数据库。若要解锁数据库,请调用对象的方法。还可以将 Using 语句与对象一起使用,一旦 Using 语句结束,数据库就会解锁。DisposeDocumentLockDocumentLock 注意:在不使用 Session 命令标志的命令的上下文中工作时,无需在修改当前文档之前锁定该文档的数据库。
在修改对象之前锁定数据库本示例创建一个新文档,然后在其中绘制一个圆圈。创建文档后,新文档的数据库将被锁定,然后向其添加一个圆圈。添加圆圈后,数据库将解锁,关联的文档窗口将设置为当前窗口。 VB.NETImports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.Geometry <CommandMethod("LockDoc", CommandFlags.Session)> _ Public Sub LockDoc() '' Create a new drawing Dim acDocMgr As DocumentCollection = Application.DocumentManager Dim acNewDoc As Document = DocumentCollectionExtension.Add(acDocMgr, "acad.dwt") Dim acDbNewDoc As Database = acNewDoc.Database '' Lock the new document Using acLckDoc As DocumentLock = acNewDoc.LockDocument() '' Start a transaction in the new database Using acTrans As Transaction = acDbNewDoc.TransactionManager.StartTransaction() '' Open the Block table for read Dim acBlkTbl As BlockTable acBlkTbl = acTrans.GetObject(acDbNewDoc.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 '' Save the new object to the database acTrans.Commit() End Using '' Unlock the document End Using '' Set the new document current acDocMgr.MdiActiveDocument = acNewDoc End Sub C#using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry; [CommandMethod("LockDoc", CommandFlags.Session)] public static void LockDoc() { // Create a new drawing DocumentCollection acDocMgr = Application.DocumentManager; Document acNewDoc = acDocMgr.Add("acad.dwt"); Database acDbNewDoc = acNewDoc.Database; // Lock the new document using (DocumentLock acLckDoc = acNewDoc.LockDocument()) { // Start a transaction in the new database using (Transaction acTrans = acDbNewDoc.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acDbNewDoc.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); } // Save the new object to the database acTrans.Commit(); } // Unlock the document } // Set the new document current acDocMgr.MdiActiveDocument = acNewDoc; } 相关概念父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-5-13 09:28
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.