Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SVG] The user should be able to provide the units of measurement for an svg #1325

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions cadquery/occ_impl/exporters/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -125,12 +103,14 @@ 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.

: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.
Expand Down Expand Up @@ -167,7 +147,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"])
Expand Down Expand Up @@ -289,14 +269,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()