02-04 控制AutoCAD环境(四) 锁定和解锁文档

 

Lock and Unlock a Document锁定和解锁文档

Requests to modify objects or access AutoCAD can occur in any context, and coming from any number of applications. To prevent conflicts with other requests, you are responsible for locking a document before you modify it. Failure to lock the document in certain contexts will cause a lock violation during the modification of the database. You want to lock the document when your application:

修改对象或访问AutoCAD的请求随时随地都发生,为避免与其他请求冲突,我们有责任在修改前锁定文档。某些情形下的锁定文档失败会导致在更新数据库过程中锁定犯规。当我们的应用程序进行下列操作是需要锁定文档:

·         Interacts with AutoCAD from a modeless dialog box 从无模式对话框与AutoCAD交互时;

·         Accesses a loaded document other than the current document 访问已调入的文档而不是当前文档时;

·         Used as a COM server 应用程序作为COM服务器时;

·         Registers a command with the Session command flag 用会话命令标志注册命令时

For example, when adding an entity to Model or Paper space in a document other than the current document, the document needs to be locked. You use the LockDocument method of the Database object you want to lock. When the LockDocument method is called, a DocumentLock object is returned.

例如,向非当前文档的模型或图纸空间添加实体时,就需要锁定文档。我们使用要锁定的数据库对象的LockDocument方法,调用LockDocument方法时,返回一个DocumentLock对象。

Once you are done modifying the locked database, you need to unlock the database. To unlock the database, you call the Dispose method of the DocumentLock object. You can also use the Using statement with the DocumentLock object, once the Using statement ends the database is unlocked.

一旦修改完已锁定数据库,就要将数据库解锁。解锁数据库,我们调用DocumentLock对象的Dispose方法。我们还可以使用Using语句,Using语句运行结束,数据库也就解锁了。(Using语句请参考C#中垃圾回收相关内容 – 译者)

NoteWhen working in the context of a command that does not use the Session command flag, you do not need to lock the database for the current document before it is modified.

注意:执行没有使用会话命令标志的命令时,我们不需要在修改前锁定当前文档的数据库。

Lock a database before modifying an object 修改对象前锁定数据库

This example creates a new document and then draws a circle in it. After the document is created, the database for the new document is locked and then a circle is added to it. After the circle is added, the database is unlocked and the associated document window is set current.

本例新建一个文档然后绘制一个圆。文档创建后,新文档的数据库被锁定,然后圆添加到文档,添加完圆后数据库解锁,相应文档窗口置为当前。

VB.NET

Imports 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 = acDocMgr.Add("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

          Dim 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)

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

          //以半径3圆心(5,5)绘圆

          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解锁文档using语句到此结束

  }

   // Set the new document current将新文档置为当前

  acDocMgr.MdiActiveDocument = acNewDoc;

}

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值