Block with Attributed text showing numbers convert

Block with Attributed text showing numbers convert

kzD1219
Collaborator Collaborator
1,258 Views
11 Replies
Message 1 of 12

Block with Attributed text showing numbers convert

kzD1219
Collaborator
Collaborator

Is there any way, to have an attributed block that has say a number value (imperial elevation) typed in, but need to convert that number to show metric elevation instead?  I don't want to have to edit every single piece of attributed text since I am dealing with a very large file.

 

I have attached to block that we need to update the attributed text value by a scale factor of 0.3048.

 

Or is there a way to type in a number for the value and have that automatically convert by a scale factor of 0.3048 and show up with the answer?  For example type in 25.00 for a value and then have it convert by 0.3048 to 7.62.

 

 

0 Likes
Accepted solutions (2)
1,259 Views
11 Replies
Replies (11)
Message 2 of 12

kzD1219
Collaborator
Collaborator

Just looking at one of the drawings files we have, I also need to convert text and mtext numbers by a scale of 0.3048.  That is a bit of a challenge

0 Likes
Message 3 of 12

ronjonp
Mentor
Mentor

Do you mean you need to show the value of the attribute text * 0.3048? Or is it the actual height of the attribute? Attached is a drawing how you could easily input imperial and output metric 🙂

 

0 Likes
Message 4 of 12

kzD1219
Collaborator
Collaborator

@ronjonp wrote:

Do you mean you need to show the value of the attribute text * 0.3048? Or is it the actual height of the attribute? Attached is a drawing how you could easily input imperial and output metric 🙂

 


No it has nothing to do with the property of the size of text.  More the text, mtext and attributed text value.  If it says 165.0, I actually need it to convert automatically (hopefully by a cool lisp routine) to say 50.29 instead which is the 0.3048 scale factor.

 

The attached image shows some of the numbers I am dealing with the 165.0, 32.00 and 42.33 is mtext, the 168.3 is an attributed text.  All need them to convert to their metric value in meters.  

0 Likes
Message 5 of 12

ronjonp
Mentor
Mentor
Accepted solution

Give this a try .. definitely test it!

