Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

1. Enter namer at the command prompt. At the prompt:

Pick object:

pick the polyline box.

2. At the next prompt

Enter name of object:

Enter square. The computer will pause for a moment then the command prompt will return. Also, the value of the attribute you inserted earlier will change to a list containing the name SQUARE and the object handle associated with the name. The Namer program uses the attribute as a storage device to store the name you give the object with its handle.

3.To see that the name square remain associated with the box, exit the chapt11 file using the end command then open the file again.

4.Load the Namer.lsp file again.

5.Now issue the copy command. At the Select object prompt, enter

(getname)

You will get the prompt

Enter name of object:

Enter square. The box will highlight indicating that it has been selected.

6.At the Base point prompt, pick a point at coordinate 2,2.

7.At the Second point prompt, pick a point at coordinate 3,3. The box is copied at the displacement 1,1.

The attribute used to store the name could have been made invisible so it doesn't intrude on the drawing. We intentionally left it visible so you could actively see what is going on.

Namer works by first extracting the object handle of the object selected then creating an association list of the handle and the name entered by the user. This association list is permanently stored as the value of an attribute. The attribute value is altered using the entmod function you saw used in the last chapter. Let's take a detailed look at how namer and getname work.

Using Object Handles

Namer starts by obtaining the object handle of the object the user picks:

(defun C:NAMER (/ group gname ename

254

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

sname nament namevl namelt)

(setq ename

(cdr (assoc 5 (entget (car (entsel "\nPick object: ")))))

)

Here, entsel is used to get the object name of a single object. The car function extracts the name from the value returned from entsel then entget get the actual object name. At the next level, the assoc function is used to extract the 5 group code sublist from the object. The actual object handle is extracted from the group code using the cdr function. This value is assigned to the variable ename.

In the next expression, a list is created containing the name given to the object by the user:

(setq gname

(list (strcase (getstring "\nEnter name of object: ")))

)

The user is prompted to enter a name. this name is converted to all upper case letters using the strcase function. Then it is converted into a list using the list function. finally, the list is assigned to the variable gname.

The next expression calls a user defined function called getatt:

(setq group (getatt))

This function extracts the attribute value from a the block named namestor. You may recall that the default attribute value of namestor was "()". Getatt extracts this value and the above expression assigns the value to the variable group. We'll look at how getatt works a little later.

Next, the object handle is appended to the list containing the name the user entered as the name for the object. This appended list is then appended to the list named group which was obtained from the attribute.

(setq gname (append gname (list ename)))

(setq group (append group (list gname)))

The variable group is the association list to which user defined object name are stored. It is the same list you see in the block attribute you inserted earlier.

The next line converts the list group into a string data type using a user defined function called ltos:

(setq sname (ltos group))

Ltos simply write the list represented by the symbol group to an external file then reads it back. The net affect is the conversion of a list into a string. This is done so the value of group can be used to replace the current value of the attribute in the namestor block. This is a situation where data type consideration is important.

255

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Attribute values cannot be anything other than strings so if our program were to try to substitute a list in place of a string, an error would occur.

Extracting Attribute Data

The next several lines obtain the property list of the namestor block attribute and its attribute value in preparation for entmod:

(setq namevl (entget

(entnext (ssname (ssget "X" '((2 . "NAMESTOR")))0)))

)

Here, the ssget "X" filter is used to select a specific object, namely the block named "NAMESTOR". In this situation, since the name of the block is a fixed value, we include the name as a permanent of the expression:

(ssget "X" '((2 . "NAMESTORE")))

This helps us keep track of what block we are using and also reduces the number of variables we need to use.

Once the block is found by ssget, ssname gets the blocks' object name and entnext extracts the attributes' object name. Entget extracts the attributes property list which is assigned to the namevl variable. The line that follows uses the assoc function to extract the actual attribute value.

(setq namelt (assoc 1 namevl))

Figure 11.9 shows how this works.

Figure 11.9: Extracting an attribute value

256

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Just as we used entnext to extract the vertices of a polyline, you can use entnext to obtain the attribute information from a block. If there is more than one attribute in a block, you step through the attributes the same way you step through the vertices of a polyline. The Getatt function works in a similar way to these expressions you have just examined.

Finally, entmod is used to update the attribute to store the association list of the object name and object handle:

(entmod (subst (cons 1 sname) namelt namevl))

(entupd (cdr (assoc -1 namevl)))

(princ)

)

The subst function is used to substitute the newly appended name with the old attribute value in the attributes property list. Then entmod updates the drawing database with the updated property list. The entupd function is used to update the display of the attribute in the block. Entupd is only needed where attributes and curve-fitted polylines are being edited and you don't want to regenerate the entire drawing to update the display. You could think of it as a regen for specific objects.

The getname function is actually quite simple compared with namer.

(defun GETNAME (/ group gname getname handl nament newent) (setq gname (strcase (getstring "\nEnter name of object: ")))

(setq group (getatt))

(handent (cadr (assoc gname group)))

)

Getname prompts the user for the name of the object to be selected. It then obtains the association list of name from the storing attribute using the user defined Getatt function. Finally, getname extracts the object handle from the association list using the name entered by the use as the key-value. The handent function returns the object name of the object whose handle it receives as an argument.

Namer and getname are fairly crude program as they have very little in the way of error checking. For example, if while using the getname function, you enter a name that does not exit, you get an AutoLISP error message. Also, if you attempt to save more than dozen names, you will get the out of string space error message. This is due to the 100 character limit AutoLISP places on string data types. There is also no facility to check for duplicate user supplied names. We wanted to keep the program simple so you won't get too confused by extra code. You may want to try adding some error checking features yourself, or if you feel confident, you can try to find a way to overcome the 100 character limit.

257

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Conclusion

Programming can be the most frustration experience you have ever encountered as well as an enormous time waster. But it is also one of the most rewarding experiences using a computer can offer. And once you master AutoLISP, you will actually begin to save time in your daily use of AutoCAD. But to get to that point, you must practice and become as familiar as possible with AutoLISP. The more familiar you are with it, the easier it will be to use and the quicker you will be able to write programs.

We hope this tutorial has been of value to your programming efforts and it will continue to be helpful to you as a reference when you are stuck with a problem. Though we didn't cover every AutoLISP function in detail, In particular, we did not cover binary operations and a few other math functions. We did cover the major functions and you were able to see how those functions are used within programs solving real world problems. You were introduced to the program in a natural progression from entering your first expression through the keyboard to designing and debugging programs and finally to accessing the AutoCAD drawing database.

258

Copyright © 2001 George Omura,,World rights reserved