Lisp to rotate text by 2 points

Lisp to rotate text by 2 points

alexT5HUV
Explorer Explorer
1,565 Views
15 Replies
Message 1 of 16

Lisp to rotate text by 2 points

alexT5HUV
Explorer
Explorer

Hi everyone

 

I have created a lisp which is doing a few different things.

I have pt3 and pt4 set as part of the routine and mtext to be inserted. The main issue I have now is the mtext is always at 0-degree rotation inside the rectangles I create. Ideally I'd like the text to follow the direction of the longest side of the rectangle. I had tried to rotate by pt3 and pt4 which are midpoints of the short sides of the rectangle however it did not work and just stopped the lisp from repeating.

I was planning on moving the text to 'midpoint' which is something created earlier in the lisp to identify the midpoint of the rectangle created however as I have stumbled at the first hurdle I'm not sure where to go.

I added the command to rotate after the 'setq mtext (entmake (list'.

 

Below is a section of the routine with the rotation removed and as mentioned I'd like to rotate the text after the parameters for the mtext have been set

 

; Replace dimension with MText
(setq mtext (entmake
(list
'(0 . "MTEXT")
'(8 . "1A FF Beam") ; Layer name
(cons 10 (midpoint pt3 pt4)) ; Midpoint between pt3 and pt4
'(40 . 44.0) ; Text height
'(41 . 1.0) ; Relative scale factor
'(71 . 5) ; Attachment point (Middle center)
'(72 . 0) ; Justification code (Middle center)
'(7 . "7") ; Text color
'(90 . 0) ; Background fill (off)
(cons 1 string) ; Text string
'(210 0.0 0.0 1.0) ; Extrusion direction
)
)
)

0 Likes
Accepted solutions (1)
1,566 Views
15 Replies
Replies (15)
Message 2 of 16

ronjonp
Mentor
Mentor

@alexT5HUV DXF code 50 sets rotation. Add something like this to your entmake list: (cons 50 (angle pt3 pt4))

0 Likes
Message 3 of 16

alexT5HUV
Explorer
Explorer

Hi @ronjonp I had tried this previously, pt3 and pt4 are picked automatically as they are finding the midpoint of the shortest side of the rectangle and it works OK for the text being positioned at 0-degrees however when adding this into the lisp the text rotates by 350.91-degrees on my current example!

As mentioned if there is a way to rotate the text post entmake list then that would be great - I'm very new to lisp and coding all together!

0 Likes
Message 4 of 16

paullimapa
Mentor
Mentor

How’s about this

(vl-cmdf "_.Rotate" (entlast) "" (midpoint pt3 pt4) (* 180.0 (/ (angle pt3 pt4)  pi)))

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

Kent1Cooper
Consultant
Consultant

Can you post an image or a small drawing file, indicating where pt3 and pt4 are?

Kent Cooper, AIA
0 Likes
Message 6 of 16

Kent1Cooper
Consultant
Consultant

@alexT5HUV wrote:

....

'(72 . 0) ; Justification code (Middle center)
....


The >DXF Reference for Mtext< does not list 0 as a choice for the code-72 entry, nor is that about justification.  Check what you really want there, and/or whether you even need to include that at all.

Also, 7 is for the Style name, not the color.  I didn't plow through all of them, but there may be other mis-designations.  Double-check all the entries for validity, then see whether adding the 50 entry works.

Kent Cooper, AIA
0 Likes
Message 7 of 16

ronjonp
Mentor
Mentor

@alexT5HUV Post a sample drawing of the results you expect. IMO there is no reason to rotate the text after insertion when it can be placed correctly initially,

0 Likes
Message 8 of 16

alexT5HUV
Explorer
Explorer

I've sorted it all; I used an if statement to calculate if delta X or delta Y is larger between points 1 and 2 (the opposite diagonal corners of the rectangle first drawn/picked) and if Y is larger, it rotates by 90, if not it does not rotate 🙂

Pasted below if anyone runs into the same issue in future and this might help them, even if its clunky

; Rotate MTEXT by 90 degrees if delta-y is larger than delta-x
    (setq delta-x (abs (- (car pt1) (car pt2))))
    (setq delta-y (abs (- (cadr pt1) (cadr pt2))))
    
    (if (> delta-y delta-x)
        (setq mtext (entmake
                      (list
                       '(0 . "MTEXT")
                       '(8 . "1A FF Beam") ; Layer name
                       (cons 10 midpt) ; Midpoint between midpt3 and midpt4
                       '(40 . 46.0) ; Text height
                       '(41 . 1.0) ; Relative scale factor
                       '(71 . 5) ; Attachment point (Middle center)
                       '(72 . 0) ; Justification code (Middle center)
                       '(7 . "7") ; Text color
                       '(90 . 0) ; Background fill (off)
                       (cons 1 string) ; Text string
                       '(210 0.0 0.0 1.0) ; Extrusion direction
                       (cons 50 (/ pi 2.0)); Rotation angle (90 degrees)
                      )
                    )
        )
        (setq mtext (entmake
                      (list
                       '(0 . "MTEXT")
                       '(8 . "1A FF Beam") ; Layer name
                       (cons 10 midpt) ; Midpoint between midpt3 and midpt4
                       '(40 . 46.0) ; Text height
                       '(41 . 1.0) ; Relative scale factor
                       '(71 . 5) ; Attachment point (Middle center)
                       '(72 . 0) ; Justification code (Middle center)
                       '(7 . "7") ; Text color
                       '(90 . 0) ; Background fill (off)
                       (cons 1 string) ; Text string
                       '(210 0.0 0.0 1.0) ; Extrusion direction
                      )
                    )
        )
    )

 

0 Likes
Message 9 of 16

hak_vz
Advisor
Advisor

Here is the code I have been using for more than 20 years to do 2 point text (mtext) alignment

 

(defun c:align_text (/ p1 p2 e ent)
	(setq p1 (trans (getpoint "\nSelect first text align point >") 1 0))
	(setq p2 (trans (getpoint "\nSelect second text align point >")1 0))
	(if (and p1 p2)
		(setq align_text_ang (angle p1 p2))
	)
	(while (and(setq e (entsel "\nSelect text object >")))
	(setq ent (entget(car e)))
	(setq ent (subst (cons 50 align_text_ang)(assoc 50 ent) ent))
	(entmod ent)
	)
	(princ)
)

(defun c:alt nil (c:align_text))

 

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 10 of 16

alexT5HUV
Explorer
Explorer

@hak_vz 

Thanks for that mate. My main aim in my lisp was to click the two corners of the rectangle and everything else worked itself out to save processes (as we repeat this lisp many, many times).

 

Definitely helpful tho!

0 Likes
Message 11 of 16

ronjonp
Mentor
Mentor
Accepted solution

@alexT5HUV 

Glad you got it sorted. FWIW you could shorten your code to this:

 

(setq	mtext (entmake (list '(0 . "MTEXT")
			     '(8 . "1A FF Beam") ; Layer name
			     (cons 10 midpt) ; Midpoint between midpt3 and midpt4
			     '(40 . 46.0) ; Text height
			     '(41 . 1.0) ; Relative scale factor
			     '(71 . 5)	; Attachment point (Middle center)
			     '(72 . 0)	; Justification code (Middle center)
			     '(7 . "7")	; Text color
			     '(90 . 0)	; Background fill (off)
			     (cons 1 string) ; Text string
			     '(210 0.0 0.0 1.0) ; Extrusion direction
			     ;; RJP - move the IF statement to here 
			     (cons 50
				   (if (> delta-y delta-x)
				     (/ pi 2.0)
				     0
				   )
			     )		; Rotation angle (90 degrees)
		       )
	      )
  )

 

 

0 Likes
Message 12 of 16

alexT5HUV
Explorer
Explorer
Brill, thanks for that!
Just by the by, is there much difference in shortening the code, other than it looking neater?
0 Likes
Message 13 of 16

ronjonp
Mentor
Mentor

@alexT5HUV I prefer brevity, plus duplicating the entmake function with only one variable changing ( rotation ) could lead to an error later if something is added to one portion and left out of the other. My 2c.

 

0 Likes
Message 14 of 16

Kent1Cooper
Consultant
Consultant

You can shorten it further.  Get rid of the incorrect 72 and 7 entries [see Message 6 -- unless "7" is actually a Style name, in which case change the comment].  And the 210 entry is optional, defaulting to what you have, so you can omit that.

Kent Cooper, AIA
0 Likes
Message 15 of 16

ronjonp
Mentor
Mentor

@Kent1Cooper wrote:

You can shorten it further.  Get rid of the incorrect 72 and 7 entries [see Message 6 -- unless "7" is actually a Style name, in which case change the comment].  And the 210 entry is optional, defaulting to what you have, so you can omit that.


There is that too 😂 I got the impression the code they had worked so did not dig deeper into it.

0 Likes
Message 16 of 16

alexT5HUV
Explorer
Explorer

@Kent1Cooper @ronjonp You're right in that it works as it is. I'll give it a go of removing though if it's not actually doing anything! This is a mix of my limited knowledge, a bit of a help from our support and chat GPT 😂

0 Likes