sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🔌 Smart Spline | Fluid way to handle splines for furniture design and complex structures. Download

    Select all geometry

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    29 貼文 6 Posters 2.1k 瀏覽 6 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • L 離線
      Laxmarsk
      最後由 編輯

      Hello

      I am working on a little script that is moving groups or components based on offset values read from a txt file and saving the files again at the new location.

      Script is working but now I want to get things done without having to manually "select all".

      I have looked everywhere for inspiration but without luck.

      Can anyone help with this?
      What I need is a way for the script to select all geometries in a open active file. Is this possible?

      1 條回覆 最後回覆 回覆 引用 0
      • fredo6F 離線
        fredo6
        最後由 編輯

        I guess you need to perform your loop exploration of entities on

        Sketchup.model.active_entities
        

        instead of

        Sketchup.model.selection
        

        This will be thus independent from the selection

        1 條回覆 最後回覆 回覆 引用 0
        • L 離線
          Laxmarsk
          最後由 編輯

          Thanks.

          I am kind of new to ruby scripting so I am not sure of what you mean!

          You suggest that I set up a kind of loop that goes through the model and selects all the components?

          Can you suggest a method for doing this?

          1 條回覆 最後回覆 回覆 引用 0
          • Didier BurD 離線
            Didier Bur
            最後由 編輯

            Hi,
            If you want to collect all compos instances and groups in your model and select them:

            
            def select_comps
            @sel=Sketchup.active_model.selection
            @sel.clear #prevents any wrong previous selection
            Sketchup.active_model.active_entities.each { |ent|
               @sel.add ent if (ent.typename == "ComponentInstance") or (ent.typename == "Group")
            }
            end
            

            There are other ways to do this (yeld method for instance) but this one may be more understandable.
            Use "select_comps" at the beginning of your script and all instances will be ready in @sel variable for further action(s):

            def my_script
            select_comps
            ...
            end

            Hope this helps,

            DB

            1 條回覆 最後回覆 回覆 引用 0
            • L 離線
              Laxmarsk
              最後由 編輯

              Hi Didier

              Thank you very much. It works perfectly!

              1 條回覆 最後回覆 回覆 引用 0
              • T 離線
                todd burch
                最後由 編輯

                In one line:

                
                Sketchup.active_model.selection.add(Sketchup.active_model.entities.to_a)
                
                

                No need to clear it first.

                Todd

                1 條回覆 最後回覆 回覆 引用 0
                • fredo6F 離線
                  fredo6
                  最後由 編輯

                  @laxmarsk said:

                  I am working on a little script that is moving groups or components based on offset values read from a txt file and saving the files again at the new location.

                  Script is working but now I want to get things done without having to manually "select all".

                  What I meant is that you do not need to select entities to perform your 'move' operations. Sketchup.active_model.active_entitiescontains all the entities of the model, and you can then apply your transformation to this list.

                  For instance, if you want to move all objects of the model by an offset offset_vector (which you'll read from a file), then all you have to write is

                  
                  all_entities = Sketchup.active_model.active_entities   #all entities of the models
                  t = Geom;;Transformation.translation offset_vector
                  all_entities.transform_entities t, all_entities
                  
                  

                  Now, if your problem is about the selection, Todd's and Didier's code will make it as a Select All.

                  1 條回覆 最後回覆 回覆 引用 0
                  • N 離線
                    N Lindenthal
                    最後由 編輯

                    @unknownuser said:

                    In one line:

                    
                    > Sketchup.active_model.selection.add(Sketchup.active_model.entities.to_a)
                    > 
                    

                    Please, Todd, tell me how to select my object »Stein«, but not all entities.

                    1 條回覆 最後回覆 回覆 引用 0
                    • Didier BurD 離線
                      Didier Bur
                      最後由 編輯

                      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 條回覆 最後回覆 回覆 引用 0
                      • N 離線
                        N Lindenthal
                        最後由 編輯

                        @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 條回覆 最後回覆 回覆 引用 0
                        • N 離線
                          N Lindenthal
                          最後由 編輯

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

                          1 條回覆 最後回覆 回覆 引用 0
                          • fredo6F 離線
                            fredo6
                            最後由 編輯

                            Just remove the exclamation mark to @sel.clear

                            1 條回覆 最後回覆 回覆 引用 0
                            • N 離線
                              N Lindenthal
                              最後由 編輯

                              @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 條回覆 最後回覆 回覆 引用 0
                              • fredo6F 離線
                                fredo6
                                最後由 編輯

                                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 條回覆 最後回覆 回覆 引用 0
                                • N 離線
                                  N Lindenthal
                                  最後由 編輯

                                  @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 條回覆 最後回覆 回覆 引用 0
                                  • fredo6F 離線
                                    fredo6
                                    最後由 編輯

                                    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 條回覆 最後回覆 回覆 引用 0
                                    • N 離線
                                      N Lindenthal
                                      最後由 編輯

                                      @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 條回覆 最後回覆 回覆 引用 0
                                      • fredo6F 離線
                                        fredo6
                                        最後由 編輯

                                        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 條回覆 最後回覆 回覆 引用 0
                                        • N 離線
                                          N Lindenthal
                                          最後由 編輯

                                          @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 條回覆 最後回覆 回覆 引用 0
                                          • fredo6F 離線
                                            fredo6
                                            最後由 編輯

                                            Which error do you get?

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement