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

    Select all geometry

    Scheduled Pinned Locked Moved Developers' Forum
    29 Posts 6 Posters 1.7k Views 6 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.
    • Didier BurD Offline
      Didier Bur
      last edited by

      Hi,
      In select_all function, replace

      @sel.add ent if (ent.typename == "ComponentInstance")
      

      with:

      @sel.add Sketchup.active_model.definitions["Stein"].instances
      

      assuming that you want to select all instances of the "Stein" component, I guess.

      If I had to code such a function, I'll write this:

      
      def select_comp_by_name(comp_name)
      @sel = Sketchup.active_model.selection
      @sel.clear!
      @sel.add Sketchup.active_model.definitions[comp_name].instances
      @sel
      end
      
      

      Function call example in the script:

      
      ...
      stones=select_comp_by_name("Stein")
      
      

      Hope this helps,

      ...

      DB

      1 Reply Last reply Reply Quote 0
      • N Offline
        N Lindenthal
        last edited by

        @Didier

        
        def select_comp_by_name(comp_name)
        @sel = Sketchup.active_model.selection
        @sel.clear!
        @sel.add Sketchup.active_model.definitions[comp_name].instances
        @sel
        end
        
        

        From this I made a text file "Aar_select_comp_by_name.rb" in the Plugins folder. Restart SU.

        
        stones=select_comp_by_name("Stein")
        
        

        This I wrote in the ruby console line.

        Then comes the following error:

        @unknownuser said:

        stones=select_comp_by_name("Stein")
        Error: #<NoMethodError: undefined method `clear!' for #Sketchup::Selection:0x1afaa3b0>
        (eval):11
        (eval):11

        How to select an object by name?

        1 Reply Last reply Reply Quote 0
        • N Offline
          N Lindenthal
          last edited by

          Unfortunately I don’t know, what to type in the Ruby console line (I am Ruby beginner).

          1 Reply Last reply Reply Quote 0
          • fredo6F Offline
            fredo6
            last edited by

            Just remove the exclamation mark to @sel.clear

            1 Reply Last reply Reply Quote 0
            • N Offline
              N Lindenthal
              last edited by

              @unknownuser said:

              Just remove the exclamation mark to @sel.clear

              Then come the following error:
              @unknownuser said:

              stones=select_comp_by_name("Stein")
              Error: #<NoMethodError: undefined method `instances' for nil:NilClass>
              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:4
              (eval):197

              1 Reply Last reply Reply Quote 0
              • fredo6F Offline
                fredo6
                last edited by

                First, I would make a test on the return value of Sketchup.active_model.definitions[comp_name], because if there is no such component definition*<comp_name>*, it will return <nil>, and applying the method instanceswill provoke an error.

                Otherwise, your code seems correct to me.

                The issue is maybe that there is no such component "Stein" in your model. You should check with the Component window.

                1 Reply Last reply Reply Quote 0
                • N Offline
                  N Lindenthal
                  last edited by

                  @Fredo6

                  @unknownuser said:

                  Sketchup.active_model.definitions[comp_name]
                  Error: #<NameError: undefined local variable or method `comp_name' for main:Object>
                  (eval):197

                  This comes from ruby console.

                  I had an object named "Stein". And your question to a component named ”Stein" let me make also a component named "Stein". Same error.

                  1 Reply Last reply Reply Quote 0
                  • fredo6F Offline
                    fredo6
                    last edited by

                    When you call it from the console, use "stein" not comp_name, which is the name of the variable. So:

                    Sketchup.active_model.definitions["stein"]
                    
                    

                    Your code looks correct. Here it is with the test on existence of the component

                    
                    def select_comp_by_name(comp_name)
                       model = Sketchup.active_model
                       sel = model.selection
                       sel.clear
                       compdef = model.definitions[comp_name]
                       if compdef  #There is a component
                          sel.clear
                          sel.add compdef.instances
                       else
                          UI.beep
                       end
                    end
                    
                    

                    Are you sure you have a component called "stein" in your model? Check with the Windows>Components. By the way, I wonder what you call "an Object named "stein"", as in Sketchup, you only give a name to Components or Groups, but there is nothing like an 'object'.

                    Note also that Sketchup is not case sensitive with the name of components.

                    Fredo

                    1 Reply Last reply Reply Quote 0
                    • N Offline
                      N Lindenthal
                      last edited by

                      @unknownuser said:

                      …
                      I wonder what you call "an Object named "stein"", as in Sketchup, you only give a name to Components or Groups, but there is nothing like an 'object'.
                      …

                      Please excuse me, I have a group named "Stein", no object.

                      1 Reply Last reply Reply Quote 0
                      • fredo6F Offline
                        fredo6
                        last edited by

                        Then, this is the source of your error.
                        You must have a Component, not a Group for your Ruby code to work.
                        In Sketchup, use the command Make Component(shortcut 'G') instead of Make Group

                        1 Reply Last reply Reply Quote 0
                        • N Offline
                          N Lindenthal
                          last edited by

                          @Fredo6
                          Now my model has no group "Stein", but one component "Stein". But I have no luck. To select my component "Stein" by script in ruby console does not work. Did you try your script lines and you could select your component by name?

                          1 Reply Last reply Reply Quote 0
                          • fredo6F Offline
                            fredo6
                            last edited by

                            Which error do you get?

                            1 Reply Last reply Reply Quote 0
                            • N Offline
                              N Lindenthal
                              last edited by

                              @Fredo6

                              This is my ruby script »Aar_Select_comp_by_name.rb« in the Plugins folder:

                              @unknownuser said:

                              def select_comp_by_name(comp_name)
                                 model = Sketchup.active_model
                                 sel = model.selection
                                 sel.clear
                                 compdef = model.definitions[comp_name]
                                 if compdef  #There is a component
                                    sel.clear
                                    sel.add compdef.instances
                                 else
                                    UI.beep
                                 end
                              end
                              #def select_comp_by_name(comp_name)
                              #@sel = Sketchup.active_model.selection
                              #@sel.clear
                              #@sel.add Sketchup.active_model.definitions[comp_name].instances
                              #@sel
                              #end

                              If I repeat loading by ruby console:

                              load "Aar_Select_comp_by_name.rb"
                              

                              then come the following notes
                              @unknownuser said:

                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:6: warning: parenthesize argument(s) for future version
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:7: warning: parenthesize argument(s) for future version
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:7: warning: parenthesize argument(s) for future version
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:8: warning: parenthesize argument(s) for future version
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:8: warning: parenthesize argument(s) for future version
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:8: warning: parenthesize argument(s) for future version
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:10: warning: parenthesize argument(s) for future version
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:10: warning: parenthesize argument(s) for future version
                              true
                              … but also »true«.

                              Now with ruby script line

                              Sketchup.active_model.definitions["Stein"]
                              

                              I get
                              @unknownuser said:

                              nil

                              and with

                              > select_comp_by_name("Stein")
                              

                              @unknownuser said:

                              Error: #<NoMethodError: undefined method ` ' for main:Object>
                              /Library/Application Support/Google SketchUp 6/SketchUp/Plugins/Aar_Select_comp_by_name.rb:2
                              (eval):197

                              … nothing is selected.

                              1 Reply Last reply Reply Quote 0
                              • M Offline
                                Matt666
                                last edited by

                                Hi !
                                Your script works fine here... Be careful with case of the definition name... I found no error in your script...
                                Perhaps some parenthesis

                                sel.add(compdef.instances)
                                

                                and unnecessary duplication

                                sel.clear
                                

                                Erase the second duplication.
                                😄

                                Frenglish at its best !
                                My scripts

                                1 Reply Last reply Reply Quote 0
                                • N Offline
                                  N Lindenthal
                                  last edited by

                                  @Matt666
                                  Thanks for your corrections.
                                  When your script runs fine, in the end you have the component "Stein" selected?

                                  1 Reply Last reply Reply Quote 0
                                  • M Offline
                                    Matt666
                                    last edited by

                                    @unknownuser said:

                                    When your script runs fine, in the end you have the component "Stein" selected?

                                    I tried with a component called "essai" created before. I had five components, and they had been all selected in the end.

                                    Can you upload an example I can test, please ?

                                    Frenglish at its best !
                                    My scripts

                                    1 Reply Last reply Reply Quote 0
                                    • N Offline
                                      N Lindenthal
                                      last edited by

                                      Matt, here is my model to select a component by name "Stein"


                                      FahrstuhlF.skp.zip to selcect component by name

                                      1 Reply Last reply Reply Quote 0
                                      • M Offline
                                        Matt666
                                        last edited by

                                        Ok I understand your problem...
                                        I have made a component called "test". Entity info window looks like this :C.jpg
                                        With your component, entity info window looks like this :c1.jpg
                                        The script search for component with definition as ["Stein"]. But your component definition is called "<gruppe#4> in the Skecthup database. His NAME is "Stein".

                                        3 methods :

                                        • Create a component directly, without create a group first. Your first ruby script will work.
                                        • Change your ruby script, and don't change your method.
                                          Try this in the ruby API :
                                        Sketchup.active_model.definitions.each{|d|return d if d.name == "Stein"}
                                        

                                        This code returns the name of the component definition which called "stein"
                                        And the ruby script :

                                        def select_comp_by_name(comp_name)
                                           model = Sketchup.active_model
                                           sel = model.selection
                                           sel.clear
                                           compdef = Sketchup.active_model.definitions.each{|d|return d if d.name == "Stein"}
                                           if compdef  #There is a component
                                              sel.clear
                                              sel.add(compdef.instances)
                                           else
                                              UI.beep
                                           end
                                        end
                                        
                                        • Add a line in ruby which control name of all component definitions if C definitions not found...
                                          Like this :
                                        def select_comp_by_name(comp_name)
                                        	model = Sketchup.active_model
                                        	sel = model.selection
                                        	sel.clear
                                        	compdef = Sketchup.active_model.definitions[comp_name]
                                        	Sketchup.active_model.definitions.each{|d|return d if d.name == "Stein"} if not compdef
                                        	if compdef
                                              sel.clear
                                              sel.add(compdef.instances)
                                           else
                                              UI.beep
                                           end
                                        end
                                        

                                        Not tested, but it sould work !
                                        Tell me if you want more explanations
                                        😉

                                        Frenglish at its best !
                                        My scripts

                                        1 Reply Last reply Reply Quote 0
                                        • N Offline
                                          N Lindenthal
                                          last edited by

                                          @Matt666
                                          I did understand you. First changes did not delete my error. Hm? What could it be?

                                          1 Reply Last reply Reply Quote 0
                                          • M Offline
                                            Matt666
                                            last edited by

                                            Oops !! 😳 A little bug !!

                                            In fact, name is linked to the component, not to the component definition.
                                            So you have to search around all the component in the drawing to select component with specific name, if no component definition have this specific name....

                                            Like this :

                                            def select_comp_by_name(comp_name)
                                            	model = Sketchup.active_model
                                            	sel = model.selection
                                            	sel.clear
                                            	if compdef = Sketchup.active_model.definitions[comp_name]
                                            		sel.add(compdef.instances)
                                            	else
                                            		Sketchup.active_model.active_entities.each do |d|
                                            			if d.typename == "ComponentInstance"
                                            				sel.add d if d.name==comp_name
                                            			end
                                            		end
                                            		if sel.count == 0
                                            			UI.beep
                                            		else
                                            			UI.messagebox(sel.count.to_s + " entities found.")
                                            		end
                                            	end
                                            end
                                            

                                            Frenglish at its best !
                                            My scripts

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

                                            Advertisement