锁定和解锁文档
 
 

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 可能发生在任何情况下,并有可能来自许多应用程序。若要避免和其它请求的冲突,应该在修改操作前锁定文档。在某些情况下锁定文档失败将引起数据库在修改期间的锁定侵犯。在如下情况下你将需要锁定文档:

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.

例如,当要在除当前文档外的其它文档的模型空间或图纸空间中添加图元时,需要先锁定其文档。用户可用要锁定的Database对象的LockDocument方法进行锁定。在调用LockDocument方法后,将返回一个DocumentLock对象。

译者注:此处有一错误,LockDocument 方法是 Document 对象的,不是 Database 对象的。

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 语句用于 DocumentLock 对象,一旦 Using 语句结束,数据库也将被解锁。

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.

注意当运行没有使用 Seesion 命令标记的命令的情况下,当前文档在修改前不需要锁定数据库。

修改对象前锁定数据库

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)
 
          '' 创建一个半径为3圆心在5,5的圆    Create a circle with a radius of 3 at 5,5
          Dim acCirc As Circle = New Circle()
          acCirc.SetDatabaseDefaults()
          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;
 
          // 创建一个半径为3圆心在5,5的圆    Create a circle with a radius of 3 at 5,5
          Circle acCirc = new Circle();
          acCirc.SetDatabaseDefaults();
          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;
}