@mhupp's LISP works for me, but I took a stab at it anyway.
I just tweaked the original abc.lsp, I would prefer if you would post a link to the original so I can properly credit the author.
For more information, Kent Cooper has quite a few LISPs for aligning blocks for certain and probably (M)Text, etc. (as well as sorting spacing etc.) on the Autodesk Forums, they should be easy to locate.
This worked on your provided drawing as well as one I made for test with Polylines, Lines, Blocks, Mtext, Text and Attributes.
;;; Align selected objects in X or Y direction with reference object. |
;;; |
;;; https://www.cadtutor.net/forum/topic/99091-i-need-a-lisp-to-align-blocks-and-texts-vertically/ |
;;; |
;;; Modified from the provided abc.lsp (author unknown) by SLW210 (a.k.a. Steve Wilson) |
;;; |
;;; Was horizontal only, added vertical align option, error and undo. |
;;; |
;;; *****************************************************************************************************|
(defun c:AlignXY (/ *error* OS mode ss albl
alpt alptx alpty ctr ename inpt
inptx inpty newpt olderr
)
;; Error handler
(setq olderr *error*)
(defun *error* (msg)
(if (/= msg "Function cancelled")
(princ (strcat "\nError: " msg))
)
(if OS
(setvar "OSMODE" OS)
)
(command "_.UNDO" "_End")
(setq *error* olderr)
(princ)
)
;; Save and set system vars
(setq OS (getvar "OSMODE"))
(setvar "OSMODE" 0)
;; Start UNDO group
(command "_.UNDO" "_Begin")
;; Ask user for alignment direction
(initget "Horizontal Vertical")
(setq mode (getkword "\nAlign [Horizontal/Vertical] <Horizontal>: "))
(if (null mode)
(setq mode "Horizontal")
)
;; Select objects
(princ "\nSelect blocks or text to align evenly: ")
(if (not (setq ss (ssget)))
(progn
(princ "\nNothing selected.")
(*error* "Function cancelled")
(exit)
)
)
;; Select reference object
(if (not
(setq albl (entsel "\nSelect reference text or block: "))
)
(progn
(princ "\nNo reference selected.")
(*error* "Function cancelled")
(exit)
)
)
(setq albl (car albl))
(setq alpt (cdr (assoc 10 (entget albl))))
(if (not alpt)
(progn
(princ "\nInvalid reference object.")
(*error* "Function cancelled")
(exit)
)
)
(setq alptx (car alpt))
(setq alpty (cadr alpt))
;; Loop through selection
(setq ctr 0)
(while (setq ename (ssname ss ctr))
(setq inpt (cdr (assoc 10 (entget ename))))
(if inpt
(progn
(setq inptx (car inpt))
(setq inpty (cadr inpt))
;; Decide new point
(cond
((= mode "Horizontal")
(setq newpt (list inptx alpty))
)
((= mode "Vertical")
(setq newpt (list alptx inpty))
)
)
(command "move" ename "" inpt newpt)
)
)
(setq ctr (+ ctr 1))
)
;; End UNDO group cleanly
(command "_.UNDO" "_End")
;; Restore vars and error handler
(setvar "OSMODE" OS)
(setq *error* olderr)
(prompt "\nAlignment complete.")
(princ)
)