List to Selection Set

List to Selection Set

msarqui
Collaborator Collaborator
2,512 Views
6 Replies
Message 1 of 7

List to Selection Set

msarqui
Collaborator
Collaborator

Hello guys,

 

May I have your help please.

 

My variable LIST1 has this list ((<Entity name: 7fffb18a180> 187.09 136.426 0.0) (<Entity name: 7fffb18a1d0> 187.09 131.426 0.0) (<Entity name: 7fffb18a220> 187.09 126.426 0.0))

 

This list has texts entitys.

Is could be possible to convert this list in a selection set?

I am trying to use JUSTIFYTEXT command to change its justification but I think this command works only with a variable made by a selection set.

I have tryed the code below, but it will move the texts and I would like to maintain they aligned.

(foreach x lst1
	(setq entity   (car x)
	      dxfdata  (entget entity)
	      old-dxf  (assoc 72 dxfdata)
	      new-dxf '(72 . 1)
	      dxfdata  (subst new-dxf old-dxf dxfdata)
	)
	(entmod dxfdata)
)

Another suggestion?

Thanks!

Marcelo

0 Likes
Accepted solutions (2)
2,513 Views
6 Replies
Replies (6)
Message 2 of 7

hmsilva
Mentor
Mentor
Accepted solution

Hi Marcelo!

 

Untested...

 

(command "_.justifytext")
(foreach l lst1
   (command (car l))
   )
(command "" "_C")

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 3 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@msarqui wrote:

... 

My variable LIST1 has this list ((<Entity name: 7fffb18a180> 187.09 136.426 0.0) (<Entity name: 7fffb18a1d0> 187.09 131.426 0.0) (<Entity name: 7fffb18a220> 187.09 126.426 0.0))

....

Is could be possible to convert this list in a selection set?

 

....

Something like [untested]:

 

(setq ss (ssadd)) ; start empty

(foreach item LIST1

  (ssadd (car item) ss)

)

Kent Cooper, AIA
0 Likes
Message 4 of 7

msarqui
Collaborator
Collaborator

Hello,

 

I have tryed both ways with no success.
With Kent's suggestion, it will work if I don't declare ssN1, ssN2, ssN3 and if I put (command "_.justifytext" ssN1 "" "_MC") directly in the AutoCAD command line. But inside the routine don't work.

 

