Properly linking the color of a subShape with the shape when exporting

I'm working on a small application that translates STEP files to other file types. I'm having issues on getting the color to show properly on the object. From what i've read in Extended Data Exchange (XDE) - Open CASCADE Technology Documentation , the color can be stored either in the shape, in its subshapes or in the faces of the shape.

The first thing my program do when exporting is iterate over the labels until i find the label of a simple shape, like this

void AuxiliarStepExchanger::buildTree(TDF_Label aLabel, TopLoc_Location location)
{
    Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(doc->Main());

    if (myAssembly->IsReference(aLabel)) {
        //calls buildTree for the refererred label
    }
    else if (myAssembly->IsSimpleShape(aLabel)) {
        //Extracts the shape referenced by the label "aLabel"
        TopoDS_Shape shape = myAssembly->GetShape(aLabel);
        //Insert the extracted values
        translateShape(shape, location);
    }
    else if (myAssembly->IsAssembly(aLabel)) {
        //calls buildTree for the children labels
    }
}

Then it translates the simple shape. In this part, i can't figure out the proper way to iterate over the simple shape so that i can get all the information about the color here. The problem is, the information about the subshape colors is stored in sublabels, which exists and i can find them when i iterate over the main label, but i can't get to these sublabels when i iterate over the subshapes and do myShapeTool->FindShape(subShape, aLabel). Iterating over the sublabels isn't viable either, because then the subshapes that don't have any color don't have a sublabel, so they wouldn't be processed.

void StepExchanger::translateShape(TopoDS_Shape& shape, const TopLoc_Location& location) 
{
    // ...
    float shapeColor[4];
    Quantity_ColorRGBA theColor;

    if (myColor->GetColor(shape, XCAFDoc_ColorSurf, theColor)) {
        //stores the shape color
    }
    else{
        //sets a default color 
    }
    TDF_Label label = myAssembly->FindShape(shape);
    TDF_LabelSequence labels;
    bool equal = false;
    myAssembly->GetSubShapes(label, labels);

    shape = shape.Located(location);        

    for (TopoDS_Iterator subShapes(shape);subShapes.More();subShapes.Next()) {
        TopoDS_Shape subShape = subShapes.Value();
        float subShapeColor[4];
        //in here, if i do myShapeTool->FindShape(subShape, aLabel); it returns a null shape.
        //myShapeTool is part of the class StepExchanger.
        if (myColor->GetColor(subShape, XCAFDoc_ColorSurf, theColor)) {
            //sets the color as the subshape color
        }
        else {
            //keeps the color from shape
        }

        for (TopExp_Explorer aFaceIt(subShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next()) {
            TopoDS_Face Face = TopoDS::Face(shape);
            float faceColor[4];
            if (myColor->GetColor(Face, XCAFDoc_ColorSurf, theColor)) {
                //sets the color as face color
            }
            else {
                //keeps the color from subshape
            }
            translateFace(aFaceIt.Current(), faceColor);
        }
    }
}

The problem is myColor->GetColor(subShape ...) can't find the color and i can't get to the label of the subShape from the subShape, which is where the color is stored. What's the right way to iterate over the subShapes to get their color? I tried a bunch of other things that didn't work and I ran out of ways to try to solve the issue, so I'm asking here.

The attached image is an example of how each iteration work for the same file

Attachments: 
Lucas Guimaraes's picture

I found out what was wrong. After using the section shape = shape.Located(location);, openCascade was not able to properly link the subShapes to their respective subLabels. To fix the issue i added the data related to location to the assembly of the other file type, instead of adding it to the shape.

What i still don't understand is why the subShape couldn't be linked to its label after the subShape location was changed. From what i understood from the ShapeTool->FindShape() documentation , it says that the subShape should link to its label no matter where it is located, but that doesn't seem to be the case based on the results from my program.

魏涛 魏's picture

Hi, What is the Funtion "translateFace"?

Lucas Guimaraes's picture

Late reply, but translateFace iterates over the vertices, indexes and colors of each Face and store them in the format i'm working with