# How to find parent group in a model?

**URL:** <https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802>\
**Category:** Ruby API\
**Created:** [October 26, 2015, 8:51pm UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802 "2015-10-26T20:51:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![weddins](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/weddins/32/5861_2.png) [@weddins](https://forums.sketchup.com/u/weddins)\
**Post date:** [October 26, 2015, 8:51pm UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/1 "2015-10-26T20:51:32Z")

</div>

In SketchUp Pro 2015, I’ve got a two ComponentInstances, contained within a group. When selecting a ComponentInstance, I want to be able to go up the heirarchy, looking at the parent groups. However, `entity#parent` is not working as I’d expect:

```
Sketchup.active_model.entities[0]
#<Sketchup::Group:0x000000086a2bd0>
Sketchup.active_model.entities[0].entities[0]
#<Sketchup::ComponentInstance:0x000000085a0bb0>
Sketchup.active_model.entities[0].entities[0].parent
#<Sketchup::ComponentDefinition:0x000000086a2c70>

```

As you can see above, the parent of a ComponentInstance gives me it’s definition instead of it’s parent. Is there another method for getting the container object?

---

<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:** [October 26, 2015, 9:06pm UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/2 "2015-10-26T21:06:23Z")

</div>

Unfortunately, no simple way. SketchUp keeps track of outside-to-inside nesting because that’s what it needs to draw Entities, but not inside-to-outside, which the engine doesn’t need. Various people have tackled this problem, though none of the solutions are a simple one-liner. Perhaps one of them will chime in…

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [October 26, 2015, 10:12pm UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/3 "2015-10-26T22:12:52Z")

</div>

> [@weddins](#):
>
> As you can see above, the parent of a ComponentInstance gives me it’s definition instead of it’s parent.

Because definitions have entities collections, and instances do not.

What may be throwing you off (a bit) here, is that the **`Group`** class has an API “shortcut” method that is basically:

```ruby
def entities()
  self.definition.entities
end

```

This is because groups are really “special” component instances, whose definitions are hidden from the listing in the Component Manager.  
So groups (being instances) do not _really_ own entities either. It is their definitions that “own” the entities collection.

* * *

**Secondly** , you are _likely_ confounding two separate API methods of the same name. Just because they both use the name “parent” does not mean they do the same thing. (There is, at this time, at least 4 different “parent” methods in the API.)

So calling the **`parent`** upon an instance of the **`Sketchup::Entities`** collection class, can return different object references than calling the same method upon a **`Sketchup::Entity`** subclass instance. Likewise if called upon a **`Sketchup::AttributeDictionary`** object (which always returns an instance of a **`Sketchup::AttributeDictionaries`** collection.)

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [October 26, 2015, 10:26pm UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/4 "2015-10-26T22:26:55Z")

</div>

All that being said, the easiest way to get the instance chain, is when the bottom instance is the active editing context.

Then you can use [Sketchup::Model#active\_path](http://www.sketchup.com/intl/en/developer/docs/ourdoc/model#active_path)

But yet there is no way to (easily) set the current editing context in Ruby. The API defines a method to exit or back out of editing contexts, but not enter them.

So I believe that hacks are done .

You save the current selection, and the active context. Then use a system call to cause (simulate) a mouse click upon the object you want until the **`model.active_entities == target.definition.entities`** then get the active editing path from the method described above.

Afterward you do an undo _n_ number of times, or back out of the editing context and restore the previous one.

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [October 26, 2015, 10:40pm UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/5 "2015-10-26T22:40:57Z")

</div>

[This](https://gist.github.com/jimfoltz/79844bb2e01bdce3b92d) may provide some ideas. There may be better methods, but I just map out the entire model.

This maps the entire model into an array of Instance _Nodes_ which have a reference the `ComponentInstance` and its `path` (or parent Instances). From the `path` you can get to the parent instance, and also calculate the instance’s transformation in world coordinates.

Use a Hash instead of an Array for faster lookups. I included the Instance name to be able to find instances by name. You can include whatever data about the instances you need.

Example of a simple Node array:

```auto
[#<Node:0x0000000b6c7d60
  @ins=#<Sketchup::ComponentInstance:0x0000000b520430>,
  @name="Parent",
  @path=[]>,
 #<Node:0x0000000b6c74a0
  @ins=#<Sketchup::ComponentInstance:0x0000000b59acf8>,
  @name="Child 1",
  @path=[#<Sketchup::ComponentInstance:0x0000000b520430>]>,
 #<Node:0x0000000b6c6e10
  @ins=#<Sketchup::ComponentInstance:0x0000000b5d4390>,
  @name="Grand Child 1",
  @path=
   [#<Sketchup::ComponentInstance:0x0000000b520430>,
    #<Sketchup::ComponentInstance:0x0000000b59acf8>]>,
 #<Node:0x0000000b6c6938
  @ins=#<Sketchup::ComponentInstance:0x0000000b5a02e8>,
  @name="Child 2",
  @path=[#<Sketchup::ComponentInstance:0x0000000b520430>]>]

```

 ![](https://global.discourse-cdn.com/sketchup/original/2X/5/565b152ca1ec1b9921a650590f5cdbf896a3ce70.png)

---

<div class="post-metadata">

**Author:** ![pagejaws](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/pagejaws/32/38257_2.png) [@pagejaws](https://forums.sketchup.com/u/pagejaws)\
**Post date:** [September 26, 2017, 8:29pm UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/6 "2017-09-26T20:29:11Z")

</div>

In the parent instances array will be a component definition that is the group. The component definition will have the method group? set true. Since this is a top down lost of parents, the last item in the list should be the parent group if one is available.

```auto
group = entity.parent.instances[-1] if entity && entity.parent.typename == "ComponentDefinition" && entity.parent.group?

```

Should return the group, or nil if it isn’t in a group. Remove the entity.parent.group? if you want the parent component instead.

example:

```auto
e
#<Sketchup::Edge:0x00026066c089d8>
g = e.parent.instances[-1] if e && e.parent.typename == "ComponentDefinition" && e.parent.group?

#<Sketchup::Group:0x00026066c10c78>

```

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [September 28, 2017, 11:36am UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/7 "2017-09-28T11:36:11Z")

</div>

> [@pagejaws](#):
>
> typename

Side-note: avoid using `.typename` - it’s _very_ slow. Prefer `.is_a?` instead.

---

<div class="post-metadata">

**Author:** ![pagejaws](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/pagejaws/32/38257_2.png) [@pagejaws](https://forums.sketchup.com/u/pagejaws)\
**Post date:** [September 29, 2017, 2:01am UTC](https://forums.sketchup.com/t/how-to-find-parent-group-in-a-model/16802/8 "2017-09-29T02:01:33Z")

</div>

Cool Thanks
