You are on page 1of 13

# FUNCTIONS Lists This function takes any number of expressions and makes a list out of them, and returns

the result. The expressions can be integers, real numbers, strings, variable names or even other lists. The coordinate of a point is a commonly used list in AutoLISP. oordinates can be t!o"dimensional or three"dimensional. T!o dimensional coordinates# \$%&.'( )(.*+, Three dimensional coordinates# \$%&.'( )(.*+ '-..', S/0TA1# \$set2 p \$list ) ( ',, 3p4 is assigned the value \$) ( ', ommand# \$set2 p \$list ) ( ',, returns \$) ( ', Alternative method of !riting lists# \$2uote \$a b c,, or 3\$a b c, each of !hich returns \$A 5 ,, 6\$a b 7c7, that returns \$A 5 7c7,, 6\$a ' 7c7, that returns \$A ' 7c7,. \$2uote \$a b c,, or 3\$a b c, returns the expression !ithout evaluating it !hile \$list a b c, returns the expression after evaluating it. ommand# \$set2 a %&& b '& c 8&&, returns 8&& ommand# \$set2 pt \$list a b c,, returns \$%&& '& 8&&, ommand# \$set2 pt 3\$a b c,, returns \$A 5 ,. So for the first expression, it returns \$%&& '& 8&&,, !hile for the second it returns \$A 5 ,. That means evaluation is not done for the second case. Car This function separates the first element out of a list. \$set2 2 \$car p,, ommand# \$set2 2 \$car p,, ) 324 is assigned the first element 3)4 of the point p. Cdr This function separates the remaining elements other than the first out of a list. If the list is empty, cdr returns nil. \$set2 t \$cdr p,, ommand# \$set2 t \$cdr p,, \$( ', 3t4 is assigned the remaining elements \$( ' , of 3p4 Cadr The functions ar and dr can be used in a combined form to get the second element of 3p4.

\$set2 u \$car \$cdr p,,, ommand# \$set2 u \$car \$cdr p,,, ( 3u4 is assigned the middle element ( of 3p4 This combined form is 3 adr4# \$set2 u \$cadr p,, ommand# \$set2 u \$cadr p,, ( Nth This function is used to retrieve an item at a specific position in a list. The syntax is# \$nth n list,. Say there is a list like \$set2 a 6\$x %& 7y7,,, !hich returns at the command prompt# \$1 %& 7y7,. 0o!, to trace, say, the second item of the list 3a4# ommand# \$nth 1 a, returns %&. 9or the third item, ommand# \$nth 2 a, evaluated as =ero. Append This command adds the items of t!o or more lists into one list. The syntax is# \$append list1 list2>, ?xample# \$append 3\$'- -..' :last;, 3\$*& :mid; .'.&,, it returns \$'- -..' 7last7 *& 7mid7 .'.&, returns :y;. Thus \$nth n list, !ould return the \$n<%,th item of a list. The first item of the list is

Assignment Function @ariable assignment in AutoLISP is done through the function 3set24, the concise form of 3S?T Auotes4. \$set2 a %', means an integer value is assigned to the variable 3a4. \$set2 p2r b, means a value 3b4 is assigned to the variable 3p2r4. \$set2 statement : By name is Asis.;, means the variable 3statement4 is assigned a string :By name is Asis.; Arithmetic Functions Arithmetic operators <, ", C, D E are used# < \$add, 9unction# \$< number% number8 >,

" \$subtract, 9unction# \$" number% number8 >, C \$multiply, 9unction# \$C number% number8 >, E \$divide, 9unction# \$E number% number8 >,

Bore than one arithmetic operator can be simultaneously used in a nested operation. ?xample# \$< (.*.- \$" -8' \$C %&.' \$E '&.( 8,,,, This means '&.( (.*.- + -8' %& . ' C 8 s2rt \$s2uare root, 9unction# \$s2rt number, This calculates and returns the s2uare root of the number. ?xample# \$s2rt -8', returns the s2uare root of -8', !hich is 8'. 0atural log of a real number# \$log x, 0atural antilog of a real number# \$exp y, \$exp \$C n \$log x,,, function

## Log Antilog To find out xn

Trigonometric operators \$sin angle,, \$cos angle, and \$atan number1 [number2], are other built in arithmetic functions. The angles have to be expressed in radians rather than degrees. %. \$sin \$E pi -,, means Sin )&& and returns &.'F 8. \$cos \$E pi -,, means os )&& and returns &.*--&8'F ). \$atan number1 number2, or \$atan number% GpromptH, is arctangent
number% number 8 . If number8 is and returns in radians the value of tan
"%

