Applying Ruby Script to Dynamic Components

I’ve written a ruby script for my dynamic component and I don’t know how to get it to apply changes to the model. Can anyone explain what I need to do to in order for the script to take effect

First of all … the DC dictionaries are named "dynamic_attributes" not "D_A".

Secondly, … changing the "lenx" attribute in the definition will change all of it’s instances. If you need to only change a single instance, (and the definition has more than 1 instance,) then you would set the attribute in the instance’s dynamic dictionary. But there are likely also other attributes that may need to exist or be changed. (There is more discussion in the DC category on this.)

See this topic …

1 Like

Thank you so much for your response.
And please forgive my ignorance on this matter, I tried to implement what I found in that forum.
If you can, please continue to point me in the right direction, as I am trying to make the right corrections but I’m unsure if I’m getting any closer to getting this working.

this is what I currently have

entities = Sketchup.active_model.entities
definition = Sketchup.active_model.definitions[0]
transformation = Geom::Transformation.new([0,0,0])
componentinstance = entities.add_instance(definition, transformation)
definition = componentinstance.definition
status = definition.set_attribute "dynamic_attributes", "_lenx_nominal", "125"

def recalc_dc(inst)
  return nil unless defined?($dc_observers)
  if inst.is_a?(Sketchup::ComponentInstance) &&
  inst.attribute_dictionary('dynamic_attributes')
    $dc_observers.get_latest_class.redraw_with_undo(inst)
  end
  nil # (there is no particular return value)
end

In order for the recalc_dc method to run, your code needs to call it.
The preamble code should also be within a method.
And both methods should be in a submodule of your unique namespace module.

Time to learn basic Ruby programming. See …