2021-03-12 04:17:19 +00:00
|
|
|
# Authors: see git history
|
|
|
|
#
|
|
|
|
# Copyright (c) 2010 Authors
|
|
|
|
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
|
|
|
|
|
2022-06-21 17:48:28 +00:00
|
|
|
from inkex import Boolean, Style
|
2023-01-28 13:55:10 +00:00
|
|
|
from lxml import etree
|
2022-06-21 17:48:28 +00:00
|
|
|
|
2021-08-07 16:18:55 +00:00
|
|
|
from ..stitch_plan import stitch_groups_to_stitch_plan
|
2020-03-22 08:16:28 +00:00
|
|
|
from ..svg import render_stitch_plan
|
2023-01-28 13:55:10 +00:00
|
|
|
from ..svg.tags import (INKSCAPE_GROUPMODE, INKSTITCH_ATTRIBS,
|
|
|
|
SODIPODI_INSENSITIVE, SVG_DEFS_TAG, SVG_GROUP_TAG,
|
|
|
|
SVG_PATH_TAG)
|
2020-03-22 08:16:28 +00:00
|
|
|
from .base import InkstitchExtension
|
2022-06-21 17:48:28 +00:00
|
|
|
from .stitch_plan_preview_undo import reset_stitch_plan
|
2020-03-22 08:16:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StitchPlanPreview(InkstitchExtension):
|
2021-11-07 19:13:08 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
InkstitchExtension.__init__(self, *args, **kwargs)
|
|
|
|
self.arg_parser.add_argument("-s", "--move-to-side", type=Boolean, default=True, dest="move_to_side")
|
|
|
|
self.arg_parser.add_argument("-v", "--layer-visibility", type=int, default=0, dest="layer_visibility")
|
|
|
|
self.arg_parser.add_argument("-n", "--needle-points", type=Boolean, default=False, dest="needle_points")
|
2023-01-28 13:55:10 +00:00
|
|
|
self.arg_parser.add_argument("-i", "--insensitive", type=Boolean, default=False, dest="insensitive")
|
2021-11-07 19:13:08 +00:00
|
|
|
|
2020-03-22 08:16:28 +00:00
|
|
|
def effect(self):
|
|
|
|
# delete old stitch plan
|
|
|
|
svg = self.document.getroot()
|
2022-06-21 17:48:28 +00:00
|
|
|
reset_stitch_plan(svg)
|
2020-03-22 08:16:28 +00:00
|
|
|
|
|
|
|
# create new stitch plan
|
|
|
|
if not self.get_elements():
|
|
|
|
return
|
2021-03-04 17:40:53 +00:00
|
|
|
|
|
|
|
realistic = False
|
2022-06-21 17:48:28 +00:00
|
|
|
visual_commands = True
|
2021-03-04 17:40:53 +00:00
|
|
|
self.metadata = self.get_inkstitch_metadata()
|
|
|
|
collapse_len = self.metadata['collapse_len_mm']
|
2022-06-22 13:22:34 +00:00
|
|
|
min_stitch_len = self.metadata['min_stitch_len_mm']
|
2021-08-07 16:37:17 +00:00
|
|
|
patches = self.elements_to_stitch_groups(self.elements)
|
2022-06-22 13:22:34 +00:00
|
|
|
stitch_plan = stitch_groups_to_stitch_plan(patches, collapse_len=collapse_len, min_stitch_len=min_stitch_len)
|
2022-06-21 17:48:28 +00:00
|
|
|
render_stitch_plan(svg, stitch_plan, realistic, visual_commands)
|
2020-03-22 08:16:28 +00:00
|
|
|
|
2021-11-07 19:13:08 +00:00
|
|
|
# apply options
|
2020-03-22 08:16:28 +00:00
|
|
|
layer = svg.find(".//*[@id='__inkstitch_stitch_plan__']")
|
2021-11-07 19:13:08 +00:00
|
|
|
|
2021-12-09 14:05:21 +00:00
|
|
|
# update layer visibility 0 = unchanged, 1 = hidden, 2 = lower opacity
|
2022-06-21 17:48:28 +00:00
|
|
|
groups = self.document.getroot().findall(SVG_GROUP_TAG)
|
2021-11-07 19:13:08 +00:00
|
|
|
if self.options.layer_visibility == 1:
|
2022-06-21 17:48:28 +00:00
|
|
|
self.set_invisible_layers_attribute(groups, layer)
|
2021-11-07 19:13:08 +00:00
|
|
|
self.hide_all_layers()
|
2022-06-21 17:48:28 +00:00
|
|
|
layer.style['display'] = "inline"
|
2021-11-14 08:53:41 +00:00
|
|
|
elif self.options.layer_visibility == 2:
|
2022-06-21 17:48:28 +00:00
|
|
|
for g in groups:
|
2021-11-14 08:53:41 +00:00
|
|
|
style = g.specified_style()
|
|
|
|
# check groupmode and exclude stitch_plan layer
|
|
|
|
# exclude objects which are not displayed at all or already have opacity < 0.4
|
|
|
|
if (g.get(INKSCAPE_GROUPMODE) == "layer" and not g == layer and
|
|
|
|
float(style.get('opacity', 1)) > 0.4 and not style.get('display', 'inline') == 'none'):
|
2022-06-21 17:48:28 +00:00
|
|
|
g.style['opacity'] = 0.4
|
2021-11-07 19:13:08 +00:00
|
|
|
|
2023-01-28 13:55:10 +00:00
|
|
|
if self.options.insensitive is True:
|
|
|
|
layer.set(SODIPODI_INSENSITIVE, True)
|
|
|
|
else:
|
|
|
|
layer.set(SODIPODI_INSENSITIVE, False)
|
|
|
|
|
2021-11-07 19:13:08 +00:00
|
|
|
# translate stitch plan to the right side of the canvas
|
|
|
|
if self.options.move_to_side:
|
|
|
|
layer.set('transform', 'translate(%s)' % svg.get('viewBox', '0 0 800 0').split(' ')[2])
|
|
|
|
else:
|
|
|
|
layer.set('transform', None)
|
|
|
|
|
|
|
|
# display needle points
|
|
|
|
if self.options.needle_points:
|
|
|
|
markers = 'marker-mid:url(#inkstitch-needle-point);marker-start:url(#inkstitch-needle-point);marker-end:url(#inkstitch-needle-point)'
|
|
|
|
for element in layer.iterdescendants(SVG_PATH_TAG):
|
2022-06-21 17:48:28 +00:00
|
|
|
style = element.style + Style(markers)
|
2021-11-07 19:13:08 +00:00
|
|
|
element.set('style', style)
|
|
|
|
self.ensure_marker()
|
|
|
|
|
2022-06-21 17:48:28 +00:00
|
|
|
def set_invisible_layers_attribute(self, groups, layer):
|
|
|
|
invisible_layers = []
|
|
|
|
for g in groups:
|
|
|
|
if g.get(INKSCAPE_GROUPMODE) == "layer" and 'display' in g.style and g.style['display'] == 'none':
|
|
|
|
invisible_layers.append(g.get_id())
|
|
|
|
layer.set(INKSTITCH_ATTRIBS['invisible_layers'], ",".join(invisible_layers))
|
|
|
|
|
2021-11-07 19:13:08 +00:00
|
|
|
def ensure_marker(self):
|
|
|
|
xpath = ".//svg:marker[@id='inkstitch-needle-point']"
|
|
|
|
point_marker = self.document.getroot().xpath(xpath)
|
|
|
|
|
|
|
|
if not point_marker:
|
|
|
|
# get or create def element
|
|
|
|
defs = self.document.find(SVG_DEFS_TAG)
|
|
|
|
if defs is None:
|
|
|
|
defs = etree.SubElement(self.document, SVG_DEFS_TAG)
|
|
|
|
|
|
|
|
# insert marker
|
|
|
|
marker = """<marker
|
|
|
|
orient="auto"
|
|
|
|
id="inkstitch-needle-point">
|
|
|
|
<circle
|
|
|
|
cx="0" cy="0" r="1.5"
|
|
|
|
style="fill:context-stroke;opacity:0.8;" />
|
|
|
|
</marker>"""
|
|
|
|
defs.append(etree.fromstring(marker))
|