Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002...

12
Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / [email protected] Course Title: Getting the Correct Selection Set Every Time Course ID: CP34-3 Course Outline: This course teaches various techniques for selecting objects using AutoLISP and Visual Basic. Programmers often know what needs to be done but have a difficult time automatically selecting the objects they want to manipulate. This class offers a thorough explanation of selection set filters and capabilities as well as various other techniques that can be used along or in conjunction to get a programmer the right selection set of entities every time. Examples of AutoLISP and VB code are provided.

Transcript of Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002...

Page 1: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Las Vegas, Nevada, December 3–6, 2002

Speaker Name: Darren J. Young / [email protected]

Course Title: Getting the Correct Selection Set Every Time

Course ID: CP34-3

Course Outline: This course teaches various techniques for selecting objects using AutoLISP and Visual Basic. Programmers often know what needs to be done but have a difficult time automatically selecting the objects they want to manipulate. This class offers a thorough explanation of selection set filters and capabilities as well as various other techniques that can be used along or in conjunction to get a programmer the right selection set of entities every time. Examples of AutoLISP and VB code are provided.

Page 2: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 2

Selection Methods

Selection mode is NOT allowed as a parameter to the (ssget) function. However, if you type “G” or “Group” at the select objects prompt after calling (ssget), you will be prompted to enter a group name.

Selection mode modifiers that may be used alone, or in combination with other selection mode modifiers. Selection mode modifiers are intended to be used interactively with the user selecting the objects they want at a “Select Objects” prompt.

This selection mode modifier filters the Paperspace viewport entity (the layout itself) not modelspace viewport entities created in a layout (Paperspace) with the MVIEW command or other methods.

AutoLisp Visual Basic (VB & VBA) Selection Action Type

Func

tion

Mod

e

Met

hod

Mod

e

Auto

mat

ic o

per

atio

n \

No u

ser

inte

ract

ion

Inte

ract

ive

Oper

atio

n \

Use

r In

tera

ctio

n

Spec

ifie

d a

t “S

elec

t O

bje

cts”

pro

mpt

Spec

ifie

d a

s ar

gum

ent

to

fti

/th

dSupport

s optional

filt

er a

rgum

ents

Sin

gle

(one)

poin

t ar

gum

ent

requir

ed

Double

(tw

o)

poin

t arg

um

ents

req

uir

ed

List

of

poin

ts r

equir

ed (

min

of

two p

oin

ts)

Can

be

use

d in w

/oth

er s

elec

tion m

odes

Crossing (ssget) “C” .Select acSelectionSetCrossing

Crossing Polygon (ssget) “CP” .SelectByPolygon acSelectionSetCrossingPolygon

Fence (ssget) “F” .SelectByPolygon acSelectionSetFence

Implied (ssget) “I” N/A N/A

Group (ssget) N/A N/A N/A

Last (ssget) “L” .Select acSelectionSetLast

Previous (ssget) “P” .Select acSelectionSetPrevious

Window (ssget) “W” .Select acSelectionSetWindow

Window Polygon (ssget) “WP” .SelectByPolygon acSelectionSetWindowPolygon

Everything in database (ssget) “X” .Select acSelectionSetAll

Select on screen (ssget) N/A .SelectOnScreen N/A

Duplicates allowed (ssget) “:D” N/A N/A

Everything within cursor’s pickbox

(ssget) “:E” .SelectAtPoint N/A

Rejects locked layers (ssget) “:L” N/A N/A

Store additional info about selection set

(ssget) “:N” N/A N/A

Rejects Paperspace Viewport

(ssget) “:P” N/A N/A

Single selection only (ssget) “:S” N/A N/A

Page 3: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 3

Selection Filters

To quickly build selection set filters in either AutoLISP or VB/VBA code, you will be frequently referring to the DXF reference located in the “Developer Help” section of the AutoCAD help system. Selection set filters specify what you are looking to select (or not select) in the drawing by using the values found in the DXF reference for the appropriate entities.

It’s not necessary to memorize everything in the DXF Reference although you’ll find that you routinely use certain things over and over. However, you should take the time to familiarize yourself with navigating the DXF reference to find what you want.

