def command_merge(self, values): for value in values: if not isinstance(value, tuple): continue pattern = value[0] v = self.v() input_files = glob.glob(v) for input_file in input_files: self.log("Merging:", input_file) pattern.stitches = pattern.stitches[0:-1] # remove flagged end. pyembroidery.read(input_file, pattern=pattern) return values
def read_file(self, filepath): if filepath is None: return pattern = pyembroidery.read(str(filepath)) if pattern is None: return pattern.extras["filename"] = filepath self.add_embroidery(pattern)
def generate_stitch_plan(embroidery_file, import_commands=True): # noqa: C901 validate_file_path(embroidery_file) pattern = pyembroidery.read(embroidery_file) stitch_plan = StitchPlan() color_block = None for raw_stitches, thread in pattern.get_as_colorblocks(): color_block = stitch_plan.new_color_block(thread) for x, y, command in raw_stitches: if command == pyembroidery.STITCH: color_block.add_stitch( Stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0)) if len(color_block) > 0: if not import_commands and command in [ pyembroidery.TRIM, pyembroidery.STOP ]: # Importing commands is not wanted: # start a new color block without inserting the command color_block = stitch_plan.new_color_block(thread) elif command == pyembroidery.TRIM: color_block.add_stitch(trim=True) elif command == pyembroidery.STOP: color_block.add_stitch(stop=True) color_block = stitch_plan.new_color_block(thread) stitch_plan.delete_empty_color_blocks() if stitch_plan.last_color_block: if stitch_plan.last_color_block.last_stitch: if stitch_plan.last_color_block.last_stitch.stop: # ending with a STOP command is redundant, so remove it del stitch_plan.last_color_block[-1] extents = stitch_plan.extents svg = inkex.SvgDocumentElement("svg", nsmap=inkex.NSS, attrib={ "width": str(extents[0] * 2), "height": str(extents[1] * 2), "viewBox": "0 0 %s %s" % (extents[0] * 2, extents[1] * 2), }) render_stitch_plan(svg, stitch_plan) # rename the Stitch Plan layer so that it doesn't get overwritten by Embroider layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") layer.set(INKSCAPE_LABEL, os.path.basename(embroidery_file)) layer.attrib.pop('id') # Shift the design so that its origin is at the center of the canvas # Note: this is NOT the same as centering the design in the canvas! layer.set('transform', 'translate(%s,%s)' % (extents[0], extents[1])) return svg
def command_input(self, values): v = self.v() input_files = glob.glob(v) patterns = [] for input_file in input_files: self.log("Loading:", input_file) emb_pattern = pyembroidery.read(input_file) if emb_pattern is None: continue patterns.append((emb_pattern, input_file, {})) return patterns
def test_read_generic(): pattern = pyembroidery.read("tests/formats/pes/Hopea.pes") pattern = pyembroidery.read("tests/formats/pec/Hopea.pec") pattern = pyembroidery.read("tests/formats/exp/Hopea.exp") pattern = pyembroidery.read("tests/formats/dst/Hopea.DST") pattern = pyembroidery.read("tests/formats/jef/Hopea.jef") pattern = pyembroidery.read("tests/formats/vp3/Hopea.vp3")
def affect(self, args): embroidery_file = args[0] pattern = pyembroidery.read(embroidery_file) stitch_plan = StitchPlan() color_block = None for raw_stitches, thread in pattern.get_as_colorblocks(): color_block = stitch_plan.new_color_block(thread) for x, y, command in raw_stitches: if command == pyembroidery.STITCH: color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0) if len(color_block) > 0: if command == pyembroidery.TRIM: color_block.add_stitch(trim=True) elif command == pyembroidery.STOP: color_block.add_stitch(stop=True) color_block = stitch_plan.new_color_block(thread) stitch_plan.delete_empty_color_blocks() if stitch_plan.last_color_block: if stitch_plan.last_color_block.last_stitch: if stitch_plan.last_color_block.last_stitch.stop: # ending with a STOP command is redundant, so remove it del stitch_plan.last_color_block[-1] extents = stitch_plan.extents svg = etree.Element("svg", nsmap=inkex.NSS, attrib={ "width": str(extents[0] * 2), "height": str(extents[1] * 2), "viewBox": "0 0 %s %s" % (extents[0] * 2, extents[1] * 2), }) render_stitch_plan(svg, stitch_plan) # rename the Stitch Plan layer so that it doesn't get overwritten by Embroider layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") layer.set(INKSCAPE_LABEL, os.path.basename(embroidery_file.decode("UTF-8"))) layer.attrib.pop('id') # Shift the design so that its origin is at the center of the canvas # Note: this is NOT the same as centering the design in the canvas! layer.set('transform', 'translate(%s,%s)' % (extents[0], extents[1])) print etree.tostring(svg)
def load(self, filename): self.Update() self.emb_pattern = pyembroidery.read(filename) for pos, stitch in enumerate(self.emb_pattern.stitches): command = stitch[2] if command != pyembroidery.END: continue self.emb_pattern.stitches = self.emb_pattern.stitches[0:pos] break self.emb_pattern.get_as_command_blocks() self.name = filename self.selected_point = None self.drag_point = None self.update_affines() self.update_drawing()
def stitch_plan_from_file(embroidery_file): """Read a machine embroidery file in any supported format and return a stitch plan.""" pattern = pyembroidery.read(embroidery_file) stitch_plan = StitchPlan() color_block = None for raw_stitches, thread in pattern.get_as_colorblocks(): color_block = stitch_plan.new_color_block(thread) for x, y, command in raw_stitches: color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0, jump=(command == pyembroidery.JUMP), trim=(command == pyembroidery.TRIM)) return stitch_plan
def RunScript(self, FilePath): # Initialize output Pattern = Grasshopper.DataTree[object]() # only do something if input is defined if FilePath != None: try: Pattern = pyembroidery.read( path.normpath(FilePath.strip("\n\r"))) except Exception as e: rml = self.RuntimeMessageLevel.Error errMsg = ("Could not read embroidery file!" + "Please check if format is readable.") self.AddRuntimeMessage(rml, errMsg) else: rml = self.RuntimeMessageLevel.Warning errMsg = ("Input FilePath failed to collect data!") self.AddRuntimeMessage(rml, errMsg) # return outputs if you have them; here I try it for you: return Pattern
def affect(self, args): embroidery_file = args[0] pattern = pyembroidery.read(embroidery_file) stitch_plan = StitchPlan() color_block = None for raw_stitches, thread in pattern.get_as_colorblocks(): color_block = stitch_plan.new_color_block(thread) for x, y, command in raw_stitches: color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0, jump=(command == pyembroidery.JUMP), trim=(command == pyembroidery.TRIM)) extents = stitch_plan.extents svg = etree.Element("svg", nsmap=inkex.NSS, attrib={ "width": str(extents[0] * 2), "height": str(extents[1] * 2), "viewBox": "0 0 %s %s" % (extents[0] * 2, extents[1] * 2), }) render_stitch_plan(svg, stitch_plan) # rename the Stitch Plan layer so that it doesn't get overwritten by Embroider layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']") layer.set(INKSCAPE_LABEL, os.path.basename(embroidery_file)) layer.attrib.pop('id') # Shift the design so that its origin is at the center of the canvas # Note: this is NOT the same as centering the design in the canvas! layer.set('transform', 'translate(%s,%s)' % (extents[0], extents[1])) print etree.tostring(svg)
import sys from pyembroidery import read, write if len(sys.argv) <= 1: print("No command arguments") exit(1) input = sys.argv[1] if len(sys.argv) >= 3: output = sys.argv[2] else: output = sys.argv[1] + ".csv" pattern = read(input) write = write(pattern, output)
#DEBUG lines: [[[Line(0+0j, 300+0j), Line(300+0j, 300+300j), Line(300+300j, 0+300j) , Line(0+300j, 0+0j)]]] PECCommands = createPECStitchRoutines(subshapeLineGroups, fillColors, threadWidth, maxStitchDistance=maxStitchDistance) left, right, bottom, top = shape.bbox() pec = PEC(label="simple", colors=PECColors, commands=PECCommands, size=complex(right - left, top - bottom)) # Render the PEC commands renderPEC(pec) pes = PES(PEC=pec, shape=shape) encodedPES = pes.encode() with open(args.outputFile, "w") as f: f.write(encodedPES) print("Wrote {} to disk.".format(args.outputFile)) if args.debugRendering: loadedPES = pyembroidery.read(args.outputFile) if loadedPES is not None: print("Generating debug image.") debugImagePath = replaceFilenameAndExtensionFromPath(args.outputFile, "debugPicture" + getFilenameAndExtensionFromPath(args.outputFile)[0], "png") pyembroidery.write_png(loadedPES, debugImagePath) print("Image written to disk: {}".format(debugImagePath)) else: print("Couldn't find output file.") # Show what the program did in the debug window # until the user quits. GenericRenderer.globalRenderer.runLoop()