views:

161

answers:

2
require 'sketchup'

     entities = Sketchup.active_model.entities
     summa = Hash.new

     for face in entities
        next unless face.kind_of? Sketchup::Face
        if (face.material)
              summa[face.material.display_name] += face.area


    end
   end

Im trying to get the structure in the array as such:

summa { "Bricks" => 500, "Planks" => 4000 }

Making a ruby script for Google Sketchup btw

But if I run this code i only get

Error: #+' for nil:NilClass> C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:17 C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:14:ineach' C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:14 C:\Program Files (x86)\Google\Google SketchUp 7\Plugins\test.rb:8:in `call'

As im used to using PHP and just doing $array['myownassoc'] += bignumber; But i guess this isnt the right approach when using Ruby?

So any help in how i need to go would be nice.

+2  A: 

summa[face.material.display_name] returns nil by default when face.material.display_name isn't an existing key. When creating a hash, you can specify a different default value to return. Something like:

summa = Hash.new(0)
Firas Assaad
Thx a lot dude, that really helped a lot of work for me ;)
Arto Uusikangas
+4  A: 
Jörg W Mittag
Omg, that was totally worth reading, and thx for all the hints and corrections to my code. If it didn't seep through i'm a beginner to Ruby and also to programming Plugins for Sketchup.
Arto Uusikangas
Excellent! +1 isn't enough! :P I wish I had the patience to do what you just did.
Peter Lindqvist
If I were to change the code to count the material on both sides, how is the best approach to do that? back_material and material that is.
Arto Uusikangas