# Authors: see git history # # Copyright (c) 2021 Authors # Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. import inkex from lxml import etree from ..i18n import _ from ..svg.tags import EMBROIDERABLE_TAGS, SVG_DEFS_TAG from .base import InkstitchExtension class SelectionToPattern(InkstitchExtension): def effect(self): if not self.get_elements(): return if not self.svg.selected: inkex.errormsg(_("Please select at least one object to be marked as a pattern.")) return for pattern in self.get_nodes(): if pattern.tag in EMBROIDERABLE_TAGS: self.set_marker(pattern) def set_marker(self, node): xpath = ".//marker[@id='inkstitch-pattern-marker']" pattern_marker = self.document.xpath(xpath) if not pattern_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 = """ """ # noqa: E501 defs.append(etree.fromstring(marker)) # attach marker to node style = node.get('style', '').split(";") style = [i for i in style if not i.startswith('marker-start')] style.append('marker-start:url(#inkstitch-pattern-marker)') node.set('style', ";".join(style))