From 039a2dd4dc6f7fa7b26346d34887029883998396 Mon Sep 17 00:00:00 2001 From: grbd Date: Tue, 22 Mar 2016 23:22:02 +0000 Subject: [PATCH] Made scale_factor optional for cli, added more comments, removed redundant code --- FlatCAMApp.py | 24 ++++++++++++++---------- camlib.py | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/FlatCAMApp.py b/FlatCAMApp.py index 2f540588..6593d9a2 100644 --- a/FlatCAMApp.py +++ b/FlatCAMApp.py @@ -1710,7 +1710,7 @@ class App(QtCore.QObject): self.inform.emit("Project copy saved to: " + self.project_filename) - def export_svg(self, obj_name, filename, outname=None): + def export_svg(self, obj_name, filename, scale_factor=0.00): """ Exports a Geometry Object to a SVG File @@ -1726,11 +1726,7 @@ class App(QtCore.QObject): return "Could not retrieve object: %s" % obj_name with self.proc_container.new("Exporting SVG") as proc: - exported_svg = obj.export_svg() - - # Sometimes obj.solid_geometry can be a list instead of a Shapely class - # Make sure we see it as a Shapely Geometry class - geom = cascaded_union(obj.flatten()) + exported_svg = obj.export_svg(scale_factor=scale_factor) # Determine bounding area for svg export bounds = obj.bounds() @@ -2206,9 +2202,16 @@ class App(QtCore.QObject): return str(self.collection.get_names()) - def export_svg(name, filename): + def export_svg(name, filename, *args): + a, kwa = h(*args) + types = {'scale_factor': float} - self.export_svg(str(name), str(filename)) + for key in kwa: + if key not in types: + return 'Unknown parameter: %s' % key + kwa[key] = types[key](kwa[key]) + + self.export_svg(str(name), str(filename), **kwa) def import_svg(filename, *args): a, kwa = h(*args) @@ -3329,9 +3332,10 @@ class App(QtCore.QObject): 'export_svg': { 'fcn': export_svg, 'help': "Export a Geometry Object as a SVG File\n" + - "> export_svg \n" + + "> export_svg [-scale_factor <0.0 (float)>]\n" + " name: Name of the geometry object to export.\n" + - " filename: Path to the file to export." + " filename: Path to the file to export.\n" + + " scale_factor: Multiplication factor used for scaling line widths during export." }, 'open_gerber': { 'fcn': open_gerber, diff --git a/camlib.py b/camlib.py index 2ae8471a..23bf1bcf 100644 --- a/camlib.py +++ b/camlib.py @@ -869,18 +869,25 @@ class Geometry(object): """ self.solid_geometry = [cascaded_union(self.solid_geometry)] - def export_svg(self): + def export_svg(self, scale_factor=0.00): """ Exports the Gemoetry Object as a SVG Element :return: SVG Element """ - # Sometimes self.solid_geometry can be a list instead of a Shapely class - # Make sure we see it as a Shapely Geometry class + # Make sure we see a Shapely Geometry class and not a list geom = cascaded_union(self.flatten()) + # scale_factor is a multiplication factor for the SVG stroke-width used within shapely's svg export + + # If 0 or less which is invalid then default to 0.05 + # This value appears to work for zooming, and getting the output svg line width + # to match that viewed on screen with FlatCam + if scale_factor <= 0: + scale_factor = 0.05 + # Convert to a SVG - svg_elem = geom.svg(scale_factor=0.05) + svg_elem = geom.svg(scale_factor=scale_factor) return svg_elem class ApertureMacro: @@ -3326,17 +3333,26 @@ class CNCjob(Geometry): self.create_geometry() - def export_svg(self): + def export_svg(self, scale_factor=0.00): """ Exports the CNC Job as a SVG Element - :return: SVG Element + :scale_factor: float + :return: SVG Element string """ + # scale_factor is a multiplication factor for the SVG stroke-width used within shapely's svg export + # If not specified then try and use the tool diameter + # This way what is on screen will match what is outputed for the svg + # This is quite a useful feature for svg's used with visicut - # This appears to match up distance wise with inkscape - scale = self.options['tooldia'] / 2 - if scale == 0: - scale = 0.05 + if scale_factor <= 0: + scale_factor = self.options['tooldia'] / 2 + + # If still 0 then defailt to 0.05 + # This value appears to work for zooming, and getting the output svg line width + # to match that viewed on screen with FlatCam + if scale_factor == 0: + scale_factor = 0.05 # Seperate the list of cuts and travels into 2 distinct lists # This way we can add different formatting / colors to both @@ -3360,9 +3376,9 @@ class CNCjob(Geometry): # It's better to have the travels sitting underneath the cuts for visicut svg_elem = "" if travels: - svg_elem = travelsgeom.svg(scale_factor=scale, stroke_color="#F0E24D") + svg_elem = travelsgeom.svg(scale_factor=scale_factor, stroke_color="#F0E24D") if cuts: - svg_elem += cutsgeom.svg(scale_factor=scale, stroke_color="#5E6CFF") + svg_elem += cutsgeom.svg(scale_factor=scale_factor, stroke_color="#5E6CFF") return svg_elem