Select “Developer Help” from the AutoCAD “Help” pull down menu.

Select “DXF Reference” from the developer help documentation

Page 4: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 4

Selection Filters

Each DXF code is paired up with a corresponding piece of data that’s associated with that code. This data can be in many formats depending on the code. The formats for this associated data can be strings (“This is a string”), real numbers (1.375), integers (45) or other data types. For a list of generic descriptions of what each code is for, look in the “Group Codes in Numerical Order” section of the DXF Reference. To see what data type it’s associated code uses, look in the Group Code Value Types” section of the DXF Reference.

List of DXF code descriptions in numerical order List of data types associated with each DXF code

Page 5: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 5

Selection Filters

Not all DXF codes are listed for each entity. Many DXF codes are common across all entities. These codes can be found in the “Common Group Codes for Entities” section of the DXF reference. Things that can be found here are properties that are common across all or most entities. Things like the entity type (DXF code 0), or layer (DXF code 8) can be found in this section.

List of DXF group codes common to most entities

Page 6: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 6

Selection Filters

Each entity has DXF codes common to many entities but containing data specific only to that particular entity. These are the codes you’ll most frequently need to look up because there are so many of them and the same DXF code can mean many different things depending on the entity.

DXF codes specific to “Line” entities DXF codes specific to “Circle” entities

Notice that the DXF code 10 means different things for a Circle and a Line. For a Line, the DXF code 10 indicates the “Start Point” of the line. For the “Circle”, the DXF code 10 indicates the “Center Point” of the Circle.

There’s one more thing you’ll want to note about the DXF reference. Look at the description for the DXF codes 10, 20 and 30. You’ll often see the prefix “DXF:” and “APP:” in the descriptions, most commonly (but not exclusively) in codes that store coordinate point data. This tells you that the DXF codes appear differently when accessed in a DXF file (“DXF:” prefix) and the AutoCAD application (“APP:” prefix).

In a DXF file, each code is associated with only one piece of data. Considering that a coordinate point consists of three separate coordinates (X, Y, and Z), three separate DXF codes are used in the DXF file. DXF code 10 stores the X-coordinate in a DXF file, DXF code 20 stores the Y-coordinate in a DXF file and DXF code 30 stores the Z-coordinate in a DXF file. If you look at the codes for a Line above, you’ll see this same pattern for the ending point as well, that is, it uses DXF codes 11, 21, and 31 to store the ending point of a line. For the most part, unless your program reads or writes a DXF file, you won’t be concerned with this. However, it’s important to know how and why these codes are used so that they aren’t mistakenly used incorrectly when building your selection filters.

When building your selection filters, you’re program will be selecting entities inside the AutoCAD application (or AutoCAD based vertical product) so the codes you’ll be concerned with when you see the “DXF:” or “APP:” prefix, are those that are prefixed with the “APP:”. In the images above, you can see that the lines starting point (X, Y, AND Z) is entirely stored in the DXF code 10. The same holds true for the Circle’s center point, which is also stored completely in the group code 10. Likewise, the Line’s entire ending coordinate is stored completely with the DXF code 11.

DXF codes that don’t use either the “APP:” or “DXF:” prefix, are used the same in DXF files as they are in the AutoCAD application.

Page 7: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 7

Selection Filters

In AutoLISP, building your filter is as easy as specifying the DXF codes and corresponding data you want…

(setq myselection (ssget “x” ‘((0 . “LINE”)))) ;Selects ALL Line entities

-or-

(setq myselection (ssget “x” ‘((0 . “LINE”)(8 . “WALL”)))) ;Selects ALL Line entities on layer WALL

Notice that the filter is a quoted list of lists. But what if a variable stores the value for the layer you want to use? A quoted list won’t work because a quoted list isn’t “evaluated”.

(setq mylayer “WALL”) (setq myselection (ssget “x” ‘((0 . “LINE”)(8 . mylayer)))) :<- Incorrect

AutoLISP doesn’t evaluate the variable mylayer to know that its value is “WALL”.

(setq mylayer “WALL”) (setq myselection (ssget “x” (list ‘(0 . “LINE”)(cons 8 mylayer)))) ;<- Correct

