Author Topic: Sorting by point number  (Read 918 times)

0 Members and 1 Guest are viewing this topic.

milanp

  • Newt
  • Posts: 35
Sorting by point number
« on: May 27, 2022, 02:50:44 AM »
I use this lisp to export points in the format (pt, y, x, z). Is it possible to sort points by the number of points in ascending order?
 Thanks!

Code: [Select]
defun C:eksport ()
(SETVAR "OSMODE" 0)
(setq F (OPEN (GETFILED "Sacuvaj" "g:/" "txt" 1) "w"))
(setq SSS (SSGET (list (cons 0 "POINT") (cons 8 "Detaljne_tacke"))))
(setq SSSL (SSLENGTH SSS))
(setq CONT 0)
(repeat sssl 
 
(setq ENAME3 (SSNAME SSS CONT))
(setq PPP (ENTGET ENAME3 '("Milan")))
(setq COORD (CDR (ASSOC 10 (CDR PPP))))
(setq X (CAR COORD))
(setq Y (CADR COORD))
(setq Z (CADDR COORD))
(setq NUMTXT (CDR (CADR (CAR (CDR (ASSOC -3 (CDR PPP)))))))
(WRITE-LINE (STRCAT NUMTXT "," (RTOS X 2 3) "," (RTOS Y 2 3)"," (RTOS Z 2 3)) F)
(setq CONT (1+ CONT))
 );_end repeat 
(CLOSE F)
(alert "Gotovo!")
)
« Last Edit: May 27, 2022, 02:58:27 AM by milanp »

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Sorting by point number
« Reply #1 on: May 27, 2022, 04:27:40 AM »
Ok 1st step is make a list of ptnum X Y Z etc ((1 23.345 45.678 123.45)(2 ...........

You can sort point number in a list via Vl-sort, one little hiccup is the way it sorts the number like 1 10 100 will be in order not 1-10 11-100.

There is some better sort routines out there google maybe start with Lee-mac in search.

Will try to find.

Your just taking in what ever order the selection returns.
A man who never made a mistake never made anything

milanp

  • Newt
  • Posts: 35
Re: Sorting by point number
« Reply #2 on: May 27, 2022, 05:55:30 AM »

Thanks for the prompt reply. I'll try to find it.  :-)

mhupp

  • Bull Frog
  • Posts: 250
Re: Sorting by point number
« Reply #3 on: May 27, 2022, 10:17:31 AM »
depending on how may points you have and if its a string or integer. changing 1-9 to 01-09

Code - Auto/Visual Lisp: [Select]
  1. (setq ptnum (strcat (if (< (read ptnum) 10) "0" "") ptnum)) ;if ptnum is a string
  2. (setq ptnum (strcat (if (< ptnum 10) "0" "") (itoa ptnum))) ;if ptnum is a integer
« Last Edit: May 27, 2022, 07:44:39 PM by mhupp »

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Sorting by point number
« Reply #4 on: May 27, 2022, 10:19:52 AM »
Hi,
First off, create a list consists of your data then after that sort them based on the number of each point object then finally export them to your target file format.

eg:
Code - Auto/Visual Lisp: [Select]
  1. (setq lst (cons (list NUMTXT x y z) lst))
  2. (setq lst (vl-sort lst '(lambda (j k) (< (car j) (car k)))))
  3. (foreach itm lst
  4.   (WRITE-LINE (STRCAT (car itm) "," (RTOS X 2 3) "," (RTOS Y 2 3) "," (RTOS Z 2 3)) F)
  5.   )
  6.  
« Last Edit: May 27, 2022, 10:23:24 AM by Tharwat »

milanp

  • Newt
  • Posts: 35
Re: Sorting by point number
« Reply #5 on: May 27, 2022, 11:28:55 AM »
Thanks to everyone for the instructions. I tried to export but now i get duplicate names with the same coordinates. I'm not doing something right. Here is an example of a  point file. Thanks a lot

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Sorting by point number
« Reply #6 on: May 27, 2022, 12:07:41 PM »
Give this a shot and let me know.
Code - Auto/Visual Lisp: [Select]
  1.    ;; https://www.theswamp.org/index.php?topic=57600.msg610125#msg610125
  2. (defun c:Test (/ *error* txt opn sel int lst get)
  3.   ;;------------------------------------------------------------;;
  4.   ;;    Author: Tharwat Al Choufi - Date: 27.May.2022           ;;
  5.   ;;    website: https://autolispprograms.wordpress.com         ;;
  6.   ;;------------------------------------------------------------;;
  7.   (defun *error* (m)
  8.     (and opn (close opn))
  9.     (princ "\n*Cancel*")
  10.   )
  11.   (and
  12.     (princ
  13.       "\nSelect point objects on layer 'Detaljne_tacke' to export to text file : "
  14.     )
  15.     (setq int -1
  16.           sel (ssget
  17.                 '((0 . "POINT") (8 . "Detaljne_tacke") (-3 ("Milan")))
  18.               )
  19.     )
  20.     (while (setq int (1+ int)
  21.                  ent (ssname sel int)
  22.            )
  23.       (and (setq get (entget ent '("Milan")))
  24.            (numberp
  25.              (atoi (setq inc (cdr (cadar (cdr (assoc -3 get))))))
  26.            )
  27.            (setq lst (cons (list inc (cdr (assoc 10 get))) lst))
  28.       )
  29.     )
  30.     (setq
  31.       lst (vl-sort lst
  32.                    '(lambda (j k) (< (atoi (car j)) (atoi (car k))))
  33.           )
  34.     )
  35.     (setq txt (getfiled "Sacuvaj" "g:/" "txt" 1))
  36.     (setq opn (open txt "w"))
  37.     (foreach itm lst
  38.       (write-line
  39.         (vl-string-trim
  40.           " , "
  41.           (apply
  42.             'strcat
  43.             (append (list (car itm) " , ")
  44.                     (mapcar '(lambda (u) (strcat (rtos u 2 3) " , "))
  45.                             (cadr itm)
  46.                     )
  47.             )
  48.           )
  49.         )
  50.         opn
  51.       )
  52.     )
  53.     (setq opn (close opn))
  54.   )
  55.   (princ)
  56. )
  57.  
« Last Edit: May 27, 2022, 12:29:28 PM by Tharwat »

milanp

  • Newt
  • Posts: 35
Re: Sorting by point number
« Reply #7 on: May 27, 2022, 12:24:01 PM »
Here is the txt file ....

Code: [Select]
1 , 7550478.101 , 4945540.066 , 72.56
10 , 7550485.716 , 4945515.14 , 73.47
100 , 7550526.529 , 4945458.405 , 78.121
101 , 7550530.971 , 4945452.037 , 78.027
102 , 7550530.579 , 4945451.796 , 78.097
103 , 7550532.556 , 4945450.007 , 78.201
104 , 7550532.077 , 4945449.761 , 78.229
105 , 7550533.835 , 4945447.392 , 78.425
106 , 7550532.768 , 4945447.153 , 78.45
107 , 7550534.469 , 4945444.179 , 78.647
108 , 7550533.453 , 4945443.098 , 78.805
109 , 7550534.099 , 4945441.779 , 78.838
11 , 7550483.956 , 4945514.682 , 73.564
110 , 7550533.406 , 4945439.933 , 79.059
111 , 7550531.335 , 4945437.153 , 79.283
112 , 7550529.983 , 4945438.052 , 79.401
113 , 7550529.713 , 4945436.949 , 79.421
114 , 7550527.222 , 4945437.342 , 80.178
115 , 7550523.734 , 4945433.223 , 80.636
116 , 7550525.315 , 4945433.815 , 80.392
117 , 7550524.785 , 4945432.008 , 80.26
118 , 7550526.185 , 4945433.192 , 79.959
119 , 7550527.87 , 4945433.204 , 79.789
12 , 7550478.939 , 4945514.299 , 73.632
.
.
.
.


I tried it and the points are not exported again in order ... maybe I'm not doing something properly? Thanks

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Sorting by point number
« Reply #8 on: May 27, 2022, 12:30:38 PM »
Sorry, I did not test the codes before posting, now codes updated above please try again.

milanp

  • Newt
  • Posts: 35
Re: Sorting by point number
« Reply #9 on: May 27, 2022, 12:55:38 PM »
now it works properly...

Code: [Select]
1,7550478.101,4945540.066,72.56
2,7550483.135,4945537.879,72.475
3,7550486.67,4945538.48,72.659
4,7550481.461,4945529.178,72.851
5,7550479.765,4945528.411,73.061
6,7550485.063,4945524.219,73.018
7,7550487.326,4945525.985,73.027
8,7550487.859,4945523.472,73.118
9,7550483.759,4945522.294,73.098
10,7550485.716,4945515.14,73.47
11,7550483.956,4945514.682,73.564
12,7550478.939,4945514.299,73.632
13,7550473.375,4945511.98,73.871
14,7550465.426,4945508.804,74.313
15,7550466.095,4945507.922,74.497
16,7550468.645,4945507.781,74.738
17,7550468.716,4945503.594,74.815
18,7550466.162,4945503.536,74.781
19,7550465.11,4945500.01,74.895
20,7550465.893,4945499.035,75.059
21,7550465.964,4945497.009,75.198
.
.
.

Thank you all for the suggestions and especially to you Tharwat...thank you for your time and help

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Sorting by point number
« Reply #10 on: May 27, 2022, 05:09:21 PM »
You're most welcome.