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 update #1350

Merged
merged 7 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 21 additions & 3 deletions cadquery/occ_impl/exporters/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def getSVG(shape, opts=None):
hiddenColor = tuple(d["hiddenColor"])
showHidden = bool(d["showHidden"])
focus = float(d["focus"]) if d.get("focus") else None
fitView = bool(d["fitView"]) if d.get("fitView") else None

hlr = HLRBRep_Algo()
hlr.Add(shape.wrapped)
Expand Down Expand Up @@ -235,8 +236,25 @@ def getSVG(shape, opts=None):
# get bounding box -- these are all in 2D space
bb = Compound.makeCompound(hidden + visible).BoundingBox()

# Determine whether the user wants to fit the drawing to the bounding box
bb_scale = 0.75
if fitView:
bb_scale = 1.0

# Figure out which dimension to base the adjusted image size on
if width / bb.xlen < height / bb.ylen:
height = width * (bb.ylen / bb.xlen)
else:
width = height * (bb.xlen / bb.ylen)

image_width = width + (marginLeft * 2.0)
image_height = height + (marginTop * 2.0)
else:
image_width = width
image_height = height

# width pixels for x, height pixels for y
unitScale = min(width / bb.xlen * 0.75, height / bb.ylen * 0.75)
unitScale = min(width / bb.xlen * bb_scale, height / bb.ylen * bb_scale)

# compute amount to translate-- move the top left into view
(xTranslate, yTranslate) = (
Expand Down Expand Up @@ -278,8 +296,8 @@ def getSVG(shape, opts=None):
"visibleContent": visibleContent,
"xTranslate": str(xTranslate),
"yTranslate": str(yTranslate),
"width": str(width),
"height": str(height),
"width": str(image_width),
"height": str(image_height),
"textboxY": str(height - 30),
"uom": str(uom),
"axesIndicator": axesIndicator,
Expand Down
1 change: 1 addition & 0 deletions doc/importexport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ options are as follows.
* *hiddenColor* - Color of the line that hidden edges are drawn with.
* *showHidden* - Whether or not to show hidden lines.
* *focus* - If specified, creates a perspective SVG with the projector at the distance specified.
* *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.

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.
Below are examples with and without options set.
Expand Down
20 changes: 20 additions & 0 deletions tests/test_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,25 @@ def testSVGOptions(self):
"out.svg",
opt={
"width": 100,
"height": 50,
"marginLeft": 10,
"marginTop": 10,
"showAxes": False,
"projectionDir": (0, 0, 1),
"strokeWidth": 0.25,
"strokeColor": (255, 0, 0),
"hiddenColor": (0, 0, 255),
"showHidden": True,
"focus": 4,
"fitView": True,
},
)

exporters.export(
self._box(),
"out.svg",
opt={
"width": 50,
"height": 100,
"marginLeft": 10,
"marginTop": 10,
Expand All @@ -562,6 +581,7 @@ def testSVGOptions(self):
"hiddenColor": (0, 0, 255),
"showHidden": True,
"focus": 4,
"fitView": True,
},
)

Expand Down