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

need lisp that will sum selected text values

15 REPLIES 15
Reply
Message 1 of 16
Anonymous
9905 Views, 15 Replies

need lisp that will sum selected text values

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!
15 REPLIES 15
Message 2 of 16
t.willey
in reply to: Anonymous

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
Message 3 of 16
Anonymous
in reply to: Anonymous

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
Message 4 of 16
ramesh.axis
in reply to: Anonymous

Hai..

 

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

Message 5 of 16
pbejse
in reply to: ramesh.axis

Maybe you are using Mtext with formatting

 

Message 6 of 16
ramesh.axis
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.

Message 7 of 16
pbejse
in reply to: ramesh.axis


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

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

 


 

Message 8 of 16
Kent1Cooper
in reply to: Anonymous


@Anonymous 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, AIA
Message 9 of 16
Kent1Cooper
in reply to: Anonymous

Message 10 of 16
pbejse
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

Message 11 of 16
alanjt_
in reply to: pbejse
Message 12 of 16
leipogs23
in reply to: alanjt_

Message 13 of 16
Rusty.Steel
in reply to: Anonymous

Tim,

 

Works great, but where in the lsp could I change the rounding precision of the final number. The numbers I am selecting are all rounded to the second decimal place (which I want), BUT the sum number the lsp is givinf me is rounded to the fisrt decimal place (which I would like to change to the second). Thanks 

Thanks,

Rusty Steel
Cobb Engineering Company
Message 14 of 16
Kent1Cooper
in reply to: Rusty.Steel


@rsteel wrote:

.... where in the lsp could I change the rounding precision of the final number. ... the sum number the lsp is givinf me is rounded to the fisrt decimal place (which I would like to change to the second). ....


[In case Tim's not still checking in after 8-1/2 years....]

 

Change the last number in the (rtos) functions such as (rtos nu5 2 1) from a 1 to a 2 -- that's the number of decimal places [see (rtos) in the AutoLISP Reference].

Kent Cooper, AIA
Message 15 of 16
braudpat
in reply to: Kent1Cooper

 

Hello

 

An other solution which can read Text & MText entities ...

This routine (TOTTXT) comes froma French forum but I don't remender the Author : SORRY !

 

Bye, Patrice

 

;; Routine TOTTXT.LSP
;; Fonctionne en principe sur les TEXT & MTEXT de type numerique
;; Version 1.1 avec Creation d un texte du Total

(defun c:tottxt (/ doc sel tot pt)
  (vl-load-com)
  (setq	tot 0
	doc (vla-get-activedocument (vlax-get-acad-object))
  )
  (princ "\nSélectionnez les textes")
  (if (ssget (list (cons 0 "TEXT,MTEXT")))
    (progn
      (vlax-map-collection
	(setq sel (vla-get-activeselectionset doc))
	'(lambda (x)
	   (setq tot (+ (atof (vla-get-textstring x)) tot))
	 )
      )
      (vla-delete sel)
      (if (setq pt (getpoint "\nPositionnez le résultat: "))
	(vla-addText
	  (vla-get-ModelSpace doc)
	  (rtos tot)
	  (vlax-3d-point pt)
	  (getvar "TEXTSIZE")
	)
      )
    )
  )
)
 

 

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


Message 16 of 16
inz.romaniuk
in reply to: braudpat

do we can adapt this list to value of attribute in block ??

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost