Grab multiple pieces of text - numbers and add together

Grab multiple pieces of text - numbers and add together

zach6CTLG
Explorer Explorer
2,238 Views
16 Replies
Message 1 of 17

Grab multiple pieces of text - numbers and add together

zach6CTLG
Explorer
Explorer

Hello there! I have repeated this task so many times over the last few years, and I know that there is some piece of code out there to make it a little quicker! I just need to grab the whole "Distance column" and add the numbers together. If possible, if the lisp could take out the "Foot (') symbol" for all lines, and have the output accessible to me somehow, it would make my life a ton easier!

0 Likes
Accepted solutions (1)
2,239 Views
16 Replies
Replies (16)
Message 2 of 17

paullimapa
Mentor
Mentor

If it’s a Table object then you can add a row at the bottom and in the cell under that column enter a formula like you do in excel to add up all the rows above


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 17

zach6CTLG
Explorer
Explorer

It is a Civil3D Parcel Table, exploded in model space to a block reference, converted to paper space using CHSP command, and exploded twice more so that the text "Bearing", and "Distance" are properly formatted to a legal description mtext inside of paper space.

0 Likes
Message 4 of 17

Patchy
Mentor
Mentor

Use "find" command and delete the ' mark, then delete the comma thousands mark.

Use the add autolisp posted all over the internet then you can get the total.

Can be done.JPG

0 Likes
Message 5 of 17

paullimapa
Mentor
Mentor

share a dwg with that exploded mtext object column


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 6 of 17

Kent1Cooper
Consultant
Consultant

I wouldn't alter the Text objects in the drawing in order to add them up, as suggested in Message 4.  You can leave them alone [other than to select them], if you build the proper alteration into the code for a routine.  If you pull the text string content from such a Text object in a selection set, you can apply this to it to make it usable in an addition:

(atof (vl-string-right-trim "'" (vl-string-subst "" "," "12,345.67'")))

which returns the number that can be put into a list for addition, or just added to an accumulating total:

12345.7

[Of course that would use a variable that the text string content is put in, not the specific value above.  And don't worry about the .67 vs. .7 -- it actually knows it's .67, and the .7 is just in display.]

 

A routine could be made to restrict your selection to Text objects that end with a foot mark, or with a decimal point plus two numerical characters plus a foot mark, etc, depending on whether such characteristic vary and how responsible you want to be to select the right kinds of things.  It could even find all qualifying objects for you, without User selection, if you can rely on them to all have the right characteristics, and on there being no qualifying other objects that you don't want selected. 

Kent Cooper, AIA
0 Likes
Message 7 of 17

hak_vz
Advisor
Advisor

@zach6CTLG  Try this

Edited to work with text and mtext string number. I've overseen that you are having mtext objects.

 

(defun c:add_dist(/ *error* ss i sum LM:StringSubst)
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
		(princ)
		)
		(princ)
	)
	(defun LM:StringSubst ( new old str / inc len )
		(setq len (strlen new)
			  inc 0
		)
		(while (setq inc (vl-string-search old str inc))
			(setq str (vl-string-subst new old str inc)
				  inc (+ inc len)
			)
		)
		str
	)
	(setq ss (ssget '((0 . "*TEXT"))) i -1 sum 0)
	(cond 
		((and ss (> (sslength ss) 0))
			(while (< (setq i (1+ i)) (sslength ss))
				(setq sum (+ sum (atof (LM:StringSubst  "" ","(cdr (assoc 1 (entget(ssname ss i))))))))
			)
			(princ (strcat "\nSum of selected distance is = " (rtos sum 2 5)))
		)
		(T (princ "\nNothing selected!"))
	)
	(princ)
)

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 8 of 17

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

....

A routine could be made to restrict your selection to Text objects that end with ... a decimal point plus two numerical characters plus a foot mark.... 


For that particular restriction, if they're always in that format, for example:

 

(defun C:AddFeetText (/ ss n)
  (if (setq total 0 ss (ssget '((1 . "*.##'")))); only in feet to two decimal places
    (repeat (setq n (sslength ss)); then
      (setq total
        (+
          total
          (atof (vl-string-right-trim "'" (vl-string-subst "" "," (cdr (assoc 1 (entget (ssname ss (setq n (1- n)))))))))
        ); +
      ); setq
    ); repeat
  ); if
  (prompt (strcat "\nTotal: " (rtos total 2 2) "'."))
  (prin1)
)

 

That leaves the numerical total [not the text-string equivalent] in the 'total' variable, to do with what you will.  It could be made to re-insert a comma into the text string if appropriate.  Which raises the question -- would you ever get into millions?  That is, would there ever be more than one comma?  [It doesn't seem likely -- a million feet is around 189 miles.]

 

It works with Mtext as well as Text, provided the Mtext is in a separate one for each distance, and they don't have internal formatting.

Kent Cooper, AIA
0 Likes
Message 9 of 17

zach6CTLG
Explorer
Explorer

In this particular case, I do need to edit the text! 

zach6CTLG_0-1709153195460.png

When I paste these values over to the right from my template, I don't need that symbol. Below this there is a "total feet" value, and that is why I was trying to delete all the ' symbols and add up at the same time.

0 Likes
Message 10 of 17

zach6CTLG
Explorer
Explorer

Here is a dwg

0 Likes
Message 11 of 17

zach6CTLG
Explorer
Explorer

This works for the addition! Thanks mate. Now if I could only get the ' symbol deleted for all the selected texts??

0 Likes
Message 12 of 17

zach6CTLG
Explorer
Explorer

These distances usually never go above 5,000 or so! And I forgot to mention that I'll never have to worry about selecting text i don't want to because all I do is create a window selection of the distance column. I sent a drawing a second ago.

0 Likes
Message 13 of 17

hak_vz
Advisor
Advisor
Accepted solution

@zach6CTLG wrote:

This works for the addition! Thanks mate. Now if I could only get the ' symbol deleted for all the selected texts??


Like this?

(defun c:add_dist(/ *error* ss i sum LM:StringSubst ent)
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
		(princ)
		)
		(princ)
	)
	(defun LM:StringSubst ( new old str / inc len )
		(setq len (strlen new)
			  inc 0
		)
		(while (setq inc (vl-string-search old str inc))
			(setq str (vl-string-subst new old str inc)
				  inc (+ inc len)
			)
		)
		str
	)
	(setq ss (ssget '((0 . "*TEXT"))) i -1 sum 0)
	(cond 
		((and ss (> (sslength ss) 0))
			(while (< (setq i (1+ i)) (sslength ss))
				(setq ent (entget(ssname ss i)))
				(setq sum (+ sum (atof (LM:StringSubst  "" ","(cdr (assoc 1 ent))))))
				(setq ent(subst (cons 1 (LM:StringSubst  "" "'"(cdr (assoc 1 (entget(ssname ss i)))))) (assoc 1 ent) ent))
				(entmod ent)
			)
			(princ (strcat "\nSum of selected distance is = " (rtos sum 2 5)))
		)
		(T (princ "\nNothing selected!"))
	)
	(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 14 of 17

zach6CTLG
Explorer
Explorer

This nailed it. Thank you! 

Message 15 of 17

hak_vz
Advisor
Advisor

@zach6CTLG wrote:

This nailed it. Thank you! 


You're welcome!

Next time attach sample dwg with your initial request and try to summarize all your wishes.

That helps us to speed up writing requested lisp script.

Bye!

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 16 of 17

Kent1Cooper
Consultant
Consultant

.... and my ADDFEETTEXT reports the total of 1191.32' from the sample drawing's chart.  You can use it for content in another Text object, or whatever.

Kent Cooper, AIA
0 Likes
Message 17 of 17

calderg1000
Mentor
Mentor

Regards @zach6CTLG 

Try this code, ..., it was edited to remove the feet symbol.

 

;;;___
(defun c:sum_tx	(/ stx i txn txi ltx)
  (setq stx (ssget '((0 . "*text"))))
  (repeat (setq i (sslength stx))
    (setq txn (entget (ssname stx (setq i (1- i))))
	  txi (atof (vl-list->string
		      (vl-remove-if
			'(lambda (x) (or (= x 39) (= x 44)))
			(vl-string->list
			  (cdr (assoc 1 txn))
			)
		      )
		    )
	      )
	  ltx (cons txi ltx)
    )
    (entmod (subst (cons 1 (rtos txi 2 2)) (assoc 1 txn) txn))
  )
  (apply '+ ltx)
)

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes