Learn the basics of AutoCAD and More!

Lesson 5.08 – Selection Sets

🔍 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 and ssname 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

FunctionPurpose
ssgetSelect objects in the drawing (manual or filtered)
sslengthGet number of items in a selection set
ssnameRetrieve an individual item from a selection set
entgetRetrieve 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

TaskCompleted
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

TipWhy It Helps
Always test if ssget returns nilAvoids crashing routines when selection is empty
Use itoa to convert numbers to strings in promptsEnables easy text output
Wrap loops in (if ss ...) to protect against nilPrevents runtime errors
You can filter by more than one propertyFor example: ((0 . "LINE") (8 . "Walls"))

🧩 Real-World Applications

Use CaseSelection FilterPurpose
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 / ResourceDescription
selectionset_demo.lspA working example of filtered selection and looping
ssget_filters.docxA table of common filters and selection types
ss_loop_template.lspA template for looping through selection sets

📖 Review Table

FunctionDescriptionExample
ssgetCreate a selection set(ssget '((0 . "LINE")))
sslengthCount entities in the set(sslength ss)
ssnameGet an entity from set by index(ssname ss 0)
entgetGet DXF data from entity(entget (ssname ss i))