Separate polysurface

I couldn’t think up a clean solution which doesn’t use C#, Python, or a custom design script definition.

The first on that list isn’t something readily distributed, and the last will usually lead to more questions than answers. So here’s some Python for you (you’ll have to build the means of iterating over a list of PolySurfaces (though you might just convert them to a single PolySurface first):

import sys,clr #import the sys and CLR modules
clr.AddReference('ProtoGeometry') #add the Dynamo geometry library to the CLR
from Autodesk.DesignScript.Geometry import * #import all of the Dynamo geometry library

polySurface = IN[0] #the polysurface to separate

faces = [i for i in polySurface.Faces] #pull the topology of the PolySurface

polysurfaces = [] #the resulting list of PolySurfaces

while faces: #while there are faces in the list
    toGroup = [faces.pop(0)] #pull the first face out and put it in the toGroup list
    grouped = [] #a list to hold the groups of faces
    
    while toGroup: #whiel there are items to group
        face = toGroup.pop(0) #pull the face out of the to group list
        grouped.append(face) #append the face to the grouped list
        edges = face.Edges #get the edges of the face
        adjFaces = [i for edge in edges for i in edge.AdjacentFaces] #get the adjacent faces for the edges
        
        for f in adjFaces: #for every face in the adjacent faces list
            if (f not in grouped) and (f not in toGroup): #if the face is not in the grouped list or in the toGroup list
                faces.remove(f) #remove the face from the faces list
                toGroup.append(f) #append the face to the toGroup list
    surfs = [f.SurfaceGeometry() for f in grouped] #convert the faces to surfaces
    polysurfaces.append(PolySurface.ByJoinedSurfaces(surfs)) #generate a polysurface and append to the list of polysurfaces

OUT = polysurfaces #return the list of PolySurfaces to the Dynamo environment
3 Likes