Close

Relief for ODA Team in Ukraine

Learn more
ODA PRC SDK
Sample of Export a .dwg /.dxf File to a .pdf File with PRC Support

Summary

ODA Product: ODA PRC SDK
Sample Name: OdPdfExportWithPrc
Categories: C++ / Beginner / Working with .prc files / Android, iOS, macOS, Linux, Windows
Source Code Location: CommonApplications/Prc/Examples/OdPdfExportWithPrc

Description

A sample console application that exports a .dwg/.dxf file to a .pdf file with PRC format support.

The example creates a drawing database, fills it with one or several entities, and exports the drawing contents to a specified .pdf file as a PRC mesh or boundary representation. The quantity of entities and type of PRC content depends on the first application argument.

Run Sample Application

To run the OdPdfExportWithPrc sample application, please do the following:

  1. Open the terminal window and go to the directory where the sample binary file is allocated:
    • For Windows® platforms: <PRC_DIR>\exe\<version>
    • For Non-Windows platforms: <PRC_DIR>/bin/<version>

    <PRC_DIR> is a directory where you unpacked the PRC SDK archive.

    To determine your <version> parameter, please see the appropriate section in the Download ODA PRC SDK topic.

  2. Run the sample with appropriate parameters:
    
                OdPdfExportWithPrc <export case> <target file>
              
    export case Name of an export case to execute. The export case determines export parameters and options. The following export cases are available:
    • OdPdfExportWithPrc — Creates a .dwg file with a red 3D box and exports it to a .pdf file. A PRC view is added (for the created 3D box) to the .pdf file's page. The 3D box is exported as a mesh.
    • OdPdfExportWithPrcSeveralEntities — Creates a .dwg file with four 3D entities and exports the .dwg file to a .pdf file. Four PRC views are added (one per entity) to the .pdf file's page. Entities are exported as boundary representation items.
    • OdPdfPrcFilterExample — Creates four 3D entities and four squares represented with polylines. Polylines are filtered while exporting, i.e. they are exported to the .pdf file's page. For each of the 3D entities, a PRC view is added to the .pdf file's page. 3D entities are exported as boundary representation items.
      See the OdPdfPrcFilterExample() function and PrcLayerFilter class and entity filtering implementation for details.
    target file Full path to the output .pdf file that will contain all exported content.

If the application executed properly, you will see the following console window:

If an error occurs during the application execution, you will see an appropriate error message in the console window. For example, the following picture illustrates the program output in the case when it was called with wrong arguments:

The sample application creates a new PDF file with specified name that can be open with any available PDF viewer with support of PRC format.

Sample Application Structure

The entry point (the main() function) is located in the OdPdfExportWithPrc.cpp file. This file also contains the declaration of the MyServices class, which combines the platform-dependent functionality (platform-dependent operations and progress metering) for .dwg and .prc files.

The main() function checks the input arguments, creates a custom services instance (a MyServices object), initializes the ODA Platform and makes other actions that are typical for console samples based on ODA Platform. When the initial actions are complete, the OdPdfExportWithPrcStart() function is called.

Below is the functions call schema:

main()
|
|-->OdPdfExportWithPrcStart()
    |
    |-->OdPdfExportWithPrc(OdDbHostAppServices, OdString)
    |    |
    |    |-->OdPdfExportWithPrc(OdDbHostAppServices, OdString, PDFExportParams::PRCSupport, bool, void (*fillerfunc)(OdDbDatabasePtr&))
    |        |
    |        |-->fillDatabase()
    |
    |-->OdPdfExportWithPrcSeveralEntities()
    |    |
    |    |-->OdPdfExportWithPrc(OdDbHostAppServices, OdString, PDFExportParams::PRCSupport, bool, void (*fillerfunc)(OdDbDatabasePtr&))
    |    |
    |    |-->fillDatabaseWithSeveralEntities()
    |
    |-->OdPdfPrcFilterExample()
        |
        |-->fillDatabase() in OdPdfPrcFilterExample.cpp
        |    |
        |    |-->fillDatabaseWithSeveralEntities()
        |    |
        |    |-->addFrame()
        |        |
        |        |-->addLayer()
        |        |
        |        |-->fillpoly()
        |
        |-->setupFilters()
        |
        |-->fillPdfExportParams()

