Change Dynamic Attribute value for selected model using Ruby


#1

Hi All,

I am facing a small problem with SketchUp.
if I have multiple models in a file. I select any one model and click on a toolbar icon, open a web dialog box and get some attribute values from Dynamic Components. When I change and update the values how to update the changes values for a selected model in a SketchUp file.

Here is my codes:

Get value code:

loadval = []
@proname = []
Sketchup.active_model.selection[0].definition.attribute_dictionaries["dynamic_attributes"].map{|dkey,dval|
  if dkey == "lenx"
    inches = dval.to_f
    loadval.push(inches.to_cm)
  elsif dkey == "leny"
    inches = dval.to_f
    loadval.push(inches.to_cm)
  elsif dkey == "lenz"
    inches = dval.to_f
    loadval.push(inches.to_cm)
  elsif dkey == "thickness"
    inches = dval.to_f
    loadval.push(inches.to_cm)
  elsif dkey == "_name"
    loadval.push(dval.to_s)
    elsif dkey == "_product_name_options"
    loadval.push(dval.to_s)
  elsif dkey == "product_name"
    @proname.push(dval.to_s)
  end
}
js_command = "showDynamicValue("+loadval.to_s+','+@proname.to_s+")"
key.execute_script(js_command)

Update the values:

dialog.add_action_callback("changeapply"){|key, val|
  puts val
  val= val.split(",")
  hashvalue = {}

  sang = Sketchup.active_model.entities[0]
  sang_def = sang.definition

  puts sang_def.name

  sang_def.set_attribute 'dynamic_attributes', '_lenx_formula', val[0]
  sang_def.set_attribute 'dynamic_attributes', '_leny_formula', val[1]
  sang_def.set_attribute 'dynamic_attributes', '_lenz_formula', val[2]
  sang_def.set_attribute 'dynamic_attributes', 'thickness', val[3]
  sang_def.set_attribute 'dynamic_attributes', 'product_name', val[4]

  dcs = $dc_observers.get_latest_class
  dcs.redraw_with_undo(sang)
}

Please help me to update values for the selected model.

Thanks,
Siva S


#2

Wrong category. This belongs in Ruby API. There are already examples.
https://forums.sketchup.com/search?q=dynamic%20category%3A13


#3

Thanks @DanRathbun


#4

Hi @DanRathbun,

I have one more doubts if any possible to disable the “Dynamic Components” menu from when right click on components. Please see the image I have marked the menu.

Please let know if have any solutions.

Thanks,
Siva S


#5

NO. This is native feature and cannot be disabled.


#6


After disabling or enabling you have to restart SU to take effect.
Keep in mind You may get an other strange behavior with the components and other extension in SU, beside the message above…


#7

He said the DC context menu, not the whole DC extension. He needs the DC functionality.


#8

Yes, you may right. It was not entirely clear, but with this method the menu disappears. Isn’t it ? :slight_smile:
But I would definitely not recommend to disable DC !


#9

I am correct. He is using DCs and needs the extension active.

He is attempting to replace the native DC interfaces with his own. This is likely to anger users.