I understand that ph.best_picked returns the single “best” entity that the user would have picked if they were using the native SketchUp Select tool. That works well. I also understand that ph.path_at(0) returns an array of entities. That works well too. I also understand that the top element in ph.path_at(0) is not necessarily the same as ph.best_picked. My question is: Is ph.best_picked always going to be somewhere in the ph.path_at(0) array? The issue is: I am using ph.best_picked and then interrogating ph.path_at(0) for the best picked parent.
NO. PickHelper#path_at(0) is only looking at the first pick path in the array of possible pick paths. To query for the number of pick paths returned by PickHelper#do_pick use the PickHelper#count method.
This implies a search of the pick path records. The API did not implement an iterator, nor did it implement a #paths method to return an array of pick record arrays.
So, basic coding of iterators is the solution. Since the pick records are zero indexed our increment variable must also be.
best_parent = nil
ph.count.times do |i|
pick_record = ph.path_at(i)
if pick_record.size == 1 # parent is the model
best_parent = Sketchup.active_model
else
best_parent = pick_record[-2]
end
end
Adjust your comparison tests for the parent to suit your needs and break out of the loop when you find what your searching for.
In my opinion, Pickhelper always get (array of) entities from the active entities, cannot pick “outside” of it. Therefore the parent of best picked is the current editing context. In other words:
If Sketchup.active_model.active_pathreturns nil, than a best picked parent (instance) is the Sketchup.active_model, otherwise it is a last element of the active_path
Easy peasey!
More thought…
This describes how best_picked works more or less well, but consider:
The Select Tool can’t select Sketchup::Axes, while ph.best_picked can return it.
The Select Tool can select Sketchup::Snap, but ph.best_picked can’t return it.
(Actually Sketchup::Snap is “invisible” for the PickHelper of for the InputPoint. They “forget” to implement…so you need to write your own method, e.g. collect all Snap-s in the editing context and use the ph.test_point(point...) method to test each snap position…)
Let’s be aware of the use of the word “parent”.
The Entity#parent method returns [Sketchup::ComponentDefinition] or [Sketchup::Model]. While Pikhelper’s paths (or arrays) return instances. I assume you’re looking for the parent instance here, perhaps because of its transformation…
Yes. I am connecting the nozzle on a tank to the nozzle on a pump with clicks. It works perfectly with pick_helper but only if the tank and pump have not moved. So I need the transform for the tank and pump, which I call the nozzle’s “parent”.
Not yet. I was afraid that neither would be assured of returning the tr of the parent.