Visual LISP, AutoLISP and General Customization

Reply
*Michael Pape & Associates
Message 1 of 15 (2,038 Views)

need lisp that will sum selected text values

2038 Views, 14 Replies
06-02-2004 08:14 AM
Can someone steer me to a way in autocad (2002) or to a lisp routine that will add together the values of text entities? For instance, if I have a big parking lot and each section is labeled with the number of spaces, I could pick the texts and it would add all the numbers together to give me a total. Right now I export to excel to do this. I have quite a few situations where this would be handy, not just for parking spaces, so I really just want a lisp that will add all my selected numbers. Mtext or Dtext. Thanks!
Mentor
t.willey
Posts: 2,504
Registered: ‎02-17-2004
Message 2 of 15 (2,039 Views)

Re: need lisp that will sum selected text values

06-02-2004 08:20 AM in reply to: *Michael Pape & Associates
I came up with this one a couple of years ago, it still works for me.

(defun c:calc()

(initget "+ - * /")
(setq app(getkword "\nWhat application would you like to do:\(+,-,*,/)"))
(if(or (= app "-")(= app "/"))(princ "\nDon't forget order counts. So pick carefully."))
(princ "\nSelect numbers to calculate:")
(setq cal(ssget '((0 . "TEXT"))))
(setq n 0)
(setq l(sslength cal))
(while(/= l n)
(get_info)
(if(= nu3 0.0)
(progn
(ssdel (ssname cal n) cal)
(setq l(- l 1))
(setq n(- n 1))
)
)
(setq n(+ n 1))
)
(setq n 0)
(setq l(sslength cal))
(cond
((= app "+")
(addc))
((= app "-")
(subc))
((= app "*")
(multc))
((= app "/")
(divdc))
)
(initget "Replace Write")
(princ "\nAnswer= ")
(princ nu5)
(setq tx1(getkword"\nWhat do you want do with the answer\(Replace, Write to screen):"))
(cond
((= tx1 "Replace")
(setq tx2(ssget '((0 . "TEXT"))))
(setq n 0)
(setq l(sslength tx2))
(while(/= n l)
(setq tx3(entget(ssname tx2 n)))
(setq tx4(subst(cons 1 (rtos nu5 2 1))(assoc 1 tx3) tx3))
(entmod tx4)
(setq n(+ 1 n))
)
)
((= tx1 "Write")
(setq nu5(rtos nu5 2 1))
(command "layer" "m" "sr_text_n" "c" "3" "" "")
;(command "textstyle" "sr-romands2")
(princ "\nPick point for text insertion")
(command "text" "j" "m" pause "" "" nu5 )
)
)

(princ)

)



(defun get_info()
(setq nu1(entget (ssname cal n)))
(setq nu2(cdr(assoc 1 nu1)))
(setq nu3(atof nu2))
)

(defun addc()
(get_info)
(setq nu4 nu3)
(setq n(+ 1 n))
(while(/= l n)
(get_info)
(setq nu5(+ nu4 nu3))
(setq nu4 nu5)
(setq n(+ 1 n))
)
)

(defun subc()
(get_info)
(setq nu4 nu3)
(setq n(+ 1 n))
(while(/= l n)
(get_info)
(setq nu5(- nu4 nu3))
(setq nu4 nu5)
(setq n(+ 1 n))
)

)


(defun multc()
(get_info)
(setq nu4 nu3)
(setq n(+ 1 n))
(while(/= l n)
(get_info)
(setq nu5(* nu4 nu3))
(setq nu4 nu5)
(setq n(+ 1 n))
)
)


(defun divdc()
(get_info)
(setq nu4 nu3)
(setq n(+ 1 n))
(while(/= l n)
(get_info)
(setq nu5(/ nu4 nu3))
(setq nu4 nu5)
(setq n(+ 1 n))
)
)


Tim
*Michael Pape & Associates
Message 3 of 15 (2,039 Views)

Re: need lisp that will sum selected text values

06-02-2004 09:53 AM in reply to: *Michael Pape & Associates
Thanks! I'll give it a go... "T.Willey" wrote in message news:6593832.1086189667449.JavaMail.jive@jiveforum2.autodesk.com... > I came up with this one a couple of years ago, it still works for me. > > (defun c:calc() > > (initget "+ - * /") > (setq app(getkword "\nWhat application would you like to do:\(+,-,*,/)")) > (if(or (= app "-")(= app "/"))(princ "\nDon't forget order counts. So pick carefully.")) > (princ "\nSelect numbers to calculate:") > (setq cal(ssget '((0 . "TEXT")))) > (setq n 0) > (setq l(sslength cal)) > (while(/= l n) > (get_info) > (if(= nu3 0.0) > (progn > (ssdel (ssname cal n) cal) > (setq l(- l 1)) > (setq n(- n 1)) > ) > ) > (setq n(+ n 1)) > ) > (setq n 0) > (setq l(sslength cal)) > (cond > ((= app "+") > (addc)) > ((= app "-") > (subc)) > ((= app "*") > (multc)) > ((= app "/") > (divdc)) > ) > (initget "Replace Write") > (princ "\nAnswer= ") > (princ nu5) > (setq tx1(getkword"\nWhat do you want do with the answer\(Replace, Write to screen):")) > (cond > ((= tx1 "Replace") > (setq tx2(ssget '((0 . "TEXT")))) > (setq n 0) > (setq l(sslength tx2)) > (while(/= n l) > (setq tx3(entget(ssname tx2 n))) > (setq tx4(subst(cons 1 (rtos nu5 2 1))(assoc 1 tx3) tx3)) > (entmod tx4) > (setq n(+ 1 n)) > ) > ) > ((= tx1 "Write") > (setq nu5(rtos nu5 2 1)) > (command "layer" "m" "sr_text_n" "c" "3" "" "") > ;(command "textstyle" "sr-romands2") > (princ "\nPick point for text insertion") > (command "text" "j" "m" pause "" "" nu5 ) > ) > ) > > (princ) > > ) > > > > (defun get_info() > (setq nu1(entget (ssname cal n))) > (setq nu2(cdr(assoc 1 nu1))) > (setq nu3(atof nu2)) > ) > > (defun addc() > (get_info) > (setq nu4 nu3) > (setq n(+ 1 n)) > (while(/= l n) > (get_info) > (setq nu5(+ nu4 nu3)) > (setq nu4 nu5) > (setq n(+ 1 n)) > ) > ) > > (defun subc() > (get_info) > (setq nu4 nu3) > (setq n(+ 1 n)) > (while(/= l n) > (get_info) > (setq nu5(- nu4 nu3)) > (setq nu4 nu5) > (setq n(+ 1 n)) > ) > > ) > > > (defun multc() > (get_info) > (setq nu4 nu3) > (setq n(+ 1 n)) > (while(/= l n) > (get_info) > (setq nu5(* nu4 nu3)) > (setq nu4 nu5) > (setq n(+ 1 n)) > ) > ) > > > (defun divdc() > (get_info) > (setq nu4 nu3) > (setq n(+ 1 n)) > (while(/= l n) > (get_info) > (setq nu5(/ nu4 nu3)) > (setq nu4 nu5) > (setq n(+ 1 n)) > ) > ) > > > Tim
Active Member
ramesh.axis
Posts: 6
Registered: ‎01-27-2012
Message 4 of 15 (1,915 Views)

Re: need lisp that will sum selected text values

01-27-2012 03:58 AM in reply to: *Michael Pape & Associates

Hai..

 

      I am very sorry to say it is not working in Autocad2008

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 5 of 15 (1,913 Views)

Re: need lisp that will sum selected text values

01-27-2012 04:12 AM in reply to: ramesh.axis

Maybe you are using Mtext with formatting

 

Active Member
ramesh.axis
Posts: 6
Registered: ‎01-27-2012
Message 6 of 15 (1,912 Views)

Re: need lisp that will sum selected text values

01-27-2012 04:17 AM in reply to: t.willey

Mr Willy

 

          I could find what i want. but it is not working. The error is - Select objects:  ; error: bad argument type: lselsetp nil.  please rectify this code for me. Iam using Autocad2008. It will very usefull to me. Thank you.

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 7 of 15 (1,908 Views)

Re: need lisp that will sum selected text values

01-27-2012 04:29 AM in reply to: ramesh.axis

Select objects:  ; error: bad argument type: lselsetp nil.  

That means incorrect Data type. So.. agaim, are you using MTEXT?

 


 

*Expert Elite*
Kent1Cooper
Posts: 5,231
Registered: ‎09-13-2004
Message 8 of 15 (1,903 Views)

Re: need lisp that will sum selected text values

01-27-2012 05:42 AM in reply to: *Michael Pape & Associates

*Michael Pape & Associates wrote:
Can someone steer me ... to a lisp routine that will add together the values of text entities?....

Another approach:

 

http://cadtips.cadalyst.com/list/sum-values-text-strings

 

I don't see anything in the description that mentions Mtext, and I haven't downloaded it, so it may also work only with Text entities.

Kent Cooper
*Expert Elite*
Kent1Cooper
Posts: 5,231
Registered: ‎09-13-2004
Message 9 of 15 (1,889 Views)

Re: need lisp that will sum selected text values

01-27-2012 10:58 AM in reply to: *Michael Pape & Associates
*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 10 of 15 (1,867 Views)

Re: need lisp that will sum selected text values

01-29-2012 04:04 AM in reply to: Kent1Cooper

Kent1Cooper wrote:
I don't see anything in the description that mentions Mtext, and I haven't downloaded it, so it may also work only with Text entities.

Exactly my point. as the error mesage indicate as such

 


pbejse wrote:
 That means incorrect Data type. So.. agaim, are you using MTEXT?

This is me short of tellimg the OP  his trying to use the porogram with Mtext entities, but apparently the OP directing the question to the orignal author of the code.

 

Oh well,..

 

Nice tip BTW

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community