CAD Exchanger SDK
Reading and Writing Files

Overview

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.

Note
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 Imported 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)) {
//OK
} 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")) {
//OK
} 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 (...);
{
ACIS_Reader aReader;
Base_ProgressStatus& aStatus = aReader.ProgressStatus();
aStatus.Register (anObserver);
bool anIsOK = false;
{
Base_ProgressScope aScope (aStatus, 10); //10%
anIsOK = aReader.ReadFile (aFileName);
}
if (anIsOK) {
Base_ProgressScope aScope (aStatus, 90); //90%
anIsOK = aReader.Transfer (aModel);
}
}

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

Examples

Refer to General Example, Progress Status Example.