Skip to content

Commit 4db3525

Browse files
Add files via upload
1 parent f9cc80a commit 4db3525

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
370 KB
Binary file not shown.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# UINT8[80] – Header - 80 bytes
2+
# UINT32 – Number of triangles - 4 bytes
3+
# foreach triangle - 50 bytes:
4+
# REAL32[3] – Normal vector - 12 bytes
5+
# REAL32[3] – Vertex 1 - 12 bytes
6+
# REAL32[3] – Vertex 2 - 12 bytes
7+
# REAL32[3] – Vertex 3 - 12 bytes
8+
# UINT16 – Attribute byte count - 2 bytes
9+
# end
10+
11+
from vpython import *
12+
import struct
13+
from collections import namedtuple
14+
import numpy as np
15+
16+
Ctypes = namedtuple('Ctype', ['fmt', 'size'])
17+
18+
Ctype_names = ['char', 'signed char', 'unsigned char', '_Bool', 'short', \
19+
'unsigned short', 'int', 'unsigned int', 'long', 'unsigned long',\
20+
'long long', 'unsigned long long', 'float', 'double', 'char[]']
21+
Ctype_sizes = [1, 1, 1, 1, 2,\
22+
2, 4, 4, 4, 4,\
23+
8, 8, 4, 8, -1]
24+
Ctype_formats = ['c', 'b', 'B', '?', 'h',\
25+
'H', 'i', 'I', 'l', 'L',\
26+
'q', 'Q', 'f', 'd', 's']
27+
28+
Ctype_dict = {}
29+
for i, name in enumerate(Ctype_names):
30+
Ctype_dict[name] = Ctypes(Ctype_formats[i], Ctype_sizes[i])
31+
32+
def binary_reader(fid, Ctype, **opts):
33+
assert Ctype in Ctype_dict, "Ctype not found in Ctype_dict"
34+
if Ctype == 'char[]':
35+
string = []
36+
for ch in range(opts['size']):
37+
char = struct.unpack(Ctype_dict['char'].fmt, fid.read(Ctype_dict['char'].size))[0]
38+
if chr(0).encode('utf-8') not in char:
39+
string.append(char.decode('utf-8'))
40+
else:
41+
string.append(' ')
42+
return ''.join(string)
43+
44+
if Ctype == 'char':
45+
return ord(struct.unpack(Ctype_dict[Ctype].fmt, fid.read(Ctype_dict[Ctype].size))[0].decode('utf-8'))
46+
return struct.unpack(Ctype_dict[Ctype].fmt, fid.read(Ctype_dict[Ctype].size))[0]
47+
48+
def stl_to_triangles(filename):
49+
with open(filename, "rb") as f:
50+
header = binary_reader(f,'char[]', size = 80)
51+
numOfTri = binary_reader(f,'unsigned int')
52+
53+
print(header)
54+
print(numOfTri)
55+
56+
tris = []
57+
58+
for i in range(numOfTri):
59+
vs = []
60+
x1, x2, x3 = binary_reader(f,'float'), binary_reader(f,'float'), binary_reader(f,'float')
61+
N = vec(x1, x2, x3)
62+
63+
x1, x2, x3 = binary_reader(f,'float'), binary_reader(f,'float'), binary_reader(f,'float')
64+
p1 = vertex(pos = vec(x1, x2, x3), normal=N, color=color.white)
65+
vs.append(p1)
66+
67+
x1, x2, x3 = binary_reader(f,'float'), binary_reader(f,'float'), binary_reader(f,'float')
68+
p2 = vertex(pos = vec(x1, x2, x3), normal=N, color=color.white)
69+
vs.append(p2)
70+
71+
x1, x2, x3 = binary_reader(f,'float'), binary_reader(f,'float'), binary_reader(f,'float')
72+
p3 = vertex(pos = vec(x1, x2, x3), normal=N, color=color.white)
73+
vs.append(p3)
74+
75+
attr = binary_reader(f,'unsigned short')
76+
tris.append(triangle(vs=vs))
77+
78+
return compound(tris)
79+
80+
if __name__ == '__main__':
81+
filename = r"\convert_stl\Part1_bin.stl" # update this file
82+
man = stl_to_triangles(filename)
83+
man.color = color.orange
84+
85+
86+

0 commit comments

Comments
 (0)