Assigning DC attributes to nested components & groups

Assigning DC attributes to nested components & groups

Postby Sugareth » Mon Oct 14, 2019 3:07 am

SketchUp_6tw8QMoCZX.png
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.

Code: Select all
# 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)
}
0

Sugareth 
 

Re: Assigning DC attributes to nested components & groups

Postby Dan Rathbun » Mon Nov 04, 2019 9:51 pm

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.)
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    Who is online

    Users browsing this forum: No registered users and 4 guests

    Visit our sponsors: