wimal Posted March 13, 2019 Posted March 13, 2019 I have a selection set of inserted texts. I want to rearrange the list, ascending order of X value of insertion points. Pl. help Quote
rlx Posted March 13, 2019 Posted March 13, 2019 (edited) something like this? (defun sort-x ( / ss); left -> right (if (setq ss (ssget ":L" '((0 . "TEXT")))) (vl-sort (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))) (function (lambda (a b) (< (cadr (assoc 10 (entget a)))(cadr (assoc 10 (entget b))))))))) (defun sort-y ( / ss); up -> down (if (setq ss (ssget ":L" '((0 . "TEXT")))) (vl-sort (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))) (function (lambda (a b) (> (caddr (assoc 10 (entget a)))(caddr (assoc 10 (entget b))))))))) (defun c:t1 ( / i) (setq i 0) (mapcar '(lambda (x) (princ (strcat "\nX" (itoa (setq i (1+ i))) " = " (rtos (cadr (assoc 10 (entget x))) 2 2)))) (sort-x)) (princ) ) (defun c:t2 ( / i) (setq i 0) (mapcar '(lambda (x) (princ (strcat "\nY" (itoa (setq i (1+ i))) " = " (rtos (caddr (assoc 10 (entget x))) 2 2)))) (sort-y)) (princ) ) Edited March 13, 2019 by rlx added sort x and sort y plus test function 1 Quote
Lee Mac Posted March 13, 2019 Posted March 13, 2019 Reducing the number of operations performed by the sorting function will significantly improve the performance of the program, since the data used for the sort need only be extracted from each entity once, as opposed to for each comparison - as such, I would suggest the following: (defun c:sorttextbyx ( / e i l s x ) (if (setq s (ssget '((0 . "TEXT")))) (progn (repeat (setq i (sslength s)) (setq i (1- i) e (ssname s i) l (cons e l) x (cons (cadr (assoc 10 (entget e))) x) ) ) (foreach n (vl-sort-i x '<) ;; Do something with the sorted result - (princ (strcat "\nX-Coord: " (rtos (nth n x)) "\tContent: " (cdr (assoc 1 (entget (nth n l)))))) ) ) ) (princ) ) 1 Quote
rlx Posted March 13, 2019 Posted March 13, 2019 21 minutes ago, Lee Mac said: Reducing the number of operations performed by the sorting function will significantly improve the performance of the program, since the data used for the sort need only be extracted from each entity once, as opposed to for each comparison - as such, I would suggest the following: I know about your dislike for the ssnamex construction and normally use the sslength structure myself , just wanted shorter code... please forgive me master Lee Quote
Lee Mac Posted March 13, 2019 Posted March 13, 2019 4 hours ago, rlx said: I know about your dislike for the ssnamex construction and normally use the sslength structure myself , just wanted shorter code... please forgive me master Lee It's nothing personal @rlx, nothing need be forgiven! My comments pertain to the operations performed by the sorting function, as opposed to your use of ssnamex. Quote
rlx Posted March 13, 2019 Posted March 13, 2019 4 minutes ago, Lee Mac said: It's nothing personal @rlx, nothing need be forgiven! My comments pertain to the operations performed by the sorting function, as opposed to your use of ssnamex. You're quite right that your sslength construct is faster especially/mainly because of the one time only use of entget , no doubt about that. Just wanted to create a 'one-liner' with as few variables as possible. Also looked at it from the perspective of theory v.s. practice. How many text can you manually select / fit in one drawing to be still practical. A couple of dozen? The gain in performance wouldn't be that great , not enough time to get a cup of tea. But when using ssget "x" and maybe hundreds or thousands of drawings like I usually have to deal with , every millisecond counts and your's would be superior (of course) , you're the man / 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.