Rotate text according to ucs angle

Rotate text according to ucs angle

carlos_m_gil_p
Advocate Advocate
504 Views
13 Replies
Message 1 of 14

Rotate text according to ucs angle

carlos_m_gil_p
Advocate
Advocate

Hello, how are you?
I wanted to ask you a favor to improve a Lisp project I'm working on.
I've used the Plant command to rotate the screen according to the UCS.
I need to select some text so it appears horizontal, according to the UCS.
But I don't understand why it works with some text and not with others.
Here's a DWG file where I'm testing and the code I wrote.

 

(defun c:xxx (/ sel i e ang) 
  ;
  (setq sel (vl-catch-all-apply 'ssget (list '((0 . "MTEXT,TEXT")))))
  ;
  ; (setq ang (angle '(0 0 0) '(1 0 0)))
  (setq ang (angle (trans '(0.0 0.0 0.0) 1 0) (trans '(1.0 0.0 0.0) 1 0)))
  ;
  (repeat (setq i (sslength sel)) 
    (setq e (entget (ssname sel (setq i (1- i)))))
    (entmod (subst (cons 50 ang) (assoc 50 e) e)))
  ;
)


I'd appreciate any help you can give me.

 


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
505 Views
13 Replies
Replies (13)
Message 2 of 14

Sea-Haven
Mentor
Mentor

Have you looked at the 210 dxf code it may do what you want not tested.

 

(210 0.0 0.0 -1.0) is text at 0.0 angle in world UCS

(210 0.0 -1.0 0.0) is text rotated in 3d 90 about the X axis

Message 3 of 14

carlos_m_gil_p
Advocate
Advocate

Hi bro, thanks for responding.
I just checked the 210 code, but all the texts are the same.
(210 0.0 0.0 1.0)


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 4 of 14

paullimapa
Mentor
Mentor

post a sample dwg and point out the text that do work vs text that don't


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 5 of 14

ВeekeeCZ
Consultant
Consultant

You can't treat MTEXT and TEXT the same way.

 

While TEXT behaves as expected, the angle is always stored according to WCS; the MTEXT angle is, for some reason, stored according to UCS.

Message 6 of 14

Sea-Haven
Mentor
Mentor

I may be going down the wrong path, Having another look at this for text & mtext.

 

The BC is in world (210 0.0 0.0 1.0)

The DEF is in a ucs of 90 rotated about X (210 0.0 -1.0 0.0)

Using Mtext

The qwert is world (210 0.0 0.0 1.0) 

The ASDF is UCS (210 0.0 -1.0 0.0)

 

View in 3D

SeaHaven_1-1748910305943.png

 

 

Message 7 of 14

komondormrex
Mentor
Mentor

hey you there,

check the following.

(defun c:rot (/ rotation)
	(mapcar '(lambda (m_text) (if (= "AcDbText" (vla-get-objectname m_text))
				    (setq rotation (- (angle '(0 0 0) (getvar 'ucsxdir)) (vla-get-rotation m_text)))
				    (setq rotation (- (angle '(0 0 0) '(1 0 0)) (vla-get-rotation m_text)))
				  )
		   		  (vla-rotate m_text (vla-get-insertionpoint m_text) rotation)
		 )
  		 (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "MTEXT,TEXT")))))))
	)
	(princ)
)
Message 8 of 14

carlos_m_gil_p
Advocate
Advocate

Hello, how are you?
I just want all the text I select to be horizontal and read from left to right.


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 9 of 14

carlos_m_gil_p
Advocate
Advocate

Hello, how are you?
I'm just realizing right now that they don't work the same.


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 10 of 14

carlos_m_gil_p
Advocate
Advocate

Hello, how are you?
This isn't what I'm looking for right now. As I mentioned above, I just want all text and mtext to be horizontal.
It doesn't matter if I have the screen or the UCs rotated.


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 11 of 14

carlos_m_gil_p
Advocate
Advocate

Hi, how are you?
I just ran the tests, and it works great in this dwg file I posted.
But I ran a new test where I set the entire screen to normal and rotated it 90 degrees.
But when I use your function again, the text remains vertical.
Is there a way to improve that?

 

