SketchUp Plugin Development: How to Number Component Instances Based on User Selection Order?

I am working on a plugin, and one of its features requires numbering selected component instances in the order the user selects them (either by single-click or box selection). However, it seems that SketchUp does not inherently keep track of the selection order. Regardless of how I make the selections, the resulting array always lists instances in the order they were created. Currently, the only solution I can think of is to use a SelectionObserver to monitor the user’s selection and then add the selected component instances to a custom array. However, this approach only works for single selections and does not function effectively for box selections or multiple additions. I would like to ask the experts if there is another way to address this issue?

You could try coding a Tool and using a pick helper or input point in it’s onlbuttondown callback to do your own selection management instead of relying on the built in selection Tool.

1 Like

This is explicitly stated in the Selection class Overview:

Note that the order of entities (selection[0] , selection[1] and so on) in the set is in no particular order and should not be assumed to be in the same order as the user selected the entities.

Regardless of whether you use the native Select tool or a custom picking tool, either the user must successively choose the instances (no box select) or the user needs to understand that a box select means to number those randomly within the ordinal set. The latter rule may require your code to keep more than one array of instances.

1 Like

I’ve come up with an idea, but I’m not sure if it’s feasible. The specific approach is as follows: replace the system’s selection tool with a custom tool. When the user performs a box selection, use onMouseMove to dynamically select objects using PickHelper and save the current selection to a custom array. When the selection set changes, compare the current selection set array with the one I’ve saved. If I find new objects that are not in my saved selection set array, I will add them to my selection set either at the end or beginning, and so on. This should address my issue, but if the user selects a large number of component instances, it seems that this method could be very inefficient.