Read/Write shape to memory

Hi,

I can write and read a TopoDS_Shape to a file no problem. But I want to get a string representation ultimately to my C# parent app, here I will save to disk along with application specific data.

So I try to pass a stream of some sort to BRepTools::Dump (also tried BRepTools::Read) but no matter what I try it either crashing or is empty.

How do I pass to the Standard_OStream& S argument a stream that I can get a std::string out or similar?

My C++ is very basic and I am going round in circles!

Many thanks

Julian

570143426_157742's picture

Hi,

I have the same question, is there anyone can help? 

Thanks.

Hugues Delorme's picture

Hello, have a look at functions BRepUtils::shapeToString() and BRepUtils::shapeFromString() at https://github.com/fougue/mayo/blob/develop/src/base/brep_utils.cpp

std::string BRepUtils::shapeToString(const TopoDS_Shape& shape)
{
    std::ostringstream oss(std::ios_base::out);
    BRepTools::Write(shape, oss);
    return oss.str();
}

TopoDS_Shape BRepUtils::shapeFromString(const std::string& str)
{
    TopoDS_Shape shape;
    BRep_Builder brepBuilder;
    std::istringstream iss(str, std::ios_base::in);
    BRepTools::Read(shape, iss, brepBuilder);
    return shape;
}
570143426_157742's picture

It works, really thank you!

570143426_157742's picture

Hi

I have another problem, and I don't know how to solve it.

When I  used the TopoDS_Shape BRepUtils::shapeFromString​, I tried to get the color of shape:

BRepTools::Read(shape, iss, brepBuilder);

Quantity_Color aColor;

aColorTool->GetColor(sss, XCAFDoc_ColorGen, aColor);   //aColor is yellow

and I found the colors are all YELLOW,  can BRepTools::Write save shape's color?

Best regards.

Hugues Delorme's picture

BRepTools::Read/Write() are functions to serialize a shape with the OpenCascade BRep format which saves only topology.

For color support you will need XCAF, have a look at STEPCAFControl_Reader/Writer or IGESCAFControl_Reader/Writer

Look at XDE in the documentations.

570143426_157742's picture

Thank you! I will try.

Best regards.

lei19942016_164393's picture

Hi,

I have the same question now.

Do you find the method to convert XDE format output to std::string?

Thanks