Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
George Omura. Lisp programing tutorial for AutoCAD customization / 620.0.The ABC's of AutoLISP - Omura, George.pdf
Скачиваний:
146
Добавлен:
02.05.2014
Размер:
1.55 Mб
Скачать

The ABC’s of AutoLISP by George Omura

Filtering Objects for Specific Properties

There are a number of other functions available that allow you to manipulate selection sets Table lists them and gives a brief description of what they do:

Function

Description

(ssadd [ent. name][s. set])

Creates a selection set. If used with no arguments, a selection set is created

 

with no objects. If only an object name given as an argument, then a

 

selection set is created that contains that object. If an object name and a

 

selection set name is given, then the object is added to the selection set.

(ssdel [ent. name][s. set])

Deletes an object from a selection set. ssdel then returns the name of the

 

selection set. IF the object is not a member of the selection set, then ssdel

 

returns nil.

(sslength [s. set])

Returns the number of objects in a selection set.

(ssmemb [ent. name][s. set])

Checks to see if an object is a member of a selection set. If it is, then ssmemb

 

returns the name of the selection set. if not, then ssmemb returns nil.

(ssname [s. set][nth object])

Returns the object name of a single object in a selection set. The second

 

argument to ssname corresponds to the object's number within the selection

 

set. The object numbers begin with zero and go to one minus the total

 

number of objects in the selection set.

You have already seen two of these functions, sslength and ssname, used in previous examples. Lets see how we can use ssadd to filter out object selections.

Filtering a Selection Set

Figure 10-8 shows a function that is intended to filter out objects in a selection set based on layers. This function is useful where a group of objects are so close together that they are difficult to select, or in situations where several objects of different layers lie on top of each other and you want to select just the object on a specific layer. It returns a selection or object name depending on whether the filtered selection set contains only one item.

226

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

;function to filter entities by layer -- Lfilter.lsp--------------------------

(defun LFILTER (/ lay sset count ent newent)

(setq lay (cons 8 (strcase (getstring "\nEnter layer name: "))))

(setq sset (ssget))

;get entities

(setq count 0)

;set counter to zero

(while (< count (sslength sset))

;while still select. set

(setq

lay2 (assoc 8 (entget(setq ent(ssname sset count))))) ;get layer

 

(if (equal lay lay2)

;if layer matches entity

 

(if (not newent)

;if new not select. set

 

(setq newent (ssadd ent))

;make new select. set

 

(setq newent (ssadd ent newent))

;else add to select. set

 

);end if

 

 

);end if

 

(setq

count (1+ count))

 

);end while

 

(if (= 1

(sslength newent))(ssname newent 0) newent) ;return select. set or

);end defun

 

entity name

Figure 10.8: The layer filtering program

Let's see first hand how this function works.

1.Open a file call Lfilter.lsp and copy the Lfilter program in figure 10.8. Save and exit the file.

2.Return to the AutoCAD Chapt10 drawing and erase any objects in the file.

3.Create the layers listed in table 10.1. Be sure to assign the line types indicated for each layer.

4.Draw the lines shown in figure 10.9. and assign each line to the layer shown directly to the right of each line. Use the coordinate and spacing information indicated in the drawing to place the lines.

5.Load the Lfilter program, then issue the erase command.

6.At the Select object prompt, enter:

(lfilter)

You will get the prompt:

Enter layer name:

227

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

8.Enter hidden. You will get the next prompt:

Select objects:

9.Enter C to use a crossing window and pick the points 2,1.25 for the lower left corner of the window and coordinate 8,8.25 for the upper right. The lines that pass through the crossing window will ghost. Press return after picking points. The lines will un-ghost then the prompt will return the number of objects found. The objects in the selection you just made with the crossing window that are on the layer hidden will ghost.

10.Press return. You have just erased only the items in your crossing window that were on the layer hidden.

11.Enter the Oops command in preparation for the next section.

Layer Name

Linetype

hidden

hidden

center

center

dashed

dashed

Table 10.1: Linetypes for drawing in figure 10.8

Figure 10.9: Test drawing for Lfilter.lsp

228

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Now that you know what Lfilter does, let's look at how it works. First, it creates a dotted pair representing the layer sublist in an object property list:

(defun LFILTER (/ lay sset count ent newent)

(setq lay (cons 8

(strcase (getstring "\nEnter layer name: "))))

When you are prompted for a layer, the name you enter is first converted to all upper case using the Strcase function. This is done because the value associated with the layer group code in a property list is always in upper case.

In order to make a comparison of data, we must make sure that the values we use in the comparison are in the same format. Since AutoLISP is case sensitive when it comes to string values, we must make sure that the value the user enters matches the case of the layer name in the property list. The strcase function will convert a string to either all upper case or lower case depending on whether a third argument is present and is not nil.

Next, the cons function creates a dotted pair using 8 as the first element. 8 is the group code for layer names. The dotted pair looks like this:

(8 . "Hidden")

Finally, the newly created dotted pair is assigned to the variable lay which will later be used as a filtering value.

The next line obtains the selection set to be filtered:

(setq sset (ssget))

Here, ssget is used without any argument thus allowing the user to select objects using any of the usual AutoCAD selection options. This selection set is then assigned to the variable sset.

Next we come to the while expression.

(setq count 0)

(while (< count (sslength sset)) (setq lay2 (assoc 8

(entget(setq ent(ssname sset count))))) (if (equal lay lay2)

(if (not newent)

(setq newent (ssadd ent))

(setq newent (ssadd ent newent))

229

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

);end if

);end if

(setq count (1+ count))

);end while

This while expression compares the 8 group code sublist of each object in the selection against the variable lay. If it finds a match, the object is added to a new selection set newent. Let's look at this while expression in detail.

First, the counter is set to zero and the conditional test is set up:

(setq count 0)

(while (< count (sslength sset))

Then the 8 group code sublist from the first object in the selection set is extracted and assigned to the variable lay2:

(setq lay2 (assoc 8

(entget(setq ent(ssname sset count)))))

Next, the variables lay lay2 are compared:

(if (equal lay lay2)

If there is a match signifying that the object is on layer "HIDDEN", then the program checks to see if the selection set newent exists:

(if (not newent)

(setq newent (ssadd ent))

(setq newent (ssadd ent newent))

If newent does not exist, then ssadd creates a new selection set containing the object whose layer group code sublist matches (8 . "HIDDEN") then assigns that selection set to the variable newent. If newent does exist, ssadd adds the object to the selection set newent and redefines newent. This last conditional if is required since ssadd must be given different arguments depending on whether it is to create a new selection set or just add an object to an existing selection set.

Finally, the counter in increased by one and the while conditional loop repeats itself:

);end if

);end if

(setq count (1+ count))

);end while

230

Copyright © 2001 George Omura,,World rights reserved