Assigning DC attributes to nested components & groups
-
Hi, I am a beginner with Ruby, and have been studying this forum for help (you might recognise code I have grabbed) but can't figure out a solution, so please go easy... I am trying to assign material volumes (mm3), weights and weight in water to some steel components so I can export to a report, but cannot figure out how to loop through everything including deeply nested groups and components.
The code below only works on a group of groups/components, but not individual groups/components, or groups of groups of groups etc. I am running it directly in Ruby Code Editor for now.
Thanks in advance for any help.
# Adds metric volume and weight information for underwater steel components def dyn_atts(e) #added error handler to avoid picking up dimensions, construction points and guides if e.is_a?(Sketchup;;Group) || e.is_a?(Sketchup;;ComponentInstance) #volume in Sketchup native inches e.set_attribute "dynamic_attributes","vol_inch", e.volume #volume in mm3 e.set_attribute "dynamic_attributes","volume", "0" e.set_attribute "dynamic_attributes", "_volume_access","VIEW" e.set_attribute "dynamic_attributes","_volume_formlabel","Volume (mm3) " e.set_attribute "dynamic_attributes","_volume_formula", "ROUND(vol_inch*25.4*25.4*25.4,0)" #mass in steel e.set_attribute "dynamic_attributes","weight", "0" e.set_attribute "dynamic_attributes", "_weight_access","VIEW" e.set_attribute "dynamic_attributes","_weight_formlabel","Weight (kg) " e.set_attribute "dynamic_attributes","_weight_formula", "ROUND(volume*7850/1000000000,0)" #weight in water e.set_attribute "dynamic_attributes","wtsubc", "0" e.set_attribute "dynamic_attributes", "_wtsubc_access","VIEW" e.set_attribute "dynamic_attributes","_wtsubc_formlabel","Weight underwater (kg) " e.set_attribute "dynamic_attributes","_wtsubc_formula", "ROUND(volume*(7850-1000)/1000000000,0)" end end Sketchup.active_model.selection.each{|e| if e.is_a?(Sketchup;;Group) e.entities.parent.entities.each{|e| dyn_atts(e) } elsif e.is_a?(Sketchup;;ComponentInstance) e.definition.entities.each{|e| dyn_atts(e) } end $dc_observers.get_latest_class.redraw_with_undo(e) }
-
DC attributes are set directly upon nested DC group instances.
BUT, ... for component instances they are defined upon the definition along with a default value. If there are to be more than 1 instance of the DC and they differ, then the differing values are then set on the instance. (However the "smarts" of each DC attribute, ie, access, formula, label are only in the definition's DC dictionary.)
If changing an instance's attribute(s) causes the definition's entities collection to change, then the DC engine will unique-ify the DC, by cloning the definition and making the instance the first instance of this new DC definition. (An example is when changing the DC so that more or less copies of a sub-component, like a stile are generated. This changes the DC definition's entities collection, so if there are more than 1 instance, the one needing a different number of stiles would need a new definition.)
Advertisement