46
1-- 1 Lesson 1 getpoiont setq defun command at the command prompt, type : line computer response: from point pick a point on the screen computer response: to point pick a point on the screen computer response: to point press Enter key Above are the steps in AutoCAD to use the Aline@ command. getpoint an AutoLisp function to request an input of a point. getpoint also echos a preassigned message. In order to understand the use of getpoint and setq, at the command prompt, type : (getpoint "Pick 1st point") computer response: Pick 1st point pick a point on the screen setq an AutoLisp function to assign variables in AutoLisp. In order to assign a point to a variable name for later retrieval at the command prompt, type: (setq A (getpoint "Pick 1st point")) computer response: Pick 1st point pick a point on the screen A point was picked, and the use of setq allows later retrieval of the point. In order to assure you did assign the point to the variable name A at the command prompt, type: !A the computer should print the co-ordinates of the point you picked last. The factorial symbol, ! , is used to get the value of any defined AutoLisp variables.

LISP-1

Embed Size (px)

Citation preview

Page 1: LISP-1

1-- 1

Lesson 1

getpoiont setq defun command

at the command prompt, type : line computer response: from point

pick a point on the screen

computer response: to point pick a point on the screen

computer response: to point press Enter key

Above are the steps in AutoCAD to use the Aline@ command.

getpoint an AutoLisp function to request an input of a point. getpoint also

echos a preassigned message.

In order to understand the use of getpoint and setq,

at the command prompt, type : (getpoint "Pick 1st point") computer response: Pick 1st point

pick a point on the screen

setq an AutoLisp function to assign variables in AutoLisp.

In order to assign a point to a variable name for later retrieval

at the command prompt, type: (setq A (getpoint "Pick 1st point"))

computer response: Pick 1st point pick a point on the screen

A point was picked, and the use of setq allows later retrieval of the point.

In order to assure you did assign the point to the variable name A

at the command prompt, type: !A

the computer should print the co-ordinates of the point you picked last. The

factorial symbol, ! , is used to get the value of any defined AutoLisp variables.

Page 2: LISP-1

1-- 2

At the command prompt, type: (setq B (getpoint "Pick 2nd point"))

computer response: Pick 2nd point pick a point on the screen

The AutoLisp function command is used to call up a standard AutoCAD

command. It should precede a standard AutoCAD command enclosed by a pair of

double quotation marks. In order to see how this function works,

at the command prompt, type: (command "line" A B "")

press Enter key

A line connecting two points, A and B, is drawn. The start point and end point of

the line will be point A and point B respectively.

What you have done was to draw a line by means of AutoLisp function. AutoLisp

could be executed line by line, just like what you have done, but a formal

AutoLisp routine to draw a line should be:

(defun c:line1 () (setq A (getpoint "Pick 1st point")) (setq B (getpoint "Pick 2nd point")) (command "line" A B "") )

Use Note Pad, Word Pad, or any other available window version text-editor and

type: (defun c:line1 () (setq A (getpoint "Pick 1st point")) (setq B (getpoint "Pick 2nd point")) (command "line" A B "") )

When done, save the file as “A:LINE1.LSP”

The file must be saved as text file.

Page 3: LISP-1

1-- 3

Explanation of the lines typed:

(defun is a standard way to start an AutoLisp programme.

c: means to define a customized command; nothing to do with drive C:

line1 the customized command will be known as line1

() a pair of bracket means there will be variables in the programme and

the variables will stay in the memory after execution of the AutoLisp

routine.

) this is the closing bracket for the whole programme.

Minimize the text editor and return to AutoCAD,

Now is the time to load and run the AutoLisp routine, LINE1.LSP, you just typed.

at the command prompt type: (load "A:line1")

computer will response: c:line1

now LINE1 becomes your customized command to draw a line. Whenever you

want to use it to draw a line you could type LINE1

Improvement of above AutoLisp routine:-

Maximize the text editor and modify the file, line1.lsp to read:

(defun c:line1 () (setq A (getpoint "Pick 1st point")) (setq B (getpoint "Pick 2nd point" A)) (command "line" A B "") )

This modification will produce an elastic band originating from point A.

Load the AutoLisp file again. Execute the file and appreciate the difference.

LINE1.LSP, though not a very practical file, does illustrate the principles of input

and output in an AutoLisp programme.

Page 4: LISP-1

1-- 4

The best way to learn AutoLisp is by doing exercises. By applying the AutoLisp

functions learned, you should be able to do the following tutorial.

Tutorial for Lesson 1:-

1. Write an AutoLisp routine which will ask the user to

pick 5 points and then draw a 5 - vertice star.

(STAR5.LSP)

2. Write an AutoLisp routine which will ask the

user to pick 20 points and then the first picked

point will joint all subsequently picked points.

(BUSH.LSP)

Page 5: LISP-1

2 -- 1

Lesson 2

getreal distance prompt terpri getint getstring

at the command prompt, type : (getreal "Type a number")

computer response: Type a number type a number and press Enter key

getreal an AutoLisp function to request an input of a real number from the

user. It, like getpoint, also echos a predetermined message.

Most of the time we want to save the number under a variable name so that the

value could be retrieved for use later. Thus, we have to combine setq and getreal

together like the following:.

at the command prompt type: (setq A (getreal "Type a number "))

type a number and press Enter key

in order to confirm that the number you typed is indeed stored under the variable

name A,

at the command prompt type: !A

computer response: [the number you typed in]

The following example, LINECIR.LSP, asks for 2 points and a real number as

input and then draws a line with 2 circles at its ends. It illustrates the use of the

getreal. function.

The terpri function issues a carriage return.

The prompt function prints a predetermined message to the screen.

Page 6: LISP-1

2 -- 2

Use Note Pad, Word Pad or any other Windows text editor and type the following

file and then save as: A:LINECIR.LSP

(defun c:linecir () (setq A (getpoint "Pick 1st point")) (terpri) (setq B (getpoint "Pick 2nd point" A)) (terpri) (setq R (getreal "Input radius")) (terpri) (command "line" A B "") (command "circle" A R) (command "circle" B R) (prompt "Thank you!") )

