Author Topic: Coordinates of polyline segment  (Read 9995 times)

0 Members and 1 Guest are viewing this topic.

Gliderider

  • Guest
Coordinates of polyline segment
« on: July 14, 2008, 10:26:18 AM »
Is there a way (or what is the best way) to extract the coordinates of the endpoints of a polyline segment by selecting the polyline?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Coordinates of polyline segment
« Reply #1 on: July 14, 2008, 10:34:14 AM »
There are many subroutine around. Here are two.
Code: [Select]
  ;;--------------------------------------------------------------------
  ;;  get pline vertex list for LWPline only
  (defun get_pline_cor (elst)
    (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) elst))
  )

  ;;----------------------------------------------------------------------------
   ;;  CAB 08/25/06 - revised 07.25.07
  ;;  get pline vertex list for any
   (defun getcoords (ent / endp idx pt result)
    (setq idx 0
          endp (vlax-curve-getEndParam ent))
    (while (< (setq idx (1+ idx)) endp)
      (setq pt (vlax-curve-getpointatparam ent idx))
      (setq result (cons pt result))
    )
    (reverse (cons (vlax-curve-getendpoint ent) result))
  )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Coordinates of polyline segment
« Reply #2 on: July 14, 2008, 10:35:34 AM »
Do you need help with the entire lisp or is this enough?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Gliderider

  • Guest
Re: Coordinates of polyline segment
« Reply #3 on: July 14, 2008, 11:16:26 AM »
Thanks CAB, I appreciate your help again...
At the risk of showing my ignorance, I guess I need more help; apparently it's not as simple as I hoped. Using your first subroutine, I figured how get a list of all the coordinates of the polyline, but how could I get a list of just the endpoints of the segment of the polyline I select? Is there a way to determine between which 2 vertices I've picked?

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Coordinates of polyline segment
« Reply #4 on: July 14, 2008, 11:33:35 AM »
This should do what you want:

