DC Custom Function - Sum of attribute values
-
[highlight=#eeeeee:1estmm5k]Plugin now released on this page:
http://sketchucation.com/forums/viewtopic.php?f=323&t=52969[/highlight:1estmm5k]Hi I am a relative newbie to Ruby, but have plenty of experience with PHP. This is my third attempt at a Ruby plugin - my first two ended in abandonment after tearing of hair .
I am trying to create a function that adds up the numeric values of a specific attribute of all nested groups/components within the selected dynamic component. So the custom function
=attributesum("price")
would look through all the nested groups and components, find the "price" attribute (if any) and add them all up to return the result. The idea is that the user will be able to access the totals of any attribute (area, price, weight, etc.) from within the model without having to generate a report.
It works thus:
I first followed TIG's post about creating custom functions for Dynamic Components Attributes here: http://sketchucation.com/forums/viewtopic.php?f=180&t=37083
I thought I had got quite far, and now I am utterly confused.
The main part of the script is here:
class DCFunctionsV1 protected # return attributesum # Usage; =attributesum("attribute_name", "dict_name"[optional]) def attributesum(a) puts 'called once' # Check attribute_name has been passed to the function if a[0].nil? || a[0] == 0 UI.messagebox('You need to specify an attribute name e.g. attributesum("weight")') return false end #if attribute_name = a[0] if defined? a[1] && (a[1].nil? || a[1] == 0) dict_name = 'dynamic_attributes' else dict_name = a[1] end #if #load the helper class helper = AttributesFormulaHelper.new # Get all child entities which are groups or components (including this entity) all_entities = helper.get_child_entities(@source_entity) puts all_entities sum = 0 # Loop through all entities and extract the attribute value all_entities.each do |ent| value = ent.get_attribute(dict_name, attribute_name, false) if (value != false && value.numeric?) flut = Float(value) puts 'The value of ' + ent.typename + '\'s ' + attribute_name + ' attribute is ' + value sum = sum + flut puts 'current sum is ' + sum.to_s end #if end #do puts 'last' return sum end #attributesum protected;attributesum end#class
The above script kind of works, but where nesting levels are deeper than 2, the script seems to be invoked twice but on different sets of groups/components (please see attached screenshot )?! Have I got some obvious syntax error? Or am I doing something else obviously wrong? I am of the opinion that there is something in the Ruby language that I have overlooked.
Any help would be much appreciated.
-
Hi! Looks like an interesting Project.
DC programing is voodo stuff. Can't help you much more than provide with some reading.
In case you have missed:
http://wiki.cfcl.com/bin/view/SketchUp/Cookbook/DAsome good stuff in there that might help.
-
@jolran said:
Can't help you much more than provide with some reading.
In case you have missed:
http://wiki.cfcl.com/bin/view/SketchUp/Cookbook/DAsome good stuff in there that might help.
Indeed there is good stuff in there! I'll delve a little deeper to see if it helps my case, but it makes me feel less like I am flying blind.
Thanks again.
-
I have rewritten the plugin to add up the attribute values as it recursively looks through the components and groups. This works without a problem.
For future reference, all those people out there dealing with dynamic attributes should probably know that something weird happens when you create an array of nested groups/components and start working on them within the DCFunctionsV1 class. That may be an incorrect diagnosis, but it explains enough for my situation.
For those interested, see the plugin attached that works. At the moment, when you change the value of an attribute of a nested group/component that is summed, it doesn't automatically update the formula value. Fixing this is my next challenge. Oh what fun I will have with observer classes (of which I know very little about)!
Advertisement