I have a question about programmatically adding built-in Component Attributes (like ‘Position: X’, or ‘Size: LenX’, or ‘Behaviours: Hidden’ via a Ruby script.
I am able to programmatically create a dynamic component and to programmatically create Custom Attributes within that component but I’ve been unable to find a way to programmatically add any of the built-in Component Attributes to the component.
As Dan once told me, there is a lot more happening in a DC than appears in the dialog. You can see the hidden attributes preceded by an _underscore with the Attribute Helper extension (by SketchUp) or the Attribute Inspector extension (by Aerilius). All of these hidden attributes need to be in place for an attribute to work properly.
Here is a working snippet for adding LenX:
EDIT: Removed an un necessary line setting LenX, setting the _lenx_formula also sets LenX.
# works on bulk selections.
Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each do |dc|
dc.definition.set_attribute( 'dynamic_attributes','_lenx_label','LenX')
dc.definition.set_attribute( 'dynamic_attributes','_lenx_formula', '24')
$dc_observers.get_latest_class.redraw_with_undo(dc)
end
Much obliged Dan. I’ll absolutely review your posts.
In the meantime, I came across this post and got myself started, using one of the snippets in the post in adding what I call a ‘built-in’ attribute (as opposed to Custom) ‘Size: LenX’
Your snippet works perfectly and is essentially a tidier version of the one I stumbled on (referenced in my reply to Dan above) after posting my question.
By the way, I checked out what I assume is your business site:
It can be tidier as there are defaults. For example if you do not explicitly set an individual attribute units (as in the non-tidier example you linked) then the attribute will default to using the component’s global units which are set in _lengthunits (which can be "INCHES" or "CENTIMETERS".)
I’m planning to build a suite of modular scripts to automate the modelling of more complex assemblies of dynamic components.
I’ve built those types of nested DCs manually over the last few years, importing models into models, and tweaking/upgrading formula references, etc., expecting at some point to dive into ruby to automate what was obviously a lot of repetitious work building components with similar attributes.