InterOp:Connect SDK/Creating a User BRep Importer

From DocR18

Jump to: navigation, search

A Representation Importer imports data from a Representation Exporter. To create a Representation Importer, you need to create a dynamically loadable library, which provides the implementation of a dedicated SPAXRepresentation subclass. In this sample example, we create a User BRep Importer that imports BRep entities from the User format. In this case, the Importer will just count the bodies in the source model.

Contents

Creating the User BRep Importer Class

The User BRep Importer Class is derived from the SPAXBRepImporter class. The class is named based on the InterOp naming convention. The Importer class uses the Exporter class to:

  • Retrieve the data about the entities in the source document.
  • Populate the translated entities in the target document.

Deriving from SPAXBRepImporter

For a BRep Importer, the Representation class should be a subclass of SPAXBRepImporter. The following code shows the declaration of the SPAXUserBRepImporter class:

class ExportedBySPAXUserBRepImporter SPAXUserBRepImporter : 
public SPAXBRepImporter
{
...
};

The InterOp BRepImporters use the methods from BRepExporter class to retrieve the source document entities descriptions and create the target model. This class should provide the implementation for all the abstract methods declared in the base class. Here, the SPAXUserBRepImporter class that derives from the SPAXBRepImporter class provides all the implementation of the abstract methods. The main task for creating an Importer is to implement the SPAXImportRepresentation DoImport method.

InitializeOptions

The library which implements a representation is automatically loaded when this representation is instantiated by InterOp. The Initialize function is then invoked for the component to perform any required initialization operations.

ProcessUserOptions

The initialization step is followed by managing user options. The ProcessUserOptions method enables the component to check the value of the specified user options that would affect its behavior.

DoImport

Once the pre-process operations are completed and user options are set, the DoImport method is invoked. The Importer uses this method to retrieve data from the source document. The DoImport method is passed an object of the Exporter class as an argument to access the Exporter object. While querying entities from the source document, the Importer is also required to populate corresponding translated entities in the target document. The BRep Importer expects the passed Exporter to be a BRep Exporter: The following code shows how the generic exporter argument can safely be cast to the proper type:

SPAXRepType& type=ipExportRepresentation->GetType();
if ( type != SpaxBRep)
   return SPAX_E_FAIL;
SPAXBRepExporter *pBRep = XSTATIC_CAST( SPAXBRepExporter *, ipExportRepresentation);

If the Exporter is a BRep Exporter, the passed SPAXExportRepresentation pointer can be cast into a SPAXBRepExporter pointer, which can be used for querying BRep entities as shown in the following code snippet:

int numSrcBodies = 0;
pBRep->GetNumberOfBodies(numSrcBodies);

The Importer then gets a pointer to its owning Document, the target document, to populate it with the translated information as shown in the following code snippet:

SPAXDocument * dstDoc = this->GetDocument();
SPAXUserDocument * UserDoc = XSTATIC_CAST( SPAXUserDocument *, dstDoc);
if( UserDoc)
   UserDoc->SetNumberOfBodies( numSrcBodies);

Finalize

The Finalize function is called immediately before the component is deleted to perform any required clean-up.

User BRep Importer Library

Naming conventions and required C function are the same as for the Representation Exporter, with Exporter replaced by Importer.

Importer Library Naming Convention

You need to name Representation Importer library based on the InterOp naming convention to enable InterOp support: SPAXFormatNameRepresentationNameImporter where FormatName is the name of the format to be implemented by the component and RepresentationName is the name of the representation to be imported.

Importer Instantiation Function

InterOp invokes the instantiation function to obtain an instance of the Representation Importer after the library is loaded. You need to name the function based on the InterOp naming convention: SPAXCreateFormatNameRepresentationNameImporter where FormatName is the name of the format to be implemented by the component and RepresentationName is the name of the representation to be imported.

User BRep Importer Library Naming Convention

The name of the Representation Importer library is based on the InterOp naming convention: SPAXUserBRepImporter. This library exports the following C function: extern "C" SPAXErrorType SPAXCreateUserBRepImporter(SPAXDocument* ipImportDocument, SPAXUserBRepImporter** oppUserBRepImporter) The function is implemented in SPAXUserBRepImporterLoader.cpp

Using the Sample

The User Document and BRep Importer components can be used as any regular InterOp components. The following code shows how to use a User Document and BRep Importer:

#include "SPAIConverter.h" 
#include "SPAIDocument.h"
int  main() 
{ 
   SPAIDocument src("C:\\model.sat"); 
   SPAIDocument dst("C:\\model.txt");
   dst.SetType("User"); 
   SPAIConverter converter;
   converter.Convert(src, dst); 
   return 0;
}
Personal tools
new releases