< >文本数字求和xtcal程序
对选集中文本进行所有数字计算(支持选择集),支持text,mtext中有多个数字字符串,支持字符串中小数,负数:
;;返回: 如文本中有数字,数字相加后写文本,并返回求和数值(非字符串).无有效数字返回nil.
;;注意:文本中的数字必须为正常格式,对 3.2.1 返回错误结果(=3),对 . . 2.1 两个以上连续点返回错误. --1.6 不能连负号.</P>
<P>
(defun xtcal ( / ss filter mspace n e str asclst strs add pt txt txth)
(defun *error* (msg) (if ss (x_draw ss 4)) (setq *error* oerr))
(princ "\n文本数字求和-----------------------陌生人.2004.1")
(princ "\n选择要计算的文本(支持*TEXT选择集):")
(setq oerr *error*
ss (ssget '((0 . "*TEXT")))
filter "0123456789.-+"
mspace (vla-get-modelspace(vla-get-activedocument (vlax-get-acad-object)))
str nil strs nil)
(if ss
(repeat (setq n (sslength ss))
(x_draw ss 3)
(setq n (1- n)
e (ssname ss n)
str (vla-get-textstring(vlax-ename->vla-object e))
asclst (mapcar '(lambda (x) (if (vl-string-search (chr x) filter) x 32)) (vl-string->list str))
strs (strcat (if strs strs " ") (apply 'strcat (mapcar 'chr asclst)) " "))
)
)
(if (and ss (/= "" strs))
(progn
(setq add (eval (read (strcat "(+ " strs ")"))))
(princ "\n文本数字和为: ")(princ add)
(if (setq pt (getpoint "\n标注位置<重新计算>:"))
(progn
(setq prec (getint "\n精度(小数位数):")
txt (rtos add 2 prec)
txth (getdist "\n字高:"))
(vla-addtext mspace txt (vlax-3D-point pt) txth)
(x_draw ss 4)
(princ) add)
(progn (if ss (x_draw ss 4))(xtcal));多次<重新计算>可以作为一个简易统计查看器.
)
)
(progn (princ "\n!空选集或文本中无有效数字!\n") nil)
)
)
;;配套程序(x_draw ss key)
(defun x_draw (ss key / n e)
(if (= 'PICKSET (type ss))
(repeat (setq n (sslength ss))
(setq n (1- n)
e (ssname ss n))
(redraw e key)
)
)
)</P>
<P>其实取得数字文本的语句很简短,就是
(if ss
(repeat (setq n (sslength ss))
(setq n (1- n)
e (ssname ss n)
str (vla-get-textstring(vlax-ename->vla-object e))
asclst (mapcar '(lambda (x) (if (vl-string-search (chr x) filter) x 32)) (vl-string->list str))
strs (strcat (if strs strs " ") (apply 'strcat (mapcar 'chr asclst)) " ")) )
)</P>
<P>
测试:
(xtcal)
选择要计算的文本(支持*TEXT选择集):
Select objects: 1 found
Select objects: Specify opposite corner: 1 found, 2 total
Select objects:
(注明:此时选中两个文本 "afsda 234.5 暗暗-558.9啊 ab " 和 "-1622.000")
文本数字和为: -1946.4 (注: 234.5-558.9-1622.000=-1946.4)
标注位置<重新计算>:
精度(小数位数):3
字高: Specify second point: -1946.4 (注:-1946.4为返回值)</P>
|