🔍 Lesson 5.8 – Selection Sets with AutoLISP
Learn how to create, filter, and iterate over selection sets using AutoLISP tools like ssget
, sslength
, and ssname
.
📚 What You’ll Learn
By the end of this lesson, you’ll be able to:
- Create selection sets manually or programmatically
- Filter objects by type or layer during selection
- Use
sslength
andssname
to work with objects in a set - Build routines that act on multiple objects efficiently
🧠 Why It Matters
Selection sets are essential for batch processing in AutoCAD. Whether you’re modifying layers, measuring object properties, or cleaning a drawing, mastering selection sets gives you the ability to handle many objects at once.
🛠️ Tools You’ll Use
Function | Purpose |
---|---|
ssget | Select objects in the drawing (manual or filtered) |
sslength | Get number of items in a selection set |
ssname | Retrieve an individual item from a selection set |
entget | Retrieve entity data for processing |
🧭 Lesson Structure
1️⃣ Creating Selection Sets
Use ssget
to create a selection set.
Manual Selection:
(setq ss (ssget))
Prompts the user to select objects.
Window or Crossing Selection:
(setq ss (ssget "W" (list 0 0) (list 50 50)))
Filter by Object Type:
(setq ss (ssget '((0 . "CIRCLE"))))
Filter by Layer:
(setq ss (ssget '((8 . "Dimensions")))) ; 8 = Layer
2️⃣ Getting Selection Set Length
Use sslength
to count how many items were selected:
(setq count (sslength ss))
(prompt (strcat "\\nTotal selected: " (itoa count)))
3️⃣ Accessing Items with ssname
Use ssname
to access a specific item from the selection set:
(setq ent (ssname ss 0)) ; Get first object
(setq data (entget ent)) ; Get object data
You can use a repeat
loop to process all items:
(setq i 0)
(repeat (sslength ss)
(setq ent (ssname ss i))
(setq data (entget ent))
;; Do something with data here
(setq i (1+ i))
)
✅ Lesson Checklist
Task | Completed |
---|---|
Created a selection set using ssget | ☐ |
Filtered a set by object type or layer | ☐ |
Counted items in a set using sslength | ☐ |
Used ssname and entget to read objects in a loop | ☐ |
📌 Quick Tips
Tip | Why It Helps |
---|---|
Always test if ssget returns nil | Avoids crashing routines when selection is empty |
Use itoa to convert numbers to strings in prompts | Enables easy text output |
Wrap loops in (if ss ...) to protect against nil | Prevents runtime errors |
You can filter by more than one property | For example: ((0 . "LINE") (8 . "Walls")) |
🧩 Real-World Applications
Use Case | Selection Filter | Purpose |
---|---|---|
Delete all dimensions | ((0 . "DIMENSION")) | Cleanup routine |
Count all circles | ((0 . "CIRCLE")) | Shape analysis or quality check |
Modify color of polylines | ((0 . "LWPOLYLINE")) | Styling or standardization |
📁 Files and Resources
File / Resource | Description |
---|---|
selectionset_demo.lsp | A working example of filtered selection and looping |
ssget_filters.docx | A table of common filters and selection types |
ss_loop_template.lsp | A template for looping through selection sets |
📖 Review Table
Function | Description | Example |
---|---|---|
ssget | Create a selection set | (ssget '((0 . "LINE"))) |
sslength | Count entities in the set | (sslength ss) |
ssname | Get an entity from set by index | (ssname ss 0) |
entget | Get DXF data from entity | (entget (ssname ss i)) |