Hide menu
No Matches

Read and write numerous CAD and BIM file formats.


Reading (importing) and writing (exporting) files are the mainstream scenarios when using CAD Exchanger SDK. The list of supported formats is available here.

When reading a file, the result is converted into a common data model that provides common format-neutral data representation and access API. Likewise, to write a file this common representation is used as a source.

If your application can already read some (simple) file formats, such as STL or FBX, then the fastest (although very suboptimal) way to integrate CAD Exchanger SDK is to add simple conversion from any format it supports that includes reading a file and then saving a result in the format your app can already read, as demonstrated below:

#include <cadex/ModelData_ModelReader.hxx>
#include <cadex/ModelData_ModelWriter.hxx>
using namespace cadex;
if (!aReader.Read ("myfile.jt", aModel)) {
//error during reading the file
ModelData_ModelWriterer aWriter;
if (!aWriter.Write (aModel, "myfile.stl")) {
//error during writing the file
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
bool Read(const Base_UTF16String &theFilePath, ModelData_Model &theModel)
Reads the file at the specified path into the specified model.
Definition: ModelData_ModelReader.cxx:182
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22

Although the above approach requires minimal implementation effort, it involves using an intermediate file and bears a risk of data quality deterioration (due to use of an interim format).

An alternative, more reliable (and thus recommended), solution is to use data model API to access data in memory, instead of dumping it on disk. For instance, instead of writing an STL file the optimal approach would be to consult polygonal representation (containing triangular meshes) in memory to get access to mesh nodes coordinates, normals and connectivity information.

There are two primary ways to handle reading and writing in CAD Exchanger:

  • Using universal API. ModelData_ModelReader and ModelData_ModelWriter classes can be used as single points of entry, capable of handling every format supported by CAD Exchanger.
  • Using format-specific API. Format-specific readers and writers may be used to get finer control over the import and export processes. Each format's classes are designated by a corresponding prefix, for instance JT_, ACIS_, IGES_ and so on.

File are identified by their (asbolute) filenames on a disk. Many converters additionally support arbitrary (C++) streams.

Refer to Supported Formats for details on the supported scope for each format.

Reading files

ModelData_ModelReader is the primary class for reading files, especially when import of multiple formats is needed. The usage is simple, one just has to call a single function with the path to the file that should be read and the target ModelData_Model (for MCAD formats) or ModelData_BIMModel (for BIM formats) instance, this reader itself determines what format the file is and performs the import correspondingly.

The data model object is populated with the contents imported from the file being read. The model is not cleaned up before conversion, therefore a few files can be accumulated in the same model.

#include <cadex/ModelData_ModelReader.hxx>
using namespace cadex;
if (!aReader.Read ("myfile.igs", aModel)) {
//error during reading the file

Alternatively, if the client application needs to have tighter control of the reading process, it's possible to use readers of individual formats, like so:

#include <cadex/IGES_Reader.hxx>
using namespace cadex;
IGES_Reader aReader;
if (aReader.ReadFile ("myfile.igs") && aReader.Transfer (aModel)) {
} else {
//error during reading the IGES file
bool Transfer(ModelData_Model &theModel)
Converts read file into a resulting model.
Definition: Base_Reader.cxx:296
bool ReadFile(const Base_UTF16String &theFileName)
Reads the file into memory.
Definition: Base_Reader.cxx:207
Reads IGES files.
Definition: IGES_Reader.hxx:29

Note that reading a file that way requires the invocation of two functions:

  • Base_Reader::ReadFile() parses a file and constructs an internal format-specific file representation in memory;
  • Base_Reader::Transfer() converts an internal format-specific file representation into neutral format-agnostic data model.

Incremental loading

Huge assemblies may take significant time to load. Instead of making your end-users wait for the entire read phase to complete, you may implement an incremental load when individual parts or sub-assemblies get loaded upon request (e.g. by clicking a checkbox in UI corresponding to a node in an assembly tree view).

In this case, the entire assembly hierarchy can be imported very fast and heavy-weight geometries can be deferred. CAD Exchanger Lab has an option to activate this delayed mode, so you might want to check it.

Refer to Delayed Conversion for details.

Writing files

ModelData_ModelWriter the primary way of writing 3D data to various formats. Similarly to the import one only has to call a single function specifying the file path and the writer automatically determines how to export the model based on the file extension. Once again, this is particularly useful if export to multiple formats is utilized in the same workflow.

#include <cadex/ModelData_ModelWriter.hxx>
using namespace cadex;
ModelData_Model aModel = ...;
if (!aWriter.Write (aModel, "myfile.stp")) {
//error during writing the STEP file
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:33
bool Write(const ModelData_Model &theModel, const Base_UTF16String &theFilePath)
Writes the specified model to the file at the specified path.
Definition: ModelData_ModelWriter.cxx:143

Analogously to import, it's also possible to export models to specific formats using per-format writer classes:

#include <cadex/Para_Writer.hxx>
using namespace cadex;
ModelData_Model aModel = ...;
Para_Writer aWriter;
if (aWriter.Transfer (aModel) && aWriter.WriteFile ("myfile.x_t")) {
} else {
//error during writing the Parasolid file
bool WriteFile(const Base_UTF16String &theFileName)
Writes contents to a target file.
Definition: Base_Writer.cxx:265
bool Transfer(const ModelData_Model &theModel)
Converts source model into memory representation of the target file.
Definition: Base_Writer.cxx:117
Writes Parasolid files.
Definition: Para_Writer.hxx:29

Symmetrically to reading a file, writing a file consists of two steps:

Format-specific details

Refer to Converter Details for details of each format, including supported format versions, scope, parameters, etc.

Progress status support

Both universal and format-specific readers and writers support progress status update. The examples below demonstrates how to add observers to an internal progress status object:

//MyObserver is a subclass of Base_ProgressStatus::Observer
MyObserver anObserver (...);
aStatus.Register (anObserver);
aReader.ProgressStatus() = aStatus;
aReader.Read (aFileName, aModel);
Provides progress status and notification mechanism.
Definition: Base_ProgressStatus.hxx:33
void Register(Observer &theObserver, value_type theValueThreshold=0.1f, unsigned int theTimeThreshold=20U)
Adds an observer that will be notified when the progress status has changed.
Definition: Base_ProgressStatus.cxx:324
const Base_ProgressStatus & ProgressStatus() const
Returns the progress status for the reading process.
Definition: ModelData_ModelReader.cxx:228

When using format-specific readers and writers the progress status needs to be broken into scopes to make sure the status is filled up continuously across both stages of the import:

ModelData_Model aModel;
//MyObserver is a subclass of Base_ProgressStatus::Observer
MyObserver anObserver (...);
Base_ProgressStatus aStatus;
aStatus.Register (anObserver);
ACIS_Reader aReader;
aReader.ProgressStatus() = aStatus;
bool anIsOK = false;
Base_ProgressScope aScope (aStatus, 25); // 25%
anIsOK = aReader.ReadFile (aFileName);
if (anIsOK) {
Base_ProgressScope aScope (aStatus, 75); // 75%
anIsOK = aReader.Transfer (aModel);

For details refer to Progress Status Support and Progress Bar Example.


Refer to Conversion, Progress Bar Example.