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:
- Creating a new selection set:
- Adding a selected entity to a new selection set:
(ssadd (car (entsel)) nil) |
- 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:
- Removing a selected entity from a selection set:
(ssdel (car (entsel)) new_set) |
- Removing the first entity from a selection set:
(ssdel (ssname new_set 0) new_set) |
- 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:
- Selecting all entities in the drawing:
(setq all_entities (ssget “X”)) |
- Selecting all circles in the drawing:
(setq all_circles (ssget “X” ‘((0 . “CIRCLE”)))) |
- Choosing entities within a specified window:
(setq window_entities (ssget “W” ‘(0 0) ‘(10 10))) |
ssgetfirst
Definition: Returns the current implied selection set.
Syntax:
Examples:
- Acquiring the implied selection set:
(setq implied_set (ssgetfirst)) |
- Determining the length of the implied selection set:
- Obtaining the first entity in the implied selection set:
sslength
Definition: Retrieves the number of entities in a selection set.
Syntax:
Examples:
- Counting entities in a selection set:
(setq count (sslength new_set)) |
- Prompting the user to select entities and then displaying the count:
(alert (strcat “You selected ” (itoa (sslength (ssget))) ” entities.”)) |
- 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:
- Checking if a selected entity is in a selection set:
(if (ssmemb (car (entsel)) new_set) (princ “\nEntity is in the selection set.”)) |
- 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.”)))) |
- 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:
- Getting the first entity from a selection set:
(setq first_entity (ssname new_set 0)) |
- Acquiring the last entity from a selection set:
(setq last_entity (ssname new_set (- (sslength new_set) 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:
Examples:
- Extracting extended data for all entities in a selection set:
(setq extended_data (ssnamex new_set)) |
- Displaying the layer of each entity in a selection set:
(foreach entity extended_data
(print (cdr (assoc 8 (cdr entity))))) |
- 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:
- Setting the implied selection set:
- Clearing the implied selection set:
- Verifying the current implied selection set:
(if (equal new_set (ssgetfirst)) (princ “\nNew set is the current implied selection set.”)) |
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!