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:
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.
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:
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 Teigha.
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:
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);
}
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:
Source files for the OdColladaImportEx application are located in Drawing/Examples/OdColladaImportEx.
Working with Other File Formats
Exporting to a Collada (.dae) File
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|