Hide menu
Reading and Writing Files


Reading (importing) and writing (exporting) files are the mainstream scenarios when using CAD Exchanger SDK. This can be the simplest way to integrate support of a new format into your application if it can already parse some format. For instance, if your application can parse STL files and you consider adding support of the IGES format then you can read IGES files and convert them to temporary STL files with the help of CAD Exchanger, and then parse those STL files using your existing code.

Although in the above example the described scenario would require the least implementation efforts, it is quite suboptimal. A better scenario would be to read the IGES file, to generate a polygonal representation and to explore the triangulation in your application (as explained in the Exploring 3D Models section). This would allow to avoid dumping to a temporary STL file and reading it back.

Each format is designated by a corresponding prefix, for instance JT_, ACIS_, IGES_, etc - refer to General Conventions for naming conventions.

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

Reading files

All reader classes subclass Base_Reader.

Reading a file consists of two steps:

  • Parsing a file and constructing an internal format-specific file representation in memory (see Base_Reader::ReadFile());
  • Converting an internal format-specific file representation into neutral format-agnostic data model (see Base_Reader::Transfer()).

Depending on a file format, Base_Reader::ReadFile() can accept a file name defined as Base_UTF16String or as a C++ stream std::istream.

Base_Reader::Transfer() accepts a ModelData_Model object which is populated with the contents imported from the file being read. The model is not cleaned up before converting, therefore a few files can be accumulated in the same model.

The following example demonstrates a typical reading workflow:

#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

Writing files

All writer classes subclass Base_Writer.

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

Base_Writer::Transfer() accepts a ModelData_Model object and uses its contents to convert to a target file.

Depending on a file format, Base_Writer::WriteFile() can accept a file name defined as Base_UTF16String or as a C++ stream std::ostream.

The following example demonstrates a typical writing workflow:

#include <cadex/Para_Writer.hxx>
using namespace cadex;
ModelData_Model aModel = ...;
STEP_Writer aWriter;
if (aWriter.Transfer (aModel) && aWriter.WriteFile ("myfile.stp")) {
} else {
//error during writing the STEP file

Progress status support

Readers and writers support progress status update. The example below demonstrates how to add observers to an internal progress status object:

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.