OdPdfExportWithPrcStart() function

Module: OdPdfExportWithPrcStart.cpp

Runs the OdPdfExportWithPrc(OdDbHostAppServices&, OdString) function.

Input parameters:

  • OdDbHostAppServices& svcs — Reference to a custom services instance.
  • OdString pdfPath — Full path to the exported .pdf file.
  • ExportMode emode — Export mode that corresponds to the export case input argument.

OdPdfExportWithPrc functions

Module: OdPdfExportWithPrcStart.cpp

The sample application contains two overrides of the OdPdfExportWithPrc function:

  • OdPdfExportWithPrc(OdDgHostAppServices&, OdString) — Calls the second OdPdfExportWithPrc function with default values for mismatched parameters and has the following input parameters:
    • OdDbHostAppServices& svcs — Reference to a custom services instance.
    • OdString pdfPath — Full path to the exported .pdf file.
  • OdPdfExportWithPrc(OdDbHostAppServices, OdString, PDFExportParams::PRCSupport, bool, void (*fillerfunc)(OdDbDatabasePtr&)) — Has the following input parameters:
    • OdDbHostAppServices& svcs — Reference to a custom services instance.
    • OdString pdfPath — Full path to the exported .pdf file.
    • PDFExportParams::PRCSupport flags — Flag that determines the kind of PRC support that should be used during the export process.
    • bool shouldUseSingleViewMode — Flag that determines whether the export should use single view mode only.
    • void (*fillerfunc)(OdDbDatabasePtr &pDb) — Pointer to a function that fills the database with entities.
This function does the following:
    1. Creates a PDF export module.
    2. Creates and fills a database with entities (fillDatabase() function).
    3. Sets export parameters.
    4. Runs the export method of the exporter object.
    5. Checks the result that is returned by the export method.

fillDatabase() function

There are two fillDatabase() functions in different modules:

  • OdPdfExportWithPrcStart.cpp file — Creates an OdDb3dSolid object and allocates it in the specified database. Input parameter:
    • OdDbDatabasePtr& pDb — Reference to the drawing database where the 3D entity should be created.
  • OdPdfPrcFilterExample.cpp file — Creates a new drawing database and adds four 3D entities to it. To create entities. it calls the OdPdfExportWithPrcSeveralEntities() function. Input parameter:
    • OdDbDatabasePtr& pDb — Reference to the drawing database object to create and add entities to.

OdPdfExportWithPrcSeveralEntities() function

Module: OdPdfExportWithPrcStart.cpp

Creates a new drawing database, adds to it a red box, green cylinder, blue cone and magenta torus (see the fillDatabaseWithSeveralEntities() function) and exports it to a .pdf file with PRC format support.

Input parameters:

  • OdDbDatabasePtr& pDb — Reference to the drawing database where the entities should be created.
  • OdString pdfPath — Full path to the output .pdf file to be created.

This function calls the OdPdfExportWithPrc() function, specifying the pointer to the fillDatabaseWithSeveralEntities() function as the last parameter.

fillDatabaseWithSeveralEntities() function

Module: OdPdfExportWithPrcStart.cpp

Creates a red box, green cylinder, blue cone, and magenta torus, and appends these entities to the specified database.

Input parameter:

  • OdDbDatabasePtr& pDb — Reference to the drawing database where the entities should be created.

OdPdfPrcFilterExample() function

Module: OdPdfPrcFilterExample.cpp

