Author Topic: Export coordinates of points in order  (Read 7473 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Export coordinates of points in order
« on: December 05, 2011, 03:36:25 PM »
hello .

I have a problem with exporting coordinates of points to a txt file ( but according to their coordinates from bottom to top ) .

I couldn't come up with any code with my poor knowledge of programming . :oops:

Is that possible ?

Thanks in advance

danallen

  • Guest
Re: Export coordinates of points in order
« Reply #1 on: December 05, 2011, 03:46:07 PM »
Can you bring into excel and just do a sort on the Y column?

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Export coordinates of points in order
« Reply #2 on: December 05, 2011, 06:46:57 PM »
vl-sort

Coder

  • Swamp Rat
  • Posts: 827
Re: Export coordinates of points in order
« Reply #3 on: December 05, 2011, 11:38:20 PM »
Thanks .

I tried vl-sort function but it needs a list ( according to what I read from help ) and my routine is not working .....

Please take a look .

Code: [Select]
(setq myfile (getfiled "save path" ""  "txt" 1))
(setq myfile (open myfile "w"))
(setq ponts (ssget '((0 . "POINT"))))
(setq n 0)
(repeat (sslength ponts)
  (setq sspts (ssname ponts n))
  (setq entgets (entget sspts))
  (setq xyz (cdr (assoc 10 entgets)))
  (vl-sort xyz ??????<<<- I don't know how to sort them in sequence ( the coordinates of points )

   (setq n (1+ n))
           )
  )
(close myfile)

danallen

  • Guest
Re: Export coordinates of points in order
« Reply #4 on: December 05, 2011, 11:55:21 PM »
here is a way to sort list of points

Code: [Select]
;;;=========================================================================
;;; Sort a list of 3d elements
;;;=========================================================================
;;; From: "Tony Tanzillo" <tony.tanzillo at caddzone dot com>
;;; Newsgroups: autodesk.autocad.customization
;;; Subject: Re: How can I sort a list of 3d points?
;;; Date: Wed, 19 Mar 2003 10:37:20 -0500
;;;
;;; You can use vl-sort, with a comparison function that
;;; weights the ordinates in whatever way you want.
;;;
;;; Here's an example that gives the greatest weight
;;; to the X ordinate, and the least weight to the Z
;;; ordinate:

;;; sort on three keys (x, y, and z)
;|
  (defun-q compare-points (a b / fuzz)
     (setq fuzz 1.0e-6) ;; comparison precision
     (if (equal (car a) (car b) fuzz)
        (if (equal (cadr a) (cadr b) fuzz)
           (> (caddr a) (caddr b))
           (> (cadr a) (cadr b))
        )
        (> (car a) (car b))
     )
  )

;;; example  (vl-sort <list-of-points> 'compare-points)

;;; If you search this newsgroup, you'll find a much
;;; more powerful sorting function along with a good
;;; discussion on why (vl-sort) can be very dangerous.
;;; For that reason, I suggest you replace the built-in
;;; vl-sort with this:

(defun-q vl-sort (lst func)
   (mapcar
     '(lambda (x) (nth x lst))
      (vl-sort-i lst func)
   )
)

|;
;;; This will ensure that (vl-sort) does not remove
;;; elements that it sees as equal.

;;;=========================================================================
;;; LSORT.LSP  Copyright 1992-98  Tony Tanzillo  all rights reserved
;;;
;;; ----------------------------------------------------------------
;;; Merging complex list sort
;;;
;;; LSORT.LSP implements a modified version of the classic
;;; merge sort algorithm that sorts arbitrarily-complex lists
;;; using a caller-defined relational predicate function.
;;;
;;;  (lsort <list> <OnCompare>)
;;;
;;;  <OnCompare> is a function that takes two arguments,
;;;  and returns non-nil if the first argument is greater
;;;  than the second, or nil otherwise. The arguments are
;;;  the elements of the list to be sorted. This argument
;;;  must be quoted.
;;;
;;;  The default sort order is descending. To change the
;;;  sort order to ascending, the <OnCompare> function can
;;;  return the logical complement (not) of it's result.
;;;
;;; Examples:
;;;
;;;  1.  Sort a list of coordinates on the Y-component:
;;;
;;;      Assume unsorted data is in 'UNSORTED
;;;
;;;    (setq sorted
;;;       (lsort unsorted
;;;         '(lambda (a b)
;;;             (> (cadr a) (cadr b))
;;;          )
;;;       )
;;;    )
;;;
;;;
;;;  2.  Sort a list of entity names by layer:
;;;
;;;    (setq sorted
;;;       (lsort unsorted
;;;         '(lambda (e1 e2)
;;;            (> (cdr (assoc 8 (entget e1)))
;;;               (cdr (assoc 8 (entget e2)))
;;;            )
;;;          )
;;;       )
;;;    )
;;;
;;;  3.  Sort a list of coordinates on multiple
;;;      keys (first by the X ordinate, and then
;;;      by the Y ordinate):
;;;
;;;     (setq epsilon 1e-6)
;;;
;;;     (defun-q compare-points (p1 p2)
;;;        (cond
;;;           (  (equal (car p1) (car p2) epsilon)  ; if x are equal,
;;;              (> (cadr p1) (cadr p2)))           ; then compare y,
;;;           (t (> (car p1) (car p2)))             ; else compare x
;;;        )
;;;     )
;;;
;;;     (setq sorted (lsort unsorted 'compare-points))
;;;=========================================================================
(defun-q lsort (input OnCompare / fun)
   (setq fun (cond (OnCompare) (t '>)))
   (lsort-aux input)
)

(defun-q lsort-aux (input)
   (if (cdr input)
      (  (lambda (tlist)
            (lsort-merge
               (lsort-aux (car tlist))
               (lsort-aux (cadr tlist))
            )
         )
         (lsort-split input)
      )
      input
   )
)

(defun-q lsort-split (right / left)
   (repeat (/ (length right) 2)
      (setq
         left (cons (car right) left)
         right (cdr right)
      )
   )
   (list left right)
)

(defun-q lsort-merge (left right / out)
   (while (and left right)
      (if (apply fun (list (car left) (car right)))
         (setq
            out (cons (car left) out)
            left (cdr left)
         )
         (setq
            out (cons (car right) out)
            right (cdr right)
         )
      )
   )
   (append (reverse out) left right)
)

Coder

  • Swamp Rat
  • Posts: 827
Re: Export coordinates of points in order
« Reply #5 on: December 06, 2011, 01:53:33 AM »
Thank you danallen .

I am sorry , I don't know which code to use form all of these you thankfully posted .

Would you please show me the one suits my situations ?  :-)

pBe

  • Bull Frog
  • Posts: 402
Re: Export coordinates of points in order
« Reply #6 on: December 06, 2011, 04:25:45 AM »
Code: [Select]
(Defun c:test (/ myfile ponts n sspts entgets xyz pts)
(setq myfile (getfiled "save path" ""  "txt" 1))
(setq myfile (open myfile "w"))
(setq ponts (ssget '((0 . "POINT"))))
(setq n 0)
(repeat (sslength ponts)
  (setq sspts (ssname ponts n))
  (setq entgets (entget sspts))
  (setq xyz (cdr (assoc 10 entgets)))
  (setq pts (cons xyz pts))  (setq n (1+ n))
  )
(foreach pt_
(vl-sort pts
  (function (lambda (y1 y2) (< (cadr y1) (cadr y2))))
  )

(write-line(strcat  (rtos (car pt_) 2 2)
              "," (rtos (cadr pt_) 2 2) ","
              (rtos (last pt_) 2 2 )) myfile)
  )(close myfile)
  (princ)
  )
« Last Edit: December 06, 2011, 05:15:32 AM by pBe »

Coder

  • Swamp Rat
  • Posts: 827
Re: Export coordinates of points in order
« Reply #7 on: December 06, 2011, 05:05:07 AM »
something wrong ... :-(

Code: [Select]
Error: too many arguments

pBe

  • Bull Frog
  • Posts: 402
Re: Export coordinates of points in order
« Reply #8 on: December 06, 2011, 05:18:55 AM »
I updated the post <<somehow the formatting doesnt work with my browser>
anyhoo. all i did is add this lines to your original code

(setq pts (cons xyz pts)) after (setq xyz ....

(foreach pt_
(vl-sort pts
  (function (lambda (y1 y2) (< (cadr y1) (cadr y2))))
  )

(write-line(strcat  (rtos (car pt_) 2 2)
              "," (rtos (cadr pt_) 2 2) ","
              (rtos (last pt_) 2 2 )) myfile)
  )


the text format will

27.26,-13.21,0.00
474.91,62.52,0.00
255.57,120.31,0.00

Depending on your rtos argument

Add not re-write so you may understand better yourself





« Last Edit: December 06, 2011, 05:39:08 AM by pBe »

Coder

  • Swamp Rat
  • Posts: 827
Re: Export coordinates of points in order
« Reply #9 on: December 06, 2011, 05:22:30 AM »
The same error .. / i think you just removed the codes that belong to bold chars in the routine .

Quote
Error: too many arguments
_$

pBe

  • Bull Frog
  • Posts: 402
Re: Export coordinates of points in order
« Reply #10 on: December 06, 2011, 05:35:44 AM »
The same error .. / i think you just removed the codes that belong to bold chars in the routine .

Quote
Error: too many arguments
_$

Yes thats what exactly what I did coder  :-D

try it again



Coder

  • Swamp Rat
  • Posts: 827
Re: Export coordinates of points in order
« Reply #11 on: December 06, 2011, 05:53:31 AM »
I think the problem is with vl-sort function without a list , but not sure  :realmad:

pBe

  • Bull Frog
  • Posts: 402
Re: Export coordinates of points in order
« Reply #12 on: December 06, 2011, 06:29:08 AM »
I think the problem is with vl-sort function without a list , but not sure  :realmad:

pts is the list

(setq pts (cons xyz pts))
(vl-sort pts  (function (lambda (y1 y2) (< (cadr y1) (cadr y2))))
  )

Tell you what, close your file and try again you probably have too many floating variables

..  and check (defun c:test ( /   myfile ponts n sspts entgets xyz pts) <---- "/"


ribarm

  • Gator
  • Posts: 3282
  • Marko Ribar, architect
Re: Export coordinates of points in order
« Reply #13 on: December 06, 2011, 06:32:54 AM »
pts is list of xyz points... And it is written (vl-sort pts (function (lambda ...)))... But I was ab to ask why is vl-sort function dangerous to use like its written here :-o
M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

pBe

  • Bull Frog
  • Posts: 402
Re: Export coordinates of points in order
« Reply #14 on: December 06, 2011, 06:39:48 AM »
i beleive vl-sort has the tendency to remove duplicates hence its dangerous