Commit 8d776b50 authored by Guillaume Seguin's avatar Guillaume Seguin

Pythonize some file handling

parent e94d076b
...@@ -55,32 +55,28 @@ def emitstl(filename, facets = [], objname = "stltool_export", binary = 1): ...@@ -55,32 +55,28 @@ def emitstl(filename, facets = [], objname = "stltool_export", binary = 1):
if filename is None: if filename is None:
return return
if binary: if binary:
f = open(filename, "wb") with open(filename, "wb") as f:
buf = "".join(["\0"] * 80) buf = "".join(["\0"] * 80)
buf += struct.pack("<I", len(facets)) buf += struct.pack("<I", len(facets))
facetformat = struct.Struct("<ffffffffffffH") facetformat = struct.Struct("<ffffffffffffH")
for i in facets: for i in facets:
l = list(i[0][:]) l = list(i[0][:])
for j in i[1]: for j in i[1]:
l += j[:] l += j[:]
l += [0] l += [0]
# print len(l) # print len(l)
buf += facetformat.pack(*l) buf += facetformat.pack(*l)
f.write(buf) f.write(buf)
f.close() else:
return with open(filename, "w") as f:
f.write("solid " + objname + "\n")
f = open(filename, "w") for i in facets:
f.write("solid " + objname + "\n") f.write(" facet normal " + " ".join(map(str, i[0])) + "\n outer loop\n")
for i in facets: for j in i[1]:
f.write(" facet normal " + " ".join(map(str, i[0])) + "\n outer loop\n") f.write(" vertex " + " ".join(map(str, j)) + "\n")
for j in i[1]: f.write(" endloop" + "\n")
f.write(" vertex " + " ".join(map(str, j)) + "\n") f.write(" endfacet" + "\n")
f.write(" endloop" + "\n") f.write("endsolid " + objname + "\n")
f.write(" endfacet" + "\n")
f.write("endsolid " + objname + "\n")
f.close()
class stl(object): class stl(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment