
Tue, 03/08/2016 - 16:24
I'm looking for some guidance on the correct way to write/read a TopoDS_Shape to/from a memory buffer. Something like BRepTools::Write/Read, but using a binary format instead of ASCII as well as being able to target a specific version. The information I found for OCAF persistence mentions a binary format, but it's vague and doesn't reference any classes. Any help is appreciated. Using OCCT 7.0 beta.
Thu, 03/10/2016 - 01:07
Brien,
Binary persistence for TopoDS_Shape is available via BinTools (part of TKBRep as of 6.8.0 - see #25439).
Its usage is similar to one of BRepTools, except that you have to explicitly construct the BinTools_ShapeSet and dump it. The following code excerpt show template-based use of text and binary persistence.
Beware though - binary persistence is known to not properly read/write incomplete shapes (e.g. an edge without 3D curve, which is of course invalid). So you may end-up in a file non readable back. For that reason we had to defer binary persistence in our product.
Hope this helps.
Roman
namespace cadex {
namespace internal {
class ModelData_DataStorage
{
public:
...
enum Mode {
Text, //<! Using plain text (BRepTools_ShapeSet)
Binary //<! Using binary mechanism (BinTools_ShapeSet)
};
...
class Driver
{
public:
__CADEX_DEFINE_MEMORY_MANAGEMENT
Standard_EXPORT virtual ~Driver();
virtual Mode GetMode() const = 0;
virtual bool Store (std::ostream& theOutStream) = 0;
virtual bool Retrieve (std::istream& theInStream) = 0;
};
};
/*! \class ModelData_ShapeStorage
\brief Provides persistence mechanism for Open CASCADE shapes.
*/
class ModelData_ShapeStorage::ShapeDriver : public ModelData_DataStorage::Driver
{
public:
virtual void Add (const TopoDS_Shape& theShape) = 0;
...
};
/*! \a T is either BRepTools_ShapeSet or BinTool_ShapeSet which both have same source API.*/
template<typename T, ModelData_DataStorage::Mode M>
class ModelData_ShapeStorage_ShapeSetDriver : public ModelData_ShapeStorage::ShapeDriver
{
public:
virtual void Add (const TopoDS_Shape& theShape) override { mySet.Add (theShape); }
virtual bool Store (Standard_OStream& theOutStream) override
{
mySet.SetFormatNb (1); //2 or 3 may trigger non-determinism when storing/retrieving
mySet.Write (theOutStream);
return true;
}
virtual bool Retrieve (Standard_IStream& theInStream) override
{
mySet.Read (theInStream);
return true;
}
virtual ModelData_DataStorage::Mode GetMode() const override
{ return M; }
protected:
T mySet;
};
typedef ModelData_ShapeStorage_ShapeSetDriver<BRepTools_ShapeSet, ModelData_DataStorage::Text>
ModelData_ShapeStorage_TextDriver;
typedef ModelData_ShapeStorage_ShapeSetDriver<BinTools_ShapeSet, ModelData_DataStorage::Binary>
ModelData_ShapeStorage_BinDriver;
void Add (const TopoDS_Shape& theShape)
{
if (!theShape.IsNull()) {
myDriver->Add (theShape);
std::stringstream aStream;
myDriver->Store (aStream);
auto aString = aStream.str();
...
}
}
}}