Showing posts with label AutoLISP. Show all posts
Showing posts with label AutoLISP. Show all posts

Friday, September 5, 2014

Conversion Functions


For instance. We are working with an integer. And now we want to convert that integer into a string. We need a conversion function.


AutoLISP has got eleven conversion functions. Here they are:


(angtos <angle> [<mode> [<precision>]])


This function converts an angle into a string. The angle is a real number in radians. The result depends on the mode and the precision.


Mode Description


0 Degrees
1 Degrees/minutes/seconds
2 Degrees (400)
3 Radians
4 Land units


The precision argument give the number of decimals. The mode and precision argument are in the system variables AUUNITS and AUPREC.


Examples:


(setq p1 (list 500 133))
(setq p2 (list 240 133))
(setq an (angle p1 p2))


(angtos an 0 0) "180"
(angtos an 0 4) "180.0000"
(angtos an 1 4) "180d0'0"
(angtos an 3 4) "3.1416r"
(angtos an 4 2) "W"


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


Now a real number is converted into a text. The units and precision depend on the arguments for mode and precision.


Mode Description


1 Mathematical
2 Decimal
3 Feet and decimal inches
4 Feet and fractional inches
5 Divisions


The mode and the precision arguments can be found in the system variables LUNITS and LUPREC.


Examples:


(rtos 17.5 1 4) "1.7500E+01"
(rtos 17.5 2 2) "17.50"
(rtos 17.5 3 2) "1'-5.50"
(rtos 17.5 4 2) "1'-5 1/2"
(rtos 17.5 5 2) "17 1/2"


(itoa <integer>)


Now an integer is converted into a text.


Examples:


(itoa 33) "33"
(itoa -17) "-17"


(atoi <text>)


A text is converted into an integer.


Examples:


(atoi "97") 97
(atoi "-3") -3
(atoi "3.9") 3


(atof <text>)


A text is converted into a real number.


Examples:


(atof "97.1") 97.1
(atof "-3") -3.0


(fix <number>)


This function translates a number into an integer.


Examples:


(fix 3) 3
(fix 3.7) 3


(float <number>)


Here a number is translated into a real number.


Examples:


(float 3) 3.0
(float 3.75) 3.75


(list <expression>)


A list is created with a number of loose expressions. It is often used to create a 2D point or 3D point.


