I’m having trouble redrawing the dynamic component

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

Just making the code readable

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

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.

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?

See answers in the Dynamic Components category …


Also:

[How to] Post correctly formatted and colorized code on the forum?

@sWilliams Your post has smartquotes in the code.

2 Likes