Above example use information input directly by the operator. A real number

could be input directly by getreal or indirectly by the distance function. The

distance function in AutoLisp will return a real number based on 2 input points. If

we want the previous 2 circles to touch each other, we could use the distance

function to obtain a real number and then calculate the required radii.

AutoLisp built-in mathematical feature is extremely useful in programming.

AutoLisp perform calculations in the following formats:

Algebra format AutoLisp format

4 + 5 (+ 4 5)

9 - 2 (- 9 2)

2 x 4 (* 2 4)

12 / 3 (/ 12 3)

Besides Algebraic functions, AutoLisp also support most of the trigonometrical

functions too.

The following example draws 2 circles, radius of the smaller one will be half of

the big one.

Page 7: LISP-1

2 -- 3

Use Note Pad, Word Pad or any other Windows text editor to type the following

file and then save as A:BSCIR.LSP

(defun c:bscir () (setq A (getpoint "Pick 1st point"))(terpri) (setq B (getpoint "Pick 2nd point" A))(terpri) (setq D (distance A B)) (setq R1 (/ D 3)) (setq R2 (* R1 2)) (command "circle" A R1) (command "circle" B R2) (prompt "Thank you!") )

The above AutoLisp programme could be further improved by adding the layering

command so that the big circle and small circle could be drawn on different layers.

The proper input sequence to use the layer command should be found out before

the AutoLisp programme is to be prepared..

(defun c:bscir2 () (setq A (getpoint "Pick 1st point"))(terpri) (setq B (getpoint "Pick 2nd point" A))(terpri) (setq D (distance A B)) (setq R1 (/ D 3)) (setq R2 (* R1 2)) (command "layer" "m" "smallcir" "c" "green" "" "") (command "circle" A R1) (command "layer" "m" "largecir" "c" "yellow" "" "") (command "circle" B R2) (prompt "Thank you!") )

Sometimes we need to input text to the programme. This could be done by the

getstring function. When getstring function is used, even if the user type a number

by the keyboard the number would be taken as a string in AutoLisp. Any

mathematical performance on that input number will not work.

Page 8: LISP-1

2 -- 4

Just like any other programming language real number and integer number should

be distinguished. AutoLisp calculation could mix up integers and real numbers but

for

AutoCAD to work properly some of the input must be integer rather than real

number. For example, in the Aarray@ operation, the number of rows and number of

columns must be integers. The following examples demonstrates the use of

getstring and getint functions.

Use Note Pad, Word Pad or any other Windows text editor and type

FAN.LSP , COLARC.LSP AND CIRPAT :-

Save all the files in Drive A:

getstring an AutoLisp function to request an input of characters from the user.

It, like getreal, also could echo a predetermined message.

FAN.LSP (defun c:fan () (setq C (getstring "type a standard color: red, yellow, green, cyan, or blue ")) (terpri) (setq D (getpoint "pick first point of line.."))(terpri) (setq E (getpoint "pick second point of line" D))(terpri) (command "color" C) (command "line" D E "") (command "array" "l" "" "p" D 50 160 "y") )

Page 9: LISP-1

2 -- 5

COLARC.LSP (defun c:colarc () (setq C (getstring "type a standard color: red, yellow, green, cyan, or blue")) (terpri) (setq D (getpoint "pick first point of arc.."))(terpri) (setq E (getpoint "pick second point of arc.."))(terpri) (setq F (getpoint "pick third point of arc.."))(terpri) (command "color" C) (command "arc" D E F) (command "array" "l" "" "p" D 20 30 "y") )

getint an AutoLisp function to request an input of an integer

from the user. It, like getreal, also echos a

predetermined message.

CIRPAT LSP (defun c:cirpat () (setq C (getstring "type a standard AutoCAD color: red, yellow, green, cyan or blue..")) (terpri) (setq D (getpoint "pick first point"))(terpri) (setq E (getpoint "pick second point"))(terpri) (setq F (getint "type a number larger than 10 :")) (command "color" C) (command "circle" "2P" D E) (command "array" "l" "" "p" D F 360 "y") )

Page 10: LISP-1

2 -- 6

Tutorial:

Write an AutoLisp programme which will ask user to pick 2 points and then will

draw 2 donuts touching each other; and also 2 circles each at the centers of donuts

The inside radius of the donut will be half of that of the outside one. Radius of the

circles to be 1/16 of the distance between the two picked points.Draw the donuts

on the layer called tire and draw the circles on the layer called axle.

File name to use: WHEEL2.LSP

Page 11: LISP-1

3 -- 1

Lesson 3

getangle polar angle if nil

Prepare a grid layout as shown as right, using 10000 drawing

units between grid lines. We are going to draw columns on this

plan at intersection of grid lines..

Use Note Pad, Word Pad or any other Windows text editor to

type the following file and then save as A:COLUMN.LSP

(defun c:column () (setq CEN (getpoint "Centroid of column: ")) (terpri) (setq HOR (getreal "X Dimension of column: ")) (terpri) (setq VRT (getreal "Y Dimension of column: ")) (terpri) (setq ANG (getangle "Rotation angle of the X side: " CEN)) (terpri) (setq M (polar CEN (- ANG (/ pi 2)) (/ VRT 2))) (setq A (polar M (- ANG pi) (/ HOR 2))) (setq AM (angle A M)) (setq B (polar A AM HOR)) (setq C (polar B (+ AM (/ pi 2)) VRT)) (setq D (polar C (+ AM pi) HOR)) (command "pline" A B C D "c") ) Exit text editor

Load the AutoLisp file, A:COLUMN.LSP

Computer should response by displaying C:COLUMN

COLUMN becomes your customized command.

at the command prompt, type : column computer response: Centroid of column

pick a point at the intersect of grids computer response: X dimension of column type 1500

press Enter key computer response: Y dimension

Page 12: LISP-1

3 -- 2

type 2000 press Enter key

computer response: Y dimension

type 2000 press Enter key

