添加或合并多个选择集
 
 

You can merge multiple selection sets be creating an ObjectIdCollection object and then adding the object ids from multiple selection sets together. In addition to adding object ids to an ObjectIdCollection object, you can remove object ids. Once all object ids are added to an ObjectIdCollection object, you can iterate through the collection of object ids and manipulate each object as needed.

用户可以合并多个选择集,需要创建一个 ObjectIdCollection 对象,然后向其中添加多个选择集的对象的 ObjectID。除了添加 ObjectID 到 ObjectIdCollection 对象中,也可以移除 ObjectID。一旦所有 ObjectID 添加到 ObjectIdCollection 对象后,就可以遍历 ObjectID 集合并操作每一个需要的对象。

添加选择的对象到选择集中

This example prompts the user to select objects twice and then merges the two selection sets created into a single selection set.

本例提示用户选择对象两次然后合并两个选择集到一个独立的选择集中。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
 
<CommandMethod("MergeSelectionSets")> _
Public Sub MergeSelectionSets()
  '' 获得当前文档的编辑器     Get the current document editor
  Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
 
  '' 要求在图形区域中选择对象    Request for objects to be selected in the drawing area
  Dim acSSPrompt As PromptSelectionResult
  acSSPrompt = acDocEd.GetSelection()
 
  Dim acSSet1 As SelectionSet
  Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
 
  '' 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
  If acSSPrompt.Status = PromptStatus.OK Then
      '' Get the selected objects
      acSSet1 = acSSPrompt.Value
 
      '' 添加选择的对象到 ObjectIDCollection 中   Append the selected objects to the ObjectIdCollection
      acObjIdColl = New ObjectIdCollection(acSSet1.GetObjectIds())
  End If
 
  '' 要求在图形区域中选择对象    Request for objects to be selected in the drawing area
  acSSPrompt = acDocEd.GetSelection()
 
  Dim acSSet2 As SelectionSet
 
  '' 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
  If acSSPrompt.Status = PromptStatus.OK Then
      acSSet2 = acSSPrompt.Value
 
      '' 检查 ObjectIDCollection 的大小,如果为 0 就重新初始化它    Check the size of the ObjectIdCollection, if zero, then initialize it
      If acObjIdColl.Count = 0 Then
          acObjIdColl = New ObjectIdCollection(acSSet2.GetObjectIds())
      Else
          Dim acObjId As ObjectId
 
          '' 遍历第二个选择集   Step through the second selection set
          For Each acObjId In acSSet2.GetObjectIds()
              '' 添加每个对象 ID 到 ObjectIDCollection 中    Add each object id to the ObjectIdCollection
              acObjIdColl.Add(acObjId)
          Next
      End If
  End If
 
  Application.ShowAlertDialog("Number of objects selected: " & _
                              acObjIdColl.Count.ToString())
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("MergeSelectionSets")]
public static void MergeSelectionSets()
{
  // 获得当前文档的编辑器     Get the current document editor
  Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
 
  // 要求在图形区域中选择对象    Request for objects to be selected in the drawing area
  PromptSelectionResult acSSPrompt;
  acSSPrompt = acDocEd.GetSelection();
 
  SelectionSet acSSet1;
  ObjectIdCollection acObjIdColl = new ObjectIdCollection();
 
  // 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
  if (acSSPrompt.Status == PromptStatus.OK)
  {
      // Get the selected objects
      acSSet1 = acSSPrompt.Value;
 
      // 添加选择的对象到 ObjectIDCollection 中   Append the selected objects to the ObjectIdCollection
      acObjIdColl = new ObjectIdCollection(acSSet1.GetObjectIds());
  }
 
  // 要求在图形区域中选择对象    Request for objects to be selected in the drawing area
  acSSPrompt = acDocEd.GetSelection();
 
  SelectionSet acSSet2;
 
  // 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
  if (acSSPrompt.Status == PromptStatus.OK)
  {
      acSSet2 = acSSPrompt.Value;
 
      // 检查 ObjectIDCollection 的大小,如果为 0 就重新初始化它    Check the size of the ObjectIdCollection, if zero, then initialize it
      if (acObjIdColl.Count == 0)
      {
          acObjIdColl = new ObjectIdCollection(acSSet2.GetObjectIds());
      }
      else
      {
          // 遍历第二个选择集   Step through the second selection set
          foreach (ObjectId acObjId in acSSet2.GetObjectIds())
          {
              // 添加每个对象 ID 到 ObjectIDCollection 中    Add each object id to the ObjectIdCollection
              acObjIdColl.Add(acObjId);
          }
      }
  }
 
  Application.ShowAlertDialog("Number of objects selected: " +
                              acObjIdColl.Count.ToString());
}
VBA/ActiveX 代码参考