diff options
| author | Lex Neva <github@lexneva.name> | 2016-10-19 20:47:30 -0400 |
|---|---|---|
| committer | Lex Neva <github@lexneva.name> | 2016-10-19 20:47:30 -0400 |
| commit | b70e0e7fa815c4e79d4470b17bc2339637c401cb (patch) | |
| tree | 1b5198f593709949fae80bb34a77e9167f11ea38 | |
| parent | 6a23fe868bd58af539c67011b53b1aef72733e0a (diff) | |
stuff
| -rw-r--r-- | embroider.inx | 2 | ||||
| -rw-r--r-- | embroider.py | 47 | ||||
| -rw-r--r-- | reorder.inx | 16 | ||||
| -rw-r--r-- | reorder.py | 38 |
4 files changed, 96 insertions, 7 deletions
diff --git a/embroider.inx b/embroider.inx index cb88271e..08020157 100644 --- a/embroider.inx +++ b/embroider.inx @@ -28,7 +28,7 @@ <_option value="csv">Embroidermodder 2 CSV</_option> <_option value="gcode">Franklin G-Code</_option> </param> - <param name="filename" type="string" _gui-text="File">embroider-output.exp</param> + <param name="path" type="string" _gui-text="Directory"></param> <effect> <object-type>all</object-type> <effects-menu> diff --git a/embroider.py b/embroider.py index 17b29aff..cad82bc2 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 00000000..77bf59d7 --- /dev/null +++ b/reorder.inx @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> + <_name>Reorder</_name> + <id>lexelby.embroider.reorder</id> + <dependency type="executable" location="extensions">reorder.py</dependency> + <dependency type="executable" location="extensions">inkex.py</dependency> + <effect> + <object-type>all</object-type> + <effects-menu> + <submenu _name="Embroidery"/> + </effects-menu> + </effect> + <script> + <command reldir="extensions" interpreter="python">reorder.py</command> + </script> +</inkscape-extension> diff --git a/reorder.py b/reorder.py new file mode 100644 index 00000000..e1722209 --- /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() |
