2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2018-06-13 02:15:32 +00:00
|
|
|
import os
|
2020-04-01 16:50:14 +00:00
|
|
|
import sys
|
2018-06-13 02:15:32 +00:00
|
|
|
import tempfile
|
2021-03-04 17:40:53 +00:00
|
|
|
from copy import deepcopy
|
2018-06-13 02:15:32 +00:00
|
|
|
from zipfile import ZipFile
|
2020-04-01 16:50:14 +00:00
|
|
|
|
2021-03-04 17:40:53 +00:00
|
|
|
from lxml import etree
|
|
|
|
|
2018-07-18 01:43:59 +00:00
|
|
|
import pyembroidery
|
2022-06-24 15:11:52 +00:00
|
|
|
from inkex import Boolean
|
2018-06-13 02:15:32 +00:00
|
|
|
|
|
|
|
from ..i18n import _
|
|
|
|
from ..output import write_embroidery_file
|
2021-08-07 16:18:55 +00:00
|
|
|
from ..stitch_plan import stitch_groups_to_stitch_plan
|
2020-04-20 18:53:39 +00:00
|
|
|
from ..threads import ThreadCatalog
|
2021-03-04 17:40:53 +00:00
|
|
|
from .base import InkstitchExtension
|
2018-06-13 02:15:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Zip(InkstitchExtension):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
InkstitchExtension.__init__(self)
|
|
|
|
|
|
|
|
# it's kind of obnoxious that I have to do this...
|
|
|
|
self.formats = []
|
2018-07-18 01:43:59 +00:00
|
|
|
for format in pyembroidery.supported_formats():
|
2022-06-24 15:11:52 +00:00
|
|
|
if 'writer' in format and format['category'] in ['embroidery', 'color', 'image', 'stitch']:
|
2018-07-18 01:43:59 +00:00
|
|
|
extension = format['extension']
|
2021-03-04 17:40:53 +00:00
|
|
|
self.arg_parser.add_argument('--format-%s' % extension, type=Boolean, dest=extension)
|
2018-06-13 02:15:32 +00:00
|
|
|
self.formats.append(extension)
|
2021-03-04 17:40:53 +00:00
|
|
|
self.arg_parser.add_argument('--format-svg', type=Boolean, dest='svg')
|
|
|
|
self.arg_parser.add_argument('--format-threadlist', type=Boolean, dest='threadlist')
|
2020-04-01 16:50:14 +00:00
|
|
|
self.formats.append('svg')
|
2020-04-20 18:53:39 +00:00
|
|
|
self.formats.append('threadlist')
|
2018-06-13 02:15:32 +00:00
|
|
|
|
|
|
|
def effect(self):
|
|
|
|
if not self.get_elements():
|
|
|
|
return
|
|
|
|
|
2021-03-04 17:40:53 +00:00
|
|
|
self.metadata = self.get_inkstitch_metadata()
|
|
|
|
collapse_len = self.metadata['collapse_len_mm']
|
2022-06-22 13:22:34 +00:00
|
|
|
min_stitch_len = self.metadata['min_stitch_len_mm']
|
2021-08-07 16:37:17 +00:00
|
|
|
patches = self.elements_to_stitch_groups(self.elements)
|
2022-06-22 13:22:34 +00:00
|
|
|
stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, min_stitch_len=min_stitch_len)
|
2018-06-13 02:15:32 +00:00
|
|
|
|
|
|
|
base_file_name = self.get_base_file_name()
|
|
|
|
path = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
files = []
|
|
|
|
|
|
|
|
for format in self.formats:
|
|
|
|
if getattr(self.options, format):
|
|
|
|
output_file = os.path.join(path, "%s.%s" % (base_file_name, format))
|
2020-04-20 18:53:39 +00:00
|
|
|
if format == 'svg':
|
2021-03-04 17:40:53 +00:00
|
|
|
document = deepcopy(self.document.getroot())
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as svg:
|
|
|
|
svg.write(etree.tostring(document).decode('utf-8'))
|
|
|
|
elif format == 'threadlist':
|
2020-04-20 18:53:39 +00:00
|
|
|
output_file = os.path.join(path, "%s_%s.txt" % (base_file_name, _("threadlist")))
|
2021-03-27 15:55:08 +00:00
|
|
|
output = open(output_file, 'w', encoding='utf-8')
|
2020-04-20 18:53:39 +00:00
|
|
|
output.write(self.get_threadlist(stitch_plan, base_file_name))
|
|
|
|
output.close()
|
|
|
|
else:
|
|
|
|
write_embroidery_file(output_file, stitch_plan, self.document.getroot())
|
2018-06-13 02:15:32 +00:00
|
|
|
files.append(output_file)
|
|
|
|
|
|
|
|
if not files:
|
|
|
|
self.errormsg(_("No embroidery file formats selected."))
|
|
|
|
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(suffix=".zip", delete=False)
|
|
|
|
|
|
|
|
# in windows, failure to close here will keep the file locked
|
|
|
|
temp_file.close()
|
|
|
|
|
|
|
|
with ZipFile(temp_file.name, "w") as zip_file:
|
|
|
|
for file in files:
|
2018-06-13 16:53:05 +00:00
|
|
|
zip_file.write(file, os.path.basename(file))
|
2018-06-13 02:15:32 +00:00
|
|
|
|
|
|
|
# inkscape will read the file contents from stdout and copy
|
|
|
|
# to the destination file that the user chose
|
2021-03-04 17:40:53 +00:00
|
|
|
with open(temp_file.name, 'rb') as output_file:
|
|
|
|
sys.stdout.buffer.write(output_file.read())
|
2018-06-13 02:15:32 +00:00
|
|
|
|
|
|
|
os.remove(temp_file.name)
|
|
|
|
for file in files:
|
|
|
|
os.remove(file)
|
|
|
|
os.rmdir(path)
|
|
|
|
|
|
|
|
# don't let inkex output the SVG!
|
|
|
|
sys.exit(0)
|
2020-04-20 18:53:39 +00:00
|
|
|
|
|
|
|
def get_threadlist(self, stitch_plan, design_name):
|
|
|
|
ThreadCatalog().match_and_apply_palette(stitch_plan, self.get_inkstitch_metadata()['thread-palette'])
|
|
|
|
thread_used = []
|
|
|
|
|
|
|
|
thread_output = "%s\n" % _("Design Details")
|
|
|
|
thread_output += "==============\n\n"
|
|
|
|
|
|
|
|
thread_output += "%s: %s\n" % (_("Title"), design_name)
|
|
|
|
thread_output += "%s (mm): %.2f x %.2f\n" % (_("Size"), stitch_plan.dimensions_mm[0], stitch_plan.dimensions_mm[1])
|
|
|
|
thread_output += "%s: %s\n" % (_("Stitches"), stitch_plan.num_stitches)
|
|
|
|
thread_output += "%s: %s\n\n" % (_("Colors"), stitch_plan.num_colors)
|
|
|
|
|
|
|
|
thread_output += "%s\n" % _("Thread Order")
|
|
|
|
thread_output += "============\n\n"
|
|
|
|
|
|
|
|
for i, color_block in enumerate(stitch_plan):
|
|
|
|
thread = color_block.color
|
|
|
|
|
|
|
|
thread_output += str(i + 1) + " "
|
|
|
|
string = "%s #%s - %s (#%s)" % (thread.name, thread.number, thread.manufacturer, thread.hex_digits.lower())
|
|
|
|
thread_output += string + "\n"
|
|
|
|
|
|
|
|
thread_used.append(string)
|
|
|
|
|
|
|
|
thread_output += "\n"
|
|
|
|
thread_output += _("Thread Used") + "\n"
|
|
|
|
thread_output += "============" + "\n\n"
|
|
|
|
|
|
|
|
for thread in set(thread_used):
|
|
|
|
thread_output += thread + "\n"
|
|
|
|
|
|
|
|
return "%s" % thread_output
|