inkstitch/lib/extensions/output.py

51 wiersze
1.8 KiB
Python
Czysty Zwykły widok Historia

2018-06-13 01:28:02 +00:00
import sys
import os
import tempfile
from .base import InkstitchExtension
from ..output import write_embroidery_file
from ..stitch_plan import patches_to_stitch_plan
2018-08-22 01:43:09 +00:00
from ..svg import PIXELS_PER_MM
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):
InkstitchExtension.__init__(self)
self.OptionParser.add_option("-c", "--collapse_len_mm",
action="store", type="float",
dest="collapse_length_mm", default=3.0,
help="max collapse length (mm)")
self.OptionParser.add_option("-f", "--format",
dest="file_extension",
help="file extension to output (example: DST)")
def effect(self):
if not self.get_elements():
return
patches = self.elements_to_patches(self.elements)
stitch_plan = patches_to_stitch_plan(patches, self.options.collapse_length_mm * PIXELS_PER_MM)
temp_file = tempfile.NamedTemporaryFile(suffix=".%s" % self.options.file_extension, delete=False)
# in windows, failure to close here will keep the file locked
temp_file.close()
write_embroidery_file(temp_file.name, stitch_plan, self.document.getroot())
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:
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)