Drawings SDK Developer Guide > Working with .dwg Files > Working with Other File Formats > Importing from Other Formats > Importing Collada (.dae) Files
Importing Collada (.dae) Files

Drawings SDK supports import from Collada (.dae) files to .dwg or .dxf files by providing the Collada import TX module:

TD_ColladaImport_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 Collada import module exposes the interface for converting .dae files to .dwg or .dxf files. More information about Collada can be found at http://opencollada.org/.

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

The Collada 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 Collada import TX module can be loaded dynamically; it does not need to be linked to. For static configurations, the Collada import module should be linked to.

Import process

Drawings SDK API provides two abstract classes that can be inherited by classes that implement their own algorithm of the Collada import: OdColladaImport and OdColladaImportModule.

The import process commonly consists of five steps:

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

Before following the steps above, 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 Collada 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 OdColladaImportModule 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 OdColladaImport).

// Load the Collada import TX module
OdColladaImportModulePtr pModule = ::odrxDynamicLinker()->loadApp(OdColladaImportModuleName, false);
// Create an importer instance
OdColladaImportPtr importer = pModule->create();

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

#include "ColladaImport.h"
using namespace TD_COLLADA_IMPORT;
// Create an importer instance
OdColladaImportPtr importer = createImporter();

There are four import parameters that must be set for importing Collada (.dae) files:

  • Database — Defines the OdDbDatabase object, where the .dae file is imported.
  • ColladaPath — Defines the filename of the .dae file to be imported.
  • ImportTextures — Defines whether textures are imported. If set to "false", only geometry is imported.
  • ConsoleInfo — Enables or disables the output of import progress information to the console.

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"ColladaPath", OdRxVariantValue(OdString(inputFileName)));
importer->properties()->putAt(L"ImportTextures", OdRxVariantValue(true));
importer->properties()->putAt(L"ConsoleInfo", OdRxVariantValue(true));

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

// Import Collada file
TD_COLLADA_IMPORT::OdColladaImport::ImportResult impRes = importer->import();

And finally comes 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_file Import failed: Wrong format of the imported .dae file.
bad_database Import failed: Invalid database pointer or corrupted database.

If the import result was "success", the imported database may be accessed for more operations. For example, for saving it in a .dwg file:

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

  // Create a write file buffer an 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);   
}

Collada Import sample application

Drawings SDK provides the OdColladaImportEx sample application, which shows how to use Collada import functionality in user applications. OdColladaImportEx is a console application that implements working with Collada import functionality.

To run the application, use the following syntax:

OdColladaImportEx <input_file><output_file>

where:

  • input_file — .dae file.
  • output_file — .dxf or .dwg file.

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

See Also

Working with Other File Formats

Exporting to a Collada (.dae) File

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