computer response: Rotation angle of the X side use osnap Anear@ pick a point on the grid to

define orientation.

A column is drawn, nice and square to the grid.

The ease to draw column by AutoLisp is obvious.

This routine introduces the use of getangle, angle

and polar.

getangle is the AutoLisp function that lets you

find an angle by pointing to two

points or bv entering the angle from

the keyboard. Therefore, instead of

picking two points, like what we

have done, we could type the value

of the angle through the keyboard.

polar is the AutoLisp function that derive a

point at a given distance and angle from another known point.In the

above example we based on the known point CEN with a known angle,

pi /2, and a known distance, VER /2 to get the point M. By the same

token, we based on M to get A, B, C and D.

angle is the AutoLisp function that measures the angle of two known points.

The value of the angle returned by AutoLisp functions is always in radian. The angle

to be used in the polar function should be in radian also. Therefore, if the angle

obtained by AutoLisp functions is to be used in AutoCAD commands then the values

of the angle should be converted from radian to degrees.

Page 13: LISP-1

3 -- 3

The next example, is an AutoLisp routine to draw a line with an arrow at both

ends. It makes use of the polyline and polyline width features to prepare arrows and

the line between arrows. Therefore, the steps to use Apline@ command and its width

feature should be found out before preparing the AutoLisp routine.

It is very difficult to predetermine a good size for the arrows. Thus, in this

programme we try to make the arrow size relative to the final plotting scale of the

drawing. The if fuction is used here so that user needs to input the scale of drawing

only once as long as the user remains in the same drawing. This programming

technique is very useful whenever AutoLisp is used to draw entities which size

should be relative to the scale of the drawing such as the insertion of direction

symbols, or other graphical symbols on a drawing.

if The if function is followed by either two or three statements. The first statement

after if is the condition to evaluate. The second statement is the step for the

routine to execute if condition is true. The third statement, if exist, is the step

for the routine to execute if condition is false.

nil in AutoLisp nil represent an empty set or non-existing condition.

Use Note Pad, Word Pad or any other Windows text editor to type the following file

and then save as A:DARR.LSP

(defun c:DARR () (if (= SC nil) (setq SC (getreal "Scale of dwg. 1: ")) ) (setq SPT (getpoint "Starting point")) (setq EPT (getpoint "End point" SPT)) (setq APT1 (polar SPT (angle SPT EPT) (* SC 2))) (setq APT2 (polar EPT (angle EPT SPT) (* SC 2))) (command "pline" SPT "w" 0.0 SC APT1 "w" 0.0 0.0 APT2 "w" SC 0.0 EPT "") )

Page 14: LISP-1

3 -- 4

Tutorial:

1. When we prepare structural steel drawings, in order to indicate a pinned joint

connection between two main members we need to draw a line which is a little

bit shorter than the two points we pick. Write an AutoLisp programme to do

this. When activated the programme should prompt you to pick two points, then

a line should be drawn with a gap between the ends

of the line and the points picked. Size of the gap

should be the same value as the scale of the

drawing. If the scale is 1:100 , gap size is 100 or if

the scale is 1:200 then the gap should be 200.

file name: SHORTER.LSP

2. Very similar to above, write a programme which

will prompt for 2 points and then it will draw a line

which will exceed the length between the two

picked

points by a distance of SC at both ends. SC will

be 100 for 1:100 or 50 for 1:50 scale drawing

file name: LONGER.LSP

3. Write an AutoLisp programme, in which the user needs to pick two points and

a line with two arrows at one end (as shown by Fig. 3.6) will be drawn

file: FARARR.LSP.

Page 15: LISP-1

4 -- 1

Lesson 4

getcorner graphscr list car cadr

At the command prompt, type: (setq A (getpoint "1st corner of rect"))

computer response: 1st corner of rect pick a point on the screen at bottom left

computer response: [displays the co-ordinate of the point picked]

