-
Notifications
You must be signed in to change notification settings - Fork 116
/
silhouette_multi.py
executable file
·324 lines (260 loc) · 11.2 KB
/
silhouette_multi.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
#!/usr/bin/env python3
import os
import sys
# we sys.path.append() the directory where this script lives.
sys.path.append(os.path.dirname(os.path.abspath(sys.argv[0])))
sys_platform = sys.platform.lower()
if sys_platform.startswith("win"):
sys.path.append(r"C:\Program Files\Inkscape\share\inkscape\extensions")
elif sys_platform.startswith("darwin"):
sys.path.append("/Applications/Inkscape.app/Contents/Resources/share/inkscape/extensions")
else: # linux
sys.path.append("/usr/share/inkscape/extensions")
import time
import subprocess
from threading import Thread
from tempfile import NamedTemporaryFile
from pathlib import Path
from collections import defaultdict
import traceback
from io import StringIO
from inkex.extensions import EffectExtension
from inkex import addNS, Boolean, Style, Color
from functools import partial
from itertools import groupby
from silhouette.ColorSeparation import ColorSeparation
from silhouette.Dialog import Dialog
multilogfile = None
def emit_to_log(msg, whether=True):
if whether:
print(msg, file=multilogfile)
multilogfile.flush()
def show_log_as_dialog(parent=None):
logname = multilogfile.name
multilogfile.close()
logtext = Path(logname).read_text().strip()
if logtext:
Dialog.info(parent, logtext, caption='Silhouette Multi Log')
class SilhouetteMulti(EffectExtension):
def __init__(self, *args, **kwargs):
EffectExtension.__init__(self, *args, **kwargs)
self.saved_argv = list(sys.argv)
def add_arguments(self, pars):
pars.add_argument(
"-b", "--block", dest="block_inkscape", type=Boolean,
default=False,
help="Make inkscape wait until silhouette_multi is done")
pars.add_argument(
"-d", "--dry_run", dest="dry_run", type=Boolean,
default=False,
help="Display generated commands but do not run them")
pars.add_argument(
"-g", "--gui", dest="gui", type=Boolean,
default=True,
help="Should silhouette_multi use a gui to select its actions?")
pars.add_argument(
"-p", "--pickle", dest="pickle_path", type=str,
default='',
help="Path of the pickle file with initial option settings")
pars.add_argument(
"-v", "--verbose", dest="verbose", type=Boolean,
default=False,
help="Enable verbose logging")
pars.add_argument(
"--register_once", dest="register_once", type=Boolean,
default=False,
help="Skip reading registration marks after the first action")
def get_style(self, element):
element_style = element.get('style')
if element_style is not None:
return dict(Style.parse_str(element_style))
return {}
def get_color(self, element):
if (element.tag == addNS('g', 'svg')
or element.tag == addNS('svg', 'svg')):
# Make sure we don't report a color on a group or on the svg as a whole
# (to avoid duplicate cutting)
return None
color = self.get_style(element).get('stroke', 'colorless')
if color == 'colorless':
color = self.get_style(element).get('fill', 'colorless')
if color != 'colorless':
color = Color(color).to_rgb()
return color
def load_selected_objects(self):
self.selected_objects = []
def traverse_element(element, selected=False, parent_visibility="visible"):
if self.get_style(element).get('display') == 'none':
return
visibility = element.get('visibility', parent_visibility)
if visibility == 'inherit':
visibility = parent_visibility
if element.get('id') in self.svg.selected:
selected = True
if selected and visibility not in ('hidden', 'collapse'):
self.selected_objects.append(element)
for child in element:
traverse_element(child, selected, visibility)
# if they haven't selected specific objects, then process all objects
if self.svg.selected:
select_all = False
else:
select_all = True
traverse_element(self.document.getroot(), selected=select_all)
def split_objects_by_color(self):
self.objects_by_color = defaultdict(list)
self.load_selected_objects()
for obj in self.selected_objects:
color = self.get_color(obj)
if color:
self.objects_by_color[color].append(obj)
def effect(self):
emit_to_log("silhouette_multi.py was called via: "
+ str(self.saved_argv), self.options.verbose)
setattr(self.options, 'unblock_inkscape',
not self.options.block_inkscape)
self.split_objects_by_color()
emit_to_log("Color keys are " + str(self.objects_by_color.keys()),
self.options.verbose)
self.color_separation = ColorSeparation(
colors=list(self.objects_by_color.keys()),
logger=emit_to_log,
options=self.options)
if self.options.gui:
import wx
from silhouette.MultiFrame import MultiFrame
from silhouette.Dialog import Dialog
app = wx.App()
self.frame = MultiFrame(
color_separation=self.color_separation,
logger=emit_to_log,
run_callback=self.run_multi,
options=self.options)
self.frame.Show()
app.MainLoop()
else:
self.color_separation.activate_preset('__LAST__', silent=True)
self.run_multi(self.color_separation.generate_actions({}))
def save_copy(self):
self.svg_copy_file = NamedTemporaryFile(
suffix='.svg', prefix='silhouette-multiple-actions',
delete=False) # this way the temp file will remain if error
self.svg_copy_file_name = self.svg_copy_file.name
self.document.write(self.svg_copy_file)
self.svg_copy_file.flush()
self.svg_copy_file.close()
def format_args(self, args):
if isinstance(args, dict):
args = args.items()
return " ".join(("--%s=%s" % (k, v) for k, v in args))
def id_args(self, nodes):
return self.format_args(("id", node.get("id")) for node in nodes)
def format_commands(self, actions):
commands = []
for i in range(0, len(actions)):
(color, settings) = actions[i]
# Copy regmark settings from first action to other actions for correct positioning
if i > 0 and self.options.register_once:
settings["regmark"] = actions[0][1]["regmark"]
settings["regoriginx"] = actions[0][1]["regoriginx"]
settings["regoriginy"] = actions[0][1]["regoriginy"]
settings["regwidth"] = actions[0][1]["regwidth"]
settings["reglength"] = actions[0][1]["reglength"]
command = '<PYTHON>' if self.options.dry_run else sys.executable
command += " sendto_silhouette.py"
if self.options.register_once:
if i > 0:
command += " --skip_init=True"
elif i < len(actions) - 1:
command += " --skip_reset=True"
command += " " + self.format_args(settings)
command += " " + self.id_args(self.objects_by_color[color])
command += " " + self.svg_copy_file_name
commands.append(command)
return commands
def run_multi(self, actions):
if self.options.dry_run:
self.svg_copy_file_name = '<DUMMY_FILE>'
else:
self.save_copy()
commands = self.format_commands(actions)
if self.options.gui:
self.frame.wrapup()
if self.options.dry_run:
emit_to_log("\n\n".join(commands))
else:
self.run_commands_with_dialog(commands)
os.remove(self.svg_copy_file_name)
def run_commands_with_dialog(self, commands):
for i, command in enumerate(commands):
returncode = self.run_command_with_dialog(command, step=i + 1, total=len(commands))
if returncode != 0:
# At this point, we have already displayed the log if we are
# going to. So if we want the user to see the failed command
# we just have to go ahead and display it.
# But we will use the dialog's extended message to reduce
# visual clutter
Dialog.info(None, "Action failed.",
extended = f"Return code: {returncode}\nCommand: '{command}'")
sys.exit(1)
def run_command_with_dialog(self, command, step, total):
# exec ensures that the shell gets replaced so that we can terminate the
# actual python script if the user cancels
process = subprocess.Popen("exec " + command, shell=True)
import wx
dialog = wx.ProgressDialog(style=wx.PD_APP_MODAL|wx.PD_CAN_ABORT|wx.PD_ELAPSED_TIME,
message="Performing action %d of %d..." % (step, total),
title="Silhouette Multiple Actions")
last_tick = time.time()
while process.returncode is None:
if time.time() - last_tick > 0.5:
dialog.Pulse()
last_tick = time.time()
process.poll()
wx.Yield()
time.sleep(0.1)
if dialog.WasCancelled():
def cancel():
process.terminate()
process.wait()
Thread(target=cancel).start()
dialog.Destroy()
wx.Yield()
Dialog.info(None, "Action aborted. It may take awhile for the machine to cancel its operation.")
sys.exit(1)
dialog.Destroy()
wx.Yield()
return process.returncode
# end of class MyFrame
if __name__ == "__main__":
unblock_inkscape = True
if any(('--block=true' in sys.argv, '--help' in sys.argv, '-h' in sys.argv, sys.platform.lower().startswith('win'))):
# windows doesn't support os.fork()
unblock_inkscape = False
pid = 0
if unblock_inkscape: pid = os.fork()
if pid != 0:
# We forked and this is the "parent", so just return
os._exit(0)
# Here we are in the process that will do the actual work:
if unblock_inkscape:
# Closing stdout and stderr allows inkscape to continue on
# while the silhouette machine is cutting. This is useful if you're
# cutting something really big and want to work on another document.
os.close(1)
os.close(2)
multilogfile = NamedTemporaryFile(
suffix='.log', prefix='silhouette-multiple-actions',
mode='w', delete=False)
else:
multilogfile = sys.stderr
try:
e = SilhouetteMulti()
e.run()
except Exception:
traceback.print_exc(file=multilogfile)
# SilhouetteMultiFrame.wrapup likely never called if there was
# an exception, so:
if unblock_inkscape:
show_log_as_dialog()
sys.exit(0)