Made scale_factor optional for cli, added more comments, removed redundant code

This commit is contained in:
grbd
2016-03-22 23:22:02 +00:00
parent ee43d8b920
commit 039a2dd4dc
2 changed files with 42 additions and 22 deletions

View File

@@ -1710,7 +1710,7 @@ class App(QtCore.QObject):
self.inform.emit("Project copy saved to: " + self.project_filename) 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 Exports a Geometry Object to a SVG File
@@ -1726,11 +1726,7 @@ class App(QtCore.QObject):
return "Could not retrieve object: %s" % obj_name return "Could not retrieve object: %s" % obj_name
with self.proc_container.new("Exporting SVG") as proc: with self.proc_container.new("Exporting SVG") as proc:
exported_svg = obj.export_svg() exported_svg = obj.export_svg(scale_factor=scale_factor)
# 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())
# Determine bounding area for svg export # Determine bounding area for svg export
bounds = obj.bounds() bounds = obj.bounds()
@@ -2206,9 +2202,16 @@ class App(QtCore.QObject):
return str(self.collection.get_names()) 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): def import_svg(filename, *args):
a, kwa = h(*args) a, kwa = h(*args)
@@ -3329,9 +3332,10 @@ class App(QtCore.QObject):
'export_svg': { 'export_svg': {
'fcn': export_svg, 'fcn': export_svg,
'help': "Export a Geometry Object as a SVG File\n" + 'help': "Export a Geometry Object as a SVG File\n" +
"> export_svg <name> <filename>\n" + "> export_svg <name> <filename> [-scale_factor <0.0 (float)>]\n" +
" name: Name of the geometry object to export.\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': { 'open_gerber': {
'fcn': open_gerber, 'fcn': open_gerber,

View File

@@ -869,18 +869,25 @@ class Geometry(object):
""" """
self.solid_geometry = [cascaded_union(self.solid_geometry)] 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 Exports the Gemoetry Object as a SVG Element
:return: SVG Element :return: SVG Element
""" """
# Sometimes self.solid_geometry can be a list instead of a Shapely class # Make sure we see a Shapely Geometry class and not a list
# Make sure we see it as a Shapely Geometry class
geom = cascaded_union(self.flatten()) 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 # Convert to a SVG
svg_elem = geom.svg(scale_factor=0.05) svg_elem = geom.svg(scale_factor=scale_factor)
return svg_elem return svg_elem
class ApertureMacro: class ApertureMacro:
@@ -3326,17 +3333,26 @@ class CNCjob(Geometry):
self.create_geometry() self.create_geometry()
def export_svg(self): def export_svg(self, scale_factor=0.00):
""" """
Exports the CNC Job as a SVG Element 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 if scale_factor <= 0:
scale = self.options['tooldia'] / 2 scale_factor = self.options['tooldia'] / 2
if scale == 0:
scale = 0.05 # 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 # Seperate the list of cuts and travels into 2 distinct lists
# This way we can add different formatting / colors to both # 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 # It's better to have the travels sitting underneath the cuts for visicut
svg_elem = "" svg_elem = ""
if travels: 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: 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 return svg_elem