Optionally auto-generate preambles

pull/3/head
Stefan Siegl 2015-01-02 14:30:33 +01:00
rodzic 60199fffe0
commit 1689309c6e
2 zmienionych plików z 62 dodań i 2 usunięć

Wyświetl plik

@ -9,6 +9,13 @@
<param name="collapse_len_mm" type="float" min="0.0" max="10.0" _gui-text="Maximum collapse length (mm)">0.0</param> <param name="collapse_len_mm" type="float" min="0.0" max="10.0" _gui-text="Maximum collapse length (mm)">0.0</param>
<param name="preserve_order" type="boolean" _gui-text="Preserve stacking order" description="if false, sorts by color, which saves thread changes. True preserves stacking order, important if you're laying colors over each other.">false</param> <param name="preserve_order" type="boolean" _gui-text="Preserve stacking order" description="if false, sorts by color, which saves thread changes. True preserves stacking order, important if you're laying colors over each other.">false</param>
<param name="hatch_filled_paths" type="boolean" _gui-text="Hatch filled paths" description="If false, filled paths are filled using equally-spaced lines. If true, filled paths are filled using hatching lines.">false</param> <param name="hatch_filled_paths" type="boolean" _gui-text="Hatch filled paths" description="If false, filled paths are filled using equally-spaced lines. If true, filled paths are filled using hatching lines.">false</param>
<param name="add_preamble" type="optiongroup" _gui-text="Add preamble" appearance="minimal">
<_option value="0">None</_option>
<_option value="010">0-1-0</_option>
<_option value="01010">0-1-0-1-0</_option>
<_option value="01210">0-1-2-1-0</_option>
<_option value="012101210">0-1-2-1-0-1-2-1-0</_option>
</param>
<param name="output_format" type="optiongroup" _gui-text="Output file format" appearance="minimal"> <param name="output_format" type="optiongroup" _gui-text="Output file format" appearance="minimal">
<_option value="melco">Melco</_option> <_option value="melco">Melco</_option>
<_option value="csv">Embroidermodder 2 CSV</_option> <_option value="csv">Embroidermodder 2 CSV</_option>

Wyświetl plik

@ -328,7 +328,36 @@ class EmbroideryObject:
self.patchList = patchList self.patchList = patchList
self.row_spacing_px = row_spacing_px self.row_spacing_px = row_spacing_px
def emit_file(self, filename, output_format, collapse_len_px):
def make_preamble_stitch(self, lastp, nextp):
def fromPolar(r, phi):
x = r * math.cos(phi)
y = r * math.sin(phi)
return (x, y)
def toPolar(x, y):
r = math.sqrt(x ** 2 + y ** 2)
if r == 0:
phi = 0
elif y == 0:
phi = 0 if x > 0 else math.pi
else:
phi = cmp(y, 0) * math.acos(x / r)
return (r, phi)
v = nextp - lastp
(r, phi) = toPolar(v.x, v.y)
PREAMBLE_MAX_DIST = 0.5 * pixels_per_millimeter # 1/2mm
if r < PREAMBLE_MAX_DIST:
# nextp is close enough to lastp, so we don't generate
# extra points in between, but just use nextp
return nextp
r = PREAMBLE_MAX_DIST
(x, y) = fromPolar(r, phi)
return PyEmb.Point(x, y) + lastp
def emit_file(self, filename, output_format, collapse_len_px, add_preamble):
emb = PyEmb.Embroidery() emb = PyEmb.Embroidery()
lastStitch = None lastStitch = None
lastColor = None lastColor = None
@ -357,6 +386,24 @@ class EmbroideryObject:
newStitch.jumpStitch = jumpStitch newStitch.jumpStitch = jumpStitch
emb.addStitch(newStitch) emb.addStitch(newStitch)
if jumpStitch and add_preamble != "0":
locs = [ newStitch ]
i = 0
nextp = PyEmb.Point(patch.stitches[i].x, -patch.stitches[i].y)
for j in xrange(1, 4):
if locs[-1] == nextp:
i += 1
nextp = PyEmb.Point(patch.stitches[i].x, -patch.stitches[i].y)
locs.append(self.make_preamble_stitch(locs[-1], nextp))
dbg.write("preamble locations: %s\n" % locs)
for j in add_preamble[1:]:
stitch = deepcopy(locs[int(j)])
stitch.color = patch.color
stitch.jumpStitch = False
emb.addStitch(stitch)
jumpStitch = False jumpStitch = False
lastStitch = newStitch lastStitch = newStitch
lastColor = patch.color lastColor = patch.color
@ -460,6 +507,11 @@ class Embroider(inkex.Effect):
choices=["true","false"], choices=["true","false"],
dest="hatch_filled_paths", default="false", dest="hatch_filled_paths", default="false",
help="Use hatching lines instead of equally-spaced lines to fill paths") help="Use hatching lines instead of equally-spaced lines to fill paths")
self.OptionParser.add_option("-p", "--add_preamble",
action="store", type="choice",
choices=["0","010","01010","01210","012101210"],
dest="add_preamble", default="0",
help="Add preamble")
self.OptionParser.add_option("-O", "--output_format", self.OptionParser.add_option("-O", "--output_format",
action="store", type="choice", action="store", type="choice",
choices=["melco", "csv"], choices=["melco", "csv"],
@ -629,7 +681,8 @@ class Embroider(inkex.Effect):
dbg.write("patch count: %d\n" % len(self.patchList.patches)) dbg.write("patch count: %d\n" % len(self.patchList.patches))
eo = EmbroideryObject(self.patchList, self.row_spacing_px) eo = EmbroideryObject(self.patchList, self.row_spacing_px)
eo.emit_file(self.options.filename, self.options.output_format, self.collapse_len_px) eo.emit_file(self.options.filename, self.options.output_format,
self.collapse_len_px, self.options.add_preamble)
new_group = inkex.etree.SubElement(self.current_layer, new_group = inkex.etree.SubElement(self.current_layer,
inkex.addNS('g', 'svg'), {}) inkex.addNS('g', 'svg'), {})