summaryrefslogtreecommitdiff
path: root/embroider_simulate.py
diff options
context:
space:
mode:
authorLex Neva <lexelby@users.noreply.github.com>2018-01-28 16:10:37 -0500
committerGitHub <noreply@github.com>2018-01-28 16:10:37 -0500
commitfabe5bcd32a8ce7488034b5d67f1dacf1b364556 (patch)
treefc484539550aced201953fd1837e4229ed0b154e /embroider_simulate.py
parentede0b2d0e6c2937182f3564948abf94c81503756 (diff)
Fix simulate (#42)
* Simulate now works regardless of the output format you chose when you ran Embroider. * Simulate (and the preview in Params) now respects TRIMs. * Inkscape restart required (embroider.inx changed). This one kind of grew in the telling. #37 was a theoretically simple bug, but in reality, the code necessary to fix it was the straw that broke the camel's back, and I had to do a fair bit of (much needed) code reorganization. Mostly the reorganization was just under the hood, but there was one user-facing change around the Embroider extension's settings window. Way back in the day, the only way to control things like the stitch length or satin density was through global options specified in the extension settings. We've long since moved to per-object params, but for backward compatibility, ink/stitch defaulted to the command-line arguments. That means that it was possible to get different stitch results from the same SVG file if you changed the extension's settings. For that reason, I never touched mine. I didn't intend for my users to use those extension-level settings at all, and I've planned to remove those settings for awhile now. At this point, the extension settings just getting in the way of implementing more features, so I'm getting rid of them and moving the defaults into the parameters system. I've still left things like the output format and the collapse length (although I'm considering moving that one too).
Diffstat (limited to 'embroider_simulate.py')
-rw-r--r--embroider_simulate.py77
1 files changed, 18 insertions, 59 deletions
diff --git a/embroider_simulate.py b/embroider_simulate.py
index 4b772692..667ef7d0 100644
--- a/embroider_simulate.py
+++ b/embroider_simulate.py
@@ -6,7 +6,8 @@ import inkex
import simplestyle
import colorsys
-from embroider import patches_to_stitches, stitches_to_polylines, PIXELS_PER_MM
+from inkstitch import PIXELS_PER_MM
+from embroider import patches_to_stitches, get_elements, elements_to_patches
class EmbroiderySimulator(wx.Frame):
def __init__(self, *args, **kwargs):
@@ -134,12 +135,25 @@ class EmbroiderySimulator(wx.Frame):
last_pos = None
last_color = None
pen = None
+ trimming = False
for stitch in stitches:
+ if stitch.trim:
+ trimming = True
+ last_pos = None
+ continue
+
+ if trimming:
+ if stitch.jump:
+ continue
+ else:
+ trimming = False
+
pos = (stitch.x, stitch.y)
if stitch.color == last_color:
- segments.append(((last_pos, pos), pen))
+ if last_pos:
+ segments.append(((last_pos, pos), pen))
else:
pen = self.color_to_pen(simplestyle.parseColor(stitch.color))
@@ -148,55 +162,6 @@ class EmbroiderySimulator(wx.Frame):
return segments
- def _parse_stitch_file(self, stitch_file_path):
- # "#", "comment"
- # "$","1","229","229","229","(null)","(null)"
- # "*","JUMP","1.595898","48.731899"
- # "*","STITCH","1.595898","48.731899"
-
- segments = []
-
- pos = (0, 0)
- pen = wx.Pen('black')
- cut = True
-
- with open(stitch_file_path) as stitch_file:
- for line in stitch_file:
- line = line.strip()
- if not line:
- continue
-
- fields = line.strip().split(",")
- fields = [self._strip_quotes(field) for field in fields]
-
- symbol, command = fields[:2]
-
- if symbol == "$":
- red, green, blue = fields[2:5]
- pen = self.color_to_pen((int(red), int(green), int(blue)))
- elif symbol == "*":
- if command == "COLOR":
- # change color
- # The next command should be a JUMP, and we'll need to skip stitching.
- cut = True
- elif command == "JUMP" or command == "STITCH":
- # JUMP just means a long stitch, really.
-
- x, y = fields[2:]
- new_pos = (float(x) * PIXELS_PER_MM, float(y) * PIXELS_PER_MM)
-
- if not segments and new_pos == (0.0, 0.0):
- # libembroidery likes to throw an extra JUMP in at the start
- continue
-
- if not cut:
- segments.append(((pos, new_pos), pen))
-
- cut = False
- pos = new_pos
-
- return segments
-
def all_coordinates(self):
for segment in self.segments:
start, end = segment[0]
@@ -330,20 +295,14 @@ class SimulateEffect(inkex.Effect):
help="Directory in which to store output file")
def effect(self):
+ patches = elements_to_patches(get_elements(self))
app = wx.App()
- frame = EmbroiderySimulator(None, -1, "Embroidery Simulation", wx.DefaultPosition, size=(1000, 1000), stitch_file=self.get_stitch_file())
+ frame = EmbroiderySimulator(None, -1, "Embroidery Simulation", wx.DefaultPosition, size=(1000, 1000), patches=patches)
app.SetTopWindow(frame)
frame.Show()
wx.CallAfter(frame.go)
app.MainLoop()
- def get_stitch_file(self):
- svg_filename = self.document.getroot().get(inkex.addNS('docname', 'sodipodi'))
- csv_filename = svg_filename.replace('.svg', '.csv')
- stitch_file = os.path.join(self.options.path, csv_filename)
-
- return stitch_file
-
if __name__ == "__main__":
effect = SimulateEffect()