Skip to content

Commit 176ff70

Browse files
committed
Added fitView option to SVG export
1 parent 7143ead commit 176ff70

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

cadquery/occ_impl/exporters/svg.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def getSVG(shape, opts=None):
180180
hiddenColor = tuple(d["hiddenColor"])
181181
showHidden = bool(d["showHidden"])
182182
focus = float(d["focus"]) if d.get("focus") else None
183+
fitView = bool(d["fitView"]) if d.get("fitView") else None
183184

184185
hlr = HLRBRep_Algo()
185186
hlr.Add(shape.wrapped)
@@ -235,8 +236,15 @@ def getSVG(shape, opts=None):
235236
# get bounding box -- these are all in 2D space
236237
bb = Compound.makeCompound(hidden + visible).BoundingBox()
237238

239+
# Determine whether the user wants to fit the drawing to the bounding box
240+
bb_scale = 0.75
241+
if fitView:
242+
bb_scale = 1.0
243+
width = bb.xlen
244+
height = bb.ylen
245+
238246
# width pixels for x, height pixels for y
239-
unitScale = min(width / bb.xlen * 0.75, height / bb.ylen * 0.75)
247+
unitScale = min(width / bb.xlen * bb_scale, height / bb.ylen * bb_scale)
240248

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

doc/importexport.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ 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 should be set to 0 or the object will fall outside the viewport. The ``width`` and ``height`` options that were specified will be overridden, which can cause unexpected final image sizes.
219220

220221
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.
221222
Below are examples with and without options set.

tests/test_exporters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ def testSVGOptions(self):
562562
"hiddenColor": (0, 0, 255),
563563
"showHidden": True,
564564
"focus": 4,
565+
"fitView": True,
565566
},
566567
)
567568

0 commit comments

Comments
 (0)