def triangle_mesh_solid(solid, lin_tol=1e-2, ang_tol=0.5): """Computes a triangular mesh for a solid using BRepMesh. The resolution or quality of the mesh approximation can be adjusted with lin_tol and ang_tol (linear and angular tolerances). The computed mesh is returned as a tuple of lists: triangles - a list of each triangles' 3x vertices represented as indexes into the vertices list vertices - a list of the mesh's 3D vertices """ if isinstance(solid, Solid): obj = [solid.wrapped] elif isinstance(solid, list): obj = [x.wrapped for x in solid] else: obj = [solid] vertices = [] triangles = [] for o in obj: mesh = BRepMesh_IncrementalMesh(o, lin_tol, False, ang_tol) mesh.Perform() ms = Shape.cast(mesh.Shape()) bt = BRep_Tool() mesh_faces = ms.Faces() for mesh_face in mesh_faces: face = mesh_face.wrapped location = TopLoc_Location() facing = bt.Triangulation(face, location) tri = facing.Triangles() num_tri = facing.NbTriangles() vtx = facing.Nodes() txf = face.Location().Transformation() rev = (True if face.Orientation() == TopAbs_Orientation.TopAbs_REVERSED else False) for i in range(1, num_tri + 1): idx = list(tri.Value(i).Get()) ci = [0, 2, 1] if rev else [0, 1, 2] for j in ci: pt = [ vtx.Value(idx[j]).Transformed(txf).X(), vtx.Value(idx[j]).Transformed(txf).Y(), vtx.Value(idx[j]).Transformed(txf).Z(), ] if pt not in vertices: vertices.append(pt) idx[j] = vertices.index(pt) triangles.append(idx) return triangles, vertices
# write_stl_file(face2_stl, obj.tempname + "_face2_001.stp", # linear_deflection=0.1, angular_deflection=0.1) # write_stl_file(face2_stl, obj.tempname + "_face2_002.stp", # linear_deflection=0.001, angular_deflection=0.001) surf2_trim = Geom_RectangularTrimmedSurface(surf2, 0.1, 0.8, 0.5, 0.7, True, True) face3 = read_step_file("surf1.step") print(face3) #surf3 = BRep_Tool.Surface(face3) # print(surf3) mesh = BRepMesh_IncrementalMesh(face2, 0.01, True, 0.01, True) mesh.Perform() face3_mesh = mesh.Shape() obj.display.DisplayShape(surf1, color="BLUE", transparency=0.5) obj.display.DisplayShape(poly1) obj.display.DisplayShape(poly1_proj) obj.display.DisplayShape(face1_holl, color="GREEN", transparency=0.5) #obj.display.DisplayShape(face1_trim, color="BLACK", transparency=0.5) obj.display.DisplayShape(surf2, color="RED", transparency=0.9) obj.display.DisplayShape(poly2) obj.display.DisplayShape(poly2_proj) obj.display.DisplayShape(face3, color="YELLOW", transparency=0.9) obj.display.DisplayShape(surf2_trim, color="RED", transparency=0.5) obj.display.DisplayShape(surf2_uiso) obj.display.DisplayShape(surf2_viso) obj.show()