I often have to calculate the total area of surface, polysurface, extrusion of each layer. Can anyone help me create a python script that calculates the total area of the faces in the layer and writes it out as text. thank
sure, post here what you created so far
Here’s a simple grasshopper option. You can take it a lot further with additional plugins (auto reference all layers, export to excel, whatever)
You can could add a print text without a plugin and run via Grasshopper Player as well.
re_area-script.gh (10.9 KB)
re_area script.3dm (150.1 KB)
run GrasshopperPlayer in the Command line and select this script. The idea being that you create a button with a reference to the script.
re_area-script-Gh Player.gh (14.2 KB)
I’m a beginner in python scripting. this is the script i’m trying and don’t know where the error is coming from
import rhinoscriptsyntax as rs
# -*- coding: utf-8 -*
import scriptcontext as sc
import Rhino
def SelectObjectTypes(objs):
for obj in objs:
otype=rs.ObjectType(obj)
if otype==8 or otype==16:
rs.SelectObject(obj)
def Layer_area():
pt = rs.GetPoint("Get point")
rs.EnableRedraw(False)
layers=rs.LayerNames()
text = 0
for layer in layers:
if rs.IsLayerSelectable(layer):
rs.UnselectAllObjects()
objs=rs.ObjectsByLayer(layer, False)
ID = SelectObjectTypes(objs)
if rs.SelectedObjects():
brep=sc.doc.Objects.Find(ID).Geometry
S = 0
for i in range(brep.Faces.Count):
area=Rhino.Geometry.AreaMassProperties.Compute(brep.Faces[i]).Area
S +=area
layer_name=layer.replace("::","_")
e_file_name = '{}{}{\n}'.format(layer_name,S)
text += e_file_name
text_id = rs.AddText(text,pt,100,font="Arial",justification=131072+2)
rs.EnableRedraw(True)
Layer_area()
1 great solution. but I don’t use fixed layers often so this idea doesn’t seem to work very well for me. thanks for your support
Here
def SelectObjectTypes(objs):
for obj in objs:
otype=rs.ObjectType(obj)
if otype==8 or otype==16:
rs.SelectObject(obj)
you don’t return any value from that called function, the function selects an object only, so nothing gets assigned to ID, you probably need to:
def SelectObjectTypes(objs):
for obj in objs:
otype=rs.ObjectType(obj)
if otype==8 or otype==16:
return obj
and then after that check if an ID has been returned
I have edited a bit in the calculation of the area and the place you said. but the script is still not correct somewhere
import rhinoscriptsyntax as rs
# -*- coding: utf-8 -*
import scriptcontext as sc
import Rhino
def SelectObjectTypes(objs):
for obj in objs:
otype=rs.ObjectType(obj)
if otype==8 or otype==16:
return obj
def objects_area(IDS):
S_all=0
for ID in IDS:
brep=sc.doc.Objects.Find(ID).Geometry
S = 0
for i in range(brep.Faces.Count):
area=Rhino.Geometry.AreaMassProperties.Compute(brep.Faces[i]).Area
S +=area
S_all += S
return S_all
def Layer_area():
pt = rs.GetPoint("Get point")
rs.EnableRedraw(False)
layers=rs.LayerNames()
text = 0
for layer in layers:
if rs.IsLayerSelectable(layer):
rs.UnselectAllObjects()
objs=rs.ObjectsByLayer(layer, False)
IDS = SelectObjectTypes(objs)
if rs.SelectedObjects():
total_area = objects_area(IDS)
layer_name=layer.replace("::","_")
e_file_name = '{}{}{\n}'.format(layer_name,total_area)
text += e_file_name
text_id = rs.AddText(text,pt,100,font="Arial",justification=131072+2)
rs.EnableRedraw(True)
Layer_area()
def SelectObjectTypes(objs):
ids = []
for obj in objs:
otype=rs.ObjectType(obj)
if otype==8 or otype==16:
ids.append(obj)
return ids
I think that this is what you want then…

