Lisp Text

Lisp Text

chan230984
Advocate Advocate
1,241 Views
15 Replies
Message 1 of 16

Lisp Text

chan230984
Advocate
Advocate

Hi All

I have Mtext, I want a lisp that can sort from left to right

 

Untitled.png

0 Likes
Accepted solutions (3)
1,242 Views
15 Replies
Replies (15)
Message 2 of 16

hak_vz
Advisor
Advisor
Accepted solution

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

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 16

pbejse
Mentor
Mentor

@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

 

 

Message 4 of 16

chan230984
Advocate
Advocate

@pbejse 

yes , i want the values sorted , Based on the chainage value

0 Likes
Message 5 of 16

pbejse
Mentor
Mentor
Accepted solution

@chan230984 wrote:

@pbejse 

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

 

0 Likes
Message 6 of 16

chan230984
Advocate
Advocate

@pbejse 

; error: bad argument type: stringp PI_001:
Command:

0 Likes
Message 7 of 16

pbejse
Mentor
Mentor

@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.

 

 

0 Likes
Message 8 of 16

chan230984
Advocate
Advocate

@pbejse 

Still the same error

Please Let's look at the sample file I attached.

0 Likes
Message 9 of 16

hak_vz
Advisor
Advisor
Accepted solution

@chan230984 

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

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 10 of 16

pbejse
Mentor
Mentor

@chan230984 wrote:

Still the same error


Dont worry about it then 🙂

 

0 Likes
Message 11 of 16

chan230984
Advocate
Advocate

@pbejse 

Load Lisp Command: ; error: syntax error

0 Likes
Message 12 of 16

hak_vz
Advisor
Advisor

@chan230984Edit my code above. Now it should be OK.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 13 of 16

pbejse
Mentor
Mentor

@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

 

0 Likes
Message 14 of 16

chan230984
Advocate
Advocate

@pbejse The code has been fixed. These two lisp's good work just as well. 

Thank you

Message 15 of 16

hak_vz
Advisor
Advisor

@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

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 16 of 16

Sea-Haven
Mentor
Mentor

Dont we all hate the 30 minute edit time limit so old fashioned.

0 Likes