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

Finding the Properties of AutoCAD Objects

One of the most powerful features of AutoLISP is its ability to access the properties of drawing objects. You can find properties such as the endpoint coordinates of lines, their layer, color and linetypes, and the string value of text. You can also directly modify these properties.

Object properties are accessed using two AutoLISP data types, object names and selection sets. Object names are similar to symbols in that they are a symbolic representation of an object. An object name is actually a device used to point to a record in a drawing database. This database record holds all the information regarding the particular object. Once you knows an object name, you can access the information stored in the object's record.

Using Selection Sets and Object Names

A selection set is a collection of object names. Selection sets can contain just one object name or several. Each name in the selection set has a unique number assigned to it from zero to 1 minus the number of names in the set.

To find out how you access this object information, lets look at the C:EDTXT PROGRAM used in chapter 7.

As you may recall, this program allows you to edit a line of text without having to enter the entire line. The fourth line in C:EDTXT does the work of actually extracting the text string from the database:

(setq oldtxt (gettxt))

Here, the program makes a call to a user defined function called gettxt:

(defun gettxt () (setvar "osmode" 64)

(setq pt1 (getpoint "\nPick text to edit: ")) (Setvar "osmode" 0)

(setq oldobj (entget (ssname (ssget pt1) 0) )) (setq txtstr (assoc 1 oldobj))

(cdr txtstr) )

Gettxt first finds a single point pt1 that locates the text to be edited:

(setvar "osmode" 64)

(setq pt1 (getpoint "\nPick text to edit: "))

(Setvar "osmode" 0)

Next, the real work of finding the object is done. The next line uses several functions to extract the object name from the drawing database:

201

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

(setq oldobj (entget (ssname (ssget pt1) 0) ))

This innocent looking set of expressions does a lot of work. It first creates a selection set of one object:

(ssget pt1)

;function to find text string from text entity

-------------------------------

(defun gettxt ()

 

(setvar "osmode" 64)

;set osnap to insert

(setq pt1 (getpoint "\nPick text to edit: "))

;get point on text

(Setvar "osmode" 0)

;set osnap back to zero

(setq oldobj (entget (ssname (ssget pt1) 0) ))

;get entity zero from prop.

(setq txtstr (assoc 1 oldobj))

;get list containing string

(cdr txtstr)

;extract string from prop.

)

 

;function to update text string of text entity-------------------------------

 

(defun revtxt ()

 

(setq newtxt (cons 1 newtxt))

;create replacement propty.

(entmod (subst newtxt txtstr oldobj))

;update database

)

 

;program to edit single line of text-----------------------------------------

 

(defun C:CHTXT (/ count oldstr newstr osleng otleng oldt old1

old2 newtxt pt1 oldobj txtstr oldtxt)

 

(setq count 0)

;setup counter to zero

(setq oldtxt (gettxt))

;get old string

from text

(setq otleng (strlen oldtxt))

;find length of

old string

(setq oldstr (getstring T "\nEnter old string "))

;get string to change

(Setq newstr (getstring T "\nEnter new string "))

;get replacement string

(setq osleng (strlen oldstr))

;find length of

substring-

;while string to replace is not found, do...

to be replaced

 

(while (and (/= oldstr oldt)(<= count otleng))

 

 

(setq count (1+ count))

;add 1 to counter

(setq oldt (substr oldtxt count osleng))

;get substring to compare

);end WHILE

;if counting stops before end of old string is reached...

(if (<= count otleng) (progn

(setq old1 (substr oldtxt 1 (1- count))) ;get 1st half of old string (setq old2 (substr oldtxt (+ count osleng) otleng));get 2nd half

(setq newtxt (strcat old1 newstr old2))

;combine to make new string

(revtxt)

;update drawing

)

 

(princ "\nNo matching string found.")

;else print message

);end IF

 

(PRINC)

 

);END C:EDTXT

 

 

 

Figure 9.9: The C:EDTXT program

202

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

You may recall from chapter 4 that ssget accepts a point location, to find objects for a selection set. If you entered the expression above at the command prompt, and pt1 has been previously defined as a point nearest an object, you would get the name of a selection set. Try the following exercise:

1.Open an AutoCAD file and place the following text in the drawing:

For want of a battle, the kingdom was lost.

2.Enter the following expression:

