Hello pcmoor,
I’m sorry I did not answer earlier but I waited to be a little more free to provide an answer worthy of your generous help.
The code below, applies all of these options to the component in a selection:
### Create a non-visible attribute that makes a simple calculation.
mod = Sketchup.active_model
sel = mod.selection
sel.grep(Sketchup::ComponentInstance).each do |s|
s.set_attribute "dynamic_attributes","calculated", "0"
s.set_attribute "dynamic_attributes", "_calculated_access","NONE"
s.set_attribute "dynamic_attributes","_calculated_formula", "100/2"
$dc_observers.get_latest_class.redraw_with_undo(s)
end
### Create a visible attribute that can not be modified by the user.
mod = Sketchup.active_model
sel = mod.selection
sel.grep(Sketchup::ComponentInstance).each do |s|
s.set_attribute "dynamic_attributes","a00title", "COMPONENT OPTIONS"
s.set_attribute "dynamic_attributes", "_a00title_access","VIEW"
s.set_attribute "dynamic_attributes","_a00title_formlabel","-"
$dc_observers.get_latest_class.redraw_with_undo(s)
end
### Create a dynamic attribute "LenX" modifiable by the user.
mod = Sketchup.active_model
sel = mod.selection
sel.grep(Sketchup::ComponentInstance).each do |s|
current_width = s.get_attribute "dynamic_attributes","lenx"
s.set_attribute "dynamic_attributes","lenx", current_width
s.set_attribute "dynamic_attributes","_lenx_formula", current_width
s.set_attribute "dynamic_attributes","_lenx_units", "CENTIMETERS"
s.set_attribute "dynamic_attributes", "_lenx_access","TEXTBOX"
s.set_attribute "dynamic_attributes","_lenx_formlabel","Length"
$dc_observers.get_latest_class.redraw_with_undo(s)
end
### Create an attribute as a list and apply the color chosen by the user to the material attribute.
mod = Sketchup.active_model
sel = mod.selection
sel.grep(Sketchup::ComponentInstance).each do |s|
s.set_attribute "dynamic_attributes","choice_colors","RED"
s.set_attribute "dynamic_attributes","_choice_colors_units", "STRING"
s.set_attribute "dynamic_attributes", "_choice_colors_access","LIST"
s.set_attribute "dynamic_attributes","_choice_colors_formlabel","Test 4 Liste"
s.set_attribute "dynamic_attributes","_choice_colors_options", "Option 1 - RED=red&Option 2 - BLUE=blue"
s.set_attribute "dynamic_attributes","material","RED"
s.set_attribute "dynamic_attributes","_material_formula", "choice_colors"
$dc_observers.get_latest_class.redraw_with_undo(s)
end
For beginners in ruby wishing to test the code, just select a component and copy paste code in the console SketchUp ruby.
Thanks for your help pcmoor.
David