CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

添加到或合并多个选择集 (.NET)

2023-1-1 14:28| 发布者: admin| 查看: 1014| 评论: 0|来自: AutoCAD

您可以合并多个选择集,方法是创建一个对象,然后将多个选择集中的对象 ID 相加。除了向对象添加对象 ID 之外,还可以删除对象 ID。将所有对象 ID 添加到对象后,您可以循环访问对象 ID 集合并根据需要操作每个对象。ObjectIdCollectionObjectIdCollectionObjectIdCollection

将选定对象添加到选择集

本示例提示用户选择对象两次,然后将创建的两个选择集合并为一个选择集。

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()

    '' If the prompt status is OK, objects were selected
    If acSSPrompt.Status = PromptStatus.OK Then
        '' Get the selected objects
        acSSet1 = acSSPrompt.Value

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

    '' If the prompt status is OK, objects were selected
    If acSSPrompt.Status = PromptStatus.OK Then
        acSSet2 = acSSPrompt.Value

        '' 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()
                '' 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();

    // If the prompt status is OK, objects were selected
    if (acSSPrompt.Status == PromptStatus.OK)
    {
        // Get the selected objects
        acSSet1 = acSSPrompt.Value;

        // 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;

    // If the prompt status is OK, objects were selected
    if (acSSPrompt.Status == PromptStatus.OK)
    {
        acSSet2 = acSSPrompt.Value;

        // 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())
            {
                // Add each object id to the ObjectIdCollection
                acObjIdColl.Add(acObjId);
            }
        }
    }

    Application.ShowAlertDialog("Number of objects selected: " +
                                acObjIdColl.Count.ToString());
}

VBA/ActiveX 代码参考

Sub MergeSelectionSets()
    ' Create a new selection set
    Dim sset As AcadSelectionSet
    Set sset = ThisDrawing.SelectionSets.Add("SS1")
 
    ' Prompt the user to select objects
    ' and add them to the selection set.
    sset.SelectOnScreen
 
    ' Prompt the user again to select objects
    ' and add them to the same selection set.
    sset.SelectOnScreen
 
    MsgBox "Number of total objects selected: " & sset.Count
 
    ' Remove the selection set at the end
    sset.Delete
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 13:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部