コレクション オブジェクトを反復処理する(.NET)

Collection オブジェクトの特定のメンバーを選択するには、Item メソッドまたは GetAt メソッドを使用します。Item メソッドおよび GetAt メソッドには、項目の名前を表す文字列形式のキーが必要です。ほとんどのコレクションでは Item メソッドが暗黙で使用されるので、実際にメソッドを使用する必要はありません。

一部の Collection オブジェクトでは、インデックス番号を使用して、取得する項目のコレクション内での位置を指定できます。使用できるメソッドは、使用している言語、およびシンボル テーブルまたはディクショナリのどちらを使用しているかによって異なります。

次の文では、画層シンボル テーブル内の「MyLayer」テーブル レコードにアクセスする方法を示します。

C# の例

acObjId = acLyrTbl["MyLayer"];

LayerTable オブジェクトを反復処理する

次の例では、LayerTable オブジェクトを反復処理し、そのすべての画層テーブル レコードの名前を表示します。

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
  }
}

LayerTable オブジェクトで MyLayer という名前の画層テーブル レコードを検索する

次の例では、LayerTable オブジェクトを調べて MyLayer という名前の画層が存在するかどうかを確認し、適切なメッセージを表示します。

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
  }
}