• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

How to get entities from selection?

Scheduled Pinned Locked Moved Developers' Forum
11 Posts 4 Posters 579 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.
  • L Offline
    lbsswu
    last edited by 13 Jul 2012, 01:42

    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
    • T Offline
      thomthom
      last edited by 13 Jul 2012, 10:44

      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
      • L Offline
        lbsswu
        last edited by 14 Jul 2012, 05:29

        @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 14 Jul 2012, 06:30

          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
          • T Offline
            TIG Moderator
            last edited by 14 Jul 2012, 09:14

            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
            • L Offline
              lbsswu
              last edited by 14 Jul 2012, 12:13

              @aerilius said:

              Yes :

              entities = selection.entities #
              > 
              

              ...

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

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 14 Jul 2012, 14:24

                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
                • L Offline
                  lbsswu
                  last edited by 15 Jul 2012, 00:48

                  @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
                  • T Offline
                    TIG Moderator
                    last edited by 15 Jul 2012, 09:35

                    @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
                    • L Offline
                      lbsswu
                      last edited by 17 Jul 2012, 03:26

                      @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
                      • T Offline
                        thomthom
                        last edited by 17 Jul 2012, 07:52

                        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
                        1 / 1
                        • First post
                          1/11
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement