summaryrefslogtreecommitdiff
path: root/lib/extensions
diff options
context:
space:
mode:
Diffstat (limited to 'lib/extensions')
-rw-r--r--lib/extensions/__init__.py6
-rw-r--r--lib/extensions/base.py31
-rw-r--r--lib/extensions/commands.py161
-rw-r--r--lib/extensions/embroider.py3
-rw-r--r--lib/extensions/flip.py40
-rw-r--r--lib/extensions/input.py40
-rw-r--r--lib/extensions/install.py (renamed from lib/extensions/palettes.py)63
-rw-r--r--lib/extensions/output.py48
-rw-r--r--lib/extensions/params.py39
-rw-r--r--lib/extensions/print_pdf.py85
-rw-r--r--lib/extensions/zip.py74
11 files changed, 469 insertions, 121 deletions
diff --git a/lib/extensions/__init__.py b/lib/extensions/__init__.py
index ebdd2fc9..8b243176 100644
--- a/lib/extensions/__init__.py
+++ b/lib/extensions/__init__.py
@@ -1,6 +1,10 @@
from embroider import Embroider
-from palettes import Palettes
+from install import Install
from params import Params
from print_pdf import Print
from simulate import Simulate
from input import Input
+from output import Output
+from zip import Zip
+from flip import Flip
+from commands import Commands
diff --git a/lib/extensions/base.py b/lib/extensions/base.py
index ff587ca5..d230f1b0 100644
--- a/lib/extensions/base.py
+++ b/lib/extensions/base.py
@@ -7,6 +7,7 @@ from collections import MutableMapping
from ..svg.tags import *
from ..elements import AutoFill, Fill, Stroke, SatinColumn, Polyline, EmbroideryElement
from ..utils import cache
+from ..commands import is_command
SVG_METADATA_TAG = inkex.addNS("metadata", "svg")
@@ -57,16 +58,12 @@ class InkStitchMetadata(MutableMapping):
def __setitem__(self, name, value):
item = self._find_item(name)
+ item.text = json.dumps(value)
- if value:
- item.text = json.dumps(value)
- else:
- item.getparent().remove(item)
-
- def _find_item(self, name):
+ def _find_item(self, name, create=True):
tag = inkex.addNS(name, "inkstitch")
item = self.metadata.find(tag)
- if item is None:
+ if item is None and create:
item = inkex.etree.SubElement(self.metadata, tag)
return item
@@ -80,9 +77,9 @@ class InkStitchMetadata(MutableMapping):
return None
def __delitem__(self, name):
- item = self._find_item(name)
+ item = self._find_item(name, create=False)
- if item:
+ if item is not None:
self.metadata.remove(item)
def __iter__(self):
@@ -111,7 +108,7 @@ class InkstitchExtension(inkex.Effect):
inkex.errormsg(_("No embroiderable paths selected."))
else:
inkex.errormsg(_("No embroiderable paths found in document."))
- inkex.errormsg(_("Tip: use Path -> Object to Path to convert non-paths before embroidering."))
+ inkex.errormsg(_("Tip: use Path -> Object to Path to convert non-paths."))
def descendants(self, node):
nodes = []
@@ -158,14 +155,15 @@ class InkstitchExtension(inkex.Effect):
else:
classes = []
- if element.get_style("fill"):
+ if element.get_style("fill", "black"):
if element.get_boolean_param("auto_fill", True):
classes.append(AutoFill)
else:
classes.append(Fill)
if element.get_style("stroke"):
- classes.append(Stroke)
+ if not is_command(element.node):
+ classes.append(Stroke)
if element.get_boolean_param("stroke_first", False):
classes.reverse()
@@ -200,6 +198,15 @@ class InkstitchExtension(inkex.Effect):
def get_inkstitch_metadata(self):
return InkStitchMetadata(self.document)
+ def get_base_file_name(self):
+ svg_filename = self.document.getroot().get(inkex.addNS('docname', 'sodipodi'), "embroidery.svg")
+
+ if svg_filename.endswith('.svg'):
+ svg_filename = svg_filename[:-4]
+
+ return svg_filename
+
+
def parse(self):
"""Override inkex.Effect to add Ink/Stitch xml namespace"""
diff --git a/lib/extensions/commands.py b/lib/extensions/commands.py
new file mode 100644
index 00000000..2f3006ff
--- /dev/null
+++ b/lib/extensions/commands.py
@@ -0,0 +1,161 @@
+import os
+import sys
+import inkex
+import simpletransform
+import cubicsuperpath
+from copy import deepcopy
+from random import random
+from shapely import geometry as shgeo
+
+from .base import InkstitchExtension
+from ..i18n import _
+from ..elements import SatinColumn
+from ..utils import get_bundled_dir, cache
+from ..svg.tags import SVG_DEFS_TAG, SVG_GROUP_TAG, SVG_USE_TAG, SVG_PATH_TAG, INKSCAPE_GROUPMODE, XLINK_HREF, CONNECTION_START, CONNECTION_END, CONNECTOR_TYPE
+from ..svg import get_node_transform
+
+
+class Commands(InkstitchExtension):
+ COMMANDS = ["fill_start", "fill_end", "stop", "trim"]
+
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self, *args, **kwargs)
+ for command in self.COMMANDS:
+ self.OptionParser.add_option("--%s" % command, type="inkbool")
+
+ @property
+ def symbols_path(self):
+ return os.path.join(get_bundled_dir("symbols"), "inkstitch.svg")
+
+ @property
+ @cache
+ def symbols_svg(self):
+ with open(self.symbols_path) as symbols_file:
+ return inkex.etree.parse(symbols_file)
+
+ @property
+ @cache
+ def symbol_defs(self):
+ return self.symbols_svg.find(SVG_DEFS_TAG)
+
+ @property
+ @cache
+ def defs(self):
+ return self.document.find(SVG_DEFS_TAG)
+
+ def ensure_symbol(self, command):
+ path = "./*[@id='inkstitch_%s']" % command
+ if self.defs.find(path) is None:
+ self.defs.append(deepcopy(self.symbol_defs.find(path)))
+
+ def get_correction_transform(self, node):
+ # if we want to place our new nodes in the same group as this node,
+ # then we'll need to factor in the effects of any transforms set on
+ # the parents of this node.
+
+ # we can ignore the transform on the node itself since it won't apply
+ # to the objects we add
+ transform = get_node_transform(node.getparent())
+
+ # now invert it, so that we can position our objects in absolute
+ # coordinates
+ transform = simpletransform.invertTransform(transform)
+
+ return simpletransform.formatTransform(transform)
+
+ def add_connector(self, symbol, element):
+ # I'd like it if I could position the connector endpoint nicely but inkscape just
+ # moves it to the element's center immediately after the extension runs.
+ start_pos = (symbol.get('x'), symbol.get('y'))
+ end_pos = element.shape.centroid
+
+ path = inkex.etree.Element(SVG_PATH_TAG,
+ {
+ "id": self.uniqueId("connector"),
+ "d": "M %s,%s %s,%s" % (start_pos[0], start_pos[1], end_pos.x, end_pos.y),
+ "style": "stroke:#000000;stroke-width:1px;stroke-opacity:0.5;fill:none;",
+ "transform": self.get_correction_transform(symbol),
+ CONNECTION_START: "#%s" % symbol.get('id'),
+ CONNECTION_END: "#%s" % element.node.get('id'),
+ CONNECTOR_TYPE: "polyline",
+ }
+ )
+
+ symbol.getparent().insert(symbol.getparent().index(symbol), path)
+
+ def get_command_pos(self, element, index, total):
+ # Put command symbols 30 pixels out from the shape, spaced evenly around it.
+
+ # get a line running 30 pixels out from the shape
+ outline = element.shape.buffer(30).exterior
+
+ # pick this item's spot arond the outline and perturb it a bit to avoid
+ # stacking up commands if they run the extension multiple times
+ position = index / float(total)
+ position += random() * 0.1
+
+ return outline.interpolate(position, normalized=True)
+
+ def remove_legacy_param(self, element, command):
+ if command == "trim" or command == "stop":
+ # If they had the old "TRIM after" or "STOP after" attributes set,
+ # automatically delete them. THe new commands will do the same
+ # thing.
+ #
+ # If we didn't delete these here, then things would get confusing.
+ # If the user were to delete a "trim" symbol added by this extension
+ # but the "embroider_trim_after" attribute is still set, then the
+ # trim would keep happening.
+
+ attribute = "embroider_%s_after" % command
+
+ if attribute in element.node.attrib:
+ del element.node.attrib[attribute]
+
+ def add_command(self, element, commands):
+ for i, command in enumerate(commands):
+ self.remove_legacy_param(element, command)
+
+ pos = self.get_command_pos(element, i, len(commands))
+
+ symbol = inkex.etree.SubElement(element.node.getparent(), SVG_USE_TAG,
+ {
+ "id": self.uniqueId("use"),
+ XLINK_HREF: "#inkstitch_%s" % command,
+ "height": "100%",
+ "width": "100%",
+ "x": str(pos.x),
+ "y": str(pos.y),
+ "transform": self.get_correction_transform(element.node)
+ }
+ )
+
+ self.add_connector(symbol, element)
+
+ def effect(self):
+ if not self.get_elements():
+ return
+
+ if not self.selected:
+ inkex.errormsg(_("Please select one or more objects to which to attach commands."))
+ return
+
+ self.svg = self.document.getroot()
+
+ commands = [command for command in self.COMMANDS if getattr(self.options, command)]
+
+ if not commands:
+ inkex.errormsg(_("Please choose one or more commands to attach."))
+ return
+
+ for command in commands:
+ self.ensure_symbol(command)
+
+ # Each object (node) in the SVG may correspond to multiple Elements of different
+ # types (e.g. stroke + fill). We only want to process each one once.
+ seen_nodes = set()
+
+ for element in self.elements:
+ if element.node not in seen_nodes:
+ self.add_command(element, commands)
+ seen_nodes.add(element.node)
diff --git a/lib/extensions/embroider.py b/lib/extensions/embroider.py
index a213be64..1e994e27 100644
--- a/lib/extensions/embroider.py
+++ b/lib/extensions/embroider.py
@@ -44,8 +44,7 @@ class Embroider(InkstitchExtension):
if self.options.output_file:
output_path = os.path.join(self.options.path, self.options.output_file)
else:
- svg_filename = self.document.getroot().get(inkex.addNS('docname', 'sodipodi'), "embroidery.svg")
- csv_filename = svg_filename.replace('.svg', '.%s' % self.options.output_format)
+ csv_filename = '%s.%s' % (self.get_base_file_name(), self.options.output_format)
output_path = os.path.join(self.options.path, csv_filename)
def add_suffix(path, suffix):
diff --git a/lib/extensions/flip.py b/lib/extensions/flip.py
new file mode 100644
index 00000000..d8d78cb5
--- /dev/null
+++ b/lib/extensions/flip.py
@@ -0,0 +1,40 @@
+import sys
+import inkex
+import cubicsuperpath
+from shapely import geometry as shgeo
+
+from .base import InkstitchExtension
+from ..i18n import _
+from ..elements import SatinColumn
+
+class Flip(InkstitchExtension):
+ def subpath_to_linestring(self, subpath):
+ return shgeo.LineString()
+
+ def flip(self, satin):
+ csp = satin.path
+
+ if len(csp) > 1:
+ flattened = satin.flatten(csp)
+
+ # find the rails (the two longest paths) and swap them
+ indices = range(len(csp))
+ indices.sort(key=lambda i: shgeo.LineString(flattened[i]).length, reverse=True)
+
+ first = indices[0]
+ second = indices[1]
+ csp[first], csp[second] = csp[second], csp[first]
+
+ satin.node.set("d", cubicsuperpath.formatPath(csp))
+
+ def effect(self):
+ if not self.get_elements():
+ return
+
+ if not self.selected:
+ inkex.errormsg(_("Please select one or more satin columns to flip."))
+ return
+
+ for element in self.elements:
+ if isinstance(element, SatinColumn):
+ self.flip(element)
diff --git a/lib/extensions/input.py b/lib/extensions/input.py
index 251859c5..cb5ac452 100644
--- a/lib/extensions/input.py
+++ b/lib/extensions/input.py
@@ -3,49 +3,29 @@ from os.path import realpath, dirname, join as path_join
import sys
from inkex import etree
import inkex
-
-# help python find libembroidery when running in a local repo clone
-if getattr(sys, 'frozen', None) is None:
- sys.path.append(realpath(path_join(dirname(__file__), '..', '..')))
-
-from libembroidery import *
+import pyembroidery
from ..svg import PIXELS_PER_MM, render_stitch_plan
from ..svg.tags import INKSCAPE_LABEL
from ..i18n import _
-from ..stitch_plan import StitchPlan
+from ..stitch_plan import StitchPlan, ColorBlock
+from ..utils.io import save_stdout
class Input(object):
- def pattern_stitches(self, pattern):
- stitch_pointer = pattern.stitchList
- while stitch_pointer:
- yield stitch_pointer.stitch
- stitch_pointer = stitch_pointer.next
-
-
def affect(self, args):
embroidery_file = args[0]
- pattern = embPattern_create()
- embPattern_read(pattern, embroidery_file)
- embPattern_flipVertical(pattern)
+ pattern = pyembroidery.read(embroidery_file)
stitch_plan = StitchPlan()
color_block = None
- current_color = None
-
- for stitch in self.pattern_stitches(pattern):
- if stitch.color != current_color:
- thread = embThreadList_getAt(pattern.threadList, stitch.color)
- color = thread.color
- color_block = stitch_plan.new_color_block((color.r, color.g, color.b))
- current_color = stitch.color
- if not stitch.flags & END:
- color_block.add_stitch(stitch.xx * PIXELS_PER_MM, stitch.yy * PIXELS_PER_MM,
- jump=stitch.flags & JUMP,
- color_change=stitch.flags & STOP,
- trim=stitch.flags & TRIM)
+ 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))
extents = stitch_plan.extents
svg = etree.Element("svg", nsmap=inkex.NSS, attrib=
diff --git a/lib/extensions/palettes.py b/lib/extensions/install.py
index f7a6c7a5..42a92113 100644
--- a/lib/extensions/palettes.py
+++ b/lib/extensions/install.py
@@ -1,3 +1,5 @@
+# -*- coding: UTF-8 -*-
+
import sys
import traceback
import os
@@ -11,29 +13,29 @@ import logging
import wx
import inkex
-from ..utils import guess_inkscape_config_path
+from ..utils import guess_inkscape_config_path, get_bundled_dir
-class InstallPalettesFrame(wx.Frame):
+class InstallerFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
- default_path = os.path.join(guess_inkscape_config_path(), "palettes")
+ self.path = guess_inkscape_config_path()
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
- text = wx.StaticText(panel, label=_("Directory in which to install palettes:"))
- font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
- text.SetFont(font)
- sizer.Add(text, proportion=0, flag=wx.ALL|wx.EXPAND, border=10)
+ text_sizer = wx.BoxSizer(wx.HORIZONTAL)
- path_sizer = wx.BoxSizer(wx.HORIZONTAL)
- self.path_input = wx.TextCtrl(panel, wx.ID_ANY, value=default_path)
- path_sizer.Add(self.path_input, proportion=3, flag=wx.RIGHT|wx.EXPAND, border=20)
- chooser_button = wx.Button(panel, wx.ID_OPEN, _('Choose another directory...'))
- path_sizer.Add(chooser_button, proportion=1, flag=wx.EXPAND)
- sizer.Add(path_sizer, proportion=0, flag=wx.ALL|wx.EXPAND, border=10)
+ text = _('Ink/Stitch can install files ("add-ons") that make it easier to use Inkscape to create machine embroidery designs. These add-ons will be installed:') + \
+ "\n\n • " + _("thread manufacturer color palettes") + \
+ "\n • " + _("Ink/Stitch visual commands (Object -> Symbols...)")
+
+ static_text = wx.StaticText(panel, label=text)
+ font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
+ static_text.SetFont(font)
+ text_sizer.Add(static_text, proportion=0, flag=wx.ALL|wx.EXPAND, border=10)
+ sizer.Add(text_sizer, proportion=3, flag=wx.ALL|wx.EXPAND, border=0)
buttons_sizer = wx.BoxSizer(wx.HORIZONTAL)
install_button = wx.Button(panel, wx.ID_ANY, _("Install"))
@@ -41,15 +43,11 @@ class InstallPalettesFrame(wx.Frame):
buttons_sizer.Add(install_button, proportion=0, flag=wx.ALIGN_RIGHT|wx.ALL, border=5)
cancel_button = wx.Button(panel, wx.ID_CANCEL, _("Cancel"))
buttons_sizer.Add(cancel_button, proportion=0, flag=wx.ALIGN_RIGHT|wx.ALL, border=5)
- sizer.Add(buttons_sizer, proportion=0, flag=wx.ALIGN_RIGHT)
+ sizer.Add(buttons_sizer, proportion=1, flag=wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM)
- outer_sizer = wx.BoxSizer(wx.HORIZONTAL)
- outer_sizer.Add(sizer, proportion=0, flag=wx.ALIGN_CENTER_VERTICAL)
-
- panel.SetSizer(outer_sizer)
+ panel.SetSizer(sizer)
panel.Layout()
- chooser_button.Bind(wx.EVT_BUTTON, self.chooser_button_clicked)
cancel_button.Bind(wx.EVT_BUTTON, self.cancel_button_clicked)
install_button.Bind(wx.EVT_BUTTON, self.install_button_clicked)
@@ -57,36 +55,31 @@ class InstallPalettesFrame(wx.Frame):
self.Destroy()
def chooser_button_clicked(self, event):
- dialog = wx.DirDialog(self, _("Choose Inkscape palettes directory"))
+ dialog = wx.DirDialog(self, _("Choose Inkscape directory"))
if dialog.ShowModal() != wx.ID_CANCEL:
self.path_input.SetValue(dialog.GetPath())
def install_button_clicked(self, event):
try:
- self.install_palettes()
+ self.install_addons('palettes')
+ self.install_addons('symbols')
except Exception, e:
wx.MessageDialog(self,
- _('Thread palette installation failed') + ': \n' + traceback.format_exc(),
+ _('Inkscape add-on installation failed') + ': \n' + traceback.format_exc(),
_('Installation Failed'),
wx.OK).ShowModal()
else:
wx.MessageDialog(self,
- _('Thread palette files have been installed. Please restart Inkscape to load the new palettes.'),
+ _('Inkscape add-on files have been installed. Please restart Inkscape to load the new add-ons.'),
_('Installation Completed'),
wx.OK).ShowModal()
self.Destroy()
- def install_palettes(self):
- path = self.path_input.GetValue()
- palettes_dir = self.get_bundled_palettes_dir()
- self.copy_files(glob(os.path.join(palettes_dir, "*")), path)
-
- def get_bundled_palettes_dir(self):
- if getattr(sys, 'frozen', None) is not None:
- return realpath(os.path.join(sys._MEIPASS, '..', 'palettes'))
- else:
- return os.path.join(dirname(realpath(__file__)), 'palettes')
+ def install_addons(self, type):
+ path = os.path.join(self.path, type)
+ src_dir = get_bundled_dir(type)
+ self.copy_files(glob(os.path.join(src_dir, "*")), path)
if (sys.platform == "win32"):
# If we try to just use shutil.copy it says the operation requires elevation.
@@ -104,9 +97,9 @@ class InstallPalettesFrame(wx.Frame):
for palette_file in files:
shutil.copy(palette_file, dest)
-class Palettes(inkex.Effect):
+class Install(inkex.Effect):
def effect(self):
app = wx.App()
- installer_frame = InstallPalettesFrame(None, title=_("Ink/Stitch Thread Palette Installer"), size=(450, 200))
+ installer_frame = InstallerFrame(None, title=_("Ink/Stitch Add-ons Installer"), size=(550, 250))
installer_frame.Show()
app.MainLoop()
diff --git a/lib/extensions/output.py b/lib/extensions/output.py
new file mode 100644
index 00000000..1dc8d19d
--- /dev/null
+++ b/lib/extensions/output.py
@@ -0,0 +1,48 @@
+import sys
+import traceback
+import os
+import inkex
+import tempfile
+
+from .base import InkstitchExtension
+from ..i18n import _
+from ..output import write_embroidery_file
+from ..stitch_plan import patches_to_stitch_plan
+from ..svg import render_stitch_plan, PIXELS_PER_MM
+from ..utils.io import save_stdout
+
+class Output(InkstitchExtension):
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self)
+ self.OptionParser.add_option("-c", "--collapse_len_mm",
+ action="store", type="float",
+ dest="collapse_length_mm", default=3.0,
+ help="max collapse length (mm)")
+ self.OptionParser.add_option("-f", "--format",
+ dest="file_extension",
+ help="file extension to output (example: DST)")
+
+ def effect(self):
+ if not self.get_elements():
+ return
+
+ patches = self.elements_to_patches(self.elements)
+ stitch_plan = patches_to_stitch_plan(patches, self.options.collapse_length_mm * PIXELS_PER_MM)
+
+ temp_file = tempfile.NamedTemporaryFile(suffix=".%s" % self.options.file_extension, delete=False)
+
+ # in windows, failure to close here will keep the file locked
+ temp_file.close()
+
+ write_embroidery_file(temp_file.name, stitch_plan, self.document.getroot())
+
+ # inkscape will read the file contents from stdout and copy
+ # to the destination file that the user chose
+ with open(temp_file.name) as output_file:
+ sys.stdout.write(output_file.read())
+
+ # clean up the temp file
+ os.remove(temp_file.name)
+
+ # don't let inkex output the SVG!
+ sys.exit(0)
diff --git a/lib/extensions/params.py b/lib/extensions/params.py
index 03a6f3cc..58fedd6b 100644
--- a/lib/extensions/params.py
+++ b/lib/extensions/params.py
@@ -19,6 +19,7 @@ from ..stitch_plan import patches_to_stitch_plan
from ..elements import EmbroideryElement, Fill, AutoFill, Stroke, SatinColumn
from ..utils import save_stderr, restore_stderr
from ..simulator import EmbroiderySimulator
+from ..commands import is_command
def presets_path():
@@ -354,6 +355,9 @@ class SettingsFrame(wx.Frame):
self.simulate_thread = None
self.simulate_refresh_needed = Event()
+ # used when closing to avoid having the window reopen at the last second
+ self.disable_simulate_window = False
+
wx.CallLater(1000, self.update_simulator)
self.presets_box = wx.StaticBox(self, wx.ID_ANY, label=_("Presets"))
@@ -392,6 +396,9 @@ class SettingsFrame(wx.Frame):
self.simulate_window.stop()
self.simulate_window.clear()
+ if self.disable_simulate_window:
+ return
+
if not self.simulate_thread or not self.simulate_thread.is_alive():
self.simulate_thread = Thread(target=self.simulate_worker)
self.simulate_thread.daemon = True
@@ -586,6 +593,7 @@ class SettingsFrame(wx.Frame):
self.close()
def use_last(self, event):
+ self.disable_simulate_window = True
self._load_preset("__LAST__")
self.apply(event)
@@ -632,6 +640,9 @@ class SettingsFrame(wx.Frame):
self.Layout()
# end wxGlade
+class NoValidObjects(Exception):
+ pass
+
class Params(InkstitchExtension):
def __init__(self, *args, **kwargs):
self.cancelled = False
@@ -645,7 +656,7 @@ class Params(InkstitchExtension):
classes.append(AutoFill)
classes.append(Fill)
- if element.get_style("stroke"):
+ if element.get_style("stroke") and not is_command(node):
classes.append(Stroke)
if element.get_style("stroke-dasharray") is None:
@@ -689,6 +700,11 @@ class Params(InkstitchExtension):
def create_tabs(self, parent):
tabs = []
+ nodes_by_class = self.get_nodes_by_class()
+
+ if not nodes_by_class:
+ raise NoValidObjects()
+
for cls, nodes in self.get_nodes_by_class():
params = cls.get_params()
@@ -745,12 +761,15 @@ class Params(InkstitchExtension):
self.cancelled = True
def effect(self):
- app = wx.App()
- frame = SettingsFrame(tabs_factory=self.create_tabs, on_cancel=self.cancel)
- frame.Show()
- app.MainLoop()
-
- if self.cancelled:
- # This prevents the superclass from outputting the SVG, because we
- # may have modified the DOM.
- sys.exit(0)
+ try:
+ app = wx.App()
+ frame = SettingsFrame(tabs_factory=self.create_tabs, on_cancel=self.cancel)
+ frame.Show()
+ app.MainLoop()
+
+ if self.cancelled:
+ # This prevents the superclass from outputting the SVG, because we
+ # may have modified the DOM.
+ sys.exit(0)
+ except NoValidObjects:
+ self.no_elements_error()
diff --git a/lib/extensions/print_pdf.py b/lib/extensions/print_pdf.py
index baeb7eba..6e2eff58 100644
--- a/lib/extensions/print_pdf.py
+++ b/lib/extensions/print_pdf.py
@@ -21,7 +21,7 @@ import requests
from .base import InkstitchExtension
from ..i18n import _, translation as inkstitch_translation
from ..svg import PIXELS_PER_MM, render_stitch_plan
-from ..svg.tags import SVG_GROUP_TAG
+from ..svg.tags import SVG_GROUP_TAG, INKSCAPE_GROUPMODE
from ..stitch_plan import patches_to_stitch_plan
from ..threads import ThreadCatalog
@@ -94,6 +94,8 @@ class PrintPreviewServer(Thread):
self.html = kwargs.pop('html')
self.metadata = kwargs.pop('metadata')
self.stitch_plan = kwargs.pop('stitch_plan')
+ self.realistic_overview_svg = kwargs.pop('realistic_overview_svg')
+ self.realistic_color_block_svgs = kwargs.pop('realistic_color_block_svgs')
Thread.__init__(self, *args, **kwargs)
self.daemon = True
self.last_request_time = None
@@ -202,6 +204,14 @@ class PrintPreviewServer(Thread):
return jsonify(threads)
+ @self.app.route('/realistic/block<int:index>', methods=['GET'])
+ def get_realistic_block(index):
+ return Response(self.realistic_color_block_svgs[index], mimetype='image/svg+xml')
+
+ @self.app.route('/realistic/overview', methods=['GET'])
+ def get_realistic_overview():
+ return Response(self.realistic_overview_svg, mimetype='image/svg+xml')
+
def stop(self):
# for whatever reason, shutting down only seems possible in
# the context of a flask request, so we'll just make one
@@ -295,38 +305,24 @@ class Print(InkstitchExtension):
return env
- def strip_namespaces(self):
+ def strip_namespaces(self, svg):
# namespace prefixes seem to trip up HTML, so get rid of them
- for element in self.document.iter():
+ for element in svg.iter():
if element.tag[0]=='{':
element.tag = element.tag[element.tag.index('}',1) + 1:]
- def effect(self):
- # It doesn't really make sense to print just a couple of selected
- # objects. It's almost certain they meant to print the whole design.
- # If they really wanted to print just a few objects, they could set
- # the rest invisible temporarily.
- self.selected = {}
+ def render_svgs(self, stitch_plan, realistic=False):
+ svg = deepcopy(self.document).getroot()
+ render_stitch_plan(svg, stitch_plan, realistic)
- if not self.get_elements():
- return
-
- self.hide_all_layers()
-
- patches = self.elements_to_patches(self.elements)
- stitch_plan = patches_to_stitch_plan(patches)
- palette = ThreadCatalog().match_and_apply_palette(stitch_plan, self.get_inkstitch_metadata()['thread-palette'])
- render_stitch_plan(self.document.getroot(), stitch_plan)
-
- self.strip_namespaces()
+ self.strip_namespaces(svg)
# Now the stitch plan layer will contain a set of groups, each
# corresponding to a color block. We'll create a set of SVG files
# corresponding to each individual color block and a final one
# for all color blocks together.
- svg = self.document.getroot()
- layers = svg.findall("./g[@{http://www.inkscape.org/namespaces/inkscape}groupmode='layer']")
+ layers = svg.findall("./g[@%s='layer']" % INKSCAPE_GROUPMODE)
stitch_plan_layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']")
# First, delete all of the other layers. We don't need them and they'll
@@ -335,9 +331,9 @@ class Print(InkstitchExtension):
if layer is not stitch_plan_layer:
svg.remove(layer)
- overview_svg = inkex.etree.tostring(self.document)
-
+ overview_svg = inkex.etree.tostring(svg)
color_block_groups = stitch_plan_layer.getchildren()
+ color_block_svgs = []
for i, group in enumerate(color_block_groups):
# clear the stitch plan layer
@@ -347,12 +343,15 @@ class Print(InkstitchExtension):
stitch_plan_layer.append(group)
# save an SVG preview
- stitch_plan.color_blocks[i].svg_preview = inkex.etree.tostring(self.document)
+ color_block_svgs.append(inkex.etree.tostring(svg))
+ return overview_svg, color_block_svgs
+
+ def render_html(self, stitch_plan, overview_svg, selected_palette):
env = self.build_environment()
template = env.get_template('index.html')
- html = template.render(
+ return template.render(
view = {'client_overview': False, 'client_detailedview': False, 'operator_overview': True, 'operator_detailedview': True},
logo = {'src' : '', 'title' : 'LOGO'},
date = date.today(),
@@ -371,14 +370,38 @@ class Print(InkstitchExtension):
svg_overview = overview_svg,
color_blocks = stitch_plan.color_blocks,
palettes = ThreadCatalog().palette_names(),
- selected_palette = palette,
+ selected_palette = selected_palette,
)
- # We've totally mucked with the SVG. Restore it so that we can save
- # metadata into it.
- self.document = deepcopy(self.original_document)
+ def effect(self):
+ # It doesn't really make sense to print just a couple of selected
+ # objects. It's almost certain they meant to print the whole design.
+ # If they really wanted to print just a few objects, they could set
+ # the rest invisible temporarily.
+ self.selected = {}
+
+ if not self.get_elements():
+ return
+
+ patches = self.elements_to_patches(self.elements)
+ stitch_plan = patches_to_stitch_plan(patches)
+ palette = ThreadCatalog().match_and_apply_palette(stitch_plan, self.get_inkstitch_metadata()['thread-palette'])
+
+ overview_svg, color_block_svgs = self.render_svgs(stitch_plan, realistic=False)
+ realistic_overview_svg, realistic_color_block_svgs = self.render_svgs(stitch_plan, realistic=True)
+
+ for i, svg in enumerate(color_block_svgs):
+ stitch_plan.color_blocks[i].svg_preview = svg
+
+ html = self.render_html(stitch_plan, overview_svg, palette)
- print_server = PrintPreviewServer(html=html, metadata=self.get_inkstitch_metadata(), stitch_plan=stitch_plan)
+ print_server = PrintPreviewServer(
+ html=html,
+ metadata=self.get_inkstitch_metadata(),
+ stitch_plan=stitch_plan,
+ realistic_overview_svg=realistic_overview_svg,
+ realistic_color_block_svgs=realistic_color_block_svgs
+ )
print_server.start()
time.sleep(1)
diff --git a/lib/extensions/zip.py b/lib/extensions/zip.py
new file mode 100644
index 00000000..02f29e8a
--- /dev/null
+++ b/lib/extensions/zip.py
@@ -0,0 +1,74 @@
+import sys
+import traceback
+import os
+import inkex
+import tempfile
+from zipfile import ZipFile
+import pyembroidery
+
+from .base import InkstitchExtension
+from ..i18n import _
+from ..output import write_embroidery_file
+from ..stitch_plan import patches_to_stitch_plan
+from ..svg import render_stitch_plan, PIXELS_PER_MM
+from ..utils.io import save_stdout
+
+
+class Zip(InkstitchExtension):
+ def __init__(self, *args, **kwargs):
+ InkstitchExtension.__init__(self)
+ self.OptionParser.add_option("-c", "--collapse_len_mm",
+ action="store", type="float",
+ dest="collapse_length_mm", default=3.0,
+ help="max collapse length (mm)")
+
+ # it's kind of obnoxious that I have to do this...
+ self.formats = []
+ for format in pyembroidery.supported_formats():
+ if 'writer' in format and format['category'] == 'embroidery':
+ extension = format['extension']
+ self.OptionParser.add_option('--format-%s' % extension, type="inkbool", dest=extension)
+ self.formats.append(extension)
+
+ def effect(self):
+ if not self.get_elements():
+ return
+
+ patches = self.elements_to_patches(self.elements)
+ stitch_plan = patches_to_stitch_plan(patches, self.options.collapse_length_mm * PIXELS_PER_MM)
+
+ base_file_name = self.get_base_file_name()
+ path = tempfile.mkdtemp()
+
+ files = []
+
+ for format in self.formats:
+ if getattr(self.options, format):
+ output_file = os.path.join(path, "%s.%s" % (base_file_name, format))
+ write_embroidery_file(output_file, stitch_plan, self.document.getroot())
+ files.append(output_file)
+
+ if not files:
+ self.errormsg(_("No embroidery file formats selected."))
+
+ temp_file = tempfile.NamedTemporaryFile(suffix=".zip", delete=False)
+
+ # in windows, failure to close here will keep the file locked
+ temp_file.close()
+
+ with ZipFile(temp_file.name, "w") as zip_file:
+ for file in files:
+ zip_file.write(file, os.path.basename(file))
+
+ # inkscape will read the file contents from stdout and copy
+ # to the destination file that the user chose
+ with open(temp_file.name) as output_file:
+ sys.stdout.write(output_file.read())
+
+ os.remove(temp_file.name)
+ for file in files:
+ os.remove(file)
+ os.rmdir(path)
+
+ # don't let inkex output the SVG!
+ sys.exit(0)