'Dynamic' array
-
Hi
I'm not sure if this is the right forum for asking ruby questions, so please feel free to move my topic.
I'm trying to create a simple function that collects the areas of the face inside groups and puts a text note that shows the totals.
But I'm having some problems figure out how to store the areas.
I have some groups. Every group consist of one face. The face has a name eg. Office home etc.
I then would like to collect the areas as:
Office 50.000 m2
Home 24.000 m2
Disp. 3.000 m2
Total 77.000 m2I thought I could use something like AreaTotal[name] = AreaTotal[name] + object.area, and then run through the array and list the name of the key and value, but it seems arrays don't accept variables as keys.
Can anyone please help me to find a solution?
Ps. I know there might be a plugin that does the job, but I find it somehow exciting to create it myself.
Thanks
Jorgensen
Denmark -
You need a hash {} to store the space as the key and the value.
You can then get an array [] of the key-names, sort that in order and then get the hash key value that matches each sorted array element.
The names need to be unrepeated.
To get the total at the end simply add up the values and assign it to a hash key 'Total'...
areas={} groups.each{|group| faces=group.entities.to_a.find_all{|e|e.class==Sketchup::Face} areas[group.name]=faces[0].area }### assuming only ever one face and its area is in sqm already names=[] areas.each_key{|k|names << k} names.sort! tot=0 names.each{|name| tot+=areas[name] puts name+" "+areas[name].to_s } puts puts "Total "+tot.to_s
You can sort hashes etc but I felt you might be happier just remembering everything in a hash and using an array to process it -
The API says that face.area returns the area in current units but my experience is that it always returns square inches. So area would need to be divided by inches/meter squared.
-
You are right about
face.area
arriving in sq".
But it's easy enough to convert to sqm and then to fix it at say 3dp usingarea_text = sprintf("%.3f", face.area*0.00064516)
I was concentrating on the array[] versus hash{} issue...
-
Not to step on TIG's toes but to find the groups
groups=Sketchup.active_model.active_entities.find_all{|e| e.class==Sketchup;;Group}
and to total area of multiple faces
areas[group.name]=0 if !areas[group.name] areas[group.name] += faces[0].area
-
And this:
names=[] areas.each_key{|k|names << k} names.sort!
can be replaced with:
names = areas.keys.sort!
-
BTW.. the following thread has a code example for getting the volumes of multiple groups.
You can copy it rename the outer and inner modules, to something like:Jorgensen::MultiArea
and modify it to return areas instead of volumes.
Volume of Multiple Groups -
If you look back at my previous post, the second code snippit has the answer to your problem.
-
Hi all
I think I'm a bit lost here.
Could this part somehow be split up? I think that could help me to understand whats going on
groups.each{|group|faces=group.entities.to_a.find_all{|e|e.class==Sketchup::Face} areas[group.name]=faces[0].area
Because I can't figure out where to place this part
areas[group.name]=0 if !areas[group.name]
, and when I add the += I get an error sayingError: #<NoMethodError: undefined method
+' for nil:NilClass>`.Right now my (your) code looks like this:
<span class="syntaxdefault">defĀ tmArea<br /><br />Ā Ā Ā Ā groups</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find_all</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">Ā e</span><span class="syntaxkeyword">.class==</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">Ā Ā Ā Ā areasĀ </span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Ā </span><span class="syntaxkeyword">{}<br /></span><span class="syntaxdefault">Ā Ā Ā Ā groups</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">faces</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">find_all</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">.class==</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">Ā Ā Ā Ā areas</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">]</span><span class="syntaxdefault">Ā </span><span class="syntaxkeyword">+=</span><span class="syntaxdefault">Ā faces</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">areaĀ Ā Ā Ā </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">namesĀ </span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Ā areas</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">keys</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">sort</span><span class="syntaxkeyword">!<br /><br /></span><span class="syntaxdefault">tot</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">0<br />names</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">tot</span><span class="syntaxkeyword">+=</span><span class="syntaxdefault">areas</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">putsĀ name</span><span class="syntaxkeyword">+</span><span class="syntaxstring">"Ā "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">areas</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">name</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_s<br /></span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">puts<br />putsĀ </span><span class="syntaxstring">"TotalĀ "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">tot</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /><br />end<br /><br /></span><span class="syntaxkeyword">if(</span><span class="syntaxdefault">notĀ file_loaded</span><span class="syntaxkeyword">?(</span><span class="syntaxstring">"TM_Area.rb"</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault">Ā Ā Mejeriet_menuĀ </span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Ā UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Plugins"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"TM"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Arealer"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">Ā Ā Mejeriet_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"On"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault">Ā </span><span class="syntaxkeyword">{</span><span class="syntaxdefault">Ā Ā tmAreaĀ </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">end<br />file_loaded</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"TM_Area.rb"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault">Ā </span>
Sorry for my noob questions.
-
First thanks to your both for the fast reply.
@TIG
the code looks far more professional than my code would look like, I'm not sure if coding in Ruby/SU has change a lot since my last attemt three years ago.As I understand your code, if two groups has the same name e.g. Office - it won't be saved as a total, but only the area of the last object will be saved.
Do I need something like
areas[group.name] += areas[group.name] + faces[0].area
to sum the areas?I have added
model = Sketchup.active_model groups = model.active_entities
Is this still the right way of getting the groups?
Thanks
-
Hi sdmitch
How do you fit in 'areas[group.name]=0 if !areas[group.name]
areas[group.name] += faces[0].area' intogroups.each{|group|faces=group.entities.to_a.find_all{|e|e.class==Sketchup::Face}
areas[group.name]=faces[0].area?
Sorry I'm a noob but I'll try to learn
-
by simply adding the line above the areas[group.name] += faces[0].area line like this
groups.each{|group| faces=group.entities.to_a.find_all{|e|e.class==Sketchup;;Face} areas[group.name]=0 if !areas[group.name] areas[group.name]+=faces[0].area }### assuming only one face in group
-
Works like a charm now
Thanks !
Advertisement