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

    How to get entities from selection?

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 4 Posters 588 Views 4 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.
    • lbsswuL Offline
      lbsswu
      last edited by

      I have a selection object with several entities, I am confused about how could I get all the entities from the selection object?

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

        You mean into an array? The Selection object is an Enumerable so you can iterate it as normal. It also have a .to_a method so it can often be mixed with arrays.

        How do you want to "get" the entities? Iterate them?

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

        1 Reply Last reply Reply Quote 0
        • lbsswuL Offline
          lbsswu
          last edited by

          @thomthom said:

          You mean into an array? The Selection object is an Enumerable so you can iterate it as normal. It also have a .to_a method so it can often be mixed with arrays.

          How do you want to "get" the entities? Iterate them?

          I just want to inplement the following code:

          entities = selection.entities (this function is what I wanted, but not exist)
          group = entities.add_group
          group.add entities (this function is what I wanted, but not exist)
          bounds = group.bounds
          center = bounds.center

          Is there any way?

          1 Reply Last reply Reply Quote 0
          • A Offline
            Aerilius
            last edited by

            Yes:

            entities = selection.entities #
            group = entities.add_group
            # "The entities method is used to retrieve
            # a collection of entities in the group."
            group.entities.add entities
            bounds = group.bounds
            center = bounds.center
            

            Sketchup::Group, Sketchup::ComponentDefinition and Sketchup::Model are similar "containers" for entities (in fact components are models in a model; and a model can be inserted as component).

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

              If you want to group a selection then it's safe to do it as the group is made as the selection must be in the active_entities [cross-threading group/entities will splat!]
              group=model.active_entities.add_group(model.selection.to_a) center=group.bounds.center

              However, if you are doing it to find the center of a selection and then exploding the temporary group you could do it different ways, e.g. ...
              bb=Geom::BoundingBox.new model.selection.each{|e| bb.add(e.bounds) } center=bb.center
              You can of course filter objects for the added bounds, by including tests inside the {}, e.g. to include only groups insert an early
              next unless e.is_a?(Sketchup::Group)

              TIG

              1 Reply Last reply Reply Quote 0
              • lbsswuL Offline
                lbsswu
                last edited by

                @aerilius said:

                Yes:

                entities = selection.entities #
                > 
                

                ...

                The first line has a "NoMethodError" error...

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

                  Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
                  I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
                  Then the 'group'.entities would have worked...

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • lbsswuL Offline
                    lbsswu
                    last edited by

                    @tig said:

                    Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
                    I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
                    Then the 'group'.entities would have worked...

                    Thanks TIG! I have tried your suggestions. It works, but maybe there is a bug in my script, I did not get the correct coordinates.

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

                      @lbsswu said:

                      @tig said:

                      Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
                      I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
                      Then the 'group'.entities would have worked...

                      Thanks TIG! I have tried your suggestions. It works, but maybe there is a bug in my script, I did not get the correct coordinates.
                      Without at least a few clues we can't help you much further πŸ˜•
                      If you are trying to find the center of a selection of objects then my examples will return the bounds.center
                      You never explained properly what it is you are trying to do...
                      We answer your query only to find it's not the answer to the question you ought to have asked really πŸ˜’
                      Can you explain what you seek and we might help with a path to get there.
                      Sometimes you seem to have gone half way down the wrong road and then ask us how to get to you destination by carrying on, when really you need to go back to the start and set off in the correct direction as it'll be much easier/quicker...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • lbsswuL Offline
                        lbsswu
                        last edited by

                        @tig said:

                        @lbsswu said:

                        @tig said:

                        Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
                        I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
                        Then the 'group'.entities would have worked...

                        Thanks TIG! I have tried your suggestions. It works, but maybe there is a bug in my script, I did not get the correct coordinates.
                        Without at least a few clues we can't help you much further πŸ˜•
                        If you are trying to find the center of a selection of objects then my examples will return the bounds.center
                        You never explained properly what it is you are trying to do...
                        We answer your query only to find it's not the answer to the question you ought to have asked really πŸ˜’
                        Can you explain what you seek and we might help with a path to get there.
                        Sometimes you seem to have gone half way down the wrong road and then ask us how to get to you destination by carrying on, when really you need to go back to the start and set off in the correct direction as it'll be much easier/quicker...

                        My task is get the parts' center of a 3D car model. I find the problem which posted in the website:
                        http://forums.sketchucation.com/viewtopic.php?f=180&t=46520&p=416248#p416248

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

                          Sample test model?

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

                          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