CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

了解 AutoCAD 如何保存图层状态 (.NET)

2023-1-1 12:53| 发布者: admin| 查看: 418| 评论: 0|来自: AutoCAD

AutoCAD 将图层设置信息保存在对象的扩展字典中。首次保存图层状态时,AutoCAD 将执行以下操作:LayerTable

  • 在图层表上创建扩展字典。
  • 在扩展字典中创建名为 ACAD_LAYERSTATE 的对象。Dictionary
  • 将图形中每个图层的特性存储在ACAD_LAYERSTATE字典中的对象中。AutoCAD 将所有图层设置存储在 中,但标识您选择保存的特定设置。恢复图层设置时,AutoCAD 仅恢复您选择保存的设置。XRecordXRecord

每次在图形中保存其他图层设置时,AutoCAD 都会创建另一个描述已保存设置的对象,并将其存储在ACAD_LAYERSTATE字典中。下图说明了该过程。XRecordXRecord

使用图层状态时,您不需要(也不应该尝试)直接操作条目。使用对象的函数访问字典。获得对字典的引用后,可以单步执行表示为对象的每个条目。LayerStateManagerDBDictionaryEntry

列出图形中保存的图层状态

如果图层状态已保存在当前图形中,则以下代码将列出所有已保存图层状态的名称:

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("ListLayerStates")> _
Public Sub ListLayerStates()
    '' 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()

        Dim acLyrStMan As LayerStateManager
        acLyrStMan = acCurDb.LayerStateManager

        Dim acDbDict As DBDictionary
        acDbDict = acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(True), _
                                     OpenMode.ForRead)

        Dim sLayerStateNames As String = ""

        For Each acDbDictEnt As DBDictionaryEntry In acDbDict
            sLayerStateNames = sLayerStateNames & vbLf & acDbDictEnt.Key
        Next

        Application.ShowAlertDialog("The saved layer settings in this drawing are:" & _
                                    sLayerStateNames)

        '' Dispose of the transaction
    End Using
End Sub

C#

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

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        LayerStateManager acLyrStMan;
        acLyrStMan = acCurDb.LayerStateManager;

        DBDictionary acDbDict;
        acDbDict = acTrans.GetObject(acLyrStMan.LayerStatesDictionaryId(true),
                                        OpenMode.ForRead) as DBDictionary;

        string sLayerStateNames = "";

        foreach (DBDictionaryEntry acDbDictEnt in acDbDict)
        {
            sLayerStateNames = sLayerStateNames + "\n" + acDbDictEnt.Key;
        }

        Application.ShowAlertDialog("The saved layer settings in this drawing are:" +
                                    sLayerStateNames);

        // Dispose of the transaction
    }
}

VBA/ActiveX 代码参考

Sub ListLayerStates()
     On Error Resume Next
    Dim oLSMDict As AcadDictionary
    Dim XRec As Object
    Dim layerstateNames As String
 
    layerstateNames = ""
    ' Get the ACAD_LAYERSTATES dictionary, which is in the
    ' extension dictionary in the Layers object.
    Set oLSMDict = ThisDrawing.Layers. _
                               GetExtensionDictionary.Item("ACAD_LAYERSTATES")
 
    ' List the name of each saved layer setting. Settings are
    ' stored as XRecords in the dictionary.
    For Each XRec In oLSMDict
       layerstateNames = layerstateNames + XRec.Name + vbCrLf
    Next XRec
 
    MsgBox "The saved layer settings in this drawing are: " + _
           vbCrLf + layerstateNames
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 11:55

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部