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

The ABC’s of AutoLISP by George Omura

In summary, to find a particular property of an object, you must first create a selection set containing that object using ssget, then extract the object name from the selection set using ssname, then extract the property list using the object name through the function entget. Once you have gotten the property list, you can apply assoc to it to get the specific property you want using group codes. Finally, cdr can be applied to the singled out propert to get the value of the property.

Understanding the structure of Property Lists

The first thing you might have notice about the property list in the example above is that most of the sublists were two element lists with a period separating the elements. This type of list is called a dotted pair. It is not a list in the true sense of the term because many of the functions used to manipulate lists will not work on dotted pairs. For this reason, dotted pairs are usually considered a data type in themselves.

You can use car and cdr on dotted pairs just as you would on lists. for example, you could enter the following:

(car '(A . B))

the symbol A is returned. You can also enter:

(cdr '(A . B))

and the symbol B is returned. Dotted pairs act slightly differently from regular lists. If cdr is applied to a normal list, as the following:

(cdr '(A B))

a list, (B), is returned. But in the case of a dotted pair, the second element of the dotted pair is returned by itself, not as part of a list.

You can create a dotted pair using the cons function. Normally, cons must have two arguments. The first is the element to be added to the beginning of a list and the second is the list to be added to. Enter the following:

(cons 'A '(B))

The list (A B) is returned. You could think of cons as the opposite of cdr. But if the second argument to cons is not a list, then a dotted pair is created. Enter the following:

(cons 'A 'B)

The dotted pair (A . B) is returned.

Cons and dotted pairs reflect the inner workings of AutoLISP and to explain these AutoLISP items thoroughly is beyond the scope of this book. At the end of chapter 10, we mention a few sources for more information on the general subject of LISP which can shed light on Cons and dotted pairs. For now, lets continue by looking at a function that allows us to directly modify the AutoCAD drawing database.

207

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Changing the properties of AutoCAD objects

Now that you have seen how object properties are found, it is a short step to actually modifying properties. To update an object record in the drawing database, you redefine the objects property list then use the function entmod to update the drawing database. Looking at the edtxt.lsp file again, we find the revtxt function:

(defun revtxt ()

(setq newtxt (cons 1 newtxt))

(entmod (subst newtxt txtstr oldobj))

)

The first thing revtxt does is use the cons function to create a dotted pair using the integer 1 for the first element and the string value held by newtxt for the second. Newtxt is a string value representing the new text that is to replace the old text oldobj. As we mentioned earlier, cons creates a dotted pair when both its arguments are atoms. The new dotted pair looks like this:

(1 . "For want of a nail, the kingdom was lost.")

Notice that the structure of this list is identical to the list oldobj which was retrieved from the text property list earlier.

The last line of the revtxt function does two things. first it uses the function subst to substitute the value of newtxt for the value of txtstr in the property list oldobj:

(subst newtxt txtstr oldobj)

Substr requires three arguments. the first is the replacement item, the second is the item to be replaced, and the third is the list in which the item to be replaced is found. Substr's syntax is as follows:

(subst [replacing item][item to be replaced][list containing item])

Subst returns a list with the substitution made.

Next, the function entmod updates the drawing database. It looks at the object name of the list that is passed to it as an argument. This list must be in the form of a property list. It then looks in the drawing database for the object name that corresponds to the one in the list. When it finds the corresponding object in the drawing database, it replaces that database record with the information in entmod's property list argument. The user sees the result as a new line of text.

Getting an Object Name and Coordinate Together

In the gettxt function, a function could have been used that doesn't require the you obtain a selection set. Entsel will find a single object name directly without having to use ssget to create a selection set. Since Entsel only allows the user to pick a single object, it is best suited where a program or function doesn't require multiple selections of objects.

208

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Here is gettxt using entsel:

(defun gettxt ()

(setq oldobj (entget (car (entsel "\nSelect object: "))))

(setq txtstr (assoc 1 oldobj))

(cdr txtstr)

)

Entsel acts like the get functions by allowing you to provide a prompt string. Instead of returning a number, string, or point, entsel returns a list of two elements. The first element is an object name, and the second is a list of coordinates specifying the point picked to select the object:

(<Object name: 60000012> (4.0 3.0 0.0))

Above is a sample of what is returned by entsel. Since our Gettxt function is only concerned with the object name, car is used on the value returned from entsel:

(car (entsel "\nSelect object: "))

This expression replaces the ssget and ssname function used previously:

(ssname (ssget pt1) 0)

Also, since entsel pauses to allow the user to select an object, the getpoint expression can be eliminated along with the setvar function.

Conclusion

You have seen how lists can be used to manipulate data both as forms and as simple lists of information. AutoLISP makes no distinction between a list that is an expression to be evaluated and a list that is used to store data. Once you understand the methods for manipulating list, you can begin to develop some powerful programs.

In the next chapter, you will look in more detail at how you can access information directly from AutoCAD. You will also look at how to access the property of complex objects such as blocks an polylines.

209

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

210

Copyright © 2001 George Omura,,World rights reserved