not given, it returns tan"% number%. 0umber8 can be entered by prompt. ?xamples# \$atan % \$s2rt ),, that returns &.'8)'++ \$atan % \$getreal 7InPut a value#7,,, putting a value, say, ' it returns &.%+.)+Jse the expression \$C \$E pi %*&, ang, to convert degrees to radians. That is, the expression converts the variable ang \$in degrees, into radians. Other operators K \$equal to, 9unction# This function compares arguments for numerical e2uality. \$K number1 number2 ...,. Arguments are a number or a

string. If all arguments are numerically e2ual, it returns T, other!ise it returns nil. ?xamples# \$K ' -.', returns nil \$K \$C ' ', \$E %&& (,, returns T EK \$not equal to, 9unction# This function compares arguments for numerical ine2uality. Arguments are a number or a string. \$EK number1 number2 ...,. If no t!o successive arguments are the same in value, it returns T, other!ise nil. ?xamples# \$EK ' -.', returns T \$EK \$C ' ', \$E %&& (,, returns nil \$EK :Auto AL; :AutoLISP;,returns T. M \$less than, 9unction# This function returns T if each argument is numerically less than the argument to its right, and returns nil other!ise. Arguments can be numeric or strings. \$M number1 number2 ...,. MK \$less than or e2ual to, 9unction# This function returns T if each argument is numerically less than or e2ual to the argument to its right, other!ise nil. The arguments can be numeric or strings. \$MK number1 number2 ...,. N \$greater than, 9unction# This function returns T if each argument is numerically greater than the argument to its right, and nil other!ise. Arguments can be numbers or strings. \$N number1 number2 ...,. NK \$greater than or e2ual to, 9unction# This function returns T if each argument is numerically greater than or e2ual to the argument to its right, and nil other!ise. Arguments are a number or a string. \$NK number1 number2 ...,. O \$bit!ise 0PT, 9unction# This function returns the bit!ise 0PT \$%4s complement, of the argument. The argument must be an integer " positive or negative. ?xamples# \$O ', returns Q\$O '&&, returns Q'&% \$O "%&&&, returns +++ %< \$increment, 9unction# This function increments the integer or real number in the argument by % and returns the result. ?xamples# \$%< ), returns (

