Selecting Entities

Newly created SelectionSet objects are empty. A SelectionSet is populated either by adding an array of objects, which I describe how to do later in this chapter, or by using one of the SelectXXX methods I describe in this section. There are SelectXXX methods for obtaining entities by specific point, by window, by arbitrary fence or polygon, by dynamic user selection, and by filtering the entities using specific criteria.

The Select Method

The Select method (see Table 12-2 for parameter details) is the basic way to add entities to SelectionSet objects. It allows you to select the previous selection set, the last visible entity, everything in the current space, or rectangular region.

SelectionSetObject.Select Mode [, Point1, Point2] _ [, FilterCodes, FilterValues]

The Select method has several modes of operation specified by the Mode parameter. AutoCAD provides the following AcSelect enum constants for use with this parameter, as shown in Table 12-3.

Table 12-2. Select Method Parameters

Name

Data Type

Description

Mode

Long

Determines the selection mode to be used. It must be one of the AcSelect constants described in Table 12-3.

Point1

Variant

An optional three-element array of doubles indicating the first corner of a rectangular region. It must be used in conjunction with Point2.

Point2

Variant

An optional three-element array of doubles indicating the other corner of a rectangular region. It must be used in conjunction with Point1.

FilterCodes

Variant

An optional array of integer entity selection filter codes. It must be used in conjunction with, and have the same length as, FilterValues.

FilterValues

Variant

An optional array of variant entity selection filter values. It must be used in conjunction with, and have the same length as, FilterCodes.

Table 12-3. Select Method Modes

Constant

Value

Description

acSelectionSetWindow

0

Entities completely contained within a window specified by Point1 and Point2 are selected.

acSelectionSetCrossing

1

Entities contained or crossing over a window specified by Point1 and Point2 are selected.

acSelectionSetPrevious

3

Entities from the most recent selection set in the current space are selected. Point1 and Point2 aren't used.

acSelectionSetLast

4

The most recently created entity within the current viewport is selected. Point1 and Point2 aren't used.

acSelectionSetAll

5

All entities in the current space are selected. Point1 and Point2 aren't used.

■ Note The Select method is capable of selecting entities on all layers, even if a certain layer is frozen or locked.

The following example creates a new selection set and then populates it based on user input. Try each of the modes to see how they differ.

Public Sub TestSelect()

Dim objSS As AcadSelectionSet Dim varPnt1 As Variant Dim varPnt2 As Variant Dim strOpt As String Dim lngMode As Long

On Error GoTo Done

With ThisDrawing.Utility

'' get input for mode

.InitializeUserInput 1, "Window Crossing Previous Last All"

"Select [Window/Crossing/Previous/Last/All]: ")

'' convert keyword into mode Select Case strOpt

Case "Window": lngMode = acSelectionSetWindow Case "Crossing": lngMode = acSelectionSetCrossing Case "Previous": lngMode = acSelectionSetPrevious Case "Last": lngMode = acSelectionSetLast Case "All": lngMode = acSelectionSetAll End Select

'' create a new selectionset

Set objSS = ThisDrawing.SelectionSets.Add("TestSelectSS")

'' if it's window or crossing, get the points If "Window" = strOpt Or "Crossing" = strOpt Then

'' get first point .InitializeUserInput 1

varPnt1 = .GetPoint(, vbCr & "Pick the first corner: ")

'' get corner, using dashed lines if crossing .InitializeUserInput 1 + IIf("Crossing" = strOpt, 32, 0) varPnt2 = .GetCorner(varPnt1, vbCr & "Pick other corner: ")

'' select entities using points objSS.Select lngMode, varPnt1, varPnt2

Else

'' select entities using mode objSS.Select lngMode End If

'' highlight the selected entities objSS.Highlight True

'' pause for the user

.GetString False, vbCr & "Enter to continue"

'' unhighlight the entities objSS.Highlight False

End With

Done:

'' if the selectionset was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub

Selection Set Filters

You use the optional FilterCodes and FilterValues parameters to filter the selection of entities based on some criteria. Common criteria include objects with a specific color or linetype, or objects on a particular layer.

The FilterCodes parameter is an array of integers that specifies the entity group codes to filter. Entity group codes are available for every entity property. See the AutoCAD DXF Reference for a complete listing of the available group codes. The FilterValues parameter is an array of variants that specifies the corresponding values for each entity group code present in the FilterCodes parameter. Because these two arrays have a one-to-one mapping, they must be of the same length.

The following example filters all the entities into a selection set based on a layer name entered by the user:

Public Sub TestSelectionSetFilter() Dim objSS As AcadSelectionSet Dim intCodes(0) As Integer Dim varCodeValues(0) As Variant Dim strName As String

On Error GoTo Done

With ThisDrawing.Utility strName = .GetString(True, vbCr & "Layer name to filter: ") If "" = strName Then Exit Sub

'' create a new selectionset

Set objSS = ThisDrawing.SelectionSets.Add("TestSelectionSetFilter")

'' set the value specified by user varCodeValues(0) = strName

'' filter the objects objSS.Select acSelectionSetAll, , , intCodes, varCodeValues

'' highlight the selected entities objSS.Highlight True

'' pause for the user

.Prompt vbCr & objSS.Count & " entities selected" .GetString False, vbLf & "Enter to continue "

'' unhighlight the entities objSS.Highlight False End With

Done:

'' if the selection was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub

When multiple filter codes are specified, they're implicitly combined using a logical AND operator. You can explicitly control how codes contribute to the selection criteria by using filter operators. The filter operators are designated by a special group code of -4 and special string keywords for the values. You use filter operators in pairs surrounding a number of filter codes known as operands.

Table 12-4 lists the SelectionSet filter operators, their meanings, and the required number of operands.

Table 12-4. SelectionSet Filter Operators

Filter Operator Start and End Value

Number of Operands Description

One or more

One or more

A logical AND of all the operands. If a criterion matches all of the operands, it will be included. This is the default for multiple criteria when no filter operators are specified.

A logical OR of all the operands. If a criterion matches any of the operands, it will be included.

Continued

266 CHAPTER 12 ■ SELECTION SETS AND GROUPS

Table 12-4. (Continued)

Filter Operator

Start and End Value Number of Operands

Description

"<XOR"

... Exactly two "XOR>"

A logical XOR of two operands. If a criterion matches one but not the other, it will be included.

"<NOT"

... Exactly one "NOT>"

A logical NOT of a single operand. If a criterion doesn't match the operand, it will be included.

The following example uses filter operators to limit the selection to lines, arcs, and circles that are not on a specified layer:

Public Sub TestSelectionSetOperator() Dim objSS As AcadSelectionSet Dim intCodes() As Integer Dim varCodeValues As Variant Dim strName As String

On Error GoTo Done

With ThisDrawing.Utility strName = .GetString(True, vbCr & "Layer name to exclude: ") If "" = strName Then Exit Sub

'' create a new selectionset

Set objSS = ThisDrawing.SelectionSets.Add("TestSelectionSetOperator") '' using 9 filters

ReDim intCodes(9): ReDim varCodeValues(9)

'' set codes

and

intCodes(0)

= -4

intCodes(1)

= -4

intCodes(2)

= 0:

intCodes(3)

= 0:

intCodes(4)

= 0:

intCodes(5)

= -4

intCodes(6)

= -4

intCodes(7)

= 8:

intCodes(8)

= -4

intCodes(9)

= -4

varCodeValues(0) = "<and" varCodeValues(1) = "<or" varCodeValues(2) = "line" varCodeValues(3) = "arc" varCodeValues(4) = "circle" varCodeValues(5) = "or>" varCodeValues(6) = "<not"

varCodeValues(7) = strName varCodeValues(8) = "not>" varCodeValues(9) = "and>"

'' filter the objects objSS.Select acSelectionSetAll, , , intCodes, varCodeValues

'' highlight the selected entities objSS.Highlight True

'' pause for the user

.Prompt vbCr & objSS.Count & " entities selected" .GetString False, vbLf & "Enter to continue "

'' unhighlight the entities objSS.Highlight False End With

Done:

'' if the selection was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub

The group code values can contain wild-card expressions if the code represents a string property. You can often use wild-card expressions to make filter criteria more succinct. For example, the preceding filter took nine group codes to define. By using wild cards, you can reduce it to just two:

ReDim intCodes(1): ReDim varCodeValues(1) intCodes(0) = 0: varCodeValues(0) = "line,arc,circle" intCodes(1) = 8: varCodeValues(1) = "~" & strName

See the AutoCAD 2006 Customization Guide for more details on wild-card expressions.

The SelectOnScreen Method

You use this method to obtain a selection of entities interactively from the user (see Table 12-5 for parameter details). A Select Objects prompt is shown at the command line, where all the standard selection-set options such as Window, Crossing, and Last are available to the user for dynamic selection.

SelectionSetObject.SelectOnScreen [, FilterCodes, FilterValues]

Table 12-5. SelectOnScreen Method Parameters

Name

Data Type

Description

FilterCodes

Variant

An optional array of integer entity selection filter codes. This must be used in conjunction with, and have the same length as, FilterValues.

FilterValues

Variant

An optional array of variant entity selection filter values. This must be used in conjunction with, and have the same length as, FilterCodes.

Like the Select method, SelectOnScreen can filter the selected entities using criteria specified in the FilterCodes and FilterValues parameters. See the "Selection Set Filters" section for a full description of filtering.

The following example gets a selection of entities dynamically from the user:

Public Sub TestSelectOnScreen()

Dim objSS As AcadSelectionSet

On Error GoTo Done With ThisDrawing.Utility

'' create a new selectionset

Set objSS = ThisDrawing.SelectionSets.Add("TestSelectOnScreen")

'' let user select entities interactively objSS.SelectOnScreen

'' highlight the selected entities objSS.Highlight True

'' pause for the user

.Prompt vbCr & objSS.Count & " entities selected" .GetString False, vbLf & "Enter to continue "

'' unhighlight the entities objSS.Highlight False

End With

Done:

'' if the selection was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub

The SelectAtPoint Method

The SelectAtPoint method (see Table 12-6 for parameter details) selects a single entity that passes through a specified point. Unlike most of the other SelectXXX methods (except the acSelectionSetLast mode of Select), it selects only a single entity.

SelectionSet.SelectAtPoint Point [,FilterCodes, FilterValues]

Table 12-6. SelectAtPoint Method Parameters

Name

Data Type

Description

Point

Variant

A three-element array of doubles indicating the point through which the selected entity must pass.

FilterCodes

Variant

An optional array of integer entity selection filter codes. This must be used in conjunction with, and have the same length as, FilterValues.

FilterValues

Variant

An optional array of variant entity selection filter values. This must be used in conjunction with, and have the same length as, FilterCodes.

Like the other Select methods, SelectAtPoint can filter the selected entities using criteria specified in the FilterCodes and FilterValues parameters. (This was covered earlier in the "Selection Set Filters" section.)

The following example selects an entity passing through a point chosen by the user:

Public Sub TestSelectAtPoint() Dim varPick As Variant Dim objSS As AcadSelectionSet

On Error GoTo Done

With ThisDrawing.Utility

'' create a new selectionset

Set objSS = ThisDrawing.SelectionSets.Add("TestSelectAtPoint")

'' get a point of selection from the user varPick = .GetPoint(, vbCr & "Select entities at a point: ")

'' let user select entities interactively objSS.SelectAtPoint varPick

'' highlight the selected entities objSS.Highlight True

'' pause for the user

.Prompt vbCr & objSS.Count & " entities selected" .GetString False, vbLf & "Enter to continue "

'' unhighlight the entities objSS.Highlight False

End With

'' if the selection was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub

The SelectByPolygon Method

This method (see Table 12-7 for parameter details) selects entities by using a boundary that may be either a closed polygon or an open figure called a fence. In the case of a closed polygon, you can further limit entities to only those lying completely within the polygon (Window-Polygon) or those that pass through the boundary, as well (Crossing-Polygon).

SelectionSetObject.SelectByPolygon Mode, Vertices _ [, FilterType, FilterData]

Table 12-7. SelectByPolygon Method Parameters

Name

Data Type

Description

Mode

Long

Determines the selection mode to be used. It must be one of the AcSelect constants described in Table 12-8.

Vertices

Variant

A required array of doubles specifying a list of 3-D vertex points in WCS coordinates. It's a simple array with a single dimension composed of alternating X, Y, and Z values (i.e., p1x, p1y, p1z, p2x, p2y, p2z, etc.).

FilterType

Variant

An optional array of integer entity selection filter codes. This must be used in conjunction with, and have the same length as, FilterData.

