Beruflich Dokumente
Kultur Dokumente
; A drawing of a calculator pops up. You pick it's buttons, or use the
; keyboard, for input. Numbers are displayed and evaluated just like a
; real calculator. It supports +, -, *, /, ^ (exponent), order of
; operations and brackets. You can do as many calculations as you like,
; exiting the routine by entering a null calculation (hit '=' twice).
; Distances, angles and areas from your drawing can be entered into calc.
; Numbers can be stored in 'memory' by picking the display and inserting a
; piece of text into your drawing. The text will be deleted at calc's exit,
; unless you take the 'Permanent' option at the insertion pt prompt. These
; numbers (and other text entities which are numbers) are recalled into calc
; by picking them. The screen menu is used for user defined functions. You
; can easily add any function you like to calc. I include trig and inverse
; trig functions as examples and give instructions for creating your own.
N5A 2P6
73417,1024
;The help screen code on line #254 assumes calc.lsp is located in C:\ACAD\LSP\.
;If it ain't so, please change line #254.
(command)
;If Release 10, turn off gridmode for each vport, set UCS to view.
;Gridmode off suppresses the redraw in each vport accompanying a UCS change.
(if(>(atof(getvar"ACADVER"))9)
(progn
(setq VPCFG(vports)LST1 nil)
(foreach TEMP VPCFG
(setvar"CVPORT"(car TEMP))
(setq LST1(cons(cons(car TEMP)(getvar"GRIDMODE"))LST1))
(setvar"GRIDMODE"0))
(setvar"CVPORT"(caar VPCFG))
(if(tblsearch"UCS""$CALC$")
(command".UCS""D""$CALC$"))
(command".UCS""V"".UCS""S""$CALC$")
;Restore gridmodes.
(foreach TEMP LST1
(setvar"CVPORT"(car TEMP))
(setvar"GRIDMODE"(cdr TEMP)))
(setvar"CVPORT"(caar VPCFG))
(setq LST1 nil))
(setq VPCFG'((1 (0.0 0.0)(1.0 1.0)))))
(setq TEMP(list(getvar"VIEWSIZE")(getvar"VIEWCTR"))
;The calculator block is 1 unit high. SCALE is used for insert cmd.
;SCALE will be the height, in dwg units, of inserted calculator.
SCALE(/(car TEMP)(-(cadr(caddar VPCFG))(car(cdadar VPCFG)))3))
ENT(entlast))
(while(CALC)
(prompt"\n "))
(exit))
;The meat.
;Gets a number and an operation from user.
;Does error checking on input.
;Accepts input from calculator buttons or keyboard.
(defun GET#( / INPUT TEMP LST1 NUMBER ENT)
(setq INPUT T NUMBER" ")
(while INPUT
(setq INPUT(grread))
(cond
((or
;Keyboard input?
(and(eq(car INPUT)2)
(member(cadr INPUT);Recognised key pressed?
(list 48 49 50 51 52 53 54 55 56 57 46 94 91 93
123 125 40 41 47 42 45 43 61 80 112 8 32 13))
(setq KEY(chr
(cadr
(cond
((assoc(cadr INPUT)
'((91 123)(93 125)(40 123)(41 125)(112 80)(13 61)(32 61))))
(INPUT)))))))
;At this point, kybd & button input are the same.
;KEY holds a single character string.
(cond
;Backspace?
((eq KEY"\010")
(_BKSPC))
;+ - * / ^ } =
((member(ascii KEY)(list 43 45 42 47 94 125 61))
(_+-*/^}=))
;Left bracket?
((eq KEY"{")
(_{))
;Plus/Minus.
((eq KEY"P")(_+/-)))
;Point picked?
((eq(car INPUT)3)
(_POINT))
;Esc key.
((equal INPUT(list 2 27))
(LSPHLP"C:\\REL\\LSP\\CALC.LSP"15);Display first 15 lines of this file.
(setq CNT -1)
(foreach STR MNULST
(grtext(setq CNT(1+ CNT))(cond(STR)(" "))))
(prompt"\n"))));End of while loop.
(if KEY
(progn
(prompt
(cond
((eq KEY"expt")"^")
((or(/= KEY"=")(= BKTCNT INFUNC 0))KEY)
("}")))
(list(read KEY)(atof NUMBER)))));Return operator and number.
(defun _BKSPC()
(setq KEY nil);Flag down redraw on button, there isn't one for bkspc.
(if(eq NUMBER" ")
(BEEP)
(progn
(entmod(subst(cons 1"")(cons 1(substr NUMBER(strlen NUMBER)))
(entget(ssname CALCSS(+(strlen NUMBER)19)))))
(prompt"\010 \010")
(if(eq(setq NUMBER(substr NUMBER 1(1-(strlen NUMBER))))"")
(setq NUMBER" ")))))
(defun _DIGIT()
(if(and(eq KEY".");Decimal pt?
(setq CNT 0)
(defun _+-*/^}=()
(setq INPUT nil);A valid operation will exit GET#.
(cond
((eq KEY"^")
(setq KEY"expt"))
((eq NUMBER" ")
(cond
((and(eq KEY"=")(null #LST))
(setq KEY nil));Return a null calculation & exit C:CALC.
(LAST#
(setq NUMBER LAST#)
(PRINT# NUMBER))
((setq INPUT T)
(BEEP))))))
(defun _{()
(if(eq NUMBER" ")
(progn
(prompt"{")
(setq BKTCNT(1+ BKTCNT)
NUMBER(strcat"{"(itoa BKTCNT)))
(DISP#)
(redraw ENT 4)
(setq LAST# nil NUMBER(CALC)BKTCNT(1- BKTCNT))
(if NUMBER
(progn(setq NUMBER LAST#)
(if(eq KEY"=")(setq INPUT nil)));If bracket was closed with
(setq NUMBER" ")))
(BEEP)))
(defun _+/-()
(if(eq NUMBER" ")
(if LAST#
(setq NUMBER LAST#))
(repeat(-(strlen NUMBER)(if(eq(ascii NUMBER)32)1 0))
(prompt"\010")))
(setq NUMBER(strcat(if(eq(ascii NUMBER)32)"-"" ")(substr NUMBER 2))
LAST# nil)
(entmod(subst(cons 1(substr NUMBER 1 1))
(defun _POINT()
(cond
;Display picked?
((and(>=(caadr INPUT)(-(car INSPT)(* SCALE 0.2545)))
(<=(caadr INPUT)(+(car INSPT)(* SCALE 0.2545)))
(>=(cadadr INPUT)(+(cadr INSPT)(* SCALE 0.4364)))
(<=(cadadr INPUT)(+(cadr INSPT)(* SCALE 0.56565)))
(or(/= NUMBER" ")LAST#))
(_STORE))
(defun _STORE()
;Copy current display into a piece of text.
(if(and
(progn(initget"P")
(setq TEMP(getpoint(list(car INSPT)(+(cadr INSPT)(* SCALE 0.501)))
" Permanent or <insertion pt>: ")))
(if(eq TEMP"P")
(setq MODE nil
TEMP(getpoint(list(car INSPT)(+(cadr INSPT)(* SCALE 0.501)))
"\n Insertion pt: "))
(setq MODE T)))
(progn
(command "" ".COPY"(ssname CALCSS 29)""
(cdr(assoc 11(entget(ssname CALCSS 29))))TEMP".SELECT")
(entmod(subst
(cons 1(if(/= NUMBER" ")NUMBER LAST#))
(cons 1(substr NUMBER 10 1))
(entget(entlast))))
(if MODE
(ssadd(entlast)CALCSS))))
(prompt"\n"))
(defun _MENU()
(setq INFUNC(1+ INFUNC))
(if(eq NUMBER" ")
(SYM
(if(and(setq MODE(assoc SYM SYSLST))(cdr MODE))
(progn(setvar SYM(cdr MODE))
(setq SYSLST(subst(cons SYM nil)MODE SYSLST)))))
(T(foreach MODE SYSLST
(if(cdr MODE)(setvar(car MODE)(cdr MODE))))
(setq SYSLST nil))))
(defun PRINT#(STR)
(prompt(substr STR(if(eq(ascii STR)32)2 1))))
;Help screen.
;Pass it file name and # of lines to read.
(defun LSPHLP( STR CNT / FH)
(if(setq FH(open STR"r"))
(progn
(textscr)
(princ"\n")
(while(> CNT 0)
(repeat(min 24 CNT)
(princ(strcat(substr(read-line FH)2)"\n")))
(princ" Hit any key to continue:")
(if(eq(car(grread))6)
(grread));Polite thing to do, in case calling program uses grread.
(setq CNT(- CNT 24)))
(close FH))
(prompt(strcat"\n Help file "STR" not found."))))
;I got lucky, acad's area cmd turned out to be easily (?) driven with Lisp.
(defun _AREA( / PT1 MODE)
(command)
(SYSVAR"CMDECHO"1)
(command".AREA")
(while(and(or MODE(null PT1))
(progn(initget"Entity Add Subtract")(setq PT1(getpoint))))
(cond
((listp PT1)
(while(progn(command PT1)(setq PT1(getpoint))))
(command""))
((eq PT1"Entity")
(while(setq PT1(entsel))
(command PT1))
(command""))
((eq PT1"Add")
(setq MODE T)
(command"Add"))
((eq PT1"Subtract")
(setq MODE T)
(command"Subtract")))
(setq PT1 T))
(SYSVAR"CMDECHO"0)
(command"")
(if(setq MODE(#TRIM(getvar"AREA")))
(PRINT# MODE))
MODE)
(defun RAD2DEG(ANG)
(*(/ ANG pi)180))
(defun DEG2RAD(ANG)
(*(/ ANG 180)pi))
(prompt"ArcCos{")
(if(setq TEMP(CALC))
(RAD2DEG(atan(/(sqrt(abs(- 1(* TEMP TEMP))))TEMP)))))
(princ)