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,0),_
New Point3d(10,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,0)
// 从(2,0)到(10,0)创建一个交叉窗口
PromptSelectionResult acSSPrompt;
acSSPrompt = acDocEd.SelectCrossingWindow(new Point3d(2,
new Point3d(10,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,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,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