Published by CFS Documentation cell Division of Centre for Electronics Design and Technology of India A Scientific Society of Department of Electronics, Govt. of India, New Delhi.
First Edition: 1999
TRADE MARKS: All brand name and product names mentioned in this book are trade marks or registered trade mark of their respective companies. Every effort has been made to supply complete and accurate information. However, CEDTI assumes no responsibility for its use, nor for any infringement of the intellectual property rights of third parties which would result from such use. No part of this publication may be stored in a retrieval system, transmitted or reproduced in any forms or by any means, electronic, photocopy, photograph, magnetic or otherwise, without written permission of CEDTI.
CEDTI/CFS/99/4/5.3/R1
FOREWORD The Information Technology and Telecom sectors have suddenly opened up avenues, which require a very large specially trained manpower. These sectors are highly dynamic and need training and re-training of manpower at a rapid rate. The growing gap of requirement of the industry and its fulfilment has created a challenging situation before manpower training institutes of the country. To meet this challenge most effectively, Centre for Electronics Design and Technology of India (CEDTI) has launched its nation-wide franchising scheme. Centre for Electronics Design and Technology of India (CEDTI) is an Autonomous Scientific Society under the Govt. of India, Department of Electronics with its Headquarters at New Delhi. It operates seven centres located at Aurangabad, Calicut, Gorakhpur, Imphal, Mohali, Jammu and Tezpur. The scheme will be implemented and coordinated by these centres. The scheme endeavours to promote high quality computer and information technology education in the country at an affordable cost while ensuring uniform standards in order to build a national resource of trained manpower. Low course fees will make this education available to people in relatively small, semi urban and rural areas. State-of-the-art training will be provided keeping in view the existing and emerging needs of the industrial and Govt. sectors. The examinations will be conducted by CEDTI and certificates will also be awarded by CEDTI. The scheme will be operated through all the seven centres of CEDTI. The CEDTI functions under the overall control and guidance of the Governing Council with Secretary, Department of Electronics as its Chairman. The members of the council are drawn from scientific, government and industrial sectors. The Centres have separate executive committees headed by Director General, CEDTI. The members of these committees are from academic/professional institutes, state governments, industry and department of electronics. CEDTI is a quality conscious organisation and has taken steps to formally get recognition of the quality and standards in various activities. CEDTI, Mohali was granted the prestigious ISO 9002 certificate in 1997. The other centres have taken steps to obtain the certification as early as possible. This quality consciousness will assist CEDTI in globalizing some of its activities. In keeping with its philosophy of ‘Quality in every Activity’, CEDTI will endeavour to impart state of the art – computer and IT training through its franchising scheme. The thrust of the Software Courses is to train the students at various levels to carry out the Management Information System functions of a medium sized esTablishment, manufacture Software for domestic and export use, make multimedia presentations for management and effectively produce various manufacturing and architectural designs. The thrust of the Hardware Courses at Technician and Telecommunication Equipment Maintenance Course levels is to train the students to diagnose the faults and carry out repairs at card level in computers, instruments, EPABX, Fax etc. and other office equipment. At Engineer and Network Engineer levels the thrust is to train them as System Engineers to instal and supervise the Window NT, Netware and Unix Networking Systems and repair Microcontrollers / Microprocessor based electronic applications.
An Advisory Committee comprising eminent and expert personalities from the Information Technology field have been constituted to advise CEDTI on introduction of new courses and revising the syllabus of existing courses to meet the changing IT needs of the trade, industry and service sectors. The ultimate objective is to provide industry-specific quality education in modular form to supplement the formal education. The study material has been prepared by the CEDTI, document centre. It is based on the vast and rich instructional experience of all the CEDTI centres. Any suggestions on the improvement of the study material will be most welcome.
(R. S. Khandpur) Director General (CEDTI)
1. INTRODUCTION TO DATA TYPES AND CONVENTIONS OF AutoLISP
1.1 INTEGER An Integer is a whole number that does not contain a decimal point. AutoLISP integers are 32 bit signed numbers with values ranging from + 2,147,483 to - 2,147. AutoLISP uses 32 bit internally; those transferred between AutoLISP and AutoCAD are restricted to 16 bit values and hence the user can pass integer values ranging from - 32,768 to + 32,767 only. e.g.: 23, 45, -32768
1.2 REAL A real number is a number containing a decimal point (i.e., floating point numbers) and can be either positive or negative. Real numbers are stored in double precision floating point format, providing at least 14 significant digits of precision, even through the AutoCAD command line areas shows only 6 significant digits. e.g.: 34, 56, 123, -29.9,-3.0
1.3 STRING A string is a group of characters enclosed within quoted strings. The backslash allows control characters or escape code to be included within the string. Individual strings are limited to 132 characters. e.g. :“cadd”, “CADD”, “123”, “A123”, A123” “\nEnter first point”
1.4 LIST A list is a group of related values separated by spaces and enclosed within parenthesis. List provides an efficient method of storing numerous related values. e.g.: (3 2 4) (a b c d), (1 “ONE”)
1.5 SYMBOL A symbol is a character or a collection of alphanumeric and notation characters except the following: (), ., `,” and;. A symbol name cannot consists of only numeric characters.
e.g.: a, xyz, a1, p1
1.6 FILE DESCRIPTOR File descriptors are alphanumeric labels assigned to files opened by AutoLISP. When an AutoLISP function needs to write or read from a file the label of the must be referenced. The following example opens the file text.dat for reading. Command: (open “text.dat” “r”)
1.7 ENTITY NAMES An entity name is a number assigned to objects in a drawing. It is actually a pointer to a file maintained by AutoCAD from which AutoLISP can find the object’s database record and its vectors. This label can be referenced by AutoLISP functions to allow a selection of objects for processing in various ways. The following example uses the “entlast”, function to get the name of the last object entered into the drawing. Command: (entlast)
1.8 SELECTION SETS Selection sets are groups of one or more objects. You can interactively add objects to or more objects from selection sets, with AutoLISP routines.
1.9 SURBS AND EXTERNAL SUBRS Most of the AutoLISP functions are built-in subroutines called SUBRS. The functions defined by external ADS and ARX applications are called EXTERNAL SUBRS. The names of Subrs and External subrs are not case sensitive. For example the “princ” function is a Subrs. The “acad_strlsort” function is External subrs.
1.10 CONSTANTS When you explicitly use a data type in an AutoLISP expression that value is known in a constant. There is a predefined constant pi evaluating to 3.14159 and a symbol T evaluating to “TRUE”. Constants should not be used as symbols. For example in the following expression the symbol “a” is set to a constant value “CADD”.
Command: (setq a “CADD”)
1.11 CONVENTIONS OF AUTOLISP AutoLISP code is usually stored in ASCII text files with a “.LSP” or “.MNL’ extension. Although AutoLISP code can be entered at the command line, testing and debugging are considerably easier when you load the AutoLISP code from a file rather than re-enter it each time when you make a refinement. •
AutoLISP follows the prefix notation . i.e., the function name should precede the arguments. e.g. (+ 4 5)
•
Symbol names can consist of any sequence of printable characters except the following:- (), `, “, ; and \
•
Expressions can span multiple lines. Each expression beings with a left parenthesis and consist of a function name and an optional list of arguments, each of which can itself be an expression. The expression ends with a right parenthesis. e.g. (+ 2 3). The arguments in form of expression lead to nesting of functions. e.g. ( + 2 3 ), ( + A B), (+ 2 3) 4)
•
The following characters terminate a symbol name or a numeric constant (), `, “, ; , (space) and end of line)
•
More than one blank space in an expression is considered as a single space. e.g. ( + 2 3 ), ( + 2 3) 7)
•
An AutoLISP expression or a function should start with a open or left parternership i.e., “(“and end with a close or right parenthesis”)”.
•
Symbol and function names are not case sensitive, i.e., you may use upper or lower case characters.
•
Numerals can be preceded by an optional “+” or “-” sign.
•
Real numbers consist of one or more numeric digits, followed by a decimal point i.e., .4 is not recognized as a real; it should be entered as 0.4. Similarly 5. is not recognized as real; it should be entered as 5.0. Integer values range between 32,768 and +32,767.
•
There should be a blank space between a function name and its arguments.
•
Expressions nested within other expressions return their result to the surrounding expressions. AutoLISP evaluates from the innermost expression and returns the value to the surrounding expressions. e.g. (+ (*3 2) (/ (+1.5 2.5))) returns 8.0
•
Comments can be included in a program and they should begin with a semicolon “ ; ” e.g. (+ 5 3 )
•
; This is an Addition
Variables can be evaluated at the “Command prompt” in autoCAD with a preceding “!” symbol. e.g. (seta 1 3) Command: !a returns 3
2. ARITHMETIC AND TRIGONOMETRIC FUNCTIONS
2.1 ARITHMETIC FUNCTIONS Thee are four functions under this group. The arguments for these function can be numbers, variables, symbols and constants. If any one argument is real then the result will be a real value. If all the arguments are integers then the result will be an integer. Syntax : (+ ...)
This function returns the sum of all numbers, which are real or integer. If all numbers are integers then the result is an integer value. If any one number is a real number then the result will be a real value. e.g. Command: (+ 5 3)
returns 8
(+ 6.2 3)
returns 9.2
(+ 8.75 -4)
returns 4.75
(+ 5. 7 9)
returns 21.0
Syntax: (- ...)
This function returns the result after subtracting from . If more than two numbers are given as arguments then the result will be the sum of second and the remaining arguments subtracted from . If only one argument is present then the result will be the value of subtraced from 0. e.g. Command: (- 5 4)
Syntax: (
returns 1
(- 5.5 2)
returns 5.5
(- 8 6 4)
returns -2
(- 10 -5)
returns 15
(- 3)
returns -3
...)
This function returns the product of all operands. If only one is given then this function returns the result of multiplying it by one and supplying no arguments returns the result zero.
e.g. Command: (* 5 4)
returns 20
(* 5.5 2)
returns 11.0
(* 1 2 3)
returns 6
Syntax: (/
...)
This function returns the quotient by dividing by . If more than two operands are given then will be divided by the product of others. If only one is given the function returns the result dividing it by one and supplying no arguments returns zero. e.g. Command: (/ 5 4)
returns 1
(/ 5 4.0)
returns 1.25
(/ 12 2 3)
returns 2
2.2 TRIGONOMETRIC FUNCTIONS
There are three functions under this group. Using these basic. Trigonometric functions you can evaluate any expressions involving Trigonometric calculations. Sin Syntax: (cos) This function returns the sine value of the as a real value. The
(sin 0)
returns 1.0
(sin 2)
returns 0.909297
Cos Syntax: (cos ) This function returns the cosine value of as a real value. The argument must be expressed in RADIANS. e.g. Command:
(cos 0)
returns 1.0
(cos 6)
returns 0.96017
atan Syntax: (atan [|) This function returns the arctangent value of in RADIANS. The result is tan inverse of . If both and are supplied then this function returns the arctangent of divided by in RADIANS. e.g. Command:
(atan 1.0)
returns 0.785398
(atan 2.0 3.0)
returns 0.588003
3. BASIC STRUCTURE FUNCTIONS
abs Syntax: (abs ) This function returns the absolute value of a . e.g. Command:
(abs -4)
returns 4
(abs 25)
returns 25
(abs -3.425)
returns 3.425
sqrt Syntax: (sqrt ) This function returns the square root of as a real number. e.g. Command:
(sqrt 16)
returns 4.0
(sqrt 2025.0) returns 45.0 log Syntax: (log ) This function returns the natural logarithm of to base should be positive. e.g. Command:
(log 1)
returns 0.0
e as a real. The
(log 2.71828) returns 0.999999 exp Syntax: (exp ) This function returns the value of the e raised to the specified number (the antilogarithm of ). e.g. Command:
(exp 0)
returns 1.0
(exp 1)
returns 2.71828
expt Syntax: (expt ) This function returns the value of raised to the specified . The arguments may be integer to real. e.g. Command:
(expt 2 2 )
returns 4
(expt 2.0.3)
returns 8.0
(expt (* 4 5) (/4 2 ) )
returns 400
max Syntax: (max ....) e.g. Command:
(expt 4 5 3)
returns 5
(expt 2. 0.1 1.2)
returns 2.0
min Syntax: (min .....) This function returns the minimum numeric value in the given set of arguments. The arguments may be signed real or integers. e.g. Command:
(min 3 2 1)
returns 1
(min -2 1.2 -0.5)
returns -2.0
5. LIST HANDLING FUNCTIONS
List provide an efficient method of storing numerous related values. car Syntax: (car ) This function returns the element of the . If the list is empty then the function returns nil. e.g. Command:
(setq L1 (list 2 “A” 32 “B”))
returns (2 “A” 32 “B”)
(car L1)
returns 2
(setq L2 (list (list 2 3) (list 4 4) ) )
returns ( (2 3) (4 4) )
(car L2)
returns (2 3)
cdr Syntax : (car ) This function returns a list which contains all the elements of , except the element of the list.
e.g. Command:
(setq L (list 1 2 3 4) ) (cdr L)
returns (1 2 3 4) returns (2 3 4) )
(setq m (list (list 2 3 4) (list 2 3 4 ) returns ( (2 3) (4 4) ) (list 5 6) ) )
returns (1 2 3) (5 6)
(cdr m)
returns ( (2 3 4) (5 6) )
The car and cdr functions can be concatenated up to FOUR levels. A few concentrations are as shown below:cadr caddr cadddr caar caaar caaaar cadar and so on e.g. Command:
(setq L (list 1 2 3 ) ) (cadr L)
returns (1 2 3) returns 2
(caddr L)
returns 3
Explanation: (cadr L)
returns (2 3)
(caddr is (car (cdr L) hence
returns 2
(caddr L) is (cdr (cdr L) which
returns (3)
length Syntax : (length ) This function returns the number of elements in the as an integer. e.g. Command:
(setq L (list 1 2 “A” ) ) (length L)
returns (1 2 “A”) returns 3
(setq m (list 1 (list 2 3) ) (length m)
returns (1 (2 3) ) returns 2
last Syntax: (last ) This function returns the last element of . The must not be nil. e.g. Command:
(setq L (list 1 2 “A” ) ) (last L)
returns (1 2 “A”) returns “A”
nth Syntax: (nth ) This function returns the indexed element of the . The element of the will always have an index 0. The second element has an index 1, the third element has an index 2 ans so on. e.g. Command:
(setq L (list 1 2 “C” “A” ) )
returns (1 2 “C” “A”)
(nth 0 L)
returns 1
(nth 1 L)
returns 2
(nth 2 L)
returns “C”
(nth 6 L)
returns nil
reverse Syntax: (reverse It returns the by rearranging the elements in reverse order. e.g. Command:
(setq L (list 1 2 3 4 ) ) (reverse L)
returns (1 2 3 4) returns (4 3 2 1)
(setq m
returns (“A” (2 3) 1)
member
Syntax: (member -
This scans through the for the occurrence of the - and returns the remaining portion of the list starting from the first occurrence of the
- . The function returns NIL, if the
- is not found in the
. e.g. Command:
(setq L (list 1 2 3 “A” “B” ) ) (reverse L)
returns (1 2 3 “A” “B”)
returns (4 3 2 1)
(setq m
returns (“A” (2 3) 1)
member Syntax: (member -
This scans through the for the occurrence of the - and returns the remaining portion of the list starting from the first occurrence of the
- . The function returns NIL, if the
- is not found in the
. e.g. Command:
(setq L (list 1 2 3 “A” “B” ) )
returns (1 2 3 “A”
“B”) (member 3 L)
returns (3 “A” “B”)
(member 5 L)
returns nil
(setq m (list (list 2 3) (list 6 7) 8) )
returns (2 3) (6 7) 8)
(member (list 6 7) m)
returns ( ( 6 7 8)
append Syntax: (append ....) This function combines all the given arguments and forms a single list in the given order. e.g. Command:
(setq L `(1 2) L2 `(3 4) ) (append L1 L2)
returns (1 2 3 4)
(append L1 L2)
returns ((1) (2) (3))
subst Syntax: (subst This function substitutes a in the place of the in the list and returns the modified list. If the does not exist in the given list then the original list is returned. e.g. Command:
(setq L `(1 2) (3 4) 1))
returns (1 2 (3 4) 1)
(subst `0 `1 a)
returns (0 1 2 (3 4) 0)
(subst `3’ `(3 4) a)
returns (1 2 3 1)
acad_strlsort Syntax: (acad_strlsort ) This function sorts a of string by alphabetical order. This will be useful when the user presents a list containing Layers, Blocks or File names in a scrolling list box or popup list within a dialog box. e.g. Command:(acad_strlsort “Thursday” “Friday” “Saturday”
`(Sunday”
“Monday”
“Tuesday”
“Wednesday”
will return (“Fdiday” “ Monday” “ Satuday” “Sunday” “Thursday” “Tuesday” “Wednesday”)
6. PROGRAM CONTROL FUNCTIONS
Syntax: (= [...) ) This function checks for equality and returns T if all s are numerically equal and NIL otherwise. The arguments can be either a number or a string. e.g. Command:
(= 4 4.0)
return T
(= “cadd” “cadd”
return T
(= “CADD” “cadd”)
returns nil
(= 45 44.999)
returns nil
/= Syntax: (/= atom2> [...]) This function checks for inequality and returns T if all the specified s are not numerically equal and NIL otherwise. e.g. Command:
(/ = 100 108)
returns T
(/ = “Cadd” “Cadd”)
returns nil
(/= “a” “b” “c”)
returns T
< Syntax: (< [atom3>....]) This function checks and returns T if is less than and NIL otherwise. If more than two s are given then T is returned, if each is LESS THAN the to its right and nil otherwise. e.g. Command:
(<50 45)
returns nil
(<“a” “b”)
returns T
(<2 3 8 8)
returns nil
(< 3 4 5 5)
returns nil
<= Syntax: (<=atom1> [...]) This checks and returns T if is less than or equal to and nil otherwise. If more than two s are given then T is returned, if each is LESS THAN or EQUAL TO the to its right. e.g.
(< = 11 12)
returns T
(< = “Cadd” “cadd”) returns T (< = 2 3 4 4)
returns T
(< = 88 66 33)
returns nil
> Syntax: (>= [...]) This checks and returns T if is greater than or equal to and nill otherwise. If more than two s are given then T is returned, if each is GREATER THAN or EQUAL TO the to its right. e.g.
(> 50 40)
returns T
(> “Caadd” “cadd”)
returns nil
>= Syntax: (>= [...]) This checks and returns T if is greater than or equal to and nil otherwise. If more than two s are given then T is returned, if each is GREATER THAN or EQUAL TO the to its right. e.g.
(> = 50 40)
returns T
(> = 5 4 3 2)
returns T
(> = 45 3 2)
returns nil
(> = “b” “c”)
returns nil
eq Syntax: (eg )
This function returns, T if the two expressions given are identical. Otherwise it returns NIL. This function determines whether and are bound to the same object. e.g. Command:
(setq a `(1 2 3) b `(1 2 3) c b) (eq a c) returns nil
This returns nil because a and c are not the same lists. (eq b c)
returns nil
This returns T if a and b are exactly the same. equal Syntax: (equal []) This function returns T, if the two expressions evaluate to the same value. This is not the same as eq function. the optional parameter denotes the maximum amount by which the and can differ and still be considered equal. e.g. Command:
(setq a(*3 2) b (*6 1) ) (equal a b)
returns T
(equal `a `b)
returns nil
e.g. Command:
(setq a 5.34679105) (setq b 5.34678106) (equal a b 0.00000001)
return T
minusp Syntax: (minusp - ) This function returns, T if the specified
- is negative and otherwise it returns NIL. e.g. Command:
(minusp -1)
returns T
(minusp -3,4)
returns T
(minusp 4)
returns nil
boundp Syntax: (boundp - ) This function returns T, if the specified
- is bound to a symbol and returns NIL if no value is bound to the
- . If the
- is an undefined symbol then it is automatically created and is bound to NIL.
e.g. Command:
(setq a 1 b bil) (boundp `a)
returns T
(boundp `b)
returns nil
zerop Syntax: (zerop - ) This function returns T, if the specified
- is real or integer and evaluated to zero. Otherwise it returns NIL. e.g. Command:
(setq a 0 b 1) (zerop a)
returns T
(zerop b)
returns nil
(zero 0.1)
returns nil
listp Syntax: (listp - ) This function returns T, if the specified
- is a list. Otherwise it returns NIL. e.g. Command:
(setq a `( (2 3) 4 5) )
(listp a)
returns T
(listp (cadr a) )
returns T
(listp `a)
retuns nil
type Syntax: (type - ) This function checks the data type of the specified
- . The allowed data types are: TYPE
DESCRIPTION
REAL
Floating-point numbers
FILE
File descriptors
STR
Strings
INT
Integers
SYM
Symbols
LIST
Lists
SUBR
Internal functions
PICKSET
Selection sets
ENAME
Entity names
PAGETB e.g.
Function paging table
Command:
(setq a 1 b 4.5 d `(1 2 3) ) (type a)
returns INT
(type b)
returns REAL
(type d)
returns LIST
(type - )
returns SUBR
(type c)
returns nil
6.1 LOGICAL OPERATORS Syntax: (and ....)
This function returns T if all the s evaluate to a non nil value. It returns NIL, if any one of the given s evaluate to NIL and ceases further evaluation. You can also supply variable names as s. e.g. Command: (setq a 5 b 10 c nil) (and (> a 2) (< b 25 ) 0
returns T
(and (= a 5) (= c 05) )
returns nil
(and a b)
returns T
(and a b c)
returns nil
or Syntax: (for ...) The function returns the value T, if any one or all the s in the argument list evaluate to a non nil value. If all the s evaluate to nil then the function returns NIL. Variable names can also be supplied as arguments. e.g. Command: (setq x 10 y 20 z 30 a nil b nl) (or (= x 10) (= y 2) )
returns T
(or (= x 10) (= y 20) )
returns T
(or (= x 11) (= y 21) )
returns nil
(or x ( (= y 20) )
returns T
(or x y)
returns T
(or a b)
returns nil
7. STRING HANDLING FUNCTIONS
String is a group of characters enclosed within quotation marks. A single string value is limited to 132 characters. Strings of unlimited length can be created by joining the strings together and control characters can also be included within quoted strings. Applications can also compare a string to a wild-card pattern and this facility is widely used when you are building a selection set and retrieving the entirely data by application name. strcase Syntax: (strcase []) This function converts the alphabetic characters of the to upper or lower case, depending on the optional argument . When is not specified or evaluated to NIL, the is converted to upper case. Otherwise it is converted to lower case characters. e.g. Command: (strcase “Hello”)
returns “HELLO”
(strcase “HELLo” 1)
returns “hello”
strcat Syntax: (strcat string2>...) This function combines multiple strings into a single string vlaue Blank and null strings can also be combined. e.g. Command: (strcat “cadd” “centre”)
returns “cadcentre”
(strcat “cadd” “ “ “centre” returns “cadd centre” strlen Syntax: (strlen ) This function returns the number of characters in the specified . Blank spaces are also considered as valid characters. e.g. Command: (strlen “AutoCAD Mechanical Desktop”)
returns 26
(strlen “cadd”)
returns 5
(strlen “ “)
returns 1
substr Syntax: (substr []) This function returns the substring of a . The argument specifies the first character of the string to be included the substring. The number of characters to be included in the substring is specified by . If is not specified then the function returns all characters including the following the specified character. e.g. Command: (substr “strength” 3 4)
returns “reng”
(substr “strength” 3)
returns “rength”
(substr “cadd centre” 3)
returns “dd centre”
(substr “cadd centre” 3 4)
returns “dd c”
(substr “cadd centre” 1)
returns “cadd centre”
(substr “cadd centre” 10)
returns “re”
wcmatch Syntax: (wcmatch ) This function enables the application to compare a to a wild-card pattern. If the string matches with the pattern then this function returns T else NIL. The wild-card patterns are similar to the regular expressions used by many application programs. The and the can be either a quoted string or a variable. This function compares the first 500 characters of the and specified. The can contain the wild-card matching characters shown in the following table: CHAR
DEFINITION
#
Matches any single numeric digit.
@
Matches any alphabetic character.
.
Matches any single non alphanumeric character
* Matches any character sequence including an empty string and can be used anywhere in the search pattern. ?
Matches any single character.
~
Matches any thing other than the next character.
[..]
Matches any one of the characters inside.
[~..]
Matches any one character except those enclosed.
-
Used in [ ] to specify a range of characters.
,
Separates any two patterns.
`
Escapes special characters.
The following example tests the string with the patterns. e.g. Command: (wcmatch “Name” “N*”)
returnsT
(wcmatch “Name” ???,~*m*,N*”)
returns T
(wcmatch “Name” “*”, *”)
returns NIL
read Syntax: (read< string>) This function returns the first atom or list in the specified . The cannot contain blank spaces except within list or string. e.g. Command: (read “end”)
returns end
(read “end of”)
returns end
In the above case from the string “end of” only end is returned as, a blank space is encountered after the first word.
8. CONVERSION FUNCTIONS The functions convert data types and units. AutoLISP also provides functions to handle decimal ASCII codes. atof Syntax: (atof - ) This function returns the conversion of a string to a real value. e.g. Command: (atof “3.4”) (atof “5’
returns 3.4 returns 5.0
atoi Syntax: (atoi - ) This function returns the conversion of a string to an integer. e.g. Command: (atoi “34”) (atoi “5.0”)
returns 34 returns 5
itoa Syntax: (itoa - ) This function returns the integer
- given as arguemnt into a string. e.g. Command: (itoa 34)
returns “34”
rtos Syntax: (rtos [mode>] [precision>]) This function converts the specified to a string according to the settings of and . The and arguments must be integers. The following are the argument values:
MODE
EDITING FORMAT
1. 2. 3. 4. 5.
Scientific Decimal Engineering (feet and decimal inches) Architectural (feet and fractional inches) Fractional
The denotes the number of decimal places. When and are not specified, the current strings of the AutoCAD system variables LUNITS and LUPRECA are used. e.g. Command: (rtos 3.4)
returns “3.4000”
(rtos 5 2 2)
returns “5.00”
(rtos 3.5 4)
returns “3 1/2”
(rtos 2.5 1 2)
returns “2.50 E+00”
ascii Syntax: (ascii ) This function returns the ascii code of the first character of the . e.g. Command: (ascii “A”)
returns 65
(ascii “CADD”
returns 67 (ASCII coee of “C”
angtos Syntax: (angtos [[ ]]) This function is used to convert the specified to a string depending on the settings of and . The arguments and must be integer values. The following are the argument values: MODE
EDITING FORMAT
0 1 2
Degrees Degrees/minutes/seconds Grads
3 4
Radians Surveyor’s units
The denotes the number of decimal places. When and are not specified, the current settings of the AutoCAD system variables AUNITS and AUPREC are used.
e.g. Command: (setq a (angle (` 2 4 ) ` (2 2) ) ) (angtos a 0 0)
returns “270”
(angtos a 1 0)
returns “270d”
(angtos a 0 4)
returns “270.0000”
(angtos a 3 3)
returns “4.712r”
chr Syntax: (chr ) This function returns the ASCII characters corresponding to the integer given as argument. The value returned is a string. e.g. Command: (chr 65) (chr 97)
returns “A” returns “a”
rem Syntax: (ren by . If there are more than two arguments then the remainder of FIRST and SECOND is divided by the THIRD and the value will be returned. e.g. Command: (rem 5 4) (rem 5 4.0)
returns 1 returns 1.0
(rem 20 7 4) i.e.,
returns 2
(rem 20 7 4) is (rem 20 7 4)
fix Syntax: (fix ) This function returns the truncated value of the as an integer after discarding the decimals. The arguments may be signed real or integer values. e.g. Command: (fix 8)
returns 8
(fix 1.32)
returns 1
(fix 2.895)
returns 2
(fix -5.85)
returns 5
float Syntax: (float ) This function returns the conversion of the into a real. the may be signed real or integer value. e.g. Command: (float 2)
returns 2.0
(float 8.959)
returns 8.959
(float -5)
returns 5.0
1+ Syntax: (1+ ) This function returns the increased by 1. The may be a real or an integer value. e.g. Command: (1+ 5) (1+ 6.75)
returns 6 returns 7.75
1Syntax: (1+ ) This function returns the increased by 1. The may be a real or an integer value. e.g. Command: (1- 5) (1- 6.75)
returns 4 returns 5.75
quote Syntax: (quote ) 1+ Syntax: (1+ ) This function returns the as a symbol without evaluating it. The function can also be represented by a “ ‘ “ e.g. Command: (quote A)
returns A
`(1 2)
returns (1 2)
Adding Commands If a function is defined with a name in the form C:XXX, it can be issued at the Command prompt just like a built-in AutoCAD command. You can use this feature to add new commands to AutoCAD or to redefine existing commands. To use the functions as AutoCAD commands the function name must use the form C:XXX when XXX is the function name which must be defined without arguments. A function defined with C:XXX can also be invoked transparently preceded by a single quote (`) from which any prompt of built-in AutoCAD command provided the function does not call the command function. for e.g. Uisng the C:XXX feature you can define a command which displays a simple message. Command: (defun C:Hello( ) prompt“Hello World”) (princ) ) returns
C: Hello Hello is now defined as a command. To execute the command Hello, specify the command name at the command prompt area as follows: Command: Hello When executed AtuoCAD displays the following message at the command prompt area. Hello World This command can be issued transparently because it does not call command function. Command: LINE From point: Hello Hello World From point: e.g. 2 Command : (defun box (sp L w / ap1 sp2 sp3) (setp sp1 (list (+ (car sp) L) (cadr sp) ) sp2 (list (car sp1) (+ (cadr sp1 w) ) sp3 (list (car sp) (cadr sp2) ) ) (command “pline: sp sp1 sp2 sp3 “c”) ) In the above function the “sp, L,w” are arguments and “sp1, sp2, sp3” are local variables for the function. These variables lose their values after the function is executed. Local Symbols in Functions AutoLISP allows you to define a list of symbols that are variable only to that function and these are known as local symbols. The use of local symbols ensures that the variables in your functions are unaffected by the surrounding application and that your variables are not available after the function has completed the task. The following example shows the use of local symbols in an user-defined function: Command: (Defun sample (/ a b) (Setq a “CADD” b “CENTRE”) (Princ (strcat “\n a has the value:” a)) (Princ (strcat “\n b has the value: “b”))
(princ) ) Before you test this new function assign variables “a” and “b” to values other than those used in the SAMPLE function. Command: (Setq a 1 b 2) You can verify that the variables “a” and “b” are actually set to those values by using the exclamation point( ! ). Command: !a 1 Command: !b 2 List Syntax: (list expr2>...) The list function combines a number of expressions into a list. This returns the ‘s as independent elements within parentheses. The elements can be of any type, the number of elements are not limited and this function is frequently used to define a 2D or 3D point variable (a list of two or three real). e.g. Command:
(list 5 10.0)
returns (5 10.0)
(list “A” “B”)
returns (“A” “B”)
(list 5 “A”)
returns ( 5 “A”)
(list `A `B)
returns (A B)
(list (list 5 10) )
returns ((5 10))
(setq A 2 B 5) (list A B)
returns (2 5)
(list (+ A 4) B)
returns (6 5)
The list function may be a nested list, which contains lists as members. e.g. Command:
(setq K (list 1 2 3) )
returns (1 2 3)
(setq L (list“C” “A” “D” “D”)
returns (“C” “A” “D” “D”)
(setq K L
returns ((1 2 3) (“C” “A” “D” “D”))
Polar Syntax: (polar ) This function locates a 3D point with respect to the given at a given and . The must be specified in RADIANS. Though the can be a 3D point, is always with respect to the current construction plane. The point is a list of two real. e.g. Command: (polar `(2 2) 0 3)
returns (5.0 2.0)
The new point is fixed at an angle 0, at a distance of 3 units from the point 2,2. Command: (polar (`2 2) 0.785398 1.414)
returns (3.0 3.0)
The new point is located at an angle of 45 degrees (denoted as 0.785398 in radian measure) at a distance of 1.414 from 2,2. angle Syntax: (angle ) This function returns the angle between the two given points in RADIANS. The angle is measured from X axis of current construction plane and if a 3D point is supplied then they are project onto the current construction plane. e.g. Command:
(angle `(2 2) `(3 3))
returns 0.785398
(corresponds to 45 degrees) (angle `(2 2) `(3 4))
returns 1.5707
(corresponds to 90 degrees) (angle `(2 2) `(4 2))
returns 0.0
(corresponds of 0 degrees) distance Syntax: (distance ) This function returns the numeric distance between the two given points and . The points can be list of a 2 or 3 real numbers or integers.
e.g. Command:
(setq P1 `(2 2) P2 `(2 4)) (distance P1 P2)
returns 2.0
(setq P1 `(2 0 0) P2 `(0 0 2 )) (distance P1 P2)
returns 2.82843
FILE HANDLING Every engineering design process, irrespective of the trade, whether Mechanical, Civil or Architectural, needs data. These data are standards and thumb rules for an optimum design solution. In conventional design process, an engineer has to repeatedly refer volumes of catalogs, tables and charts which is a tedious process. In the modern world, where computers are used to aid in the design process, one need not look the design data table. All the necessary data can be stored as data files and the design program can be made to open and read the files to locate the data. Data files in the present scenario are simple ASCII files generated either manually or through some other means AutoLISP provides powerful yet simple functions to search and access the data files, read and write/append information on to the data file. A file can be opened and read/modified anywhere in a program. When you open a file, AutoLISP provides a pointer which has to be assigned to a variable for further reference. The functions included in this chapter are as listed below. The functions included in this chapter are,
findfile
open
close
read-line
write-line
read-char
write-char
getfiled
findfile
Searches the AutoCAD library path for the specified file findfile ) Returns: