"How to use Ruby code to set the default unit of custom attributes in a dynamic component to centimeters?"

“How to use Ruby code to set the default unit of custom attributes in a dynamic component to centimeters?”

# Where inst is the reference to a dynamic component instance object:
dyna_cdef = inst.definition
dyna_cdef.set_attribute("dynamic_attributes", "_lengthunits", "CENTIMETERS")
dyna_cdef.instances.each { |i|
  $dc_observers.get_latest_class.redraw_with_undo(i)
}

This causes all numeric length attributes at that component level to use centimeters.

It also applies to all instances of that dynamic component.


P.S. : There is no “Free Plan” for Desktop SketchUp after version 2017. Please correct your profile.

To separately apply specific units to individual attributes, you prefix the attribute name with an underscore, then suffix with "_units" and "_formulaunits", using one of the values: "DEFAULT", "INCHES" or "CENTIMETERS".

ie …

# Where the custom attribute name is "meaculpa":
dyna_cdef.set_attribute("dynamic_attributes", "_meaculpa_units", "CENTIMETERS")
dyna_cdef.set_attribute("dynamic_attributes", "_meaculpa_formulaunits", "CENTIMETERS")

If you make this change after the fact you may need to redraw the definition’s instance as shown above.