FilterData

Variant

An optional array of variant entity selection filter values. This must be used in conjunction with, and have the same length as, FilterType.

The SelectByPolygon method has several modes of operation specified by the Mode parameter. AutoCAD provides the AcSelect constants listed in Table 12-8 for use with this parameter.

Table 12-8. SelectByPolygon Method Modes

Constant_Value Description_

acSelectionSetFence 2 Entities crossing a multisegment fence specified by Vertices. At least two vertices must be specified.

acSelectionSetWindowPolygon 6 Entities completely contained within a polygon specified by the Vertices are selected. At least three vertices must be specified.

acSelectionSetCrossingPolygon 7

Entities contained within or crossing a polygon specified by the Vertices are selected. At least three vertices must be specified.

Like the other Select methods, SelectByPolygon can filter the selected entities using criteria specified in the FilterCodes and FilterValues parameters. See the "Selection Set Filters" section for a full description of filtering.

The following example selects entities based on a user-specified boundary and mode:

Public Sub TestSelectByPolygon()

Dim objSS As AcadSelectionSet Dim strOpt As String Dim lngMode As Long Dim varPoints As Variant

On Error GoTo Done With ThisDrawing.Utility

'' create a new selectionset

Set objSS = ThisDrawing.SelectionSets.Add("TestSelectByPolygon1")

'' get the mode from the user

.InitializeUserInput 1, "Fence Window Crossing"

strOpt = .GetKeyword(vbCr & "Select by [Fence/Window/Crossing]: ")

'' convert keyword into mode Select Case strOpt

Case "Fence": lngMode = acSelectionSetFence Case "Window": lngMode = acSelectionSetWindowPolygon Case "Crossing": lngMode = acSelectionSetCrossingPolygon End Select

'' let user digitize points varPoints = InputPoints()

'' select entities using mode and points specified objSS.SelectByPolygon lngMode, varPoints

'' highlight the selected entities objSS.Highlight True

'' pause for the user

.Prompt vbCr & objSS.Count & " entities selected" .GetString False, vbLf & "Enter to continue "

'' unhighlight the entities objSS.Highlight False

End With

'' if the selection was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub

This example uses a helper function named InputPoints to obtain the boundary points. Here's the code for this function:

Function InputPoints() As Variant Dim varStartPoint As Variant Dim varNextPoint As Variant Dim varWCSPoint As Variant Dim lngLast As Long Dim dblPoints() As Double

On Error Resume Next '' get first points from user With ThisDrawing.Utility .InitializeUserInput 1

varStartPoint = .GetPoint(, vbLf & "Pick the start point: ")

'' setup initial point ReDim dblPoints(2) dblPoints(0) = varStartPoint(0) dblPoints(1) = varStartPoint(1) dblPoints(2) = varStartPoint(2) varNextPoint = varStartPoint

'' append vertexes in a loop Do

'' translate picked point to UCS for basepoint below varWCSPoint = .TranslateCoordinates(varNextPoint, acWorld, _

acUCS, True)

'' get user point for new vertex, use last pick as basepoint varNextPoint = .GetPoint(varWCSPoint, vbCr & _

'' exit loop if no point picked If Err Then Exit Do

'' get the upper bound lngLast = UBound(dblPoints)

'' expand the array

ReDim Preserve dblPoints(lngLast + 3)

'' add the new point dblPoints(lngLast + 1) dblPoints(lngLast + 2) dblPoints(lngLast + 3)

Loop End With

'' return the points InputPoints = dblPoints End Function

Figure 12-1 illustrates the results of the three selection options. The solid shapes represent AutoCAD drawing objects, and the dashed line represents the path of the selecting polygon.

Vertex2 Vertexl VertexS

Vertex2 Vertexl VertexS

Figure 12-1. TestSelectByPolygon example output

The following parameters are used to define the area selected: acSelectionSetFence: only the ellipse is selected acSelectionSetWindowPolygon: the circle and square are both selected acSelectionSetCrossingPolygon: all objects are selected

Was this article helpful?

+2 -4
100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook


Responses

  • ULRICH
    How to cancel the prompt select object in the command bar when i use SelectOnScreen in vb?
    8 years ago
  • emppu
    How to add more than 1 layer into selectionset in vba autoacad?
    1 day ago

Post a comment