03-03 創建和編輯AutoCAD實體(三) 使用選擇集(1)

 

Work with Selection Sets使用選擇集

A selection set can consist of a single object, or it can be a more complex grouping: for example, the set of objects on a certain layer. A selection set is created by requesting a user to select an object in the drawing area before a command is started through pick first selection or at the

Select objects:

prompt when a command is active.

選擇集可以由單個對象組成,或者是一個複雜的編組:比如某圖層上的一組對象。選擇集的典型創建過程,是在通過先選擇後執行方式啓動命令前,或在命令行出現

選擇對象:

提示時,要求用戶選擇圖形中的對象。

Selection sets are not persistent objects, if you need to maintain a selection set for use between multiple commands or future use, you will need to create a custom dictionary and record the object ids found in the selection set as a record.

選擇集不是持久生存的對象,如果需要在多個命令間維持選擇集或爲以後的使用保留選擇集,就需要創建一個自定義字典並記錄選擇集中各對象的ID。

 

Topics in this section本節內容

·         Obtain the PickFirst Selection Set 獲得先選擇後執行選擇集

·         Select Objects in the Drawing Area 在圖形區域選擇對象

·         Add To or Merge Multiple Selection Sets 添加或合併多個選擇集

·         Define Rules for Selection Filters 定義選擇集過濾器規則

·         Remove Objects From a Selection Set 從選擇集刪除對象

 

1、Obtain the PickFirst Selection Set獲得先選擇後執行選擇集

The PickFirst selection set is created when you select objects prior to starting a command. Several conditions must be present in order to obtain the objects of a PickFirst selection set, these conditions are:

·         PICKFIRST system variable must be set to 1

·         UsePickSet command flag must be defined with the command that should use the Pickfirst selection set

·         Call the SelectImplied method to obtain the PickFirst selection set

在啓動命令之前選擇對象就創建了PickFirst選擇集。獲得PickFirst選擇集對象必須具備下列幾個條件:

·         系統變量PICKFIRST必須設置爲1;

·         要使用PickFirst選擇集的命令必須定義好UsePickSet命令標誌;

·         調用SelectImplied方法獲得PickFirst選擇集;

The SetImpliedSelection method is used to clear the current PickFirst selection set.

SetImpliedSelection方法用來清空當前PickFirst選擇集。

Get the Pickfirst selection set 獲得PickFirst選擇集

This example displays the number of objects in the PickFirst selection set and then requests the user to select additional objects. Before requesting the user to select objects, the current PickFirst selection set is cleared with the SetImpliedSelection method.

本例顯示PickFirst選擇集中對象的數量,然後請求用戶選擇其他的對象。在請求用戶選擇對象之前,使用SetImpliedSelection方法清空了當前PIckFirst選擇集。

VB.NET

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.EditorInput

 

<CommandMethod("CheckForPickfirstSelection", CommandFlags.UsePickSet)> _

Public Sub CheckForPickfirstSelection()

  '' Get the current document

  Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor

 

  '' Get the PickFirst selection set

  Dim acSSPrompt As PromptSelectionResult

  acSSPrompt = acDocEd.SelectImplied()

 

  Dim acSSet As SelectionSet

 

  '' If the prompt status is OK, objects were selected before

  '' the command was started

  If acSSPrompt.Status = PromptStatus.OK Then

      acSSet = acSSPrompt.Value

 

      Application.ShowAlertDialog("Number of objects in Pickfirst selection: " & _

                                  acSSet.Count.ToString())

  Else

      Application.ShowAlertDialog("Number of objects in Pickfirst selection: 0")

  End If

 

  '' Clear the PickFirst selection set

  Dim idarrayEmpty() As ObjectId

  acDocEd.SetImpliedSelection(idarrayEmpty)

 

  '' Request for objects to be selected in the drawing area

  acSSPrompt = acDocEd.GetSelection()

 

  '' If the prompt status is OK, objects were selected

  If acSSPrompt.Status = PromptStatus.OK Then

      acSSet = 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.EditorInput;

 

[CommandMethod("CheckForPickfirstSelection", CommandFlags.UsePickSet)]

public static void CheckForPickfirstSelection()

{

  // Get the current document獲取當前文檔

  Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;

 

  // Get the PickFirst selection set獲取PickFirst選擇集

  PromptSelectionResult acSSPrompt;

  acSSPrompt = acDocEd.SelectImplied();

 

  SelectionSet acSSet;

 

  // If the prompt status is OK, objects were selected before

  // the command was started

  // 如果提示狀態OK,說明啓動命令前選擇了對象;

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      acSSet = acSSPrompt.Value;

 

      Application.ShowAlertDialog("Number of objects in Pickfirst selection: " +

                                  acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Number of objects in Pickfirst selection: 0");

  }

 

  // Clear the PickFirst selection set清空選擇集

  ObjectId[] idarrayEmpty = new ObjectId[0];

  acDocEd.SetImpliedSelection(idarrayEmpty);

 

  // Request for objects to be selected in the drawing area

  // 請求從圖形區域選擇對象

  acSSPrompt = acDocEd.GetSelection();

 

  // If the prompt status is OK, objects were selected

  // 如果提示狀態OK,表示已選擇對象

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      acSSet = acSSPrompt.Value;

 

      Application.ShowAlertDialog("Number of objects selected: " +

                                  acSSet.Count.ToString());

  }

  else

  {

      Application.ShowAlertDialog("Number of objects selected: 0");

  }

}

