Skip to content

Commit 63afebe

Browse files
authored
Merge pull request #1350 from CadQuery/svg_update
Svg update
2 parents fa0abf0 + 2a8b413 commit 63afebe

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

cadquery/occ_impl/exporters/svg.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def getSVG(shape, opts=None):
133133
:type Shape: Vertex, Edge, Wire, Face, Shell, Solid, or Compound.
134134
:param opts: An options dictionary that influences the SVG that is output.
135135
:type opts: Dictionary, keys are as follows:
136-
width: Document width of the resulting image.
137-
height: Document height of the resulting image.
136+
width: Width of the resulting image (None to fit based on height).
137+
height: Height of the resulting image (None to fit based on width).
138138
marginLeft: Inset margin from the left side of the document.
139139
marginTop: Inset margin from the top side of the document.
140140
projectionDir: Direction the camera will view the shape from.
@@ -169,8 +169,13 @@ def getSVG(shape, opts=None):
169169
# need to guess the scale and the coordinate center
170170
uom = guessUnitOfMeasure(shape)
171171

172-
width = float(d["width"])
173-
height = float(d["height"])
172+
# Handle the case where the height or width are None
173+
width = d["width"]
174+
if width != None:
175+
width = float(d["width"])
176+
height = d["height"]
177+
if d["height"] != None:
178+
height = float(d["height"])
174179
marginLeft = float(d["marginLeft"])
175180
marginTop = float(d["marginTop"])
176181
projectionDir = tuple(d["projectionDir"])
@@ -235,8 +240,22 @@ def getSVG(shape, opts=None):
235240
# get bounding box -- these are all in 2D space
236241
bb = Compound.makeCompound(hidden + visible).BoundingBox()
237242

238-
# width pixels for x, height pixels for y
239-
unitScale = min(width / bb.xlen * 0.75, height / bb.ylen * 0.75)
243+
# Determine whether the user wants to fit the drawing to the bounding box
244+
if width == None or height == None:
245+
# Fit image to specified width (or height)
246+
if width == None:
247+
width = (height - (2.0 * marginTop)) * (
248+
bb.xlen / bb.ylen
249+
) + 2.0 * marginLeft
250+
else:
251+
height = (width - 2.0 * marginLeft) * (bb.ylen / bb.xlen) + 2.0 * marginTop
252+
253+
# width pixels for x, height pixels for y
254+
unitScale = (width - 2.0 * marginLeft) / bb.xlen
255+
else:
256+
bb_scale = 0.75
257+
# width pixels for x, height pixels for y
258+
unitScale = min(width / bb.xlen * bb_scale, height / bb.ylen * bb_scale)
240259

241260
# compute amount to translate-- move the top left into view
242261
(xTranslate, yTranslate) = (

doc/importexport.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ Exporting SVG
205205
The SVG exporter has several options which can be useful for achieving the desired final output. Those
206206
options are as follows.
207207

208-
* *width* - Document width of the resulting image.
209-
* *height* - Document height of the resulting image.
208+
* *width* - Width of the resulting image (None to fit based on height).
209+
* *height* - Height of the resulting image (None to fit based on width).
210210
* *marginLeft* - Inset margin from the left side of the document.
211211
* *marginTop* - Inset margin from the top side of the document.
212212
* *projectionDir* - Direction the camera will view the shape from.

tests/test_exporters.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,24 @@ def testSVGOptions(self):
552552
"out.svg",
553553
opt={
554554
"width": 100,
555+
"height": None,
556+
"marginLeft": 10,
557+
"marginTop": 10,
558+
"showAxes": False,
559+
"projectionDir": (0, 0, 1),
560+
"strokeWidth": 0.25,
561+
"strokeColor": (255, 0, 0),
562+
"hiddenColor": (0, 0, 255),
563+
"showHidden": True,
564+
"focus": 4,
565+
},
566+
)
567+
568+
exporters.export(
569+
self._box(),
570+
"out.svg",
571+
opt={
572+
"width": None,
555573
"height": 100,
556574
"marginLeft": 10,
557575
"marginTop": 10,

0 commit comments

Comments
 (0)