I created a component with its definition and instance in Ruby. Is it possible to add option lists with Ruby to the component just like creating Dynamic Components to control something like size?
I tried this code:
# Convert the group to a component and set its name and attributes
inst = group.to_component
inst.definition.name = "material"
inst.name = thickness.to_s
# Set the thickness attribute to the component's LenY
inst.set_attribute("dynamic_attributes", "LenY", thickness)
but it gave me only “LenY” that is a Custom Attribute
I know “LenY” here is a string. So is there a way to interact with the native or default attributes in DC or create a custom one connected with the native attributes (This would be better because we can customize the name appearing in the DC options.
Yes but … the DC extension is a closed source extension that has no public documentation. It is also very complex and has interacting "dynamic_attributes" dictionaries on both the definition and instance (for components) and just the instance for nested dynamic groups.
Much of this has already been discussed in the Dynamic Components subcategory.
Hi Dan, thanks for the hint! I have explored a little bit about “dynamic_attributes” dictionaries. I created a new code and there was a progress actually.
# Extrude the face to create a wall with the specified thickness (LenY)
len_y = 100.mm
f.pushpull(len_y)
# Convert the group to a component and set its name and attributes
inst = group.to_component
inst.definition.name = "material"
inst.name = len_y.to_s
# Set the thickness attribute to the component's LenY
inst.set_attribute("dynamic_attributes", "Thickness", len_y)
# Set the display name for the "T" attribute
dict = inst.attribute_dictionary("dynamic_attributes", true)
# Set the thickness attribute formula based on LenY
dict["_thickness_formula"] = 'LenY'
# Set the thickness label and units
dict["_thickness_label"] = 'Thickness'
dict["_thickness_units"] = 'STRING'
# Set the thickness access type
dict["_thickness_access"] = 'TEXTBOX'
In this case the custom attribute “Thickness” is related to the default value “LenY” (left in image) and also shows in DC options dialog although it is empty (right in image) . Is there a way to make default value “LenY” and len_y(100mm) interact? Like when I change the thickness in DC option input box, the LenY (thickness) of the model also changes.
The dynamic component information displayed in Attribute Inspector I am afraid is a copy of the internal values that the dynamic components work with.
If the attributes in the dictionaries are modified, the geometries are not updated.
I think there are many extensions that do that job and that are very developed (e.g.) Profile Builder, (I actually draw practically everything with that extension) so I think it is easier to use that tool than to create it from scratch.
This way works but it convert to string
This way doesn’t work (also if you change to float):
Hi, Dan. Yes I have tried Attribute Inspector and tried to examined one DC I manually created in Sketchup. It looks possible, with Ruby, to access and manipulate DC’s both custom and default attributes that are attributes starting with underscores, for example _leny that represents LenX (maybe a default attribute for size control?) under Size category when setting attributes. But it looks very complex and time-consuming to create a DC with input or options with Ruby.
So my vision is to create a DC with Ruby scripts but I have not tested this method and as you said there might be a risk that If the attributes in the dictionaries are modified, the geometries are not updated.
Except I really suggest (again) that you examine one of the DCs that come with SketchUp.
Change options with the DC interface and watch how the dynamic attributes change in both the instance and definition dictionaries. Make sure you test with both a single dynamic instance and multiple instances in the model. (There is differing behavior when there are multiple instances.)
These are the “meta” attributes that the DC extension uses to display the user interface and control the instances of dynamic components.
It’s been quite awhile since I mucked around with DCs, but I think you may need to go the other 'way around. Ie, set the _leny_formula = to the thickness custom attribute.
Then, don’t forget to do a redraw on the instance. See the last line of code in Scott’s example at the SCF post you linked above.