排序图层和线型
 
 

You can iterate through the Layers and Linetypes tables to find all the layers and linetypes in a drawing.

用户可以遍历 Layers 和 Linetypes 表来查找图形中的所有图层和线型。

遍历层表

The following code iterates through the Layers table to gather the names of all the layers in the drawing. The names are then displayed in a message box.

以下代码遍历 Layers 表,以合并图形中所有图层的名称,然后将这些名称显示在消息框中。  

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 代码参考