作図領域でオブジェクトを選択する(.NET)

ユーザにインタラクティブにオブジェクトを選択させることによってオブジェクトを選択するか、AutoCAD .NET API を使用してさまざまなオブジェクト選択オプションの多くをシミュレートすることができます。ルーチンで複数の選択セットを実行する場合は、返される各選択セットを追跡するか、ObjectIdCollection オブジェクトを作成して、選択されたすべてのオブジェクトを追跡する必要があります。次の関数を使用すると、図面からオブジェクトを選択することができます。

GetSelection

ユーザに画面上でオブジェクトを選択するよう要求します。

SelectAll

図面のすべてのオブジェクトを選択します。

注: すべてのレイアウトと空間内のオブジェクトと、ロックまたはフリーズされたオブジェクトが選択されます。
SelectCrossingPolygon

複数の点を指定して定義されたポリゴンの内部のオブジェクトまたはポリゴンと交差するオブジェクトが選択されます。ポリゴンはどんな形でもかまいませんが、自己交差したり自己接触したりするものであってはなりません。

SelectCrossingWindow

2 つの点で定義される領域の内部のオブジェクト、または領域と交差しているオブジェクトが選択されます。

SelectFence

選択フェンスと交差するすべてのオブジェクトが選択されます。フェンス選択は、フェンスが閉じておらず、フェンスがそれ自体と交差できることを除いてポリゴン交差選択と同様です。

SelectLast

現在の空間で作成された最後のオブジェクトを選択します。

SelectPrevious

前の[オブジェクトを選択]プロンプト中に選択されたすべてのオブジェクトを選択します。

SelectWindow

2 つの点で定義される長方形の内側に完全に入っているすべてのオブジェクトが選択されます。

SelectWindowPolygon

複数の点によって定義されたポリゴン内部に完全に入っているオブジェクトが選択されます。ポリゴンはどんな形でもかまいませんが、自己交差したり自己接触したりするものであってはなりません。

SelectAtPoint

指定した点を通るオブジェクトを選択し、それをアクティブな選択セット内に配置します。

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