kopia lustrzana https://github.com/inkstitch/inkstitch
switch to pyembroidery for file generation
rodzic
04ab1c4ae9
commit
dbcbf7cff1
|
@ -1,4 +1,4 @@
|
||||||
import libembroidery
|
import pyembroidery
|
||||||
import inkex
|
import inkex
|
||||||
import simpletransform
|
import simpletransform
|
||||||
import shapely.geometry as shgeo
|
import shapely.geometry as shgeo
|
||||||
|
@ -7,15 +7,6 @@ from .utils import Point
|
||||||
from .svg import PIXELS_PER_MM, get_doc_size, get_viewbox_transform
|
from .svg import PIXELS_PER_MM, get_doc_size, get_viewbox_transform
|
||||||
|
|
||||||
|
|
||||||
def make_thread(color):
|
|
||||||
thread = libembroidery.EmbThread()
|
|
||||||
thread.color = libembroidery.embColor_make(*color.rgb)
|
|
||||||
|
|
||||||
thread.description = color.name
|
|
||||||
thread.catalogNumber = ""
|
|
||||||
|
|
||||||
return thread
|
|
||||||
|
|
||||||
def add_thread(pattern, thread):
|
def add_thread(pattern, thread):
|
||||||
"""Add a thread to a pattern and return the thread's index"""
|
"""Add a thread to a pattern and return the thread's index"""
|
||||||
|
|
||||||
|
@ -23,20 +14,17 @@ def add_thread(pattern, thread):
|
||||||
|
|
||||||
return libembroidery.embThreadList_count(pattern.threadList) - 1
|
return libembroidery.embThreadList_count(pattern.threadList) - 1
|
||||||
|
|
||||||
def get_flags(stitch):
|
def get_command(stitch):
|
||||||
flags = 0
|
|
||||||
|
|
||||||
if stitch.jump:
|
if stitch.jump:
|
||||||
flags |= libembroidery.JUMP
|
return pyembroidery.JUMP
|
||||||
|
elif stitch.trim:
|
||||||
if stitch.trim:
|
return pyembroidery.TRIM
|
||||||
flags |= libembroidery.TRIM
|
elif stitch.color_change:
|
||||||
|
return pyembroidery.COLOR_CHANGE
|
||||||
if stitch.color_change:
|
elif stitch.stop:
|
||||||
flags |= libembroidery.STOP
|
return pyembroidery.STOP
|
||||||
|
else:
|
||||||
return flags
|
return pyembroidery.NEEDLE_AT
|
||||||
|
|
||||||
|
|
||||||
def _string_to_floats(string):
|
def _string_to_floats(string):
|
||||||
floats = string.split(',')
|
floats = string.split(',')
|
||||||
|
@ -102,27 +90,28 @@ def get_origin(svg):
|
||||||
def write_embroidery_file(file_path, stitch_plan, svg):
|
def write_embroidery_file(file_path, stitch_plan, svg):
|
||||||
origin = get_origin(svg)
|
origin = get_origin(svg)
|
||||||
|
|
||||||
pattern = libembroidery.embPattern_create()
|
pattern = pyembroidery.EmbPattern()
|
||||||
|
|
||||||
for color_block in stitch_plan:
|
for color_block in stitch_plan:
|
||||||
add_thread(pattern, make_thread(color_block.color))
|
pattern.add_thread(color_block.color.pyembroidery_thread)
|
||||||
|
|
||||||
for stitch in color_block:
|
for stitch in color_block:
|
||||||
if stitch.stop:
|
command = get_command(stitch)
|
||||||
# This is the start of the extra color block added by the
|
pattern.add_stitch_absolute(command, stitch.x, stitch.y)
|
||||||
# "STOP after" handler (see stitch_plan/stop.py). Assign it
|
|
||||||
# the same color.
|
|
||||||
add_thread(pattern, make_thread(color_block.color))
|
|
||||||
|
|
||||||
flags = get_flags(stitch)
|
pattern.add_stitch_absolute(pyembroidery.END, stitch.x, stitch.y)
|
||||||
libembroidery.embPattern_addStitchAbs(pattern, stitch.x - origin.x, stitch.y - origin.y, flags, 1)
|
|
||||||
|
|
||||||
libembroidery.embPattern_addStitchAbs(pattern, stitch.x - origin.x, stitch.y - origin.y, libembroidery.END, 1)
|
|
||||||
|
|
||||||
# convert from pixels to millimeters
|
# convert from pixels to millimeters
|
||||||
libembroidery.embPattern_scale(pattern, 1/PIXELS_PER_MM)
|
# also multiply by 10 to get tenths of a millimeter as required by pyembroidery
|
||||||
|
scale = 10 / PIXELS_PER_MM
|
||||||
|
|
||||||
# SVG and embroidery disagree on the direction of the Y axis
|
settings = {
|
||||||
libembroidery.embPattern_flipVertical(pattern)
|
# correct for the origin
|
||||||
|
"translate": -origin,
|
||||||
|
|
||||||
libembroidery.embPattern_write(pattern, file_path)
|
# convert from pixels to millimeters
|
||||||
|
# also multiply by 10 to get tenths of a millimeter as required by pyembroidery
|
||||||
|
"scale": (scale, scale)
|
||||||
|
}
|
||||||
|
|
||||||
|
pyembroidery.write(pattern, file_path, settings)
|
||||||
|
|
|
@ -38,6 +38,15 @@ class ThreadColor(object):
|
||||||
def to_hex_str(self):
|
def to_hex_str(self):
|
||||||
return "#%s" % self.hex_digits
|
return "#%s" % self.hex_digits
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pyembroidery_thread(self):
|
||||||
|
return {
|
||||||
|
"name": self.name,
|
||||||
|
"id": self.number,
|
||||||
|
"manufacturer": self.manufacturer,
|
||||||
|
"rgb": int(self.hex_digits, 16),
|
||||||
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hex_digits(self):
|
def hex_digits(self):
|
||||||
return "%02X%02X%02X" % self.rgb
|
return "%02X%02X%02X" % self.rgb
|
||||||
|
|
|
@ -65,6 +65,9 @@ class Point:
|
||||||
else:
|
else:
|
||||||
raise ValueError("cannot multiply Point by %s" % type(other))
|
raise ValueError("cannot multiply Point by %s" % type(other))
|
||||||
|
|
||||||
|
def __neg__(self):
|
||||||
|
return self * -1
|
||||||
|
|
||||||
def __rmul__(self, other):
|
def __rmul__(self, other):
|
||||||
if isinstance(other, (int, float)):
|
if isinstance(other, (int, float)):
|
||||||
return self.__mul__(other)
|
return self.__mul__(other)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
pyembroidery >=1.2.0, <1.3.0
|
pyembroidery >=1.2.12, <1.3.0
|
||||||
backports.functools_lru_cache
|
backports.functools_lru_cache
|
||||||
wxPython
|
wxPython
|
||||||
networkx
|
networkx
|
||||||
|
|
Ładowanie…
Reference in New Issue