Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Importing .dwf/dwfx Files

Use the DWF import TX module to import from Design Web Format (DWF) .dwf and .dwfx files to .dwg or .dxf files:

TD_Dwf7Import_xx.yy_zz.tx

where:

  • xx — Major Drawings SDK version number.
  • yy — Minor Drawings SDK version number.
  • zz — Visual C++® compiler version number. For example, for Microsoft® Visual Studio® 2015 version, the number is 14.

The module exposes a single interface, which serves as a class factory for OdDwfImport objects. An instance of the OdDwfImport class (an importer) does the conversion.

The DWF import TX module is a single support module for the Drawings SDK, and it's available in dynamic and static forms for Windows, Linux, and macOS (x86 and x64 versions). All source is available in the release packages.

For dynamic (.dll) configurations, the DWF import TX module can be loaded dynamically; it does not need to be linked to. For static configurations, the DWF import module should be linked to.

Import process

There are two abstract classes that can be inherited by classes that implement their own algorithm of DWF import: OdDwfImport and OdDwfImportModule.

The import process commonly consists of five steps:

  1. Create a DWF import module.
  2. Create a DWF importer instance.
  3. Set the DWF import parameters.
  4. Call the import() method of the DWF importer object.
  5. Analyze the results of the import process.

Prior to following the steps from the above list, some preliminary actions must be performed, including creating a custom services object and initializing it:

// Create a custom Services object.
OdStaticRxObject svcs;
// Initialize ODA Platform.
odInitialize(&svcs);

For DWF import module creation, the method loadModule() of the odrxDynamicLinker object must be called. The loadModule() method returns a smart pointer to the import module (import module object is an instance of the class inherited from the OdDwfImportModule class). Then call the create() method of the import module object to get a smart pointer to the importer instance (an instance of the class inherited from OdDwfImport).

// Load the DWF import TX module
OdDwfImportModulePtr pModule = ::odrxDynamicLinker()->loadApp(OdDwf7ImportModuleName, false);
// Create an importer instance
OdDwfImportPtr importer = pModule->create();

Additionally, the function createImporter() defined in the TD_DWF_IMPORT namespace can be used for creation of the DWF importer object. When using this function, there is no need to create a DWF import module because createImporter() creates it by itself.

#include "DwfImport.h"
using namespace TD_DWF_IMPORT;
// Create an importer instance
OdDwfImportPtr importer = createImporter();

There are several import parameters that must be set for importing DWF (.dwf or .dwfx) files:

  • Database — Defines the OdDbDatabase object, where the .dwf/.dwfx file is imported.
  • DwfPath — Defines the filename of the .dwf/.dwfx file to be imported.
  • Password — Defines the password for a password protected .dwf/.dwfx file. Empty by default.
  • PreserveColorIndices — Defines whether the color indices from the imported file will be preserved in the resulting database. If the color indices are preserved, the representation of colors may differ from the origin.
  • LayoutNumber — Defines the layout to be imported. A value of "-1" results in the import of all layouts.
  • ImportW3d — Enables or disables the import of 3D models from the .dwf/.dwfx file.

The following code creates an instance of the OdDbDatabase class and puts a pointer to it in the pDb variable. This pointer will be used for setting up a "Database" parameter.

// Create an instance of OdDbDatabase class and get a pointer to it
OdDbDatabasePtr pDb = svcs.createDatabase();

Parameters are set by defining the corresponding properties of the importer object:

// Set the import parameters
importer->properties()->putAt(L"Database", pDb);
importer->properties()->putAt(L"DwfPath", OdRxVariantValue(OdString(inputFileName)));
importer->properties()->putAt(L"Password", OdRxVariantValue(OdString::kEmpty));
importer->properties()->putAt(L"PreserveColorIndices", OdRxVariantValue(true));
importer->properties()->putAt(L"LayoutNumber", OdRxVariantValue(OdInt32(-1)));
importer->properties()->putAt(L"ImportW3d", OdRxVariantValue(true));

After the properties are set, the .dwf/.dwfx file can be imported using the import() method:

// Import DWF file
TD_DWF_IMPORT::OdDwfImport::ImportResult impRes = importer->import();

And finally, analysis of the result. The impRes variable is of the ImportResult type which is an enumeration with the following values:

Identifier Error description
success Import finished successfully.
fail Import failed: Internal import error.
bad_password Import failed: Wrong password for the imported .dwf/.dwfx file.
bad_file Import failed: Wrong format of the imported .dwf/.dwfx file.
bad_database Import failed: Invalid database pointer or corrupted database.
encrypted_file Import failed: Imported .dwf/.dwfx file is protected.

The "encrypted_file" import result arises when an object of the kzMIMEType_W3D_S MIME type is detected during the import process. This means that the imported .dwf/.dwfx file is protected. For more details, follow the link.

If the import result was "success", the imported database can be accessed for further operations, for example, saving it as a .dwg file:

if(impRes == TD_DWF_IMPORT::OdDwfImport::success) {
  // The importer object is not needed anymore
  importer = 0;

  // Create a write file buffer and link it with the output file
  OdWrFileBuf fb(OdString(outputFileName));

  // Get the extension of the output file to determine its type
  OdString ext = outputFileName.right(4);
  ext.makeUpper();

  // Write the database to the file taking into account the file type
  if ( ext == L".DWG" )
    pDb->writeFile(&fb, OdDb::kDwg, OdDb::kDHL_CURRENT);
  else if ( ext == L".DXF")
    pDb->writeFile(&fb, OdDb::kDxf, OdDb::kDHL_CURRENT);   
}

DWF Import sample application

The OdDwfImportEx sample application shows how to use DWF import functionality in user applications. OdDwfImportEx is a console application that implements working with DWF import functionality.

To run the application, use the following syntax:

OdDwfImportEx <input_file><output_file>

where:

  • input_file — .dwf/.dwfx file.
  • output_file — .dwg file.

Source files for the OdDwfImportEx application are located in the Drawing/Examples/OdDwfImportEx folder.

See Also

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