From b70e0e7fa815c4e79d4470b17bc2339637c401cb Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Wed, 19 Oct 2016 20:47:30 -0400 Subject: [PATCH] stuff --- embroider.inx | 2 +- embroider.py | 47 +++++++++++++++++++++++++++++++++++++++++------ reorder.inx | 16 ++++++++++++++++ reorder.py | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 reorder.inx create mode 100644 reorder.py diff --git a/embroider.inx b/embroider.inx index cb88271ec..080201577 100644 --- a/embroider.inx +++ b/embroider.inx @@ -28,7 +28,7 @@ <_option value="csv">Embroidermodder 2 CSV <_option value="gcode">Franklin G-Code - embroider-output.exp + all diff --git a/embroider.py b/embroider.py index 17b29aff8..cad82bc25 100644 --- a/embroider.py +++ b/embroider.py @@ -650,10 +650,14 @@ class Embroider(inkex.Effect): choices=["melco", "csv", "gcode"], dest="output_format", default="melco", help="File output format") - self.OptionParser.add_option("-F", "--filename", + self.OptionParser.add_option("-P", "--path", action="store", type="string", - dest="filename", default="embroider-output.exp", - help="Name (and possibly path) of output file") + dest="path", default=".", + help="Directory in which to store output file") + self.OptionParser.add_option("-b", "--max-backups", + action="store", type="int", + dest="max_backups", default=5, + help="Max number of backups of output files to keep.") self.patches = [] def get_sort_order(self, threadcolor, node): @@ -961,6 +965,33 @@ class Embroider(inkex.Effect): process(self.document.getroot()) + def get_output_path(self): + svg_filename = self.document.getroot().get(inkex.addNS('docname', 'sodipodi')) + csv_filename = svg_filename.replace('.svg', '.csv') + output_path = os.path.join(self.options.path, csv_filename) + + def add_suffix(path, suffix): + if suffix > 0: + path = "%s.%s" % (path, suffix) + + return path + + def move_if_exists(path, suffix=0): + source = add_suffix(path, suffix) + + if suffix >= self.options.max_backups: + return + + dest = add_suffix(path, suffix + 1) + + if os.path.exists(source): + move_if_exists(path, suffix + 1) + os.rename(source, dest) + + move_if_exists(output_path) + + return output_path + def effect(self): # Printing anything other than a valid SVG on stdout blows inkscape up. old_stdout = sys.stdout @@ -1007,7 +1038,7 @@ class Embroider(inkex.Effect): self.hide_layers() eo = EmbroideryObject(self.patchList, self.row_spacing_px) - emb = eo.emit_file(self.options.filename, self.options.output_format, + emb = eo.emit_file(self.get_output_path(), self.options.output_format, self.collapse_len_px, self.options.add_preamble) new_layer = inkex.etree.SubElement(self.document.getroot(), @@ -1207,12 +1238,16 @@ class Embroider(inkex.Effect): patch = Patch(color=threadcolor, sortorder=sortorder) def offset_stitches(pos1, pos2, offset_px): - if pos1 == pos2: + distance = (pos1 - pos2).length() + + if (pos1 - pos2).length() < 0.0001: # if they're the same, we don't know which direction # to offset in, so we have to just return the points return pos1, pos2 - print pos1, pos2 + # don't offset so far that pos1 and pos2 switch places + if offset_px < -distance/2.0: + offset_px = -distance/2.0 midpoint = (pos2 + pos1) * 0.5 pos1 = pos1 + (pos1 - midpoint).unit() * offset_px diff --git a/reorder.inx b/reorder.inx new file mode 100644 index 000000000..77bf59d70 --- /dev/null +++ b/reorder.inx @@ -0,0 +1,16 @@ + + + <_name>Reorder + lexelby.embroider.reorder + reorder.py + inkex.py + + all + + + + + + diff --git a/reorder.py b/reorder.py new file mode 100644 index 000000000..e1722209e --- /dev/null +++ b/reorder.py @@ -0,0 +1,38 @@ +#!/usr/bin/python +# +# Remove selected objects from the document and readd them in the order they +# were selected. + +import sys +sys.path.append("/usr/share/inkscape/extensions") +import os +import inkex + + +class Reorder(inkex.Effect): + def get_selected_in_order(self): + selected = [] + + for i in self.options.ids: + path = '//*[@id="%s"]' % i + for node in self.document.xpath(path, namespaces=inkex.NSS): + selected.append(node) + + return selected + + def effect(self): + objects = self.get_selected_in_order() + + for obj in objects[1:]: + obj.getparent().remove(obj) + + insert_parent = objects[0].getparent() + insert_pos = insert_parent.index(objects[0]) + + insert_parent.remove(objects[0]) + + insert_parent[insert_pos:insert_pos] = objects + +if __name__ == '__main__': + e = Reorder() + e.affect()