# Creating a group inside a component

**URL:** <https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784>\
**Category:** Ruby API\
**Created:** [September 4, 2025, 9:02am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784 "2025-09-04T09:02:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 4, 2025, 9:02am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/1 "2025-09-04T09:02:24Z")

</div>

Hi there folks, noob coder here so thank you in advance for any help!  
I’m trying to group some geometry inside a component definition, but I’m seeing unexpected behavior. Imagine you have selected a component that contains some loose faces and edges. Then:

```auto
model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition
ents = defi.entities

geometry = ents.select { |e| e.is_a?(Sketchup::Edge) || e.is_a?(Sketchup::Face) }

```

Now, if I do something like this, it creates an **empty group inside the component** :

```auto
group = ents.add_group

```

But if I do this, it creates a group **with the elements from `geometry` outside the component** :

```auto
group = ents.add_group(geometry)

```

Is there a way to create a group **inside the component** that includes the `geometry`?

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [September 4, 2025, 11:36am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/2 "2025-09-04T11:36:53Z")

</div>

It is a bug. I guess, it is related to similar bug reported a long time ago here:  
[Entities#add\_group with arguments not working when not used in the active drawing context · Issue #42 · SketchUp/api-issue-tracker](https://github.com/SketchUp/api-issue-tracker/issues/42)

---

<div class="post-metadata">

**Author:** ![ene\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ene_su/32/103091_2.png) [@ene\_su](https://forums.sketchup.com/u/ene_su)\
**Post date:** [September 4, 2025, 12:51pm UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/3 "2025-09-04T12:51:41Z")

</div>

The preferred way to create groups using the API is to first create an empty group and then draw the content to it. Grouping existing geometry sadly only works at the active entities.

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 4, 2025, 3:16pm UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/4 "2025-09-04T15:16:38Z")

</div>

Thank you @ene_su! I think that’s the key: using active\_path. It works now!

```auto
    model = Sketchup.active_model
    selection = model.selection

    comp = selection.first
    defi = comp.definition

    model.active_path = [comp]

    ents1 = defi.entities
    ents2 = ents1.to_a

    group = ents1.add_group(ents2)

```

Thank you @dezmo and @ene_su for answers!  
Have a nice day

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 4, 2025, 3:48pm UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/5 "2025-09-04T15:48:23Z")

</div>

Just to add a bit more info: Actually, that code from my previous post will group everything inside the component.

After some trial and error, this version only groups loose edges and faces. Everything else, like other groups or components, remains unchanged.

```auto
model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition

model.active_path = [comp]

ents = defi.entities
geom = ents.select { |e| e.is_a?(Sketchup::Edge) || e.is_a?(Sketchup::Face) }

group = ents.add_group(geom.to_a)

```

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [September 4, 2025, 3:52pm UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/6 "2025-09-04T15:52:12Z")

</div>

Just FYI, the grep method is faster than your iteration block over is\_a?

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 5, 2025, 6:17am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/7 "2025-09-05T06:17:58Z")

</div>

Hi @slbaumgartner, thank you for the tip! I’ve updated the code below to reflect that, in case someone else finds this helpful later on.

```auto
model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition

model.active_path = [comp]

ents = defi.entities
geom = ents.grep(Sketchup::Edge) + ents.grep(Sketchup::Face)

group = ents.add_group(geom.to_a)

```

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [September 5, 2025, 7:22am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/8 "2025-09-05T07:22:18Z")

</div>

Some other thoughts:

