Write shapes in BRep file format

BRepTools::Write



BRepTools::Write is a function in the Open CASCADE Technology (OCCT) library that allows you to write a TopoDS shape to a BRep (Boundary Representation) file. This function comes under the BRepTools module, which gives you tools for working with BRep objects in OCCT.

To use this you must create a TopoDS shape object that represents the geometry you want to save. You can use inherited objects from the TopoDS_Shape class such as TopoDS_Solid, TopoDS_Face, TopoDS_Shell, TopoDS_Vertex, etc. 



The most simple way of writing a shape is as follows.
#include <BRepTools.hxx>
TopoDS_Shape shape = ...;        // shape you want to write
Standard_CString pathName = ...; // file name
BRepTools::Write(shape, pathName);
The function takes two parameters:

shape - reference to the TopoDS shape object you want to write

pathName - string containing the path and file name where you want to save the shape. Make sure you add the extention ".brep" at the end of the string. You can use "const char*" instead of "Standard_CString" since 'Standard_CString" is a type definition of "Standard_Character*" and "Standard_Character is the type definition of "const char".

Important fact

Apart from the main parameters, there are optional parameters that you can define to write a file. The parameter called "theVersion" is one of the important parameters. If you are using the above code snippet to write a file and open that using OCCT-based CAD viewer (eg: FreeCAD, SALOME) those BRep files cannot be opened. The main reason is the default version of the file cannot be read correctly. Hence you have to mention the correct file format.
Here is the explanation of these formats.


This is how to use the file version

#include <BRepTools.hxx>
TopoDS_Shape                 shape      = ...; // shape you want to write
Standard_CString             pathName   = ...; // file name
const TopTools_FormatVersion theVersion = ...  // file version
BRepTools::Write(shape, pathName, true, false, theVersion);

The default version of the file is "TopTools_FormatVersion_VERSION_3". If you use this version, there are issues when trying to read the files back. Hence use "TopTools_FormatVersion_2" in your case and that will not cause issues during reading the files.

Additional functions in BRepTools

There are other functions in BRepTools module to work with BRep shapes. BRepTools::Read allows you to read the shape in the ".brep" file format. BRepTools::Clean function can be used to clean up and repair the shape before exporting the shape.

The BRepTools::Write function can be used to transfer complex 3D geometries between OCCT based applications.

Post a Comment

Previous Post Next Post