Save time, and automate your tasks !!!
Lesson no. 4
- Some functions are used to put interaction inthe routine. We call them the GET functions, because they all start withthese three letters. When a routine meet a GET function, it stop the executionto let the user answer the question.
(SETQ ANSWER (GETXXX "\nWhat is the answer?"))
- Each function as a precise use:
FUNCTION UTILITY EXAMPLE GETINT an integer 4 GETREAL a real 4.0 GETPOINT a point a click in the drawing GETDIST a real 4'-0" (or two points) GETANGLE an angle (in radian) 3.1416 (or two points) GETCORNER a point a click in the drawing GETKWORD a key word Yes GETSTRING a string Michel
Examples:(SETQ age (GETINT "\nHow old are you ? : "))
(SETQ weight (GETREAL "\nHow much do you weight? : "))
(SETQ start (GETPOINT "\nStart point: "))
(SETQ height (GETDIST "\nWhat is the heightof the fence ? : "))
(SETQ name (GETSTRING "\nWhat is your name? : "))
Variable User answer Value kept in memory age 41 41 weight 71 71.0 start click 12,14,0 height 5'-8" 68.0 name Michel "Michel"
a small program,lets analyse the DEFUN function with two little examples.
- Instead of always type in the command prompt area , we will now typeour codes in a text editor (Notepad). To let you build
In the text editor, you will save the file with a new extension .LSP,then, in AutoCAD, you will have to put it into memory. We will explorelots of ways to do this, but for now, we'll type it in the command promptarea.
COMMAND: (LOAD "d:/myfolder/autolisp/star.lsp")
The LOAD function let you put the AutoLISP file into memory. We put,in double quote, the name of the file (star.lsp) with the full path (d:\\myfolder\\autolisp\\)
Like you can see, i used the \\ instead of \ ....because the \ isa reserved caracter.
Example 1 - New AutoCAD command
(DEFUN C:STAR () (SETVAR "CMDECHO" 0) (SETQ OSN (GETVAR "OSMODE")) (SETVAR "OSMODE" 0) (SETQ PT (GETPOINT "\nGivethe center of the star")) (SETQ RD (GETDIST "\nRadius? : ")) (SETQ PT1 (POLAR PT 1.5707RD)) (SETQ PT2 (POLAR PT 2.8274RD)) (SETQ PT3 (POLAR PT 4.0841RD)) (SETQ PT4 (POLAR PT 5.3407RD)) (SETQ PT5 (POLAR PT 0.3142RD)) (SETQ PT1A (INTERS PT1 PT3PT2 PT5)) (SETQ PT2A (INTERS PT2 PT4PT1 PT3)) (SETQ PT3A (INTERS PT3 PT5PT2 PT4)) (SETQ PT4A (INTERS PT4 PT1PT3 PT5)) (SETQ PT5A (INTERS PT4 PT1PT2 PT5)) (COMMAND "_PLINE" PT1 PT1APT2 PT2A PT3 PT3A
PT4 PT4A PT5 PT5A "C")(SETVAR "OSMODE" OSN) (PRINC) ) In that example, we just defined a new AutoCAD command "star". So,like "line" or "circle", you can execute the "star"command . In thefirst line of the program, we define the new AutoCAD command. We writeC:before the name....... C:STAR. But be careful !! C: dont say that yourfile is in the C: drive.
Example 2 - New AutoLISP function
(DEFUN DTR (X)
(/ (* X PI) 180.0)
)(DEFUN C:STAR ()
(SETVAR "CMDECHO" 0)
(SETQ OSN (GETVAR "OSMODE"))
(SETVAR "OSMODE" 0)
(SETQ PT (GETPOINT "\nGive thecenter of the star"))
(SETQ RD (GETDIST "\nRadius? : "))
(SETQ PT1 (POLAR PT (DTR90) RD))
(SETQ PT2 (POLAR PT (DTR162) RD))
(SETQ PT3 (POLAR PT (DTR234) RD))
(SETQ PT4 (POLAR PT (DTR306) RD))
(SETQ PT5 (POLAR PT (DTR18) RD))
(SETQ PT1A (INTERS PT1 PT3 PT2PT5))
(SETQ PT2A (INTERS PT2 PT4 PT1PT3))
(SETQ PT3A (INTERS PT3 PT5 PT2PT4))
(SETQ PT4A (INTERS PT4 PT1 PT3PT5))
(SETQ PT5A (INTERS PT4 PT1 PT2PT5))
(COMMAND "_PLINE" PT1 PT1A PT2PT2A PT3 PT3A PT4 PT4A PT5 PT5A "C")
(SETVAR "OSMODE" OSN)
(PRINC)
)In this new example, we just defined a new AutoLISP functionthat will help us in an other program. The DTR function convert DegresTo Radians. Like you can see, we didnt put C:, so, AutoLISP understandthat we want to define a LISP function. The X in the parenthesis is theargument. That is to tell AutoLISP that this function require one argument.
If i would like to create a function that add two numbers, i wouldwrite:
(DEFUN ADD (x y)
Then in a program, i could do this: (add4 5)
(+ x y)
)- Like we said in previous lessons, all our resultsare kept in memory with variables. These results are importants for a goodexecution, but when the routine is finished, we should clear those variables. This will free some memory, and assure that they wont disturb the resultsof an other program. To do that, we define locals variables. Inthe two examples, all the variables were globals. We define localsvariables in the DEFUN function in the parenthèsis, like this:
(DEFUN C:STAR (/ OSN PT RD PT1PT2 PT3 PT4 PT5 PT1A PT2A PT3A PT4A PT5A)
......
)