(setq pt1 (getpoint "\nPick the text: "))

3.Use the insert osnap override option from either the side or pull down menu and pick the text.

4.Enter the following expression:

(ssget pt1)

You will get a message that looks similar to the following:

<Selection set: 1>

This is a selection set. The number following the colon in the above example would be different depending on whether previous selections sets have been created.

5. Once a selection set has been created, ssname is used to find the object name. Enter the following:

(ssname (ssget pt1) 0)

Ssname will return the object name of a single object in the selection set. If you enter the expression above, you get an object name that looks similar to the following:

<Object name: 600000c8>

Just as with selection sets, the number that follows the colon will different depending on the editing session.

The syntax for ssname is:

(ssname [selection set][integer])

The selection set can be gotten from a symbol representing the selection set or directly from the ssget function as in our example above. The integer argument tells ssname which object to select within the selection set. In our example, there is only one object, so we use the integer 0 which represents the first object in a selection set. If there were several objects in the selection set, say 4, we could use an integer from 0 to 3.

At the next level, the function entget does the actual database extraction. Enter the following:

(setq oldobj (entget (ssname (ssget pt1) 0)))

203

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

You will get a list revealing the properties of the text.

((-1 . <Entity name: 20a0598>) (0 . "TEXT") (5 . "33") (100 .

"AcDbEntity") (67 . 0) (8 . "0") (100 . "AcDbText") (10 -0.147023 2.84992 0.0) (40 . 0.2) (1 . "For want of a battle, the kingdom was lost") (50 . 0.0) (41 .

1.0) (51 . 0.0) (7 . "STANDARD") (71 . 0) (72 . 0) (11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . "AcDbText") (73 . 0))

This list obtained using entget is called a property list. Entgets' syntax is:

(entget [object name])

Entget returns a list containing the object's properties. Property lists consists of other lists whose first element is an integer code. The code represents a particular property like an objects layer, color, linetype or object type. Property lists are a class of list called association lists.

You may recall that earlier in this chapter, you constructed a list of system variables. that list looked like the following:

(("osmode" 0)("orthomode" 1)("cmdecho" 1))

This is also an association list. each element of the list is a list of two elements, the first of which can be considered a keyword.

Each list within an object's property list starts with an integer code. That integer code is the key-value to that list otherwise known as the group code. The group code is associated with a particular property. For example, the group code 1 in associated with the string value of a text object. The 10 group code is associated with the insertion point of the text. Table 9.2 shows the group codes for text and their meaning.

204

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Code Meaning

-1 Entity name

0 Entity type ("TEXT", "LINE", "ARC", etc.

7Text style

8Layer

10Insertion point

11Center alignment point (for centered text)

21 Right alignmenet point (for right-justified text)

31 Second alignment point (for fit or aligned text)

40Text height

41X scale factor

50Rotation angle

51Oblique angle

71Text mirror code (2, mirrored in x axis; 4, mirrored in y axis)

72Text alignment code (0, left justified; 1, center at baseline;2, right justified; 3, text uses align option; 4, centered at middle; 5, text uses fit option)

210 3-D extrusion amount in x, y, or z direction

If you are familiar with the AutoCAD DXF file format and coding system, then these group codes should be familiar. Appendix C gives a detailed listing of these codes if you want to know more.

Now that our expression has retrieved the property list, we need a way to pull the information out of the list. A function for this purpose is found in the next line. Enter the following:

(setq txtstr (assoc 1 oldobj))

In the previous expression, the property list is assigned to the variable oldobj. In the above expression, we see a new function called assoc:

(assoc 1 oldobj)

205

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

This expression returns the list:

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

Remember that oldobj is the variable for the property list of the text you selected earlier.

The Syntax for Assoc is:

(assoc [key-value] [association list])

Assoc looks through an association list and finds the list whose first value is the key-value. It then returns the list containing the key-value.

In the case of our property list example, assoc looks through the property list oldobj and finds the list whose first element is the group code 1 then it returns that list. The list returned by assoc is assigned to the symbol txtstr. Finally, cdr is applied to txtsrt to obtain the string value of the selected text. Enter the following:

(cdr txtstr)

The string value associated with the group code 1 is retrieved. Figure 9.10 diagrams the entire operation.

Figure 9.10: Diagram of property list extraction

206

Copyright © 2001 George Omura,,World rights reserved