TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Dommy2Hotty on September 13, 2005, 06:04:33 PM
-
I'm trying use this code to select text that has a number value, extract the text contents, and make calculations based on that.
(setq ent (entget (car (entsel "\nSELECT text: ")))
contents (cdr (assoc 1 ent))
8percent (rtos (* contents 0.08) 2 1)
);setq
But I keep getting [; error: bad argument type: numberp: nil]
I've done (type contents) and it comes back as STR, so RTOS should work, in my mind, anyway...
Does RTOS need to go in when I'm extracting assoc code 1 instead of during the calculation?
-
Read up on distof, then consider this --
(defun MyDistOf ( string )
;; returns numeric value or nil
(cond
((/= 'str (type string)) nil)
((distof string 2))
((distof string 3))
((distof string 5))
((distof string 1))
)
)
-
Read up on distof.
Fantastic! Thank you very much! :mrgreen:
Finished code:
(setq ent (entget (car (entsel "\nSELECT text: ")))
contents (cdr (assoc 1 ent))
contents (distof contents 2)
8percent (rtos (* contents 0.08) 2 1)
);setq
-
My pleasure.
:-)
-
Just for fun ...
(defun MyDistOf ( string / result )
(if (eq 'str (type string))
(vl-some
'(lambda (mode)
(setq result
(distof
string
mode
)
)
)
'(2 3 5 4 1)
)
)
result
)
-
Actually ... I shouldn't test that the argument passed is a string; iow, if passed anything but a string it should promptly crash. Thus:
(defun MyDistOf ( string / result )
(vl-some
'(lambda (mode)
(setq result
(distof
string
mode
)
)
)
'(2 3 5 4 1)
)
result
)
Or ...
(defun MyDistOf ( string )
(cond
((distof string 2))
((distof string 3))
((distof string 5))
((distof string 4))
((distof string 1))
)
)
Carry on.
-
:? :-o :?