TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: vincent.r on August 22, 2019, 12:39:43 PM

Title: Texts sorted with Y coordinates, but output is strange
Post by: vincent.r on August 22, 2019, 12:39:43 PM
I have selected all text from drawing to sort by Y axis. It gives some strange output that is some texts are on same Y axis but they sorted differently. I don't understand any logical reason.

Any help will be highly appreciated.

For your ready reference screenshot and drawing attached.

(setq ipassocvfd (ssget "_w" '(8.02218 21.63036) '(34.03070 0.86320) '((0 . "text,mtext") (1 . "IP*"))))
(repeat (setq il (sslength ipassocvfd))
  (setq fele (ssname ipassocvfd (setq il (1- il))))
  (setq Mcoord (cdr (assoc 11 (entget fele ))))
  (setq Mtag (cdr (assoc 1 (entget fele ))))
  (setq ipdata (cons fele ipdata))
  (setq ipdata (cons Mtag ipdata))
  (setq ipdata (cons (cadr Mcoord) ipdata))
  (setq ipdata (cons (car Mcoord) ipdata))
  (setq masterlist (cons ipdata masterlist))
  (setq ipdata '())
)
(setq im (length masterlist))
(setq masterlist1 masterlist)
(while (/= masterlist nil)
  (setq y1 (cadr (setq motn (car masterlist))))
  (setq listtosearchY (mapcar '(lambda (sublist) (nth 1 sublist)) masterlist))
     (repeat (length listtosearchY)
        (setq pos (vl-position y1 listtosearchY))
        (if (/= pos nil)
      (progn
        (setq listtosearchY (LM:RemoveNth pos listtosearchY))
      (setq Yax (cons (nth pos masterlist) Yax))
      (setq masterlist (LM:RemoveNth pos masterlist))
      ))
   )
     (if (/= Yax nil) (progn (setq sortedlist (cons Yax sortedlist))))
  (setq Yax nil)
)


(defun LM:RemoveNth ( n l / i )
(setq i -1)
(vl-remove-if '(lambda ( x ) (= (setq i (1+ i)) n)) l)
)
;;;
Title: Re: Texts sorted with Y coordinates, but output is strange
Post by: ronjonp on August 22, 2019, 01:02:32 PM
So what is the problem? The drawing provided has two rows of text at Y = 6.1394 and Y = 13.2007. Are you trying to sort by < X ?

Sorting by coordinates can be achieved with something like this:

Code - Auto/Visual Lisp: [Select]
  1. ;; Sort by smallest Y
  2. (vl-sort pts '(lambda (r j) (< (cadr r) (cadr j))))
  3. ;; Sort by smallest X
  4. (vl-sort pts '(lambda (r j) (< (car r) (car j))))
Title: Re: Texts sorted with Y coordinates, but output is strange
Post by: vincent.r on August 22, 2019, 01:58:34 PM
Thanks ronjonp for your quick reply.

So what is the problem? The drawing provided has two rows of text at Y = 6.1394 and Y = 13.2007.  -     Yes, you are correct. Expecting result is like this only. Kindly refer screenshot. Watch window shows two different lists for a same coordinate.

Are you trying to sort by < X ? - point of activity yet to come.

Sorting by coordinates can be achieved with something like this: - Yes but in this case I can't  retrieve entity of Y coordinate.
Title: Re: Texts sorted with Y coordinates, but output is strange
Post by: ronjonp on August 22, 2019, 02:14:55 PM
Thanks ronjonp for your quick reply.

So what is the problem? The drawing provided has two rows of text at Y = 6.1394 and Y = 13.2007.  -     Yes, you are correct. Expecting result is like this only. Kindly refer screenshot. Watch window shows two different lists for a same coordinate.

Are you trying to sort by < X ? - point of activity yet to come.

Sorting by coordinates can be achieved with something like this: - Yes but in this case I can't  retrieve entity of Y coordinate.
Sorry not following you ;/ . You might post all your code.
Make sure you are localizing your variables too .. that can cause some major headaches.

Here is a simpler way to compile your 'masterlist':
Code - Auto/Visual Lisp: [Select]
  1. (if (setq ipassocvfd (ssget '((0 . "text,mtext") (1 . "IP*"))))
  2.   (repeat (setq il (sslength ipassocvfd))
  3.     (setq fele (ssname ipassocvfd (setq il (1- il))))
  4.     (setq masterlist
  5.            (cons (list (cdr (assoc 11 (entget fele))) (cdr (assoc 1 (entget fele))) fele)
  6.                  masterlist
  7.            )
  8.     )
  9.   )
  10. )
Title: Re: Texts sorted with Y coordinates, but output is strange
Post by: vincent.r on August 23, 2019, 12:23:40 AM
yes. that's a good one.