Exploring the autolisp SSGET function

If you have written routines with Autolisp, then you have probably used the (ssget) function to select entities on the screen, either automatically or by prompting the user.

(ssget) is a powerful function that can do more than you probably realize. Let’s look at a simple example.

(ssget '((0 . "TEXT")))

This prompts the user for a general selection set, but only TEXT entities are added to the resulting selection set.

(ssget '((0 . "*TEXT")))

Notice the wild card that was added. This is the same as above, except now any entity type that ends in TEXT is added to the selection set. At first, this looks like a good way to select TEXT and MTEXT, and it is. However, you have to be careful because this will also select RTEXT entities, and if your code is not equipped to deal with RTEXT, it may fail.

(ssget '((0 . "MTEXT,TEXT")))

This is a better way to select MTEXT and TEXT entities. But what if you don’t want to bother the user to select entities, you just want to select ALL the MTEXT and TEXT entities in the drawing?

(ssget "_X" '((0 . "MTEXT,TEXT")))

Notice the “_X” that was added. This tells the (ssget) function to evaluate every entity in the drawing and then the filtering mechanism will filter out everything except MTEXT and TEXT. Entities on frozen layers are included when using the “_X” selection method.

Let’s look at some other selection methods.

(ssget "_W" (list 5.0 5.0)(list 8.0 8.0))

Above is an example that will select objects inside a window from 5,5 to 8,8.

(ssget "_CP" (list (list 5.0 5.0)(list 8.0 8.0)(list 8.0 3.0)))

The line above will select all entities inside or touching a triangle defined by the points 5,5; 8,8; and 8,3. The CP is for crossing polygon.

The power of (ssget) comes from the incredibly fast filtering that it performs. Can you imagine having to evaluate an entire database of entities and manually filter out all the circles whose radius is less than 1.0 that fall inside a particular polygon? (ssget) can do this very fast. Example below.

(ssget "_W" 
 (list 5.0 5.0)(list 8.0 8.0)
 '((0 . "CIRCLE")(-4 . "<")(40 . 1.0))
)

Ok, getting back to the last (ssget) call in part 1…

(ssget "_W" 
 (list 5.0 5.0)(list 8.0 8.0)
 '((0 . "CIRCLE")(-4 . "<")(40 . 1.0))
)

The “_W” is the window selection method. Then we are passing it two points (5,5 and 8,8). Then we are telling it to only accept circle entities. The last two pieces tell it to only accept circles whose radius (DXF code 40) is less than 1.0.

So in summary, this bit of code will search the entire database and return only circles whose radius is less than 1.0 who also lie inside a polygon defined by the corners of 5,5 and 8,8.

The -4 group code is a special code that lets you perform relational testing. There are codes for equal, not equal, less than, less than or equal, greater than, greater than or equal, and two bitwise operators.

This type of filtering works great for locating entities on a certain layer, or that have a certain color or linetype.

(ssget "_X" 
  '((0 . "LINE")(8 . "TEMP"))
)

The code above will select all LINE entities on the TEMP layer.

(ssget "_X" 
  '((8 . "TEMP")(62 . 3))
)

The code above will select all entity types on the TEMP layer whose color is green (3). Be careful when filtering for color, linetype and lineweight. These filters only apply if the particular property is explicitly set. In the example above, if the color of all entities on the TEMP layer is set to BYLAYER, the selection set will be empty, even if the color of layer TEMP is green (3).

You can use wildcard matching in selection set filtering also.

(ssget "_X" 
  '((0 . "DIMENSION")(3 . "DIM##"))
)

The code above will select all dimensions whose dimstyle is DIM## (where the # represents a single numeral). For example, a dimension whose dimstyle is DIM55 or DIM39 will be selected. Dimensions with a dimstyle of DIM4T, DIM777, DIMKK, or DIM are not selected. Other wildcard syntax can be found under the (wcmatch) function in the Autolisp Reference Guide.

You may have figured it out by now, but “AND” is implied when when combining multiple filters such as above. In other words, in the above example, it’s going to find entities that have the type dimension AND whose dimension style is DIM##. There may be cases were you want to specify “AND”, especially if you are using an “OR” also.

(ssget "X" 
 '(
    (-4 . "<OR")
      (-4 . "<AND")
        (0 . "CIRCLE")
        (40 . 1.0)
      (-4 . "AND<")
      (-4 . "<AND")
        (0 . "LINE")
        (8 . "ABC")
      (-4 . "AND<")
    (-4 . "OR<")
  )
)

The code above is straight out of the Autolisp Developer’s Guide. It creates a selection set out of CIRCLE entities that have a radius of exactly 1.0 and LINE entities on the ABC layer. If you did not use the “OR” condition, then the (ssget) function would be trying to find entities that are a CIRCLE and a LINE, which is obviously impossible. Let’s look at one more example.

(ssget '((0 . "POLYLINE,LWPOLYLINE")(-4 . "&")(70 . 1)))

Last, but not least… The code above uses a “bitwise AND” to filter on a bit coded DXF code. In this case, DXF code 70 on a POLYLINE and/or LWPOLYLINE. The goal here is to create a selection set of closed polylines. If DXF code 70 has the “1” bit set, this indicates a closed polyline. However, we cannot simply filter for DXF code 70 = 1, because this is a bit coded field. If LTGEN is turned on and it’s a closed polyline, then DXF code 70 will equal 129, not 1. This is why you must use the “&” (Bitwise AND) special filter.

There is much more information in the Autolisp Developer’s Guide on advanced selection set handling. Good luck and post some of your examples in the comments section if you want.