Load STEP from memory instead of file

Hi,

Is it possible to open a file directly from a byte array instead of reading a file?

thanks,

Alfonso

 

Kirill Gavrilov's picture

There is not such functionality (yet).
The feature status can be tracked within issue #0027342 on bug tracker (don't be confused by current status "resolved", existing patch is not ready for using).

Alfonso Tamés's picture

Thanks for the update, I hope it supports C++ streams soon.

All the best,

Alfonso

Martin Walter's picture

Is there any insight on when this will be available for writing step files?

Best regards,

Martin

Kirill Gavrilov's picture

OCCT 7.5.0 has been introduced support for reading STEP files from a C++ stream.
So far, nobody reported the feature request for writing STEP file into stream on OCCT Bugtracker - you can be first:
https://dev.opencascade.org/index.php?q=home/get_involved

haidong ma's picture

add this write stream feature in here: #0032350

Jan Selchow's picture

Hello it seems like the write to Stream Issue has also bin resolved for OCC v7.7.0
Could someone post a working example for both reading from and writing to byte[]?
Also is this only for STEP or can I also import and export for example GLTFs from/to byte[]?
Thank you.

gkv311 n's picture

Jan Selchow wrote

it seems like the write to Stream Issue has also bin resolved for OCC v7.7.0

"resolved" bug state doesn't mean integration, it means that somebody provided a patch for review.

Dmitrii Pasukhin's picture

Hello,

Writing a STEP file to a stream is integrated into ver. 7.7

// Reading to XCAF
STEPCAFControl_Reader aReader;
std::ifstream aStream;
OSD_OpenStream (aStream, aFilePath, std::ios::in | std::ios::binary);
aReader.ReadStream (aFileNameShort, aStream);
aReader.Transfer (aDoc);

// Writing from XCAF
STEPCAFControl_Writer aWriter;
aWriter.Transfer (aDoc);
std::ofstream aStream;
OSD_OpenStream (aStream, aFilePath, std::ios::out | std::ios::binary);
aWriter.WriteStream (aStream);

Best regards, Dmitrii.

Dmitrii Pasukhin's picture

Additionally, to work with "buffer" you can use a special C++ class - std::ostringstream.

std::ostringstream anOutputStream;
aWriter.WriteStream (anOutputStream);
std::string aRes = anOutputStream.str();

Unfortunately, you can't export/import GLTF to/from any buffers yet. Only file. If it is necessary, we can create a ticket to impliment ability to work RWMesh ToolKit (Gltf, Obj, Ply) with stream.

Best regards, Dmitrii.

Jan Selchow's picture

Hello,
I have come across this again.
I would say if not necessary it would definitely be useful to be able to write formats like gltf(.glb) to a stream/buffer.

Dmitrii Pasukhin's picture

Hello, thank you for your suggestion. Implementing stream export support for DE components one of the features that have interest from community. The issue is 0030844: Data Exchange - Support import from and export to stream for data exchange interface - MantisBT (opencascade.org). New subtask will be created later.

Import from stream into GLTF are supported now.

You are free to take a part in our survey: Shape the Future of OCCT! Your Opinion Matters! - Forum Open Cascade Technology

Best regards, Dmitrii.

gkv311 n's picture

Jan Selchow wrote:

I would say if not necessary it would definitely be useful to be able to write formats like gltf(.glb) to a stream/buffer.

glTF file format is multi-file by design, so that only in case of packing everything into a binary .glb version of the format it is possible to have a single file. But glb format specification makes it difficult writing an efficient writer, that would be able done all the work directly into a stream in a 1 pass.

So that current implementation of RWGltf_CafWriter performs export into .glb in 2 passes - first it writes all binary data into temporary file (as model might be quite large in size - it might be not a good idea to allocate it in memory), then generates JSON based on offsets in binary data, writes it into final file, copies content of a temporary file and deletes this temporary file.

Dealing with external streams (e.g. not a normal file) would make implementation less predictable. You may expect, that an 'ideal' glTF writer would be able to write output directly into some HTTP streaming interface, but glTF writer is not that plain simple and an attempt to avoid creation of a temporary file on a local server storage (or even memory-mapped FS) could lead to considerable slowdown of actual generation/transfer process.

Note that you may already use streams with OCCT through defining an own protocol (something like myproc:// and pass file names with this prefix) via OSD_FileSystem::AddDefaultProtocol() interface. Unlike passing a single std::istream or std::ostream, this interface allows processing formats that work with multiple files (like STEP/glTF/OBJ file referring to external references, expected to be placed within the same folder as the main file). But file system interface is more complicated to implement and OSD_FileSystem is incomplete in current state (it lacks methods for deleting, copying files and exploring folders).