AutoLISP.8: Selection sets in AutoLISP programs



Author Orhan Toker
Profession Architect M.Sc.
  Autodesk Authorized Consultant
  Database Specialist
e-mail  

All AutoLISP versions

Hello everyone,

By this lesson, we will move on to advanced level. In most of the program that you will create by using AutoLISP, you may also need to make selections other then selections made by user. Or, you may want to filter some specific objects among the selection set created by user or all of the database of the drawing. Thus, for all of these reasons, you should learn about the selection sets and how to manage them.

First of all, let’s take a look at what happens when you enter any editing command in AutoCAD. For example, this is what happens when you enter ERASE command:

Select Objects:

appears in command line. You select entities either by picking, by a selection window or fence and then continue to make selection or exit. At this point, AutoCAD show the objects that you have selected so far as dashed. These entities are members of current selection set. By selecting a new entity, you add a new member to selection set; and when you remove a member by using REMOVE command, you erase a member from selection set. Making all of these operations inside AutoLISP is called creating a selection set and managing selection sets.

Creating a new selection set by using (ssget)

(ssget) function is used to create a selection set. This is how you use this function:

(ssget [sel-method] [pt1 [pt2]] [pt-list] [filter-list])

Arguments are explained as follows:

Selection Method [sel-method] (It is an alphanumerical value that determines the selection method.)
• ”C” crossing (rectangular fence selection type)
• ”CP” crossing polygon (polygon fence selection)
• ”F” fence (fence selection)
• ”I” implied (initially selected set)
• ”L” last (last drawn entitiy)
• ”P” previous (previous selected set)
• ”W” window (window selection)
• ”WP” window polygon (window shaped polygon selection)
• ”X” selects all of the entities inside the drawing (including entities in turned off LAYERS and invisible entities)
• ”:E” selects entities inside the pick-box at the end of drawing cursor
• ”:N” Selects sub-entities inside a complex object like BLOCK or POLYLINE inside the drawing.
• ”:S” Forces user to select one single entity.

[pt1 [pt2]]
If the selection set will be created by using a window, then pt1 and pt2 values determine the two corners of this window.

[point-list] Point list
• Point list  for “CP” “WP” and “F

[filter-list]
• If you are going to make filtering inside a selection set, then you should pass the corresponding list that belongs to it.

Examples:

Command:(ssget)
<Selection Set: 2>

It prompts the user to make a selection and returns the resultant new selection set when the selection is over.

Command: (ssget '(2 2))
nil

Selects and entity if there is one that passes through 2,2 point.

Command: (ssget "_P")
<Selection set: 4>

It creates a new selection set by using the previous selection set.

Command: (ssget "_C" '(0 0) '(1 1))
<Selection set: b>

It inserts into the selection set all of the entities that is included and touched by the crossing window that is made of corners of points 0,0 and 1,1.

Command: (ssget "_W" '(0 0) '(5 5))
<Selection set: d>

It creates a selection set from all of the entities that is inside the 0,0 and 5,5 window.

Command: (ssget "_I" '((0 . "LINE") (62 . 5)))
<Selection set: 4>

This example selects blue lines among entities that have been initially selected.  ‘((0 . “LINE”) (62 . 5)) this part is used to filter entities whose color is blue.

Command: (setq pt_list '((1 1)(3 1)(5 2)(2 4)))
((1 1) (3 1) (5 2) (2 4))
Command: (ssget “_CP” pt_list)
<Selection set: 13>

In this example, entities that is included or touched by the fence polygon that is composed of 1,1 3,1 5,2 2,4 points. You can also see how the point list is passed to ssget function.

Manipulating a selection set

Fig.1
Fig.1

Once you create a selection set, you can add entities to it or remove entities from it, you can iterate among the objects inside it, or question those entities. In the example, we acquired the first and last entities inside the drawing by using entnext and entlast functions. This example will not work if there are no entities inside our drawing. If our selection set contains some entities, then this example creates a selection set from the first entity inside the drawing and adds the last entity to this selection set.

(ssdel fname ourset)

Above code erases the first entity of the drawing from the selection set. If there is one member inside the drawing, then first and last entity inside the drawing will be same and code will erase the selection set completely.

(sslength) function counts how many entities are there inside the selection set. If there is more then one element inside the drawing, then the example shown above will give the result of 2 when we call (sslength ourset).

Iterating though entities inside the selection set (ssname)  

You can call entities inside the selection set by using ssname. Example that is shown below explains how ssname function is used.

Fig.2
Fig.2

Removing entities from selection set

You can remove entities from selection set by using ssdel function. As an example, we can remove ent1 entity from the selection set above by using the following code:

(ssdel ent1 sset)

Filtering selection set 

You can make filtering according to the criterions that you want when creating a selection set. To do this, we must pass a filter list while creating a selection set. This list that you will create must be a list with dotted couple list similar to DXF code list. Below, you can find some examples:

Table.1
Table.1

That’s all for this lesson. In following lessons, I will try to give examples for selection sets.

Have a nice day.

Technorati Tags: , ,

Bu yazinin Turkcesini okumak icin basiniz.

Similar Posts

Rate this article:

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

4 Responses to “AutoLISP.8: Selection sets in AutoLISP programs

Leave a Reply