Here, DXF code 8 is evaluated so AutoLISP knows the variable mylayer has a value of “WALL”. The DXF code 0 is quoted because there is no variable used. The entire list is build with the (list) function so that the DXF code 8 will get evaluated.

In Visual Basic (VB & VBA) this process is a little different. Visual Basic doesn’t support “Lists”. Instead, Visual Basic uses “Arrays” to build the selection filter but the data they contain is really the same.

Dim objSS as AcadSelectionSet Dim intCode(0) As Integer, varData(0) As Variant intCode(0) = 0 varData(0) = "LINE” On Error Resume Next Err.Clear Set objSS = ThisDrawing.SelectionSets.Item(“MySelection”) If Err.Number > 0 Then objSS.Clear Err.Clear Else Set objSS = ThisDrawing.SelectionSets.Add(“MySelection”) EndIf objSS.Select acSelectionSetAll, , ,intCode,varData ‘Selects ALL Line entities

-or-

Dim objSS as AcadSelectionSet Dim intCode(0 to 1) As Integer, varData(0 to 1) As Variant intCode(0) = 0: intCode(1) = 8 varData(0) = "LINE": varData(1) = “WALL” On Error Resume Next Err.Clear Set objSS = ThisDrawing.SelectionSets.Item(“MySelection”) If Err.Number > 0 Then objSS.Clear Err.Clear Else Set objSS = ThisDrawing.SelectionSets.Add(“MySelection”) EndIf objSS.Select acSelectionSetAll, , ,intCode,varData ‘Selects ALL Line entities on layer WALL

Notice use a filter in VB/VBA that the filter uses 2 separate arrays. One array is nothing but integers representing the DXF codes. The second array is defined as a variant because it can store different types of data. The DXF code in the first element of one array is associated to the data in the first element of the other array, the second to the second and so on.

In the case where a particular piece of data is stored in a variable, there’s no need to worry about “evaluating” that variable because VB/VBA evaluates it when it’s stored in the array. In fact, the entire array itself IS a variable.

Page 8: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 8

Logical Filter Grouping

Not all selection filters are so basic as the previous examples. Perhaps you want to select Lines –AND- Circles in your selection set. Initial thoughts would indicate that the filters are structured like this…

AutoLISP: (setq myselection (ssget “x” ‘((0 . “LINE”)(0 . “CIRCLE”)))) ;Incorrectly built filter to select Lines & Circles

Visual Basic: Dim intCode(0 to 1) As Integer, varData(0 to 1) As Variant ‘Incorrectly build filter to select Lines & Circles intCode(0) = 0: intCode(1) = 0 varData(0) = "LINE": varData(1) = “CIRCLE”

The above is incorrect because instead of trying to select all Lines and Circles, it instead tries to select all entities that are Lines and Circles. In AutoCAD, there are no entities that are both a Line and a Circle. In cases like this, AutoCAD provides some special codes that are used to logically group filters allowing you to select what you want. These filters use special DXF codes and data just like any other filter. If you look in the “DXF Codes in Numerical Order” section of the DXF reference, you’ll see that this special DXF code is –4 and only applies to the programs running in the AutoCAD application (“APP:” prefix in the description) and are never found in a DXF file. Here is a list of the logical grouping filters. These logical grouping filters are used in pairs.

Code Data Description

“<AND” Mark the beginning of an “AND” grouping. One or more filters in the group. - “This” AND “That” -4

”AND>” Mark the ending of an “AND” grouping. One or more filters in the group. - “This” AND “That”

“<OR” Mark the beginning of an “OR” grouping. One or more filters in the group. - “This” OR “That” -4

”OR>” Mark the ending of an “OR” grouping. One or more filters in the group. - “This” OR “That”

“<XOR” Mark the beginning of an “XOR” grouping. Two operations only in the group. - “This” OR “That” but not both “This” AND “That”.

-4

”XOR>” Mark the ending of an “XOR” grouping. Two operations only in the group. - “This” OR “That” but not both “This” AND “That”.

“<NOT” Mark the beginning of an “NOT” grouping. One operation only in the group. - NOT “This”. -4

