From 7e357513bd0e7cbecb1e2812d2bf9f8a98d3d684 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Sat, 13 May 2023 21:01:04 +0200 Subject: [PATCH] Draft: Support group styles in SVG import - fixes #6900 (#9522) * Draft: Support group styles in SVG import - fixes #6900 * Draft: SVG importer - apply styleto polylines --- src/Mod/Draft/importSVG.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Mod/Draft/importSVG.py b/src/Mod/Draft/importSVG.py index 18581dbc6b..aeb6ef6ed6 100644 --- a/src/Mod/Draft/importSVG.py +++ b/src/Mod/Draft/importSVG.py @@ -663,6 +663,7 @@ class svgHandler(xml.sax.ContentHandler): self.count = 0 self.transform = None self.grouptransform = [] + self.groupstyles = [] self.lastdim = None self.viewbox = None self.symbols = {} @@ -710,6 +711,7 @@ class svgHandler(xml.sax.ContentHandler): self.count += 1 _msg('processing element {0}: {1}'.format(self.count, name)) _msg('existing group transform: {}'.format(self.grouptransform)) + _msg('existing group style: {}'.format(self.groupstyles)) data = {} for (keyword, content) in list(attrs.items()): @@ -844,10 +846,10 @@ class svgHandler(xml.sax.ContentHandler): m.scale(Vector(25.4/self.svgdpi, 25.4/self.svgdpi, 1)) self.grouptransform.append(m) if 'fill' in data: - if data['fill'][0] != 'none': + if data['fill'] != 'none': self.fill = getcolor(data['fill']) if 'stroke' in data: - if data['stroke'][0] != 'none': + if data['stroke'] != 'none': self.color = getcolor(data['stroke']) if 'stroke-width' in data: if data['stroke-width'] != 'none': @@ -867,6 +869,27 @@ class svgHandler(xml.sax.ContentHandler): self.color = self.col self.width = self.lw + # apply group styles + if name == "g": + self.groupstyles.append([self.fill, self.color, self.width]) + if self.fill is None: + if "fill" not in data or data['fill'] != 'none': + # do not override fill if this item has specifically set a none fill + for groupstyle in reversed(self.groupstyles): + if groupstyle[0] is not None: + self.fill = groupstyle[0] + break + if self.color is None: + for groupstyle in reversed(self.groupstyles): + if groupstyle[1] is not None: + self.color = groupstyle[1] + break + if self.width is None: + for groupstyle in reversed(self.groupstyles): + if groupstyle[2] is not None: + self.width = groupstyle[2] + break + pathname = None if 'id' in data: pathname = data['id'][0] @@ -1366,6 +1389,7 @@ class svgHandler(xml.sax.ContentHandler): sh = self.applyTrans(sh) obj = self.doc.addObject("Part::Feature", pathname) obj.Shape = sh + self.format(obj) if self.currentsymbol: self.symbols[self.currentsymbol].append(obj) @@ -1503,6 +1527,8 @@ class svgHandler(xml.sax.ContentHandler): if name == "g" or name == "svg": _msg("closing group") self.grouptransform.pop() + if self.groupstyles: + self.groupstyles.pop() if name == "symbol": if self.doc.getObject("svgsymbols"): group = self.doc.getObject("svgsymbols")