Manage different access to dynamic attributes in ruby

Hello everyone! :sun_with_face:

I want to manage access to dynamic attributes by users.

“Users can edit as a textbox” => Is called with the method “TEXTBOX”.

s.set_attribute 'dynamic_attributes', '_attribut_access', 'TEXTBOX'

There are 2 other options, which I wish I could adjust in ruby:

“Users can see this attribute” =>?
“Users can select from a list” =>?

Do you know how to do it ?

thank you in advance

David

s.set_attribute ‘dynamic_attributes’, ‘_attribut_access’, ‘NONE’
s.set_attribute ‘dynamic_attributes’, ‘_attribut_access’ ,‘VIEW’
s.set_attribute ‘dynamic_attributes’, ‘_attribut_access’, ‘LIST’

if you use an attribute inspector like one by Aerilius, you can observe any value if you create the object

2 Likes

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. :wink:

David

5 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.