Drawings SDK Developer Guide > Working with .dwg Files > Working with Other File Formats > Exporting to Other Formats > Exporting to a PDF File > Export Part of a CAD Drawing to a PDF File
Export Part of a CAD Drawing to a PDF File

This topic describes how to export part of a drawing to a PDF file instead of the whole drawing.

There is a default option ZoomToExtents in the PDF Export settings, and the same option is set by default in the PDFExportDialog of the OdaMfcApp sample application.

The option is a flag in PdfExportSettings, which makes it easy to start using the PDF Export functionality. If this flag is not set, all settings relating to the plot area are taken from PlotSetting, which is stored in the drawing database. The ZoomToExtents option makes the PdfExport module export a drawing as if the following options are set:

  • Plot area — extents
  • Scaled to fit
  • Center the plot

The following drawing is an example.

Export it to PDF with the ZoomToExtents option, and you get this resulting PDF file.

However, you can also export only the entity located in the center. The following code block shows how to do it.

// read the drawing
OdDbDatabasePtr pDb = m_pHostApp.readFile(inputFile);

// get the pointer to the entity using its ID
OdDbEntityPtr pRect = OdDbEntity::cast(pDb->getOdDbObjectId( OdDbHandle(0x2AB)).safeOpenObject(OdDb::kForRead));

if (pRect.isNull())
  throw OdError(eNullEntityPointer);

Note: In this example, OdDbHandle(0x2AB) is the hardcoded handle, and it was copied from a particular drawing. You should replace 0x2AB with your own handle from the drawing you are working with.

// get the entity extents:
OdGeExtents3d ext;
pRect->getGeomExtents(ext);

// get access to the plot settings:
OdDbObjectId idLayout = OdDbBlockTableRecordPtr(pDb->getActiveLayoutBTRId().safeOpenObject())->getLayoutId();
OdDbLayoutPtr pLayout = idLayout.safeOpenObject(OdDb::kForWrite);
OdDbPlotSettings *pPlotSettings = pLayout.get();

// set the necessary plot area:
OdDbPlotSettingsValidatorPtr pValidator = pDb->appServices()->plotSettingsValidator();
pValidator->setPlotWindowArea(pPlotSettings, ext.minPoint().x, ext.minPoint().y, ext.maxPoint().x, ext.maxPoint().y);
pValidator->setPlotType(pPlotSettings, OdDbPlotSettings::kWindow); 
pValidator->setPlotCentered(pPlotSettings, true);
pValidator->setStdScaleType(pPlotSettings, OdDbPlotSettings::kScaleToFit);

// export the drawing:
OdPdfExportModulePtr pPdfModule = ::odrxDynamicLinker()->loadApp(OdPdfExportModuleName);
OdPdfExportPtr exporter = pPdfModule->create();

PDFExportParams params;
params.setDatabase(pDb);
params.setExportFlags((PDFExportParams::PDFExportFlags)(params.exportFlags() & (~PDFExportParams::kZoomToExtentsMode)));

OdUInt32 errCode = exporter->exportPdf(params);

As a result you get the following PDF file.

See Also

Exporting to a PDF File

Export to PDF Parameters and Flags

OdaMfcApp Sample Application

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