Jump to content

Recommended Posts

Posted

I have a selection set of inserted texts. I want to rearrange the list, ascending order of X value of insertion points.

Pl. help

Posted (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 by rlx
added sort x and sort y plus test function
  • Thanks 1
Posted

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)
)

 

  • Thanks 1
Posted
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 :danger: 

Posted
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 :danger: 

 

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.

Posted
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 :beer: / 🍵

 

🐉

  • Like 1

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...