Collecting picked point in an array

Dear all,

I am a Civil Engineer, passionate sketchup user.
Because of my “modeling routin” I’m currently trying to develop a tool that let me collect picked point on the screen in an array.
To better explain, imagine creating a broken line with the line tool, what I would like is to save all the picked start and end points in an array and have them sorted correctly in successive order from the first to the last.
I checked the community far and wide, look at every document that deal with ruby for sketchup but I can’t figure it out.

All your help will be precious for me!

Thank you and happy new year to everyone in the cmmunity!

Your first stop will be to read and understand the example line tool …

Then to push Ruby object references into an array, use:

@pt_ary = []
@pt_ary << pt1
@pt_ary << pt2
# ... etc.

Within the example tool (linked above), in the onLButtonDown callback method, you can push the inputpoint position into an array …

@pt_ary << @mouse_ip.position

For another example, say that you had a selection of multiple edges …

edges = selection.grep(Sketchup::Edge)
edge_points = edges.flat_map(&:vertices)

Thank you so much for your tip DanRathbun!!

I spent most of my holidays studying and understanding the example line tool in github.
Yesterday, after a day of testing and figuring out how to reach my goal, I thought the only way was to ask the community for help.
A few minutes after the publication, during dinner time, it dawned on me!!
I made the mistake of initializing the array within the onLbuttonDown method so that with each new click the array was overwritten storing only the last selected point. So I thought about initializing the array in the initialized method present in the first lines of the line tool, and hurrà goal achieved.

I also want to thank you for this very useful tip, I use to select edges in the model and try to extract vertices but these were randomly ordered. With this input my pains are gone.

Normally the initialize method of a class is the best place to create and init your reference objects.

But a Tool class is special. You might rather do this in the tool’s activate method. Your tool also needs to handle clearing out such data objects in the reset_tool method.

Well be careful. Sometimes the selection set can be randomly ordered. This might result in your point list being randon as well.

You might need to check for curve objects, and get the vertices from the curve rather than all it’s edges separately.

curve = edges[0].curve
curve_points =

This will ensure that the points for the curve are all in order.

1 Like