Jump to content

vlax-curve-getClosestPointTo [extend]


Kowal

Recommended Posts

How to get the coordinates of a point on the extension of the LWpoliline.

If I use the function with the argument T is not getting the expected result.

 

(vlax-curve-getClosestPointTo curve-obj givenPnt [extend])

Extend.png

Link to comment
Share on other sites

The extend option will only extend certain objects, e.g.:

 

extend.png

 

(defun c:test ( / ent pnt )
   (if
       (and
           (setq pnt (getpoint "\nPick point: "))
           (setq ent (car (entsel)))
       )
       (entmake (list '(0 . "POINT") (cons 10 (vlax-curve-getclosestpointto ent (trans pnt 1 0) t))))
   )
   (princ)
)

Link to comment
Share on other sites

Thank you Lee.

I tried to use 3DPOLYLINE. But another problem appeared.:shock:

At the end of 3DPolyline does not function properly.

 

How to do at the end of the line is perpendicular.

Sorry for my english

3DP.png

Link to comment
Share on other sites

How to do at the end of the line is perpendicular.

 

Sorry, I don't understand your question :oops:

Maybe you require vlax-curve-getclosestpointtoprojection? - an explanation of the difference can be found here.

Link to comment
Share on other sites

One...

If I have understood correctly.

 

(defun c:test ( / 3dp p p_end p_prev)
    (if
        (and
            (setq 3dp (vlax-ename->vla-object (car (entsel "\nSelect 3Dpolyline"))))
            (setq p (trans (getpoint "\nSelect Point") 1 0))
        )
        (progn
            (setq p_end (vlax-curve-getEndPoint 3dp))
            (setq p_prev (vlax-curve-getPointAtParam 3dp (- (vlax-curve-getEndParam 3dp) 0.5)))
            (setq p_perp (LM:ProjectPointToLine p p_end p_prev))
            (entmake
                (list
                    '(0 . "POINT")
                    (cons 10 p_perp)
                )
            )
        )
    )
    (princ)
)
;----------------------------------------------------------------
(defun LM:ProjectPointToLine ( pt p1 p2 / nm )
    (setq nm (mapcar '- p2 p1)
          p1 (trans p1 0 nm)
          pt (trans pt 0 nm)
    )
    (trans (list (car p1) (cadr p1) (caddr pt)) nm 0)
)

461.gif

Edited by GP_
Link to comment
Share on other sites

:oops:

I need projection point to LWpolylne or 3Dpolyline (0 level).

 

PPP.png

 

I have a entitiy lwpolyline and coordinate red points.

I need coordinate green points. :)

Link to comment
Share on other sites

I need projection point to LWpolylne or 3Dpolyline (0 level).

 

Try this:

(defun closestpointto ( ent pnt )
   (if (vlax-curve-isclosed ent)
       (vlax-curve-getclosestpointto ent pnt)
       (car
           (vl-sort
               (list
                   (vlax-curve-getclosestpointto ent pnt)
                   (LM:projectpointtoline pnt
                       (vlax-curve-getpointatparam ent (vlax-curve-getstartparam ent))
                       (vlax-curve-getpointatparam ent (1+ (vlax-curve-getstartparam ent)))
                   )
                   (LM:projectpointtoline pnt
                       (vlax-curve-getpointatparam ent (vlax-curve-getendparam ent))
                       (vlax-curve-getpointatparam ent (1- (vlax-curve-getendparam ent)))
                   )
               )
              '(lambda ( a b ) (< (distance pnt a) (distance pnt b)))
           )
       )
   )
)

;; Project Point to Line  -  Lee Mac
;; Projects pt onto the line defined by p1,p2

(defun LM:projectpointtoline ( pt p1 p2 / nm )
   (setq nm (mapcar '- p2 p1)
         p1 (trans p1 0 nm)
         pt (trans pt 0 nm)
   )
   (trans (list (car p1) (cadr p1) (caddr pt)) nm 0)
)

(vl-load-com) (princ)

 

Test code:

(defun c:test ( / ent pnt )
   (if
       (and
           (setq pnt (getpoint "\nPick point: "))
           (setq ent (car (entsel)))
       )
       (entmake (list '(0 . "POINT") (cons 10 (closestpointto ent (trans pnt 1 0)))))
   )
   (princ)
)

Link to comment
Share on other sites

Than you Lee. Code is great.

:oops:

But for lwpolyline coordinates'((0.000 0.000) (18.028 0.890) (40.211 2.298 ))

not working properly.

Point (35.924 1.679) returns (35.919 1.773).

Correctly (35.924 1.679) -> (35.902 2.024).

Link to comment
Share on other sites

Than you Lee. Code is great.

:oops:

But for lwpolyline coordinates'((0.000 0.000) (18.028 0.890) (40.211 2.298 ))

not working properly.

Point (35.924 1.679) returns (35.919 1.773).

Correctly (35.924 1.679) -> (35.902 2.024).

 

Good catch! - try this instead:

 

(defun closestpointto ( ent pnt )
   (if (vlax-curve-isclosed ent)
       (vlax-curve-getclosestpointto ent pnt)
       (car
           (vl-sort
               (list
                   (vlax-curve-getclosestpointto ent pnt)
                   (LM:projectpointtoray pnt
                       (vlax-curve-getpointatparam ent (1+ (vlax-curve-getstartparam ent)))
                       (vlax-curve-getpointatparam ent (vlax-curve-getstartparam ent))
                   )
                   (LM:projectpointtoray pnt
                       (vlax-curve-getpointatparam ent (1- (vlax-curve-getendparam ent)))
                       (vlax-curve-getpointatparam ent (vlax-curve-getendparam ent))
                   )
               )
              '(lambda ( a b ) (< (distance pnt a) (distance pnt b)))
           )
       )
   )
)

;; Project Point to Ray  -  Lee Mac
;; Projects pt onto the ray defined by p1,p2

(defun LM:projectpointtoray ( pt p1 p2 / nm )
   (setq nm (mapcar '- p2 p1)
         p1 (trans p1 0 nm)
         pt (trans pt 0 nm)
   )
   (trans (list (car p1) (cadr p1) (max (caddr p1) (caddr pt))) nm 0)
)

(vl-load-com) (princ)

Link to comment
Share on other sites

  • 7 years later...

image.thumb.png.785dbbdac4f70042b61edc8409b7daca.png

 

Hello Lee Mac! First of all I would like to thank you very much for what you bring to the worldwide community and I myself use a lot. I have used your program above, but when I draw a 3D Polyline triangle like the picture above, it doesn't work as expected. Hope you answer.

Link to comment
Share on other sites

When I view 3D, I understand how it is. But I want to lower the perpendicular in the XY plane, how will I do it?

image.thumb.png.abab5dccd195e401168e9a75b324e75e.png

Link to comment
Share on other sites

(defun TD:ClosestPointTo (eObj Pnt / LTSVER OBJPLINE2D PT1 PT2 TYPEOBJ X)
	(setq TypeObj (cdr (assoc 0 (entget eObj))))
	(cond	((= TypeObj "POLYLINE")
				 (progn
					 (setq LtsVer (mapcar '(lambda (x) (list (car x) (cadr x) 0.0)) (acet-geom-vertex-list eObj)))
					 (entmake
						 (append
							 (list (cons 0 "LWPOLYLINE")
										 (cons 100 "AcDbEntity")
										 (cons 100 "AcDbPolyline")
										 (cons 90 (length LtsVer))
							 )
							 (mapcar (function (lambda (x) (cons 10 x))) LtsVer)
						 )
					 )
					 (setq ObjPline2D (entlast))
					 (if (vlax-curve-isclosed (vlax-ename->vla-object eObj))
						 (vla-put-closed (vlax-ename->vla-object ObjPline2D) :vlax-true)
					 )
					 (setq Pt1 (ClosestPointTo (vlax-ename->vla-object ObjPline2D) Pnt))
					 (setq Para (vlax-curve-getParamAtPoint (vlax-ename->vla-object ObjPline2D) Pt1))
					 (setq Pt2 (vlax-curve-getPointAtParam (vlax-ename->vla-object eObj) Para))
					 (entdel ObjPline2D)
				 )
				)
				((= TypeObj "LWPOLYLINE")
				 (setq Pt2 (ClosestPointTo (vlax-ename->vla-object eObj) Pnt))
				)
	)
	Pt2
)

I figured it out and made it like this! Don't know is that okay?

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...