diff --git a/PyEmb.py b/PyEmb.py index e16f8f48d..441e965b8 100644 --- a/PyEmb.py +++ b/PyEmb.py @@ -123,6 +123,12 @@ def get_flags(stitch): return flags def write_embroidery_file(file_path, stitches): + # Embroidery machines don't care about our canvas size, so we relocate the + # design to the origin. It might make sense to center it about the origin + # instead. + min_x = min(stitch.x for stitch in stitches) + min_y = min(stitch.y for stitch in stitches) + pattern = libembroidery.embPattern_create() threads = {} @@ -141,12 +147,18 @@ def write_embroidery_file(file_path, stitches): last_color = stitch.color flags = get_flags(stitch) - libembroidery.embPattern_addStitchAbs(pattern, stitch.x, -stitch.y, flags, 0) + libembroidery.embPattern_addStitchAbs(pattern, stitch.x - min_x, stitch.y - min_y, flags, 0) if flags & libembroidery.JUMP: # I'm not sure this is right, but this is how the old version did it. - libembroidery.embPattern_addStitchAbs(pattern, stitch.x, -stitch.y, flags & ~libembroidery.JUMP, 0) + libembroidery.embPattern_addStitchAbs(pattern, stitch.x - min_x, stitch.y - min_y, flags & ~libembroidery.JUMP, 0) - libembroidery.embPattern_addStitchAbs(pattern, stitch.x, -stitch.y, libembroidery.END, 0) + libembroidery.embPattern_addStitchAbs(pattern, stitch.x - min_x, stitch.y - min_y, libembroidery.END, 0) + + # convert from pixels to millimeters libembroidery.embPattern_scale(pattern, 1/PIXELS_PER_MM) + + # SVG and embroidery disagree on the direction of the Y axis + libembroidery.embPattern_flipVertical(pattern) + libembroidery.embPattern_write(pattern, file_path)