Rotate Text to line and keep in position

Rotate Text to line and keep in position

johnc11
Participant Participant
1,894 Views
5 Replies
Message 1 of 6

Rotate Text to line and keep in position

johnc11
Participant
Participant

Hi All, 

 

For the last decade I have been using a lisp to rotate text to match the angle of line but the text is kept in position (the next is not moved to a offset to the line).

Unfortunately with the last couple of version of Autocad 2022 – 2024 we have found the lisp has become unreliable. Selecting the text works fine but selecting the line can generate the error

Select point on object to label.; error: bad argument type: numberp: nil

It can be hit and miss when the error will occur. Sometime trying the same thing will work on the third go. 

 

Does anyone have another lisp that would rotate text in place by selectign the a line? Or possibly the time to review the one below. The original lisp was found here. https://www.cadtutor.net/forum/topic/560-rotate-text-to-align-line/#comment-4179 

 

Many thanks

;;  Text Rotated to the selected object angle
(defun c:TRA() (c:TextRotate2Angle))

(defun c:TextRotate2Angle (/ ss lst pt ang obj
                  get_pt_and_angle )
  (vl-load-com)


  ;;  User selection of curve object
  ;;  return pick point & average angle of curve at pick point
  (defun get_pt_and_angle (prmpt / ent p@pt parA parB pt ang)
    (if (and (setq ent (entsel prmpt))
             (not (vl-catch-all-error-p
                    (setq pt (vl-catch-all-apply
                               'vlax-curve-getClosestPointTo
                               (list (car ent) (cadr ent))
                             )
                    )
                  )
             )
        )
      (progn
        (setq ent  (car ent)
              p@pt (vlax-curve-getParamAtPoint ent pt)
              parA (max 0.0 (- p@pt 0.05))
              parB (min (+ p@pt 0.05) (vlax-curve-getEndParam ent))
              ang (angle (vlax-curve-getPointAtParam ent parA)
                         (vlax-curve-getPointAtParam ent ParB)
                  )
        )
        (list pt ang)
      )
    )
  )

  
  ;;  Get Text to align & object to alignment angle
  ;;  Text is not moved, just rotated to the alignment angle
  ;;  Object must have curve data
  (prompt "\nSelect text object to align.")
  (if (and (or (setq ss  (ssget "_+.:E:S" '((0 . "Text,Mtext"))))
               (prompt "\n**  No Text object selected.  **"))
           (or (setq lst (get_pt_and_angle "\nSelect point on object to label."))
               (prompt "\n**  Missed or no curve data for object."))
       )
    (progn
      (setq pt  (car lst)
            ;; ang (FixTextAngle (cadr lst))
            ang (cadr lst)
            obj (vlax-ename->vla-object (ssname ss 0))
      )
      (vla-put-rotation Obj ang)
    )
    
  )
  (princ)
)

 

 

 

0 Likes
1,895 Views
5 Replies
Replies (5)
Message 2 of 6

TomBeauford
Advisor
Advisor

Lee Mac's Align Text to Curve works on straight lines as well curves with nice options.

Scroll to the bottom of the page for the Program Demonstration.

 

64bit AutoCAD Map & Civil 3D 2023
Architecture Engineering & Construction Collection
2023
Windows 10 Dell i7-12850HX 2.1 Ghz 12GB NVIDIA RTX A3000 12GB Graphics Adapter
Message 3 of 6

komondormrex
Mentor
Mentor

hey there,

what object did you select when you received the error? maybe it's xline or ray or other that does not support getting curve parameters like line or pline does?

0 Likes
Message 4 of 6

johnc11
Participant
Participant

I had a look at that one, but it appears to move the text to be located along the line. I already have a version of this for alinging text to lines with a required offset. 

0 Likes
Message 5 of 6

johnc11
Participant
Participant

 Thanks komondormrex


@komondormrex wrote:

hey there,

what object did you select when you received the error? maybe it's xline or ray or other that does not support getting curve parameters like line or pline does?


I had a bit of a look and all the object are either a line of a polyline. I did notices that if I draw a new line over a line that is not working the lisp will work correctly.

Looks like there might be an issue with the lines. I believe they were imported from another program. Apart from the properties, do you know of anywhere is should look to further investigate the line?

0 Likes
Message 6 of 6

komondormrex
Mentor
Mentor

look at its dump with

(vlax-dump-object (setq e (vlax-ename->vla-object (car (entsel)))))

or dxf groups

with

(progn (foreach list_element (entget (car (entsel))) (print list_element)) (princ))

either will show what the object/entity is.

0 Likes