@tig said:
Let's start simple...
Thanks, TIG, for the suggestion. Unfortunately it does not work very well at all, basically because in 3D the mouse represents a ray, not a point. Sketchup generates a point via InputPoint and PickHelper using a heuristic that does not work very well at all in this application.
I have a method that works well:
get the mouse ray via view.pickray(x,y)
loop over all segments of the polyline
compute the position along the line containing the segment, where the ray is closest to the line
trim that to the endpoints of the segment
compute distance from the resulting point to the ray
remember the closest one
now I have the segment and location within the segment of the mouse
generate the Geom::Transform for that location
Basically this represents the centerline of a particle accelerator, and the code is inside my custom DragTool, using the mouse to place an object along the centerline; so the object is constrained to lie on the centerline, and its local z-axis is aligned with the centerline where it is located. For <= 200 segments this tracks the mouse quite well; for 1000 segments it takes about 1 second to catch up. That's acceptable for now; ultimately I may optimize it. I'm rather surprised that this much Ruby computation is acceptable.
If a segment is parallel to the pickray, the math will divide by zero. So my code disallows any segment that is nearly parallel to the pickray. This makes sense, as the user cannot possibly select a position along such a segment; use the middle button to rotate the display so the desired position is visible.
I ended the centerline with a half-infinite straight line. SketchUp cannot draw that (or rather, it tries to do so, zooming out so much that it's useless). So I split the final segment in two, with the first being half as long as the preceding centerline; the code does not display the final segment.
The next challenge is to generalize this to include segments that are circular arcs (so far the code is limited to radius=0)....
If anyone wants my code, or references to the geometrical calculations I'm using, just ask.