2012-03-16 00:31:47 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2012-03-12 01:13:07 +00:00
|
|
|
import os
|
2012-03-12 22:55:27 +02:00
|
|
|
import openscad
|
2012-03-12 01:13:07 +00:00
|
|
|
import shutil
|
|
|
|
import sys
|
2013-03-24 18:48:03 +00:00
|
|
|
import c14n_stl
|
2016-01-02 13:54:07 +00:00
|
|
|
from set_machine import *
|
2016-01-02 16:09:08 +00:00
|
|
|
from time import *
|
2012-03-12 01:13:07 +00:00
|
|
|
|
2014-06-12 19:21:16 +01:00
|
|
|
from bom import source_dir
|
2012-03-12 01:13:07 +00:00
|
|
|
|
2014-06-12 19:21:16 +01:00
|
|
|
def bom_to_stls(machine, assembly = None):
|
2012-03-16 00:31:47 +00:00
|
|
|
#
|
|
|
|
# Make a list of all the stls in the BOM
|
|
|
|
#
|
|
|
|
stl_files = []
|
2014-06-12 19:21:16 +01:00
|
|
|
if assembly:
|
|
|
|
bom = "accessories/%s.txt" % assembly
|
|
|
|
else:
|
|
|
|
bom = "bom.txt"
|
|
|
|
for line in open(machine + "/bom/" + bom, "rt").readlines():
|
2012-03-16 00:31:47 +00:00
|
|
|
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
|
|
|
|
|
2012-03-14 00:01:25 +08:00
|
|
|
def stls(machine, parts = None):
|
2012-03-12 01:13:07 +00:00
|
|
|
#
|
|
|
|
# Make the target directory
|
|
|
|
#
|
|
|
|
target_dir = machine + "/stls"
|
|
|
|
if os.path.isdir(target_dir):
|
2012-03-14 23:15:12 +00:00
|
|
|
if not parts:
|
2012-03-14 23:21:37 +00:00
|
|
|
shutil.rmtree(target_dir) #if making the BOM clear the directory first
|
2016-01-02 16:09:08 +00:00
|
|
|
sleep(0.1)
|
2012-03-14 23:21:37 +00:00
|
|
|
os.makedirs(target_dir)
|
2012-03-14 23:15:12 +00:00
|
|
|
else:
|
|
|
|
os.makedirs(target_dir)
|
2012-03-12 01:13:07 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Set the target machine
|
|
|
|
#
|
2016-01-02 13:54:07 +00:00
|
|
|
set_machine(machine)
|
2012-03-12 01:13:07 +00:00
|
|
|
|
|
|
|
#
|
2012-03-27 18:12:04 +01:00
|
|
|
# Decide which files to make
|
2012-03-12 01:13:07 +00:00
|
|
|
#
|
2012-03-14 23:15:12 +00:00
|
|
|
if parts:
|
2012-03-16 00:31:47 +00:00
|
|
|
targets = list(parts) #copy the list so we dont modify the list passed in
|
2012-03-14 23:15:12 +00:00
|
|
|
else:
|
2012-03-16 00:31:47 +00:00
|
|
|
targets = bom_to_stls(machine)
|
2012-03-12 01:13:07 +00:00
|
|
|
#
|
|
|
|
# Find all the scad files
|
|
|
|
#
|
2012-03-16 00:31:47 +00:00
|
|
|
used = []
|
2012-03-12 01:13:07 +00:00
|
|
|
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]
|
2012-03-14 23:15:12 +00:00
|
|
|
stl = module.replace("_stl", ".stl")
|
|
|
|
if stl in targets:
|
2012-03-12 01:13:07 +00:00
|
|
|
#
|
|
|
|
# make a file to use the module
|
|
|
|
#
|
|
|
|
stl_maker_name = source_dir + "/stl.scad"
|
|
|
|
f = open(stl_maker_name, "w")
|
2012-03-27 18:12:04 +01:00
|
|
|
f.write("use <%s>\n" % filename)
|
2012-03-12 01:13:07 +00:00
|
|
|
f.write("%s();\n" % module);
|
|
|
|
f.close()
|
|
|
|
#
|
|
|
|
# Run openscad on the created file
|
|
|
|
#
|
|
|
|
stl_name = target_dir + "/" + module[:-4] + ".stl"
|
2013-09-25 01:28:24 +01:00
|
|
|
openscad.run("-D$bom=1","-o", stl_name, stl_maker_name)
|
2013-03-24 18:48:03 +00:00
|
|
|
c14n_stl.canonicalise(stl_name)
|
2012-03-14 23:15:12 +00:00
|
|
|
targets.remove(stl)
|
2012-03-16 00:31:47 +00:00
|
|
|
#
|
|
|
|
# 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])
|
2012-03-12 01:13:07 +00:00
|
|
|
#
|
|
|
|
# List the ones we didn't find
|
|
|
|
#
|
|
|
|
for module in targets:
|
2014-02-06 19:43:19 -08:00
|
|
|
print("Could not find", module)
|
2012-03-16 00:31:47 +00:00
|
|
|
return used
|
2012-03-12 01:13:07 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) > 1:
|
2012-03-14 00:01:25 +08:00
|
|
|
stls(sys.argv[1], sys.argv[2:])
|
2012-03-12 01:13:07 +00:00
|
|
|
else:
|
2016-01-16 10:47:25 +00:00
|
|
|
print("usage: stls dibond|mendel|sturdy|huxley|your_machine [part.stl ...]")
|
2016-01-06 00:49:11 +00:00
|
|
|
sys.exit(1)
|