> [@alsomar](#):
>
> `geom = ents.grep(Sketchup::Edge) + ents.grep(Sketchup::Face)`

can be done like

`geom = ents.grep(Sketchup::Edge).concat( ents.grep(Sketchup::Face) )`

[Array#+](https://ruby-doc.org/3.3.2/Array.html#method-i-2B) : Returns a new array built by concatenating the two arrays together to produce a third array.  
[Array#conca](https://ruby-doc.org/3.3.2/Array.html#method-i-concat)t : Appends the elements of other\_ary to self.

So, the + operator will create a new array each time it is called (which is “more expensive”), while concat only appends the new element.  
Actually, I don’t think it would mean too much in your case, I just want to show some other possibilities…  
\_\_

> [@alsomar](#):
>
> `geom.to_a`

[Enumerable#grep](https://ruby-doc.org/3.3.2/Enumerable.html#method-i-grep) returs Array, so the `geom` already is an instance of Array, therefore [Array#to\_a](https://ruby-doc.org/3.3.2/Array.html#method-i-to_a) does not really make any sense here.

\_\_

> [@alsomar](#):
>
> `model.active_path = [comp]`

You might consider “recording” the active path before changing it and restoring it to the original when you’re done.

```ruby
original_path = model.active_path
model.active_path = [comp]

# do the things

model.active_path = original_path

```

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 5, 2025, 8:06am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/9 "2025-09-05T08:06:38Z")

</div>

Thanks a lot, @dezmo!  
As you pointed out, it might not make much difference in this tiny example, but it’s definitely something useful to keep in mind for larger scripts. Also, restoring the original path at the end makes the `active_path` change invisible for the user, that’s really smart! Thank you again!

So the updated version of the snippet would be:

```auto
model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition

og_path = model.active_path
model.active_path = [comp]

ents = defi.entities
geom = ents.grep(Sketchup::Edge).concat(ents.grep(Sketchup::Face))

group = ents.add_group(geom)
model.active_path = og_path
```

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [September 5, 2025, 8:23am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/10 "2025-09-05T08:23:02Z")

</div>

One more thing 😉 : Don’t forget about [Undo Stack](https://ruby.sketchup.com/file.extension_requirements.html#label-Undo+Stack) either.

The [Model#start\_operation](https://ruby.sketchup.com/Sketchup/Model.html#start_operation-instance_method) method and the [Model#commit\_operation](https://ruby.sketchup.com/Sketchup/Model.html#commit_operation-instance_method) method…

```ruby
model = Sketchup.active_model
selection = model.selection
comp = selection.first
defi = comp.definition
ents = defi.entities
geom = ents.grep(Sketchup::Edge).concat(ents.grep(Sketchup::Face))
og_path = model.active_path

model.start_operation("Loose geom to group", true)
model.active_path = [comp]
group = ents.add_group(geom)
model.active_path = og_path
model.commit_operation

```

---

<div class="post-metadata">

**Author:** ![ene\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ene_su/32/103091_2.png) [@ene\_su](https://forums.sketchup.com/u/ene_su)\
**Post date:** [September 5, 2025, 8:29am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/11 "2025-09-05T08:29:35Z")

</div>

Is the extension meant to group some content the user has created previously, or something the extension itself is creating?

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 5, 2025, 8:33am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/12 "2025-09-05T08:33:03Z")

</div>

In this case, it groups the loose geometry inside a selected component. Imagine you have a component with several elements such as subcomponents, groups, and loose faces. The extension will group the loose faces into a new group inside that component, helping to avoid mixing loose geometry with groups and components.

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 5, 2025, 8:42am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/13 "2025-09-05T08:42:12Z")

</div>

Thank you again! The thing is your previous answer about saving and restoring the original path made me think about the initial selection. So I save the initial selection (`initial_s`) in order to restore it at the end of the process. I’m not sure whether it should be inside or outside the commit operation.

```auto
model = Sketchup.active_model
selection = model.selection

initial_s = selection.to_a

comp = selection.first
defi = comp.definition
ents = defi.entities
geom = ents.grep(Sketchup::Edge).concat(ents.grep(Sketchup::Face))
og_path = model.active_path

model.start_operation("Loose geom to group", true)
model.active_path = [comp]
group = ents.add_group(geom)
model.active_path = og_path

selection.clear
selection.add(initial_s)

model.commit_operation

```

---

<div class="post-metadata">

**Author:** ![ene\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ene_su/32/103091_2.png) [@ene\_su](https://forums.sketchup.com/u/ene_su)\
**Post date:** [September 5, 2025, 8:45am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/14 "2025-09-05T08:45:25Z")

</div>

Okay, in that case the workaround of opening and then closing the component makes sense. If the extension was creating its own geometry, it would make more sense to add it to the group after the group was created.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [September 5, 2025, 11:57am UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/15 "2025-09-05T11:57:12Z")

</div>

💡

I would do something like this… I’ll leave it to you the fun to discover how it works and find the bug(s) if there are any… because I haven’t really tested it. 😉

```ruby
def instances(e)
  e.grep(Sketchup::Group).concat(e.grep(Sketchup::ComponentInstance))
end

def loose_geom_to_groups
  model = Sketchup.active_model
  selection = model.selection
  initial_s = selection.to_a
  og_path = model.active_path
  processed_defs = []
  model.start_operation("Loose geom to groups", true)

  instances(selection).each{|instance|
    initial_s -= [instance]
    defi = instance.definition
    next if processed_defs.include?(defi)
    processed_defs<< defi
    model.active_path = [instance]
    ents = defi.entities
    while edge = ents.find{|e| e.is_a?(Sketchup::Edge)} 
      connected = edge.all_connected
      group = ents.add_group(connected)
      ents = defi.entities
    end
  }
  
  model.active_path = og_path
  selection.clear
  selection.add(initial_s)

  model.commit_operation
  
end
loose_geom_to_groups

```

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 5, 2025, 1:52pm UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/16 "2025-09-05T13:52:02Z")

</div>

That’s great! It separates each set of connected geometry into its own group. Much better than my initial idea, thank you so much!

---

<div class="post-metadata">

**Author:** ![alsomar](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/alsomar/32/86230_2.png) [@alsomar](https://forums.sketchup.com/u/alsomar)\
**Post date:** [September 5, 2025, 3:17pm UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/17 "2025-09-05T15:17:09Z")

</div>

Hi there again! After trying out the code, I made a small fix because `selection.add(initial_s)` wasn’t working as expected. I believe it’s due to the fact that each iteration of `initial_s -= [instance]` was modifying the same array intended to restore the selection so it ends up empty by the end.

To fix this, I renamed `initial_s` to `sel_array`, and created a new `initial_s = sel_array.dup` to preserve the original selection. This way, I can safely subtract the processed instances from `sel_array` without affecting the original list. That also meant changing `initial_s -= [instance]` to `sel_array -= [instance]`.

```auto
def instances(e)
  e.grep(Sketchup::Group).concat(e.grep(Sketchup::ComponentInstance))
end

def loose_geom_to_groups
  model = Sketchup.active_model
  selection = model.selection
  sel_array = selection.to_a
  initial_s = sel_array.dup
  og_path = model.active_path
  processed_defs = []
  model.start_operation("Loose geom to groups", true)

  instances(selection).each{|instance|
    sel_array -= [instance]
    defi = instance.definition
    next if processed_defs.include?(defi)
    processed_defs<< defi
    model.active_path = [instance]
    ents = defi.entities
    while edge = ents.find{|e| e.is_a?(Sketchup::Edge)} 
      connected = edge.all_connected
      group = ents.add_group(connected)
      ents = defi.entities
    end
  }
  model.active_path = og_path
  selection.clear
  selection.add(initial_s)
  model.commit_operation
end

loose_geom_to_groups
```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [September 12, 2025, 3:17pm UTC](https://forums.sketchup.com/t/creating-a-group-inside-a-component/339784/18 "2025-09-12T15:17:52Z")

</div>

Closed on September 12, 2025, 3:17pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