(defun c:MTT (/ ss i ss1 lst_0 lst_1 lst_2 lst_3 binlst binsort a b out bin ssN1 ssN2 ssN3)
(command "-layer" "_N" "MTT" "_C" "11" "MTT" "")				;Create a temporary layer
(setvar "cecolor" "256")
(if (setq ss (ssget '((0 . "*TEXT")(-4 . "<NOT")(62 . 1)(-4 . "NOT>"))))	;Need to avoid some texts on red color
(progn
	(command "._chprop" ss "" "color" "bylayer" "layer" "MTT" "")
	(setvar "qaflags" 1)							;http://forums.augi.com/showthread.php?11688-Explode-in-LISP-Does-Not-Explode-Selection-Set
	(command "_explode" ss)							;Explode MTEXTS to convert in TEXTS
	(setvar "qaflags" 0)
	(setq ss1 (ssget "_X" '((0 . "TEXT") (8 . "MTT") (-4 . "<NOT")(62 . 1)(-4 . "NOT>")))) ;Because the explode will change the selection set
	(repeat (setq i (sslength ss1))
	(setq lst_0 (cons (cons (setq e (ssname ss1 (setq i (1- i))))
	(cdr (assoc 10 (entget e)))
	)
	lst_0
	)
	)
	);repeat
	(setq lst_0 (vl-sort lst_0 (function (lambda (a b) (< (cadr a) (cadr b))))))

	;http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-to-separate-a-list/m-p/5985357/highlight/false#M338048
	;;Sorts a list into bins based on a comparison function and a group-by function
	;;D. C. Broad, Jr.  1/8/2016
	;To accept texts that are not perfectly aligned
	(defun binsort (lst comparefun groupbyfun)
	(groupbyfun (vl-sort lst comparefun))
	)
	(setq binlst nil
	      binlst (binsort lst_0		;data
			      '(lambda (a b) (< (cadr a) (cadr b))) ;comparefun
			      (lambda (lst_0 / out bin) ;groupbyfn
				(setq bin (cons (car lst_0) bin))
				(foreach i (cdr lst_0)
				  (if (equal (cadr i) (cadar bin) 2.0)
				    (setq bin (cons i bin))
				    (setq out (cons (reverse bin) out)
					  bin (cons i nil)
				    )
				  )
				)
				(reverse (cons bin out))
			      )
		     )
	)
	(setq	lst_1 (nth 0 binlst)
		lst_2 (nth 1 binlst)
		lst_3 (nth 2 binlst)
	)
;;;Kent
(setq ssN1 (ssadd)) ; start empty
(foreach item lst_1
  (ssadd (car item) ssN1)
)
(command "_.justifytext" ssN1 "" "_MC")

(setq ssN2 (ssadd)) ; start empty
(foreach item lst_2
  (ssadd (car item) ssN2)
)
(command "_.justifytext" ssN2 "" "_ML")

(setq ssN3 (ssadd)) ; start empty
(foreach item lst_3
  (ssadd (car item) ssN3)
)
(command "_.justifytext" ssN3 "" "_MC")

;;;Henrique
;;;(command "_.justifytext")
;;;(foreach l lst_1
;;;   (command (car l))
;;;   )
;;;(command "" "_MC")
;;;
;;;(command "_.justifytext")
;;;(foreach l lst_2
;;;   (command (car l))
;;;   )
;;;(command "" "_ML")
;;;
;;;(command "_.justifytext")
;;;(foreach l lst_3
;;;   (command (car l))
;;;   )
;;;(command "" "_MC")
  
);progn
);if
);defun

Please, see attached file.

Thanks

Marcelo

0 Likes
Message 5 of 7

hmsilva
Mentor
Mentor

Marcelo,

both approaches should work as expected, the problem was the explode command...

 

(defun c:MTT_h (/ e ss i ss1 lst_0 lst_1 lst_2 lst_3 binlst binsort a b out bin ssN1 ssN2 ssN3)
    (command "-layer" "_N" "MTT" "_C" "11" "MTT" "") ;Create a temporary layer
    (setvar "cecolor" "256")
    (if (setq ss (ssget '((0 . "*TEXT") (-4 . "<NOT") (62 . 1) (-4 . "NOT>")))) ;Need to avoid some texts on red color
        (progn
            (command "._chprop" ss "" "color" "bylayer" "layer" "MTT" "")
            (setvar "qaflags" 1) ;http://forums.augi.com/showthread.php?11688-Explode-in-LISP-Does-Not-Explode-Selection-Set
            (command "_.explode" ss "") ;Explode MTEXTS to convert in TEXTS
            (command) ; to cancel the explode command if called a second time
            (setvar "qaflags" 0)
            (setq ss1 (ssget "_X" '((0 . "TEXT") (8 . "MTT") (-4 . "<NOT") (62 . 1) (-4 . "NOT>")))) ;Because the explode will change the selection set
            (repeat (setq i (sslength ss1))
                (setq lst_0 (cons (cons (setq e (ssname ss1 (setq i (1- i))))
                                        (cdr (assoc 10 (entget e)))
                                  )
                                  lst_0
                            )
                )
            ) ;repeat
            (setq lst_0 (vl-sort lst_0 (function (lambda (a b) (< (cadr a) (cadr b))))))

 ;http://forums.autodesk.com/t5/visual-lisp-autolis?p-and-general/help-to-separate-a-list/m-p/5985357/highlight/false#M338048
 ;Sorts a list into bins based on a comparison function and a group-by function
 ;D. C. Broad, Jr.  1/8/2016
 ;To accept texts that are not perfectly aligned
            (defun binsort (lst comparefun groupbyfun)
                (groupbyfun (vl-sort lst comparefun))
            )
            (setq binlst nil
                  binlst (binsort lst_0 ;data
                                  '(lambda (a b) (< (cadr a) (cadr b))) ;comparefun
                                  (lambda (lst_0 / out bin) ;groupbyfn
                                      (setq bin (cons (car lst_0) bin))
                                      (foreach i (cdr lst_0)
                                          (if (equal (cadr i) (cadar bin) 2.0)
                                              (setq bin (cons i bin))
                                              (setq out (cons (reverse bin) out)
                                                    bin (cons i nil)
                                              )
                                          )
                                      )
                                      (reverse (cons bin out))
                                  )
                         )
            )
            (setq lst_1 (nth 0 binlst)
                  lst_2 (nth 1 binlst)
                  lst_3 (nth 2 binlst)
            )

;;; Henrique
            (command "_.justifytext")
            (foreach l lst_1
                (command (car l))
            )
            (command "" "_MC")
            (command "_.justifytext")
            (foreach l lst_2
                (command (car l))
            )
            (command "" "_ML")
            (command "_.justifytext")
            (foreach l lst_3
                (command (car l))
            )
            (command "" "_MC")
        ) ;progn
    ) ;if
    (princ)
) ;defun

(defun c:MTT_k (/ e ss i ss1 lst_0 lst_1 lst_2 lst_3 binlst binsort a b out bin ssN1 ssN2 ssN3)
    (command "-layer" "_N" "MTT" "_C" "11" "MTT" "") ;Create a temporary layer
    (setvar "cecolor" "256")
    (if (setq ss (ssget '((0 . "*TEXT") (-4 . "<NOT") (62 . 1) (-4 . "NOT>")))) ;Need to avoid some texts on red color
        (progn
            (command "._chprop" ss "" "color" "bylayer" "layer" "MTT" "")
            (setvar "qaflags" 1) ;http://forums.augi.com/showthread.php?11688-Explode-in-LISP-Does-Not-Explode-Selection-Set
            (command "_.explode" ss "") ;Explode MTEXTS to convert in TEXTS
            (command) ; to cancel the explode command if called a second time
            (setvar "qaflags" 0)
            (setq ss1 (ssget "_X" '((0 . "TEXT") (8 . "MTT") (-4 . "<NOT") (62 . 1) (-4 . "NOT>")))) ;Because the explode will change the selection set
            (repeat (setq i (sslength ss1))
                (setq lst_0 (cons (cons (setq e (ssname ss1 (setq i (1- i))))
                                        (cdr (assoc 10 (entget e)))
                                  )
                                  lst_0
                            )
                )
            ) ;repeat
            (setq lst_0 (vl-sort lst_0 (function (lambda (a b) (< (cadr a) (cadr b))))))

 ;http://forums.autodesk.com/t5/visual-lisp-autolis?p-and-general/help-to-separate-a-list/m-p/5985357/highlight/false#M338048
 ;Sorts a list into bins based on a comparison function and a group-by function
 ;D. C. Broad, Jr.  1/8/2016
 ;To accept texts that are not perfectly aligned
            (defun binsort (lst comparefun groupbyfun)
                (groupbyfun (vl-sort lst comparefun))
            )
            (setq binlst nil
                  binlst (binsort lst_0 ;data
                                  '(lambda (a b) (< (cadr a) (cadr b))) ;comparefun
                                  (lambda (lst_0 / out bin) ;groupbyfn
                                      (setq bin (cons (car lst_0) bin))
                                      (foreach i (cdr lst_0)
                                          (if (equal (cadr i) (cadar bin) 2.0)
                                              (setq bin (cons i bin))
                                              (setq out (cons (reverse bin) out)
                                                    bin (cons i nil)
                                              )
                                          )
                                      )
                                      (reverse (cons bin out))
                                  )
                         )
            )
            (setq lst_1 (nth 0 binlst)
                  lst_2 (nth 1 binlst)
                  lst_3 (nth 2 binlst)
            )
;;;Kent
            (setq ssN1 (ssadd)) ; start empty
            (foreach item lst_1
                (ssadd (car item) ssN1)
            )
            (command "_.justifytext" ssN1 "" "_MC")

            (setq ssN2 (ssadd)) ; start empty
            (foreach item lst_2
                (ssadd (car item) ssN2)
            )
            (command "_.justifytext" ssN2 "" "_ML")

            (setq ssN3 (ssadd)) ; start empty
            (foreach item lst_3
                (ssadd (car item) ssN3)
            )
            (command "_.justifytext" ssN3 "" "_MC")
        ) ;progn
    ) ;if
    (princ)
) ;defun

 

Hope this helps,
Henrique

EESignature

Message 6 of 7

Anonymous
Not applicable

Hi,

 

try this instead of justify.

This is for Mcenter (table under code)

Also i have to add; is it correct that all text will be moved to same place? and that for list 1 and 3 they have the same justification?

 

 

(setq ssN1 (ssadd))
(foreach item lst_1       (setq ent (entget (car item)))       (setq ent(subst (cons 72 1)(assoc 72 ent) ent))       (setq ent(subst (cons 73 2)(assoc 73 ent) ent))       (entmod ent) )

(setq ssN2 (ssadd)) ; start empty
(foreach item lst_2
(setq ent (entget (car item)))
(setq ent(subst (cons 72 0)(assoc 72 ent) ent))
(setq ent(subst (cons 73 2)(assoc 73 ent) ent))
(entmod ent)
)

(setq ssN3 (ssadd)) ; start empty
(foreach item lst_3
(setq ent (entget (car item)))
(setq ent(subst (cons 72 1)(assoc 72 ent) ent))
(setq ent(subst (cons 73 2)(assoc 73 ent) ent))
(entmod ent)
)



;;;Group 73 Group 72
;;; 0 1 2 3 4 5
;;;3 (top) TLeft TCenter TRight
;;;2 (middle) MLeft MCenter MRight
;;;1 (bottom) BLeft BCenter BRight
;;;0 (baseline) Left Center Right Aligned Middle Fit

 

 

 

0 Likes
Message 7 of 7

msarqui
Collaborator
Collaborator

@hmsilva wrote:

 

...the problem was the explode command...

 


Well, always learning something new.

Thanks Henrique!

0 Likes