Hi
Sorry didn't see reply until today
To clarify a few points, a component seen on the model interface is a component instance whereas the definition is retained in data even after all the instances are deleted unless the file is purged. Deleting a component definition in the component dialog will delete all instances.
Component instances have their own naming system that matches groups, for dynamic components (DCs) provided you don’t use the “name” attribute, changes in instance name are reflected in the title of the attribute and options dialog of the DC. This may be enough for your workflow in showing some uniformity in outliner, labels, and the DCs options.
If this is too limited, then ruby extensions are required, DCs seem to be very low on the sketchup developers priority list, I naively assume that they would get attention on each new release, been 5 years now with little or any visible improvement, so you really need to use ruby in conjunction with your DC program
It would be best if you could give an outline of your intentions with regards DCs, for example I use them to create structural drawings and material lists, so easy excess to my DC folders and a placement, scale and orientation tool is my first ruby, then another to simplify the DC to an inert form…and so on. I now use the “description” attribute as main reference to create reports on materials and the instance name to locate container objects with instance name “WF1” or “WF2”… or “RT2” rather than the DC definition name wallframe# whatever number or rooftruss#103 (no longer important or included in my report)
With regards the ruby API, this is specific to Sketchup and not a ruby learning deposit. To understand ruby you would need to check out some youtube videos, on line books...
However there is nothing like taking the plunge...
There a few downloads
Ruby editor:
tool editor:
https://sketchucation.com/pluginstore?pln=ae_ToolbarEditor
- convenience of running snippets
- make buttons toolbar without knowing the ruby required, later you can install your own or a web form for more complex projects
Attribute editor: https://sketchucation.com/pluginstore?pln=ae_AttributeInspector
- to understand the DC behind the scenes attributes (_underscore values)
- to copy lists from one dropdown to another
- to insert a formatted, excel list rather than write a long list and values
With DCs a very important ruby code step is redraw.
There are some situations when a redraw is not triggered, such as entering a DC and changing its raw geometry, consider the facearea() is not updated until a redraw (right click context menu) is made.
The area example, open option dialog, then the DC, move an edge or two, close, you will note the area calculation does not update. Unless you force a redraw via the context menu, DC,redraw or another method to update the option dialog.
area example.skp
Consider the snippets kindly donated by Tig, Dan, Mitch and many others and attach the code to a button create in toolbar editor
https://sketchucation.com/forums/viewtopic.php?f=180&t=68489
or https://forums.sketchup.com/t/redraw-all-dynamic-components/77512
Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each do |s| $dc_observers.get_latest_class.redraw_with_undo(s) end
This would be a good starting point, reading it, dot containers, we are looking at a selection, so a selection must be done for this to work, look up grep on internet, so filtered to specific elements of an array, with each do an action….redraw
Try this first on the example model
Expand this with
Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each do |s| puts s.name puts s.definition.name $dc_observers.get_latest_class.redraw_with_undo(s) end
puts writes the result to the ruby console, so open this and run the script to see both names
using puts to the ruby console is a method of checking the results you are seeking, as Dan wrote, messagebox can silently fail, so use the console to get some feedback as you experiment
Now to this script we can add attributes “name_defn” and “name_inst” or of own choosing. Then assign the results
` Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each do |s|
s.set_attribute 'dynamic_attributes','namedefn', s.definition.name.to_s
s.set_attribute 'dynamic_attributes','nameatt', s.name.to_s
$dc_observers.get_latest_class.redraw_with_undo(s)
end`
Try it on the example
It is best if the attribute in is already defined in the DC before altering its value, as there is a reference issue in the internal DC code if done on the fly.