SVG Export via Code-Driven File IO - specify page size and scale

It is great to see the new Code-Driven File IO which came in with Rhino 8 by @stevebaer

I can successfully use it to export a headless document as a SVG file.
However unlike other formats I cannot see a FileSvgWriteOptions class to help me set the options for export.

I’d like to be able to specify the SVG page size and scale factor being used for the export.

Is there way I can specify these options programatically using RhinoCommon? @wim I think you might have experienced this before?

Hoping the Code-Driven File IO changes things since this.
Other requests for this

We should update the SVG export for consistency on this feature, but there is already a way to write SVGs with options in RhinoCommon. See the ViewCapture.CaptureToSVG function

https://developer.rhino3d.com/api/rhinocommon/rhino.display.viewcapture/capturetosvg

Thanks @stevebaer that would be great, does ViewCapture.CaptureToSVG work for a Headless document?

That’s a darn good question. I’m not sure; I would need to test.

A headless document has an empty collection of Views, so unfortunately that approach will not work for us.

SVG Export seems so close - the geometry is there and the export works - we only need to control page size and scale.

I tried scaling and offseting the headless document to suit the SVG export default settings but while i can get the scale roughly correct, I cannot figure out the offset its using - everything seems to get offset to the bottom right quadrant. (-12,-12)>(12,12) becomes (500,-2000)>(1700,-500). I suspect the scale factor is closers to 96dpi but the SVG file is always (0,0)>(2400,2400)

Really looking to just have control of output page size and then use Scale To Fit.

Would a peek at the export code enlighten us what settings its trying to use and why its offset?

tmpF646.3dm (2.0 MB)
tmpF646

Thank you for your advice on this. @stevebaer @CallumSykes

@jnash and I have had another look at this.
We have a work around which will get us by for now.

At present SVG export via RhinoCommon has no control over the exported area of the document nor the destination page size in SVG.

However, if you can scale your document geometry to -20,-20 > 20,20 you can export the whole geometry and then modify resulting SVG Width/Height and viewbox dimensions you can achieve correct exports (scale factor is 60).

Here is a Script which demonstrates the workaround.

using System;
using System.IO;
using Rhino;
using Rhino.Geometry;

// create a new doc headless
var doc = RhinoDoc.CreateHeadless(null);
// create geometry between -20,-20 and 20,20
doc.Objects.AddLine(new Line(new Point3d(-20, 20,0), new Point3d(20, 0 ,0)));
doc.Objects.AddLine(new Line(new Point3d(-20, 0 ,0), new Point3d(20, 20,0)));
doc.Objects.AddLine(new Line(new Point3d(-20,-20,0), new Point3d(20, 0 ,0)));
doc.Objects.AddLine(new Line(new Point3d(-20, 0 ,0), new Point3d(20,-20,0)));
doc.Objects.AddCircle(new Rhino.Geometry.Circle(Point3d.Origin,20));


var outputFolder = @"C:\Temp\"; // check this exists on disk
Directory.CreateDirectory(outputFolder);

// 3dm exports perfectly
doc.Export(Path.Combine(outputFolder,"test.3dm")); 

// svg export is scaled by 60 from -20,-20 > 20,20 to 0,0 > 2400,2400
// There is no control via RhinoCommon for these bounding boxes or scaling. 
doc.Export(Path.Combine(outputFolder,"test.svg"));

test

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" width="2400pt" height="2400pt" viewBox="0 0 2400 2400" xmlns="http://www.w3.org/2000/svg">
  <defs />
  <g id="Default">
    <path d="M0.48,2399.52 L2399.52,1200" stroke="#000000" stroke-width="4.1666665" stroke-linecap="round" stroke-linejoin="round" fill="none" />
    <path d="M0.48,1200 L2399.52,0.48" stroke="#000000" stroke-width="4.1666665" stroke-linecap="round" stroke-linejoin="round" fill="none" />
    <path d="M0.48,0.48 L2399.52,1200" stroke="#000000" stroke-width="4.1666665" stroke-linecap="round" stroke-linejoin="round" fill="none" />
    <circle cx="1200" cy="1200" r="1200" stroke="#000000" stroke-width="4.167" fill="none" />
    <path d="M0.48,1200 L2399.52,2399.52" stroke="#000000" stroke-width="4.1666665" stroke-linecap="round" stroke-linejoin="round" fill="none" />
  </g>
</svg>

It would be great to have Code Driven Export for SVG down the road but this will work for now.

2 Likes