Message 1 of 6
Lisp modification for Average, Min, Max value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Can someone please assist me?
I found the below Lisp that does a great job already of calculating Average value for example, but I need to have it modified to ONLY read numeric values within selected text strings. I have values that regularly have prefixes that are not allowing the Lisp to function properly.
e.g. "H=0.60" + "B=2.75" to return "1.675"
Currently it is returning the result as "0.000".
Thanks in advance!
; Max, Min and Average by Lee McDonnell -- 12/08
(defun c:number (/ ans ss_max ssl_max xth tent_max ent_max ent_base_max
num_max ss_min ssl_min yth tent_min ent_min ent_base_min
num_min ss ssl index tents tot ent num
ave
)
(princ "\nInitializing...")
(initget 1 "Max Min Average")
(setq ans (getkword "\nSpecify Numerical Requirement [MAx/MIn/Ave]: "))
(cond
((= ans "Max")
(setq ss_max (ssget)
ssl_max (sslength ss_max)
xth 0
tent_max 0
) ;_ end setq
(if (/= ssl_max 0)
(progn
(while (< xth ssl_max)
(setq ent_max (entget (ssname ss_max xth)))
(if (= (cdr (assoc 0 ent_max)) "TEXT")
(progn
(setq ent_base_max (atof (cdr (assoc 1 ent_max))))
(setq xth ssl_max)
) ;_ end progn
(setq xth (1+ xth))
) ;_ end if
) ;_ end while
(setq xth 0)
(repeat ssl_max
(setq ent_max (entget (ssname ss_max xth)))
(if (= (cdr (assoc 0 ent_max)) "TEXT")
(progn
(setq num_max (atof (cdr (assoc 1 ent_max))))
(if (> num_max ent_base_max)
(setq ent_base_max num_max)
) ;_ end if
(setq tent_max (1+ tent_max))
) ;_ end progn
) ;_ end if
(setq xth (1+ xth))
) ;_ end repeat
(alert (strcat "Maximum of " (rtos tent_max) " Numbers is: " (rtos ent_base_max)))
) ;_ end progn
(alert "No Entities Selected.")
) ;_ end if
)
((= ans "Min")
(setq ss_min (ssget)
ssl_min (sslength ss_min)
yth 0
tent_min 0
) ;_ end setq
(if (/= ssl_min 0)
(progn
(while (< yth ssl_min)
(setq ent_min (entget (ssname ss_min yth)))
(if (= (cdr (assoc 0 ent_min)) "TEXT")
(progn
(setq ent_base_min (atof (cdr (assoc 1 ent_min))))
(setq yth ssl_min)
) ;_ end progn
(setq yth (1+ yth))
) ;_ end if
) ;_ end while
(setq yth 0)
(repeat ssl_min
(setq ent_min (entget (ssname ss_min yth)))
(if (= (cdr (assoc 0 ent_min)) "TEXT")
(progn
(setq num_min (atof (cdr (assoc 1 ent_min))))
(if (< num_min ent_base_min)
(setq ent_base_min num_min)
) ;_ end if
(setq tent_min (1+ tent_min))
) ;_ end progn
) ;_ end if
(setq yth (1+ yth))
) ;_ end repeat
(alert (strcat "Minimum of " (rtos tent_min) " Numbers is: " (rtos ent_base_min)))
) ;_ end progn
(alert "No Entities Selected.")
) ;_ end if
)
((= ans "Average")
(setq ss (ssget)
ssl (sslength ss)
index 0
tents 0
tot 0
) ;_ end setq
(repeat ssl
(setq ent (entget (ssname ss index)))
(if (= (cdr (assoc 0 ent)) "TEXT")
(progn
(setq num (atof (cdr (assoc 1 ent))))
(setq tot (+ num tot))
(setq tents (1+ tents))
) ;_ end progn
) ;_ end if
(setq index (1+ index))
) ;_ end repeat
(if (/= tents 0)
(progn
(setq ave (/ tot tents))
(alert (strcat "Average of " (rtos tents) " Numbers is: " (rtos ave)))
) ;_ end progn
(alert "No Text Entities Selected.")
) ;_ end if
)
) ;_ end cond
(princ)
) ;_ end defun