Drawings SDK Developer Guide > Working with .dwg Files > Working with Other File Formats > Importing from Other Formats > Importing .dgn Files to .dwg Files
Importing .dgn Files to .dwg Files

Importing from .dgn files

Drawings SDK supports import from .dgn files to .dwg files by providing the DGN import TX module:

TD_DgnImport_x.yysrc_zz.tx

where:

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

The DGN import module exposes the interface for converting .dgn files to .dwg files.

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

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

Since Teigha version 4.0, DGN Import supports the following features:

  • Raster Reference import.
  • Cell Object to Block Reference conversion.
  • Complex Shapes import.
  • Some additional options for import: Reference Attachment import mode, active dgn model import option, etc.

Since Teigha version 4.0.1, DGN Import also supports these additional features:

  • Shape to Hatch conversion.
  • Font conversion (.rsc font to .shx font).
  • Mesh with Line String import or Complex String import improvements.
  • Dimensions import redesigned.

Since Teigha version 4.2, DGN Import supports the following features:

  • Ability to skip invisible elements during import.
  • Protocol extension for OdDgMultiline elements.
  • Entity mapping for some element types: Shape, Closed B-Spline Curve, Ellipse.
  • Cells and shared cells clipping.
  • Converting line weight from .dgn value to .dwg value.
  • Converting color indexes from .dgn file to true color values.
  • Converting empty data fields to spaces.
  • Converting material data from .dgn to .dwg format.
  • Supporting dimension association.

Import process

Drawings SDK uses three approaches for importing DGN content:

  • Explode call — Complex element is divided into a set of simple entities with loss of properties.
  • Protocol Extension inherited from base class OdDgnImportPE — In most cases creation of a Protocol Extension keeps all properties and features of behavior for the imported element and encapsulates the specific entity type.
  • Client Tool — Developers can create a custom tool (application, class, or module) using the Drawings SDK to import drawing data or properties that are unsupported by the first two methods.

Most DGN import of elements is implemented with the Protocol Extension approach. If there is no custom Protocol Extension for an element, the default Protocol Extension is used, which explodes geometry to simplify DGN elements before importing.

In the case of using a custom Protocol Extension approach, a particular Protocol Extension class must be created for each element of the DGN model. Such Protocol Extension classes should be inherited from the OdDgnImportPE class with custom implementation of the importElement() method.

The following picture illustrates how the DGN import technology is implemented through Protocol Extensions.

DGN import process organization

For detailed information about supported DGN elements, see DGN Elements Available for Import to .dwg Files .

Importing process contains several stages:

  1. Load DGN import TX module.
  2. Create an importer instance.
  3. Set host application services object (to create a new database, etc.).
  4. Set import parameter values.
  5. Set the path to be imported (in-memory streams are not supported yet).
  6. Do the importing (call the import() method of the importer).
  7. Check that the resulting code is correct.

A property map is used for storing import parameters. The following parameters are the most important:

  • Services — An instance of OdDbHostAppServices, used for the creation of the new database.
  • DgnPath — A path to the .dgn file to be imported (property type is OdString).
  • Database — An instance of OdDbDatabase containing the import result.

For a complete list of input parameters, see DGN Import Parameters.

The importer may fail and return an error code from the “ImportResult” enumeration. The supported codes are:

Identifier Error description
success Import was finished successfully.
bad_password Currently not supported, will be used when encryption support is added to Drawings SDK for .dgn files.
bad_file Import failed: Wrong format of the imported .dgn file. The file is probably not a .dgn file.
bad_database Import failed: The file is a .dgn file, but there is something wrong with the database structure.
encrypted_file Import failed: The file is password protected (there is no current support for decryption).

If the import result was “success”, the imported .dwg file database may be obtained from the importer property map under the “Database” key. Most of the objects in a .dgn file directly correspond to objects in the imported .dwg file database (the file structures are very similar).

Code sample for DGN import


      // Load DGN import TX module
      OdDgnImportModulePtr pModule = ::odrxDynamicLinker()->loadApp(OdDgnImportModuleName, false);
      // Create an importer instance
      OdDgnImportPtr importer = pModule->create();
      // Set host application services object (to create new database, etc.)
      importer->properties()->putAt(L"Services", pHostApp);
      // Set the path to be imported (in-memory streams are not supported yet)
      importer->properties()->putAt(L"DgnPath", OdRxVariantValue(path));
      // Do the importing
      OdDgnImport::ImportResult res = importer->import();
      // Check that the result code is ok
      if (res != OdDgnImport::success)
        throw res;
      // Retrieve the imported DWG database
      OdDbDatabasePtr pDb = importer->properties()->getAt(L"Database");
    

Another way to create a DGN importer is to use the createDgnImporter() function (from TD_DGN_IMPORT namespace):


      OdDgnImportPtr importer = createDgnImporter();
      if (importer.isNull())
      {
          odPrintConsoleString(L"Could not create dgn importer\n");
      }
    

DGN Import customization

To create a custom Protocol Extension, inherit the OdDgnImportPE class and re-implement the importElement() method.


      struct OdDgCustomDefinedImportPE : OdDgnImportPE
      {
        void importElement(OdDgElement* e, OdDbBlockTableRecord* owner) ODRX_OVERRIDE
        {
           // Create a dwg  entity and convert dgn data to it
           // ...
           // Append the dwg entity to owner
        }
      }
    

Then it is necessary to modify registerElementLoaders() and unregisterElementLoaders() for the DgnImporter class:


      void DgnImporter::registerElementLoaders( OdDbHostAppServices* pHostAppServices )
      {
        // ...
        // Add your code to the end
        static OdStaticRxObject dgComplesStringImp;

        OdDgComplexString::desc()->addX(OdDgnImportPE::desc(),      &dgComplesStringImp);
      }
    

      void DgnImporter::unregisterElementLoaders( OdDbHostAppServices* pHostAppServices )
      {
      // ... 
      // Add your code here

      OdDgComplexString::desc()->delX(OdDgnImportPE::desc());

      // ...
      }
    

DGN Import sample application

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

To run the application, use the following usage syntax: ExDgnImport <source_dgn_file> <target_dwg_file>.

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

For more details, see the following topics:

DGN Import Parameters

DGN Elements Available for Import to .dwg Files

See Also

Working with Files

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