At the command prompt, type: (setq C (getcorner "2nd corner of

rectangle " A))

computer response: 2nd corner of rect (and display an elastic rectangle) pick a point on the screen at upper right

Now two points were input. These two points represent the diagonal corners of a

rectangle.

getcorner the function to acquire a second point basing on a defined first point. and

it provides the user with an elastic rectangle.

Points are variables with x and y coordinates. With the two diagonal points

established, we try to get the x, y values and form the remaining points of the

rectangle.

Any variable with more than one value is called a list. Point A is a list because it

contains x and y coordinates. Same does point C. We are going to combine x

coordinate of A and y coordinate of C to form point B.

graphscr is the AutoLisp function to switch the computer to graphic mode, if it is

in text mode.

car is the AutoLisp function to get the first element of a list.

cadr is the AutoLisp function to get the 2nd element of a list.

list is the AutoLisp function to group individual elements together.

At the command prompt, type: (setq B (list (car A) (cadr C)))

computer response: [displays a set of coordinate for point B]

At the command prompt, type: (setq D (list (car C)(cadr A)))

computer response: [displays a set of coordinate for point D]

Page 16: LISP-1

4 -- 2

Now all points for the rectangle are defined.

at the command prompt, type : pline computer response: from point

type: !A

computer response: ARC/CLOSE/........../Width/<Endpoint of line>:

type: w

computer response: Starting width <0.050> type: 0.0 computer response: Ending width <0.00> type: 0.0 computer response: ARC/CLOSE/........../Width/<Endpoint of line>:

type: !B

computer response: ARC/CLOSE/........../Width/<Endpoint of line>: type: !C computer response: ARC/CLOSE/........../Width/<Endpoint of line>: type: !D

press Enter key

A rectangle is drawn. Now try to type the following file, rectan.lsp. Then, load the

programme and see how it works.

(defun c:rectan () (graphscr) (setq A (getpoint "Pick corner of rectangle "))(terpri) (setq C (getcorner "2nd corner of rectangle " A))(terpri) (setq B (list (car A)(cadr C))) (setq D (list (car C)(cadr A))) (command "pline" A "w" 0.0 0.0 B C D "c") )

The next example will make full use of

what you have learned to draw the symbol

of a broken line. The programme uses the

drawing scale to determine size of the

broken symbol at the middle of the two

picked points. It uses car and cadr to

locate mid-pont of the symbol. From the

Page 17: LISP-1

4 -- 3

mid-point it uses polar to find 4 other points then it uses pline to draw the complete

broken line symbol.

Use a Windows text editor to type the following file, bline1.lsp:-

(defun c:bline1 () (GRAPHSCR) (setq SC (getreal "scale of dwg. 1:")) (setq BPT (getpoint "start point of break line ")) (terpri) (setq EPT (getpoint BPT "point of break line ")) (setq ANG (angle BPT EPT)) (setq MPTX (/ (+ (car BPT) (car EPT)) 2)) (setq MPTY (/ (+ (cadr BPT) (cadr EPT)) 2)) (setq MPT (list MPTX MPTY)) (setq PT1 (polar MPT (- ANG pi) (* SC 0.5))) (setq PT3 (polar MPT ANG (* SC 0.5))) (setq PT2 (polar MPT (+ ANG (/ pi 2)) (* SC 2))) (setq PT4 (polar MPT (- ANG (/ pi 2)) (* SC 2))) (command "pline" BPT "w" 0.0 0.0 PT1 PT2 PT4 PT3 EPT "") )

Page 18: LISP-1

4 -- 4

Tutorial

1. Write an AutoLisp programme, in which when the user picks 2 diagonal points

a rectangle with round corners will be drawn. Radius for the round corners will

be 1/10th of the shorter side of the rectangle. See Fig. 4.3

(Hints: use AutoLisp to draw a rectangle; find the shorter side and calculate for

the fillet radius; use the polar function to determine points to be used for the

fillet command.)

file:RECRND.LSP

2. Write an AutoLisp programme to draw a rectangle and its shadow at its lower

right side as shown on Fig. 4.4 Width of the shadow to be 1/25th of the length

of the shorter side of the rectangle. file: RECSHD.LSP

Page 19: LISP-1

5-- 1

Lesson 5

repeat while getdist

All computer languages have loops to do repetitive jobs. In AutoLisp both the repeat

and while function create loops to repeat a series of procedures.

repeat The function to repeat a series of procedure according to a specified

number of times.

while The function to invoke a loop while a variable or expression is not nil or

invalid.

getdist The function to request input as a real number, either through the

keyboard or by pointing.

The following example demonstrate the usage of the repeat function. Once loaded

and executed, it will ask user to pick two points and draw 5 concentric circles with

decreasing radius. Each subsequent radius will be 50% of the previous one. And the

circles are in different colors beginning with red (color 1).

Use a Windows text editor and type the following:-

(defun c:conc () (setq A (getpoint "Pick center of circle "))(terpri) (setq R (getdist "Pick circum of outside circle" A))(terpri) (setq C 1) (repeat 5 (command "color" c) (command "circle" A R) (setq R (* R 0.5)) (setq C (+ C 1)) ) (command "color" "bylayer") )

“bylayer” means the entity color will be same as the color of the layer on which it is

drawn. The default feature for color is bylayer on the original acad.dwg. In order not

to modify this feature, at the end of the programme, we try to return the drawing to

have its entity color to be Abylayer@.

Page 20: LISP-1

5-- 2

The while loop carries on and on as long as a specified condition is satisfied. The

number of loops is not necessarily to be known. The following example bases on the

previous one. It demonstrates the use of the while loop. The while loop will draw

circles as long as the radius is larger than 0.1.

Use a Windows text editor and type the following:-

(defun c:conc2 () (setq A (getpoint "Pick center of circle "))(terpri) (setq R (getdist "Pick circum of outside circle" A))(terpri) (setq C 1) (while (> R 0.1) (command "color" c) (command "circle" A R) (setq R (* R 0.5)) (setq C (+ C 1)) ) (command "color" "bylayer") )

Above examples show that repeat and while have

their places. They are used at different situations.

The last example limits the smallest circle to be 0.1. Whether radius 0.1 is too small

to see or not depends very much on the final plotting scale of the drawing. A better

way would be to draw the smallest circle according to the final plotting scale. In 1:100

drawing 100 units will be represented by 1mm, and in 1:200 drawing, 200 will be

represented by 1mm. In order to have the smallest circle visible its radius should be

relative to the final plotting scale of the drawing. The next example uses the while

loop to control the smallest circle to be drawn.

Save the previous AutoLisp file to another name, conc3.lsp, and revise it as follow:-

(defun c:conc3 () (setq A (getpoint "Pick center of circle "))(terpri) (setq R (getdist "Pick circum of outside circle" A))(terpri) (if (= SC nil) (setq SC (getreal "Scale of the dwg is 1: ")) ) (if (= COL nil) (setq COL 1) ) (while (> R SC) (if (> COL 6)

Page 21: LISP-1

5-- 3

(setq COL 1) ) (command "color" COL) (command "circle" A R) (setq R (* R 0.5)) (setq COL (+ COL 1)) ) (command "color" "bylayer") )

The outermost circle drawn by the above revised programme, conc3.lsp, may not be

red unless it is the first time to use this programme in that drawing editor.

Tutorial

1. Write an AutoLisp programme to draw steps. Input allowed: two points, one

point at bottom of steps another point at top of last step; number of steps.

file:

STEPS.LSP

2. Write an AutoLisp programme to draw a happy face. Allowed input would be

only two points. The first point dictates the center of the face. The second point

will define the circumference

of the circle. Orientation of

the happy face should be

according to the angle

defined by the two points

picked.

file:HAPFACE.LSP

Page 22: LISP-1

6 --1

Lesson 6

getvar setvar rtos strcat strlen substr

progn strcase /=

In this lesson we are going to use AutoLisp to put text on the drawing. Many a

time we need to know the coordinate of a point on the drawing. Nowadays due to the

advance technology in land surveying most of the site plans are actually done with

coordinates.AutoCAD is a very accurate software in this respect. After a designer

drew a roadway on a topographical AutoCAD drawing, coordinates for all the

important points such as BC( beginning of curve), EC(end of curve), PCC(point on

compound curve) are actually known and readily available for surveyors to use for site

layout.

The following example will write the coordinates of a selected point directly on

the drawing. Whenever AutoCAD command Atext@ is used, it is good to define the

text style to be used. If textstyle is not defined in the same programme, current text

style of the drawing will be used and it may not be the right one for your programme.

getvar AutoLisp function to get the existing value of a system variable.

setvar AutoLisp function to set an AutoCAD system variable.

rtos AutoLisp function to convert a number or variable to a string.

strcat AutoLisp function to joint two strings to become one string.

Use a Windows text editor to type the following file:-

(defun c:pid () (graphscr) (if (= SC nil) (setq SC (getreal "Scale of the dwg is 1: ")) ) (setq B (getvar "blipmode")) (setvar "blipmode" 1) (setq C (getpoint "Select point for I.D. ")) (terpri) (setq ST1 (getpoint "Select point for beginning of text. ")) (terpri) (setq ANG (getangle "Rotation of text " ST1)) (terpri) (setq TS (* SC 2.5)) (command "style" "COOR" "SIMPLEX" TS 1.0 0.0 "n" "n" "n") (setq VDT (* 1.50 TS)) (setq ST2 (polar ST1 (- ANG (/ pi 2)) VDT))

Page 23: LISP-1

6 --2

(setq ANGD (/ (* ANG 180) pi)) (setq X (car C)) (setq Y (cadr C)) (setq EAST (rtos X )) (setq NORTH (rtos Y )) (setq ECOOR (strcat "E " EAST)) (setq NCOOR (strcat "N " NORTH)) (command "text" ST1 ANGD ECOOR) (command "text" ST2 ANGD NCOOR) (command "redraw") (setvar "blipmode" B) )

The following example is an AutoLisp routine to do text along a circular path.

strlen This function returns an integer that is the number of characters in a string.

substr The function returns a substring of a string.

e.g. (substr "abcde" 2) returns "bcde"

(substr "abcde" 2 1) returns "b" (substr "abcde" 3 2) returns "cd"

/= This not equal to.

Page 24: LISP-1

6 --3

Use a Windows text editor and type the file as below:-

(defun c:CURTEX () (setq PT1 (getpoint "Pick the starting point of text"))(terpri) (setq PT2 (getpoint "Pick the center of arc for the text" PT1)) (terpri) (setq TEX (getstring T "Please start to type the text....")) (terpri) (if (= SC nil) (setq SC (getreal "Scale of dwg. 1: "))) (setq TXZ (* SC 2.54)) (setq STY (getvar "textstyle")) (if (/= STY "curtex") (command "style" "curtex" "complex" TXZ 1.0 0.0 "n" "n" "n") ) (setq TXL (strlen TEX)) (setq N 1) (repeat TXL (setq TTC (angle PT1 PT2)) (setq RAD (distance PT1 PT2)) (setq TAN (+ (/ pi 2) TTC)) (setq TANG (* TAN 57.2958)) (setq PT3 (polar PT1 TAN TXZ)) (setq PT4 (polar PT2 (angle PT2 PT3) RAD)) (setq CTX (substr TEX N 1)) (command "text" PT1 TANG CTX) (setq PT1 PT4) (setq N (+ N 1)) ) )

Page 25: LISP-1

6 --4

Page 26: LISP-1

6 --5

FIG. 6.2 shows the text done by CURTEX.LSP As we can see the spacing for

some of the alphabets is not very well proportioned. There are certain letters, we

have to pay extra attention to. The following AutoLisp programme bases on the

previous one but spacings for the letters, m, i, and l are adjusted to make the text

look better.

progn This function evaluates each expression sequentially, usually used

with the if function.

strcase This function evaluates all characters in a variable as uppercase.

(defun c:CURTEX2 () (setq PT1 (getpoint "Pick the starting point of text"))(terpri) (setq PT2 (getpoint "Pick the center of arc for the text" PT1))(terpri) (setq TEX (getstring T "Please start to type the text...."))(terpri) (if (= SC nil) (setq SC (getreal "Scale of dwg. 1: "))) (setq TXZ (* SC 2.54)) (setq STY (getvar "textstyle")) (if (/= STY "curtex") (command "style" "curtex" "complex" TXZ 1.0 0.0 "n" "n" "n") ) (setq TXL (strlen TEX)) (setq N 1) (repeat TXL (setq TTC (angle PT1 PT2)) (setq RAD (distance PT1 PT2)) (setq TAN (+ (/ pi 2) TTC)) (setq TANG (* TAN 57.2958)) (setq PT3 (polar PT1 TAN TXZ )) (setq PT4 (polar PT2 (angle PT2 PT3) RAD)) (setq CTX (substr TEX N 1)) (command "text" PT1 TANG CTX) (if (= CTX "m") (progn (setq PT3 (polar PT1 TAN (* TXZ 1.5))) (setq PT4 (polar PT2 (angle PT2 PT3) RAD)) ) ) (if (= CTX "l") (progn (setq PT3 (polar PT1 TAN (* TXZ 0.5))) (setq PT4 (polar PT2 (angle PT2 PT3) RAD)) ) )

Page 27: LISP-1

6 --6

(setq CHK (strcase CTX)) (if (= CHK "I") (progn (setq PT3 (polar PT1 TAN (* TXZ 0.5))) (setq PT4 (polar PT2 (angle PT2 PT3) RAD)) ) ) (setq PT1 PT4) (setq N (+ N 1)) ) )

Tutorial:

1. Write an AutoLisp programme which will prompt to pick up points and a

line will connect each subsequently picked points with the length of the line

written at its side. The process will continue as long as points are being

picked. file: LINETX.LSP

2. Write an AutoLisp programme which will ask user to pick 2 points to define

a rectangular area and input the verical load (e.g. 15KN/M); then it will start

filling the defined area by downward arrows and put the vertical load value

above the arrows.(As shown in Fig. 6.5) file: VLOADAR.LSP

Page 28: LISP-1

6 --7

3. Similar to above, write an AutoLisp programme

which will ask user to pick 2 points to define a

rectangular area and input the horizontal load (e.g.

15KN/M); then it will start filling the defined area by

horizontal arrows and put the horizontal load value

close to the arrows.(As shown in Fig. 6.6)

file: HLOADAR.LSP

Page 29: LISP-1

6 --8

4.. Write an AutoLisp programme which will draw the

shape of horizontal triangular load (earth load). The

input will be bottom and top of triangular load, end

of the bottom arrow and value of force at bottom. (As

shown in Fig. 6.7) file: TLOADAR.LSP

Page 30: LISP-1

-- 1

Lesson 7

Math & AutoLisp

or cons foreach

AutoLisp include all trigonometrical and mathematical functions. Using the

built-in functions many geometrical shapes could be drawn easily by means of

AutoLisp. The following example is an AutoLisp routine to draw a star of any

specified vertices.The ability to write this kind of programme depends very much on

the programmer=s mathematical and geometrical skill.

In order to write this programme, we need to understand how the angles within

a polygon are related to each other.

Page 31: LISP-1

-- 2

In FIG. 7.1

< INCLAN = 360Ε ) NC Where NC = number of vertices (or sides)

< ANIN = <INCLAN ) 2 Substended by half arc length

