This line is not doing what you think it is...
Dim pol As Point3d = prof.GetClosestPointTo(mousePoint, False)
Remember, the profile is the 3d representation of the alignment, so when you use the getclosestpointto () method, it is doing so along the alignment. What you need to do is get the station/elev x/y from the ProfileView. Here is what I use to get the station in plan or profileview of any alignment or a given profile. It's c# and is a slightly different approach than what @hippe013 used, but hopefully you can see what I did and adapt it.
static void selectProfileStation_PointMonitor(object sender, PointMonitorEventArgs e)
{
double sta = Double.NaN, elev = 0;
Point3d cursorPt3d = e.Context.RawPoint;
Point2d cursorPt = cursorPt3d.Convert2d(GeometryUtil.PlaneXY);
double x = Double.NaN, y = Double.NaN;
Alignment align = m_CurrentProfile.GetAlignment();
bool lookInPlanView = true;
string stationString = "";
Point3d endPt = new Point3d();
try
{
ViewportGeometry geom = e.Context.DrawContext.Geometry;
foreach (ProfileView view in align.ProfileViews())
{
view.FindStationAndElevationAtXY(cursorPt.X, cursorPt.Y, ref sta, ref elev);
var extMin = view.GeometricExtents.MinPoint;
var extMax = view.GeometricExtents.MaxPoint;
if (cursorPt.X < extMax.X && cursorPt.X > extMin.X && cursorPt.Y < extMax.Y && cursorPt.Y > extMin.Y)
{
// cursor in profile view
if (sta < align.StartingStation)
sta = align.StartingStation;
else if (sta > align.EndingStation)
sta = align.EndingStation;
elev = m_CurrentProfile.ElevationAt(sta);
view.FindXYAtStationAndElevation(sta, elev, ref x, ref y);
endPt = new Point3d(x, y, 0);
lookInPlanView = false;
break;
}
}
if (lookInPlanView)
{
// cursor not in profile view
try
{
align.StationOffsetQuux(cursorPt.X, cursorPt.Y, out sta, out elev);
}
catch
{
sta = align.ClosestEndpointStationToPoint(cursorPt);
}
align.PointLocation(sta, 0, ref x, ref y);
endPt = new Point3d(x, y, 0);
}
Line line = new Line(endPt, cursorPt3d);
line.ColorIndex = 3;
geom.Draw(line);
geom.DrawCircularHighlightGlyph(endPt);
stationString = align.GetStationStringWithEquations(sta);
e.AppendToolTipText("\nSelected Station: " + stationString);
}
catch { }
}