Author Topic: Looking for closest points between two polylines  (Read 3863 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Looking for closest points between two polylines
« on: January 22, 2016, 11:24:52 AM »
I swore I saw a routine here a long time ago that found the closest points between two polylines next to each other but can't seem to find it...anyone have anything like this they'd be willing to toss our direction? 

ronjonp

  • Needs a day job
  • Posts: 7533
Re: Looking for closest points between two polylines
« Reply #1 on: January 22, 2016, 12:05:01 PM »
Perhaps THIS thread?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Looking for closest points between two polylines
« Reply #2 on: January 24, 2016, 10:19:54 AM »
For straight-segmented non-intersecting polylines only:
Code - Auto/Visual Lisp: [Select]
  1. (defun LM:nearestpolypoints ( en1 en2 )
  2.     (   (lambda ( lst / dis rtn tmp )
  3.             (setq rtn (car lst)
  4.                   dis (apply 'distance rtn)
  5.             )
  6.             (foreach itm (cdr lst)
  7.                 (if (< (setq tmp (apply 'distance itm)) dis)
  8.                     (setq dis tmp rtn itm)
  9.                 )
  10.             )
  11.             rtn
  12.         )
  13.         (apply 'append
  14.             (mapcar
  15.                '(lambda ( a b )
  16.                     (mapcar
  17.                        '(lambda ( p )
  18.                             (list (trans (cdr p) a 0)
  19.                                   (vlax-curve-getclosestpointto b (trans (cdr p) a 0))
  20.                             )
  21.                         )
  22.                         (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget a))
  23.                     )
  24.                 )
  25.                 (list en1 en2) (list en2 en1)
  26.             )
  27.         )
  28.     )
  29. )

To test:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / ftr pl1 pl2 )
  2.     (setq ftr '((0 . "LWPOLYLINE") (-4 . "<NOT") (-4 . "<>") (42 . 0.0) (-4 . "NOT>")))
  3.     (if (and (princ "\nSelect 1st polyline: ")
  4.              (setq pl1 (ssget "_+.:E:S" ftr))
  5.              (princ "\nSelect 2nd polyline: ")
  6.              (setq pl2 (ssget "_+.:E:S" ftr))
  7.         )
  8.         (foreach pnt (LM:nearestpolypoints (ssname pl1 0) (ssname pl2 0))
  9.             (entmake (list '(0 . "POINT") (cons 10 pnt)))
  10.         )
  11.     )
  12.     (princ)
  13. )
« Last Edit: January 24, 2016, 10:43:18 AM by Lee Mac »

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Looking for closest points between two polylines
« Reply #3 on: January 24, 2016, 02:44:06 PM »
Ron that's the one bahhaha....thank you so much!

Lee nice code!

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Looking for closest points between two polylines
« Reply #4 on: January 25, 2016, 08:55:05 AM »
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.

xdcad

  • Swamp Rat
  • Posts: 522
Re: Looking for closest points between two polylines
« Reply #5 on: December 11, 2023, 09:36:03 AM »
Using the AcGe geometry library, you can easily find the shortest distance and position between any two curves.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt ()
  2.   (if (and (setq e1 (xdrx-entsel "\nPICK FIRST CURVE<EXIT>:" '((0 . "*line,arc,ellipse,circle"))))
  3.            (setq p1 (cadr e1)
  4.                  e1 (car e1)
  5.            )
  6.            (setq e2 (xdrx-entsel "\nPICK FIRST CURVE<EXIT>:" '((0 . "*line,arc,ellipse,circle"))))
  7.            (setq p2 (cadr e2)
  8.                  e2 (car e2)
  9.            )
  10.            (setq g1 (xdge::constructor e1))
  11.            (setq g2 (xdge::constructor e2))
  12.            (setq p1 (xdge::getpropertyvalue g1 "ClosestPointTo" p1))
  13.            (setq p2 (xdge::getpropertyvalue g2 "ClosestPointTo" p2))
  14.            (setq pt1 (xdge::constructor "kpointoncurve3d"))
  15.            (setq pt2 (xdge::constructor "kpointoncurve3d"))
  16.            (setq a (xdge::getpropertyvalue
  17.                      g1 "getClosestPointto" g2 pt1 pt2)
  18.            )
  19.       )
  20.     (progn
  21.       (xdrx-line-make
  22.         (setq p1 (xdge::getpropertyvalue (car a) "point"))
  23.         (setq p2 (xdge::getpropertyvalue (cadr a) "point"))
  24.       )
  25.       (xd::pnt:mark p1 2 0.008)
  26.       (xd::pnt:mark p2 2 0.008)
  27.       (xdrx-prompt (xdrx-string-format "\n####Two Curve ClosestDistance is: %.2f ####" (distance p1 p2)))
  28.     )
  29.   )
  30.   (princ)
  31. )
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

trogg

  • Bull Frog
  • Posts: 255
Re: Looking for closest points between two polylines
« Reply #6 on: December 11, 2023, 10:37:01 AM »
What is the AcGe geometry library?
And how does one reference it so this routine works?

Thanks

xdcad

  • Swamp Rat
  • Posts: 522
Re: Looking for closest points between two polylines
« Reply #7 on: December 11, 2023, 10:48:03 AM »
What is the AcGe geometry library?
And how does one reference it so this routine works?

Thanks

It is the geometry library of the AUTOCAD ARX library,
For specific help, you can download the ARX SDK and see the help file arxref.chm inside.

The XDRX API encapsulates the AcGe geometry library and allows LISP to use it.
For details, please see the sub-forum and the posts I made.

https://www.theswamp.org/index.php?board=78.0

========================

The AcGe class library is a tool class library provided for the AcDb class library, such as vector objects and matrix objects for two-dimensional and three-dimensional operations. In addition, many basic geometric objects are provided, such as points, curves, and surfaces.

The AcGe class library mainly contains two sub-sets: classes for two-dimensional calculations and classes for three-dimensional calculations. The main base classes are AcGeEntity2d and AcGeEntity3d. There are also several classes without a base class, including AcGePoint2d, AcGeVector2d, and AcGeMaterix2d. These basic classes can be used to perform many types of common operations, such as point and vector operations, multiplication and cross-multiplication between vectors. Operations between matrices. Many implementations of these subclasses make use of the operations of these base classes.

The hierarchical structure of the AcGe class library is as follows:

« Last Edit: December 11, 2023, 10:55:39 AM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net