(defun c:foo (/ lm:splitstring _putval s)
  ;; RJP » 2019-01-23
  ;; From http://www.theswamp.org/index.php?topic=53496.msg582151#msg582151
  (defun lm:splitstring	(str)
    ((lambda (l / f)
       (read
	 (strcat "(\""
		 (vl-list->string
		   (apply 'append
			  (mapcar (function (lambda (a b c)
					      (cond ((member b '(34 92))
						     (if f
						       (progn (setq f nil) (list 34 32 34 92 b))
						       (list 92 b)
						     )
						    )
						    ((or (< 47 b 58) (and (= 46 b) f (< 47 c 58)))
						     (if (or f (not a))
						       (progn (setq f t) (list b))
						       (progn (setq f t) (list 34 32 34 b))
						     )
						    )
						    (f (setq f nil) (list 34 32 34 b))
						    ((list b))
					      )
					    )
				  )
				  (cons nil l)
				  l
				  (append (cdr l) '(()))
			  )
		   )
		 )
		 "\")"
	 )
       )
     )
      (vl-string->list str)
    )
  )
  (defun _putval (o op num / _foo)
    (defun _foo	(s / _trim)
      (defun _trim (s) (vl-string-right-trim "." (vl-string-right-trim "0" s)))
      (apply 'strcat
	     (mapcar '(lambda (x)
			(cond ((/= 0 (atof x))
			       (_trim ;; RTOS can have strange results with different dimzin values!
				      (rtos (eval (op (atof x) num)) 2 1)
			       )
			      )
			      (x)
			)
		      )
		     (lm:splitstring s)
	     )
      )
    )
    (cond ((and (vlax-property-available-p o 'hasattributes) (= -1 (vlax-get o 'hasattributes)))
	   (foreach a (vlax-invoke o 'getattributes)
	     (vla-put-textstring a (_foo (vla-get-textstring a)))
	   )
	  )
	  ((vlax-property-available-p o 'textstring)
	   (vla-put-textstring o (_foo (vla-get-textstring o)))
	  )
    )
  )
  (if (setq s (ssget ":L" '((0 . "insert,mtext,text"))))
    (foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      ;; Here's your conversion formula (/ valuefound 3.28084) below
      (_putval (vlax-ename->vla-object x) / 3.28084)
    )
  )
  (princ)
) 
Message 6 of 12

kzD1219
Collaborator
Collaborator

WOW!  This will certainly help tremendously.  Thank you so very much!  It is changing the numbers that need to be changed.  The only ones that get a little odd are a couple of the mtext and it changes the text width to 0.3.  But if that is all we have to edit, that is nothing compared to calculate every number.  An easy acad filter will change those text widths.  I was a bit concerned because some of these number are in groups, but it is even picking those up.

0 Likes
Message 7 of 12

ronjonp
Mentor
Mentor

@kzD1219 wrote:

...

The only ones that get a little odd are a couple of the mtext and it changes the text width to 0.3.  

...


Glad it works for you :). I'm not sure about the textwidth changing, there isn't anything in the code that should do this.

0 Likes
Message 8 of 12

kzD1219
Collaborator
Collaborator

I would image the width thing boils down to how it was drafted and how old it may be?

 

Quick question, using this produces numbers with one number after the decimal place, is there anyway to change it to make it round to 2 decimal places.  EX. instead of 44.5 it should read 44.52?

0 Likes
Message 9 of 12

ronjonp
Mentor
Mentor
Accepted solution

See image below change the 1 to 2 for two decimals.

image.png

0 Likes
Message 10 of 12

kzD1219
Collaborator
Collaborator


@ronjonp wrote:

See image below change the 1 to 2 for two decimals.

image.png


Never would have figured that out, thanks!!
0 Likes
Message 11 of 12

ronjonp
Mentor
Mentor

You're welcome 🙂

 

Here is the code modified to set the precision prior to selecting the items.

(defun c:foo (/ lm:splitstring _putval pr s)
  ;; RJP » 2019-01-23
  ;; From http://www.theswamp.org/index.php?topic=53496.msg582151#msg582151
  (defun lm:splitstring	(str)
    ((lambda (l / f)
       (read
	 (strcat "(\""
		 (vl-list->string
		   (apply 'append
			  (mapcar (function (lambda (a b c)
					      (cond ((member b '(34 92))
						     (if f
						       (progn (setq f nil) (list 34 32 34 92 b))
						       (list 92 b)
						     )
						    )
						    ((or (< 47 b 58) (and (= 46 b) f (< 47 c 58)))
						     (if (or f (not a))
						       (progn (setq f t) (list b))
						       (progn (setq f t) (list 34 32 34 b))
						     )
						    )
						    (f (setq f nil) (list 34 32 34 b))
						    ((list b))
					      )
					    )
				  )
				  (cons nil l)
				  l
				  (append (cdr l) '(()))
			  )
		   )
		 )
		 "\")"
	 )
       )
     )
      (vl-string->list str)
    )
  )
  (defun _putval (o op num pr / _foo)
    (defun _foo	(s / _trim)
      (defun _trim (s) (vl-string-right-trim "." (vl-string-right-trim "0" s)))
      (apply 'strcat
	     (mapcar '(lambda (x)
			(cond ((/= 0 (atof x))
			       (_trim ;; RTOS can have strange results with different dimzin values!
				      (rtos (eval (op (atof x) num)) 2 pr)
			       )
			      )
			      (x)
			)
		      )
		     (lm:splitstring s)
	     )
      )
    )
    (cond ((and (vlax-property-available-p o 'hasattributes) (= -1 (vlax-get o 'hasattributes)))
	   (foreach a (vlax-invoke o 'getattributes)
	     (vla-put-textstring a (_foo (vla-get-textstring a)))
	   )
	  )
	  ((vlax-property-available-p o 'textstring)
	   (vla-put-textstring o (_foo (vla-get-textstring o)))
	  )
    )
  )
  (initget 4)
  (or (setq pr (getint "\nSet precision (<Enter> for 2): ")) (setq pr 2))
  (if (setq s (ssget ":L" '((0 . "insert,mtext,text"))))
    (foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      ;; Here's your conversion formula (/ valuefound 3.28084) below
      (_putval (vlax-ename->vla-object x) / 3.28084 pr)
    )
  )
  (princ)
)
0 Likes
Message 12 of 12

neam
Collaborator
Collaborator
Infinitely great
0 Likes