Question about order in selection
-
Copied verbatim from the API docs...
@unknownuser said:
Introduction
SketchUp 6.0+
A set of the currently selected entities. Use the Model.selection method to get a Selection object. 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.However you could create a SelectionObserver and keep your own "nicely ordered" array of selected objects
-
As far as I've seen, the order of selection is the same as the order in the entities list (order of creation).
-
Well, not for me...
I create ent1 and then ent2.
Then I select ent1 and ent2.
But in the selection sel[1] returns ent1 and sel[0] returns ent2.
Strange? -
Nah, that is normal. You can't be certain of the selection order. You can also copy and move a line, and that might change its order in the selection set too from what I've seen.
Chris
-
I think there's no consistent logic to their order. It will often follow their creation time but only approximately and not reliably.
In a tool you need to get the user to pick them in order and then add these to an array/hash for processing later...
Perhaps you could get their IDs and sort by those as I think they are perhaps based on a 'time-stamp'?
Why is the selected order important to you ? -
@tig said:
Why is the selected order important to you ?
I want to do different things with them so I need to be sure its the first or the second selected entity.
-
Can't your Tool ask the user to pick the first and then the second thing ?
Then you are sure...
If the things are different - e.g. a face and a group it's easy to decide which is which. Or are both 'things' the same type of entity ? -
I started out trying with the script asking the user to select first and then second entity but workflow wise I think it would be more efficient to just select them and press the script button. Yes they are the same type. I have given the grouped entities names though.
-
If they are always groups and they have consistent names it's easy to sort...
Assuming obj has already passed an 'is_a_group' test thenif obj.name=="aaa1" #do this elsif obj.name=="aaa2" #do that end#if
Even similar names are useful as you can look for patterns
if obj.name=~/1$/ #do this elsif obj.name=~/2$/ #do that end#if
Here we look for names ending in '1' or '2' - the rest of the name can vary......
-
Thanks will try this.
-
I am also in the same trouble
My solution is: select firt, then press the tool button to active my tool then use the pickhelper do another select operation,press return key to finishIt is not a nice solution,because I found it is hard to do a box crossing selection by ruby
-
That is the code I use, but I don't like too much use observers.
If someone knows better solution...require 'sketchup' class MySelectionObserver < Sketchup;;SelectionObserver @@sel2_ord=[] def initialize() @s1=[] @s2=[] end def onSelectionBulkChange(selection) @@sel2_ord=[] if selection.length==1 then @s1=selection.collect{|el| el}; @s2=[] end if selection.length==2 then @s2=selection.collect{|el| el} if @s2.include?(@s1[0]) then @s2.delete(@s1[0]) @@sel2_ord=[@s1[0],@s2[0]]; @s1=[];@s2=[] end end end def onSelectionCleared(selection) @s1=[] @s2=[] @@sel2_ord=[] end def self.sel2_ord() return @@sel2_ord end end if( not file_loaded?(__FILE__) ) bool_2d_obs = MySelectionObserver.new() Sketchup.active_model.selection.add_observer(bool_2d_obs ) file_loaded(__FILE__) end #To find out the two objects selected in order, write in ruby command window #p(MySelectionObserver.sel2_ord)
-
I've had the same problem,
I need to be sure what is the first or the second selected entity with the mouse (the two have the same class).
I've used SelectionObserver (onSelectionBulkChange, onSelectionCleared) storing selection entities in two variables,
one with first click and the other with next click. So, comparing variables I can find out what is first entity and second.
I'm not very sure if it is the right form...but works. -
If you are using a tool, then just have an array that holds the selected objects and everytime the user clicks the left mouse button, just add whatever they are hovered over to your selection array.
Then you always know what order they selected things in. And if you only want them to select 2 things, then once they select that second object, then your tool should continue to process the selected objects instead of allowing more selections.
-
Yes, it's a good way, but I'm looking something to know the order of selection tool, not the order of my own tool.
Like in "Solid tools" toolbar.
In "Solid tools" you can use the selection tool and substract two entities, second entity will be substracted from first entity selected. Solid tools knwos the order you pick the entities with selection tool. -
@juantxo said:
Yes, it's a good way, but I'm looking something to know the order of selection tool, not the order of my own tool.
Like in "Solid tools" toolbar.
In "Solid tools" you can use the selection tool and substract two entities, second entity will be substracted from first entity selected. Solid tools knwos the order you pick the entities with selection tool.Solid Tools doesn't make use of the native Select tool. It's a custom variant.
-
Solid tools has its own tool, but also, you can use selection tool, pick two entities and substract them. And order its detected.
So, order of selection tool is detected by "solid tools". -
So, I was also hoping for something like this without using a custom tool. As an alternative I'm thinking of re-sorting the selection array based one some logic. In this case I want to take a bunch of horizontal adjacent surfaces, and turn them into room numbers. As is they jump all over the place. I was going to allow the user to click to define, but another solution could be to start with bottom left corner and work my way around. Here is the logic I was thinking of.
- Set the face with lowest "x" and then "y" as a tiebreaker as first object (they all have same z)
- Next find the adjacent face with the lowest "x" and then "y" as tiebreaker
- keep repeating step above, excluding spaces that have already been added to new array.
- In many cases I can step through all faces without painting myself in a corner, but in some cases, I may have gotten to a situation where there are no adjacent faces, but not all faces have been added to the new array. If i do get painted into a corner, I would just go back to step 1 with remaining faces.
Has anyone done any programmatic re-ordering of the selection similar to this?
-
Here's a start. Rename the module or cut & paste the proxy class block into one of your modules.
module Sort class << self def sort_faces_by_vertex() @sel = Sketchup.active_model.selection.to_a @sel.delete_if {|e| not e.is_a?(Sketchup;;Face) } @sel.sort! {|a,b| ([a.vertices.min{|v1,v2| v1.position.x <=> v2.position.x }.position.x, a.vertices.min{|v1,v2| v1.position.y <=> v2.position.y }.position.y] <=> [b.vertices.min{|v1,v2| v1.position.x <=> v2.position.x }.position.x, b.vertices.min{|v1,v2| v1.position.y <=> v2.position.y }.position.y] ) } end def assign_room_nums_to_faces() sbv = sort_faces_by_vertex() sbv.each_with_index {|face,i| dict = face.attribute_dictionary('Properties',true) dict['name']= "Room #{(i+1).to_s}" } end end # proxy class end # module Sort
Advertisement