循环访问集合对象 (.NET)
若要选择 Collection 对象的特定成员,请使用 or 方法。and 方法需要一个字符串形式的键,其中表示项的名称。对于大多数集合,Item 方法是隐含的,这意味着您实际上不需要使用方法。ItemGetAtItemGetAt 对于某些 Collection 对象,还可以使用索引号来指定要检索的集合中项的位置。您可以使用的方法因您使用的语言以及您使用的符号表或字典而异。 以下语句说明如何访问图层符号表中的“MyLayer”表记录。 VB.NETacObjId = acLyrTbl.Item("MyLayer")
acObjId = acLyrTbl("MyLayer")
C#acObjId = acLyrTbl["MyLayer"]; VBA/ActiveX 代码参考acLayer = ThisDrawing.Layers.Item("MyLayer")
acLayer = ThisDrawing.Layers("MyLayer")
循环访问 LayerTable 对象以下示例循环访问对象并显示其所有图层表记录的名称:LayerTable VB.NETImports 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 代码参考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 的图层表记录以下示例检查对象以确定名为 MyLayer 的图层是否存在,并显示相应的消息:LayerTable VB.NETImports 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
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 代码参考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
相关概念父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-31 06:56
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.