Get and Sort Polyline Start and End Coordinates

Get and Sort Polyline Start and End Coordinates

codyhornyak
Advocate Advocate
468 Views
4 Replies
Message 1 of 5

Get and Sort Polyline Start and End Coordinates

codyhornyak
Advocate
Advocate

I am working on a LISP to get and sort polyline start and end coordinates. This is what I have so far. Line 7 is giving me issues. How should I change this?

 

 

(defun C:PLINESELSET

  (/ ssMessenger i vlaObject points alldata_plines)

  (setq ssMessenger (ssget "x" '(0 . "LINE,*LWPOLYLINE")))
  (repeat (setq i (sslength ssMessenger)) ; repeat for each line found
    (setq vlaObject (vlax-ename->vla-object (ssname ssMessenger (setq i (1 - i)))))
    (setq points (vlax-get-property vlaObject 'coordinates)) ; get coordinates
    (setq alldata_plines
      (cons
        (list points)))
  ) ; repeat

  (print alldata_plines)
) ; defun PLINESELSET

 

 

One I get the start and end coordinates of a single polyline, they should be sorted from west to east. Then, once there is a list of all the polyline coordinates in the drawing, they should be sorted from north to south. For example:

 

PLINE 1 from (0,0) to (1,0)
PLINE 2 from (1,1) to (-1,1)

First, sort each coordinate pair from west to east:
PLINE 1: (0,0) to (1,0)
PLINE 2: (-1,1) to (1,1)

Then, sort each coordinate pair from north to south(can just use y-coordinate of the first coordinate in the pair:
PLINE 2: (-1,1) to (1,1)
PLINE 1: (0,0) to (1,0)

 

Hopefully this question makes sense. Thanks.

 

0 Likes
Accepted solutions (1)
469 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Here you are. Note that

* got start and end points

* made a list of these two

* sorted lst of them by X

* added a sorted lst to the overall lst

* sorted the overall lst by Y.

 

(defun c:Plinesort ( / s i e l)

  (if (setq s (ssget '((0 . "LWPOLYLINE,LINE"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i)))
	    l (cons (vl-sort (list (vlax-curve-getstartpoint e) (vlax-curve-getendpoint e))
			     '(lambda (e1 e2) (< (car e1) (car e2))))
		    l))))
  
  (vl-sort l '(lambda (e1 e2) (> (cadar e1) (cadar e2))))
  (print l)
  (princ)
  )

 

*LWPOLYLINE does not make sense. it should be *POLYLINE 

To build a list, use (setq l (cons item l)). In this case, it should be (setq l (cons (list startpt endpt) l).

 

I think I already linked this to you... Need to be familiar with THIS  group of CURVE functions. 

Message 3 of 5

codyhornyak
Advocate
Advocate

Thanks a bunch @ВeekeeCZ . I am learning, its just taking time. I have this LISP wrapped up for now, moving onto the next one shortly.

0 Likes
Message 4 of 5

JBerns
Advisor
Advisor

@codyhornyak

 

Congrats on your efforts and success.

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 5 of 5

calderg1000
Mentor
Mentor

Regards @codyhornyak 

Try this code

Sort the coordinates of lines or polylines, from East to West and from North to South.

 

(defun C:Plset (/ ssMessenger i vlaObject points)
  (setq ssMessenger (ssget "_+.:E:S" '((0 . "LINE,LWPOLYLINE")))
        vlaObject   (vlax-ename->vla-object (ssname ssMessenger 0))
  )
  (if (not (= (vla-get-Objectname vlaObject) "AcDbLine"))
    (progn
      (setq points (vlax-get vlaObject 'coordinates) ; get coordinates
            lf     (pts2 points)
      )
    )
    (progn
      (setq lf (list (vlax-get vlaObject 'startpoint)
                     (vlax-get vlaObject 'endpoint)
               )
      )
    )
  )
  (print (setq lf_e (vl-sort lf '(lambda (e1 e2) (< (car e1) (car e2))))))
                                                  ;Points, sorted by Easting-West coordinate
  (print (setq lf_n (vl-sort lf '(lambda (e1 e2) (> (cadr e1) (cadr e2))))))
                                                  ;Points, sorted by Norting-Sur coordinate
  (princ)
)                                                 ; defun PLINESELSET

(defun pts2 (lp)
  (if lp
    (cons
      (setq lpt (list (nth 0 lp) (nth 1 lp)))
      (pts2 (cddr lp))
    )
  )
)

 

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes