PDA

View Full Version : (code) rotate (M)TEXT based on selected entity



Mark Thomas
7th Dec 2004, 05:40 pm
Just posted this on theswamp and thought I'd share with good people here at the CADTutor Forum



(defun c:rrt (/
; local functions
getSegment get-opp-ang undobegin undoend
; local variables
ent obj obj_typ ang ans
)

;;; FUNCTION
;;; rotates the user selected (M)TEXT to the user selected
;;; entity. valid entites are light weight plines, lines
;;; and (m)text. you are given the chance to rotate the
;;; by 180 degrees after intial rotation.
;;;
;;; ARGUMENTS
;;; none
;;;
;;; USAGE
;;; enter RRT on the comand line
;;;
;;; PLATFORMS
;;; 2000+
;;;
;;; AUTHOR
;;; Copyright© 2004 Mark S. Thomas
;;; mark_AT_theswamp.org
;;;
;;; VERSION
;;; 1.0 Tue Dec 07, 2004
;;;
;;; TODO:
;;; handle text that has 'fit' justification
;;; add more entites for angle extraction
;;; more testing

(vl-load-com)

;; credit Stig Madsen
;; refer to thread titled "relaxed-curves" under the "Teach Me"
;; section of TheSwamp at www.theswamp.org/phpBB2/
(defun getSegment (obj pt / cpt eParam stParam)
(cond ((setq cpt (vlax-curve-getClosestPointTo obj pt))
(setq eParam (fix (vlax-curve-getEndParam obj)))
(if (= eParam (setq stParam (fix (vlax-curve-getParamAtPoint obj cpt))))
(setq stParam (1- stParam))
(setq eParam (1+ stParam))
)
(list eParam (vlax-curve-getPointAtParam obj stParam)
(vlax-curve-getPointAtParam obj eParam))
)
)
)

;; undo functions
(defun undobegin ()
(vla-EndUndoMark
(vlax-get-property
(vlax-get-acad-object)
'ActiveDocument
)
)
(vla-StartUndoMark
(vlax-get-property
(vlax-get-acad-object)
'ActiveDocument
)
)
)

(defun undoend ()
(vla-EndUndoMark
(vlax-get-property
(vlax-get-acad-object)
'ActiveDocument
)
)
)

;; returns the oppsite of an angle define in radians
(defun get-opp-ang (ang)
&#40;cond &#40;&#40;< ang pi&#41;&#40;+ ang pi&#41;&#41;
&#40;&#40;> ang pi&#41;&#40;- ang pi&#41;&#41;
&#40;&#40;equal ang pi&#41; 0.0&#41;
&#40;&#40;equal ang 0.0&#41; pi&#41;
&#41;
&#41;

;; ================= body of main function starts here ======================

&#40;cond &#40;&#40;setq ent &#40;entsel "\nSelect entity for alignment&#58; "&#41;&#41;
&#40;setq obj &#40;vlax-ename->vla-object &#40;car ent&#41;&#41;
obj_typ &#40;vlax-get-property obj 'ObjectName&#41;
&#41;
&#40;cond &#40;&#40;= obj_typ "AcDbPolyline"&#41;
&#40;if &#40;setq pt_lst &#40;getSegment obj &#40;last ent&#41;&#41;&#41;
&#40;setq ang &#40;angle &#40;cadr pt_lst&#41;&#40;caddr pt_lst&#41;&#41;&#41;
&#41;
&#41;
&#40;&#40;= obj_typ "AcDbLine"&#41;
&#40;setq ang &#40;vlax-get-property obj 'Angle&#41;&#41;
&#41;
&#40;&#40;= obj_typ "AcDbText"&#41;
&#40;setq ang &#40;vlax-get-property obj 'Rotation&#41;&#41;
&#41;
&#40;&#40;= obj_typ "AcDbMText"&#41;
&#40;setq ang &#40;vlax-get-property obj 'Rotation&#41;&#41;
&#41;
&#40;T &#40;alert "That's not an entity I deal with"&#41;&#41;
&#41;
&#41;

&#41;

&#40;if ang
&#40;cond &#40;&#40;setq ent &#40;car &#40;entsel "\nSelect text to align"&#41;&#41;&#41;
&#40;setq obj &#40;vlax-ename->vla-object ent&#41;
obj_typ &#40;vlax-get-property obj 'ObjectName&#41;
&#41;
&#40;cond
&#40;&#40;or &#40;= obj_typ "AcDbMText"&#41; &#40;= obj_typ "AcDbText"&#41;&#41;
&#40;undobegin&#41;
&#40;vlax-put-property obj 'Rotation ang&#41;
&#40;setq ans &#40;getstring "\nRotate 180 &#91;Y/N&#93;<N>&#58; "&#41;&#41;
&#40;if &#40;= &#40;strcase ans&#41; "Y"&#41;
&#40;vlax-put-property obj 'Rotation &#40;get-opp-ang ang&#41;&#41;
&#41;
&#40;vlax-release-object obj&#41;
&#40;undoend&#41;
&#41;

&#40;T &#40;alert "I only know how to align &#40;M&#41;TEXT, sorry! "&#41;&#41;
&#41;
&#41;
&#41;
&#41;
&#40;princ&#41;
&#41;

CADTutor
7th Dec 2004, 06:21 pm
Mark, a fine piece of work - thank you. I can think of plenty of instances where that will come in really handy 8)

Fantomas
7th Dec 2004, 06:47 pm
Really usefull routine. Thank you!