CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

对图层和线型进行排序 (.NET)

2023-1-1 13:33| 发布者: admin| 查看: 336| 评论: 0|来自: AutoCAD

您可以循环访问图层和线型表以查找图形中的所有图层和线型。

循环访问图层表

以下代码循环访问“图层”表以收集图形中所有图层的名称。然后,这些名称将显示在消息框中。

VB.NET

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

        Dim sLayerNames As String = ""

        For Each acObjId As ObjectId In acLyrTbl
            Dim acLyrTblRec As LayerTableRecord
            acLyrTblRec = acTrans.GetObject(acObjId, _
                                            OpenMode.ForRead)

            sLayerNames = sLayerNames & vbLf & acLyrTblRec.Name
        Next

        Application.ShowAlertDialog("The layers in this drawing are: " & _
                                    sLayerNames)

        '' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("DisplayLayerNames")]
public static void DisplayLayerNames()
{
    // 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;

        string sLayerNames = "";

        foreach (ObjectId acObjId in acLyrTbl)
        {
            LayerTableRecord acLyrTblRec;
            acLyrTblRec = acTrans.GetObject(acObjId,
                                            OpenMode.ForRead) as LayerTableRecord;

            sLayerNames = sLayerNames + "\n" + acLyrTblRec.Name;
        }

        Application.ShowAlertDialog("The layers in this drawing are: " +
                                    sLayerNames);

        // Dispose of the transaction
    }
}

VBA/ActiveX 代码参考

Sub DisplayLayerNames()
    Dim layerNames As String
    Dim entry As AcadLayer
    layerNames = ""
 
    For Each entry In ThisDrawing.Layers
        layerNames = layerNames + entry.Name + vbCrLf
    Next
 
    MsgBox "The layers in this drawing are: " + _
           vbCrLf + layerNames
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-27 09:44

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部