Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

add numbers lisp

1 REPLY 1
Reply
Message 1 of 2
Anonymous
1158 Views, 1 Reply

add numbers lisp

i can't seem to get this lisp to add numbers unless they have a whole number in front of them as in 0 3/4", 1'-2 1/4... it will not add 3/4", 1/4"..., it reads the number as 3 or 1 respectively. can someone see what i'm missing?


(DeFun FixStr(Str)
(While (Member (SubStr Str 1 1)
'("0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
(SetQ Str (SubStr Str 2))))

(DeFun C:Add()
(SetQ Fun + Pmpt "\nAdding machine: pick screen +num >() " Tot 0.0)
(While (Not (Null Fun))
(While (SetQ E
(EntSel (StrCat (SubStr Pmpt 1 20) (RToS Tot) (SubStr Pmpt 21) )))
(SetQ Str (Cdr (Assoc 1 (entget(car e))))
Tot (Fun Tot
(ProgN
(SetQ Num (Float (AToI Str)))
(SetQ Str (FixStr Str))
(If (= (SubStr Str 1 2) "'-")
(ProgN (SetQ Num (* Num 12.0) Str (SubStr Str 3))
(SetQ Num (+ Num (Float (AToI Str))))
(SetQ Str (FixStr Str))))
(If (/= (SetQ F (Float (AToI Str))) 0)
(+ (/ F (AToI (SubStr (FixStr (SubStr Str 2)) 2))) Num)
Num))))
); end while get input
(SetQ F T)
(While F
(SetQ St (GetString "\nOptions +, -, k, q : "))
(Cond
((Member St '("q" "Q" ""))
(SetQ F Nil Fun Nil))
((= St "+")
(SetQ Fun + Pmpt "\npick screen +num >() " F Nil))
((= St "-")
(SetQ Fun - Pmpt "\npick screen -num >() " F Nil))
((Member St '("k" "K"))
(While (/= "" (SetQ Str
(GetString (StrCat "\nEnter keyboard num (" (RToS Tot) ") : "))))
(SetQ Num (AToF Str)
Sign (If (MinusP Num) -1 1)
Num (* Num Sign)
Ft (Fix Num)
Num (* (- Num Ft) 100.0)
In (Fix (+ Num 1.0e-12))
Num (- Num In))
(If (< Num 1.0e-12) (SetQ Num 0.0))
(SetQ Num (+ (* Ft 12.0) In (* Num 6.25))
Tot (+ Tot (* Sign Num))))))))
(SetQ StrDef (RToS Tot))
(Princ StrDef)
)


Edited by: joeyl10 on Oct 15, 2008 10:46 PM
1 REPLY 1
Message 2 of 2
Anonymous
in reply to: Anonymous


joeyl10 wrote:
i
can't seem to get this lisp to add numbers unless they have a whole
number in front of them as in 0 3/4", 1'-2 1/4... it will not add 3/4",
1/4"..., it reads the number as 3 or 1 respectively. can someone see
what i'm missing?

Joey -



You're working way too hard! Try using (getdist) as below -- it will do
all the entry validation and conversion for you.



This routine accepts numbers in any format, as well as on-screen
distances. Not much error-checking, but not much needed.



-Bill



==========



;;; AD adds a
series of numbers entered in any valid AutoCAD units

;;; format, and returns the result in current units and precision.



;;; Use of the GETDIST function provides built-in error checking

;;; and units conversion. Numbers and distances can be entered

;;; either from the keyboard or by indicating points on the screen.



;;; by Bill Gilliss. Help yourself.



(defun c:ad ( / value)

  (setq oldunits (getvar "LUNITS"))

  (setvar "LUNITS" 4)  ;accepts any input format, incl scientific

  (setq adTotal 0)

  (setq value 0)

  (princ "\nEnter a number or indicate a distance (ENTER to finish):")

  (princ "\n")



  (while (/= value nil)

   (setq value (getdist ))

   (if (/= value nil)(setq AdTotal (+ AdTotal value)));if

   (princ (strcat "\n= " (rtos AdTotal) " + "))

    );while



  (setvar "LUNITS" oldunits)

  (princ (strcat "\nAdTotal: " (rtos AdTotal )))

  (princ)

);defun



(princ "AD loaded. Result will be stored in variable AdTotal.")

(princ)


Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost