Lessons

Selection Sets Management

Today, we embark on an exciting chapter in our AutoLISP journey: Selection Sets Management. In the CAD environment, efficiently selecting and managing entities is crucial for many operations. AutoLISP provides a rich set of functions tailored for this, making the process not just efficient but also highly customizable.

Learning Outcomes

By the conclusion of this lesson, you will be adept at:

  • Creating and modifying selection sets.
  • Extracting specific entities from selection sets.
  • Understanding and manipulating the structure of selection sets.
  • Managing the order and properties of selection sets.

Dive into Selection Sets

ssadd

Definition: Adds an entity to a selection set or creates a new selection set.

Syntax:

(ssadd [entity [selection_set]])

Examples:

  1. Creating a new selection set:
(setq new_set (ssadd))
  1. Adding a selected entity to a new selection set:
(ssadd (car (entsel)) nil)
  1. Including another entity to an existing selection set:
(ssadd (car (entsel)) new_set)

ssdel

Definition: Removes an entity from a selection set.

Syntax:

(ssdel entity selection_set)

Examples:

  1. Removing a selected entity from a selection set:
(ssdel (car (entsel)) new_set)
  1. Removing the first entity from a selection set:
(ssdel (ssname new_set 0) new_set)
  1. Prompting the user to select an entity to remove:
(ssdel (car (entsel “\nSelect entity to remove: “)) new_set)

ssget

Definition: Selects entities based on specified criteria.

Syntax:

(ssget [mode] [point1 [point2]] [filter_list])

Examples:

  1. Selecting all entities in the drawing:
(setq all_entities (ssget “X”))
  1. Selecting all circles in the drawing:
(setq all_circles (ssget “X” ‘((0 . “CIRCLE”))))
  1. Choosing entities within a specified window:
(setq window_entities (ssget “W” ‘(0 0) ‘(10 10)))

ssgetfirst

Definition: Returns the current implied selection set.

Syntax:

(ssgetfirst)

Examples:

  1. Acquiring the implied selection set:
(setq implied_set (ssgetfirst))
  1. Determining the length of the implied selection set:
(sslength (ssgetfirst))
  1. Obtaining the first entity in the implied selection set:
(ssname (ssgetfirst) 0)

sslength

Definition: Retrieves the number of entities in a selection set.

Syntax:

(sslength selection_set)

Examples:

  1. Counting entities in a selection set:
(setq count (sslength new_set))
  1. Prompting the user to select entities and then displaying the count:
(alert (strcat “You selected ” (itoa (sslength (ssget))) ” entities.”))
  1. Checking if a selection set is empty:
(if (= (sslength new_set) 0) (princ “\nSelection set is empty.”))

ssmemb

Definition: Determines if an entity is part of a selection set.

Syntax:

(ssmemb entity selection_set)

Examples:

  1. Checking if a selected entity is in a selection set:
(if (ssmemb (car (entsel)) new_set) (princ “\nEntity is in the selection set.”))
  1. Testing multiple entities against a selection set:
(foreach ent (list entity1 entity2 entity3)
(if (ssmemb ent new_set) (princ (strcat “\n” (itoa ent) ” is in the selection set.”))))
  1. Removing an entity from a selection set if it’s a member:
(setq ent_to_check (car (entsel)))
(if (ssmemb ent_to_check new_set) (ssdel ent_to_check new_set))

ssname

Definition: Retrieves the entity name from a specified index in a selection set.

Syntax:

(ssname selection_set index)

Examples:

  1. Getting the first entity from a selection set:
(setq first_entity (ssname new_set 0))
  1. Acquiring the last entity from a selection set:
(setq last_entity (ssname new_set (- (sslength new_set) 1)))
  1. Iterating through all entities in a selection set:
(foreach idx (vl-range 0 (sslength new_set))
(print (ssname new_set idx)))

ssnamex

Definition: Similar to ssname but returns extended data about the entity.

Syntax:

(ssnamex selection_set)

Examples:

  1. Extracting extended data for all entities in a selection set:
(setq extended_data (ssnamex new_set))
  1. Displaying the layer of each entity in a selection set:
(foreach entity extended_data
(print (cdr (assoc 8 (cdr entity)))))
  1. Retrieving the color of the first entity in a selection set:
(cdr (assoc 62 (cdr (nth 0 (ssnamex new_set)))))

sssetfirst

Definition: Sets the implied selection set.

Syntax:

(sssetfirst nil selection_set)

Examples:

  1. Setting the implied selection set:
(sssetfirst nil new_set)
  1. Clearing the implied selection set:
(sssetfirst nil nil)
  1. Verifying the current implied selection set:
(if (equal new_set (ssgetfirst)) (princ “\nNew set is the current implied selection set.”))

Conclusion

Selection sets are a robust means to manage entities in AutoLISP. From creating tailored selections to modifying and querying them, these tools are indispensable for efficient CAD operations. Your understanding of these functions will greatly enhance your AutoLISP coding capabilities, making your routines more dynamic and powerful. Keep practicing, keep coding, and stay curious!