From 3fe3399d909de6549a7ce64f4347b342933b2465 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Sat, 1 Sep 2018 13:35:47 -0400 Subject: simulator standalone mode --- lib/extensions/simulate.py | 20 ++------------------ lib/simulator.py | 28 ++++++++++++++++++++++++++-- lib/stitch_plan/__init__.py | 3 ++- lib/stitch_plan/read_file.py | 21 +++++++++++++++++++++ 4 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 lib/stitch_plan/read_file.py (limited to 'lib') diff --git a/lib/extensions/simulate.py b/lib/extensions/simulate.py index dcb5e604..e23d391b 100644 --- a/lib/extensions/simulate.py +++ b/lib/extensions/simulate.py @@ -1,8 +1,6 @@ -import wx - from .base import InkstitchExtension from ..i18n import _ -from ..simulator import EmbroiderySimulator +from ..simulator import show_simulator from ..stitch_plan import patches_to_stitch_plan @@ -19,18 +17,4 @@ class Simulate(InkstitchExtension): return patches = self.elements_to_patches(self.elements) stitch_plan = patches_to_stitch_plan(patches) - app = wx.App() - current_screen = wx.Display.GetFromPoint(wx.GetMousePosition()) - display = wx.Display(current_screen) - screen_rect = display.GetClientArea() - - simulator_pos = (screen_rect[0], screen_rect[1]) - - # subtract 1 because otherwise the window becomes maximized on Linux - width = screen_rect[2] - 1 - height = screen_rect[3] - 1 - - frame = EmbroiderySimulator(None, -1, _("Embroidery Simulation"), pos=simulator_pos, size=(width, height), stitch_plan=stitch_plan) - app.SetTopWindow(frame) - frame.Show() - app.MainLoop() + show_simulator(stitch_plan) diff --git a/lib/simulator.py b/lib/simulator.py index c797d221..8f316be3 100644 --- a/lib/simulator.py +++ b/lib/simulator.py @@ -4,8 +4,9 @@ from wx.lib.intctrl import IntCtrl import time from itertools import izip -from .svg import color_block_to_point_lists, PIXELS_PER_MM +from .svg import PIXELS_PER_MM from .i18n import _ +from .stitch_plan import stitch_plan_from_file # L10N command label at bottom of simulator window COMMAND_NAMES = [_("STITCH"), _("JUMP"), _("TRIM"), _("STOP"), _("COLOR CHANGE")] @@ -330,7 +331,8 @@ class DrawingPanel(wx.Panel): canvas.SetPen(pen) if stitch + len(stitches) < self.current_stitch: stitch += len(stitches) - canvas.DrawLines(stitches) + if len(stitches) > 1: + canvas.DrawLines(stitches) last_stitch = stitches[-1] else: stitches = stitches[:self.current_stitch - stitch] @@ -615,3 +617,25 @@ class EmbroiderySimulator(wx.Frame): def clear(self): self.simulator_panel.clear() + +def show_simulator(stitch_plan): + app = wx.App() + current_screen = wx.Display.GetFromPoint(wx.GetMousePosition()) + display = wx.Display(current_screen) + screen_rect = display.GetClientArea() + + simulator_pos = (screen_rect[0], screen_rect[1]) + + # subtract 1 because otherwise the window becomes maximized on Linux + width = screen_rect[2] - 1 + height = screen_rect[3] - 1 + + frame = EmbroiderySimulator(None, -1, _("Embroidery Simulation"), pos=simulator_pos, size=(width, height), stitch_plan=stitch_plan) + app.SetTopWindow(frame) + frame.Show() + app.MainLoop() + + +if __name__ == "__main__": + stitch_plan = stitch_plan_from_file(sys.argv[1]) + show_simulator(stitch_plan) diff --git a/lib/stitch_plan/__init__.py b/lib/stitch_plan/__init__.py index 791a5f20..2aaa0ab9 100644 --- a/lib/stitch_plan/__init__.py +++ b/lib/stitch_plan/__init__.py @@ -1,2 +1,3 @@ -from stitch_plan import patches_to_stitch_plan, StitchPlan, ColorBlock +from .stitch_plan import patches_to_stitch_plan, StitchPlan, ColorBlock from .stitch import Stitch +from .read_file import stitch_plan_from_file diff --git a/lib/stitch_plan/read_file.py b/lib/stitch_plan/read_file.py new file mode 100644 index 00000000..ff5a68ac --- /dev/null +++ b/lib/stitch_plan/read_file.py @@ -0,0 +1,21 @@ +import pyembroidery +from .stitch_plan import StitchPlan + +from ..svg import PIXELS_PER_MM + + +def stitch_plan_from_file(embroidery_file): + """Read a machine embroidery file in any supported format and return a stitch plan.""" + pattern = pyembroidery.read(embroidery_file) + + stitch_plan = StitchPlan() + color_block = None + + for raw_stitches, thread in pattern.get_as_colorblocks(): + color_block = stitch_plan.new_color_block(thread) + for x, y, command in raw_stitches: + color_block.add_stitch(x * PIXELS_PER_MM / 10.0, y * PIXELS_PER_MM / 10.0, + jump=(command == pyembroidery.JUMP), + trim=(command == pyembroidery.TRIM)) + + return stitch_plan -- cgit v1.2.3