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

Figure 11.6: Finding the angle of an arc

Accessing Object Handles and Block Attributes

In the last section, you saw how to extract information from a complex object. You can use the same method to extract information from block attributes. The program you are about to examine uses block attributes as well as object handles and external files to help assign and store names to objects in your drawing. We will look at how entnext can be used to get attribute information from a block and how you can use attributes to permanently store information. You will also explore one possible way of using object handles which are permanent names AutoCAD can give to objects in a drawing.

Using Object Handles

We know that every object is given an object name. This name is the key to accessing the object's record in the drawing database. Unfortunately, this object name changes from editing session to editing session. This means that during one editing session, an object will have the object name <Object name: 600000d4> while in another session the same object will have the name <Object name: 60000012>. For the most part, this may not be of concern to you. But if you want to have a way of permanently identifying objects from one editing session to another, you may find this fact disturbing.

Fortunately, starting with release 10, you can add what is called an object handle to each and every object in your AutoCAD drawing. Handles are added to the object's in a drawing by AutoCAD whenever you turn on the handles function using the handles command. You do not have control over the naming of Objects, AutoCAD automatically assigns an alpha-numeric name to every object in the drawing.

The handent function in conjunction with other functions can be used to obtain an objects handle from the drawing

249

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

database. The handles are added to an object's property list as a group code 5 property sublist. To get an object's handle, you use the usual assoc-entget function combination to extract the sublist.

1.Return to the Chapt11 drawing and enter the following at the command prompt: handles

2.At the prompt:

Handles are disables.

ON/DESTROY:

enter ON. Next enter

(assoc 5(entget(car (entsel))))

3. At the select object prompt, pick the polyline box. You will get a list similar to the following:

(5 . "29")

The second element of the group 5 property is the object handle. Note that the handle is a numeric value in quotes, so it is really a string data type even though it is a number.

Using handles, you could write a simple routine to display an object's handle which you could record somewhere. Then, you could have another program to retrieve an object based on this handle. We've taken this idea a step further and have written a program that allows you to assign any name you like to an object and later select that object by entering the name you have assigned to it. Figure 11.7 shows this program and Figure 11.8 shows a diagram of how it works.

250

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

;Function to turn a list into a string----------------------------------------

 

(defun ltos (lst / gfile strname)

 

(setq gfile (open "acad.grp" "w"))

;open a file on disk

(prin1 lst gfile)

;print list to file

(close gfile)

;close file

(setq gfile (open "acad.grp" "r"))

;open file

(setq strname (read-line gfile))

;read list from file

(close gfile)

;close file

strname

;return converted list

)

 

;Function to obtain name list stored in attribute-----------------------------

 

(defun getatt (/ nament)

 

(setq nament (ssname(ssget "X" '((2 . "NAMESTOR")))0))

;get attribute block

(read (cdr (assoc 1(entget (entnext nament)))))

;get attribute value

)

 

;Function to clear stored names-----------------------------------------------

(defun attclr ()

(setq nament (ssname (ssget "X" '((2 . "NAMESTOR")))0)) ;get attrib. block

(setq namevl

(entget (entnext nament)))

;get attrib. ent. list

(setq namelt (assoc 1 namevl))

;get attrib. value

(entmod (subst (cons 1

"()") namelt namevl))

;add list to attrib

)

 

 

 

;Program to assign a name to an entity

 

;-----------------------------------------------------------------------------

 

 

 

(defun C:NAMER (/ group gname ename sname namevl namelt)

 

(setq ename

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

(setq gname

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

(setq group

(getatt))

 

;get names from attrib.

(setq gname

(append gname (list ename)))

;new name + ent. name

(setq group

(append group (list gname)))

;add names to list

(setq sname

(ltos group))

;convert list to strng

(setq namevl

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

(setq namelt (assoc 1 namevl))

;get attrib. value

(entmod (subst (cons 1

sname) namelt namevl))

;add list to attrib

(entupd (cdr (assoc -1

namevl)))

 

(princ)

 

 

 

)

 

 

 

;Function to select an entity by its name-------------------------------------

 

(defun GETNAME (/ group gname )

 

(setq gname

(strcase (getstring "\nEnter name of entity: ")))

(setq group

(getatt))

 

;get names from attrib.

(handent (cadr (assoc gname group)))

)

Figure 11.7: Program to give names to objects

251

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 11.8: Diagram of a program to name objects

This program makes use of a block attribute as a storage medium for the names you assign to objects. Lets take a closer look.

First, you need to define the attribute used for storage.

1. Exit the Chapt11 file and open an AutoLISP file called Namer.lsp. Copy the program shown in figure 11.7 into your file then save and exit the file.

252

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

2.Return to the Chapt11 file then load Namer.lsp.

3.Enter attdef to start the attribute definition command.

4.Enter the following responses to the attdef prompts:

Attribute modes -- Invisible:N Constant:N Verify:N Preset:N

Enter (ICVP) to change, RETURN when done:~CR

Attribute tag: name

Attribute name: name

Default attribute value: ()

Start point or Align/Center/Fit/Middle/Right/Style: 2,9

Height (2.0): ~CR

Rotation angle <0>: ~CR

5. The word name will appear in at coordinate 2,9. Now issue the block command and enter the following responses to the block prompts:

Block name (or ?): namestor

Insert base point: 2,9

select objects: [pick attribute defined in previous step.]

6. Issue the insert command and enter the following responses to the insert prompts: Block name (or ?): namestor

Insertion point: 2,9

X scale factor <1> / Corner / XYZ: ~CR

Y scale factor (default=X): ~CR

Rotation angle <0>: ~CR

Enter attribute values name <()>: ~CR

You have just defined the attribute within which the program namer will store your object names. Now you are ready to use the program.

253

Copyright © 2001 George Omura,,World rights reserved