Group objects by contour plane in python

Hi all,
is there a way to group (get a nested list) contour curves with the same z value.
Rhino.Geometry.Brep.CreateContourCurves have no option to group the result it spits a flat list out.

thanks

I guess you are making contours along Z axis? How are you calling .CreateContourCurves? It looks like .CreateContourCurves returns a curve array per plane or line it is passed, so you can make a list of arrays, which behaves pretty much like a list of lists:

import Rhino as R
import rhinoscriptsyntax as rs
import scriptcontext as sc

# get brep/surface
guid = rs.GetObject('select object for contours')

# to make the intersecting planes use the bounding box Z dimension
geo = rs.coercegeometry(guid)
bbox = geo.GetBoundingBox(R.Geometry.Plane.WorldXY)

# make a LineCurve to divide
z_start = bbox.PointAt(0, 0, 0)
z_end = bbox.PointAt(0, 0, 1)
z_line = R.Geometry.LineCurve(z_start, z_end)

# divide by 10 to make 10 contours
divide_t_points = z_line.DivideByCount(10, True)
divide_points = []
for t in divide_t_points:
    divide_points.append(z_line.PointAt(t))

# make planes at the divide points
planes = []
for p in divide_points:
    planes.append(R.Geometry.Plane(p, R.Geometry.Vector3d.ZAxis))

# now can get the contours
contours = []
for plane in planes:
    curve_array = R.Geometry.Brep.CreateContourCurves(geo, plane)
    contours.append(curve_array)
    
# check
print(contours)
    
# do something with them: adding to doc grouped
for i, c_array in enumerate(contours):
    group_index = sc.doc.Groups.Add('contour_group_{}'.format(i))
    for c in c_array:
        c_guid = sc.doc.Objects.AddCurve(c)
        sc.doc.Groups.AddToGroup(group_index, c_guid)

Result:

1 Like

Big thanks for your help.

I called it like this.

Rhino.Geometry.Brep.CreateContourCurves(object,ptZmax,ptZmin,step)
which give a flat contour list as a result.
I recognized in the help menue that calling it with a plane is also possible which give a nested list result for all contours from the planes.

I guess you solved my problem.

Tried it and work as expected.

import rhinoscriptsyntax as rs
import Rhino as rh
import ghpythonlib.treehelpers as th

PlaneXY = rh.Geometry.Plane.WorldXY
BBox = rh.Geometry.Brep.GetBoundingBox(object,PlaneXY)
ptZ = BBox.Max.Z
ptmin = rh.Geometry.Point3d(0.0,0.0,BBox.Min.Z)

plpt = []
def sume(ptZ):
    if len(plpt) == step:
        return False
    else:
        plpt.append(rh.Geometry.Point3d(0.0,0.0,ptZ))
        return sume(ptZ-depth)
sume(ptZ)

vec = rh.Geometry.Vector3d(0.0,0.0,1)
Plane = [rh.Geometry.Plane(i,vec) for i in plpt]
Cont = [rh.Geometry.Brep.CreateContourCurves(object,j) for j in Plane]
con = th.list_to_tree(Cont)

Thanks again

1 Like

Glad it helped.