Code: [Select]
(defun rjp-getadjacentplinevertices (ent pt / i p1 p2)
  (if (= (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
    (progn
      (setq i  (fix (vlax-curve-getParamAtPoint
      ent
      (vlax-curve-getClosestPointTo ent pt)
    )
       )
    p1 (vlax-curve-getPointAtParam ent i)
    p2 (vlax-curve-getPointAtParam ent (1+ i))
      )
      (list p1 p2)
    )
  )
)

To use:

(setq ent (entsel)
      pt  (cadr ent)
      ent (car ent)
)
(rjp-getadjacentplinevertices ent pt)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Gliderider

  • Guest
Re: Coordinates of polyline segment
« Reply #5 on: July 14, 2008, 11:48:21 AM »
Thanks all, that worked great ronjonp. Now I've got some dissecting to do.
Thanks again...

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Coordinates of polyline segment
« Reply #6 on: July 14, 2008, 12:11:27 PM »
Nice example Ron. 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Coordinates of polyline segment
« Reply #7 on: July 14, 2008, 12:37:04 PM »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Coordinates of polyline segment
« Reply #8 on: July 14, 2008, 12:37:20 PM »
Thanks all, that worked great ronjonp. Now I've got some dissecting to do.
Thanks again...

Glad to help.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

taner

  • Guest
Re: Coordinates of polyline segment
« Reply #9 on: July 15, 2008, 01:06:32 AM »
Maybe we should use it like this:
(setq ent (entsel)
      pt  (osnap(cadr ent)"nea")
      ent (car ent)
)

This should do what you want:

Code: [Select]
(defun rjp-getadjacentplinevertices (ent pt / i p1 p2)
  (if (= (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
    (progn
      (setq i  (fix (vlax-curve-getParamAtPoint
      ent
      (vlax-curve-getClosestPointTo ent pt)
    )
       )
    p1 (vlax-curve-getPointAtParam ent i)
    p2 (vlax-curve-getPointAtParam ent (1+ i))
      )
      (list p1 p2)
    )
  )
)

To use:

(setq ent (entsel)
      pt  (cadr ent)
      ent (car ent)
)
(rjp-getadjacentplinevertices ent pt)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Coordinates of polyline segment
« Reply #10 on: July 15, 2008, 05:59:59 AM »
Hi,

Quote
Maybe we should use it like this:
(setq ent (entsel)
      pt  (osnap(cadr ent)"nea")
      ent (car ent)
)

As far as I tested, the way to get a point on the polyline have  to be different according to polyline type (lw, 2d or 3d) if the polyline plane is not parallel to WCS XY plane.

Code: [Select]
(setq pt (trans (osnap (cadr ent) "_nea") 1 0))Works fine with lwpolyline and 3d polylines, but may returns a wrong point with a 2d polyline (fited or splined polyline)

Code: [Select]
(vlax-curve-getClosestPointToProjection
  (car ent)
  (trans (cadr ent) 1 0)
  (mapcar '-
  (trans (getvar "VIEWDIR") 1 0)
  (trans '(0 0 0) 1 0)
  )
)
Works with 2d and lwpolyline, but may returns a wrong point with a 3d polyline.
Speaking English as a French Frog

prexem

  • Guest
Re: Coordinates of polyline segment
« Reply #11 on: July 19, 2008, 06:33:39 PM »
Is there a way (or what is the best way) to extract the coordinates of the endpoints of a polyline segment by selecting the polyline?
Try this, with vla-objects:

Code: [Select]
(vl-load-com)
(setq e     (car (entsel "\nselect pline:"))
vla-e (vlax-ename->vla-object e)
vertexs     (vlax-get vla-e 'coordinates)
  )
8-)


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Coordinates of polyline segment
« Reply #12 on: July 20, 2008, 12:12:53 AM »
Is there a way (or what is the best way) to extract the coordinates of the endpoints of a polyline segment by selecting the polyline?
Try this, with vla-objects:

Code: [Select]
(vl-load-com)
(setq e     (car (entsel "\nselect pline:"))
vla-e (vlax-ename->vla-object e)
vertexs     (vlax-get vla-e 'coordinates)
  )
8-)
Welcome to TheSwamp prexem.

You misunderstood the request, he said "coordinates of a polyline segment "
which is two points only and not ALL the vertex points.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

prexem

  • Guest
Re: Coordinates of polyline segment
« Reply #13 on: July 21, 2008, 08:34:15 PM »

Welcome to TheSwamp prexem.

You misunderstood the request, he said "coordinates of a polyline segment "
which is two points only and not ALL the vertex points.

;|Hello CAB:
I am a neophyte in autolisp ......
To obtain coordinates of every segment, showing a list:
|;
Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun coords-segments (/     errant   *error*     e     vla-e v
c     cant  xp   c1 yp    zp    p   lp
rlp   cantp p1   p2 d
       )
  (vl-load-com)
  (defun errcs (m) (prompt "\n*end of command*"))
  (setq errant *error*
*error* errcs
  )
  (setq e
(car
   (entsel
     "\nselect a pline:"
   )
)
  )
  (if
    (or
      (equal (cdr (assoc 0 (entget e))) "POLYLINE")
      (equal (cdr (assoc 0 (entget e))) "LWPOLYLINE")
    )
     (progn
       (setq vla-e (vlax-ename->vla-object e)
     v    (vlax-get vla-e 'coordinates)
       )
       (setq c 0)
       (setq cant (/ (length v) 2))
       (repeat cant
(setq xp (nth c v)
       c1 (1+ c)
       yp (nth c1 v)
       zp 0.0
       p  (list xp yp zp)
       lp (cons p lp)
       c  (1+ c1)
)
       )
       (setq rlp (reverse lp))
       (setq cantp (length rlp))
       (setq c 0)
       (repeat cantp
(setq p1 (nth c rlp))
(setq c1 (1+ c))
(setq p2 (nth c1 rlp))
(setq d (distance p1 p2))
(setq c (1+ c))
(princ "\nsegment ")
(princ c1)
(princ ": ")
(princ d)
(princ "\nstart of segment ")
(princ c1)
(princ ": ")
(princ p1)
(princ "\nend of segment ")
(princ c1)
(princ ": ")
(princ p2)
       )
     )
     (princ "\n*THIS OBJECT IS NOT A PLINE*")
  )
  (setq *error* errant)
  (princ)
)
(defun c:cs () (coords-segments) (princ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;|Or show coordinates of only a segment:|;
Code: [Select]
(defun coords-1segment (/     errant   *error*     e     ns   vla-e
v     c     cant  xp c1    yp    zp   p
lp    rlp   cantp p1 p2    duo   lstduo
lst   duons
       )
  (vl-load-com)
  (defun errcs (m) (prompt "\n*end of command*"))
  (setq errant *error*
*error* errcs
  )
  (setq e
(car
   (entsel
     "\nselect a pline:"
   )
)
  )
  (setq ns (getint "\nnumber of segment: "))
  (if
    (or
      (equal (cdr (assoc 0 (entget e))) "POLYLINE")
      (equal (cdr (assoc 0 (entget e))) "LWPOLYLINE")
    )
     (progn
       (setq vla-e (vlax-ename->vla-object e)
     v    (vlax-get vla-e 'coordinates)
       )
       (setq c 0)
       (setq cant (/ (length v) 2))
       (repeat cant
(setq xp (nth c v)
       c1 (1+ c)
       yp (nth c1 v)
       zp 0.0
       p  (list xp yp zp)
       lp (cons p lp)
       c  (1+ c1)
)
       )
       (setq rlp (reverse lp))
       (setq cantp (length rlp))
       (setq c 0)
       (repeat cantp
(setq p1 (nth c rlp))
(setq c1 (1+ c))
(setq p2 (nth c1 rlp))
(setq duo (list p1 p2))
(setq lstduo (cons duo lstduo))
(setq c (1+ c))
       )
       (setq lst (reverse lstduo))
       (setq duons (nth (1- ns) lst))
       (princ "\ncoordinates of segment: ")
       (princ duons)
     )
     (princ "\n*THIS OBJECT IS NOT A PLINE*")
  )
  (setq *error* errant)
  (princ)
)
(defun c:c1s () (coords-1segment) (princ))
« Last Edit: July 21, 2008, 09:02:04 PM by prexem »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Coordinates of polyline segment
« Reply #14 on: July 21, 2008, 09:12:57 PM »
prexem
Both of these sub functions return the list of vertex:
http://www.theswamp.org/index.php?topic=23951.msg289954#msg289954

This routine returns the list of vertex for a pline segment selected.
http://www.theswamp.org/index.php?topic=23951.msg289975#msg289975

Give them a try.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.