def write(pattern: EmbPattern, file): with open(file, "wb") as f: extends = pattern.extends() width = extends[2] - extends[0] height = extends[3] - extends[1] name = pattern.name if name is None: name = "Untitled" f.write(bytes("LA:%-16s\r" % (name), 'utf8')) f.write(bytes("ST:%7d\r" % (pattern.count_stitches()), 'utf8')) f.write(bytes("CO:%3d\r" % (pattern.count_color_changes()), 'utf8')) x_extend = math.ceil(PPMM * width / 2) y_extend = math.ceil(PPMM * height / 2) f.write(bytes("+X:%5d\r" % (x_extend), 'utf8')) f.write(bytes("-X:%5d\r" % (x_extend), 'utf8')) f.write(bytes("+Y:%5d\r" % (y_extend), 'utf8')) f.write(bytes("-Y:%5d\r" % (y_extend), 'utf8')) f.write(bytes("AX:+%5d\r" % (0), 'utf8')) f.write(bytes("AY:+%5d\r" % (0), 'utf8')) f.write(bytes("MX:+%5d\r" % (0), 'utf8')) f.write(bytes("AY:+%5d\r" % (0), 'utf8')) f.write(bytes("PD:%6s\r" % ("******"), 'utf8')) if extended_header: if pattern.author is not None: f.write(bytes("AU:%s\r" % (pattern.author), 'utf8')) if pattern.copyright is not None: f.write(bytes("CP:%s\r" % (pattern.copyright), 'utf8')) if len(pattern.threadlist) > 0: for thread in pattern.threadlist: f.write( bytes( "TC:%s,%s,%s\r" % (thread.hex_color(), thread.description, thread.catalog_number), 'utf8')) f.write(b'\x1a') for i in range(f.tell(), DSTHEADERSIZE): f.write(b'\x20') # space stitches = pattern.stitches xx = 0 yy = 0 for stitch in stitches: x = stitch[0] y = stitch[1] data = stitch[2] dx = x - xx dy = y - yy if (data is EmbPattern.TRIM): f.write(encode_record(2, 2, EmbPattern.JUMP)) f.write(encode_record(-4, -4, EmbPattern.JUMP)) f.write(encode_record(2, 2, EmbPattern.JUMP)) else: f.write(encode_record(round(dx), round(dy), data)) xx = x yy = y
def process(self, p: EmbPattern) -> EmbPattern: copy = EmbPattern.EmbPattern() EmbPattern.set(p, copy) layer = copy.stitches for stitch in layer: stitch[0] = round(stitch[0] - self.translate_X) stitch[1] = round(stitch[1] - self.translate_Y) p.stitches = [] p.threadlist = [] self.write_code(copy, p) self.write_thread(copy, p) return p
def write_pec_stitches(pattern: EmbPattern, f): extends = pattern.extends() width = extends[2] - extends[0] height = extends[3] - extends[1] name = pattern.name if name is None: name = "Untitled" name = name[:8] f.write(bytes("LA:%-16s\r" % (name), 'utf8')) f.write(b'\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\xFF\x00\x06\x26') pattern.fix_color_count() thread_set = EmbThreadPec.getThreadSet() chart = [None] * len(thread_set) for thread in set(pattern.threadlist): index = thread.find_nearest_color_index(thread_set) thread_set[index] = None chart[index] = thread colorlist = [] for thread in pattern.threadlist: colorlist.append(thread.find_nearest_color_index(chart)) current_thread_count = len(colorlist) if current_thread_count is not 0: f.write(b'\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20') colorlist.insert(0, current_thread_count - 1) f.write(bytes(colorlist)) else: f.write(b'\x20\x20\x20\x20\x64\x20\x00\x20\x00\x20\x20\x20\xFF') for i in range(current_thread_count, 463): f.write(b'\x20') # 520 f.write(b'\x00\x00') stitch_encode = io.BytesIO() pec_encode(pattern, stitch_encode) graphics_offset_value = stitch_encode.tell() + 20 helper.write_int_24le(f, graphics_offset_value) f.write(b'\x31\xff\xf0') helper.write_int_16le(f, round(width)) helper.write_int_16le(f, round(height)) helper.write_int_16le(f, 0x1E0) helper.write_int_16le(f, 0x1B0) helper.write_int_16le(f, 0x9000 | -round(extends[0])) helper.write_int_16le(f, 0x9000 | -round(extends[1])) pec_encode(pattern, f) # shutil.copyfileobj(encodef, f) blank = PecGraphics.blank f.write(bytes(blank)) for i in range(0, current_thread_count): f.write(bytes(blank))
def process(self, p): self.needle_x = 0 self.needle_y = 0 copy = EmbPattern.EmbPattern() EmbPattern.set(p, copy) layer = copy.stitches for stitch in layer: stitch[0] = round(stitch[0] - self.translate_x) stitch[1] = round(stitch[1] - self.translate_y) p.stitches = [] p.threadlist = [] self.write_code(copy, p) self.write_thread(copy, p) return p
def eread(document: vp.Document, filename: str): # populate the vp_source[s] properties document.set_property(vp.METADATA_FIELD_SOURCE, pathlib.Path(filename).absolute()) document.add_to_sources(filename) pattern = EmbPattern(filename) for stitches, color in pattern.get_as_stitchblock(): if len(stitches) == 0: continue lc = vp.LineCollection() lc.scale(1.0 / _EMB_SCALE_FACTOR) stitch_block = np.asarray(stitches, dtype="float") stitch_block = stitch_block[..., 0] + 1j * stitch_block[..., 1] lc.append(stitch_block) lc.set_property(vp.METADATA_FIELD_COLOR, vp.Color(color.hex_color())) document.add(lc, with_metadata=True) return document
def __init__(self): self.pattern = EmbPattern.EmbPattern()
def ewrite(document: vp.Document, filename: str, version: str): pattern = EmbPattern() for layer in document.layers.values(): for p in layer: m = p * _EMB_SCALE_FACTOR for v in m: pattern.add_stitch_absolute(STITCH, int(v.real), int(v.imag)) pattern.add_command(SEQUENCE_BREAK) pattern.add_command(COLOR_BREAK) if version is not None: pattern.write(filename, version=version) else: pattern.write(filename) return document
# Initial test code. pyembroidery expReader.read("BN00883_A.EXP",reader0) dstReader.read("tree.dst",reader1) dstWriter.write(reader0.pattern,"bn-convert.dst") expWriter.write(reader1.pattern,"tree-covnert.exp") dstReader.read("sequin.dst",reader2) pesWriter.write(reader2.pattern,"sequin-convert.pes") #invents own embpattern pattern = EmbPattern.EmbPattern() pattern.add_stitch_absolute(0,0,EmbPattern.STITCH_NEW_COLOR) for x in range(1,10): for y in range(1,10): pattern.add_stitch_absolute(x*150,y*150,EmbPattern.STITCH) pattern.add_stitch_absolute(x*150, y*150, EmbPattern.STITCH_FINAL_COLOR) pattern.add_stitch_absolute(x*150, y*150, EmbPattern.STITCH_NEW_COLOR) pattern.add_stitch_absolute(0,0,EmbPattern.END) pattern = encoder.process(pattern) #invokes the encoder, to convert this to standard forms. pecWriter.write(pattern,"generated.pec") pesWriter.write(pattern,"generated.pes") expWriter.write(pattern,"generated.exp") dstWriter.write(pattern,"generated.dst") dstWriter.extended_header = True