-
Notifications
You must be signed in to change notification settings - Fork 17
/
BinaryToASCII.py
50 lines (35 loc) · 1.41 KB
/
BinaryToASCII.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- encoding: utf-8 -*-
import struct
infile = open('binary.stl') #import file
out = open('ASCII.stl', 'w') #export file
data = infile.read()
out.write("solid ")
for x in xrange(0,80):
if not ord(data[x]) == 0:
out.write(struct.unpack('c', data[x])[0])
else:
pass
out.write("\n")
number = data[80] + data[81] + data[82] + data[83]
faces = struct.unpack('I',number)[0]
for x in range(0,faces):
out.write("facet normal ")
xc = data[84+x*50] + data[85+x*50] + data[86+x*50] + data[87+x*50]
yc = data[88+x*50] + data[89+x*50] + data[90+x*50] + data[91+x*50]
zc = data[92+x*50] + data[93+x*50] + data[94+x*50] + data[95+x*50]
out.write(str(struct.unpack('f',xc)[0]) + " ")
out.write(str(struct.unpack('f',yc)[0]) + " ")
out.write(str(struct.unpack('f',zc)[0]) + "\n")
out.write("outer loop\n")
for y in range(1,4):
out.write("vertex ")
xc = data[84+y*12+x*50] + data[85+y*12+x*50] + data[86+y*12+x*50] + data[87+y*12+x*50]
yc = data[88+y*12+x*50] + data[89+y*12+x*50] + data[90+y*12+x*50] + data[91+y*12+x*50]
zc = data[92+y*12+x*50] + data[93+y*12+x*50] + data[94+y*12+x*50] + data[95+y*12+x*50]
out.write(str(struct.unpack('f',xc)[0]) + " ")
out.write(str(struct.unpack('f',yc)[0]) + " ")
out.write(str(struct.unpack('f',zc)[0]) + "\n")
out.write("endloop\n")
out.write("endfacet\n")
out.close()
print "end"