-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.py
More file actions
148 lines (121 loc) · 5.46 KB
/
Loader.py
File metadata and controls
148 lines (121 loc) · 5.46 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import struct
class Load:
def __init__(self) -> None:
self.vertices = []
self.triangles = 0
self.points = []
self.count = 0
def load(self, filename):
#file = open(filename,'rb')
#data = file.read(80) #80 bytes is enough to determin if the file is binary
#type = data[0:5]
#file.close()
Ascii = True
fp = open(filename, 'r')
try:
line = fp.readline()
words = line.split()
except:
self.loadBinaryStl(filename)
Ascii = False
if(Ascii == True):
self.loadTextStl(filename)
#if len(words) > 0:
# if words[0] == 'solid':
# self.loadTextStl(filename)
# print("ASCII")
# else:
# self.loadBinaryStl(filename)
# print("Binary")
def loadTextStl(self, filename):
fp = open(filename, 'r')
self.normal = []
for line in fp.readlines():
words = line.split()
if len(words) > 0:
if words[0] == 'facet':
#name = words[1]
self.normal.append(eval(words[2]))
self.normal.append(eval(words[3]))
self.normal.append(eval(words[4]))
if words[0] == 'vertex':
#final list of vertices
self.vertices.append(eval(words[1]))
self.vertices.append(eval(words[2]))
self.vertices.append(eval(words[3]))
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(self.normal[0])
self.vertices.append(self.normal[1])
self.vertices.append(self.normal[2])
if words[0] == 'endloop':
#for OpenGL to create the triangle, we get the index of the vertex in the list vertices
#self.triangle.append((self.vertices.index(self.points[0]), self.vertices.index(self.points[1]), self.vertices.index(self.points[2])))
#self.triangle.append((len(self.vertices)-3, len(self.vertices)-2, len(self.vertices)-1))
self.normal = []
def loadBinaryStl(self, filename):
fp = open(filename, 'rb')
header = fp.read(80)
self.vertices = []
self.triangles = 0
self.normal = []
l = struct.unpack('i',fp.read(4))[0]
self.triangles = l
while True:
try:
self.normal = []
p = fp.read(12)
if len(p) == 12:
normal = []
self.normal.append(struct.unpack('f',p[0:4])[0])
self.normal.append(struct.unpack('f',p[4:8])[0])
self.normal.append(struct.unpack('f',p[8:12])[0])
p=fp.read(12)
if len(p)==12:
self.vertices.append(struct.unpack('f',p[0:4])[0])
self.vertices.append(struct.unpack('f',p[8:12])[0])
self.vertices.append(struct.unpack('f',p[4:8])[0])
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(self.normal[0])
self.vertices.append(self.normal[1])
self.vertices.append(self.normal[2])
p=fp.read(12)
if len(p)==12:
self.vertices.append(struct.unpack('f',p[0:4])[0])
self.vertices.append(struct.unpack('f',p[8:12])[0])
self.vertices.append(struct.unpack('f',p[4:8])[0])
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(self.normal[0])
self.vertices.append(self.normal[1])
self.vertices.append(self.normal[2])
p=fp.read(12)
if len(p)==12:
self.vertices.append(struct.unpack('f',p[0:4])[0])
self.vertices.append(struct.unpack('f',p[8:12])[0])
self.vertices.append(struct.unpack('f',p[4:8])[0])
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(1.00000000000000)
self.vertices.append(self.normal[0])
self.vertices.append(self.normal[1])
self.vertices.append(self.normal[2])
self.count += 1
fp.read(2)
if len(p)==0:
break
except EOFError: #when file is finished
break
fp.close()
def returnCount(self):
return self.count
def returnTriangles(self):
return self.triangles