Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Exporting to an SVG file

Drawings SDK supports exporting .dwg and .dxf files to SVG format (an .svg file). The export module is a single support module for the Drawings SDK, and it is available in dynamic and static forms for Windows, Linux, and macOS (x86 and x64 versions). All source files are available in the release packages.

The following components are available:

  • TD_SvgExport.tx - A .tx support module for SVG export.
    Location: Kernel\Exports\SvgExport\
    This module should be loaded by the user application to get access to SVG export functionality. The main class is "SvgExportModule".
  • SVG export example application - Contains sample code for .dwg files.
    Location: Drawing\Examples\OdSvgExportEx\

SVG export

The SVG export module supports saving the contents of either a single viewport or a whole drawing to an .svg file. The module renders the drawing with a special SVG export graphics device, which is derived from the OdGsDevice class and provides a set of rendering properties listed in the following table.

Property name Property data type Description
BackGround ODCOLORREF Background color. If set to non-white, a rectangle of given color will be drawn behind the drawing, simulating a background.
ExplodeShxTexts bool Explode shx text flag. If the flag is switched on, shx text is exploded into lines and arcs (polylines algorithm). In the other case, shx text is rendered using the font family.
DefaultImageExt OdString Default image format (PNG and JPEG format are supported). For more information about images in .svg files, visit http://www.w3.org/TR/SVG11/struct.html#ImageElement.
EnableGouraudShading bool Determines whether the gouraud filter is used for hatches. If the flag is on, gradient hatches are exported into triangles with shading filters; in the other case triangles get an average color of the gradient. Using the gouraud filter can cause a significant increase in output file size and poor performance of the SVG interpreter.
GenericFontFamily OdString Generic font family used for displaying text in the output .svg file. Additional information about using fonts in .svg files can be found at http://www.w3.org/TR/SVG11/fonts.html#FontFaceElement.
ImageBase OdString Location in the local computer file system where image files will be physically allocated.
ImageUrl OdString Prefix added to an image path stored in the <image> tag, which is used when an .svg file is used on a website.
LineWeightScale double Scale ratio of lineweights. Scale ratio depends on the scale of the original .dwg file. It can be set by the user to regulate the lineweight scale in the output .svg file.
Output OdStreamBufPtr Stream for the output .svg file.
Precision OdInt32 Number of digits in float values (6 by default).
ShxLineWeight double An additional lineweight scale applied to shx text only. Default value is 1.0.
Tolerance double Geometric tolerance for approximation (used when the approximation is applicable). Default value is 0.5 (in pixels).
UseHLR bool Flag responsible for hidden line removal algorithm usage. If flag is on, the algorithm is used.

Parameters are stored in a dictionary, which belongs to the device object. To get the parameters dictionary, use the properties() method of the device class. To set an appropriate parameter, use putAt() method.

For example, to create a graphics device and set the output file in which to save:


      OdGsModulePtr pModule = ::odrxDynamicLinker()->loadModule(OdSvgExportModuleName, false);
      OdGsDevicePtr dev = pModule->createDevice();
      if (!dev.isNull())
      {
        // Create the output file
        OdStreamBufPtr file = odSystemServices()->createFile("output.svg", Oda::kFileWrite, Oda::kShareDenyNo, Oda::kCreateAlways);
        // Setup device output stream to the output file
        dev->properties()->putAt("Output", file.get());
      }
    

Therefore, to export a drawing to an .svg file, you need to create an SVG export graphics device, set the required rendering properties, set up the layout to render, define the device output rectangle, and then initiate the rendering process using OdGsDevice::update(). For setting the device output rectangle, the OdGsDevice::OnSize() method is used; note that SVG coordinates are upside down, for example, to define the rectangle 1024*768 use OdGsDCRect(0, 1024, 768, 0).

Note that the SVG export module doesn't support exporting of layouts and layers: only the active layout and only visible layers can be exported from a .dwg file.

Example application

The OdSvgExportEx sample application illustrates exporting a .dwg/.dxf file to an .svg file.

SVG resources

For more information about the SVG format: http://www.w3.org/Graphics/SVG/About.html.

For viewing .svg files, you can use various SVG viewers, for example, Inkscape at http://inkscape.org.

Copyright © 2002 – 2022. Open Design Alliance. All rights reserved.