def svmesh_to_solid(verts, faces, precision=1e-6, remove_splitter=True, method=FCMESH): """ input: verts: list of 3element iterables, [vector, vector...] faces: list of lists of face indices precision: a conversion factor defined in makeShapeFromMesh (FreeCAD) remove_splitter: default True, removes duplicate geometry (edges) output: a FreeCAD solid """ if method == FCMESH: tri_faces = ensure_triangles(verts, faces, True) faces_t = [[verts[c] for c in f] for f in tri_faces] mesh = Mesh.Mesh(faces_t) shape = Part.Shape() shape.makeShapeFromMesh(mesh.Topology, precision) if remove_splitter: # may slow it down, or be totally necessary shape = shape.removeSplitter() return Part.makeSolid(shape) elif method == BMESH: fc_faces = [] for face in faces: face_i = list(face) + [face[0]] face_verts = [Base.Vector(verts[i]) for i in face_i] wire = Part.makePolygon(face_verts) wire.fixTolerance(precision) try: fc_face = Part.Face(wire) #fc_face = Part.makeFilledFace(wire.Edges) except Exception as e: print(f"Face idxs: {face_i}, verts: {face_verts}") raise Exception("Maybe face is not planar?") from e fc_faces.append(fc_face) shell = Part.makeShell(fc_faces) solid = Part.makeSolid(shell) if remove_splitter: solid = solid.removeSplitter() return solid else: raise Exception("Unsupported method")
def svmesh_to_solid(verts, faces, precision, remove_splitter=True): """ input: verts: list of 3element iterables, [vector, vector...] faces: list of lists of face indices precision: a conversion factor defined in makeShapeFromMesh (FreeCAD) remove_splitter: default True, removes duplicate geometry (edges) output: a FreeCAD solid """ tri_faces = ensure_triangles(verts, faces, True) faces_t = [[verts[c] for c in f] for f in tri_faces] mesh = Mesh.Mesh(faces_t) shape = Part.Shape() shape.makeShapeFromMesh(mesh.Topology, precision) if remove_splitter: # may slow it down, or be totally necessary shape = shape.removeSplitter() return Part.makeSolid(shape)