def convert(fq_in_path, fq_out_path, skip_ascii=False): print 'reading: {}...'.format(fq_in_path), with open(fq_in_path) as f: data = f.read() if data.startswith('solid'): if skip_ascii: print ' skipped because in ASCII format' return try: solid = read_ascii_file(StringIO(data)) except ascii.SyntaxError: print ' FAILED to convert!' return else: try: solid = read_binary_file(StringIO(data)) except binary.FormatError: print ' FAILED to convert!' return fq_out_base, ext = splitext(fq_out_path) fq_out_path = fq_out_base + '.raw' with open(fq_out_path, 'w') as f: for facet in solid.facets: f.write(' '.join(' '.join(map(str, vertex)) for vertex in facet.vertices) + '\n') print ' converted in: {}'.format(fq_out_path)
def addSTL(self, fileName=[], stlObject=[], scale=1.0, move=(0.0, 0.0, 0.0), rotate=(0.0, 0.0, 0.0, 0.0), name='stl', matType=1): if len(fileName) > 0 and len(stlObject) > 0: print("Cannot give both a filename and a stl object") if len(fileName) > 0: print("Reading in stl file: " + fileName) try: stlMesh = stl.mesh.Mesh.from_file(fileName) except: with open(fileName) as f: stlMesh = stl.read_ascii_file(f) if name == 'stl': name += str(len(self.stlFiles)) newSTL = stlOb(name=name, matType=matType, stlMesh=stlMesh, move=move, rotate=rotate, scale=scale, fileLocation=fileName, stlObject=stlObject) self.stlFiles.append(newSTL)
def get_combined_face(facebits): combined_vertices = set() for vertex in facebits: combined_vertices.add(vertex) combined_vertices.update(get_combined_face(vertmap.pop(vertex))) return vertices.union(get_combined_face(facebits[1])) if __name__ == "__main__": fl = open(sys.argv[1]) print(sys.argv[1]) try: mesh = stl.read_binary_file(fl) except stl.binary.FormatError: mesh = stl.read_ascii_file(fl) # planes in stl files are made up of many triangles. We only want to have joints at the corners, where they are needed. # To determine what faces can be combined, faces can be group'd by thier inclination. Not all faces with the same inclination will combine, but a lot will. normal2tris = defaultdict(set) tri2normal = {} vert2tris = defaultdict(set) vert2normals = defaultdict(set) tri2edges = defaultdict(set) for tri in mesh.facets: # round the things so that we can get floating point coordinate-vertices to land on the same little reality block. tri = tuple(tuple(round(ordo, rounding_percision) for ordo in vertex) for vertex in tri.vertices) # add this face to the data dicts. normal = getNormalNormal(tri) normal2tris[normal].add(tri)
#!/usr/bin/python import stl import sys import fileinput infile_ascii = True in_stl = sys.stdin if infile_ascii: solid = stl.read_ascii_file(in_stl) else: solid = stl.read_binary_string(in_stl) solid.sort_facets() if infile_ascii: solid.write_ascii(sys.stdout) else: solid.write_binary(sys.stdout)