Additional Filter Options for (ssget) with autolisp in AutoCAD
Filter lists for (ssget) can include a variety of options, including relational tests for numerical values and Boolean expressions that look for information such as “this and that,” “this or that,” or “not this and that.”
Relational Tests
For numeric groups (integers, reals, points, and vectors) you can use relational test operators.
- To apply a relational test to the value of a numeric group within a filter-list, place a -4 group code along with the operator in a sub-list immediately preceding the group to which you want the test applied.
Example
This filter-list would return all the Circle entities in a drawing which have a radius greater than or equal to 1.0 units.
(ssget “x” ‘((0 . “CIRCLE”) (-4 . “>=”)(40 . 1.0)))
Boolean Operators
Unless otherwise specified, an “equal to” test is implied for each item in a filter list. AutoCAD allows four Boolean operators in filter lists: AND, OR, XOR, and NOT.
You can test groups of objects by creating nested Boolean expressions that are specified in -4 groups, like the relational operators. Each Boolean expression must be paired and balanced correctly. The number of operands that an expression can enclose depends on the operation.
Example
This filter-list would select all circles with a radius of 1.0 and also select all lines on layer OBJECT:
(ssget “x”
‘((-4 . “<OR”)
(-4 . “<AND”)
(0 . “CIRCLE”)
(40 . 1.0)
(-4 . “AND>”)
(-4 . “<AND”)
(0 . “LINE”)
(8 . “OBJECT”)
(-4 . “AND>”)
(-4 . “OR>”)
)
)
- For string groups, use wild-card tests, instead of relational tests.
PRACTICE
In this practice you will change the (textlayer) routine we created in the previous practice so that it selects both TEXT and MTEXT. Estimated time for completion: 5 minutes.
Solution
(defun C:TEXTLAYER ()
(setq CE-SAV (getvar “cmdecho”))
(setvar “cmdecho” 0)
(setq ss (ssget “x” ‘((-4 . “<OR”) (0 . “mtext”)(0 . “text”) (-4 . “OR>”))))
(setq POSITION 0)
(while (setq ENAME (ssname SS POSITION))
(setq OLDELIST (entget ENAME)
OLDLAYERNAMELIST (assoc 8 OLDELIST)
NEWLAYERNAMELIST (cons 8 “text”)
NEWELIST (subst NEWLAYERNAMELIST
OLDLAYERNAMELIST OLDELIST)
) ;_ end of setq
(entmod NEWELIST)
(setq POSITION (1+ POSITION))
) ;_ end of while
(setq SS nil)
(setvar “cmdecho” CE-SAV)
(princ)
) ;_ end of defun
- A complete solution to this exercise is on your class disk as TEXTLAYER-B.LSP
Combining Entity Selection and Filter Lists
When creating selection sets, you can combine the selection mode with a filter-list. For example, you can select all the Circle entities on layer 0 with a color of red (or 1) that fall within a Crossing window with a lower left corner of 0,0 and an upper right corner of 12,9.
You can even combine interactive object selection with a filter list. For example, the following code prompts the user to create a selection-set, then filters all the objects and returns only those objects that the user has selected and are LINE entities on Layer 0.
(setq SSET (ssget ‘((0 . “LINE”) (8 . “0”))))