EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 547|回复: 0
打印 上一主题 下一主题

[资料] 控制AutoCAD环境(四) 锁定和解锁文档

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12406

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-8 16:08:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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;

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-27 06:02

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表