”NOT>” Mark the ending of an “NOT” grouping. One operation only in the group. - NOT “This”.

AutoLISP Filter: (setq myselection (ssget “x” ‘((-4 . “<OR”)(0 . “LINE”)(0 . “CIRCLE”)(-4 . “OR>”))));Filter for all Lines & Circles

(setq myselection (ssget “x” ‘((-4 . “<OR”) ;Filter for all Lines on layer Wall & Circles on layer Ceiling (-4 “<AND”) (0 . “LINE”)(8 . “WALL”) (-4 “AND>”) (-4 “<AND”) (0 . “CIRCLE”)(8 . “CEILING”) (-4 “AND>”) (-4 . “OR>”))

))

Visual Basic Filter: Dim intCode(0 to 3) As Integer, varData(0 to 3) As Variant ‘Filter for all Lines & Circles intCode(0) = -4: intCode(1) = 0: intCode(2) = 0: intCode(3) = -4 varData(0) = “<OR”: varData(1) = "LINE": varData(2) = “CIRCLE”: varData(3) = “OR>”

Dim intCode(0 to 9) As Integer, varData(0 to 9) As Variant ‘Filter for all Lines on layer Wall & Circles on layer Ceiling intCode(0) = -4: intCode(1) = -4: intCode(2) = 0: intCode(3) = 8: intCode(4) = -4 intCode(5) = -4: intCode(6) = 0: intCode(7) = 8: intCode(8) = -4: intCode(9) = -4 varData(0) =“<OR”: varData(1)=”<AND”: varData(2)="LINE": varData(3)=“WALL”: varData(4)=“AND>” varData(5) =“<AND”: varData(6)=”CIRCLE”: varData(7)="CEILING": varData(8)=“AND>”: varData(9)=“OR>”

Page 9: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 9

Relational Filter Tests

In addition to logical filter grouping, relational tests can also be performed on filters. These relational test filters are like the logical grouping filters in that they also use a –4 DXF group code. But unlike the logical grouping filters that are used in pairs, relational test filters are used one at a time and apply only to the filter immediately following it. Here is a list of the relational test filters…

Code Data Description

-4 “*” Anything goes – Always true

-4 “=” Equals

-4 “!=” Not equal to

-4 “/=” Not equal to

-4 “<>” Not equal to

-4 “<” Less than

-4 “<=” Less than or equal to

-4 “>” Greater than

-4 “>=” Greater than or equal to

-4 “&” Bitwise AND – Valid only for integer data groups (check if the specified bit flag is set in group)

-4 “&=” Bitwise masked equals – Valid only for integer groups (specified bit flag matches exactly to group)

AutoLISP Filter: (setq myselection (ssget “x” ‘((0 . “CIRCLE”)(-4 . “<”)(40 . 10.0))));Filter for all Circles with radius less than 10 units

Visual Basic Filter: Dim intCode(0 to 2) As Integer, varData(0 to 2) As Variant ‘Filter for all Circles with radius less than 10 units intCode(0) = 0: intCode(1) = -4: intCode(2) = 40 varData(0) = “CIRCLE”: varData(1) = "<": varData(2) = 10.0

Relational Test Filter Restrictions:

• All relational test filters are valid for Real & Integer groups except the Bitwise relational test filters (“&” and “&=”)

• Bitwise relational test filters (“&” and “&=”) are only valid for Integer groups

• Direction vectors (DXF code 210 group) can only be tested with “*”, “=”, and “!=” or one of the equivalent “not equal” strings.

• Relational test filters will not work on String groups

Page 10: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 10

Wildcard Filter Tests

The last major filter item is wildcard matching. Relational test filters don’t work on String data groups so that’s where Wildcard matching comes into play. With Wildcard matching, string data groups can easily be included in your filters to select items within a wide variety of parameters. Unlike the logical filter grouping or relational filter tests, wildcard filters don’t require any special DXF codes. Instead, wildcards are specified right in the data string for the DXF code you are looking to match. However, like the other filters, wildcards can be used in combination with each other. Here’s a list of the wildcard matching available…

Wildcard Description

# Pound – Any single numeric digit

@ At – Any single alphabetic character

. Period – Any single non-alphanumeric character

