Modify texts in a dwg file

Modify texts in a dwg file

carlos_m_gil_p
Advocate Advocate
1,181 Views
10 Replies
Message 1 of 11

Modify texts in a dwg file

carlos_m_gil_p
Advocate
Advocate

Hello boys how are you.

You could help me with a routine to modify some texts.

I will attach a dwg so you can understand me.

Thank you.

Excuse my English, I only speak Spanish. 😞

 


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Accepted solutions (3)
1,182 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

code edited.

 

(defun c:FixNumbers ( / dz ss i ed tx)
  
  (setq dz (getvar 'dimzin))
  (setvar 'dimzin 0)
  (if (setq ss (ssget '((0 . "*TEXT") (1 . "#*"))))
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i)))))
      (entmod (append (subst (cons 1 (rtos (atof (cdr (assoc 1 ed))) 2 3))
			     (assoc 1 ed)
			     ed)
		      '((41 . 0.) (71 . 5) (72 . 5) (75 . 0))))))
  
  (setvar 'dimzin dz)
  (princ)
  )

 

0 Likes
Message 3 of 11

Kent1Cooper
Consultant
Consultant
Accepted solution

In very simplest terms, try this:

(defun C:TEST ()
  (setq
    ss (ssget ":L" '((0 . "MTEXT")))
    dec (getint "\nNumber of decimal places: ")
  )
  (repeat (setq n (sslength ss))
    (setq txt (vlax-ename->vla-object (ssname ss (setq n (1- n)))))
    (vla-put-TextString txt (rtos (atof (vla-get-TextString txt)) 2 dec))
    (vla-put-AttachmentPoint txt 5); middle-center justification
  )
)

 

Set DIMZIN to some value to include trailing zeros, such as 3 [that could be built in if you want].  It depends on their being Mtext only, not plain Text.  And it depends on their all starting with numbers -- (atof) very conveniently translates only as far as numbers go, so there's no need to search for where the x is.

Kent Cooper, AIA
0 Likes
Message 4 of 11

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

.... 

....
'((41 . 0.) (71 . 5) (72 . 5) (75 . 0)))))) ....

 

 

That may be appropriate for Mtext, but not for Text [which is permitted in the selection, but shouldn't be if this is how it changes the justification].  The justification of plain Text is stored in a combination of the 72- and 73-code entries.

Kent Cooper, AIA
Message 5 of 11

carlos_m_gil_p
Advocate
Advocate

Hello how are you.
Thanks for the help, both solutions work very well.

 

One more question.

There is some way to know which is the largest number of decimals that the selected text has.

For example:
In the file that I had attached, of the original texts the one with the highest decimals was 3 decimals.
I ask this, because it would be the value that I would use to put the number of decimals to the new texts.


Thank you.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 6 of 11

dbhunia
Advisor
Advisor

Hi

 

For.....

 

@carlos_m_gil_p wrote:

 

...................

One more question.

There is some way to know which is the largest number of decimals that the selected text has.

For example:
In the file that I had attached, of the original texts the one with the highest decimals was 3 decimals.
I ask this, because it would be the value that I would use to put the number of decimals to the new texts.

.......................


 

Try this........(For your particular type of drawings).....lightly tested

 

(defun C:CHN (/)
  (setq ss (ssget ":L" '((0 . "*TEXT"))))
  (setq dec 0)
  (repeat (setq n1 (sslength ss))
    (setq txt (vlax-ename->vla-object (ssname ss (setq n1 (1- n1)))))
    (setq NO (vla-get-TextString txt))
    (setq lst1 (parse NO "."))
    (setq DP (- (strlen (rtos (atoi (nth 1 lst1)) 2 1)) 2))
    (if (> DP dec) (setq dec DP))
  )
  (repeat (setq n (sslength ss))
    (setq txt (vlax-ename->vla-object (ssname ss (setq n (1- n)))))
    (vla-put-TextString txt (rtos (atof (vla-get-TextString txt)) 2 dec))
  )
(command "_.justifytext" ss "" "M")  
)
(defun parse (str delim / lst pos)
(setq pos (vl-string-search delim str))
   (while (> pos 0)
	(setq lst (cons (substr str 1 pos) lst)
	      str (substr str (+ pos 2))
              pos (vl-string-search delim str)
	)
   )
(if (> (strlen str) 0)(setq lst (cons str lst)))
(reverse lst)
)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 7 of 11

ВeekeeCZ
Consultant
Consultant
Accepted solution
(defun c:RoundFirstNumber ( / dz ss i ed tx mx dc)
  
  (setq dz (getvar 'dimzin))
  (setvar 'dimzin 0)
  
  (if (and (setq ss (ssget '((0 . "MTEXT") (1 . "#*"))))
           (setq mx 0)
           (repeat (setq i (sslength ss))
             (setq tx (cdr (assoc 1 (entget (ssname ss (setq i (1- i))))))
                   mx (max mx
                           (if (vl-string-search "." tx) 		; if the are decimals
                             (- (cond ((vl-string-search "x" tx))	; then substruct from x position
                                      ((strlen tx)))			;      or from last position if there is no x
                                (1+ (vl-string-search "." tx)))		;      the position of delimiter
                             0))))					; else, use 0 as no decimal
           (setq dc (cond ((getint (strcat "\nNumber of decimal places <" (itoa mx) ">: ")))
                          (mx)))
           )
    (repeat (setq i (sslength ss))
      (setq ed (entget (ssname ss (setq i (1- i)))))
      (entmod (append (subst (cons 1 (rtos (atof (cdr (assoc 1 ed))) 2 dc))
			     (assoc 1 ed)
			     ed)
		      '((41 . 0.) (71 . 5) (72 . 5) (75 . 0))))))
  
  (setvar 'dimzin dz)
  (princ)
  )
0 Likes
Message 8 of 11

carlos_m_gil_p
Advocate
Advocate

Hi brother.
Thank you very much, it works very well.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 9 of 11

carlos_m_gil_p
Advocate
Advocate

Hello dbhunia.
Thanks for your help and help too.
I did the test but it did not work.
I do not know why.
Anyway later I review it calmly.
Thanks anyway.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 10 of 11

dbhunia
Advisor
Advisor
Accepted solution

Hi

 

You are right in your drawing ......."DIMZIN"  is set to "8"...... I forgot to take care of that......

Capture.PNG

 

Now I take care of that...... Try this.......

 

(defun C:CHN (/)
  (setq DIM (getvar 'dimzin))
  (setvar 'dimzin 0)
  (setvar 'cmdecho 0)
  (setq ss (ssget ":L" '((0 . "*TEXT"))))
  (setq dec 0)
  (repeat (setq n1 (sslength ss))
    (setq txt (vlax-ename->vla-object (ssname ss (setq n1 (1- n1)))))
    (setq NO (vla-get-TextString txt))
    (setq lst1 (parse NO "."))
    (setq DP (- (strlen (rtos (atoi (nth 1 lst1)) 2 1)) 2))
    (if (> DP dec) (setq dec DP))
  )
  (repeat (setq n (sslength ss))
    (setq txt (vlax-ename->vla-object (ssname ss (setq n (1- n)))))
    (vla-put-TextString txt (rtos (atof (vla-get-TextString txt)) 2 dec))
  )
(command "_.justifytext" ss "" "M") 
(setvar 'dimzin DIM) 
(setvar 'cmdecho 1)
(princ)
)
(defun parse (str delim / lst pos)
(setq pos (vl-string-search delim str))
   (while (> pos 0)
	(setq lst (cons (substr str 1 pos) lst)
	      str (substr str (+ pos 2))
              pos (vl-string-search delim str)
	)
   )
(if (> (strlen str) 0)(setq lst (cons str lst)))
(reverse lst)
)

 

 

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 11 of 11

carlos_m_gil_p
Advocate
Advocate

Thanks for your solution.
It also works very well for me.
Greetings.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes