Collection オブジェクトの特定のメンバーを選択するには、Item メソッドまたは GetAt メソッドを使用します。Item メソッドおよび GetAt メソッドには、項目の名前を表す文字列形式のキーが必要です。ほとんどのコレクションでは Item メソッドが暗黙で使用されるので、実際にメソッドを使用する必要はありません。
一部の Collection オブジェクトでは、インデックス番号を使用して、取得する項目のコレクション内での位置を指定できます。使用できるメソッドは、使用している言語、およびシンボル テーブルまたはディクショナリのどちらを使用しているかによって異なります。
次の文では、画層シンボル テーブル内の「MyLayer」テーブル レコードにアクセスする方法を示します。
acObjId = acLyrTbl.Item("MyLayer")
acObjId = acLyrTbl("MyLayer")
acObjId = acLyrTbl["MyLayer"];
acLayer = ThisDrawing.Layers.Item("MyLayer")
acLayer = ThisDrawing.Layers("MyLayer")
次の例では、LayerTable オブジェクトを反復処理し、そのすべての画層テーブル レコードの名前を表示します。
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
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
}
}
Sub IterateLayers()
' Iterate through the collection
On Error Resume Next
Dim lay As AcadLayer
Dim msg As String
msg = ""
For Each lay In ThisDrawing.Layers
msg = msg + lay.Name + vbCrLf
Next
ThisDrawing.Utility.prompt msg
End Sub
次の例では、LayerTable オブジェクトを調べて MyLayer という名前の画層が存在するかどうかを確認し、適切なメッセージを表示します。
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)
'' 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
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
}
}
Sub FindMyLayer()
' Use the Item method to find a layer named MyLayer
On Error Resume Next
Dim ABCLayer As AcadLayer
Set ABCLayer = ThisDrawing.Layers("MyLayer")
If Err <> 0 Then
ThisDrawing.Utility.prompt "'MyLayer' does not exist"
Else
ThisDrawing.Utility.prompt "'MyLayer' exists"
End If
End Sub