;;;
;;; convert multiple text entities to multiple mtext entities maintaining the insertion point
;;;

;;; basically create a selection set of the entities to change, extract all the relevant information 
;;;    ie size, style, rotation angle, insertion point, justification
;;; erase the text and create a piece of mtext
;;;




(defun C:Tx2M (/ ss itm elist H V hnd ent ins)


;;;
;;;--------------------------------------
;;; convert acad polar angle to a bearing
;;; polar angle must be in decimal degrees
;;;______________________________________
;;;
(defun polar_ang_to_bng (a)
  (setq a (- 450 a) )
  (if (>= a 360)
         (setq a (- a 360)) (setq a a) 
  ) ;if
) ; 




	(prompt "\nSelect Text objects to convert to MText: ")
	(setq ss (ssget (list (cons 0 "TEXT"))))
	(setq itm 0)
	(if	ss
		(repeat	(sslength ss)
			
			(setq hnd (ssname ss itm)) ; get the handle for itm
			(setq ent (entget hnd))		 ; 

			(setq 
		    en_type   (cdr (assoc 0  ent))
		    en_txt    (cdr (assoc 1  ent))	; the text value
		    en_lyr    (cdr (assoc 8  ent))	; the layer the text is in
		    en_size   (cdr (assoc 40 ent))	; text size
		    en_ang    (cdr (assoc 50 ent))	; text angle
		    en_style  (cdr (assoc 7  ent))	; text style
				
				en_H 			(cdr (assoc 72 ent)) ; get the horizontal alignment of the text
				en_V			(cdr (assoc 73 ent)) ; get the vertical alignment of the text
			); setq
	
			(if (and (= en_H 0) (= en_V 0))	  ; if the horiz and vert just = 0 then 
			  (setq en_ins (cdr (assoc 10 ent))) ; we have the default insertion point 
			  (setq en_ins (cdr (assoc 11 ent))) ; otherwise get the insertion point
			)
			

			(setq en_just "")

			(cond
					( (= en_V 3) (setq en_just "T" ) )
					( (= en_V 2) (setq en_just "M" ) )
					( (= en_V 1) (setq en_just "B" ) )
			)

			(cond
					( (= en_H 0) (setq en_just (strcat en_just "L" )) )
					( (= en_H 1) (setq en_just (strcat en_just "C" )) )
					( (= en_H 2) (setq en_just (strcat en_just "R" )) )
			)

;
; angle is POLAR in radians - 0 horizontal increase anticlockwise
; convert to degrees & bearings - 0 vertical increase clockwise
;
		  	(setq en_ang (/ (* en_ang 180.0) Pi) )
		  
			(setq en_ang (Polar_ang_to_Bng en_ang)) ; convert polar to bearing

			(setq en_ang (- en_ang 90.0)) ; subtract 90 because mtext is different 

		  
		  
		  	;(while (> en_ang 180.0) (setq en_ang (- en_ang 180.0)))
			  

			

			(command "ERASE" hnd "")
			(command "Layer" "Set" en_lyr "")
			(command "MTEXT" en_ins "Height" en_size "Justify" en_just "Rotation" en_ang "Style" en_style "Width" "0" en_txt "")
			
		); repeat
		
	); if
  
); function