Skip to content

Instantly share code, notes, and snippets.

Created September 28, 2011 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1248127 to your computer and use it in GitHub Desktop.
Save anonymous/1248127 to your computer and use it in GitHub Desktop.
sort coincident faces in pythonocc
def sort_coincident_faces(self):
"""
"""
# make sure the shell can be oriented
# otherwise we're at a loss before starting...
from OCC.BRepCheck import BRepCheck_Shell
orient = BRepCheck_Shell(self.topo).Orientation()
if not orient==0:
raise AssertionError( 'orientation of the shell is messed up...')
tp_shell = Topo(self.topo)
faces = [fc for fc in self.Faces()]
# hash face to its edges
edges_from_faces = dict([[fc, [edg for edg in fc.Edges()]] for fc in self.Faces()])
source_faces = list(edges_from_faces.keys())
sorted_faces = [edges_from_faces.keys()[0]]
# add the edges of the face contained in sorted_faces
identified_edges = list(edges_from_faces[sorted_faces[0]])
for i in identified_edges:
i.show()
while source_faces:
connections = defaultdict(int)
for k,v in edges_from_faces.iteritems():
print 'k',k
for edg in v:
for i in identified_edges:
if edg.topo.IsPartner(i.topo):
print 'connect///'
connections[k]+=1
# there can be several faces that are connected to the faces in `sorted_faces`
# the face of interest is the one with the most coincident edges
k = sorted(connections.iteritems(), key=operator.itemgetter(1), reverse=True)[0][0]
if not k in sorted_faces:
sorted_faces.append(k)
identified_edges.extend(edges_from_faces[k])
for i in sorted_faces:
if i in edges_from_faces:
del edges_from_faces[i]
if i in source_faces:
del source_faces[source_faces.index(i)]
return sorted_faces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment