-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.py
executable file
·348 lines (274 loc) · 9.09 KB
/
slice.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python
import argparse
import math
import multiprocessing
import string
import subprocess
import logging
import os
#==============================================================================
LOG = logging.getLogger(__name__)
SLICER_TEMPLATE = string.Template("""
$import_str
projection(cut=true)
{
translate([0, 0, -LAYER_HEIGHT-0.001])
{
difference()
{
union()
{
$object_str
}
union()
{
$key_str
}
}
}
}
""")
#==============================================================================
def frange(end,start=0,inc=0,precision=1):
"""
A range function that accepts float increments.
"""
if not start:
start = end + 0.0
end = 0.0
else:
end += 0.0
if not inc:
inc = 1.0
count = int(math.ceil((start - end) / inc))
L = [None] * count
L[0] = end
for i in (xrange(1,count)):
L[i] = L[i-1] + inc
return L
#==============================================================================
def execute_slice(args):
"""
Create new slicing thread.
@param args Arguments:
scad_file .scad file used to create slice
height Height at which to take slice
output_file DXF file to save to
openscad OpenSCAD executable
"""
scad_file, height, output_file, openscad = args
height_param = "-DLAYER_HEIGHT={0}".format(height)
try:
LOG.info("Starting slice at height {0}".format(height))
subprocess.check_call([openscad,
height_param,
"-o", output_file,
scad_file])
LOG.info("Completed slice at height {0}".format(height))
except subprocess.CalledProcessError as cpe:
LOG.error(str(cpe))
#==============================================================================
class SlicingOperation(object):
"""
Configuration and runner for a slicing operation.
"""
def __init__(self, out_format, openscad='openscad', processes=4):
"""
Create new slicing configuration.
Output format must include both $height in the file name and end in
.dxf
@param out_format Output directory and file format
@param openscad Executable for OpenSCAD (default: openscad)
@param processes Number of processes (default: 4)
"""
self.scad_includes = []
self.scad_object_modules = []
self.scad_key_modules = []
self.keep_scad_file = False
self.scad_filename = 'object_slice.scad'
self._out_format = out_format
self._openscad_command = openscad
self._processes = processes
self._scad_include_str = 'include <{0}>;'
self._slices = []
def set_slices(self, start=0.0, end=1.0, step=None, num=None):
"""
Sets the slicing.
Set either step or num, not both.
@param start Height (in mm) of first slice
@param end Height (in mm) of end slice
@param step Step (in mm) between slices
@param num Number of slices (not guaranteed)
"""
if step is None:
s = (end - start) / float(num)
self._slices = frange(start, end, s)
elif num is None:
self._slices = frange(start, end, step)
else:
raise RuntimeError()
LOG.info('Num slices: %d', len(self._slices))
LOG.info('Slices: %s', self._slices)
def slice(self):
"""
Executes the slicing.
"""
self._make_output_dir();
self._make_slice_file();
out_format = string.Template(self._out_format)
jobs = [(self.scad_filename, h, out_format.substitute(height=h), self._openscad_command) for h in self._slices]
pool = multiprocessing.Pool(processes=self._processes)
pool.map(execute_slice, jobs)
if not self.keep_scad_file:
LOG.info('Removing .scad file: %s', self.scad_filename)
os.remove(self.scad_filename)
def _make_output_dir(self):
"""
Creates the output directory if it does not already exist.
"""
out_dir = os.path.dirname(self._out_format)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
LOG.info('Created output directory: %s', out_dir)
def _make_slice_file(self):
"""
Generates the .scad file used to slice the object at a given height.
"""
import_str = '\n'.join([self._scad_include_str.format(i) for i in self.scad_includes])
object_str = '\n'.join([m + ';' for m in self.scad_object_modules])
key_str = '\n'.join([m + ';' for m in self.scad_key_modules])
contents = SLICER_TEMPLATE.substitute(import_str=import_str,
object_str=object_str,
key_str=key_str,
openscad=self._openscad_command)
LOG.debug('.scad file contents: %s', contents)
with open(self.scad_filename, 'w') as f:
f.write(contents)
LOG.info('Created .scad file: %s', self.scad_filename)
#==============================================================================
def parse_cli():
"""
Parses the command line.
@return Namespace of parsed options
"""
parser = argparse.ArgumentParser(description='Slice 3D objects into many 2D projections')
parser.add_argument(
'-om', '--object-module',
action='append',
type=str,
help='An SCAD module that makes up the object being sliced'
)
parser.add_argument(
'-km', '--key-module',
action='append',
type=str,
help='An SCAD module that makes up the keying of the sliced object'
)
parser.add_argument(
'-i', '--include',
action='append',
type=str,
help='An include that is inserted into the SCAD file'
)
parser.add_argument(
'-st', '--start',
action='store',
type=float,
default=0.0,
help='Minimum object height to slice from'
)
parser.add_argument(
'-ed', '--end',
action='store',
type=float,
default=100.0,
help='Maximum object height to slice to'
)
parser.add_argument(
'-s', '--step',
action='store',
type=float,
help='Seperation between slices'
)
parser.add_argument(
'-n', '--number',
action='store',
type=int,
help='Number of slices to make'
)
parser.add_argument(
'-o', '--output',
action='store',
type=str,
default='./out/slice_$height.dxf',
help='Output directory and format, must contain "$height" and end in ".dxf"'
)
parser.add_argument(
'--scad-filename',
action='store',
type=str,
default='object_slice.scad',
help='FIlename to save OpenSCAD file that slices a single layer as'
)
parser.add_argument(
'-k', '--keep-scad-file',
action='store_true',
default=False,
help='Keep the OpenSCAD file that slices a single layer'
)
parser.add_argument(
'--openscad-command',
action='store',
type=str,
default='openscad',
help='Command used to execute OpenSCAD'
)
parser.add_argument(
'-j', '--jobs',
action='store',
type=int,
default=4,
help='Number oj jobs (processes)'
)
parser.add_argument(
'--log-level',
action='store',
type=str,
default='INFO',
help='Logging level [DEBUG,INFO,WARNING,ERROR,CRITICAL]'
)
props = parser.parse_args()
return props
#==============================================================================
def run_from_cl(props):
"""
Configures a SlicingOperation from parsed command line options and executes
it.
@param props Parsed options
"""
# Setup logging
log_level = getattr(logging, props.log_level.upper(), None)
if not isinstance(log_level, int):
log_level = logging.INFO
logging.basicConfig(level=log_level)
logging.getLogger(__name__).info('Hello, world!')
if props.object_module is None:
sys.out.write('Need at least one object module!\n')
props.print_help()
sys.exit(1)
# Setup slicing
sc = SlicingOperation(props.output, props.openscad_command, props.jobs)
sc.scad_object_modules.extend(props.object_module)
sc.scad_key_modules.extend(props.key_module if props.key_module is not None else [])
sc.scad_includes.extend(props.include if props.include is not None else [])
sc.keep_scad_file = props.keep_scad_file
sc.scad_filename = props.scad_filename
if props.step:
sc.set_slices(start=props.start, end=props.end, step=props.step)
elif props.number:
sc.set_slices(start=props.start, end=props.end, num=props.number)
sc.slice()
#==============================================================================
if __name__ == '__main__':
props = parse_cli()
run_from_cl(props)