* Asterisk – Any character sequence

? Question – Any single character

~ Tilde – If first character in pattern, matches anything but pattern

[…] Matches any of the enclosed characters

[~…] Matches anything but one of the enclosed characters

- Hyphen – Used inside brackets to specify a range for a single character

, Comma – Separates two patterns

` Reverse Quote – Reads the next character literally

AutoLISP Filter: (setq myselection (ssget “x” ‘((0 . “TEXT”)(1 . “TEST#”))))

Visual Basic Filter: Dim intCode(0 to 1) As Integer, varData(0 to 1) As Variant intCode(0) = 0: intCode(1) = 1 varData(0) = “TEXT”: varData(1) = "TEST#"

Would select the following text: “TEST1”, “TEST5”, “TEST2”

Would not select the following text: “GREEN”, “TEST1A”, “TEST33”, “TEST”, “TESTSTRING”

AutoLISP Filter: (setq myselection (ssget “x” ‘((0 . “LINE”)(8 . “WALL,ELEC[1-9]P*,TEST-@*”))))

Visual Basic Filter: Dim intCode(0 to 1) As Integer, varData(0 to 1) As Variant intCode(0) = 0: intCode(1) = 1 varData(0) = “LINE”: varData(1) = " WALL,ELEC[1-9]P*,TEST-@*"

Would select Lines on the following layers: “WALL”, “ELEC1POWER”, “ELEC7POWER”, “ELEC9P”, “TEST-APP”, ”TEST-BOX”

Would not select Lines on the following layers: “CEILING”, “ELEC1BOX”, “ELECTRIC”, “ELEC98”, “TESTING”, ”TEST-1BORD”

Page 11: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 11

Selection Filter Wrap-up

• Selection filters can be used with most any selection method (Everything, Crossing, …etc…)

• Any of the selection filtering methods (logical grouping, relational tests, wildcard matching) can be used alone or in any combination with each other provided each is being used properly.

• Wildcard filter matching is not explained ANYWHERE in the AutoCAD help system except under the AutoLISP Reference in the following 2 sections…

• Despite only being discussed in the AutoLISP Reference, Wildcard matching in selection filters works exactly the same when building filters in Visual Basic (VB/VBA).

• (ssget) is the only function needed to select objects in AutoLISP

• Multiple methods are used to select objects in Visual Basic depending on the selection method needed.

• Filtering can be done to select entities with XDATA

(ssget "X" '((0 . "CIRCLE") (-3 ("APPNAME"))))

• AND is assumed when multiple applications are specified in an XDATA filter

(ssget "X" '((0 . "CIRCLE") (-3 ("APP1") ("APP2"))))

• Wildcard are permitted when filtering for XDATA (either application can be attached)

(ssget "X" '((0 . "CIRCLE") (-3 ("APP[12]"))))

(ssget "X" '((0 . "CIRCLE") (-3 ("APP1,APP2"))))

• Use (command “select” …) to help build your selection sets using “add” and “remove” to - combine/remove one selection set to/from another.

• If your selection filter doesn’t work because it’s too complex, build smaller, simpler selection sets with (command “select” …). When you have build your selection set with this method, then use (ssget “p”) in AutoLISP or ‘.Select acSelectionSetPrevious’ in Visual Basic to select the previous selection set and manipulate it further in your code.

• Refer to the DXF Reference to help build your selection filters

• Use drafting standards that make use of styles, layers, naming conventions, etc. to help separate your CAD data so it’s more easily selected with a filter.

• Download this handout, samples files, sample code and power point presentation at this web address… ftp://ftp.mcwi.com/au.

• Refer to the sample code & files to examples of how filters are put together.

Page 12: Las Vegas, Nevada, December 3–6, 2002 · 2011. 8. 25. · Las Vegas, Nevada, December 3–6, 2002 Speaker Name: Darren J. Young / dyoung@mcwi.com Course Title: Getting the Correct

Getting the Correct Selection Set Every Time

www.autodesk.com 12

• Please feel free to contact me via Email after you leave Autodesk University with any questions or problems on selecting objects in AutoCAD. Just indicate that you were one of my students at Autodesk University.