From 8cd5d253be28c221c9f913c77e2075bbbe60c5ab Mon Sep 17 00:00:00 2001 From: Marius Stanciu Date: Sun, 16 May 2021 20:31:43 +0300 Subject: [PATCH] - fixed SVG import to show an error for SVG files that have SVG units not mm or cm or inch - for SVG files with cm units the scaling factor is multiplied by 10 - the app will signal failure when encountering the 'g' SVG element which is not supported and cancel the SVG import --- CHANGELOG.md | 6 ++++++ appParsers/ParseSVG.py | 9 ++++++++- app_Main.py | 5 ++++- camlib.py | 13 ++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35fb6f6f..cfdf7bcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ CHANGELOG for FlatCAM beta ================================================= +16.05.2021 + +- fixed SVG import to show an error for SVG files that have SVG units not mm or cm or inch +- for SVG files with cm units the scaling factor is multiplied by 10 +- the app will signal failure when encountering the 'g' SVG element which is not supported and cancel the SVG import + 7.05.2021 - fixed the Gerber parser to work for the case of having coordinates with negative values and the trailing zeros are removed and leading zeros are kept diff --git a/appParsers/ParseSVG.py b/appParsers/ParseSVG.py index 4b4b83aa..b42fd805 100644 --- a/appParsers/ParseSVG.py +++ b/appParsers/ParseSVG.py @@ -426,7 +426,10 @@ def getsvggeo(node, object_type, root=None, units='MM', res=64, factor=1.0, app= for child in node: subgeo = getsvggeo(child, object_type, root=root, units=units, res=res, factor=factor, app=app) if subgeo is not None: + if subgeo == 'fail': + return geo += subgeo + # Parse elif kind == 'path': log.debug("***PATH***") @@ -474,9 +477,13 @@ def getsvggeo(node, object_type, root=None, units='MM', res=64, factor=1.0, app= if ref is not None: geo = getsvggeo(ref, object_type, root=root, units=units, res=res, factor=factor, app=app) - elif kind in ['defs', 'namedview', 'format', 'type', 'title']: + elif kind in ['defs', 'namedview', 'format', 'type', 'title', 'desc']: log.warning('SVG Element not supported: %s. Skipping to next.' % kind) + elif kind in ['g']: + log.warning("SVG Element %s not supported and causing failure. Cancelling." % kind) + return "fail" + else: log.warning("Unknown kind: " + kind) geo = None diff --git a/app_Main.py b/app_Main.py index 59b7dbf0..bc333e86 100644 --- a/app_Main.py +++ b/app_Main.py @@ -10623,7 +10623,10 @@ class MenuFileHandlers(QtCore.QObject): units = self.defaults['units'].upper() def obj_init(geo_obj, app_obj): - geo_obj.import_svg(filename, obj_type, units=units) + res = geo_obj.import_svg(filename, obj_type, units=units) + if res == 'fail': + return 'fail' + geo_obj.multigeo = True with open(filename) as f: diff --git a/camlib.py b/camlib.py index 561b12fb..aadd3b3f 100644 --- a/camlib.py +++ b/camlib.py @@ -1164,13 +1164,24 @@ class Geometry(object): # Change origin to bottom left # h = float(svg_root.get('height')) # w = float(svg_root.get('width')) - h = svgparselength(svg_root.get('height'))[0] # TODO: No units support yet + svg_parsed_dims = svgparselength(svg_root.get('height')) + h = svg_parsed_dims[0] # TODO: No units support yet + svg_units = svg_parsed_dims[1] + if svg_units in ['em', 'ex', 'pt', 'px']: + self.app.log.error("camlib.Geometry.import_svg(). SVG units not supported: %s" % svg_units) + self.app.inform.emit("[ERROR_NOTCL] %s" % _("Failed.")) + return units = self.app.defaults['units'] if units is None else units res = self.app.defaults['geometry_circle_steps'] factor = svgparse_viewbox(svg_root) + if svg_units == 'cm': + factor *= 10 + geos = getsvggeo(svg_root, object_type, units=units, res=res, factor=factor, app=self.app) + if geos is None: + return 'fail' self.app.log.debug("camlib.Geometry.import_svg(). Finished parsing the SVG geometry.")