-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenSCADJob.py
More file actions
57 lines (48 loc) · 1.93 KB
/
OpenSCADJob.py
File metadata and controls
57 lines (48 loc) · 1.93 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
import os
from multiprocessing import Pool
import multiprocessing
import numpy
import stl
from stl import mesh
def executeJob(job):
job.do()
class JobDescription():
def __init__(self, scad, filename):
self.variables = {}
self.scad = scad
self.filename = filename
def addVariable(self, name, value):
if isinstance(value, str):
self.variables[name] = "\\\"{}\\\"".format(value)
elif type(value) == bool:
self.variables[name] = "true" if value else "false"
elif isinstance(value, list):
# just hope it's a list of strings, otherwise I cba to refactor this
self.variables[name] = "[\\\"" + ("\\\",\\\"".join(value)) + "\\\"]"
else:
self.variables[name] = value
def addVariables(self, dict):
'''
given dict of ["variable name"] = value, add them all
:param dict:
:return:
'''
for key in dict:
self.addVariable(key, dict[key])
def getVariableString(self):
return " ".join(
["-D {varname}={varvalue}".format(varname=key, varvalue=self.variables[key]) for key in self.variables])
def do(self):
cmd = "openscad -o out/{filename}.stl {variablestring} {scad}".format(filename=self.filename,
variablestring=self.getVariableString(),
scad=self.scad)
print(cmd)
os.system(cmd)
print("finished {}".format(self.filename))
model = mesh.Mesh.from_file("out/{filename}.stl".format(filename=self.filename))
model.save("out/{filename}-binary.stl".format(filename=self.filename), mode=stl.Mode.BINARY)
# TODO can even use matplotlib to render it!
#
def multiprocessJobs(jobs):
p = Pool(multiprocessing.cpu_count() - 1)
p.map(executeJob, jobs)