CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2018 开发者帮助

升级和降级打开的对象 (.NET)

2024-5-18 16:57| 发布者: admin| 查看: 70| 评论: 0|原作者: admin|来自: AutoCAD

升级和降级打开的对象 (.NET)

通过升级或降级对象,可以将对象的当前打开模式从读取更改为写入或写入为读取。

这些方法可用于升级或降级以前打开的对象:

  • Transaction.GetObjectmethod – 如果对象是使用该方法打开的,请使用该方法以所需的新打开模式重新打开对象。Transaction.GetObject
  • UpgradeOpen和 方法 – 如果对象是用 方法打开的,或者 ,使用该方法将对象的打开模式从“读取”更改为“写入”,或者使用该方法将对象的打开模式从“写入”更改为“读取”。您不需要将 对每个 的调用配对,因为关闭对象或释放事务将充分清理对象的打开状态。DowngradeOpenOpenOpenCloseTransaction.GetObjectUpgradeOpenDowngradeOpenDowngradeOpenUpgradeOpen

建议使用与对象使用方式最匹配的模式打开对象,因为打开对象进行读取和查询对象属性比打开对象进行写入和查询对象属性更有效。如果不确定是否需要修改对象,最好打开对象进行读取,然后升级它进行写入,因为这有助于减少程序的开销。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("FreezeDoorLayer")> _
Public Sub FreezeDoorLayer()
    '' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        '' Open the Layer table for read
        Dim acLyrTbl As LayerTable
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)

        '' Step through each layer and update those that start with 'Door'
        For Each acObjId As ObjectId In acLyrTbl
            '' Open the Layer table record for read
            Dim acLyrTblRec As LayerTableRecord
            acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead)

            '' Check to see if the layer's name starts with 'Door' 
            If (acLyrTblRec.Name.StartsWith("Door", _
                                            StringComparison.OrdinalIgnoreCase) = True) Then
                '' Check to see if the layer is current, if so then do not freeze it
                If acLyrTblRec.ObjectId <> acCurDb.Clayer Then
                    '' Change from read to write mode
                    acTrans.GetObject(acObjId, OpenMode.ForWrite)

                    '' Freeze the layer
                    acLyrTblRec.IsFrozen = True
                End If
            End If
        Next

        '' Commit the changes and dispose of the transaction
        acTrans.Commit()
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("FreezeDoorLayer")]
public static void FreezeDoorLayer()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Layer table for read
        LayerTable acLyrTbl;
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                        OpenMode.ForRead) as LayerTable;

        // Step through each layer and update those that start with 'Door'
        foreach (ObjectId acObjId in acLyrTbl)
        {
            // Open the Layer table record for read
            LayerTableRecord acLyrTblRec;
            acLyrTblRec = acTrans.GetObject(acObjId,
                                            OpenMode.ForRead) as LayerTableRecord;

            // Check to see if the layer's name starts with 'Door' 
            if (acLyrTblRec.Name.StartsWith("Door",
                                            StringComparison.OrdinalIgnoreCase) == true)
            {
                // Check to see if the layer is current, if so then do not freeze it
                if (acLyrTblRec.ObjectId != acCurDb.Clayer)
                {
                    // Change from read to write mode
                    acTrans.GetObject(acObjId, OpenMode.ForWrite);

                    // Freeze the layer
                    acLyrTblRec.IsFrozen = true;
                }
            }
        }

        // Commit the changes and dispose of the transaction
        acTrans.Commit();
    }
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 11:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部