Try this.
(defun c:amt (/ ss step i e eo ent ip mi ma dx elist minx maxy p1 te );
;Author: hak_vz
;https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5530556
;Created: Tuesday, September 7, 2021
;Posted at: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-text/td-p/10604639
(princ "\nSelect mtext objects to align horizontaly >")
(setq ss (ssget '((0 . "MTEXT"))))
(initget 7)
(setq step (getreal "\nDesired spacing between mtext objects >"))
(setq i -1)
(while (< (setq i (1+ i)) (sslength ss))
(setq e (ssname ss i))
(setq eo (vlax-ename->vla-object e))
(setq ip(cdr(assoc 10(entget e))))
(vla-getBoundingbox eo 'mi 'ma)
(setq mi (vlax-safearray->list mi))
(setq ma (vlax-safearray->list ma))
(setq dx (car(mapcar '- ma mi)))
(setq elist (cons (list ip dx e) elist))
)
(setq minx (apply 'min(mapcar 'caar elist)))
(setq maxy (apply 'max(mapcar 'cadar elist)))
(setq p1 (list minx maxy))
(setq elist (vl-sort elist '(lambda (x y) (< (distance (car x) p1)(distance (car y) p1)))))
(setq i -1)
(while (< (setq i (1+ i)) (length elist))
(setq te (nth i elist))
(setq e (last te) ent (entget e) dx (cadr te))
(setq ent (subst (cons 10 p1) (assoc 10 ent) ent))
(entmod ent)
(setq p1 (mapcar '+ p1 (list dx 0)))
(setq p1 (mapcar '+ p1 (list step 0)))
)
(princ "\nDone!")
(princ)
)
(princ "\nCommand AMT aligns multiple MTEXT object horizontally with equal spacing")
(princ)
Miljenko Hatlak
@chan230984 wrote:
Hi All
I have Mtext, I want a lisp that can sort from left to right
You want the values sorted yes? , Based on the chainage value corretct?
if not then just use TEXTALIGN
yes , i want the values sorted , Based on the chainage value
@chan230984 wrote:
yes , i want the values sorted , Based on the chainage value
Lets make it easier and sorted with station number
"PI_001" -> "PI_002" -> etc
(defun c:SAL (/ ss _NTS pt d i ent MtextColl); sort and align
(defun _NTS (v)
(vl-list->string
(Vl-remove-if-not '(lambda (n)(<= 48 n 57))
(vl-string->list v)
)
)
)
(if (and
(setq ss (ssget "_:L" '((0 . "MTEXT")(1 . "*@@_###:*"))))
(setq pt (getpoint "\nPick new location: "))
(setq d (getdist pt "\nPick distance between Mtext insertion"))
)
(progn
(repeat (setq i (sslength ss))
(setq ent (entget (setq e (ssname ss (setq i (1- i))))))
(setq MtextColl (cons
(list (_NTS (read (cdr (assoc 1 ent))))(assoc 10 ent) ent) MtextColl))
)
(setq MtextColl (vl-sort MtextColl '(lambda (a b)
(< (distof (car a))(distof (car b))))))
(while (setq a (Car MtextColl))
(entmod (subst (cons 10 pt) (cadr a) (caddr a)))
(setq pt (polar pt 0.0 d)
MtextColl (Cdr MtextColl))
)
)
)
(princ)
)
HTH
; error: bad argument type: stringp PI_001:
Command:
@chan230984 wrote:
; error: bad argument type: stringp PI_001:
Command:
I dont see that error unless you the _NTS sub is not loaded. I sugggest try copying code again.
i Attached the lips on the previous post.
Here is modified version of code that sorts mtext according to station distance. You have to tell that before.
(defun c:amt (/ string_to_list ss step i e eo ent ip mi ma dx elist minx maxy p1 te stp txt st )
;Author: hak_vz
;https://forums.autodesk.com/t5/user/viewprofilepage/user-id/5530556
;Created: Tuesday, September 7, 2021
;Posted at: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-text/td-p/10604639
(defun string_to_list ( str del / pos )
(if (setq pos (vl-string-search del str))
(cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
(list str)
)
)
(princ "\nSelect mtext objects to align horizontaly >")
(setq ss (ssget '((0 . "MTEXT"))))
(initget 7)
(setq step (getreal "\nDesired spacing between mtext objects >"))
(setq i -1)
(while (< (setq i (1+ i)) (sslength ss))
(setq e (ssname ss i))
(setq eo (vlax-ename->vla-object e))
(setq ip(cdr(assoc 10(entget e))))
(vla-getBoundingbox eo 'mi 'ma)
(setq txt (vlax-get eo 'Textstring))
(setq stp(car(string_to_list txt "\\")))
(setq stp (substr stp (1-(vl-string-position (ascii "+") stp))))
(setq stp(string_to_list txt "+"))
(setq stp (mapcar 'atof stp))
(setq st (+(* (car stp) 1000.0)(cadr stp)))
(setq mi (vlax-safearray->list mi))
(setq ma (vlax-safearray->list ma))
(setq dx (car(mapcar '- ma mi)))
(setq elist (cons (list ip dx st e) elist))
)
(setq minx (apply 'min(mapcar 'caar elist)))
(setq maxy (apply 'max(mapcar 'cadar elist)))
(setq p1 (list minx maxy))
(setq elist (vl-sort elist '(lambda (x y) (< (nth 2 x) (nth 2 y)))))
(setq i -1)
(while (< (setq i (1+ i)) (length elist))
(setq te (nth i elist))
(setq e (last te) ent (entget e) dx (cadr te))
(setq ent (subst (cons 10 p1) (assoc 10 ent) ent))
(entmod ent)
(setq p1 (mapcar '+ p1 (list dx 0)))
(setq p1 (mapcar '+ p1 (list step 0)))
)
(princ "\nDone!")
(princ)
)
(princ "\nCommand AMT aligns multiple MTEXT object horizontally with equal spacing")
(princ)
Miljenko Hatlak
Load Lisp Command: ; error: syntax error
@chan230984Edit my code above. Now it should be OK.
Miljenko Hatlak
@chan230984 wrote:Load Lisp Command: ; error: syntax error
Why did you marked it as solution if its not working for you @chan230984 ?
Unless i know that it does work for you then you can mark it as a solution.
EDIT: Removed Mark at post # 10
@pbejse I think OP has accidentally tagged you instead of me. There was an error in my code (empty setq) that caused an error.
Miljenko Hatlak