(list 'a 'b 'c) (A B C)
(list 'a '(b c) "text") (A (B C) "text")
(list 3.9 6.7) (3.9 6.7)


(ascii "text")


The ASCII value of the first character of the text is given.


(ascii "a") 97
(ascii "A") 66
(ascii "all") 97


(chr <number>)


The number is an ASCII value. The corresponding character is givne back.


Examples:


(chr 65) "A"
(chr 66) "B"
(chr 97) "a"


(read <text>)


This function gives back what has been read. The text cannot contain spaces. What is after the space is ignored.


(read "LISP") LISP
(read "A") A


Exercise:


Create an AutoLISP program, that creates the following parking spot. Do you see? The sizes for the length, width, and angle are given.








This is how the AutoLISP program works:


Command: PRKSP
Width/Length/Angle/<starting point>:W
Width <default>: width is entered
Width/Length/Angle/<starting point>:L
Length <default>: length is entered
Width/Length/Angle/<starting point>:A
Angle <default>: angle is entered
Number of parking spots <default>: number is entered


Take care. The AutoLISP program is going to be very big. So make separate functions for entering the width, length, and angle.


Exercise


Let's write an AutoLISP program. This is the drawing that is created by the AutoLISP program.




Two points are picked. The circle is drawn in one point. And as line goes to the other point, starting in the first point.


The user enters what text is to be drawn in the circle. And a box is drawn around the text. The TEXTBOX function is used.








Saturday, August 23, 2014

Graphical Screen Functions


These functions can be used to make changes to the graphical screen. The graphical screen is the drawing area of AutoCAD.


(grclear)


This function is used to clean th active view port of the drawing area. The information is not erased, but made invisible.


If the REDRAW command is used, then the information is visible again.


(grdraw <p1> <p2> <color> [<highlight>])


This function is used to draw a line between the points p1 and p2. The points can be 2D points and 3D points.


The color argument sets the color of the line. If the color argument is -1, then the line is drawn in the color in a complimentary color.


If another vector is drawn over the same points with the color, then the complete line is made invisible.


If the highlight argument is present and unequal to zero, then the line is drawn in a highlighted format.


(grtext [<box> <text> [<higlight>])


With this function you can write text to the screen menu of AutoCAD. I'm afraid screen menus are no longer used. So forget it.


(grread [<track>])


With this function you can read input from devices such as keyboard, digitizer, mouse, etc. But most of the time GET functions are used.


If a mouse is used, then this is what could be given back by the GRREAD fucntion:


(3 (-13.9732 13.0249 0.000000))


The first element is the the code of the device. These are the codes that are available:


2 Keyboard and the ASCII code of the character.


3 Mouse. The point is put in a list.


4 Screen menu cell with the number of the cell.


5 A list of the point for the drag mode. Only if the TRACK argument is unequal to nil.


6 Button number of the mouse.


7 Selected item in TABLET 1 menu.


8 Selected item in TABLET 2 menu.


9 Selected item in TABLET 3 menu.


10 Selected item in TABLET 4 menu.


11 Selected item in AUX 1 menu.


12 Point of the mouse.


13 Selected screen menu item.


(menucmd <text>)


This is another function that works with the screen menu. We are not using it. So you can forget about it.


Exercise


Now we are going to create an AutoLISP program, that draws circles and lines. The center points of the circles are picked.


To create the AutoLISP program the WHILE function is used. Here is the drawing that is created. Four circles have been drawn. But more circles could be drawn.




Sunday, August 17, 2014

Graphical Screen Functions




These functions can be used to make changes to the graphical screen. The graphical screen is the drawing area of AutoCAD.


(grclear)


This function is used to clean th active view port of the drawing area. The information is not erased, but made invisible.


If the REDRAW command is used, then the information is visible again.


(grdraw <p1> <p2> <color> [<highlight>])


This function is used to draw a line between the points p1 and p2. The points can be 2D points and 3D points.


The color argument sets the color of the line. If the color argument is -1, then the line is drawn in the color in a complimentary color.


If another vector is drawn over the same points with the color, then the complete line is made invisible.


If the highlight argument is present and unequal to zero, then the line is drawn in a highlighted format.


(grtext [<box> <text> [<higlight>])


With this function you can write text to the screen menu of AutoCAD. I'm afraid screen menus are no longer used. So forget it.


(grread [<track>])


With this function you can read input from devices such as keyboard, digitizer, mouse, etc. But most of the time GET functions are used.


If a mouse is used, then this is what could be given back by the GRREAD fucntion:


(3 (-13.9732 13.0249 0.000000))


The first element is the the code of the device. These are the codes that are available:


2 Keyboard and the ASCII code of the character.


3 Mouse. The point is put in a list.


4 Screen menu cell with the number of the cell.


5 A list of the point for the drag mode. Only if the TRACK argument is unequal to nil.


6 Button number of the mouse.


7 Selected item in TABLET 1 menu.


8 Selected item in TABLET 2 menu.


9 Selected item in TABLET 3 menu.


10 Selected item in TABLET 4 menu.


11 Selected item in AUX 1 menu.


12 Point of the mouse.


13 Selected screen menu item.


(menucmd <text>)


This is another function that works with the screen menu. We are not using it. So you can forget about it.


Exercise


Now we are going to create an AutoLISP program, that draws circles and lines. The center points of the circles are picked.


To create the AutoLISP program the WHILE function is used. Here is the drawing that is created. Four circles have been drawn. But more circles could be drawn.




Friday, August 8, 2014

Text Screen Functions

Text Screen Functions


Hari Raya


Sorry. I could not add a new post to my blog for two weeks. It was not my fault. It has to do
with Hari Raya.


I live in Malaysia. And you find a lot of Muslims here. They celebrate Ramadan every year. Ramadan is a month of fasting.


When Ramadan is over, then we have Hari Raya. It lasts a couple of days, when everybody has a national holiday.


But the majority of the people take a holiday. And if they have a shop, they close it. Even Chinese shops and Indian shops are closed.


I do my Internet in an Internet cafe. And my Internet cafe was closed too. So I could not add a new post to my blog.


Don't worry. I keep on adding new posts to my blog. I didn't give up. I still have a lot that I want to say.


Text Screen Functions


In AutoLISP we have the following functions that can be used for giving an AutoLISP program a presentation. They are:


- Text screen functions
- Graphical screen functions
- Text functions
- Conversion functions


Now we are going to talk about the text screen functions. Later we are going to talk about the other functions.


(graphscr)
(textscr)


You can switch from the text screen to the graphic screen with the (graphscr) functions and go to the text screen with the (textscr) function.


System Variables


AutoCAD works with system variables. You can check the value of a system variable with the following function:


(getvar <variabele name>)


Examples:


(getvar “dwgname”) TEST
(getvar “cmdecho”) 1


Some system variables are read only. You cannot change their value. But if you can, you can change the value with:


(setvar <variable name> <value>)


Examples:


(setvar “filletrad” 200) 200
(setvar “cmdecho” 0) 0


It is advisable to set the system variable to its original value after the value has been changed. This is how it can be done:


(setq ce (getvar “cmdecho”)
(setvar “cmdecho” 0)
(setvar “cmdecho” ce)


Printing Data


(princ <expression> [<file description>])


This function writes the expression to the text screen and gives as a result the expression. The expression can be a text or a number.


The file description argument is optional and is used if the expression is written to an external file. We will talk about it later.


Examples:


(setq a 123)
(setq b '(a))


(princ 'a) A
(princ a) 123
(princ b) (A)
(princ “Hello”) “Hello”


If the expression is a text, then control codes can be added to it. The following control codes are available.


\\ the \ character
\e escape
\n new line
\r return
\t tab
\nnn the character with the octal code nnn


The PRINC function can also be used without an argument. Then an empty string is written to the screen. That is often at the end of a program.


Example:


(defun c:setng ()
(setvar “lunits” 4)
(setvar “blipmode” 0)
(princ)
)


After the AutoLISP program has been loaded into AutoCAD it is started by typing its name at the command prompt.


At the end of the AutoLISP program the command prompt is shown again without any additional information. There is no 0.


(prin1 <expression> [<file description>])


This function is the same as the previous function, but control codes are not evaluated.


Examples:


(setq a 123)
(setq b '(a))


(princ 'a) A
(princ a) 123
(princ b) (A)
(princ “Hello”) “Hello”


(print <expression> <file description>)


This function is the same as the previous function. But the expression is print on a new line and after the expression is a tab.


(prompt <text>)


Now you can only print a text to the text screen. The function prints the text and gives back nil.


Example:


(prompt “New value”) New value


Exercise


For this exercisxe the REPEAT function is used. Write an AutoLISP program, that draws five circles with the number of the circle in it.


This is the drawing that is created by the AutoLISP program.



Saturday, July 12, 2014

Repetition Functions

Repetition Functions


Now we come to the repetition functions. They are very important. Repetition functions are often used in AutoLISP programs.


Here they are:


WHILE Function


(while (<test> <expression> …)


This function performs a test at the beginning. If the test gives nil, then the expressions are evaluated.


Example:


(defun c:numbr (/ nr)
(setq nr 1)
(while (<= nr 5)
(princ "\nNumber: ")
(princ nr)
(setq nr (1+ nr))
)
(princ)
)
(c:numbr)


At the start of the AutoLISP program the variable NR is given the value 1. The value of the variable is tested at the beginning of the WHILE function.


As long as the value is less or equal to five, then the expressions in the WHILE function are evaluated.


This is given back by the AutoLISP program:


Number: 1
Number: 2
Number: 3
Number: 4
Number: 5


Repeat Function


(repeat <number> <expression> …)


Now the number of repetitions can be specified.


Example:


(princ "\n")
(defun c:repfc ()
(repeat 4
(princ "-")
)
(princ)
)
(c:repfc)


This is given back by the AutoLISP program:


----


Foreach Function


(foreach <name> <list> <expression> …)


This function goes through a list and gives every element of the list a name. The expression is performed for every name.


Example:


(foreach n (list 1 2 3) (* n 2))


The AutoLISP line gives back 6. The number 3 is multiplied with 2.


Exercise:


We have been talking about predicates, test functions and repetition functions. Here is an exercise to use what has been taught.


Write the SLINE AutoLISP program. The following prompts will show up:


Command: SLINE
Color/Linetype/<first point>: C
Color: 1
Color/Linetype/<first point>: L
Linetype: Hidden
Color/Linetype/<first point>: 100,100
Close/<next point>: 300,300
Close/<next point>: C
Command:












Sunday, June 29, 2014

Test Functions

Test Functions


AutoLISP has got two text functions. They are:


- IF function
- COND function


IF Function


(if <expression> <then> <else>)


The expression is evaluated. If it is true, then the THEN part is performed. If not, then the ELSE part is performed.


Examples:


Function: Gives:


(if (= 1 3) "Yes" "No") "No"
(if (= 2 (+ 1 1) "Yes") "Yes"
(if (= 2 (+ 3 4) "Yes") nil


The THEN part or the ELSE part of the IF function can only contain one expression. If more there are more expressions, then PROGN is used.


Example:


(if (= a b)
(progn
(setq a (+ a 10))
(setq b (+ b 10))
)
(progn
(setq a (- a 10))
(setq b (- b 10))
)
)


Here is another example of how the IF function can be used. Now we have an AutoLISP program. A line is drawn and a circle and a text.


(defun c:ball1 (/ p1 p2 an tx aw ht)
(setq p1 (getpoint "\nFirst point:")
p2 (getpoint p1 "\nSecond point:")
an (angle p1 p2)
)
(initget 1 "Yes" No")
(setq aw (getkword "Text (Yes/No)? "))
(if (= aw "Yes")
(setq ht (getdist "\nHeight: ")
tx (getstring "\nText: ")
)
(setq ht (getdist p1 "\nRadius: "))
)
(command "line" (polar p1 an ht) p2 ""
"circle" p1 ht
)
(if (= aw "Yes")
(command "text" "m" p1 ht 0 tx)
)
)


COND Function


(cond (<text1> <result1> …)


This function is used to perform an action depending on the value of a variable. Here is an AutoLISP program that shows it.


If is the program from before. But now you can draw a circle, a square, or a triangle. You choose in the beginning.


(defun c:ball2 (/ p1 p2 an ht tx aw)
(setq p1 (getpoint "\nFirst point: ")
p2 (getpoint p1 "\nSecond point: ")
an (angle p1 p2)
ht (getdist "\nHeight text: ")
tx (getstring "\nText: ")
)
(initget "Cir Tri Quadr")
(setq aw (getkword "\nTriangle/Quadrant/
<Circle>: ")
)
(command "line" (polar p1 an ht) p2 "")
(cond
((= aw "Tri")
(command "polygon" 3
p1
"c"
(polar p1 an ht)
)
)
((= aw "Quadr")
(command "polygon" 4
p1
"c"
ht
)
)
(T
(command "circle" p1 ht)
)
)
(command "text" "m" p1 ht 0 txt)
)


Friday, June 20, 2014

Predicates Or Boolean Expressions

Predicates Or Boolean Expressions


We have done parametric drawing. Now we are going to talk about predicates or boolean expressions. AutoLISP has got a lot of them.


A predicate gives back true or lase. True is presented as T and false or untrue is presented as nil.


Predicates


(= <atom> <atom> …)


This function checks if two or more texts and numbers are equal. If so T is given back. Otherwise nil is given back.


Examples:


(= 4 4.0) T
(= 20 300) nil
(= 2.4 2.4 2.4) T
(= 400 500 400) nil
(= “I” “I”) T


(/= <atom> <atom>)


This fiction is the same as the one from before. Except. T is given back if untrue and nil is given back if true.


Examples:


(/= 10 20) T
(/= “You” “You”) nil
(/= 5.34 5.44) T

(< <atom> <atom>)


This function works on numbers and texts. T is given back if the first atom is smaller than the second atom.


Examples:


(< 10 20) T
(< “b” “c”) T
(< 357 2.4) nil
(< 2.3 88) T
(< 2.3 4.4) T


(<= <atom> <atom>)


Now there is checked if both atoms are equal or the first atom is smaller than the second atom.


Examples:


(<= 10 20) T
(<= “b” “b”) T
(<= 357 33.2) nil
(<= 2.9 9) T


(> <atom> <atom>)


Now there is checked if the first atom is more than the second atom. As before. The atoms can be numbers and texts.


Examples:


(> 120 17) T
(> “c” “b”) T
(> 3.5 1792) nil
(> 77.4 4.2) T
(> 77.4 4) T


(>= <atom> <atom>)


The same as before. Except now there is checked if the atoms are equal or the first atom is more than the second atom.


Examples:


(>= 120 17) T
(>= “c” “c”) T
(>= 3.5 1792) nil
(>= 77 4 4.0) T
(>= 77 4 9) nil


(equal <expression1> <expression2> <fuzz>)


The expressions are checked. Are they equal or not. If working with numbers, you want to introduce a fuzz.


Even if numbers are supposed to be equal, there can be a little difference between them. The difference is specified in the fuzz.


Examples:


(setq f1 '(a b c))
(setq f2 '(a b c))
(setq f3 f2)


(equal f1 f3) T
(equal f3 f2) T


(setq a 1.23456)
(setq b 1.23457)


(equal a b 0.00001) T


(eq <expression1> <expression2>)


This function is used to check if two lists are equal. Are they bound to the same object? If so T is given back.


Examples:


(setq f1 '(a b c))
(setq f2 '(a b c))
(setq f3 f2)


(eq f1 f3) nil
(eq f1 f3) T


(atom <item>)


This function gives back T if the item is an atom and not a list. If it is a list, then nil is given back.


Examples:


(setq a '(x y z))
(setq b 'a)


(atom 'a) T
(atom a) nil
(atom 'b) T
(atom b T
(atom '(a b c)) nil


(listp <item>)


You want to check if the item is a list. Use this function for doing that. It gives back T if it is an item.


Examples:


(listp '(a b c)) T
(listp 'a) nil
(listp 4.343) nil


(boundp <item>)


Use this fucntion to see whether the item has got a value. The value can be a number or a text.


Examples:


(setq a 2)
(setq b nil)


(boundp 'a) T
(boundp 'b) nil


(numberp <item>)


This function checks if the item is an integer number or an real number.


Examples:


(numberp 4) T
(numberp 3.824) T
(numberp “Hallo”) nil
(numberp (setq a 10)) T


(minusp <item>)


Is the item a negative integer number or a negative real number?


Examples:


(minusp -1) T
(minusp (- 1 4)) T
(minusp 830.3) nil


(zerop <item>)


Is the value of the item zero?


Examples:


(zerop 0) T
(zerop (- 4 4.0)) T
(zerop 0.00001) nil


(and <expression> …)


This function checks if all expressions have a value of that is not nil. If one value is nil, then it gives back nil.


Examples:


(setq a 100)
(setq b nil)
(setq c “text”)


(and 1.4 a c) T
(and 1.4 a b c) nil


(or <expression> …)


This function checks if at least one expression is unequal to nil. If all expressions are equal to nil, the it gives back nil.


Examples:


(setq a 100)
(setq b nil)
(setq c “text”)


(or nil a b c) T
(or nil b '()) nil




(not <item>)


The result of this function is T is the value of the item is nil. Otherwise it is is nil.


Examples:


(null <item>)


Now there is checked if the value of the item is nil. If so then T is given back.


Examples:


(setq a 123)
(setq b nil)
(setq c “text”)


(null a) nil
(null b) T
(null c) nil
(null '()) T


(type <item>)


Here the type of the item is found. These are the types that are known in AutoLISP.


REAL real number
FILE file descriptor
STR text
INT integer number
SYM symbol
LIST list and user function
SUBSR internal AutoLISP function
PICKSET selection set
ENAME entity name
PAGETB function page table


Examples:


(setq a 123)
(setq r 3.5)
(setq t “text”)
(setq l '(a b c))


(type 'a) SYM
(type a) INT
(type r) REAL
(type t) STR
(type l) LIST
(type *) SUBR




Saturday, June 14, 2014

Parametric Drawing


We can create AutoCAD drawings using AutoLISP. Here is an AutoLISP program that does the job. The name of the program is NEWDR.LSP.


The AutoLISP Program


(defun c:newdr (/ p1 p2 p3 p4 p5 p6 p7 sd ts ws)
(setvar “cmdecho” 0)
(setq p1 (getpoint “\nPosition door: “)
sd (getdist “\nStructural dimension: “)
ws (getdist “\nWidth style: “)
ts (getdist “\nThickness style: “)
p2 (polar p1 0 ts)
p3 (polar p2 (/ pi 2) ws)
p4 (polar p3 pi ts)
p5 (polar p2 0 sd)
p6 (polar p3 0 sd)
p7 (polar p3 (/ pi 2) sd)
)
(command “pline” p1 “w” 0 0 p2 p3 p4 “c”)
(command “copy” “l” “” p1 p5 “”)
(command “pline” p2 p5 “”)
(command “copy” “l” “” p2 p3 “”)
(command “pline” p3 p7 “”)
(command “arc” p6 “c” p3 p7)
(command “zoom” “a”)
(command “zoom” “0.8x”)
(setvar “cmdecho” 1) (princ)
)
(c:newdr)


Using the program a door will be drawn. See how the door looks. As you can see. The door has different sizes and a position.



Copy The AutoLISP Program


That is so good about the listing of an AutoLISP program. You can save it on your hard disk and use it again. Copy the program to a text file.


That is important. When the listing of an AutoLISP program is saved, it is saved with the extension LSP. It can be loaded into your CAD program.


Loading The AutoLISP Program


Let's talk about how to load an AutoLISP program into the AutoCAD program. That must be done first before you can use the AutoLISP program.


I will talk about loading the AutoLISP program into IntelliCAD. Loading it into AutoCAD is done in the same way. So don't worry.


You are in IntelliCAD. Click on Tools in the menu. In the pop-up menu click on Load Application. The Load Application files dialog box is displayed.


In the dialog box click on the Add File button. You can go to different folders and you can select the AutoLISP file that you want to load.


Click on the file. And click on the Open button. You come back into the Load Application file dialog box. Click in the Load button.


The AutoLISP file is now loaded into IntelliCAD. And you can start it by typing its name at the prompt.


Starting Automatically


Before I talk about the AutoLISP program, I want to say something about what has been added to the program. At the end you see:


(c:newdr)


Through that line the AutoLISP starts as it has been loaded into IntelliCAD. As you can see. The name of the AutoLISP program is in the line.


Functions


In the AutoLISP program we find functions. The first function we find it the DEFUN function. Here is the syntax of the DEFUN function:


(defun <symbol> <argument list> <expression> ...)


This how the DEFUN function is used for defining an AutoLISP function. SYMBOL is the name of the new AutoLISP function.


Arguments


In the argument list you find all the arguments that the new AutoLISP function needs to run properly. If not there, then an error occurs.


Local Variables


In the argument list you can find a slash. After the slash comes a list of variables. Those variables are local variables.


The local variables only have a value in the function. Outside the function they have no value. Or the value of them is nil.


If no argument list is given then the symbol of an empty list must be used. This is how that symbol looks like: ().


Variables that are not defined as local have a value outside the function. They have a value when the function is no longer running.


Here are some examples:


(defun funct (x y) – the function has two arguments


(defun funct (/ a b) – the function has two local variables


(defun funct (z / a) – the function has no argument and one local variable


(defun funct () - the function has no arguments



In the AutoLISP function the GETPOINT and GETDIST functions are used. To get information. In a separate chapter we talk about the functions.


As you can see. AutoCAD commands were used in the AutoLISP program. I will talk about using AutoCAD commands in another report.


Exercise


You now have seen how an AutoLISP program is created. You have seen how distances can be entered and how AutoCAD commands are used.



Go ahead. Write your own program. This time the program is going to draw a house. Enter the sizes of the house.

Friday, June 6, 2014

Arithmetic And Geometric Functions


Arithmetic Functions


That is important. In an arithmetic function the mathematical symbol always comes before the arguments.


(+ <number> <number> ...)


The plus sign is a mathematical symbol.


Examples:


Function Gives back


(+ 1 2) 3
(+ 12 13) 25
(+ 1 2 3 4 5) 15


(- <number> <number> ...)


Examples:


Function Gives back


(- 50 40) 10
(- 40 30 5 0) 5
(- 10 20) -10


(* <number> <number> ...)


Examples:


Function Gives back



(* 2 3) 6
(* 12 15 2) 360
(* 3 -4.5) -13.5


(/ <number> <number> ...)


Function Gives back



(/ 100 2) 50
(/ 100 2.0) 50.0
(/ 100 20 2) 5


(1+ <number>)


Example:



Function Gives back



(1+ 5) 6


(1- <number>)


Example:



Function Gives back



(1- 10) 9


(abs <number>)


This function gives the absolute value of the number. The number can be a real number of an integer.


Examples:


Function Gives back


(abs 100) 100
(abs -97.25) 97.25


(max <number> <number> ...)


This function gives back the number that is the maximum of all the numbers. The number can be a real number or an integer.


Examples:


Function Gives back


(max 12 20) 20
(max 5,5 7) 7


(min <number> <number> ...)


This function is the opposite of the previous function. Now the minimum number is given back. The number can be a real number or an integer.


Examples:


Function Gives back


(min 4.07 -144) -144
(min 88.5 19 5) 5


(gcd <number> <number>)


Now we are going to do a complicated calculation.With this function the greatest common divisor is found. The numbers are integers.


Examples:


Function Gives back



(gcd 81 57) 3
(gcd 12 20) 4


(rem <number> <number> ...)


This function calculates how much remains after a division. The numbers can be integers and real numbers.


Examples:



Function Gives back



(rem 42 12) 6
(rem 9 4) 1



(exp <number>)


This function raises e to the mach of the number. The number can be an integer as a real number. The result is always positive.


Examples:


Function Gives back



(exp 1) 2.71828
(exp 2.2) 9.02501
(exp -0.4) 0.67032


(expt <base> <exponent>)


Now the base number is raised to the mach of the exponent number. The base and the exponent can be integers and real numbers.


Examples:


Function Gives back



(expt 2 4) 16
(expt 3.0 2) 9.0


(sqrt <number>)


This function calculates the square root of the number. The number can be an integer as well as a real number.


Examples:


Function Gives back



(sqrt 9) 3
(sqrt 2.0) 1.41321


(log <number>)


This function gives as result the natural logarithm of the number. The number can be an integer as well as a real number.


Examples:


Function Gives back



(log 4.5) 1.50408
(log 1.22) 0.198851


pi


This is no function but a constant number. The value of the number is 3.1415926.


Geometric Functions


(cos <angle>)


This function calculates the cosinus of an angle. The angle is expressed in radians.


Examples:



Function Gives back



(cos 0) -1
(cos pi) 1



(sin <angle>)


The same as before. Except this time the sinus of an angle is calculated. The angle is expressed in radians.


Examples:


Function Gives back



(sin 1.0) 0.841471
(sin 0,0) 0.0


(atan <number> [<number>])


If the second number is not present, then the function gives back the tangent of the first number in radians.


The number of the angle can be between pi and -pi,


Examples:


Function Gives back



(atan 0.5) 0.463648
(atan 1.0) 0.785398
(atan -1.0) -0.785398


If the second number is present, then the function gives back the inverse tangent of the quotient of the two numbers.


Examples:



Function Gives back



(atan 2.0 3.0) 0.588003
(atan -2.0 3.0) -0.68003
(atan -2.0 -3.0) -2.55359
(atan 1.0 0.0) 1.570796
(atan -0.5 0.0) -1.570796


(angle <point1> <point2>)


The function calculates the angle of the straight line going from point1 to point2. It calculates the angle between the line and the active UCS.


UCS stands for User Coordinate System. And the angles are measured counter clockwise. And the angle is given in radians.


Examples:


Function Gives back



(angle '(1.0 1.0) '(1.0 4.0)) 1.5708
(angle '(5.0 1.33) '(2.4 1.33)) 3.14159


(distance <point1> <point2>)


The function gives the distance between the two points. The distance is given in screen units.


If the value of the FLATLAND system variable is unequal to zero, then 2D points are expected. If a 3D point is used, then the Z value is ignored.


Examples:
Function Gives back



(distance '(1.0 2.5 3.0)
'(7.7 2.5 3.0)
) 6.7
(distance '(1.0 5.0)
'(1.0 15.0)
) 10.0


(inters <point1> <point2> <point3> <point4> [<on>])


The function calculates where the lines between point 1 and point 2 and point 3 and point 4 cross. A point is given back.


When the optional argument ON is present and nil. Then the two lines are considered to be infinite.


Examples:


(setq p1 (list 1.0 1.0)
p2 (list 9.0 9.0)
p3 (list 4.0 1.0)
p4 (list 4.0 2.0)
)


Function Gives back



(inters p1 p2 p3 p4) nil
(inters p1 p2 p3 p4 T) nil
(inters p1 p2 p3 p4 nil) (4.0 4.0)


(polar <point> <angle> <distance>)


This function gives as a result a point that is under an angle from the point that has been given and on the distance.


Example:


Function Gives back



(polar '(1.0 1.0) 0 4.0) (5.0 1.0)


(osnap <point> <mode>)


Depending on the value of the text of the mode a point is calculated. These are the modes there
are:


Mode Description


nea” Nearest snap
endp” Endpoint snap
midp” Midpoint snap
centre” Center snap
perp” Perpendicular snap
tan” Tangent snap
quad” Quadrant snap
int” Insertion point snap
pnt” Point Snap
int” Intersection snap



Example:


Function



(setq pt (osnap pt “nea”))


Exercises



1. Write the following arithmetic functions:


- 12 plus 13
- 8 times 5
- 200 divided by 4
- What is maximum of 12, 25 3 2, 94?
- What is the greatest common divisor of 256 and 326?
- What is the 8th power of 3?


2. Using geometric functions:


- What is the angle of a horizontal line? From left to right and from right to left.
- What is the distance between the points 10,20 and 40,50?
- What point is found starting in punt 10,20 and under an angel of 45 degrees and over a distance of 25?


Friday, May 23, 2014

Basic Concepts




Let's start with AutoLISP. It is used for writing programs for AutoCAD and CAD programs similar to AutoCAD. IntelliCAD is such a program.


With AutoLISP you can write programs that perform tasks and calculations automatically.


AutoLISP is a dialect of the programming language LISP. LISP has been developed by John McCarthy in the nineteen fifties.


LISP is the second higher programming language after FORTRAN. In the nineteen eighties LISP became really popular.


LISP was used for developing scientific systems, the so called Expert Systems, and systems for artificial intelligence.


Now we not only have LISP. There are also many dialects of LISP. Some dialects are InterLISP, MacLISP, ZetaLISP, CommonLISP, and SCHEME.


AutoLISP is also a dialect of LISP and it is based XLISP ODF David Bentz. AutoLISP looks very much like CommonLISP.


The name LISP comes from “LISt Programming” or LISt Processing”. It is also called “Lots of Insane Stupid Parenthesis”.


The Introduction Of AutoLISP



In AutoLISP you can write programs and functions that can be used in the AutoCAD program and CAD programs that are alternatives of AutoCAD.



Similar Programs


Autodesk started with the introduction of AutoLISP. They praised the:


- Flexibility
- Simplicity
- Interpretation


AutoLISP is flexible, because you define functions and programs in many ways. And variables can have many values.


AutoLISP is simple, because it is easy to learn. Of the higher programming languages AutoLISP is the easiest one to learn.


You can type an expression on the command prompt and right away you get to see the result. That is not complicated.


AutoLISP is being interpreted instead of being compiled. The advantage is that errors in the program can be found right away.


AutoLISP As A Tool


AutoLISP can be used as you are drawing in AutoCAD or an alternative CAD program. It can be used for making calculations. Or store data in memory.


Here is an example. Suppose you want to insert a block. And you want to insert it with a scale factor of three divided by seven.


The INSERT command is used. These are the prompts that show up at the command prompt:


- Command:
- Blockname:
- Insertion point:
- X scale factor:
- Y scale factor:
- Rotation angle:


We fill in the name of the block and we pick a point. That point is the insertion point. Now we must enter the scale factor.


We know the scale factor is three divided by seven. We could calculate what the value is and use the number.


The exact value is 0.4285714285714286. Maybe you round the value up to 0.43. But if you round it up, you do not get an exact value.


There is another way. It is the AutoLISP way. If you follow that way, you will use the exact value of the calculation.


At the prompt, where is asked for the X value, enter (/ 3.0 7.0). AutoLISP calculates the value and gives it back to the command.


Concepts


In AutoLISP there are the following concepts:


- Atoms
- Symbols
- Strings
- Real numbers
- Integers
- Subrs (built in functions)
- File descriptors
- AutoCAD entity names
- AutoCAD selection sets
- Lists


A symbol is a variable in which data is stored. Use a name that says something about the data that is stored in the symbol.


Example:


p1 - point
e1 - entity


A string is a text. In AutoLISP a string has got quotes. Here is an example of a string as is used in AutoLISP.


This is a string”


Real numbers have a value behind the point. Here are some real numbers:


- 3.24
- 14.9
- 15.0



An integer is a number without a part behind the point. Here are integers:


- 23
- 10


Subrs or built in functions are used by AutoLISP. They perform an action. Here are two examples:


+ - count
/ - divide


If you work with external files in AutoLISP, then
file descriptors are used. With the file descriptor the external file is specified.



Here is how a file descriptor may look:



<file #E814>



In an AutoCAD drawing you can find a lot of entities. Every line, circle, block, etc. is an entity.



These entities have a name. This is how the name of an entity may look:


<Entity name: 60000014>



In AutoCAD you may find selection sets. Selection sets are created by AutoLISP. They are named like this:
<Selection set:5>



These are the atoms of AutoLISP. But AutoLISP is also having lists. A list is a group of elements between brackets.


This is how a list may look:



- (A B C)
- (1 2 (3 4) 5)


A point is a list too. You can have 2D points and you can have 3D points. This is how they may look:



- 2D point: (10 20)
- 3D point: (10 20 30)


The numbers in the list for the point stand for the X value, the Y value, and the Z value. Only 3D points also have a Z value.


Functions


In AutoLISP there are a lot of functions. We have used a function as we were calculating a value for the INSERT command.


The function was (/ 3.0 7.0). Let's be more specific:


(/ 3.0 7.0) - list
/ - built in function
3.0, 7.0 - real numbers
/, 3.0, 7.0 - atoms


A call to a function is done with a list. You have seen what list has been used here. The first element is the symbol of the function.


The symbol of the function is the built in function. It is a dash. We have got a prefix notation. The symbol comes first.


You will find more functions in this book. They are all explained. I will not give you any more examples of functions.


Storing Data


We have used a function with the INSERT command. But the result of the function was not stored in the CAD program.


We can store data in the CAD program. The data is stored in a symbol. That is done with the SETQ function. Here is how it is done:


(setq vl 5)


The symbol VL gets the value five.


But if we use a function for a calculation, then the function gives back a value too. And we store that value in a symbol.


Here is what we can do with the function that we used with the INSERT command:


(setq dv (/ 3.0 7.0))


Now the value of three divided by seven is stored in the symbol DV.


Getting A Value


You have stored value in symbols. And now you want to know the values of the symbols. That is easy. Put an exclamation mark in front of the symbol.


We have used the SETQ function like this:



(setq vl 5)



To find the value that is stored in the symbol VL we enter “!vl” at the command prompt. And five is given back.



You could also enter at the command prompt:



(eval vl)


That function also gives back five.


Lexical Appointments


We were talking about AutoLISP. You saw the start of programming in AutoLISP. But there are some appointments to follow.


- For programing in AutoLISP lowercase characters and uppercase characters can be used. The use of lowercase characters is preferred.



- Symbol names can contain all characters except:


(
)
.
'

;


- The following characters end a symbol name:


(
)
'

;
(space)
(end of line)


- Symbol names and function names cannot start with a number.


- Texts are enclosed in quotes


- AutoLISP works with control codes. The following control codes are available:


\\ \ character
\e escape
\n new line
\r return
\t tab
\nnn character nnn


- You can place comments in an AutoLISP program by placing a semi colon in front of the colon. The AutoLISP program will ignore the comment.


- For symbols use a name of two characters. For functions use a name of five characters. To save computer memory.


Exercises


1. Is (1 4 “hallo”) a list?
2. Is '(1 2 3)' a list?
3. Is 21 a symbol?
4. How many elements has the list (1 45 (“this” “is”) “a text”)?
5. What is the integer of 3.1536?
6. What is the integer of 3.0?
7. Is point.1 a symbol?
8. Is point_1 as symbol?
9. What is the result of (setq ct 56 pt 23)
10. What character is placed in front of a symbol to get the value of the symbol?