From ce240feb45eb4060df01ac91a0a028d0ad2818ab Mon Sep 17 00:00:00 2001 From: John Skottis Date: Tue, 25 Apr 2023 12:08:48 +0100 Subject: [PATCH 1/3] explicit unitOfMeasure parameter --- cadquery/occ_impl/exporters/svg.py | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/cadquery/occ_impl/exporters/svg.py b/cadquery/occ_impl/exporters/svg.py index d2b9c421c..818e89ee7 100644 --- a/cadquery/occ_impl/exporters/svg.py +++ b/cadquery/occ_impl/exporters/svg.py @@ -59,28 +59,6 @@ class UNITS: IN = "in" -def guessUnitOfMeasure(shape): - """ - Guess the unit of measure of a shape. - """ - bb = BoundBox._fromTopoDS(shape.wrapped) - - dimList = [bb.xlen, bb.ylen, bb.zlen] - # no real part would likely be bigger than 10 inches on any side - if max(dimList) > 10: - return UNITS.MM - - # no real part would likely be smaller than 0.1 mm on all dimensions - if min(dimList) < 0.1: - return UNITS.IN - - # no real part would have the sum of its dimensions less than about 5mm - if sum(dimList) < 10: - return UNITS.IN - - return UNITS.MM - - def makeSVGedge(e): """ Creates an SVG edge from a OCCT edge. @@ -125,7 +103,7 @@ def getPaths(visibleShapes, hiddenShapes): return (hiddenPaths, visiblePaths) -def getSVG(shape, opts=None): +def getSVG(shape, unitOfMeasure = UNITS.MM, opts=None): """ Export a shape to SVG text. @@ -167,7 +145,7 @@ def getSVG(shape, opts=None): d.update(opts) # need to guess the scale and the coordinate center - uom = guessUnitOfMeasure(shape) + uom = unitOfMeasure width = float(d["width"]) height = float(d["height"]) @@ -289,14 +267,14 @@ def getSVG(shape, opts=None): return svg -def exportSVG(shape, fileName: str, opts=None): +def exportSVG(shape, fileName: str, unitOfMeasure = UNITS.MM, opts=None): """ Accept a cadquery shape, and export it to the provided file TODO: should use file-like objects, not a fileName, and/or be able to return a string instead export a view of a part to svg """ - svg = getSVG(shape.val(), opts) + svg = getSVG(shape.val(), unitOfMeasure, opts) f = open(fileName, "w") f.write(svg) f.close() From 0acd70560d9975ef445beaddbf62188caea4b406 Mon Sep 17 00:00:00 2001 From: John Skottis Date: Tue, 25 Apr 2023 12:19:41 +0100 Subject: [PATCH 2/3] Add documentation to getSVG --- cadquery/occ_impl/exporters/svg.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cadquery/occ_impl/exporters/svg.py b/cadquery/occ_impl/exporters/svg.py index 818e89ee7..0a31a099d 100644 --- a/cadquery/occ_impl/exporters/svg.py +++ b/cadquery/occ_impl/exporters/svg.py @@ -109,6 +109,8 @@ def getSVG(shape, unitOfMeasure = UNITS.MM, opts=None): :param shape: A CadQuery shape object to convert to an SVG string. :type Shape: Vertex, Edge, Wire, Face, Shell, Solid, or Compound. + :param unitOfMeasure: The unit of measurement to be used for the svg (mm/inch). + :type UNITS: MM, or INCH. :param opts: An options dictionary that influences the SVG that is output. :type opts: Dictionary, keys are as follows: width: Document width of the resulting image. From f63eccf304b0c4ccec1c902fa218e324a34a598d Mon Sep 17 00:00:00 2001 From: John Date: Tue, 25 Apr 2023 12:28:47 +0100 Subject: [PATCH 3/3] Fix formatting --- cadquery/occ_impl/exporters/svg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cadquery/occ_impl/exporters/svg.py b/cadquery/occ_impl/exporters/svg.py index 0a31a099d..b3f907171 100644 --- a/cadquery/occ_impl/exporters/svg.py +++ b/cadquery/occ_impl/exporters/svg.py @@ -103,7 +103,7 @@ def getPaths(visibleShapes, hiddenShapes): return (hiddenPaths, visiblePaths) -def getSVG(shape, unitOfMeasure = UNITS.MM, opts=None): +def getSVG(shape, unitOfMeasure=UNITS.MM, opts=None): """ Export a shape to SVG text. @@ -269,7 +269,7 @@ def getSVG(shape, unitOfMeasure = UNITS.MM, opts=None): return svg -def exportSVG(shape, fileName: str, unitOfMeasure = UNITS.MM, opts=None): +def exportSVG(shape, fileName: str, unitOfMeasure=UNITS.MM, opts=None): """ Accept a cadquery shape, and export it to the provided file TODO: should use file-like objects, not a fileName, and/or be able to return a string instead