Convert CAD / DWG to PDF in C++

CAD Module is a new optional add-on utility which can be used with Apryse SDK 7.0 or later to convert CAD documents to PDF while preserving layers without any external dependencies. Supported CAD formats include DWG, DWF, DXF, and DGN can be directly converted to PDF using the PDF.Convert.FromCAD method with this module. Beta support for RVT format is also available on Windows.

You can find more details about how to install CAD Module.

Supported formats

File Format

Supported Versions

DGN

V7, V8

DXF, DWF, DWG

AutoCAD R12 and newer

RVT *

All ✓

* RVT format is currently on beta support only for Microsoft Windows

Usage

To use the CAD Module, you must use the PDFNet.AddResourceSearchPath method with the path to the module's Lib/ folder to include the module files.

Requires the CAD module add-on

1PDFNet::Initialize();
2PDFNet::AddResourceSearchPath("relative/path/to/Lib/");
3if(!CADModule::IsModuleAvailable())
4{
5 cout << "Apryse SDK CAD module not available." << endl;
6}

If the module has been successfully added, you can use the Convert.FromCAD method. This method requires that you pass in a PDFDoc object that will append converted pages to the specified PDF document.

1PDFDoc doc;
2Convert::FromCAD(doc, input_file_path + input_file_name, NULL);
3doc.Save(output_file_path + output_file_name, SDF::SDFDoc::e_linearized, NULL);

Conversion options

You can also optionally provide a CADConvertOptions as an argument to change the PageHeight and PageWidth attributes for the conversion of the CAD document. If your document is of the RVT format, you can also set the RasterDPI and Sheets attributes to control the rasterization resolution and list of sheets to be converted (RVT beta support currently only available on Windows). You must call the AddSheets method multiple times depending on the number of sheets you would like to add.

1CADConvertOptions opts;
2opts.SetPageWidth(800);
3opts.SetPageHeight(600);
4
5// RVT documents only:
6opts.SetRasterDPI(150);
7opts.AddSheets("sheet_id1");
8
9Convert::FromCAD(doc, input_file_path + input_file_name, &opts);

Layers

By default, layers present in the original CAD drawing will be preserved as Optional Content Groups (OCGs) in the PDF. OCGs can be extracted, edited, manipulated, and removed with Apryse SDK. They can also be rendered into a viewer where users can interact with them.

Sample

The CAD Module ships with the CAD2PDFTest sample which can be used to test the conversion functionality using provided test files or your own documents. The sample first initializes PDFNet, which is required for all Apryse SDK functionality, then uses PDFNet.AddResourceSearchPath to include the CAD Module. The same lines of code can be used to integrate the module in your application or project. Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.

The CAD2PDFTest sample also shows how to check if a file is of RVT file format and uses CADConvertOptions to convert to PDF (currently only supported on Windows).

Troubleshooting

Fonts

For best results, the CAD Module requires access to the fonts (SHX, RSC and TTF) used in the drawing. If the requisite fonts are not embedded in the drawing and not installed on your server, the CAD Module will use its own bundled fonts (located in the in fonts/ folder within the CAD module package) as substitutes for the originals. The specifics of the substitution mappings are defined in a file called substitution.json (which is also located in the fonts/ folder). If necessary, this file can be customized to define different font mappings or add new mappings. The JSON has following entries:

  • fontFileMappings:
    • This is where direct substitutions based on font file name are specified.
    • For each font you want to replace, you can define multiple fonts in list form. CAD Module searches fonts one by one from the beginning of the list and choose the first font that is found.
  • fallbackFonts:
    • This is where fallback fonts are defined.
    • Fallback fonts for monospaced and proportional font are defined separately.
    • You can define/customize fallback fonts for each language as needed; default ones are used otherwise.

An alternative way of performing direct substitutions for a font is to provide a font file and name it as "<target_font_name>_fallback.[ttf | ttc |otf]". For example, if you want to map iso8.shx to a TTF font, then name your TTF file to iso8_fallback.ttf and place it in the location discoverable by the CAD Module (you can use PDFNet.AddResourceSearchPath() to specify the location of the font). Note that the fallback font name must be in lowercase. This method takes priority over the JSON file method.

Fonts for DGN when resource files (.rsc) are not present

There are times when a DGN CAD file uses fonts that are packaged in resource files (.rsc), but these files are not available where the DGN file is loaded from.

Without the .rsc files present alongside the .dgn, there is no way to get the equivalent font data for the fonts used in the DGN file which are specified using font ID numbers. Font ID numbers may be different from the known standard ones which can be mapped internally to known font names. In this case, the DGN may, in some cases, have font names provided as hints. We can map these names to TrueType font file names.

For example, to map the names Arial, Arial Narrow, and Arial Bold, you can add the following lines to the substitutions.json in the fontFileMappings section:

JSON

1"arial.shx": [ "arial.ttf" ],
2"Arial Narrow.shx": [ "arialn.ttf" ],
3"Arial Bold.shx": [ "arialb.ttf" ]

You can then copy, or make available, the corresponding files (e.g. arialn.ttf, arialb.ttf, and arial.ttf) under the fonts folder which is in the module binary’s folder, in the same location where the .DGN file is located. Alternatively, you can use PDFNet.AddResourceSearchPath() to specify the location of the files.

OpenGL

On Linux CADModule uses OpenGL API for rendering 3D objects. In a headless server environment (i.e., without monitors/graphical drivers) implementation of OpenGL specification needs to be provided. One such implementation is Mesa. On headless Linux you may need to install libgl1-mesa-dev, libx11-dev and libglu1-mesa-dev.

Custom Plot Styles

The CAD module supports optionally applying some style changes when converting a CAD file to PDF.

