2018-06-13 01:28:02 +00:00
|
|
|
import os
|
2018-10-20 23:50:39 +00:00
|
|
|
import sys
|
2018-06-13 01:28:02 +00:00
|
|
|
import tempfile
|
|
|
|
|
|
|
|
from ..output import write_embroidery_file
|
|
|
|
from ..stitch_plan import patches_to_stitch_plan
|
2018-10-20 23:50:39 +00:00
|
|
|
from .base import InkstitchExtension
|
2018-06-13 01:28:02 +00:00
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2018-06-13 01:28:02 +00:00
|
|
|
class Output(InkstitchExtension):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2018-10-20 23:50:39 +00:00
|
|
|
InkstitchExtension.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
def getoptions(self, args=sys.argv[1:]):
|
|
|
|
# inkex's option parsing can't handle arbitrary command line arguments
|
|
|
|
# that may be passed for a given output format, so we'll just parse the
|
|
|
|
# args ourselves. :P
|
|
|
|
self.settings = {}
|
|
|
|
extra_args = []
|
|
|
|
for arg in args:
|
|
|
|
if arg.startswith('--') and not arg.startswith('--id='):
|
|
|
|
name, value = arg[2:].split('=')
|
|
|
|
|
|
|
|
try:
|
|
|
|
value = float(value)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
value = {
|
|
|
|
"true": True,
|
|
|
|
"false": False
|
|
|
|
}[value]
|
|
|
|
except (KeyError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.settings[name] = value
|
|
|
|
else:
|
|
|
|
extra_args.append(arg)
|
|
|
|
|
|
|
|
self.file_extension = self.settings.pop('format')
|
|
|
|
del sys.argv[1:]
|
|
|
|
|
|
|
|
InkstitchExtension.getoptions(self, extra_args)
|
2018-06-13 01:28:02 +00:00
|
|
|
|
|
|
|
def effect(self):
|
|
|
|
if not self.get_elements():
|
|
|
|
return
|
|
|
|
|
|
|
|
patches = self.elements_to_patches(self.elements)
|
2018-10-20 23:50:39 +00:00
|
|
|
stitch_plan = patches_to_stitch_plan(patches)
|
2018-06-13 01:28:02 +00:00
|
|
|
|
2018-10-20 23:50:39 +00:00
|
|
|
temp_file = tempfile.NamedTemporaryFile(suffix=".%s" % self.file_extension, delete=False)
|
2018-06-13 01:28:02 +00:00
|
|
|
|
|
|
|
# in windows, failure to close here will keep the file locked
|
|
|
|
temp_file.close()
|
|
|
|
|
2018-10-20 23:50:39 +00:00
|
|
|
write_embroidery_file(temp_file.name, stitch_plan, self.document.getroot(), self.settings)
|
2018-06-13 01:28:02 +00:00
|
|
|
|
2018-08-03 02:05:42 +00:00
|
|
|
if sys.platform == "win32":
|
|
|
|
import msvcrt
|
|
|
|
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
|
|
|
|
|
2018-06-13 01:28:02 +00:00
|
|
|
# inkscape will read the file contents from stdout and copy
|
|
|
|
# to the destination file that the user chose
|
2018-08-04 02:11:13 +00:00
|
|
|
with open(temp_file.name, "rb") as output_file:
|
2018-07-18 01:34:08 +00:00
|
|
|
sys.stdout.write(output_file.read())
|
2018-08-04 01:50:54 +00:00
|
|
|
sys.stdout.flush()
|
2018-06-13 01:28:02 +00:00
|
|
|
|
|
|
|
# clean up the temp file
|
|
|
|
os.remove(temp_file.name)
|
|
|
|
|
|
|
|
# don't let inkex output the SVG!
|
|
|
|
sys.exit(0)
|