On_BinaryFile becomes unusable after each read (C++)

In my company we have some bits and pieces around to read particular pieces of information from Rhino files. I am busy compiling these into a reusable library and noted something very strange: after reading anything from the file, it seems impossible to read anything else.

What it boils down to, is that the following snippet doesn’t work:

// File is a std::filesystem::path instance
ON::Begin();
ON_BinaryFile archive(ON::archive_mode::read3dm, file.c_str());
int version{-1};
ON_String sectionComments;
if (archive.Read3dmStartSection(&version, sectionComments))
{
// process info
}
ON_wString log;
ON_MessageReceiverLog logger(log);
ONX_Model model;
if (model.Read(archive, &logger)) // <— this fails
{
// process the model
}

The first read works, then the read of the model fails. If I first read the model instead, then reading the model succeeds, and reading the version and comments fails. I also tried adding a to SeekFromStart(0) inbetween, which also didn’t help. The other thing I tried is creating a separate file pointer using ON::OpenFile(file,"rb") and then use that to construct the ON_BinaryFile instance, but also that didn’t improve things.

The only thing I have found that helps, is closing the file, and reopening.

What is happening to the ON_BinaryFile after reading something? Is it going into some default cannot-read-anything-else mode? Is not possible to read multiple parts of one file using the same ON_BinaryFile instance?

I started looking into a bit more detail on what is in ONX_Model … it looks like the section comments as well as the version (and also properties) are actually also a member of that class as well. So now my wild guess is that I cannot ‘read’ it because it was already read (into the model instance)?

Hi @Jakob_van_Bethlehem,

If you’re reading a .3dm file using ONX_Model, then there isn’t any reason to (try to) read anything else as the file has been fully read and the contents of the file are on the ONX_Model object.

– Dale