def read_stl(filepath):
"""
Return the triangles and points of an stl binary file.
Please note that this process can take lot of time if the file is
huge (~1m30 for a 1 Go stl file on an quad core i7).
- returns a tuple(triangles, triangles' normals, points).
triangles
A list of triangles, each triangle as a tuple of 3 index of
point in *points*.
triangles' normals
A list of vectors3 (tuples, xyz).
points
An indexed list of points, each point is a tuple of 3 float
(xyz).
Example of use:
>>> tris, tri_nors, pts = read_stl(filepath)
>>> pts = list(pts)
>>>
>>> # print the coordinate of the triangle n
>>> print(pts[i] for i in tris[n])
"""
import time
start_time = time.process_time()
tris, tri_nors, pts = [], [], ListDict()
with open(filepath, 'rb') as data:
# check for ascii or binary
gen = _ascii_read if _is_ascii_file(data) else _binary_read
for nor, pt in gen(data):
# Add the triangle and the point.
# If the point is allready in the list of points, the
# index returned by pts.add() will be the one from the
# first equal point inserted.
tris.append([pts.add(p) for p in pt])
tri_nors.append(nor)
print('Import finished in %.4f sec.' % (time.process_time() - start_time))
return tris, tri_nors, pts.list
评论列表
文章目录