CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2020 开发者帮助

选择绘图区域中的对象 (.NET)

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

选择绘图区域中的对象 (.NET)

您可以通过让用户以交互方式选择对象来选择对象,也可以通过 AutoCAD .NET API 模拟许多不同的对象选择选项。如果例程执行多个选择集,则需要跟踪返回的每个选择集,或创建一个对象来跟踪所有选定对象。以下函数允许您从图形中选择对象:ObjectIdCollection

获取选择

提示用户从屏幕中选取对象。

全选

选择图形中的所有对象。

注意:将选择所有布局和空间中的对象,以及锁定或冻结的对象。
选择交叉多边形

选择通过指定点定义的多边形内和多边形交叉的对象。多边形可以是任何形状,但不能交叉或接触自身。

选择交叉窗口

选择由两点定义的区域内和穿过区域的对象。

选择围栏

选择穿过选择围栏的所有对象。篱笆选择与交叉面选择类似,只是篱笆不是闭合的,并且篱笆可以自行交叉。

选择最后

选择在当前空间中创建的最后一个对象。

选择上一页

选择在上一个“选择对象:”提示符下选择的所有对象。

选择窗口

完全选择由两点定义的矩形内的所有对象。

SelectWindowPolygon

完全在由点定义的多边形内选择对象。多边形可以是任何形状,但不能交叉或接触自身。

选择点

选择通过给定点的对象,并将其放入活动选择集中。

SelectByPolygon

选择围栏内的对象,并将其添加到活动选择集中。

提示屏幕上的对象并迭代选择集

本示例提示用户选择对象,然后将所选每个对象的颜色更改为“绿色”或“AutoCAD 颜色索引”3。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
 
<CommandMethod("SelectObjectsOnscreen")> _
Public Sub SelectObjectsOnscreen()
    '' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        '' Request for objects to be selected in the drawing area
        Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()

        '' If the prompt status is OK, objects were selected
        If acSSPrompt.Status = PromptStatus.OK Then
            Dim acSSet As SelectionSet = acSSPrompt.Value

            '' Step through the objects in the selection set
            For Each acSSObj As SelectedObject In acSSet
                '' Check to make sure a valid SelectedObject object was returned
                If Not IsDBNull(acSSObj) Then
                    '' Open the selected object for write
                    Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, _
                                                            OpenMode.ForWrite)

                    If Not IsDBNull(acEnt) Then
                        '' Change the object's color to Green
                        acEnt.ColorIndex = 3
                    End If
                End If
            Next

            '' Save the new object to the database
            acTrans.Commit()
        End If

        '' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("SelectObjectsOnscreen")]
public static void SelectObjectsOnscreen()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Request for objects to be selected in the drawing area
        PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();

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

            // Step through the objects in the selection set
            foreach (SelectedObject acSSObj in acSSet)
            {
                // Check to make sure a valid SelectedObject object was returned
                if (acSSObj != null)
                {
                    // Open the selected object for write
                    Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
                                                        OpenMode.ForWrite) as Entity;

                    if (acEnt != null)
                    {
                        // Change the object's color to Green
                        acEnt.ColorIndex = 3;
                    }
                }
            }

            // Save the new object to the database
            acTrans.Commit();
        }

        // Dispose of the transaction
    }
}

VBA/ActiveX 代码参考

Sub SelectObjectsOnscreen()
    ' 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
 
    Dim acEnt As AcadEntity
 
    ' Step through the selected objects and change
    ' each object's color to Green
    For Each acEnt In sset
        ' Use the Color property to set the object's color
        acEnt.color = acGreen
    Next acEnt
 
    ' Remove the selection set at the end
    sset.Delete
End Sub

选择具有交叉窗口的对象

本示例选择交叉窗口内和与交叉窗口相交的对象。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
 
<CommandMethod("SelectObjectsByCrossingWindow")> _
Public Sub SelectObjectsByCrossingWindow()
    '' Get the current document editor
    Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor

    '' Create a crossing window from (2,2,0) to (10,8,0)
    Dim acSSPrompt As PromptSelectionResult
    acSSPrompt = acDocEd.SelectCrossingWindow(New Point3d(2, 2, 0), _
                                              New Point3d(10, 8, 0))

    '' If the prompt status is OK, objects were selected
    If acSSPrompt.Status = PromptStatus.OK Then
        Dim acSSet As SelectionSet = acSSPrompt.Value

        Application.ShowAlertDialog("Number of objects selected: " & _
                                    acSSet.Count.ToString())
    Else
        Application.ShowAlertDialog("Number of objects selected: 0")
    End If
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("SelectObjectsByCrossingWindow")]
public static void SelectObjectsByCrossingWindow()
{
    // Get the current document editor
    Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;

    // Create a crossing window from (2,2,0) to (10,8,0)
    PromptSelectionResult acSSPrompt;
    acSSPrompt = acDocEd.SelectCrossingWindow(new Point3d(2, 2, 0),
                                                new Point3d(10, 8, 0));

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

        Application.ShowAlertDialog("Number of objects selected: " +
                                    acSSet.Count.ToString());
    }
    else
    {
        Application.ShowAlertDialog("Number of objects selected: 0");
    }
}

VBA/ActiveX 代码参考

Sub SelectObjectsByCrossingWindow()
    ' Create a new selection set
    Dim sset As AcadSelectionSet
    Set sset = ThisDrawing.SelectionSets.Add("SS1")
 
    ' Define the points for the crossing window
    Dim pt1(0 To 2) As Double
    Dim pt2(0 To 2) As Double
 
    pt1(0) = 2#: pt1(1) = 2#: pt1(2) = 0#:
    pt2(0) = 10#: pt2(1) = 8#: pt2(2) = 0#:
 
    ' Create a crossing window from (2,2,0) to (10,8,0)
    sset.Select acSelectionSetCrossing, pt1, pt2
 
    MsgBox "Number of objects selected: " & sset.Count
 
    ' Remove the selection set at the end
    sset.Delete
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 12:47

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部