forked from nophead/Mendel90
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stls.py
executable file
·98 lines (91 loc) · 3.01 KB
/
stls.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
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
#!/usr/bin/env python
import os
import openscad
import shutil
import sys
import c14n_stl
source_dir = "scad"
def bom_to_stls(machine):
#
# Make a list of all the stls in the BOM
#
stl_files = []
for line in open(machine + "/bom/bom.txt", "rt").readlines():
words = line.split()
if words:
last_word = words[-1]
if len(last_word) > 4 and last_word[-4:] == ".stl":
stl_files.append(last_word)
return stl_files
def stls(machine, parts = None):
#
# Make the target directory
#
target_dir = machine + "/stls"
if os.path.isdir(target_dir):
if not parts:
shutil.rmtree(target_dir) #if making the BOM clear the directory first
os.makedirs(target_dir)
else:
os.makedirs(target_dir)
#
# Set the target machine
#
f = open("scad/conf/machine.scad","wt")
f. write("include <%s_config.scad>\n" % machine);
f.close()
#
# Decide which files to make
#
if parts:
targets = list(parts) #copy the list so we dont modify the list passed in
else:
targets = bom_to_stls(machine)
#
# Find all the scad files
#
used = []
for filename in os.listdir(source_dir):
if filename[-5:] == ".scad":
#
# find any modules ending in _stl
#
for line in open(source_dir + "/" + filename, "r").readlines():
words = line.split()
if(len(words) and words[0] == "module"):
module = words[1].split('(')[0]
stl = module.replace("_stl", ".stl")
if stl in targets:
#
# make a file to use the module
#
stl_maker_name = source_dir + "/stl.scad"
f = open(stl_maker_name, "w")
f.write("use <%s>\n" % filename)
f.write("%s();\n" % module);
f.close()
#
# Run openscad on the created file
#
stl_name = target_dir + "/" + module[:-4] + ".stl"
openscad.run("-o", stl_name, stl_maker_name)
c14n_stl.canonicalise(stl_name)
targets.remove(stl)
#
# Add the files on the BOM to the used list for plates.py
#
for line in open("openscad.log"):
if line[:7] == 'ECHO: "' and line[-6:] == '.stl"\n':
used.append(line[7:-2])
#
# List the ones we didn't find
#
for module in targets:
print "Could not find", module
return used
if __name__ == '__main__':
if len(sys.argv) > 1:
stls(sys.argv[1], sys.argv[2:])
else:
print "usage: stls [mendel|sturdy|your_machine] [part.stl ...]"
sys.exit(1)