Working with AutoCAD Selection Sets (Harry's Code Class Newsletter)24 Nov, 2008
When building a Visual LISP or VBA routine, understand the unique rules that control these objects.
A few weeks ago, a post appeared on Cadalyst’s Hot Tip Harry-Requests Discussion Forum that raised the question about how selection sets are ordered in AutoCAD. The query seemed simple enough: How does one get objects in a selection set into a specific sequence? (In other words, how do you sort the contents of a selection set in a program that has just accepted a selection set for input?) Unlike lists, selection sets are AutoCAD objects and are subject to basic rules that are unique to selection sets. The techniques used to sort a list of data in Visual LISP or a pick set collection in VBA/.NET are useless when dealing with only a selection set, so just how does one do it?
To answer the question, let us first take a look at selection sets and how they are constructed. AutoCAD selection sets, also known as pick sets, are a group of entity objects that were selected in some fashion. The selection mechanism can be based on user input, a window or polygon area, a filter where only specific entity objects are picked, or some combination of all the objects.
The majority of AutoCAD-related applications that use a selection set are not really concerned about the sequence of the objects in the set. They manipulate the entire set at once, such as changing the properties of all the objects in the set. However, in numerous instances it is beneficial to work with the objects of the set in a specific sequence, such as when creating an output report. In those cases, your choices are to move the contents of the set into a structure for more conventional manipulation or work the set to achieve the order desired.
The first choice is the easiest. Move the data of importance to a format easily manipulated in your language of choice (list in Visual LISP or an array in VBA/.NET and ObjectARX), then do your manipulations on the new structure. You don’t need to move all the entity object data to the new structure, just the primary sorting data along with an index back into the original selection set.
To illustrate this concept, suppose you need to sort entities in a selection set by location as well as the layer name. The new list or array structure would need to include the coordinates of interest along with the layer name and an index pointer back into the original selection set. In LISP, this is done with a nested list. For VBA/.NET, you might choose to build a collection of objects or just carry the data in arrays. The process would be to first build a selection set, then iterate through the set to obtain and store the data of interest from each entity object. Data is sorted in the next step, following whatever rules you need for your application. Finally, you can construct a new selection set using the sorted data index pointer values and the original selection set.
An interesting aspect of selection sets is the order in which they are constructed initially compared with how they were selected. At times you want to know the order in which the entities in a selection set were picked. When your program asks the operator to select entities, the order in the selection set is the order in which they were individually selected. However, if a window is used to select the objects, the sequence is based on when the entities were created in the drawing, with the newest appearing first. This all means that if you mix window picking with individual picking, you will have no idea about the order of selection in a single selection set.
The only way to maintain control over how entities are added to a selection is to avoid using the general-purpose selection set input option and instead build a loop that accepts one entity at a time and adds it to the selection set. Another way to say this in AutoCAD programmer talk is that you should not use SSGET, but instead build a loop based on ENTSEL.
I hope these concepts help clear up some of the confusion related to sorting a selection set. The key is to separate the data of interest along with a way to get back to the original data, sort that data, and rebuild a new selection set based on the sorted data.Until next time, keep on programmin’.
Check out these other cool online resources that will help you understand ordering in selection sets.
- Selection Order (by coordinates) (Hot Tip Harry Discussion Forum)
- What Determines the Order of Objects Added to a Selection Set via SSGET (AUGI Discussion Forum)
- Reorder List Elements (Hot Tip Harry Discussion Forum)
- Visual LISP Utilities: Sort by Bill Kramer
- Sorting Tools in Visual LISP (Harry's Code Class, February 2008)
- Example Sorting Program by Bill Kramer