# I’m having trouble redrawing the dynamic component

**URL:** <https://forums.sketchup.com/t/i-m-having-trouble-redrawing-the-dynamic-component/256910>\
**Category:** Ruby API\
**Created:** [January 25, 2024, 12:35pm UTC](https://forums.sketchup.com/t/i-m-having-trouble-redrawing-the-dynamic-component/256910 "2024-01-25T12:35:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdrianoR](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@AdrianoR](https://forums.sketchup.com/u/AdrianoR)\
**Post date:** [January 25, 2024, 12:35pm UTC](https://forums.sketchup.com/t/i-m-having-trouble-redrawing-the-dynamic-component/256910/1 "2024-01-25T12:35:30Z")

</div>

I´m trying to make some changes via ruby console, in a component i have drawn in sketchup, heres the code:

# Get the current selection

selection = Sketchup.active\_model.selection

# Check if something is selected

if !selection.empty?

# Iterate over the selected items

selection.each do |item|  
# Check if the item is an instance of a component  
if item.is\_a?(Sketchup::ComponentInstance)  
# Change the value of the ‘acab\_pta’ attribute to the desired new value (replace ‘NewValue’ with the desired value)  
item.set\_attribute(‘dynamic\_attributes’, ‘acab\_pta’, ‘108’)  
puts “Attribute ‘acab\_pta’ changed for the selection!”

```
  # Force a screen refresh to reflect the changes
  item.model.active_view.refresh
else
  puts "The selected item isn't a Component Instance."
end

```

end  
else  
puts “Nothing is selected. Please select at least one component to apply the change.”  
end  
the problem is i am actually have to open the context menu manually and reload my component to the changes apply to the view, anyone got a hint on how to solve this?  
sorry my bad english, its not my mother language, also thank you for answering

---

<div class="post-metadata">

**Author:** ![sWilliams](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sWilliams](https://forums.sketchup.com/u/sWilliams)\
**Post date:** [January 25, 2024, 2:13pm UTC](https://forums.sketchup.com/t/i-m-having-trouble-redrawing-the-dynamic-component/256910/2 "2024-01-25T14:13:07Z")

</div>

Just making the code readable

```Ruby
selection = Sketchup.active_model.selection

if !selection.empty?
  selection.each do |item|

    # Check if the item is an instance of a component
    if item.is_a?(Sketchup::ComponentInstance)

      # Change the value of the ‘acab_pta’ attribute to the desired new value (replace ‘NewValue’ with the desired value)
      item.set_attribute(‘dynamic_attributes’, ‘acab_pta’, ‘108’)
      puts “Attribute ‘acab_pta’ changed for the selection!”

      # Force a screen refresh to reflect the changes
      item.model.active_view.refresh

    else
      puts "The selected item isn't a Component Instance."

    end
  end
else
  puts “Nothing is selected. Please select at least one component to apply the change.”
end

```

---

<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:** [January 25, 2024, 2:27pm UTC](https://forums.sketchup.com/t/i-m-having-trouble-redrawing-the-dynamic-component/256910/3 "2024-01-25T14:27:47Z")

</div>

This is just a guess…perhaps something has to occur to cause the dynamic component to notice the change? There is no published API for DCs. Everything known about coding to them was puzzled out experimentally.

---

<div class="post-metadata">

**Author:** ![AdrianoR](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@AdrianoR](https://forums.sketchup.com/u/AdrianoR)\
**Post date:** [January 25, 2024, 3:23pm UTC](https://forums.sketchup.com/t/i-m-having-trouble-redrawing-the-dynamic-component/256910/4 "2024-01-25T15:23:13Z")

</div>

just addying some informations:  
im changing an attribute i created, acab\_pta, it controls some features in my component.  
i was thinking it may be related to the fact it´s created by me? perhaps if i was changing other attribute it could work?

---

<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:** [January 25, 2024, 8:08pm UTC](https://forums.sketchup.com/t/i-m-having-trouble-redrawing-the-dynamic-component/256910/5 "2024-01-25T20:08:06Z")

</div>

See answers in the Dynamic Components category …

- [DC set\_attribute doesn’t save with the component - SketchUp / Dynamic Components - SketchUp Community](https://forums.sketchup.com/t/dc-set-attribute-doesnt-save-with-the-component/161197)

- [Redraw all dynamic components? - SketchUp / Dynamic Components - SketchUp Community](https://forums.sketchup.com/t/redraw-all-dynamic-components/77512)

* * *

Also:

[[How to] Post correctly formatted and colorized code on the forum?](https://forums.sketchup.com/t/how-to-post-correctly-formatted-and-colorized-code-on-the-forum/46189/)

@sWilliams Your post has smartquotes in the code.
