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.

There are two primary ways to handle reading and writing in CAD Exchanger. ModelData_ModelReader and ModelData_ModelWriter classes can be used as single points of entry, capable of handling every format supported by CAD Exchanger. Alternatively, 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_, etc - refer to General Conventions for naming conventions.

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 instance, this reader itself determines what format the file is and performs the import correspondingly.

The ModelData_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

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

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

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

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

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:

ModelData_Model aModel;
//MyObserver is a subclass of Base_ProgressStatus::Observer
MyObserver anObserver (...);
Base_ProgressStatus aStatus;
aStatus.Register (anObserver);
ModelData_ModelReader aReader;
aReader.ProgressStatus() = aStatus;
aReader.Read (aFileName, aModel);

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.

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:119
bool Transfer(const ModelData_Model &theModel)
Converts source model into memory representation of the target file.
Definition: Base_Writer.cxx:96
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:40
Writes Parasolid files.
Definition: Para_Writer.hxx:28
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:31
bool Read(const Base_UTF16String &theFilePath, ModelData_Model &theModel)
Reads the file at the specified path into the specified model.
Definition: ModelData_ModelReader.cxx:120
bool WriteFile(const Base_UTF16String &theFileName)
Writes contents to a target file.
Definition: Base_Writer.cxx:154
Base_ProgressStatus & ProgressStatus() const
Returns a progress status.
Definition: Base_Reader.cxx:280
bool Transfer(ModelData_Model &theModel)
Converts read file into a resulting model.
Definition: Base_Reader.cxx:229
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:31
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22
bool ReadFile(const Base_UTF16String &theFileName)
Reads the file into memory.
Definition: Base_Reader.cxx:160
Reads IGES files.
Definition: IGES_Reader.hxx:28