Captura de pantalla 2025-06-03 220533.jpg

I turned it from here.

 

I am attaching a new dwg where I am doing the new tests.

 


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 12 of 14

carlos_m_gil_p
Advocate
Advocate

Hey guys, I just found a routine from Mr. Lee_Mac, and it works fine.
And with lee_mac's permission, I wonder if there's a way to modify the getorient function.
It's possible to make it get the angle itself so it doesn't have to select two points on the screen.
In the tests I've run, getorient determines the direction of the text, so I always select two points horizontally from left to right, and all the text always remains horizontal.

 

(defun c:xxx (/ ss ang e eLst xAng e) 
  ; Lee Mac  ~  16.04.10[/i]
  (if 
    (and 
      (setq ss (ssget "_:L" 
                      '((-4 . "<OR")
                        (0 . "*TEXT,ATTDEF")
                        (-4 . "<AND")
                        (0 . "INSERT")
                        (66 . 1)
                        (-4 . "AND>")
                        (-4 . "OR>"))))
      (setq ang (getorient "\nSpecify Angle: ")))
    ((lambda (i) 
       (setq xAng (angle '(0.0 0.0 0.0) (trans (getvar 'UCSXDIR) 0 (trans '(0.0 0.0 1.0) 1 0 t))))
       (while (setq e (ssname ss (setq i (1+ i)))) 
         (cond 
           ((eq "INSERT" (cdr (assoc 0 (setq eLst (entget e)))))
            (while (not (eq "SEQEND" (cdr (assoc 0 (setq eLst (entget (setq e (entnext e)))))))) 
              (entmod (subst (cons 50 (+ ang xAng)) (assoc 50 eLst) eLst))))
           ((entmod (subst (cons 50 (if (eq "MTEXT" (cdr (assoc 0 eLst))) ang (+ ang xAng))) (assoc 50 eLst) eLst)))))) 
      -1))
  (princ))

 


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 13 of 14

carlos_m_gil_p
Advocate
Advocate

Hey guys, I just found a routine from @Lee_Mac and it works fine.
And with Lee_Mac's permission, I wonder if there's a way to modify the getorient function.
It's possible to make it get the angle itself so it doesn't have to select two points on the screen.
In the tests I've run, getorient determines the direction of the text, so I always select two points horizontally from left to right, and all the text always remains horizontal.

 

(defun c:xxx (/ ss ang e eLst xAng e) 
  ; Lee Mac  ~  16.04.10[/i]
  (if 
    (and 
      (setq ss (ssget "_:L" 
                      '((-4 . "<OR")
                        (0 . "*TEXT,ATTDEF")
                        (-4 . "<AND")
                        (0 . "INSERT")
                        (66 . 1)
                        (-4 . "AND>")
                        (-4 . "OR>"))))
      (setq ang (getorient "\nSpecify Angle: ")))
    ((lambda (i) 
       (setq xAng (angle '(0.0 0.0 0.0) (trans (getvar 'UCSXDIR) 0 (trans '(0.0 0.0 1.0) 1 0 t))))
       (while (setq e (ssname ss (setq i (1+ i)))) 
         (cond 
           ((eq "INSERT" (cdr (assoc 0 (setq eLst (entget e)))))
            (while (not (eq "SEQEND" (cdr (assoc 0 (setq eLst (entget (setq e (entnext e)))))))) 
              (entmod (subst (cons 50 (+ ang xAng)) (assoc 50 eLst) eLst))))
           ((entmod (subst (cons 50 (if (eq "MTEXT" (cdr (assoc 0 eLst))) ang (+ ang xAng))) (assoc 50 eLst) eLst)))))) 
      -1))
  (princ))

 


AutoCAD 2026.1.1
Visual Studio Code 1.105.1
AutoCAD AutoLISP Extension 1.6.3
Windows 10 - 22H2 (64 bits)

0 Likes
Message 14 of 14

komondormrex
Mentor
Mentor

hey, rot custom command will rotate {m}texts to align with x axle of the current ucs, which in your very case of rtext2 drawing is pretty vertical. so they surely will stay vertical. 

0 Likes