Author Topic: Two vertex distance on polyline  (Read 1838 times)

0 Members and 1 Guest are viewing this topic.

civil.eng

  • Newt
  • Posts: 66
Two vertex distance on polyline
« on: June 07, 2021, 03:08:38 AM »
Hi all, I'm writing some codes to calculate distance between two points on the polyline, :

Code: [Select]
(setq obj (vlax-ename->vla-object (car (entsel))))

  (setq pt1 (getpoint "\nPick the first point"))
  (setq pt2 (getpoint "\nPick the first point"))


  (setq ds1 (vlax-curve-getdistatpoint obj pt1))
  (setq ds2 (vlax-curve-getdistatpoint obj pt2))

  (setq ds (- ds2 ds1))

It's not working right anywhere of polyline, for example in this polyline, it gives the correct distance : (first image)

But it gives the distance from start point of the polyline, while it should calculate minimum distance between pt1 and pt2, not from startpoint : (second image)

Is it possible to write a function or correct my codes to calculate distance between two points of anywhere on the polyline?
« Last Edit: June 07, 2021, 03:23:22 AM by civil.eng »

kpblc

  • Bull Frog
  • Posts: 396
Re: Two vertex distance on polyline
« Reply #1 on: June 07, 2021, 03:35:52 AM »
Maybe someting like this?
Code - Auto/Visual Lisp: [Select]
  1. (setq obj    (car (entsel))
  2.       normal (cdr (assoc 210 (entget obj)))
  3.       obj    (vlax-ename->vla-object obj)
  4. ) ;_ end of setq
  5.  
  6. (setq pt1 (getpoint "\nPick the first point"))
  7. (setq pt2 (getpoint "\nPick the second point"))
  8.  
  9.  
  10.  
  11. (setq ds (abs (- ds2 ds1)))
Sorry for my English.

civil.eng

  • Newt
  • Posts: 66
Re: Two vertex distance on polyline
« Reply #2 on: June 07, 2021, 03:46:55 AM »
Thanks for replying, but your codes calculate from start point like my codes for second attached image, actually I want to calculate minimum distance between two points,

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Two vertex distance on polyline
« Reply #3 on: June 07, 2021, 04:13:48 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:dist-2pts-lw ( / lw p1 p2 d1 d2 )
  2.   (if (setq lw (car (entsel "\nPick CLOSED LWPOLYLINE...")))
  3.     (progn
  4.       (initget 1)
  5.       (setq p1 (getpoint "\nPick start point : "))
  6.       (setq p1 (vlax-curve-getclosestpointto lw (trans p1 1 0)))
  7.       (initget 1)
  8.       (setq p2 (getpoint "\nPick end point : "))
  9.       (setq p2 (vlax-curve-getclosestpointto lw (trans p2 1 0)))
  10.         (setq d1  (vlax-curve-getdistatpoint lw p1) d2 (vlax-curve-getdistatpoint lw p2))
  11.       )
  12.       (prompt "\nDistance is : ") (princ (rtos (- d2 d1) 2 15))
  13.     )
  14.   )
  15.   (princ)
  16. )
  17.  
« Last Edit: June 07, 2021, 04:22:58 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

civil.eng

  • Newt
  • Posts: 66
Re: Two vertex distance on polyline
« Reply #4 on: June 07, 2021, 04:45:25 AM »
Thank you, it works
Can you also adjust for this mode: (it means if the order of the first point and the second point is changed)

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Two vertex distance on polyline
« Reply #5 on: June 07, 2021, 04:53:44 AM »
Distance is calculated from first point to second one. IOW, order of point does matter... If you have straight segment between points and you are interested in that distance, you can use simple (distance p1 p2) function... Also make sure you are picking "CLOSED" LWPOLYLINE (curve) in order to get correct answer (you can do it and with "OPENED", but distance is then calculated if points are picked in order like in your picure as sum of distance from p1 to end of curve and distance from start of curve to p2)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

civil.eng

  • Newt
  • Posts: 66
Re: Two vertex distance on polyline
« Reply #6 on: June 07, 2021, 09:46:11 AM »
Polyline is closed and Distance command is not appropriate for my file, because it's possible to select two points like the image:

Isn't there any way to calculate from second point to first point ? I mean the codes gives the minimum distance between p1  to p2 and also p2 to p1.

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Two vertex distance on polyline
« Reply #7 on: June 07, 2021, 09:55:13 AM »
It is recommended to set your OSMODE to 512 to enable the Nearest option then reset it back at the end of codes.
Here is a simple example to give you a push forward.  :-D

Code - Auto/Visual Lisp: [Select]
  1. (and (setq 1p (getpoint "\nPick 1st point on Polyline : "))
  2.      (or (setq pl (ssget 1p '((0 . "LWPOLYLINE"))))
  3.          (alert "No LWpolyline detected on the picked point. Try again!")
  4.          )
  5.      (setq 1p (vlax-curve-getclosestpointto (ssname pl 0) 1p))
  6.      (setq 2p (getpoint "\nPick 2nd point on Polyline : "))
  7.      (or (setq pl (ssget 2p '((0 . "LWPOLYLINE"))))
  8.          (alert "No LWpolyline detected on the picked point. Try again!")
  9.          )
  10.      (alert (rtos (distance 1p (vlax-curve-getclosestpointto (ssname pl 0) 2p)) 2 4))
  11.      )
  12.  

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Two vertex distance on polyline
« Reply #8 on: June 07, 2021, 10:37:47 AM »
Polyline is closed and Distance command is not appropriate for my file, because it's possible to select two points like the image:

Isn't there any way to calculate from second point to first point ? I mean the codes gives the minimum distance between p1  to p2 and also p2 to p1.

It is possible, if you know what you want to get... According to your pictures, you are actually wishing not distance from p1 to p2, but smallest distance that connects p1 and p2... For that, you could try something like this :

Code - Auto/Visual Lisp: [Select]
  1. (defun c:dist-2pts-lw ( / lw p1 p2 d1 d2 dd dd1 dd2 )
  2.   (if (setq lw (car (entsel "\nPick CLOSED LWPOLYLINE...")))
  3.     (progn
  4.       (initget 1)
  5.       (setq p1 (getpoint "\nPick start point : "))
  6.       (setq p1 (vlax-curve-getclosestpointto lw (trans p1 1 0)))
  7.       (initget 1)
  8.       (setq p2 (getpoint "\nPick end point : "))
  9.       (setq p2 (vlax-curve-getclosestpointto lw (trans p2 1 0)))
  10.         (setq d1 (vlax-curve-getdistatpoint lw p1) d2 (+ dd (vlax-curve-getdistatpoint lw p2)))
  11.         (setq d1  (vlax-curve-getdistatpoint lw p1) d2 (vlax-curve-getdistatpoint lw p2))
  12.       )
  13.       (setq dd1 (- d2 d1))
  14.       (setq dd2 (- dd dd1))
  15.       (prompt "\nDistance is : ") (princ (rtos (min dd1 dd2) 2 15))
  16.     )
  17.   )
  18.   (princ)
  19. )
  20.  
« Last Edit: June 08, 2021, 03:19:53 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Two vertex distance on polyline
« Reply #9 on: June 07, 2021, 07:53:26 PM »
The distance will return a +ve or -ve answer depending on point values, so maybe use (abs (- d1 d2)
A man who never made a mistake never made anything

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: Two vertex distance on polyline
« Reply #10 on: June 08, 2021, 03:24:00 AM »
The distance will return a +ve or -ve answer depending on point values, so maybe use (abs (- d1 d2)

Just saw a mistake... Now corrected : (setq dd (vlax-curve-getdistatparam lw (vlax-curve-getendparam lw))) should have been placed before (if ... ) statement and not only in - then condition of (if)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

civil.eng

  • Newt
  • Posts: 66
Re: Two vertex distance on polyline
« Reply #11 on: June 09, 2021, 03:08:56 AM »
Thank you all, specially Tharwat and ribarm.