遍历集合对象
 
 

要选择集合对象的特定成员,请使用 ItemGetAt 方法。 ItemGetAt 方法需要一个字符串形式的关键字,它表示项目的名字。 大多数集合中, Item 方法是默认方法,也就是说用户不需要实际指定这个方法名。

有些集合对象,用户也可以使用项目 (Item) 在集合内部指定位置的索引号 (Index) 来获得想要的对象。这种方法适用于你所使用的各种不同语言,就如使用符号表或字典一样。

下面的语句显示怎样访问层表中的 “MyLayer” 表记录。

VB.NET

acObjId = acLyrTbl.Item("MyLayer")
 
acObjId = acLyrTbl("MyLayer")

C#

acObjId = acLyrTbl["MyLayer"];
VBA/ActiveX 代码参考

通过 LayerTable 遍历对象

下面的例子通过 LayerTable 遍历对象并显示所有层表记录的名称:

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("IterateLayers")> _
Public Sub IterateLayers()
  ''获得当前文档和数据库,并启动一个事务   Get the current document and database, and start a transaction
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
      ''这个例子返回当前数据库的层表  This example returns the layer table for the current database
      Dim acLyrTbl As LayerTable
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                   OpenMode.ForRead)
 
      '' 遍历层表并输出每一个层的名称 Step through the Layer table and print each layer name
      For Each acObjId As ObjectId In acLyrTbl
          Dim acLyrTblRec As LayerTableRecord
          acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForRead)
 
          acDoc.Editor.WriteMessage(vbLf & acLyrTblRec.Name)
      Next
 
      '' 销毁事务  Dispose of the transaction
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("IterateLayers")]
public static void IterateLayers()
{
  // Get the current document and database, and start a transaction
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // This example returns the layer table for the current database
      LayerTable acLyrTbl;
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                   OpenMode.ForRead) as LayerTable;
 
      // Step through the Layer table and print each layer name
      foreach (ObjectId acObjId in acLyrTbl)
      {
          LayerTableRecord acLyrTblRec;
          acLyrTblRec = acTrans.GetObject(acObjId,
                                          OpenMode.ForRead) as LayerTableRecord;
 
          acDoc.Editor.WriteMessage("\n" + acLyrTblRec.Name);
      }
 
      // Dispose of the transaction
  }
}
VBA/ActiveX 代码参考

在层表(LayerTable)对象上查找名称为 MyLayer 的层表记录(LayerTableRecord)

下面的例子检查 LayerTable 对象以确定命名为 MyLayer 的图层存在与否,并显示相应的信息:

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("FindMyLayer")> _
Public Sub FindMyLayer()
  ''获得当前文档和数据库,并启动一个事务   Get the current document and database, and start a transaction
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
      ''返回当前数据库的层表   Returns the layer table for the current database
      Dim acLyrTbl As LayerTable
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
                                   OpenMode.ForRead)
 
      ''检验名字为 MyLayer 的图层是否已经存在在层表中 Check to see if MyLayer exists in the Layer table
      If Not acLyrTbl.Has("MyLayer") Then
          acDoc.Editor.WriteMessage(vbLf & "'MyLayer' does not exist")
      Else
          acDoc.Editor.WriteMessage(vbLf & "'MyLayer' exists")
      End If
 
      '' 销毁事务  Dispose of the transaction
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("FindMyLayer")]
public static void FindMyLayer()
{
  // Get the current document and database, and start a transaction
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // Returns the layer table for the current database
      LayerTable acLyrTbl;
      acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                   OpenMode.ForRead) as LayerTable;
 
      // Check to see if MyLayer exists in the Layer table
      if (acLyrTbl.Has("MyLayer") != true)
      {
          acDoc.Editor.WriteMessage("\n'MyLayer' does not exist");
      }
      else
      {
          acDoc.Editor.WriteMessage("\n'MyLayer' exists");
      }
 
      // Dispose of the transaction
  }
}
VBA/ActiveX 代码参考