Text Alignment to PLINE

Text Alignment to PLINE

klapeyre64
Observer Observer
359 Views
4 Replies
Message 1 of 5

Text Alignment to PLINE

klapeyre64
Observer
Observer

Can someone please tell me why this lisp sometime moves the text to a position above the pline and sometime below the pline. (After a bit of research, it may have something to do with text justification, just a thought.)

 

(vl-load-com)
(defun c:MFL ( / ss pl i en ed et pc pt pp an fn tmp)
  
  (or *mtl-ds*
      (setq *mtl-ds* 0.))
  
  (if (and (setq ss (ssget '((0 . "LINE,LWPOLYLINE,ARC,CIRCLE,RAY,*TEXT,INSERT"))))
	   (progn
	     (while (not (progn
			   (initget "Copy")
			   (setq tmp (getdist (strcat "\nDistance from line [Copy] <" (rtos *mtl-ds* 2 2) ">: ")))
			   (cond ((not tmp))
				 ((= tmp "Copy")
				  (setq fn entmake)
				  nil)
				 ((setq *mtl-ds* tmp))))))
	     *mtl-ds*)
	   (or (and (setq pl (acet-ss-ssget-filter ss '((0 . "LINE,LWPOLYLINE,ARC,CIRCLE,RAY"))))
		    (setq pl (ssname pl 0)))
	       (and (setq pl (car (entsel "\nSelect a poline: ")))
		    (or (wcmatch (cdr (assoc 0 (entget pl))) "LINE,LWPOLYLINE,ARC,CIRCLE,RAY")
			(prompt "\nError: Wrong selection. Must be LINE,LWPOLYLINE,ARC,CIRCLE or RAY"))))
	   (setq ss (acet-ss-ssget-filter ss '((0 . "*TEXT,INSERT"))))
	   )
    (repeat (setq i (sslength ss))
      (if (setq en (ssname ss (setq i (1- i)))
		ed (entget en)
		et (cdr (assoc 0 ed))
		pc (if (and (= "TEXT" et)
			    (/= 0 (cdr (assoc 72 ed)) (cdr (assoc 73 ed))))
		     11
		     10)
		pt (cdr (assoc pc ed))
		pp (vlax-curve-getClosestPointTo pl pt)
		an (angle '(0 0 0) (vlax-curve-getFirstDeriv pl (vlax-curve-getParamAtPoint pl pp))))
	((cond (fn) (entmod))
	  (append ed
		  (list (cons pc (polar pp (+ an (/ pi 2)) *mtl-ds*))
			(cons 50 (setq an (if (= et "MTEXT")
					    (- an (angle '(0 0 0) (getvar 'UCSXDIR)))
					    an)
				       an (if (< an 0)
					    (+ an (* 2 pi))
					    an)
				       an (if (and (<= (if (= et "MTEXT")
							 an
							 (angle (trans '(0 0 0) 0 1) (trans (polar '(0 0 0) an 1) 0 1))) ; readable text mtext ucs
						       (* 1.5 pi))
						   (>  (if (= et "MTEXT")
							 an
							 (angle (trans '(0 0 0) 0 1) (trans (polar '(0 0 0) an 1) 0 1)))
						       (* 0.5 pi)))
					    (+ an pi)
					    an)))))))))
  (princ)
  )

 

0 Likes
360 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant

Post a dwg to take a look.

0 Likes
Message 3 of 5

john.uhden
Mentor
Mentor

@klapeyre64 

Without much studying, I think the confusion rests with...

... an (angle '(0 0 0) (vlax-curve-getFirstDeriv pl (vlax-curve-getParamAtPoint pl pp))))

The FirstDeriv returns a vector to determine the instantaneous angle of the pline at the parameter picked.

But plines can be drawn in one direction or the other, so without knowing the direction you don't know if the complementary angle is pointing off to the left vs. to the right or up vs. down, or NE vs. SW, etc.

You may want to solve the problem by casting a perpendicular point and seeing whether its distance from the text is greater or less than the distance from the text to the polyline.  Then it's up to your coding to decide whether to locate the text on the far side or the near side.

John F. Uhden

0 Likes
Message 4 of 5

klapeyre64
Observer
Observer

TY, I believe I understand the explanation. The lines are on a P&ID so more often than not, they are horizontal or vertical. But I do understand the direction explanation. I will look into resolving that issue now.

0 Likes
Message 5 of 5

Sea-Haven
Mentor
Mentor

Google "text Readable Autocad lisp"

0 Likes