CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

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

2023-1-1 16:37| 发布者: admin| 查看: 544| 评论: 0|来自: AutoCAD

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

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

以下语句显示了如何访问图层符号表中的“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

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 Code Reference

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

Find the layer table record named MyLayer in the LayerTable object

The following example checks the object to determine if the layer named MyLayer exists or not, and displays the appropriate message: 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

路过

雷人

握手

鲜花

鸡蛋

最新评论

AutoCAD Civil 3D.NET二次开发

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

GMT+8, 2024-5-7 03:03

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部