Does the following:

  1. Creates a PDF export module.
  2. Creates a database and adds four 3D entities and four polylines to it.
  3. Initializes and registers a filter for entities (using an instance of the PrcLayerFilter class). See PrcLayerFilter class and entity filtering implementation for information about filtering entities for export.
  4. Initializes the conversion parameters.
  5. Runs the export procedure for the drawing database and checks the result.

Input parameters:

  • OdDbDatabasePtr& pDb — Reference to the drawing database where the entities should be created.
  • OdString pdfPath — Full path to the output .pdf file to be created.

addFrame() function

Module: OdPdfPrcFilterExample.cpp

Creates four squares centered at the origin (for creating each polyline the fillpoly() function is called):

  1. Opens model space for writing.
  2. Adds a frame with the name "Frame" to model space (calls the addLayer() function).
  3. Creates four polylines and fills them with vertexes (using the fillpoly() function).
  4. Adds created polylines to the layer "Frame".
  5. Adds four polylines to the model space block table record.

Input parameter:

  • OdDbDatabasePtr& pDb — Reference to the drawing database where polylines should be added.

addLayer() function

Module: OdPdfPrcFilterExample.cpp

Adds a new layer to a specified drawing:

  1. Opens the layer table for writing.
  2. Creates a new layer table record.
  3. Sets the layer's name (you must specify layer name before adding it to the table).
  4. Adds the new record to the layer table.

Input parameters:

  • OdDbDatabase* pDb — Pointer to the drawing database where the new layer will be added.
  • const OdString& name — Name of the new layer.

fillpoly() function

Module: OdPdfPrcFilterExample.cpp

Creates a square centered at the origin by adding vertexes to a specified polyline. Newly created vertices have the following coordinates (x, y), (x, y2), (x2, y2), (x, y).

Input parameters:

  • OdDbPolylinePtr pPoly — Pointer to a polyline.
  • double x — X-coordinate of the first opposite corner of the square.
  • double y — Y-coordinate of the first opposite corner of the square.
  • double x2 — X-coordinate of the second opposite corner of the square.
  • double y2 — Y-coordinate of the second opposite corner of the square.

setupFilters() function

Registers the PrcLayerFilter class in the ODA Platform environment.

The PrcLayerFilter class implements filtering functionality. For additional information, see PrcLayerFilter class and entity filtering implementation.

fillPdfExportParams() function

Sets .pdf file export parameters:

  1. Creates an output .pdf file
  2. Sets export parameters to the passed instance of the PDFExportParams class:
  3. Adds the Model layout to the list of exported layouts.
  4. Sets the printing page size and margins (default values: A4 paper size and zero margins).

Input parameters:

  • PDFExportParams& params — Reference to export parameters storage.
  • OdDbDatabasePtr pDb — Pointer to the drawing database to be exported.
  • OdString pdfPath — Full path to the output .pdf file to be created.

PrcLayerFilter class and entity filtering implementation

The PrcLayerFilter class is derived from OdPrcContextForPdfExport, which is derived from OdRxObject.

PrcLayerFilter overrides the shouldExportAsPRC() method to determine whether a graphical entity should be exported to PRC format.

Input parameters of the shouldExportAsPRC() method:

  • const OdGiDrawable* pDrawable — Pointer to a graphical object.
  • const OdGiPathNode* entity — Pointer to a path node.
  • const OdGsClientViewInfo* pView — Window and viewport information.
  • OdUInt32& viewportidx — Index of the viewport.
The method returns a boolean value that determines whether the drawing should be exported to PRC format:
  1. Tries to cast a graphical object (OdGiDrawable object) pointer to a database entity smart pointer. In the other words, tries to find out whether the graphical object represents a drawing database entity.
  2. If a graphical object represents a database entity and this entity does not belong to the layer with the name "Frame", it is exported to a PRC view. In the other case it is exported to a .pdf page.

Therefore, the squares created in the addFrame() function that are located on the "Frame" layer will be exported to a .pdf page, not to a PRC view.

See Also:

ODA PRC SDK Sample Applications

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