text total

text total

k005
Advisor Advisor
852 Views
10 Replies
Message 1 of 11

text total

k005
Advisor
Advisor

 

Hello

 

How can I get the sum of the values I gave in the sample drawing?

 

I colored the objects. to be descriptive. But I will make the selection manually...

 

Thanks in advance

 

 

0 Likes
Accepted solutions (1)
853 Views
10 Replies
Replies (10)
Message 2 of 11

rkmcswain
Mentor
Mentor
  • Use (ssget) to grab a selection from the user (or Automatic if you can filter on one or more properties)
  • Iterate that selection, and in your example, I'd search for the ":" and the "m" and grab the value in between and add that value to a variable.
  • At the end of the loop, that variable should be your total.
R.K. McSwain     | CADpanacea | on twitter
Message 3 of 11

hak_vz
Advisor
Advisor

Try this

 

(defun c:total ( / *error* string_to_list ss total ent e i) 
(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ)
		)
	)
	(defun string_to_list ( str del / pos )
		(if (setq pos (vl-string-search del str))
			(cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
			(list str)
		)
	)
	(setq del (getstring "\nSelect text specific delimiter (: = )  > " ))
	(princ "\nSelect text to add up > ")
	(setq ss (ssget '((0 . "TEXT"))) i -1)
	(setq total 0.0)
	(cond 
		((and ss)
			(while (< (setq i (1+ i)) (sslength ss))
				(setq ent (entget(ssname ss i)))
				(setq e (cdr (assoc 1 ent)))
				(setq e (cadr (string_to_list e del)))
				(setq total (+ total (atof(cadr (string_to_list e " ")))))
			)
		)
	)
	(princ (strcat "\nTotal sum is " (rtos total 2 2)))
	(princ)
)

 

 

Assure to have one " " in front and behind  delimiter i.e. " : " or " = ".  Write yourself a note in your lisp file

to remember this

;this is how to  write comment in autolisp  (start each  line with 😉

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.
Message 4 of 11

k005
Advisor
Advisor

@hak_vz 

 

(setq e (cadr (string_to_list e ":")))

 

It was nice. Can we just include = in this code?

like xxxxxxspace=spacexxx m³

 

0 Likes
Message 5 of 11

hak_vz
Advisor
Advisor

@k005  My previous code is updated so try it now. I would have to add some additional string trimmings to remove spaces but this way you'll have consistent texts in your drawings.

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 6 of 11

3wood
Advisor
Advisor

You can also try ALTEXT.

The number sum is shown at the bottom of the dialogue box.

Capture.PNG

Message 7 of 11

k005
Advisor
Advisor

@hak_vz 

 

" : " and " = " I won't enter it manually. The Lisp routine should be able to select this automatically... that's what I meant.

 

* It is very nice as it is.

0 Likes
Message 8 of 11

hak_vz
Advisor
Advisor
Accepted solution

@k005 wrote:

@hak_vz 

 

" : " and " = " I won't enter it manually. The Lisp routine should be able to select this automatically... that's what I meant.


That way you can have any expression of type (description delimiter value unit) processed, but ok, here is the way you wish.

 

(defun c:total ( / *error* string_to_list ss total ent e i) 
(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ)
		)
	)
	(defun string_to_list ( str del / pos )
		(if (setq pos (vl-string-search del str))
			(cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
			(list str)
		)
	)
	(princ "\nSelect text to add up > ")
	(setq ss (ssget '((0 . "TEXT"))) i -1)
	(setq total 0.0)
	(cond 
		((and ss)
			(while (< (setq i (1+ i)) (sslength ss))
				(setq ent (entget(ssname ss i)))
				(setq e (cdr (assoc 1 ent)))
				(if (vl-string-position (ascii ":") e) (setq del ":")(setq del "="))
				(setq e (cadr (string_to_list e del)))
				(setq total (+ total (atof(cadr (string_to_list e " ")))))
			)
		)
	)
	(princ (strcat "\nTotal sum is " (rtos total 2 2)))
	(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.
Message 9 of 11

k005
Advisor
Advisor

@hak_vz 

 

again a very good solution.
Thanks a lot, it's ok. 🤗

0 Likes
Message 10 of 11

pbejse
Mentor
Mentor

@k005 wrote:

How can I get the sum of the values I gave in the sample drawing?


 

(Defun c:Totally ( / Text ss pt i ent data a str b p m3 v pref m2_Coll)
;;		pBe Jul 2021		;;;
(defun Text (l pt str)
  (entmakex (append l
	      (list (cons 10 pt)
		    (cons 1 str)
	      )
	    )
	  )
	)	 
(if (and
      (setq ss (ssget '((0 . "TEXT")(1 . "*:*m²,*=*m³"))))
      (setq pt (Getpoint "\nPick Text insertion point"))
      )
  (progn
    (repeat (setq i (sslength ss))
      (setq ent (entget (ssname ss (setq i (1- i)))))      
	(setq data (Cons
		     (mapcar '(lambda (d)
			      (assoc d ent)) '(1 0 7 8 40 50)) data))      
      )
	(while (setq a (car data))
	  	(setq str (cdar a) b (Cdr data))
		(cond
		  ((setq p (vl-string-position 61 str))
		   	(setq m3 (cons (read (substr str (+ 2 p))) m3))
		   	(foreach c (vl-remove-if-not '(lambda (d)
						(vl-string-position 61 (Cdar d))) b)
			  	(setq m3 (cons (read (substr (Cdar c) (+ 2 (vl-string-position 61 (Cdar c))))) m3 )
				      b (vl-remove c b)))
		   	(setq m3 (list
				   ( strcat  "Beton = " (rtos (apply '+ m3) 2 2) " m³")
				       (cdr a)))
		   	)
		  ((setq p (vl-string-position 58 str))
		    (setq v (cons (read (substr str (+ 2 p))) v))		   
		    (setq pref (substr str 1 (1+ p)))
		     (foreach itm b
		       (if (= (vl-string-mismatch pref (cdar itm))  (1+ p))
		   	(setq v (cons  (read (substr (cdar itm) (+ 2 p))) v) b
			       (vl-remove itm b))
			)
		     )
		   (setq m2_Coll (cons (list
					 (strcat  pref " " (rtos (apply '+ v) 2 2) " m²")
					     (cdr a)) m2_Coll) v nil)
		   )
		)
		  (setq data b)
		  )
	  (foreach val m2_Coll
	    (Text (cadr val) pt (car val))
	    	(setq pt (polar pt (/ pi 2.0)
				(* (cdr (cadddr (cadr val))) 1.666))))    
	(and m3 (Text (cadr m3) pt (car m3)))    	 	
	)
  )
  (princ)
)

 

Command: Totally

 

HTH

 

Message 11 of 11

k005
Advisor
Advisor

@pbejse 

 

Thank you very much for the work.

Alternatively, I save it to my archive.

thanks my friend.

0 Likes