lisp to draw line between the nearest vertex of 2 polylines

lisp to draw line between the nearest vertex of 2 polylines

almora92
Contributor Contributor
1,501 Views
11 Replies
Message 1 of 12

lisp to draw line between the nearest vertex of 2 polylines

almora92
Contributor
Contributor

Hi all,

i'm new with autolisp,

 

i have a lot of couples of poly/3Dpoly with the same vertex number (generally a poly/3dpoly is the horizontal an vertical offset of another poly/3dpoly): i need to draw all the lines that join all the vertex of the first polyline with the nearest vertex of the other polyline.

 

Do you think it could be possible? do you have any tips for me?

 

 

thanks a lot in advance

0 Likes
Accepted solutions (1)
1,502 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

It sounds quite possible, assuming the 2 Polylines in question are drawn in the same direction.  The likely approach would involve (vlax-curve-getPointAtParam) functions [the vertices are at whole-number parameter values] with the same parameter value applied to both Polylines.  If they might possibly be drawn in opposite directions, that could be accounted for by a REVERSE command applied to one of them, or by counting the parameter values in opposite directions.

Kent Cooper, AIA
Message 3 of 12

Sea-Haven
Mentor
Mentor

Post a sample  dwg with a before and after.

Message 4 of 12

john.uhden
Mentor
Mentor

@almora92 ,

I started a challenge a while back to solve for the shortest distance between 2 polylines (which I think I won).  If you can find that, then maybe you can alter it to find the shortest distance between 2 vertices.  It may be that the @closer function is the trick.

If you (or @hak_vz) can't find it, I'll look into my bag of tricks.

John F. Uhden

Message 5 of 12

almora92
Contributor
Contributor

beforebeforeafterafter

0 Likes
Message 6 of 12

almora92
Contributor
Contributor

that's a simple example of what i need to do with multiple of these polylines. 'til now, in case of many polylines,  i solved the problem with excel, determining, for every vertex of a polyline, the nearest vertices of another polyline...but, obviously, it's a time-wasting and not so smart procedure; therefore I decided to try to program a lisp that would perform this task.

 

my idea for the first step is that of a lisp that does this by selecting, one by one, the pairs of polylines on which to perform this operation. And then, if necessary, improve the lisp to do everything automatically by selecting all the n polylines in the drawing together

0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant

Are these pairs created in such a way that you can count on their start/end vertices aiming in the same direction from their centers, so that if you go from the start of one to the start of the other, then from the second vertex of one to the second of the other, etc., you don't get this kind of thing?

Kent1Cooper_0-1697218433967.png

If not, some kind of figuring would need to be done to determine which vertices to connect.

 

EDIT:  And come to think of it, even when the start points align, do they always progress in the same direction, so you don't get this kind of thing?

Kent1Cooper_0-1697220839244.png

Kent Cooper, AIA
0 Likes
Message 8 of 12

almora92
Contributor
Contributor

yes, can say that the polylines are always drawn in the same direction, so the green lines must be drawn between the i-vertex of polyline 1  and i-vertex of polyline 2

0 Likes
Message 9 of 12

john.uhden
Mentor
Mentor

@almora92 ,

Wait a second.  You stated to the nearest vertex of the "other" polyline.  And unless one is offset from the other without being moved, then you stand a decent chance of getting different results depending on which you pick first.

John F. Uhden

0 Likes
Message 10 of 12

Sea-Haven
Mentor
Mentor
Accepted solution

This is very rough and expects the 2 objects to be 3dplines. Really need some true samples to test properly. You need to be in a 3d view so can pick 2 objects. Like others expects that the plines go in same direction. Very much a 1st go.

SeaHaven_0-1697250832314.png

 

 

 

; join 2 3dplines verticaly by vertices
; By AlanH Oct 2023

(defun c:wow ( / co-ords2xy obj pt1 pt2 x xyz)

; convert now to xyz
(defun co-ords2xy (xyz / i xy)
(setq co-ordsxy '())
(if (= xyz 2)
(progn
(setq I 0)
(repeat (/ (length co-ords) 2)
(setq xy (list (nth i co-ords)(nth (+ I 1) co-ords) ))
(setq co-ordsxy (cons xy co-ordsxy))
(setq I (+ I 2))
)
)
)
(if (= xyz 3)
(progn
(setq I 0)
(repeat (/ (length co-ords) 3)
(setq xy (list (nth i co-ords)(nth (+ I 1) co-ords)(nth (+ I 2) co-ords) ))
(setq co-ordsxy (cons xy co-ordsxy))
(setq I (+ I 3))
)
)
)
(princ)
)


(setvar 'osnapz 0)
(setq obj (vlax-ename->vla-object (car  (entsel "\nPick object 1 "))))
(setq co-ords (vlax-get obj 'coordinates))
(cond
  (( = (vla-get-objectname obj) "AcDb2dPolyline")(co-ords2xy 2))
  (( = (vla-get-objectname obj) "AcDb3dPolyline")(co-ords2xy 3))
)
(setq co-ords1 co-ordsxy)
(setq obj (vlax-ename->vla-object (car  (entsel "\nPick object 2 "))))
(setq co-ords (vlax-get obj 'coordinates))
(cond 
  (( = (vla-get-objectname obj) "AcDb2dPolyline")(co-ords2xy 2))
  (( = (vla-get-objectname obj) "AcDb3dPolyline")(co-ords2xy 3))
)
(setq co-ords2 co-ordsxy)
(setq x 0)
(repeat (length co-ords1)
(setq pt1 (nth x co-ords1) pt2 (nth x co-ords2))
(command "line"  pt1 pt2 "")
(setq x (1+ x))
)
(princ)
)
(c:wow)

 

 

 

Message 11 of 12

almora92
Contributor
Contributor

that's perfect! Many thanks! I'll try to improve it, using it, step by step when and if necessary; but the first tests were very satisfactory! I will surelly keep you updated on future developments

0 Likes
Message 12 of 12

komondormrex
Mentor
Mentor

direction of polylines doesn't matter. elevation adds as z coordinate to lw and 2d plines.

 

;*************************************************************************************************************************************************************************

(defun get_vertices (ename / vertices_list index elevation object)
  (setq index -1
	elevation (cdr (assoc 38 (entget ename)))
	object (vlax-ename->vla-object ename) 
  )
  (repeat (if (minusp (vlax-get object 'closed)) (fix (vlax-curve-getendparam ename)) (1+ (fix (vlax-curve-getendparam ename))))
    (setq vertices_list (append vertices_list (list (vlax-safearray->list (vlax-variant-value (vla-get-coordinate object (setq index (1+ index))))))))
  )
  (if elevation (mapcar '(lambda (vertex) (append vertex (list elevation))) vertices_list) vertices_list)
)

;*************************************************************************************************************************************************************************

(defun c:closest_vertices_lines (/ vertices_list_1 vertices_list_2)
	(setq vertices_list_1 (get_vertices (car (entsel "\nPolyline 1: ")))
	      vertices_list_2 (get_vertices (car (entsel "\nPolyline 2: ")))
	)
	(mapcar '(lambda (test_vertex)
			(vla-addline (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
		       		     (vlax-3d-point (nth (car (vl-sort-i (mapcar '(lambda (vertex) (distance test_vertex vertex))
										  vertices_list_2
									 )
								         '<
							      )
							 )
							 vertices_list_2
						    )
				     )
	  	                     (vlax-3d-point test_vertex)
			)
		   )
		  vertices_list_1
	)
  	(princ)
)

;*************************************************************************************************************************************************************************

 

 

0 Likes