(No space bet een ! " # sign\$ but space bet een # sign and the next entr%& 1- (decrement) Function: This function decrements the integer or real number in the argument by 1 and returns the result. ?xamples# \$%" ), returns 8 (No space bet een ! " ' sign\$ ho e(er) space bet een ' sign and the next entr%&* +efining a ne function 3Lefun4 is the function for defining a ne! function in AutoLISP. The t!o terms L?fine 9J0ction constitute L?9J0. (+efun function_name (& & Say the ne! function name is 3test4, then (+efun test (& (set, a -..& (set, b (/ - 0&& (# a b& & The most common method to create an AutoLISP program is to save it as a text file !ith the extension .lsp, such as, test.lsp. The function can be created alternatively in the follo!ing !ay# (+efun c1test (& (set, a -..& (set, b (/ - 0&& (# a b& & 2eometric Utilit% 3oint The syntax is# \$set2 p% 3 \$a b c,, or \$set2 p8 3 \$'.& %&.& &.&,, +istance 2. It finds the distance bet!een t!o points in a plane or in

space. \$distance 1 2,,. It finds the distance bet!een t!o points 1 and

Angle It returns the angle in radians bet!een an imaginary line passing through t!o points and the 1"axis \$of the current J S,. The syntax is# \$angle 1 2,,.. 3olar This function finds a point via polar coordinates relative to an initial point. \$set2 p8 \$polar p% ang length,,. Rere p8 is at a distance e2ual to length and at an angle ang, !ith the 1"axis \$of the current J S,.

2etxxx Functions 2etint This function prompts the user to input an integer value. \$getint !"n ut number of circles : #, The symbol 3In4 stands for 3ne! line4 and moves the cursor to the next line before prompting the user for input. 2etreal This function prompts the user to input a real number. \$getreal !"n ut radius of circles: #,

2etdist This function asks the user to enter a numeric value to indicate distance bet!een t!o vectors. The input may be integer or real and gets automatically converted to real. The syntax for this function is as follo!s. \$getdist !"n ut height of cone: #,

It also returns distance bet!een an optional point and cursor position set through prompt. \$getdist t1 !"nselect a oint: #, 2etpoint This function allo!s the operator to pick a point from the Auto AL screen. \$getpoint ! ic\$ a oint : #,

Rere the point p !ill be assigned three elements, !hich should be typed separated by a space in bet!een. ommand# \$set2 p \$getpoint ! ic\$ a oint : #,, Pick a point# \$8%.'()8 )'.'(.* &, \$8%.'()8 )'.'(.* &, 2etangle This enables an user to input an angle. /ou can input the value in degrees but AutoLISP converts it into radians. If you specify t!o points through lists or one point through a list and other point by mouse on the screen, AutoLISP returns the angle made by the line Soining the above points !ith positive 1 direction. /ou can also give inputs by selecting t!o points by mouse. In each case AutoLISP returns the angle in radians.

The getangle function measures angles !ith the =ero"radian direction \$set by the A0T5AS? system variable, !ith angles increasing in the counter" clock!ise direction. 9ollo!ing expressions illustrate ho! the function is used# \$getangle %"nenter an angle: #,F \$getangle pt% pt8,F \$getangle pt% %"nenter a oint: %,, it !aits for user input of a point by mouseF \$getangle 7InSelect t!o points# 7,F \$getangle,, it !aits for user input of t!o points by mouse. The other (2etxxx& functions are# 2etstring This function pauses for user input of a string, and returns that string. The syntax is# \$getstring :In/ou learnt Auto ALU# ;, it returns /ou learnt Auto ALU# If you !rite / after colon, it returns :/; The length of the string should be of maximum %)8 characters. If the string is longer than that, the function returns only the first %)8 characters of the string. 2etcorner It finds a point \$the opposite corner of a box, from the screen through mouse. \$getcorner pt GstringH, Tiven t as the first corner of a box or a rectangle, it pauses for the user input of the opposite corner. GstringH is a string as a message. ?xample# \$getcorner 3\$'&.& .'.& &.&, :InPick other corner# ;,, it returns Pick other corner# If you pick another point on the screen by mouse, it returns that point. /ou cannot input the second point by providing coordinates follo!ing the syntax of LISP. Say, you input the second point in the form of \$%&&.& %'&.& &.&,, on the prompt line you see# an6t reenter LISP.

LOO3 9or repeating an action for, say, 3n4 number of times loops are formed in Auto LISP codes. 4epeat Vhen the number of iterations are kno!n this function is used. Syntax#

\$repeat n """"""""""""" """"""""""""" ,Fend of repeat 5hile It is a very useful loop generator. The loop remains active as long as the specified condition is being fulfilled. Vhenever the condition is not fulfilled, the loop stops. AutoLISP has no 3go to4 statement. Syntax# \$!hile \$condition or test"expression, \$expression, \$expression, """"""""""""" ,Fend !hile If'Then'6lse 5y this function one can precisely define !hat one can do if a pre"defined condition is met and !hat else one can do if that is not satisfied. 3If4 is follo!ed by a 3then4 and thereafter an 3else4. Ro!ever, the 3else4 condition is optional. onditions# \$If \$M number% number8,,F \$If \$N number% number8,,F \$If \$K number% number8,,F \$If \$EK number% number8,,F \$If \$MK number% number8,,F \$If \$NK number% number8,,. Syntax# \$if \$condition expression, \$expression,Fthen \$expression,Felse

,Fend if 3rogn This is used !hen several expressions need to be evaluated !here only one expression is expected. An 3if& function expects one 3then& and an optional 3else& expression for evaluation. This function evaluates each expression se2uentially and returns the value of the last expression. ?ffectively progn converts a number of expressions into one expression. Syntax# \$if \$condition expression, \$progn \$expression, """""""""""""" ,Fend progn \$progn \$expression, """""""""""""" ,Fend progn ,Fend if 7oolean Operations Or The syntax of the logical \$or, is \$or \$e' ression1, \$e' ression2, > , This function evaluates the t!o expressions, and returns T \$true, if either or both of them evaluate to T. It returns 3nil4 if neither expression evaluates to T. And The syntax of the logical and is \$and \$e' ression1, \$e' ression2, > , F else expressions \$optional, F then expressions

This statement means if all the expressions return true, the function !ill also return true. Ro!ever, the function !ill return nil, if even one of the expressions do not return true. Not The syntax of the logical not is \$not , The meaning of the expression is# if has a value of nil, the function returns true and if has any non"nil value including =ero, the function returns nil. Input 8alidation1 Initget This function automatically validates the users input at a 3Tetxxx4 statement. The 3Initget4 function sets flags for the type of validation method to use. This function should be !ritten immediately before entering an input data. The syntax is# \$Initget bits string, 7its argument The bits are codes !ith special significance. 9eaning of the code of bit Prevents null input and checks hitting MenterN Prevents =ero input values Prevents negative input values Skips checking dra!ing limits even if Auto AL command LIB R? W is on %0ot used no! )8 Provides dashed lines !hile rubber band is used for lines or boxes -( Prevents X"coordinate input %8* Sets arbitrary entry bit Table1 Initget bit (alues ith their meaning \$initget %, \$set2 ang \$getreal 7In?nter the value of the angle#7,, This statement prevents the user from entering a null entry for input value of 3ang4. If the user Sust hit the key 3enter4, Auto AL !ill return a message 3Ye2uires numeric value.4 \$initget 8, \$set2 ang \$getreal 7In?nter the value of the angle#7,, Auto AL !ill pause for a non=ero entry for the input value of 3ang4. If the user inputs =ero, Auto AL !ill not accept it and !ill return a message stating 3 (alue must be non)ero&. The program !ill stop until a non"=ero value is entered. \$initget (, \$set2 ang \$getreal 7In?nter the value of the angle#7,, 7it (alue % 8 ( *

the user !ill be prevented from entering a negative value of 3ang4. The Auto AL !ill sho! a message like *(alue must be ositi(e&. Sometimes the bit values are summed up to get combined effect of different bits. The syntax is# \$initget \$< % 8 (,, \$set2 ang \$getreal 7In?nter the value of the angle#7,, This !ould prevent the user from entering a null, a =ero and a negative value. Summing up the bit values one may !rite, \$initget .,. String argument of Initget Take an example# \$initget % :/ 0;, \$set2 reply \$getk!ord 7In/es or 0o M/E0N# 7,, The user here is allo!ed to enter only / or 0. If the user tries to enter any other letter, the command prompt returns the message 3+n(alid o tion \$ey,ord& and allo!s him to input again. 2et: ord is the function used for enabling the user to get a key !ord.

6NTIT; S6L6CTION In Auto AL there are commands like ?YAS?, This is also true !hile !riting Auto LISP program. The functions are# Ssget1 It is the most commonly used function for getting selection set. The syntax is, \$ssget Gselection"modeH GargumentH, The modes are described in Table. 9or the modes corresponding to serial numbers -, . and *, i.e., for :V;, :9; and :VP;, in the syntax the term like 3pt% pt84 or 3\$list pt% pt8 pt),4 is the argument of the function. Serial No* % 9ode Selection of points 8 :L; Last 9eaning S%ntax \$set2 pt% 6 \$&.& &.& &.&, pt8 6 \$'.& *.& &.&, pt) 6 \$-.& +.& &.&,, \$set2 ? \$ssget :l;,, 6ffect Sets pt%, pt8 and pt) to point values reates a selection set of the last obSect PP/, RPYPP, BIYYPY, P99S?T, BP@?, YPTAT? etc, !hich re2uire the user to create a selection of dra!ing entities.

added to the database that is visible on the screen ) ( :c; :P; rossing Previous \$set2 ? \$ssget :c; pt% pt8,, \$set2 ? \$ssget :p;,, reates a selection set from the most recently created ' :Point; Passing through a point :V; Vindo! \$set2 ? \$ssget :!; pt% pt8,, \$set2 ? \$ssget pt,, selection set reates a selection set of an obSect passing through point pt reates a selection set of the obSects inside the !indo! . :9; 9ence \$set2 ? \$ssget :f; \$list pt% pt8 pt),,, from pt% to pt8 reates a selection set of the obSects crossing the fence and defined by the points pt%, pt8 and * :VP; Vpolygon \$set2 ? \$ssget :!p; \$list pt% pt8 pt),,, pt) reates a selection set of the obSects inside the polygon defined by the points + :1; All obSects \$set2 ? \$ssget :x;,, pt%, pt8 and pt) reates a selection of all obSects in the database 6ntsel1 This enables the user to select an entity from the graphics screen and returns the name of the entity.

Lra! a polygon, say, a s2uare by Auto AL and on the command prompt enter \$entsel,. The entity name !ill be something like# \$M?ntity name# %*%e+'*N \$%((... %.%.8-% &.&,, Rere, the first one, i.e., %*%e+'* is the entity name and the second one is the point at !hich the entity is selected. In this case the point !here the entity is selected is \$%((... %.%.8-% &.&,. The name of the entity in Auto AL is thus a number. Pne cannot select more than one entity at a time by \$entsel,.