Skip to content

Commit 2a8b413

Browse files
committed
Reworked changes to use None instead of -1 to denote autofit
1 parent 1638fad commit 2a8b413

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

cadquery/occ_impl/exporters/svg.py

Lines changed: 11 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: Width of the resulting image (-1 to fit based on height).
137-
height: Height of the resulting image (-1 to fit based on width).
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"])
@@ -236,9 +241,9 @@ def getSVG(shape, opts=None):
236241
bb = Compound.makeCompound(hidden + visible).BoundingBox()
237242

238243
# Determine whether the user wants to fit the drawing to the bounding box
239-
if width <= 0 or height <= 0:
244+
if width == None or height == None:
240245
# Fit image to specified width (or height)
241-
if width <= 0:
246+
if width == None:
242247
width = (height - (2.0 * marginTop)) * (
243248
bb.xlen / bb.ylen
244249
) + 2.0 * marginLeft

doc/importexport.rst

Lines changed: 2 additions & 3 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.
@@ -216,7 +216,6 @@ options are as follows.
216216
* *hiddenColor* - Color of the line that hidden edges are drawn with.
217217
* *showHidden* - Whether or not to show hidden lines.
218218
* *focus* - If specified, creates a perspective SVG with the projector at the distance specified.
219-
* *fitView* - If specified, will attempt to fit the height and width of the image to the contents. The ``marginLeft`` and ``marginTop`` options will be respected and will add to the total image size so that the margin can be duplicated at the bottom right. The ``width`` or ``height`` options that were specified will be overridden, but the largest dimension should be preserved except when margins are used.
220219

221220
The options are passed to the exporter in a dictionary, and can be left out to force the SVG to be created with default options.
222221
Below are examples with and without options set.

tests/test_exporters.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def testSVGOptions(self):
552552
"out.svg",
553553
opt={
554554
"width": 100,
555-
"height": 50,
555+
"height": None,
556556
"marginLeft": 10,
557557
"marginTop": 10,
558558
"showAxes": False,
@@ -562,15 +562,14 @@ def testSVGOptions(self):
562562
"hiddenColor": (0, 0, 255),
563563
"showHidden": True,
564564
"focus": 4,
565-
"fitView": True,
566565
},
567566
)
568567

569568
exporters.export(
570569
self._box(),
571570
"out.svg",
572571
opt={
573-
"width": 50,
572+
"width": None,
574573
"height": 100,
575574
"marginLeft": 10,
576575
"marginTop": 10,
@@ -581,7 +580,6 @@ def testSVGOptions(self):
581580
"hiddenColor": (0, 0, 255),
582581
"showHidden": True,
583582
"focus": 4,
584-
"fitView": True,
585583
},
586584
)
587585

0 commit comments

Comments
 (0)