# Does $dc\_observers.get\_latest\_class.redraw\_with\_undo(instance) only work with Sketchup::ComponentInstance?

**URL:** <https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158>\
**Category:** Dynamic Components\
**Tags:** group, groups\
**Created:** [February 25, 2024, 2:30am UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158 "2024-02-25T02:30:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tenquin](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tenquin](https://forums.sketchup.com/u/tenquin)\
**Post date:** [February 25, 2024, 2:30am UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158/1 "2024-02-25T02:30:46Z")

</div>

I have a group with a LenX attribute, where I am setting any value with  
`instance.set_attribute(DYDICT, key, value)`

Then with the code `$dc_observers.get_latest_class.redraw_with_undo(instance)` I try to redraw it but it doesn’t work.

Is there a way to redraw an instance of Sketchup::Group?  
I notice that in the ‘Component Options’ this works.

Thanks… 🙂

---

<div class="post-metadata">

**Author:** ![ryan.moore](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ryan.moore/32/106089_2.png) [@ryan.moore](https://forums.sketchup.com/u/ryan.moore)\
**Post date:** [February 25, 2024, 11:16am UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158/2 "2024-02-25T11:16:00Z")

</div>

Redraw should work on groups or components. Can you post your code?

EDIT: Group attributes are kept in the ~~definition~~ instance, where components have attributes at the instance and definition level, this is ~~likely~~ probably not your issue… 🤷‍♂️ You have to set the group attribute at the ~~definition~~ instance.

EDIT2: I had it backwards, corrected above. Thanks @DanRathbun!

> [@How to set a dynamic component attribute using the API](https://forums.sketchup.com/t/how-to-set-a-dynamic-component-attribute-using-the-api/100067/2):
>
> The DC **definition** ’s “dynamic\_attributes” dictionary contains the **default** values and formulae for ALL instances.
> 
> The DC **instance** ’s “dynamic\_attributes” dictionary contains ANY **overridden** dynamic attribute values **for that particular instance**.
> 
> Any nested DC groups have ALL attributes in the group **instance** ’s “dynamic\_attributes” dictionary, **rather than** the group’s definition.

---

<div class="post-metadata">

**Author:** ![tenquin](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tenquin](https://forums.sketchup.com/u/tenquin)\
**Post date:** [February 25, 2024, 1:28pm UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158/3 "2024-02-25T13:28:42Z")

</div>

```auto
DYDICT ||= 'dynamic_attributes'
model = Sketchup.active_model
sel = model.selection

def self.set_instance_or_definition_attributes(instance, key, value)
  val = instance.get_attribute(DYDICT, key, nil)
  instance.set_attribute(DYDICT, key, value) unless val.nil?
  puts "val: #{val}"
  puts "#{key}, #{value}"

  val = instance.definition.get_attribute(DYDICT, key, nil)
  instance.definition.set_attribute(DYDICT, key, value) unless val.nil?
  puts "val: #{val}"
  puts "#{key}, #{value}"
end

def self.set_instance_attributes(entities)
  entities.grep(Sketchup::ComponentInstance).concat(entities.grep(Sketchup::Group)).each do |instance|
    key = 'lenx'
    value = (800 / 25.4).to_s

    set_instance_or_definition_attributes(instance, key, value)
    $dc_observers.get_latest_class.redraw_with_undo(instance)
  end
rescue StandardError => error
  puts error.backtrace
  raise
end

set_instance_attributes(sel)

```

 ![Redraw Test](https://global.discourse-cdn.com/sketchup/original/3X/c/8/c86ed2c86e2a3ad85561baabee12bba09f2cf71f.png)  
[Redraw Test.skp](https://forums.sketchup.com/uploads/short-url/gdX3SZcc8PsfjC78WuwlKvO68z1.skp) (853.4 KB)

See the code example. I also attached the test model and now not only the group but the component instance doesn’t seem to work.

It is possible to see with ‘Attribute Inspector’ that the value was set correctly, but it is not redrawn.

---

<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:** [February 25, 2024, 1:57pm UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158/4 "2024-02-25T13:57:58Z")

</div>

![image](https://global.discourse-cdn.com/sketchup/original/3X/6/c/6cfece049e351b4dc72e06e244113b53929fcd36.png)

However you set it as string:

> [@tenquin](#):
>
> ```auto
> key = 'lenx'
> value = (800 / 25.4).to_s
> 
> ```

Try this:

```auto
    key = 'lenx'
    value = 800 / 25.4

```

---

<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:** [February 25, 2024, 2:10pm UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158/5 "2024-02-25T14:10:38Z")

</div>

> [@tenquin](#):
>
> I have a group with a LenX attribute, …

Dynamic groups **must** be **nested** within a dynamic component. I.e., the dynamic group **cannot** be the progenitor dynamic object.

> [@tenquin](#):
>
> I also attached the test model and now not only the group but the component instance doesn’t seem to work.

Secondly, … if there is only one instance of a dynamic component, then **all** dynamic attributes will be on the definition and the instance will use it’s definition’s attributes.

---

<div class="post-metadata">

**Author:** ![tenquin](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tenquin](https://forums.sketchup.com/u/tenquin)\
**Post date:** [February 25, 2024, 4:42pm UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158/6 "2024-02-25T16:42:30Z")

</div>

I tried this way, but I was not successful either.

---

<div class="post-metadata">

**Author:** ![tenquin](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tenquin](https://forums.sketchup.com/u/tenquin)\
**Post date:** [February 25, 2024, 4:47pm UTC](https://forums.sketchup.com/t/does-dc-observers-get-latest-class-redraw-with-undo-instance-only-work-with-sketchup-componentinstance/261158/7 "2024-02-25T16:47:07Z")

</div>

The only way I was able to do this was to add a formula attribute to the component instance and definition, and then remove it from the attribute dictionary.  
I also had to do a redraw before removing it from the dictionary.

```auto
DYDICT ||= 'dynamic_attributes'
model = Sketchup.active_model
sel = model.selection

# Removes an attribute from a dictionary.
# @param [AttributeDictionary] dictionary - The dictionary to remove attribute.
# @param [Array<String>] attributes - Array of attributes to remove.
def self.remove_dynamic_attribute(dict, attributes = [])
  return if attributes.empty?

  attributes.each do |attribute|
    dict&.delete_key(attribute)
  end
end

def self.set_instance_or_definition_attributes(instance, key, value)
  definition = instance.definition
  formula = "_#{key}_formula"

  instance.set_attribute(DYDICT, formula, value)
  definition.set_attribute(DYDICT, formula, value)
  $dc_observers.get_latest_class.redraw_with_undo(instance)

  dict = instance.attribute_dictionary(DYDICT) # instance dictionary
  definition_dict = definition.attribute_dictionary(DYDICT) # definition dictionary

  remove_dynamic_attribute(dict, [formula, "_#{key}_error"])
  remove_dynamic_attribute(definition_dict, [formula, "_#{key}_error"])
end

def self.set_instance_attributes(entities)
  entities.grep(Sketchup::ComponentInstance).concat(entities.grep(Sketchup::Group)).each do |instance|
    key = 'lenx'
    value = (800 / 10).to_s

    set_instance_or_definition_attributes(instance, key, value)
  end
rescue StandardError => error
  puts error.backtrace
  raise
end

set_instance_attributes(sel)

```
