@oli-123 wrote:
.... the numbers I want to convert into Mtext are always in pairs, so there won't any with 3 texts.
....
Give this a try [it worked for me in your sample drawing]:
(defun C:2T2M ; = 2 nearby Text objects {to} two-line Mtext
(/ ss1 T1 minpt maxpt LL UR ss2 T2 data1 data2 txt1 txt2)
(if (setq ss1 (ssget "_:L" '((0 . "TEXT"))))
(while (> (sslength ss1) 1); still at least 2 Text objects remaining
(setq T1 (ssname ss1 0))
(vla-getboundingbox (vlax-ename->vla-object T1) 'minpt 'maxpt)
(setq
LL (vlax-safearray->list minpt)
UR (vlax-safearray->list maxpt)
); setq
(if
(and ; finds 2 Text objects [including T1] in vicinity
(setq ss2
(ssget
"_C"
(polar LL (angle UR LL) (distance LL UR))
(polar UR (angle LL UR) (distance LL UR))
'((0 . "TEXT"))
); ssget
); setq
(= (sslength ss2) 2)
); and
(progn ; then
(setq T2 (ssname (ssdel T1 ss2) 0))
(vla-getboundingbox (vlax-ename->vla-object T2) 'minpt 'maxpt)
(setq ; collective bounding box of both
LL (mapcar 'min (vlax-safearray->list minpt) LL)
UR (mapcar 'max (vlax-safearray->list maxpt) UR)
data1 (entget T1)
data2 (entget T2)
txt1 (cdr (assoc 1 data1))
txt2 (cdr (assoc 1 data2))
); setq
(command "_.ucs" "_object" T1)
(if (minusp (cadr (trans (cdr (assoc 10 data2)) 0 1))); T2 "under" T1
(setq txtA txt1 txtB txt2); then
(setq txtA txt2 txtB txt1); else
); if
(command
"_.ucs" "_previous"
"_.mtext"
"_non" LL
"_height" (cdr (assoc 40 data1))
"_rotation" (cdr (assoc 50 data1))
"_style" (cdr (assoc 7 data1))
"_justify" "_mc" "_non" UR
(strcat txtA "\\P" txtB) ""
"_.matchprop" T1 (entlast) "" ; Layer, color etc. override(s)
"_.erase" T1 T2 ""
); command
(ssdel T1 ss1) (ssdel T2 ss1)
); progn
(ssdel T1 ss1); else [no other nearby]
); if
); while
); if
(princ)
); defun
It can very slightly shift the positions, because its line spacing is as defined in the font used, which can be different from the spacing between the original Text objects [which is not the same everywhere in the sample drawing], and also because the bounding box is affected even by the particular text content and the rotation.
It could have unexpected results if arrangement is ever non-standard, such as if the pairs do not match in angle, which could mean it might read which is on top differently from what you would expect. Or if they don't match in other properties -- it uses the one called T1 for all the characteristics [Style, Rotation, Height, Layer and any property overrides], but that could be either the upper or the lower one in a given pair, depending on the method of selection and/or the drawing order.
It could use an *error* handler to ensure setting the UCS back, and command-echo suppression, and the other typical things, but first see whether it does what you want.
Kent Cooper, AIA