Attributes on component

Hello every one :slight_smile:

I work on project which my component have to contains attributes.
Actually i insert them with
ruby # new_instance = entities.add_instance component.componentDefinition , transf

I have just one entity on my entities and i cant create an entity to use Sketchup::AttributeDictionay
I have few type of component and i stock them in hash to load them just one time.
At the end I want to catch ever pounds of every component and return the sum of them.
I cant do that on the same script cause I want to catch this sum after adding ohter component in Sketchup with graphic interface + component of my script.

Maybe there existe a simply way to do this ?

Thanks for help and sorry for my bad english :3

If I understand right, you programmatically insert components and you want to programmatically sum an attribute value of each instance, but at a different point in time.

You basically want to do two different procedures. Then split it into two functions (called “methods” in Ruby). Of course you can do that in the same script!

A script is a file of Ruby code. If the code consists just of a sequence of imperative lines of statements, they will be executed immediately when the script is being loaded.

If you wrap statements into methods (procedural programming), they will all be loaded when the file is being loaded, but they execute only as soon as you call the method.

def load_and_insert_components
  # …
  new_instance = entities.add_instance(component.componentDefinition , transf)
  # …
end

def calculate_sum_of_pounds(instances)
  # …
end

And your graphical interface has a button to call load_and_insert_components() and another button to later call calculate_sum_of_pounds(). You should pass relevant parameters (references to component instances) to the methods so they know on which data they should work.

new_instance.set_attribute('Inst_Cost', 4.87)

# latter
total = []
instances.each{|i|  total << i.get_attribute('Inst_Cost')

total.inject(0, :+)

# an example
total = [4.6,4.8,8]
total.inject(0, :+)
# returns 17.4

john

Thanks all that works now

I use two function as you say and in second function I use this to take my attribute


total = 0
entities.each {|entity|

   dict = entity.attribute_dictionary 'test'
   if (dict != nil)
        puts dict['Inst_Poids'].inspect	
        total = total + dict['Inst_Poids']
   end
   #instances.each{|i|  total << i.get_attribute('Inst_Poids') }
}

Now I try to introduce my attribute on the component Sketchup to drag en drop the component and count it on the final calcul. I try to add the list of component of my script and the component I created on Sketchup and drago and drop on my drawing

Thank all that really nice and really good advice. I started Ruby and Sketchup two week ago but the community is really good :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.