def command_output(self, values): out_path = [] v = self.v() while v is not None: out_path.append(v) v = self.v() for value in values: if not isinstance(value, tuple): continue pattern = value[0] filename = value[1] settings = value[2] for path in out_path: path = formatted_string(path, pattern, filename) ext_split = os.path.splitext(path) name = ext_split[0] ext = ext_split[1] if '*' in name: filename_suffix = os.path.split(filename)[1] name = name.replace('*', filename_suffix) if ext == "" or ext == ".*": for file_type in pyembroidery.supported_formats(): if 'writer' in file_type: out_file = name + '.' + file_type['extension'] self.log("Saving:", out_file) pyembroidery.write_embroidery(file_type['writer'], pattern, out_file, settings) else: self.log("Saving:", name + ext) pyembroidery.write(pattern, name + ext, settings) return []
def write_embroidery_file(file_path, stitch_plan, svg): origin = get_origin(svg) pattern = pyembroidery.EmbPattern() for color_block in stitch_plan: pattern.add_thread(color_block.color.pyembroidery_thread) for stitch in color_block: command = get_command(stitch) pattern.add_stitch_absolute(command, stitch.x, stitch.y) pattern.add_stitch_absolute(pyembroidery.END, stitch.x, stitch.y) # convert from pixels to millimeters # also multiply by 10 to get tenths of a millimeter as required by pyembroidery scale = 10 / PIXELS_PER_MM settings = { # correct for the origin "translate": -origin, # convert from pixels to millimeters # also multiply by 10 to get tenths of a millimeter as required by pyembroidery "scale": (scale, scale), # 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, } pyembroidery.write(pattern, file_path, settings)
def on_menu_export(self, event): page = self.main_notebook.GetCurrentPage() if not isinstance(page, EmbroideryView) or page.emb_pattern is None: return files = "" for format in pyembroidery.supported_formats(): try: if format["writer"] is not None: files += format["description"] + "(*." + format[ "extension"] + ")|*." + format["extension"] + "|" except KeyError: pass with wx.FileDialog(self, "Save Embroidery", wildcard=files[:-1], style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog: if fileDialog.ShowModal() == wx.ID_CANCEL: return # the user changed their mind # save the current contents in the file pathname = fileDialog.GetPath() pyembroidery.write(page.emb_pattern, str(pathname))
def on_menu_save(self, event): page = self.main_notebook.GetCurrentPage() if not isinstance(page, EmbroideryView) or page.emb_pattern is None: return path = page.emb_pattern.extras["filename"] if path is None: return pyembroidery.write(page.emb_pattern, str(path))
def write_embroidery_file(file_path, stitch_plan, svg, settings={}): origin = get_origin(svg, stitch_plan.bounding_box) pattern = pyembroidery.EmbPattern() stitch = Stitch(0, 0) for color_block in stitch_plan: pattern.add_thread(color_block.color.pyembroidery_thread) for stitch in color_block: if stitch.stop: jump_to_stop_point(pattern, svg) command = get_command(stitch) pattern.add_stitch_absolute(command, stitch.x, stitch.y) pattern.add_stitch_absolute(pyembroidery.END, stitch.x, stitch.y) # convert from pixels to millimeters # also multiply by 10 to get tenths of a millimeter as required by pyembroidery scale = 10 / PIXELS_PER_MM settings.update({ # correct for the origin "translate": -origin, # convert from pixels to millimeters # also multiply by 10 to get tenths of a millimeter as required by pyembroidery "scale": (scale, scale), # 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, }) 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 try: pyembroidery.write(pattern, file_path, settings) except IOError as e: # L10N low-level file error. %(error)s is (hopefully?) translated by # the user's system automatically. msg = _("Error writing to %(path)s: %(error)s") % dict( path=file_path, error=e.strerror) inkex.errormsg(msg) sys.exit(1)
def on_menu_save_as(self, event): page = self.main_notebook.GetCurrentPage() if not isinstance(page, EmbroideryView) or page.emb_pattern is None: return files = "Comma-separated values, csv (*.csv)|*.csv" with wx.FileDialog(self, "Save Embroidery", wildcard=files, style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog: if fileDialog.ShowModal() == wx.ID_CANCEL: return # the user changed their mind # save the current contents in the file pathname = fileDialog.GetPath() pyembroidery.write(page.emb_pattern, str(pathname)) page.emb_pattern.extras["filename"] = pathname
def on_menu_export(self, event): files = "" for format in pyembroidery.supported_formats(): try: if format["writer"] is not None and format[ "category"] == "stitch": files += format["description"] + "(*." + format[ "extension"] + ")|*." + format["extension"] + "|" except KeyError: pass with wx.FileDialog(self, "Save Stitch", wildcard=files[:-1], style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog: if fileDialog.ShowModal() == wx.ID_CANCEL: return # the user changed their mind # save the current contents in the file pathname = fileDialog.GetPath() pyembroidery.write(self.stitch_panel.emb_pattern, str(pathname))
def test_simple(): pattern = pyembroidery.EmbPattern() pattern.add_thread({ "rgb": 0x0000FF, "name": "Blue Test", "catalog": "0033", "brand": "PyEmbroidery Brand Thread" }) pattern.add_thread({ "rgb": 0x00FF00, "name": "Green", "catalog": "0034", "brand": "PyEmbroidery Brand Thread" }) test_fractals.generate(pattern) settings = { "tie_on": True, "tie_off": True } temp_dir = "temp" if not os.path.isdir(temp_dir): os.mkdir(temp_dir) pyembroidery.write(pattern, temp_dir + "/generated.u01", settings) pyembroidery.write(pattern, temp_dir + "/generated.pec", settings) pyembroidery.write(pattern, temp_dir + "/generated.pes", settings) pyembroidery.write(pattern, temp_dir + "/generated.exp", settings) pyembroidery.write(pattern, temp_dir + "/generated.dst", settings) settings["extended header"] = True pyembroidery.write(pattern, temp_dir + "/generated-eh.dst", settings) pyembroidery.write(pattern, temp_dir + "/generated.jef", settings) pyembroidery.write(pattern, temp_dir + "/generated.vp3", settings) settings["pes version"] = 1, pyembroidery.write(pattern, temp_dir + "/generatedv1.pes", settings) settings["truncated"] = True pyembroidery.write(pattern, temp_dir + "/generatedv1t.pes", settings) settings["pes version"] = 6, pyembroidery.write(pattern, temp_dir + "/generatedv6t.pes", settings) pyembroidery.convert(temp_dir + "/generated.exp", temp_dir + "/genconvert.dst", {"stable": False, "encode": False}) shutil.rmtree(temp_dir)
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)
import pyembroidery as emb def h_straight(pattern, x, y, dx, nx): pattern.add_stitch_absolute(emb.JUMP,x,y) for x in range(x,x_nx,dx): pattern.add_stitch_absolute(emb.STITCH, x,y) if x != x+nx: pattern.add_stitch_absolute(emb.STITCH, x+nx,y) pattern.add_command(emb.STOP) def v_straight(pattern, x, y, dy, ny): pattern.add_stitch_absolute(emb.JUMP,x,y) for y in range(y,y+ny,dy): pattern.add_stitch_absolute(emb.STITCH, x,y) if y != y+ny: pattern.add_stitch_absolute(emb.STITCH, x,y+ny) pattern.add_command(emb.STOP) def squares(pattern,del): for inset in range(10,50): square(inset, inset) pattern.add_command(emb.END) return pattern pattern = squares() emb.write(pattern, "squares.pes")
def RunScript(self, Pattern, FilePath, Execute): # check supported formats supported = [d["extension"] for d in pyembroidery.supported_formats()] supported = ["." + ext for ext in supported] supported = {ext: True for ext in supported} # verify pattern and filepaths if Pattern and FilePath: # make info message if len(Pattern) > len(FilePath): rml = self.RuntimeMessageLevel.Remark imsg = ("List of supplied filepaths does not suffice. " + "Additional filepaths will be constructed based on " + "the last one to write all patterns!") self.AddRuntimeMessage(rml, imsg) # loop over patterns and verify/construct filepaths for i, pat in enumerate(Pattern): # if there is a valid supplied filepath if i <= len(FilePath) - 1: # normalize path and get extension fp = path.normpath(FilePath[i].strip("\n\r")) ext = path.splitext(fp)[1] # if format in not supported, add warning if ext not in supported: rml = self.RuntimeMessageLevel.Warning errMsg = ("Format with extension '{}' is not a " + "supported file format! Please check the " + "description for a list of supported " + "formats. This pattern will not be written!") errMsg = errMsg.format(ext) self.AddRuntimeMessage(rml, errMsg) continue else: fp = path.normpath(FilePath[-1].strip("\n\r")) fp, ext = path.splitext(fp) # if format in not supported, add warning if ext not in supported: rml = self.RuntimeMessageLevel.Warning errMsg = ("Format with extension '{}' is not a " + "supported file format! Please check the " + "description for a list of supported " + "formats. This pattern will not be written!") errMsg = errMsg.format(ext) self.AddRuntimeMessage(rml, errMsg) continue # alter filepath to avoid overwriting original files fp = fp + " ({})" fp = fp.format(i - len(FilePath) + 1) + ext # write on execute if Execute: pyembroidery.write(pat, fp) else: rml = self.RuntimeMessageLevel.Warning if not Pattern: errMsg = "Input Pattern failed to collect data!" self.AddRuntimeMessage(rml, errMsg) if not FilePath: errMsg = "Input FilePath failed to collect data!" self.AddRuntimeMessage(rml, errMsg)