AfraLISP - Learn AutoLISP for AutoCAD productivity

Selection Set Filters

by Jarvis Fosdick

Very often I will want to select all items on a layer and move them off to the right hand side of my workspace, but what happens if I want to select two layers? There are always several ways to do things. I have found that using the filter command is somewhat cumbersome. Another way to use these filters is from the ssget function.

Command: copy

Select objects:
(ssget '((8 . "mylayer") (0 . "circle") ))

This will only allow you to select circles on mylayer. If you wanted to select all the circles on layers mylayer and mylayer2 it would look as follows :

Command: copy

Select objects:
(ssget '((0 . circle) (-4 . "<or") (8 . "mylayer") (8 . "mylayer2") (-4 . "or>")))

The –4 dxf group code (-4 . "<or") begins the conditional operator. The less than or open alligator symbol < tells AutoCad to evaluate the conditional until it finds a closing alligator >. We use (-4 . "or>") to end the conditional.

Conditionals can be used together :

(ssget '(
   (-4 . "<or") 
   (8 . "notes") (0 . "circle")
   (-4 . "<and")
   (8 . "s-boundary")(0 . "line")
   (-4 . "and>")
   (-4 . "or>")
))

This would select only those entities that are on the layer notes or are circles and entities that are both lines on the layer s-boundary.

The Conditional Operators

Conditional operators -- AND, OR, XOR, and NOT -- must be paired and balanced correctly in the filter list. The number of operands you can enclose depends on the operation. Here is a list of the Conditionals or "selection set filters" you can use :

AND

"<AND" (One or more operands) "AND>"

(ssget '((-4 . "<and") (0 . "line") (8 . "text") (62 . 3) (-4 . "and>") ))

Selects entities that match all conditions.

OR

"<OR" (One or more operands) "OR>"

(ssget '((-4 . "<or") (0 . "line") (8 . "text") (62 . 3) (-4 . "or>") ))

Selects entities that match any of the conditions.

XOR

"<XOR" (Two operands) quot;XOR>"

(ssget '((-4 . "<xor") (8 . "text") (62 . 3) (-4 . "xor>") ))

Selects entities that match one or the other condition.

NOT

"<NOT" (One operand) "NOT>"

(ssget '((-4 . "<not") (0 . "line") (-4 . "not>") ))

Selects entities that do not match one condition.

The XOR conditional works as an exclusive OR operator. For instance, using OR we may select entities that are text and are either on the layer notes or have the color 3 or both.

(ssget '((0 . "text") (-4 . "<or") (8. "notes) (62 . 3) (-4 . "or>") ))

However, using XOR we may select only entities that are text and on layer notes or entities that are text and the color 3. We cannot select entities that are text on the layer notes and the color 3.

(ssget '((0 . "text") (-4 . "<xor") (8. "notes) (62 . 3) (-4 . "xor>") ))

If we wish to select text that is both on the layer notes and the color 3 using XOR we must group these properties with another conditional.

(ssget '(
   (0 . "text")
   (-4 . "<xor") 
   (-4 . "<and") (8 . "notes") (62 . 3) (-4 . "and>") 
   (-4 . "xor>") 
))

We can nest all sorts of conditionals into a selection set filter :

(ssget '(
   (-4 . "<xor") (8 . "mylayer") 
   (-4 . "<or") (0 . "text") 
   (-4 . "<xor") (8 . "notes") 
   (-4 . "<and") (62 . 2) (0 . "line") (-4 . "and>") 
   (-4 . "xor>") 
   (-4 . "or>") 
   (-4 . "xor>")
))

This is probably not practical, but it will work. See if you can figure out what it would select. There is not really all that much to these conditionals and they are very handy. I use them mostly at the command prompt to copy several layers at once, which tends to be easier then using the filter dialog box. The following lisp routine will let you copy all the objects from two layers.

(defun c:layer_copy ( / laa la p1 p2 ss )
(princ "\n Choose an object on desired layer: ")
   (setq laa (assoc 8 (entget (car (entsel)))))
   (princ "\n Choose another object a different desired layer: ")
   (setq la (assoc 8 (entget (car (entsel)))))
   (setq ss (ssget "x" (list (cons -4 "<or") laa la (cons -4 "or>") )))
   (command "copy" ss )
 );defun
(princ)