'Dynamic' array
-
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