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.
|
|
|
|
|
2023-02-03 05:27:16 +00:00
|
|
|
import os
|
2024-06-12 10:25:52 +00:00
|
|
|
import re
|
2018-12-23 03:12:23 +00:00
|
|
|
import sys
|
|
|
|
|
2022-06-24 15:11:52 +00:00
|
|
|
import inkex
|
2024-06-12 10:25:52 +00:00
|
|
|
from pyembroidery.exceptions import TooManyColorChangesError
|
|
|
|
|
2019-11-14 06:15:38 +00:00
|
|
|
import pyembroidery
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-12-23 03:12:23 +00:00
|
|
|
from .commands import global_command
|
2018-09-02 00:11:44 +00:00
|
|
|
from .i18n import _
|
2018-12-31 00:59:07 +00:00
|
|
|
from .stitch_plan import Stitch
|
2019-11-14 06:15:38 +00:00
|
|
|
from .svg import PIXELS_PER_MM
|
2018-12-23 03:12:23 +00:00
|
|
|
from .utils import Point
|
2018-05-02 01:21:07 +00:00
|
|
|
|
|
|
|
|
2018-07-16 00:15:35 +00:00
|
|
|
def get_command(stitch):
|
2018-05-02 01:21:07 +00:00
|
|
|
if stitch.jump:
|
2018-07-16 00:15:35 +00:00
|
|
|
return pyembroidery.JUMP
|
|
|
|
elif stitch.trim:
|
|
|
|
return pyembroidery.TRIM
|
|
|
|
elif stitch.color_change:
|
|
|
|
return pyembroidery.COLOR_CHANGE
|
|
|
|
elif stitch.stop:
|
|
|
|
return pyembroidery.STOP
|
|
|
|
else:
|
|
|
|
return pyembroidery.NEEDLE_AT
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
|
2018-05-02 01:21:07 +00:00
|
|
|
def _string_to_floats(string):
|
|
|
|
floats = string.split(',')
|
|
|
|
return [float(num) for num in floats]
|
|
|
|
|
|
|
|
|
2022-06-24 15:11:52 +00:00
|
|
|
def get_origin(svg, bounding_box):
|
|
|
|
(minx, miny, maxx, maxy) = bounding_box
|
2018-08-23 02:48:40 +00:00
|
|
|
origin_command = global_command(svg, "origin")
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-08-23 02:48:40 +00:00
|
|
|
if origin_command:
|
|
|
|
return origin_command.point
|
2018-08-23 02:13:51 +00:00
|
|
|
else:
|
2019-11-14 06:15:38 +00:00
|
|
|
bounding_box_center = [(maxx+minx)/2, (maxy+miny)/2]
|
|
|
|
default = Point(*bounding_box_center)
|
2018-05-02 01:21:07 +00:00
|
|
|
return default
|
|
|
|
|
|
|
|
|
2018-08-23 02:48:40 +00:00
|
|
|
def jump_to_stop_point(pattern, svg):
|
|
|
|
stop_position = global_command(svg, "stop_position")
|
|
|
|
if stop_position:
|
|
|
|
pattern.add_stitch_absolute(pyembroidery.JUMP, stop_position.point.x, stop_position.point.y)
|
|
|
|
|
|
|
|
|
2018-10-20 23:50:39 +00:00
|
|
|
def write_embroidery_file(file_path, stitch_plan, svg, settings={}):
|
2022-06-24 15:11:52 +00:00
|
|
|
# convert from pixels to millimeters
|
|
|
|
# also multiply by 10 to get tenths of a millimeter as required by pyembroidery
|
|
|
|
scale = 10 / PIXELS_PER_MM
|
|
|
|
|
2019-11-14 06:15:38 +00:00
|
|
|
origin = get_origin(svg, stitch_plan.bounding_box)
|
2022-07-08 05:33:45 +00:00
|
|
|
# origin = origin * scale
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-07-16 00:15:35 +00:00
|
|
|
pattern = pyembroidery.EmbPattern()
|
2023-02-03 05:27:16 +00:00
|
|
|
|
|
|
|
# For later use when writing .dst header title field.
|
2023-02-16 18:32:37 +00:00
|
|
|
pattern.extras['name'] = os.path.splitext(svg.name)[0]
|
2023-02-03 05:27:16 +00:00
|
|
|
|
2018-12-31 00:59:07 +00:00
|
|
|
stitch = Stitch(0, 0)
|
2018-05-02 01:21:07 +00:00
|
|
|
|
|
|
|
for color_block in stitch_plan:
|
2018-07-16 00:15:35 +00:00
|
|
|
pattern.add_thread(color_block.color.pyembroidery_thread)
|
2018-05-02 01:21:07 +00:00
|
|
|
|
|
|
|
for stitch in color_block:
|
2018-08-23 02:48:40 +00:00
|
|
|
if stitch.stop:
|
|
|
|
jump_to_stop_point(pattern, svg)
|
2018-07-16 00:15:35 +00:00
|
|
|
command = get_command(stitch)
|
|
|
|
pattern.add_stitch_absolute(command, stitch.x, stitch.y)
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-07-16 00:15:35 +00:00
|
|
|
pattern.add_stitch_absolute(pyembroidery.END, stitch.x, stitch.y)
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-10-20 23:50:39 +00:00
|
|
|
settings.update({
|
2018-08-22 00:32:50 +00:00
|
|
|
# correct for the origin
|
|
|
|
"translate": -origin,
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
# convert from pixels to millimeters
|
|
|
|
# also multiply by 10 to get tenths of a millimeter as required by pyembroidery
|
|
|
|
"scale": (scale, scale),
|
2018-07-16 02:53:18 +00:00
|
|
|
|
2018-08-22 00:32:50 +00:00
|
|
|
# This forces a jump at the start of the design and after each trim,
|
|
|
|
# even if we're close enough not to need one.
|
|
|
|
"full_jump": True,
|
2018-10-20 23:50:39 +00:00
|
|
|
})
|
2018-05-02 01:21:07 +00:00
|
|
|
|
2022-07-08 05:33:45 +00:00
|
|
|
if not file_path.endswith(('.col', '.edr', '.inf')):
|
|
|
|
settings['encode'] = True
|
|
|
|
|
2018-08-27 19:36:54 +00:00
|
|
|
if file_path.endswith('.csv'):
|
|
|
|
# Special treatment for CSV: instruct pyembroidery not to do any post-
|
|
|
|
# processing. This will allow the user to match up stitch numbers seen
|
|
|
|
# in the simulator with commands in the CSV.
|
|
|
|
settings['max_stitch'] = float('inf')
|
|
|
|
settings['max_jump'] = float('inf')
|
|
|
|
settings['explicit_trim'] = False
|
2022-06-24 15:11:52 +00:00
|
|
|
elif file_path.endswith('.png'):
|
|
|
|
settings['linewidth'] = 1
|
|
|
|
settings['background'] = 'white'
|
2018-08-27 19:36:54 +00:00
|
|
|
|
2018-08-25 01:44:42 +00:00
|
|
|
try:
|
2019-08-04 03:20:55 +00:00
|
|
|
pyembroidery.write(pattern, file_path, settings)
|
2018-08-25 01:44:42 +00:00
|
|
|
except IOError as e:
|
|
|
|
# L10N low-level file error. %(error)s is (hopefully?) translated by
|
|
|
|
# the user's system automatically.
|
2021-03-04 17:40:53 +00:00
|
|
|
msg = _("Error writing to %(path)s: %(error)s") % dict(path=file_path, error=e.strerror)
|
|
|
|
inkex.errormsg(msg)
|
2018-08-25 01:44:42 +00:00
|
|
|
sys.exit(1)
|
2024-06-12 10:25:52 +00:00
|
|
|
except TooManyColorChangesError as e:
|
|
|
|
num_color_changes = re.search("d+", str(e)).group()
|
|
|
|
msg = _("Couldn't save embrodiery file.")
|
|
|
|
msg += '\n\n'
|
|
|
|
msg += _("There are {num_color_changes} in your design. This is way too many.").format(num_color_changes=num_color_changes)
|
|
|
|
msg += '\n'
|
|
|
|
msg += _("Please reduce color changes. Find more information on our website:")
|
|
|
|
msg += '\n\n'
|
|
|
|
msg += _("http://inkstitch.org/docs/faq/#too-many-color-changes")
|
|
|
|
inkex.errormsg(msg)
|
|
|
|
sys.exit(1)
|