# Dynamic component to group

**URL:** <https://forums.sketchup.com/t/dynamic-component-to-group/92441>\
**Category:** Ruby API\
**Created:** [April 3, 2019, 10:11pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441 "2019-04-03T22:11:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexey.umrikhin](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@alexey.umrikhin](https://forums.sketchup.com/u/alexey.umrikhin)\
**Post date:** [April 3, 2019, 10:11pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/1 "2019-04-03T22:11:14Z")

</div>

Hello. I’am trying to make my projects a little bit lighter by replacing dynamic components by usual ones.

I want to explode a dynamic component then make a group from it and then make a usual component again.

```
                selection = $SpawnedComponent.explode
                puts "COMPONENT EXPLODED #{selection}"
                Sketchup.active_model.definitions.purge_unused
                Sketchup.active_model.active_entities.add_group(selection)

```

 ![05](https://global.discourse-cdn.com/sketchup/original/3X/7/f/7f598b8e96cd927f321e858c5d2985848b01e3aa.jpeg)  
My way doesn’t work because some dynamic component information stays. Please be adviced how could I avoid it.

---

<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:** [April 3, 2019, 10:34pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/2 "2019-04-03T22:34:04Z")

</div>

1. Do not use global variables. Use a module variable or a instance variable within our own modules or classes.

2. The only thing that makes a component dynamic is an attribute dictionary named “dynamic\_attributes”.

```ruby
selected_component_instance.attribute_dictionaries.delete("dynamic_attributes")
selected_component_instance.definition.attribute_dictionaries.delete("dynamic_attributes")

```

---

<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:** [April 3, 2019, 10:37pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/3 "2019-04-03T22:37:32Z")

</div>

If you wish to treat the whole model, then …

```ruby
Sketchup.active_model.definitions.each do |cdef|
  cdef.attribute_dictionaries.delete("dynamic_attributes")
  cdef.instances.each {|inst|
    inst.attribute_dictionaries.delete("dynamic_attributes")
  }
end

```

---

<div class="post-metadata">

**Author:** ![alexey.umrikhin](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@alexey.umrikhin](https://forums.sketchup.com/u/alexey.umrikhin)\
**Post date:** [April 3, 2019, 11:15pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/4 "2019-04-03T23:15:11Z")

</div>

Tried different variations but doesn’t work… Probably I’am doing something wrong but tried both in console and in script… Result still the same 🙄

 ![34](https://global.discourse-cdn.com/sketchup/original/3X/c/7/c715413639e8ae87fb857fa3c97a0a3b818b0f03.png)

---

<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:** [April 3, 2019, 11:46pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/5 "2019-04-03T23:46:27Z")

</div>

> [@alexey.umrikhin](#):
>
> Probably I’am doing something wrong

Yes you are. The [`Sketchup::Selection`](http://ruby.sketchup.com/Sketchup/Selection.html) object does not have an [`attribute_dictionaries`](http://ruby.sketchup.com/Sketchup/Entity.html#attribute_dictionaries-instance_method) method.

Only subclasses of [`Sketchup::Entity`](http://ruby.sketchup.com/Sketchup/Entity.html) have [this method](http://ruby.sketchup.com/Sketchup/Entity.html#attribute_dictionaries-instance_method). So you’ll need to get an entity object FROM the selection and verify that it `responds_to?(:attribute_dictionaries)`.

Use a recursive method …

```ruby
def undynamicize(obj)
  return if obj.attribute_dictionaries.nil?
  obj.attribute_dictionaries.delete("dynamic_attributes")
  cdef = obj.definition
  cdef.attribute_dictionaries.delete("dynamic_attributes") unless cdef.attribute_dictionaries.nil?
  cdef.instances.each { |inst|
    inst.attribute_dictionaries.delete("dynamic_attributes") unless inst.attribute_dictionaries.nil?
  }
  cdef.entities.each { |ent|
    undynamicize(ent) if ent.respond_to?(:definition)
  }
end

```

Then in other code use it …

```ruby
UI.add_context_menu_handler do |context_menu|
  selection = Sketchup.active_model.selection
  if selection.single_object? &&
  selection.first.responds_to?(:definition) &&
  selection.first.attribute_dictionary("dynamic_attributes",false)
    context_menu.add_item("Undynamicize Component") {
      undynamicize(selection.first)
    }
end

```

---

<div class="post-metadata">

**Author:** ![alexey.umrikhin](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@alexey.umrikhin](https://forums.sketchup.com/u/alexey.umrikhin)\
**Post date:** [April 4, 2019, 8:43am UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/6 "2019-04-04T08:43:27Z")

</div>

Thank’s a lot. That’s a perfect solution in my case!

---

<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:** [April 4, 2019, 7:45pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/7 "2019-04-04T19:45:08Z")

</div>

No problem. Be aware that some times for very complex models with many nesting levels, that the recursive pattern in the example above may fail with a Stack Level Too Deep error.

Also, the example is meant to be inside your namespace / plugin modules according to good behavior in a shared environment.

> **[SketchUp Plugin Checklist](http://www.thomthom.net/thoughts/2013/02/sketchup-plugin-checklist/)**
>
> Here’s a simple overview of important items to remember when developing and distributing a SketchUp plugin. Where I have already written about the topic or other sources exists it’s bee…

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [April 4, 2019, 11:55pm UTC](https://forums.sketchup.com/t/dynamic-component-to-group/92441/8 "2019-04-04T23:55:21Z")

</div>

An alternative solution?

Christina Eneroth @eneroth3 has a plugin that seems to do most of what you want, to remove DC elements from a component:  
[https://extensions.sketchup.com/en/content/eneroth-de-dc-ify](https://extensions.sketchup.com/en/content/eneroth-de-dc-ify)

It doesn’t make groups, though, just components.