< ANIN = <ANOUT + <ANOUT Exterior angle of a triangle

� <ANOUT = <INCLAN ) 4

In ♠ PNT1 PNT2 CEN

<PNT2 PNT1 CEN = ANOUT Bottom of issoc.♠

<PNT2 CEN PNT1 = ANIN <s Subtended by equal arcs in same υ

� use sine formula for ♠ PNT1 PNT2 CEN

IRAD / SIN (180Ε - ANIN - ANOUT) = SRAD / SIN ANOUT

� SRAD = SIN ANOUT * IRAD / SIN (180Ε - ANIN - ANOUT)

Where IRAD is the distance from CEN to PNT1

SRAD is the distance from CEN TO PNT2

After PNT2 is established and connected to PNT1,

command Amirror@ is used to form the second line.

These two lines are then central-arrayed to complete the

programme.

Page 32: LISP-1

-- 3

Use a text editor and type the following file:

(defun c:STAR () (graphscr) (setq NC (getint "Input number of vertices for the star ")) (setq CEN (getpoint "Centroid of star ")) (setq PNT1 (getpoint "One vertex of the star " cen)) (setq INCLAN (/ (* 2 pi) NC)) ;included angle at center (setq ANOUT (/ (/ (* 2 pi) NC) 4)) (setq ANIN (* 2 ANOUT)) (setq ANG (angle CEN PNT1)) (setq IRAD (distance CEN PNT1)) (setq SRAD (* IRAD (/ (sin ANOUT) (sin (- pi (+ ANOUT ANIN)))))) (setq PNT2 (polar CEN (+ ANG ANIN) SRAD)) (command "line" PNT1 PNT2 "" "mirror" PNT1 "" CEN PNT2 "") (setq INCLAND (* (/ INCLAN pi) 180)) (command "array" "l" PNT1 "" "c" CEN INCLAND NC "y") )