This section describes the format for the the Plot-styles JSON object which can be provided by the user to specify custom plot styles when converting a CAD drawing to PDF.

Plot styles JSON location

To provide plot style changes/override, a Plot-styles JSON object can be authored and saved in a .json file. The CAD module tries to load the settings for plot styles from the following locations:

  • The CAD file folder
  • The CAD Module binary's folder
  • The current working directory of the process

The name of the plot style settings JSON file would be:

  • cadstyledwg.json for DWG files.
  • cadstyledgn.json for DGN files.
  • cadstyle.json if neither of the above are found.

Example of Plot-styles object in JSON

The following is a sample of using the plot-styles object.

JSON

1"Plot-styles": { // The plot styles object. If missing, no plot styles will be applied.
2 "Named-styles": { // We can have either the Named-styles object or Direct-overrides object (see below). Priority is for Direct-overrides if both are present.
3 "Input-stb-file": "C:/Temp/test.stb", // Pathname of an .stb file (AutoCAD styles table.). If present will try to load and apply styles from this file. It takes priority over the Styles object below.
4 "Styles": { // List of named styles. Named styles can be used together with Layer-to-style-map below to assign a style by name to layers.
5 "Normal": { // A style name
6 "ColorRGBVal": "00FF2702", // A Hex color value. “AARRGGBB” - can also be “RRGGBB”
7 "LineWeightIndex": 0 // An index into the Lineweights table (see below). The index value can range between 0 and 23 even if the Lineweights table below does not override all 24 entries.
8
9 },
10 "New Level Style": { // Another named style
11 "ColorRGBVal": "00FFFF00",
12 "LineWeightIndex": 23,
13 "LineType": "dashed", // See list of accepted line types below
14 "LineEndType": "bevel", // See list of accepted line end types below
15 "LineJoinType": "miter", // See list of accepted line join types below
16 "FillType": "solid", // See list of accepted fill types below
17 },
18 "New Level-1 Style": { // Another named style
19 "ColorRGBVal": "00FF00FF",
20 "LineWeightIndex": 3,
21 "LineType": "iso-dash",
22 "LineEndType": "bevel",
23 "LineJoinType": "miter"
24 },
25 "New Level-2 Style": {
26 }
27 },
28
29
30 "Lineweights": { // Default line weights override table
31 "0": { "mm": 1.5 }, // Override default value at index 0 with 1.5 millimeters
32 "3": { "in": 0.05 }, // Override default value at index 3 with 0.05 inches
33 "23": { "mm": 12.0 }
34 },
35
36
37 "Layer-to-style-map": { // Map of layer names to style names to set on layers
38 "0": "Normal", // Sets the style called Normal on the layer named 0
39 "Default": "Normal", // Sets the style called Normal on the layer named Default
40 "New Level": "New Level Style", // Sets the style called New Level Style on the layer named New Level
41 "New Level-1": "New Level-2 Style"
42 }
43 }, // End of Named-styles object
44
45 "Direct-overrides": { // Direct-overrides object, when present, takes priority
46 // over the Named-styles section (see above). This approach
47 // works differently from the styles approach above. Here we
48 // specify attributes that correspond to the standard indices
49 // for line weight tables and color tables and change the default
50 // values for those indices. We then iterate all the layers and
51 // objects in a CAD drawing and for each layer or object that has
52 // for its line weight or color index one of the indices we
53 "Lineweights": { // Overridable line weight indices for DWG range from 0 to 23
54 "0": { "mm": 1.5 },
55 "3": { "mm": 0.5 },
56 "23": { "mm": 2.0 }
57 },
58 "Colors": { // Overridable color indices for DWG range from 1 to 255
59 "2": "FFFF00",
60 "5": "270200",
61 "7": "2702FF"
62 },
63 "fordgn": { // The same as above. but these apply to DGN CAD files
64 "Lineweights": { // Overridable line weight indices for DGN range from 0 to 31
65 "1": { "mm": 2.5 },
66 "31": { "in": 0.01 },
67 "0": 2.0 // No unit specified means value is mm
68 },
69 "Colors": { // Overridable color indices for DGN range from 0 to 255
70 "1": "FF2702",
71 "3": "00FF00",
72 "0": "FF00FF"
73 }
74 }
75 } // End of Direct-overrides object
76} // End of Plot-styles object

Accepted values for LineType entry

Acceptable values for the LineType entry from the example above include:

"solid", "dashed", "dotted", "dash-dot", "short-dash", "medium-dash", "long-dash", "short-dash-x2", "medium-dash-x2", "long-dash-x2", "medium-long-dash", "medium-dash-short-dash-short-dash", "long-dash-short-dash", "long-dash-dot-dot", "long-dash-dot", "medium-dash-short-dash-dot", "sparse-dot", "iso-dash", "iso-dash-space", "iso-long-dash-dot", "iso-long-dash-double-dot", "iso-long-dash-triple-dot", "iso-dot", "iso-long-dash-short-dash", "iso-long-dash-double-short-dash", "iso-dash-dot", "iso-double-dash-dot", "iso-dash-double-dot", "iso-double-dash-double-dot", "iso-dash-triple-dot", "iso-double-dash-triple-dot"

Accepted values for LineEndType entry

Acceptable values for the LineEndType entry from the example above include:

"butt", "square", "round", "diamond"

Accepted values for LineJoinType entry

Acceptable values for the LineJoinType entry from the example above include:

"miter", "bevel", "round", "diamond"

Accepted values for FillType entry

Acceptable values for the FillType entry from the example above include:

"solid", "checkerboard", "crosshatch", "diamonds", "horizontal-bars", "slant-left", "slant-right", "square-dots", "vertical-bars"

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales