Skip to content

Commit 83e8b01

Browse files
Merge pull request #162 from vpython/BruceSherwood-minor-improvement-to-convert_stl.py
Add files via upload
2 parents b36657f + 5548de9 commit 83e8b01

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

Demos_no_notebook/convert_stl/convert_stl.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def stl_to_triangles(fileinfo): # specify file
3232
fd = open(fileinfo, mode='rb')
3333
text = fd.read()
3434
tris = [] # list of triangles to compound
35+
keywords = [b'outer', b'endloop', b'endfacet', b'solid', b'endsolid']
3536
if False: # prevent executing code for binary file
3637
pass
3738
# The following code for binary files must be updated:
@@ -60,16 +61,18 @@ def stl_to_triangles(fileinfo): # specify file
6061
else:
6162
fd.seek(0)
6263
fList = fd.readlines()
64+
print('Number of lines =', len(fList))
6365

6466
# Decompose list into vertex positions and normals
6567
ret = [] # will return a list of compounds if necessary
6668
vs = []
6769
vertices = 0
6870
for line in fList:
6971
FileLine = line.split( )
70-
if FileLine[0] == b'facet':
72+
first = FileLine[0]
73+
if first == b'facet':
7174
N = vec(float(FileLine[2]), float(FileLine[3]), float(FileLine[4]))
72-
elif FileLine[0] == b'vertex':
75+
elif first == b'vertex':
7376
vertices += 1
7477
vs.append( vertex(pos=vec(float(FileLine[1]), float(FileLine[2]), float(FileLine[3])), normal=N, color=color.white) )
7578
if len(vs) == 3:
@@ -80,16 +83,20 @@ def stl_to_triangles(fileinfo): # specify file
8083
ret.append(compound(tris))
8184
tris = []
8285
vertices = 0
86+
elif first in keywords:
87+
pass
88+
else:
89+
print(line) # for debugging
8390
if len(tris) > 0: ret.append(compound(tris))
8491
if len(ret) == 1: return ret[0]
8592
else: return ret
8693

8794
if __name__ == '__main__':
88-
man = stl_to_triangles('z-as.stl')
95+
man = stl_to_triangles('STLbot.stl')
96+
man.pos = vec(-200,0,0)
97+
man.color = color.cyan
98+
part = stl_to_triangles('Part1.stl')
99+
part.size *= 200
100+
part.pos = vec(250,0,0)
101+
part.color = color.orange
89102
print('Done')
90-
# man.pos = vec(-200,0,0)
91-
# man.color = color.cyan
92-
# part = stl_to_triangles('Part1.stl')
93-
# part.size *= 200
94-
# part.pos = vec(250,0,0)
95-
# part.color = color.orange

0 commit comments

Comments
 (0)