FIG. 7.2 Shows the stars drawn by this programme.

AutoLisp could be used to draw the graph of a equation. Programming for a graph

would include: the evaluatin of X & Y coordinates, the combining of X & Y to form

points, the construction of a list for all these calculated points, and finally use the

Apline@ command and step through all the points in the list.

The following example uses AutoLisp to draw a graph :

Y = X2 ) 5 for the range -10 < X < 10

To begin with, a list with all the calculated points is needed. The list has to be

assigned a name. Here let us call it PNTLIST. Before the list has any calculated

value, we always set it to nil in order to ensure it starts as an empty list.

Page 33: LISP-1

-- 4

(setq PNTLIST nil)

Before a calculation loop, the lower limit of X value should be specified.

(setq X -10)

or a condition function in AutoLisp

The while loop is then used to calculate the points, and to specify the upper limit of X

(while (or (< X 10) (= X 10)) ..... ....)

The equation Y = X2 ) 5 in AutoLisp is:

(setq Y (/ (* X X) 5))

points are formed by using the list function.

(setq PNT (list X Y))

cons AutoLisp function to construct the list by adding new data to the list

(setq PNTLIST (cons PNT PNTLIST))

After each point is added to the list, increment of X is applied. (setq X (+ X 0.2))

After the list is completed, the following 4 lines work together to draw the polyline

and fit the curve.

(command "pline") (foreach P PNTLIST (command P)) (command "") (command "pedit" "l" "s" "w" 0.0 "x")

foreach AutoLisp function to cause execution of a command on each element of

a list.

The whole programme should read as the following:

Page 34: LISP-1

-- 5

(defun c:SQC () (setq PNTLIST nil) (setq X -10) (while (or (< X 10) (= X 10)) (setq Y (/ (* X X) 5)) (setq PNT (list X Y)) (setq PNTLIST (cons PNT PNTLIST)) (setq X (+ X 0.2)) ) (command "pline") (foreach P PNTLIST (command P)) (command "") (command "pedit" "l" "f" "w" 0.0 "x") )

Shell out and type the above programme.

The above method is a handy way to draw any graph for an explicit equation on a

drawing. Coordinates of the points dictate where the curve will be drawn. But

many a time we would like to put the curve at any convenient location on the

drawing, regardless of the drawing global coordinate system. This could be done

simply by using the UCS features of AutoCAD.

The above programme could be revised to read as follow:

(defun c:SQC () (setq B (getpoint "Pick origin of graph.")) ;added line

(setq PNTLIST nil) (setq X -10) (while (or (< X 10) (= X 10)) (setq Y (/ (* X X) 5)) (setq PNT (list X Y)) (setq PNTLIST (cons PNT PNTLIST))

Page 35: LISP-1

-- 6

(setq X (+ X 0.2)) ) (command "ucs" "o" B) ;added line (command "pline") (foreach P PNTLIST (command P)) (command "") (command "pedit" "l" "f" "w" 0.0 "x")

(command "ucs" "p") ;added line )

