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

    Why is my Group deleted while trying to add entities to it?

    Scheduled Pinned Locked Moved Developers' Forum
    20 Posts 6 Posters 617 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.
    • T Offline
      tiktuk
      last edited by

      @thomthom said:

      I'm guessing it refer to line 58. I'm pretty sure it's because you make the containing group after you make the child entities. Refactor the code so you always create the container first - and then add the entities to the container.

      strut = create_strut edge, center, size frame_group.entities.add_group strut

      I think you are onto something there. I just noticed that I am creating an extra group in that line, that I don't want or need. create_strut is already returning a group and I have used add_group strut to add that group to my frame_group but while doing that I am creating another group.

      How do I add an existing entity (a group in this instance), to an existing group? I can't find any methods for that.

      1 Reply Last reply Reply Quote 0
      • T Offline
        tiktuk
        last edited by

        @tig said:

        Only ever use entities.add_group(ents) when 'entities' is the 'model.active_entities' and 'ents' are also in the 'model.active_entities'...
        Something like...
        frame_group = Sketchup.active_model.active_entities.add_group() for edge in edges frame_group.entities.add_group(self.create_strut(edge,center,size)) end
        might be better...

        Thanks TIG. Did not solve this problem but learning about 'active_entities' was very useful. Was wondering why my struts were being created outside the active component before..

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

          Above:

          delete line 44

          change line 42 to:
          create_strut edge, center, size, frame_group

          change line 3 to:
          def create_strut edge, center, size, frame_group

          change line 4 to:
          entities = frame_group.entities

          πŸ˜„

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • K Offline
            kwalkerman
            last edited by

            @tiktuk said:

            @thomthom said:

            I'm guessing it refer to line 58. I'm pretty sure it's because you make the containing group after you make the child entities. Refactor the code so you always create the container first - and then add the entities to the container.

            strut = create_strut edge, center, size frame_group.entities.add_group strut

            I think you are onto something there. I just noticed that I am creating an extra group in that line, that I don't want or need. create_strut is already returning a group and I have used add_group strut to add that group to my frame_group but while doing that I am creating another group.

            How do I add an existing entity (a group in this instance), to an existing group? I can't find any methods for that.

            I'm not aware of any way to do this directly. If it's a group you want to add you can say:

            new_group.entities.add_instance original_group.entities.parent
            

            (original_group.entities.parent will give you the original group's definition, so you add it like you're adding a component instance)

            as far as adding individual items to a group, you can do it this way, but it's a little iffy:

            entities_i_want_to_add = []   #array of entities you want to add to the new group
            new_group = entities.add_group entities_i_want_to_add
            

            then I guess you could find the definition of this new group and add it to whatever entities collection you want, then explode the original

            The most foolproof way to add entities to a group is to add the raw geometry using Entities.add_edges and Entities.add_face. You'd also need to copy the materials and any attributes you wanted to go along with them.

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

              As Karen said...
              You can replicate the faces, edges etc inside a new group [erasing the originals if desired], but you ought to include all materials, layers, smoothed/softened/hidden-settings and attributes etc to make a perfect clone.
              You can 'safely' use group=entities.add_group(array_of_stuff) BUT only if the new group and all of the elements in the array are in the same 'context' AND that is the model.active_entities... you can check for that, or if for example you are working on a preselection you know that it is the active_entities, so this will work...
              new_group=model.active_entities.add_group(model.selection.to_a)

              Then you could 'move' that into any other group/component's entities by getting a reference to its definition [components, groups and images are each instances of variations of a 'definition'].
              Let's assume you have another group called 'receiver_group'...
              tr=Geom::Transformation.new() new_group_clone=receiver_group.entities.add_instance(new_group.entities.parent, tr)
              So the selection has been grouped, that group has been cloned inside another group.

              To tidy up use new_group.erase! - you could also use new_group_clone.explode so the selection is loose inside the 'receiver_group'...

              TIG

              1 Reply Last reply Reply Quote 0
              • T Offline
                tiktuk
                last edited by

                Thanks everybody, this forum is splendid, got it working thanks to your explanations πŸ‘ . Ended up just collecting the groups in an array an creating a group with that. Works perfectly.

                <span class="syntaxdefault">def&nbsp;create_frame&nbsp;edges</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">center</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">size<br />&nbsp;&nbsp;all_struts&nbsp;</span><span class="syntaxkeyword">=&nbsp;[]<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">edges</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_with_index&nbsp;</span><span class="syntaxkeyword">{&nbsp;|&nbsp;</span><span class="syntaxdefault">edge</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">index&nbsp;</span><span class="syntaxkeyword">|<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">strut&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">create_strut&nbsp;edge</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">center</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">size<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;props&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">strut_properties&nbsp;edge</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strut</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">props</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"name"</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">strut</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">props</span><span class="syntaxkeyword">[</span><span class="syntaxstring">"color"</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">all_struts&nbsp;</span><span class="syntaxkeyword"><<&nbsp;</span><span class="syntaxdefault">strut<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">frame_group&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group&nbsp;all_struts<br />&nbsp;&nbsp;&nbsp;&nbsp;frame_group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxstring">"Geodome&nbsp;frame"<br /></span><span class="syntaxdefault">end</span>
                

                I see that your idea would work too, Dan, thanks!

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

                  @tiktuk said:

                  I see that your idea would work too, Dan, thanks!

                  Your welcome.

                  It is a normal way to pass the context into a method, that creates geometry, so that the method can create the geometry in any context (model or group or component.)

                  It makes your methods more flexible, so that they can be reused for multiple commands and plugins.

                  I'm not here much anymore.

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

                    I second what Dan said.

                    When you model in SketchUp you draw then group.

                    When you use the API you make the group and then draw entities within that group. It's the clean and safe way of grouping. It's also faster, because you add the entities directly into the context where you want then. If you take existing geometry and group them it takes extra processing to move them.

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

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

                      @th3lurker said:

                      Also, even if you don't use it, group.add_faces_from_mesh and group.fill_from_mesh seem to delete everything existent in that group.

                      Only group.fill_from_mesh - as described in the API docs for the methods.

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

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        th3lurker
                        last edited by

                        Also, even if you don't use it, group.entities.add_faces_from_mesh and group.entities.fill_from_mesh seem to delete everything existent in that group.
                        Edit: thomthom corrected me, sorry. Also, i wrote group.fill_from_mesh instead of group.ENTITIES.fill_from_mesh.
                        I'm going to crawl back to my hole now, one post, two mistakes is quite embarrassing. 😒

                        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