Can you do math in a text object?

Can you do math in a text object?

atuckerJKU9U
Enthusiast Enthusiast
618 Views
22 Replies
Message 1 of 23

Can you do math in a text object?

atuckerJKU9U
Enthusiast
Enthusiast

Hey all, I assume a lisp for something like this exists, it's just hard to search for.  I have a tree survey with thousands of trees, and I need to add the values of multi trunk trees together and replace with the new value.  In the image below, I want the 6",6" to say 12" instead, and the 6",7" to say 13" instead.  Typically we do this separately in a spreadsheet, but in this case I need it to be in the drawing.  Anybody know of a lisp that would do this?  Currently they are dtext, could convert them to mtext if that would help.  EG 6",6" is a single dtext.  Doing find and replace beforehand to get rid of commas, inches, adding a plus, all no problem.  Thanks!

 

atuckerJKU9U_0-1756130065951.png

 

0 Likes
Accepted solutions (1)
619 Views
22 Replies
Replies (22)
Message 21 of 23

ВeekeeCZ
Consultant
Consultant

An alternative workflow would be to just export all the texts to the csv file, do the math and other adjustments in Excel, then reimport the texts back to the drawing.

 

See here for example of such routines  

https://www.cadtutor.net/forum/topic/75466-exporting-text-and-mtext-with-base-point-to-a-txt-file-an...

by BIGAL.

0 Likes
Message 22 of 23

komondormrex
Mentor
Mentor

@atuckerJKU9U

hey there,

check this different one. it won't break the command running when false enumaration occurs, as @paullimapa mentioned above. instead it will output this false enumeration as error on autocad console. valid enumeration is integers followed by the inch mark separated with comma only. 

;**********************************************************************************************************************

(defun string_to_list (input_string delimiter / delimiter_position output_list)
  (while (setq delimiter_position (vl-string-search delimiter input_string 0))
    (setq output_list (append output_list
                 (list (vl-string-trim " " (substr input_string 1 delimiter_position)))
              )
        input_string (substr input_string (+ 2 delimiter_position))
    )
  )
  (append output_list (list (vl-string-trim " " input_string)))
)

;**********************************************************************************************************************

(defun c:sum_text_comma (/ trunk_list trunk_asc text_dxf)
  (foreach text (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "text") (1 . "*`,*"))))))
    (if (vl-every '(lambda (trunk) (and (= 34 (last (setq trunk_asc (vl-string->list trunk))))
                       (vl-every '(lambda (asc) (member asc '(48 49 50 51 52 53 54 55 56 57)))
                          (cdr (reverse trunk_asc)))
                         )
             )
             (setq trunk_list (string_to_list (cdr (assoc 1 (setq text_dxf (entget text)))) ","))
      )
      (entmod (subst (cons 1 (strcat (itoa (apply '+
                            (mapcar 'atoi
                                (mapcar '(lambda (trunk) (vl-string-right-trim "\"" trunk))
                                     trunk_list
                                )
                            )
                               )
                               )
                          "\""
                             )
               )
                      (assoc 1 text_dxf)
                      text_dxf
              )
      )
      (princ (strcat "\nError found in trunk enumeration: \"" 
              (cdr (assoc 1 text_dxf)) 
              "\" -> \"" 
              (vl-list->string trunk_asc) 
              "\""
           )
      )
    )
    )
    (princ)
)

;**********************************************************************************************************************

 

 

0 Likes
Message 23 of 23

pbejse
Mentor
Mentor
(defun c:AddThemInch ( / textStringWithComma stringValue numericalValue i)
  (if (setq textStringWithComma (ssget  '((0 . "TEXT")(1 . "#*\"`,#*\""))))
    	(repeat (Setq i (sslength textStringWithComma))
	  	(Setq stringValue (cdr (Assoc 1
					      (setq entityData (entget (ssname textStringWithComma (setq i (1- i))))))))
	  	(setq numericalValue 
	  	    (  (lambda ( str / l)		   
	  		(while (setq p (vl-string-position 44 str nil t))
		  		(setq l (cons (atoi (substr str (+ p 2))) l)
					 str     (substr str 1 p)))
			 	l
			  ) stringValue ))
	      (entmod (subst  (cons 1 (Strcat (itoa 
				(apply '+ (cons (atoi stringValue) numericalValue)))"\"")) (assoc 1 entityData) entityData))
	  )
    )
  (princ)
  )
0 Likes