Now the programme will ask the user to pick a point which will be used as the

origin (0,0) for the curve anywhere on the drawing.

Tutorial:

1 Write an AutoLisp programme to draw the following curve on the drawing:-

a) y = x3 / 5 for -5 < x < 5

b) Y=Cos X for 0 < X < 2 pi

c) Y=Sin X for 0 < X < 2 pi

d) Y=Sin2X for 0 <X < 2pi

e) Y=Sin X/2 for 0 < X < 2 pi

Page 36: LISP-1

--1

Lesson 8

fix entlast

Fig. 8-1 is the building block for Fig. 8-2. The drawing of insulation is always

necessary on architectural drawings. This AutoLisp makes use of an existing block

inserted on the drawing with a scale calculated according to input from the user

and then copied to fill a specified area. In this example, the object selection

method, Alast@, is not used. Instead of Alast@, the AutoLisp function entlast is

used. In object selection mode, Alast@ means the last entity created and visible on

the screen. If the entity created last is outside the display area, then Alast@ will not

select the entity you want. However, enlast will return the name of the last

nondeleted main entity whether visible or not..

fix AutoLisp function to convert a real number to an integer.

entlast AutoLisp function to return the name of the last nondeleted main

entity.

Page 37: LISP-1

--2

Use a text editor to type the following file:-

(defun c:insulate ()

(setq a nil)

(setq PT1 (getpoint "Starting Point of Insulation "))

(setq PT2 (getpoint PT1 "\nEnd Point of Insulation "))

(setq THK (getdist PT1"\nThickness of insulation: "))

(setq L1 (distance PT1 PT2))

(setq ANGR (angle PT1 PT2))

(setq ANGD (* ANGR (/ 180. pi)))

(setq NUM (/ L1 50))

(setq NUM (fix NUM))

(setq L2A (/ L1 NUM))

(setq L2 50.0) ;original block is 50wide

(setq FACTOR (/ L2A L2))

(setq THKF (/ THK 75.0)) ;original block is 75high

(command "layer" "m" "AR-INSUL-RD" "c" "1" "" "")

(command "insert" "insulate" PT1 FACTOR THKF ANGD)

(setq N 1)

(while (< N NUM)

(setq E1 (entlast))

(setq PT3 (polar PT2 ANGR (* FACTOR L2)))

(command "copy" E1 "" PT2 PT3)

(setq PT2 PT3)

(setq N (+ N 1))

)

(command "redraw")

)

To understand the above example is very

important, because this method could be

used to prepare other useful graphics, for

example, layer of gravel, layer of solid

insulation, or corrugate steel plate

section....etc.

Fig. 8-3 shows the fence symbol by

AutoLisp. AutoCAD allows the preparation

of linetype with a symbol as part of it. To

prepare that kind of linetype you need to

prepare a shape first. If you do not want to

prepare a shape, the alternative is to use

Figure 8-3

Page 38: LISP-1

--3

AutoLisp as shown by the example below: (defun c:fence ()

(graphscr)

(setq STY (getvar "textstyle"))

(COMMAND "LAYER" "M" "ST-MISCL-YE" "C" "2" "" "")

(if (= SC nil)

(setq SC (getreal "Scale of dwg. 1: "))

)

(setq TXTSZ (* SC 1.0))

(command "style" "fence" "simplex" TXTSZ 1.0 0.0 "n" "n" "n")

(setq PT1 (getpoint "First point of fence ")) (terpri)

(setq PT2 (getpoint "2nd point of fence" PT1)) (terpri)

(while PT2

(setq LL (* SC 1.0))

(setq ANG1T2 (angle PT1 PT2))

(setq ANG2T1 (angle PT2 PT1))

(setq ANG1T2D (* ANG1T2 57.29578))

(setq LEN (distance PT1 PT2))

(setq SEC (/ LEN (* LL 10.0)))

(setq FSEC (fix SEC))

(setq SECL (/ LEN FSEC))

(setq PTALIST nil)

(setq DLEN SECL)

(repeat FSEC

(setq PTA (polar PT1 ANG1T2 DLEN))

(setq PTALIST (cons PTA PTALIST))

(setq DLEN (+ SECL DLEN))

)

(foreach PTA PTALIST

(setq PTA2 (polar PTA ang2T1 (- SECL TXTSZ )))

(setq PTA1 (polar PTA ang2T1 TXTSZ))

(command "text" "m" PTA ANG1T2D "X" )

(command "line" PTA1 PTA2 "")

)

(command "redraw")

(setq PT1 PT2)

(setq PT2 (getpoint "2nd point of fence" PT1)) (terpri)

)

(setvar "textstyle" STY)

)

The above programme could be easily modified to include other text to indicate

utility lines on the drawing. It is wise to make sure the steps and logics are fully

understood.

Page 39: LISP-1

--4

Tutorial:

1. Draw the symbol of the section of a cut-off pipe and write an AutoLisp

programme to fit the symbol to two selected points.

file: PEND.LSP

2. Write an AutoLisp programme to draw railway lines.

file: RAIL.LSP

3. Write an AutoLisp programme to draw a symbol to represent an exhaust fan, as

shown on Fig. 8-6. The user input will include the location for center of fan, and a

point at the edge of fan only. Draw the grille and the blades on different layers. file:

EXFAN.LSP

Page 40: LISP-1

9--1

LESSON 9

entsel entget assoc ssget ssname sslength

subst entmod cdr

Open a drawing named AENTITIES@

which consist of 3 lines and 2 layers,

ROADEP and ROADCL.

At the command prompt, type: (setq a2 (entsel))

Computer response: Select object:

pick the solid line at right of the centerline.

Computer response: (<Entity name: ba4540> (8.51974 2.20869 0.0)

entsel AutoLisp function to select an entity, and it returns the entity name

and the coordinate of the point you picked.

On the drawing database all entities are identified by an alphanumeric name as

shown above. It is by using this entity name we could tap into the information

specific for that entity.

entget AutoLisp function to extract the entity list which contains all

information neccessary to describe the entity on the drawing

database.

At the command prompt, type (setq b2 (entget (car a2)))

Computer response: ((-1 . <Entity name: ba4540>) (0.@Line@))

Now all the information of the entity are assigned to the variable name, b2. When

!b2 is type, the whole list will be displayed. Each item in the list is really in two

parts. The first is a code number that tells the function of that entity=s aspect.

Listed below are a few code numbers :-

0 description

Page 41: LISP-1

9--2

6 line type name

7 text style name

8 layer name

assoc An AutoLisp function that searches for a sublist within any list. With

an entity list, assoc uses the entity code number as key.

at command prompt type: (setq L1 (assoc 8 b2))

computer response: (8 . AROADEP@)

at the command prompt type: (setq L2 (cdr L1))

computer response: AROADEP@

As shown, by using entsel, entget, car, assoc and cdr we could find the layer name

of a selected entity. Therefore, it would not be too complicate to write an AutoLisp

to set the current layer to a selected entity.

Use text editor to type the following file: (defun c:setlayer ()

(graphscr)

(setq A2 (entsel "Select object to set current layer: "))

(setq B2 (entget (car A2)))

(setq L1 (assoc 8 B2))

(setq L2 (cdr L1))

(command "layer" "s" L2 "")

)

The above example uses entsel to select an object. When many entities have to be

selected at the same time, the function to use should be ssget. Usually after ssget

is used, the number of objects selected has to be known so that a repeat function

could be used to perform a certain task for each of the selected entity. sslength is

the function to find out how many selected entities are there. Each selected entity

also carry an index which starts from 0. ssname followed by the variable name of a

selected set and the index will secure the name of an entity on the drawing

database.. Properties of an entity could be changed by changing the values of their

sublist. subst and entmod are the functions to achieve this operation.

ssget AutoLisp function to select entities.

Page 42: LISP-1

9--3

ssname AutoLisp function to secure the name of an entity.

sslength The function to determine the number of entities in a selection set.

subst AutoLisp command the substitutes on entity sublist for another within

an entity list.

entmod AutoLisp function that updates the drawing database with the new

entity list.

Use a text editor to type the following file:

(defun c:chlayer ()

(graphscr)

(prompt Aselect objects to change to another layer@)

(setq A1 (ssget))

(terpri)

(setq A2 (entsel "Select object in the right layer:"))

(setq N (sslength A1))

(setq INDEX 0)

(setq B2 (entget (car A2)))

(setq D2 (assoc 8 B2))

(repeat N

(setq B1 (entget (ssname A1 INDEX)))

(setq D1 (assoc 8 B1))

(setq B3 (subst D2 D1 B1))

(entmod B3)

(setq INDEX (+ INDEX 1))

)

)

The following is another example to use the AutoLisp functions just introduced.

cdr AutoLisp function that produces the second and remaining elements in a list.

e.g. if a is a list (4 5 6 7), then (cdr a) will return (5 6 7).

(defun c:chtexth ()

(prompt Aselect the texts to be change...@)

(setq A (ssget))(terpri)

(setq TH (getreal "Enter new text height: "))

(setq N (sslength A))

(setq INDEX 0)

(repeat N

Page 43: LISP-1

9--4

(setq B1 (entget (ssname A INDEX)))

(setq INDEX (+ 1 INDEX))

(setq B (assoc 0 B1))

(if (= "TEXT" (cdr B))

(progn

(setq C (assoc 40 B1))

(setq D (cons (car C) TH))

(setq B2 (subst D C B1))

(entmod B2)

)

)

)

)

Page 44: LISP-1

G-i

GLOSSARY/INDEX The following is a glossary and index of AutoLISP functions used in the course.

There are far more AutoLISP functions than what we have covered. For further

information, consult your AutoLISP Programmer=s Reference which comes with

AutoCAD software.

(angle <pt1> <pt2>) Lesson 3

(assoc <item> <alist>) Lesson 9

(cadr <list>) Lesson 4

(car <list>) Lesson 4

(cdr <list>) Lesson 9

(command <args>.....) Lesson 1

(cons <new first element> <list>) Lesson 7

(defim <sym> <argument list> <expr>...) Lesson 1

(distance <pt1> <pt2>) Lesson 2

(entget <ename>) Lesson 9

(entlast) Lesson 8

(entmod <elist>) Lesson 9

(entsel [<prompt>] ) Lesson 9

(fix <number>) Lesson 8

(foreach <name> <list> <expr>......) Lesson 7

(getangle [<pt>] [<prompt>]) Lesson 3

(getcorner pt [prompt]) Lesson 4

(getdist [<pt>] {<prompt>]) Lesson 5

(getint [<prompt>]) Lesson 2

Page 45: LISP-1

G-ii

(getpoint [<pt>] {<prompt>]) Lesson 1

(getreal [<prompt>]) Lesson 2

(getstring [<cr>] [<prompt>]) Lesson 2

(getvar <varname>) Lesson 6

(graphscr) Lesson 4

(if <testexpr> <thenexpr> [<elseexpr>]) Lesson 3

(list ,expr>......) Lesson 4

nil Lesson 3

(or <expr>......) Lesson 7

(polar <pt> <angle> <distance>) Lesson 3

(progn <expr>.......] Lesson 6

(prompt <msg>) Lesson 2

(repeat <number> <expr>.....) Lesson 5

(rtos <number> [<mode> [<precision>]]) Lesson 6

(setq <sym1> <expr1>) Lesson 1

(setvar <varname> <value>) Lesson 6

(ssget) Lesson 9

(sslength <ss>) Lesson 9

(ssname <ss> <index>) Lesson 9

(strcat <string1> <string2>.....) Lesson 6

(strlen <string>) Lesson 6

Page 46: LISP-1

G-iii

(subst <newitem> <olditem> <list>) Lesson 9

(substr <string> <start> [<length>]) Lesson 6

(terpri) Lesson 2 (while <testexpr> <expr>......) Lesson 5