I kept most of your code.
What I added:
- code to determine if a point is inside a closed polyline.
What the code does is cast a RAY (half a XLINE, just to one side) from said point (being the insert point of the MTEXT). If that ray touches/intersects the polyline an odd number of times, then the point is inside.
- You don't have to select the MTEXT.
You select the polyline, then the script will select all MTEXTS and only rotate the MTEXTS that are inside the polyline
(vl-load-com)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This section is all about determining if a point is inside a closed polyline
(defun c:testray ( / pt)
(drawxRay (setq pt (getpoint "\nPoint: ")) (list 1.0 0.0))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun drawxRay (pt vec)
(entmakex (list (cons 0 "RAY")
(cons 100 "AcDbEntity")
(cons 100 "AcDbRay")
(cons 10 pt)
(cons 11 vec))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Intersections - Lee Mac
;; http://www.lee-mac.com/intersectionfunctions.html
;; Returns a list of all points of intersection between two objects
;; for the given intersection mode.
;; ob1,ob2 - [vla] VLA-Objects
;; mod - [int] acextendoption enum of intersectwith method
;; acextendnone Do not extend either object
;; acextendthisentity Extend obj1 to meet obj2
;; acextendotherentity Extend obj2 to meet obj1
;; acextendboth Extend both objects
(defun LM:intersections ( ob1 ob2 mod / lst rtn )
(if (and (vlax-method-applicable-p ob1 'intersectwith)
(vlax-method-applicable-p ob2 'intersectwith)
(setq lst (vlax-invoke ob1 'intersectwith ob2 mod))
)
(repeat (/ (length lst) 3)
(setq rtn (cons (list (car lst) (cadr lst) (caddr lst)) rtn)
lst (cdddr lst)
)
)
)
(reverse rtn)
)
;; what this function does: from point pt we draw a RAY to the right. We detect intersections of the ray with the closed polyline.
;; => if the number of intersections is odd -> this means the point is inside the polygon, and the ray exits the polygon at the last intersection.
;; => if the number of intersections is even (0, 2, 4...) -> the point is outside the polygon. The ray doesn't intersect, or it enters then exits...
(defun point_inside_closed_polyline (pt pline / ray ins)
(setq ray (drawxRay pt (list 1.0 0.0)))
(setq ins (LM:intersections
(vlax-ename->vla-object pline)
(vlax-ename->vla-object ray)
acextendnone
))
;; delete the ray
(entdel ray)
(if (= 0 (rem (length ins) 2)) nil T) ;; (rem number 2) returns 1 when number is odd, 0 when even. We return T when odd, nil when even
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:RotateMtext ( / ent mtexts i mtext is_inside pt entObj mtextObj vertices longestSide longestAngle p1 p2 dist) ;; don't forget these. Local variables must be listed like this.
(setq ent (car (entsel "\nSelect enclosed polyline: ")))
;; this selects all MTEXTS
(setq mtexts (ssget "_x" (list (cons 0 "MTEXT") )))
(setq i 0)
(repeat (sslength mtexts)
;;(setq mtext (car (entsel "\nSelect MTEXT: ")))
(setq mtext (ssname mtexts i))
(setq is_inside (point_inside_closed_polyline (setq pt (cdr (assoc 10 (entget mtext)))) ent))
(if (and ent mtext is_inside)
(progn
(setq entObj (entget ent))
(setq mtextObj (entget mtext))
(if (eq (cdr (assoc 0 entObj)) "LWPOLYLINE")
(progn
(setq vertices '())
(foreach item entObj
(if (or (eq (car item) 10) (eq (car item) 11))
(setq vertices (append vertices (list (cdr item))))
)
)
(setq longestSide 0 longestAngle 0)
(repeat (- (length vertices) 1)
(setq p1 (nth 0 vertices) p2 (nth 1 vertices))
(setq vertices (cdr vertices))
(setq dist (distance p1 p2))
(if (> dist longestSide)
(progn
(setq longestSide dist)
(setq longestAngle (angle p1 p2))
)
)
)
(setq mtextObj (subst (cons 50 longestAngle) (assoc 50 mtextObj) mtextObj))
(entmod mtextObj)
(princ "\nMTEXT rotated successfully.")
)
(princ "\nSelected entity is not a polyline.")
)
)
(princ "\nInvalid selection.")
)
(setq i (+ i 1))
)
(princ)
)
Happy with this?