画層および線種をソートする(.NET)

画層および線種テーブル内を走査すると、図面内のすべての画層および線種を確認できます。

画層テーブル内を走査する

次のコードは画層テーブルを走査し、図面内のすべての画層の名前を収集します。名前はメッセージ ボックスに表示されます。

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