try to save step file after scaled shape but i loose color

Hi everyone,
this is my first post.

I try to scale a step object and save to another step file
when i transform the shape it works but I lose the color.
i try to copy the color of each face from the original to a scaled one but I receive this error:
Exception caught: A null Label has no attribute.

I put in attachment the step file I try to scale and save

I try to follow this tutorial.
https://www.youtube.com/watch?v=dq2-evewPeA

Here my code to save step file.

 
bool WriteLabelToSTEP(const Handle(XCAFDoc_ShapeTool)& shapeTool, const TDF_Label& label, const std::string& filename, double scaleFactor = 10.0) {
    // Create a writer for exporting STEP files
    STEPCAFControl_Writer writer;
    writer.SetColorMode(true);  // Make sure this is set
    writer.SetNameMode(true);
    writer.SetLayerMode(true);
    Interface_Static::SetIVal("write.stepcaf.subshapes.name", 1);
   
    TDF_Label refLabel = label;
    if (shapeTool->IsReference(label))
        shapeTool->GetReferredShape(label, refLabel);

    // Access color tool
    Handle(XCAFDoc_ColorTool) colorTool = XCAFDoc_DocumentTool::ColorTool(refLabel);

    // Retrieve the original shape
    TopoDS_Shape originalShape = shapeTool->GetShape(refLabel);

    // Apply scaling to the shape
    gp_Trsf scaleTrsf;
    scaleTrsf.SetScaleFactor(scaleFactor);
    BRepBuilderAPI_Transform transformer(scaleTrsf);
    transformer.Perform(originalShape, true);
    TopoDS_Shape scaledShape = transformer.Shape();
    

    // Explore original and scaled shapes to reapply colors
    TopExp_Explorer expOrig(originalShape, TopAbs_FACE);
    TopExp_Explorer expScaled(scaledShape, TopAbs_FACE);
    
    while (expOrig.More() && expScaled.More()) {
        const TopoDS_Face& originalFace = TopoDS::Face(expOrig.Current());
        const TopoDS_Face& scaledFace = TopoDS::Face(expScaled.Current());
        

        // Debug output to check the presence of faces
        std::cout << "Original face found: " << &originalFace << std::endl;
        std::cout << "Scaled face found: " << &scaledFace << std::endl;

        Quantity_Color color;
        if (colorTool->GetColor(originalFace, XCAFDoc_ColorSurf, color)) {
            std::cout << "Color found: " << color.StringName(color.Name()) << std::endl;
            /*
            TDF_Label colorLabel = shapeTool->AddSubShape(refLabel, originalFace);
            //colorTool->SetColor(scaledFace, color, XCAFDoc_ColorSurf);
            colorTool->SetColor(colorLabel, color, XCAFDoc_ColorSurf);
            */
           

            try {
                // Your existing code inside this try block
                 // Associate color with the scaled face
                TDF_Label scaledFaceLabel = shapeTool->AddSubShape(refLabel, scaledFace);
                colorTool->SetColor(scaledFaceLabel, color, XCAFDoc_ColorSurf);
             
                
            }
            catch (const Standard_Failure& e) {
                std::cerr << "Exception caught: " << e.GetMessageString() << std::endl;
            }
            catch (const std::exception& e) {
                std::cerr << "Standard exception: " << e.what() << std::endl;
            }
            catch (...) {
                std::cerr << "Unknown exception caught" << std::endl;
            }
            
        }
        else {
            std::cout << "No color found for this face." << std::endl;
        }

        expOrig.Next();
        expScaled.Next();
    }

    // Set the scaled shape back to the label
    shapeTool->SetShape(refLabel, scaledShape);

    // Transfer the label (with the scaled shape) to the writer
    if (writer.Transfer(refLabel, STEPControl_AsIs) != IFSelect_RetDone) {
        std::cerr << "Failed to transfer label to STEP writer." << std::endl;
        return false;
    }

    // Write to a STEP file
    if (writer.Write(filename.c_str()) != IFSelect_RetDone) {
        std::cerr << "Failed to write STEP file: " << filename << std::endl;
        return false;
    }

    return true;
}

thank you for your help.
Best regards
Daniel

Attachments: 
Dmitrii Pasukhin's picture

Hello, the main remarks, your colours assigne to faces only? You need to go no all sub labels and regenerate based on them them. Additionally, you need to remove all of old subshapes, I'm not sure that shapetool clean old subshapes.

The functionality for your case already implemented in 7.7. Please Check XCAFDoc_Editor.

Best regards, Dmitrii.

Dmitrii Pasukhin's picture

Well, I will check the possibility to start OCCT tutorials or guiding from OCCT team. Thank you for sharing your link.

Best regards, Dmitrii.