VBA/ActiveX Code Reference

Sub CheckForPickfirstSelection()

    ' Get the Pickfirst selection set

    Dim acSSet As AcadSelectionSet

    Set acSSet = ThisDrawing.PickfirstSelectionSet

 

    ' Display the number of selected objects

    MsgBox "Number of objects in Pickfirst selection set: " & acSSet.Count

 

    ' Create a new selection set

    Dim acSSetUser As AcadSelectionSet

    Set acSSetUser = ThisDrawing.SelectionSets.Add("User")

 

    ' Select objects in the drawing

    acSSetUser.SelectOnScreen

 

    ' Display the number of selected objects

    MsgBox "Number of objects selected: " & acSSetUser.Count

    ' Remove the new named selection set

    acSSetUser.Delete

End Sub

 

2、Select Objects in the Drawing Area在圖形區域選擇對象

You can select objects through by having the user interactively select objects, or you can simulate many of the various object selection options through the .NET API. If your routine performs multiple selection sets, you will need to either track each selection set returned or create an ObjectIdCollection object to keep track of all the selected objects. The following functions allow you to select objects from the drawing:

可以通過與用戶交互來選擇對象,或者,通過.NET API模擬各種不同的對象選擇選項。如果程序執行多個選擇集,要麼需要跟蹤返回的每個選擇集,要麼需要創建一個ObjectIdCollection對象來跟蹤所有已選擇的對象。下列函數用來從圖形中選擇對象:

GetSelection

Prompts the user to pick objects from the screen. 提示用戶從屏幕拾取對象。

SelectAll

Selects all objects in the current space in which are not locked or frozen. 選定當前空間內所有未鎖定及未凍結的對象。

SelectCrossingPolygon

Selects objects within and crossing a polygon defined by specifying points. The polygon can be any shape but cannot cross or touch itself. 選定所有給定點定義的多邊形內的對象以及與多邊形相交的對象。多邊形可以是任意形狀,但不能與自己交叉或接觸。

SelectCrossingWindow

Selects objects within and crossing an area defined by two points. 選定兩個點定義的窗口內的對象以及與窗口相交的對象。

SelectFence

Selects all objects crossing a selection fence. Fence selection is similar to crossing polygon selection except that the fence is not closed, and a fence can cross itself. 選定所有與選擇圍欄相交的對象。圍欄選擇類似與多邊形選擇,所不同的是圍欄不是封閉的,且不能與自己相交。

SelectLast

Selects the last object created in the current space. 選定當前空間中最後創建的那個對象。

SelectPrevious

Selects all objects selected during the previous Select objects: prompt. 選定前一個“選擇對象:”提示符期間所選擇的所有對象。

SelectWindow

Selects all objects completely inside a rectangle defined by two points. 選定所有完全框入由兩個點定義的矩形內的對象。

SelectWindowPolygon

Selects objects completely inside a polygon defined by points. The polygon can be any shape but cannot cross or touch itself. 選定完全框入由點定義的多邊形內的對象。多邊形可以是任意形狀,但不能與自己交叉或接觸。

SelectAtPoint

Selects objects passing through a given point and places them into the active selection set. 選擇通過給定點的對象,並將其放入活動選擇集。

SelectByPolygon

Selects objects within a fence and adds them to the active selection set. 選擇圍欄裏面的對象,並將其添加到活動選擇集。

Prompt for objects on screen and iterate the selection set 提示選擇屏幕上的對象並遍歷選擇集

This example prompts the user to select objects, then changes the color of each object selected to Green or the AutoCAD Color Index of 3.

本例提示用戶選擇對象,然後將選定的每個對象的顏色改爲綠色(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

      // 如果提示狀態OK,表示已選擇對象

      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

              // 確認返回的是合法的SelectedObject對象

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

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

 

Select objects with crossing window 選擇與窗口相交的對象

This example selects the objects within and that intersect a crossing window.

本例選擇窗口內的及切割窗口的對象。

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)

  // 從(2,2,0)到(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

  // 如果提示狀態OK,表示已選擇對象

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

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

 

3、Add To or Merge Multiple Selection Sets添加或合併多個選擇集

You can merge multiple selection sets by 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集合對象然後從多個選擇集中將對象id都添加到集合中。除了向ObjectIdCollection對象添加對象id,還可以從中刪除對象id。所有對象id都添加到ObjectIdCollection集合對象後,可以遍歷該集合並根據需要操作每個對象。

Add selected objects to a selection set 將所選對象添加到選擇集

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

 

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

  // 如果提示狀態OK,表示以選擇對象

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      // Get the selected objects

      // 獲取所選對象

      acSSet1 = acSSPrompt.Value;

 

      // Append the selected objects to the ObjectIdCollection

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

  // 如果提示狀態OK,表示以選擇對象

  if (acSSPrompt.Status == PromptStatus.OK)

  {

      acSSet2 = acSSPrompt.Value;

 

      // Check the size of the ObjectIdCollection, if zero, then initialize it

      // 檢查ObjectIdCollection集合大小,如果爲0就對其初始化

      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

              // 將第二個選擇集中的每個對象id添加到集合內

              acObjIdColl.Add(acObjId);

          }

      }

  }

 

  Application.ShowAlertDialog("Number of objects selected: " +

                              acObjIdColl.Count.ToString());

}

VBA/ActiveX Code Reference

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

 

 

發佈了4 篇原創文章 · 獲贊 20 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章