QCAD
Open Source 2D CAD
Exporting a Drawing to SVG

SVG Export in QCAD is completely implemented in ECMAScript with one of the main classes being SvgExporterPG:

1 // include class SvgExporterPG:
2 include("scripts/Pro/ImportExport/SvgExporter/SvgExporterPG.js");

The export can be done through funciton 'exportFile':

1 var exporter = new SvgExporterPG(document, { "scale": "10:1"});
2 exporter.exportFile("example.svg");

Complete code:

1 //! [include]
2 // include class SvgExporterPG:
3 include("scripts/Pro/ImportExport/SvgExporter/SvgExporterPG.js");
4 //! [include]
5 
6 //! [init]
7 // init application name:
8 qApp.applicationName = "MyApplication";
9 
10 //! [document]
11 var storage = new RMemoryStorage();
12 var spatialIndex = new RSpatialIndexNavel();
13 var document = new RDocument(storage, spatialIndex);
14 document.setUnit(RS.Millimeter);
15 //! [document]
16 
17 //! [operation]
18 // create an operation:
19 var operation = new RAddObjectsOperation();
20 
21 // corner point of the triangle::
22 var p1 = new RVector(0, 0);
23 var p2 = new RVector(10, 0);
24 var p3 = new RVector(5, Math.sin(RMath.deg2rad(60))*10);
25 
26 // add line entity to operation:
27 operation.addObject(new RLineEntity(document, new RLineData(p1, p2)));
28 operation.addObject(new RLineEntity(document, new RLineData(p2, p3)));
29 operation.addObject(new RLineEntity(document, new RLineData(p3, p1)));
30 
31 // apply operation to document:
32 operation.apply(document);
33 //! [operation]
34 
35 //! [export]
36 var exporter = new SvgExporterPG(document, { "scale": "10:1"});
37 exporter.exportFile("example.svg");
38 //! [export]
39