Jump to content

The two closest points among multiple points


baris gunes

Recommended Posts

Hello,

 

I want to find and select the two closest points among the multiple points I will select.

 

It will be much appreciated if you give me some hints, thank you in advance.

Link to comment
Share on other sites

Draw a line between the two closest points.

 

(defun c:ordpt(/ lista_puntos list_ord)
  (setq lista_puntos nil list_ord nil)
  (setq conj_pt (ssget "_X" '((0 . "POINT"))))
  (setq n 0)
  (repeat (sslength conj_pt)
    (setq lista_puntos (cons (cdr (assoc 10 (entget (ssname conj_pt n)))) lista_puntos))
    (setq n (1+ n))
  )
  
  (while (setq pt_ini (getpoint"\nSelecciona un punto: "))
    (setq list_ord
     (vl-sort lista_puntos '(lambda (el1 el2) (< (distance pt_ini el1)(distance pt_ini el2))))
    )
    (entmake (list
           '(0 . "LINE")
           '(100 . "AcDbEntity")
               '(100 . "AcDbLine")
               (cons 10 (car list_ord))
           (cons 11 (cadr list_ord))
             )
    )
   )
  (princ)


)

  • Like 1
Link to comment
Share on other sites

On 2/16/2024 at 10:57 AM, baris gunes said:

Hello,

 

I want to find and select the two closest points among the multiple points I will select.

 

It will be much appreciated if you give me some hints, thank you in advance.

 

Edited by devitg
not need to upload
Link to comment
Share on other sites

Hello robierzo

 

Many thanks for that, works..

 

But what I need is to select the points collectively from the screen rather than one by one. Then finding the two points closest to each other among these points..

 

Baris

 

image.png.0cdd2da6aa14783a2acbefedd938b427.png

work_01.dwg

Link to comment
Share on other sites

You can also try this:

(defun c:ordpt (/ lista_puntos list_ord lista_total_dist conj_pt lista_puntos-1 el el1 coord_el d1 lista_dist pt1 pt2)
  (setq lista_puntos nil list_ord nil lista_total_dist nil)
  (prompt "\nSelecciona los puntos: ")
  (setq conj_pt (ssget '((0 . "POINT"))))
  (setq n 0)
  (repeat (sslength conj_pt)
    (setq lista_puntos (cons (ssname conj_pt n) lista_puntos));lista con los nombres de los puntos
    (setq n (1+ n))
  )
  (setq lista_puntos-1 (cdr lista_puntos))
  (setq n 0)
  (repeat (1- (length lista_puntos))
    (setq el (nth n lista_puntos))
    (setq coord_el (cdr (assoc 10 (entget el))))
    (foreach el1 lista_puntos-1
      (setq coord_el1 (cdr (assoc 10 (entget el1))))
      (setq d1 (distance coord_el coord_el1))
      (setq lista_dist (list d1 el el1))
      (setq lista_total_dist (cons lista_dist lista_total_dist))
    )
    (setq lista_puntos-1 (cdr lista_puntos-1))
    (setq n (1+ n))
  )
  ;ordenamos de menor a mayor distancia
  (setq lista_ord (vl-sort lista_total_dist '(lambda (el1 el2) (< (car el1) (car el2)))))
  ;puntos más cercanos
  (setq pt1 (cdr (assoc 10 (entget (cadar lista_ord)))))
  (setq pt2 (cdr (assoc 10 (entget (caddar lista_ord))))) 
  (entmake (list
           '(0 . "LINE")
           '(100 . "AcDbEntity")
               '(100 . "AcDbLine")
               (cons 10 pt1)
           (cons 11 pt2)
             )
  )
  (princ)
)

 

Edited by robierzo
Link to comment
Share on other sites

My solution:

(defun c:pp()
  (setq ss (ssget '((0 . "POINT"))) points (sslength ss) ps nil)
  (repeat (setq i (sslength ss))
    (setq ps (cons (cdr (assoc 10 (entget (ssname ss (setq i (1- i)))))) ps)))
  (setq dl nil)
  (setq i -1)
  (repeat (1- points)
    (setq i (1+ i) j i p1 (nth i ps))
    (repeat (- points i 1)
      (setq j (1+ j) p2 (nth j ps) dist (distance p1 p2))
      (setq dl (cons (list dist p1 p2) dl))
      )
    )
  (setq mindist (apply 'min (mapcar 'car dl)))
  (foreach x dl
    (if (= mindist (car x)) (entmake (list '(0 . "LINE") '(62 . 3) (cons 10 (cadr x)) (cons 11 (caddr x)))))
    )
  (strcat "Shortest distance= " (rtos mindist))
  )

This will output in the command line the shortest distance. Also it will connect with a green line the point pairs placed at exactly that distance from each other.

Link to comment
Share on other sites

On 2/19/2024 at 12:40 PM, robierzo said:

You can also try this:

(defun c:ordpt (/ lista_puntos list_ord lista_total_dist conj_pt lista_puntos-1 el el1 coord_el d1 lista_dist pt1 pt2)
  (setq lista_puntos nil list_ord nil lista_total_dist nil)
  (prompt "\nSelecciona los puntos: ")
  (setq conj_pt (ssget '((0 . "POINT"))))
  (setq n 0)
  (repeat (sslength conj_pt)
    (setq lista_puntos (cons (ssname conj_pt n) lista_puntos));lista con los nombres de los puntos
    (setq n (1+ n))
  )
  (setq lista_puntos-1 (cdr lista_puntos))
  (setq n 0)
  (repeat (1- (length lista_puntos))
    (setq el (nth n lista_puntos))
    (setq coord_el (cdr (assoc 10 (entget el))))
    (foreach el1 lista_puntos-1
      (setq coord_el1 (cdr (assoc 10 (entget el1))))
      (setq d1 (distance coord_el coord_el1))
      (setq lista_dist (list d1 el el1))
      (setq lista_total_dist (cons lista_dist lista_total_dist))
    )
    (setq lista_puntos-1 (cdr lista_puntos-1))
    (setq n (1+ n))
  )
  ;ordenamos de menor a mayor distancia
  (setq lista_ord (vl-sort lista_total_dist '(lambda (el1 el2) (< (car el1) (car el2)))))
  ;puntos más cercanos
  (setq pt1 (cdr (assoc 10 (entget (cadar lista_ord)))))
  (setq pt2 (cdr (assoc 10 (entget (caddar lista_ord))))) 
  (entmake (list
           '(0 . "LINE")
           '(100 . "AcDbEntity")
               '(100 . "AcDbLine")
               (cons 10 pt1)
           (cons 11 pt2)
             )
  )
  (princ)
)

 

 

 

 

Hello, Fuccaro

Thank you for the work, the lisp is successful and works as I want.

Many thanks

 

 

 

 

 

Link to comment
Share on other sites

On 2/19/2024 at 12:40 PM, robierzo said:

You can also try this:

(defun c:ordpt (/ lista_puntos list_ord lista_total_dist conj_pt lista_puntos-1 el el1 coord_el d1 lista_dist pt1 pt2)
  (setq lista_puntos nil list_ord nil lista_total_dist nil)
  (prompt "\nSelecciona los puntos: ")
  (setq conj_pt (ssget '((0 . "POINT"))))
  (setq n 0)
  (repeat (sslength conj_pt)
    (setq lista_puntos (cons (ssname conj_pt n) lista_puntos));lista con los nombres de los puntos
    (setq n (1+ n))
  )
  (setq lista_puntos-1 (cdr lista_puntos))
  (setq n 0)
  (repeat (1- (length lista_puntos))
    (setq el (nth n lista_puntos))
    (setq coord_el (cdr (assoc 10 (entget el))))
    (foreach el1 lista_puntos-1
      (setq coord_el1 (cdr (assoc 10 (entget el1))))
      (setq d1 (distance coord_el coord_el1))
      (setq lista_dist (list d1 el el1))
      (setq lista_total_dist (cons lista_dist lista_total_dist))
    )
    (setq lista_puntos-1 (cdr lista_puntos-1))
    (setq n (1+ n))
  )
  ;ordenamos de menor a mayor distancia
  (setq lista_ord (vl-sort lista_total_dist '(lambda (el1 el2) (< (car el1) (car el2)))))
  ;puntos más cercanos
  (setq pt1 (cdr (assoc 10 (entget (cadar lista_ord)))))
  (setq pt2 (cdr (assoc 10 (entget (caddar lista_ord))))) 
  (entmake (list
           '(0 . "LINE")
           '(100 . "AcDbEntity")
               '(100 . "AcDbLine")
               (cons 10 pt1)
           (cons 11 pt2)
             )
  )
  (princ)
)

 

 

 

Hello, Robierzoo

Thank you for the work, the lisp is successful and works as I want.

Many thanks

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...