Get the closest point to a polyline segment

Get the closest point to a polyline segment

Browning_Zed
Advocate Advocate
139 Views
2 Replies
Message 1 of 3

Get the closest point to a polyline segment

Browning_Zed
Advocate
Advocate

Hi,
Autolisp has a function vlax-curve-getClosestPointTo that returns the point on a curve that is nearest to the specified point. I need the same method that can be used only for a part of a curve (polyline segment).
I can get the geometric parameters of a segment like this:

(setq
    pik (entsel)
    ent (car pik)
    gpp (fix
            (vlax-curve-getparamatpoint ent
                (vlax-curve-getclosestpointto
                    ent
                    (cadr pik)
                )
            )
        )
    pt1 (vlax-curve-getpointatparam ent gpp)
    pt2 (vlax-curve-getpointatparam ent (1+ gpp))
)

How can I get the closest point to the segment between points pt1 and pt2 without creating a new entity (line or polyline) from these two points?

0 Likes
140 Views
2 Replies
Replies (2)
Message 2 of 3

Kent1Cooper
Consultant
Consultant

If what you mean is the point precisely on that segment that is closest to the pick point [which may not be precisely on the object]:

(osnap (cadr pik) "_nea")

No need to save gpp or pt1 or pt2.

If I have misunderstood, can you explain in greater detail, and provide an image?

Kent Cooper, AIA
0 Likes
Message 3 of 3

marko_ribar
Advisor
Advisor

@Browning_Zed 

You are forgetting (trans) function when dealing with (vlax-curve-xxx) functions... So here is my revision :

(setq
    pik (entsel)
    ent (car pik)
    gpp (fix
            (vlax-curve-getparamatpoint ent
                (vlax-curve-getclosestpointto
                    ent
                    (trans (cadr pik) 1 0)
                )
            )
        )
    pt1 (trans (vlax-curve-getpointatparam ent gpp) 0 1)
    pt2 (trans (vlax-curve-getpointatparam ent (1+ gpp)) 0 1)
)

And yes, Kent answered correctly - using "_nea" osnap is much more effective for obtaining just that single point, plus you don't have to worry about (trans) - "_nea" osnap will return always point in current UCS...

If I may ask, what do you need to acomplish with that single point? Of course that making new entity to only get reqired data is slappy approach, but be aware that sometime it could also get you to your needs with correct postprocessing - deletion of those dummy entities...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes