CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2022 开发者帮助

循环访问集合对象 (.NET)

2024-5-18 18:48| 发布者: admin| 查看: 7| 评论: 0|原作者: admin|来自: AutoCAD

循环访问集合对象 (.NET)

若要选择 Collection 对象的特定成员,请使用 or 方法。and 方法需要一个字符串形式的键,其中表示项的名称。对于大多数集合,Item 方法是隐含的,这意味着您实际上不需要使用方法。ItemGetAtItemGetAt

对于某些 Collection 对象,还可以使用索引号来指定要检索的集合中项的位置。您可以使用的方法因您使用的语言以及您使用的符号表或字典而异。

以下语句说明如何访问图层符号表中的“MyLayer”表记录。

VB.NET

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

C#

acObjId = acLyrTbl["MyLayer"];

VBA/ActiveX 代码参考

acLayer = ThisDrawing.Layers.Item("MyLayer")
 
acLayer = ThisDrawing.Layers("MyLayer")

循环访问 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 代码参考

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.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)
 
      '' 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

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-6-27 16:23

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部