CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

锁定和解锁文档 (.NET)

2023-1-1 15:52| 发布者: admin| 查看: 570| 评论: 0|来自: AutoCAD

修改对象或访问 AutoCAD 的请求可以在任何上下文中发生,并且来自任意数量的应用程序。为防止与其他请求发生冲突,您有责任在修改文档之前锁定文档。在某些上下文中未能锁定文档将导致在修改数据库期间发生锁定冲突。您希望在以下情况下锁定文档:

  • 从无模式对话框与 AutoCAD 交互
  • 访问当前文档以外的已加载文档
  • 用作 COM 服务器
  • 使用会话命令标志注册命令

例如,将实体添加到当前文档以外的文档中的“模型”或“图纸”空间时,需要锁定该文档。使用要锁定的对象的方法。调用该方法时,返回一个对象。LockDocumentDatabaseLockDocumentDocumentLock

修改完锁定的数据库后,需要解锁数据库。若要解锁数据库,请调用对象的方法。您还可以将 Using 语句与对象一起使用,一旦 Using 语句结束,数据库就会解锁。DisposeDocumentLockDocumentLock

注意:在不使用 Session 命令标志的命令上下文中工作时,无需在修改当前文档之前锁定该文档的数据库。

在修改对象之前锁定数据库

本示例创建一个新文档,然后在其中画一个圆圈。创建文档后,将锁定新文档的数据库,然后向其添加一个圆圈。添加圆圈后,数据库将解锁,关联的文档窗口设置为当前。

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 = 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;
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 13:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部