Overwriting the value of an attribute containing a formula

Hello, everyone.
I’ve encountered an issue with setting an attribute value.
Originally, the attribute value is set as a formula. I’m trying to replace the formula with a static value.

You can see the result in the video at the following link.

Here is my code.

def name_change(guid, name)
  model = Sketchup.active_model
  view = model.active_view
  component_definition = model.definitions.find { |e|  e.guid == guid }
  
  if component_definition
    if component_definition.get_attribute("dynamic_attributes", "cut")
      parent_component = component_definition.parent
      puts "tmp 1 = #{component_definition.class}"
	  component_definition.delete_attribute("dynamic_attributes", "l1_component_102_article")
      $dc_observers.get_latest_class.redraw_with_undo(component_definition)
    else
      component_definition.entities.each do |entity|
       if entity.is_a?(Sketchup::ComponentInstance)
         if entity.get_attribute("dynamic_attributes", "cut")
           parent_component1 = entity.parent
           SKETCHUP_CONSOLE.clear
           puts "tmp 2 = #{entity.class}"
           entity.set_attribute("dynamic_attributes", "l1_component_102_article", name)
           entity.definition.invalidate_bounds
           puts "attribute before = #{entity.get_attribute("dynamic_attributes", "l1_component_102_article", '')}"
           model.commit_operation
           puts "attribute after = #{entity.get_attribute("dynamic_attributes", "l1_component_102_article", '')}"
           puts "oki "
          end
        end
      end
    end
  end
end

Notice how we must do excessive horizontal scrolling to view your code?
The indentation is not correct. Ruby uses 2 space indents.


If you had a formula for dynamic attribute "l1_component_102_article", then there will be a "_l1_component_102_article_formula" attribute in the definition’s DC dictionary that contains the formula or a static value. This would need to be set, cleared or deleted.

There also may be a "_l1_component_102_article_nominal" in the definition’s DC dictionary which may need to be set, cleared or deleted. (Likely just contains the static value.)

The value of the "l1_component_102_article" dynamic attribute will be taken from these above or if they are not present (or deleted), then you can set this attribute directly.


If the value of the "l1_component_102_article" dynamic attribute for the instance differs from the default value of the definition, then the instance will have a dynamic dictionary with this override value. (This means you ALWAYS check the instance FIRST for a dynamic dictionary and value, and if it lacks, THEN check the definition’s dynamic dictionary. This does not apply to nested dynamic groups whose dynamic dictionary is only upon the instance.)

Whenever an instance’s dynamic dictionary gets an override value, this last used value is saved in the definition’s dynamic dictionary with a "_inst_" prefix. Ie … "_inst_l1_component_102_article".

1 Like