Serialization of opennurbs objects in memory buffer


I want to write ONX_Model object in memory buffer instead of file. I tried the following code. It succeeds at writing the model, but fails at reading the model. If I try the same example with file it works. Any idea ?

ON_Buffer buffer;

// write model
ONX_Model model;
ON_BinaryArchiveBuffer ba(ON::write3dm,&buffer);
ON_TextLog error_log;
const char* sStartSectionComment = __FILE__ "Session" __DATE__; 
bool result = model.Write(ba, 4, sStartSectionComment, &error_log );

// read model
ONX_Model model2;
ON_TextLog error_log2;
ON_BinaryArchiveBuffer ba2( ON::read3dm, &buffer);  
// result2 is false
bool result2 = model2.Read(ba2,  &error_log2 );  

The error in the error_log2 says :
ERROR: Unable to read start section. (ON_BinaryArchive::Read3dmStartSection() returned false.)

(Dale Fugier) #2

Hi Elizabeta,

Before reading, do this:

buffer.Seek( 0, SEEK_SET ); // Seek from beginning of buffer


Thank you Dale