The 2nd problem is different, this time I pick point not on the polyline, let's say 3 units away from the polyline and I need to find its station (projected to the polyline).
OK. A bit more information is needed then. What is the projection vector to the selected curve? Perpendicular (the closest point)? Orthographic? Nearest Vertex?
An idea for coding this to the perpendicular would be to get the closest intersect point perpendicular to the segment (note: I'm doing this without being able to test at the moment - so forgive any typos):
;; untested snippet
(if (and
(setq sel (entsel "\nSelect Alignment: "))
(setq pt (getpoint "\nSelect Point: "))
)
(setq spt1 (osnap (cadr sel) "nea") ; get the nearest point to the entity pick
spt2 (osnap spt1 "end") ; get the nearest endpoint to the entity pick
ang (angle spt1 spt2) ; determine the angle of the segment
int (inters spt1 spt2 pt (polar pt (+ ang (/ pi 2)) 1.0) nil) ; get the intersection with the point and the curve, at perp. angle.
int (if (osnap int "nea") int spt2) ; If no intersection is found, set to the nearest endpoint found. NOTE this requires the user to select the curve at a location nearest to the projection.
ent (car sel)
stais (vlax-curve-getDistAtPoint ent (vlax-curve-getClosestPointTo ent (trans int 1 0)))
)
)
This method would require the user to select the curve segment (and hence the point on the segment from "(cadr sel)") as close to the projection point as possible. It's late so I'll test this out tomorrow. Might be a better way to do this, but it is to much code than I have time for at the moment.
EDIT: Tested this and found a couple mistakes so I updated the code. Seems to work OK now.
EDIT2: Removed an extra parenthesis