sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Question about order in selection

    Scheduled Pinned Locked Moved Developers' Forum
    20 Posts 10 Posters 1.6k Views 10 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • PixeroP Offline
      Pixero
      last edited by

      I have a question in what order entities are added to a selection?
      I've tried getting the first selected entity with selection[0] as I thought that would return the entity I selected first and then selection[1] to get the second.
      Strangely it works when I do it the other way around (selection[1] for first and selection[0] for second).
      Any thoughts?

      1 Reply Last reply Reply Quote 0
      • J Offline
        jessejames
        last edited by

        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 πŸ˜‰

        Always sleep with a loaded gun under your pillow!

        1 Reply Last reply Reply Quote 0
        • C Offline
          cjthompson
          last edited by

          As far as I've seen, the order of selection is the same as the order in the entities list (order of creation).

          1 Reply Last reply Reply Quote 0
          • PixeroP Offline
            Pixero
            last edited by

            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?

            1 Reply Last reply Reply Quote 0
            • Chris FullmerC Offline
              Chris Fullmer
              last edited by

              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

              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                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

                1 Reply Last reply Reply Quote 0
                • PixeroP Offline
                  Pixero
                  last edited by

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    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 ?

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • PixeroP Offline
                      Pixero
                      last edited by

                      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.

                      1 Reply Last reply Reply Quote 0
                      • TIGT Offline
                        TIG Moderator
                        last edited by

                        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 then

                        if 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......

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • PixeroP Offline
                          Pixero
                          last edited by

                          Thanks will try this.

                          1 Reply Last reply Reply Quote 0
                          • B Offline
                            bigcatln
                            last edited by

                            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 finish

                            It is not a nice solution,because I found it is hard to do a box crossing selection by ruby

                            1 Reply Last reply Reply Quote 0
                            • JuantxoJ Offline
                              Juantxo
                              last edited by

                              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)
                              
                              1 Reply Last reply Reply Quote 0
                              • JuantxoJ Offline
                                Juantxo
                                last edited by

                                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.

                                1 Reply Last reply Reply Quote 0
                                • Chris FullmerC Offline
                                  Chris Fullmer
                                  last edited by

                                  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.

                                  Lately you've been tan, suspicious for the winter.
                                  All my Plugins I've written

                                  1 Reply Last reply Reply Quote 0
                                  • JuantxoJ Offline
                                    Juantxo
                                    last edited by

                                    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.

                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      @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.

                                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • JuantxoJ Offline
                                        Juantxo
                                        last edited by

                                        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".

                                        1 Reply Last reply Reply Quote 0
                                        • DavidBoulderD Offline
                                          DavidBoulder
                                          last edited by

                                          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?

                                          --

                                          David Goldwasser
                                          OpenStudio Developer
                                          National Renewable Energy Laboratory

                                          1 Reply Last reply Reply Quote 0
                                          • Dan RathbunD Offline
                                            Dan Rathbun
                                            last edited by

                                            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
                                            

                                            I'm not here much anymore.

                                            1 Reply Last reply Reply Quote 0
                                            • 1 / 1
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement