Setting colors in TopoDS_Face is not resolved by FreeCAD or SolidWork

frankpian

Looking around for some CAD
Use XCAFDoc_ColorTool to set the color of a TopoDS_Face, ,then save it as a STEP file.

C++:
Handle(TDocStd_Document) hDoc;
    Quantity_Color redColor(Quantity_NOC_RED);
    XCAFApp_Application::GetApplication()->NewDocument("MDTV-XCAF", hDoc);

    Handle(XCAFDoc_ColorTool) hColorTool = XCAFDoc_DocumentTool::ColorTool(hDoc->Main());
    Handle(XCAFDoc_ShapeTool) hShapeTool = XCAFDoc_DocumentTool::ShapeTool(hDoc->Main());

    gp_Circ aCirc(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), 100);
    BRepBuilderAPI_MakeWire wire;
    TopoDS_Edge aEdge = BRepBuilderAPI_MakeEdge(aCirc);
    wire.Add(aEdge);
    BRepBuilderAPI_MakeFace face(wire.Wire());

    TDF_Label aLabel = hShapeTool->NewShape();
    TDataStd_Name::Set(aLabel, "testModel");
    TDF_Label newLabel = hShapeTool->AddComponent(aLabel, face.Face());
    hColorTool->SetColor(newLabel, redColor, XCAFDoc_ColorSurf);
    hColorTool->SetColor(newLabel, redColor, XCAFDoc_ColorGen);
    hColorTool->SetColor(newLabel, redColor, XCAFDoc_ColorCurv);
    hShapeTool->UpdateAssemblies();

    STEPCAFControl_Writer aWriter;
    aWriter.SetColorMode(Standard_True);
    if (aWriter.Transfer(hDoc, STEPControl_AsIs) != IFSelect_RetDone)
    {
        std::cerr << "Failed to transfer document" << std::endl;
        return 0;
    }

    if (aWriter.Write("myColoredFace.stp") != IFSelect_RetDone)
    {
        std::cerr << "Failed to write STEP file" << std::endl;
        return 0;
    }

Viewing with CAD assistant is normal.

screenshot-20231115-175525.png

Freecad and Solidwork don't show colors.
screenshot-20231115-175559.pngscreenshot-20231115-175735.png
 

calciffer

Active CAD practitioner
hColorTool->SetColor(newLabel, redColor, XCAFDoc_ColorSurf);
hColorTool->SetColor(newLabel, redColor, XCAFDoc_ColorGen);
hColorTool->SetColor(newLabel, redColor, XCAFDoc_ColorCurv);
This might be the issue
 
Last edited:

Quaoar

Administrator
Staff member
@frankpian

A subtle problem with your code is that you're attaching a color to a "component" instead of attaching it right to the face. Anything referenced in an XDE document is a "prototype" (see here: https://www.analysissitus.org/features/features_asm-code-architecture.html#toc-asm-ent-prototype), no matter if that's a part, a subassembly or a subshape.

This is what your code results in (looking in the DF browser):

1701100158726.png

Notice that a color is attached to 0:1:1:1:1, which is a REFERENCE to your face. Now, let's modify your code like this:

Code:
TDF_Label newLabel = hShapeTool->AddComponent(aLabel, face.Face());
TDF_Label newLabelProto = hShapeTool->FindShape(face.Face()); // one
hColorTool->SetColor(newLabelProto, redColor, XCAFDoc_ColorSurf); // two
hColorTool->SetColor(newLabelProto, redColor, XCAFDoc_ColorGen); // three
hColorTool->SetColor(newLabelProto, redColor, XCAFDoc_ColorCurv); // four

Here we attach a color to the "prototype's" label and it results in the following structure:

1701100306066.png

Some software packages would not interpret correctly any colors associated with references and not with the prototypes. CAD Processor with your code:

1701100401478.png

With the modification:

1701100423682.png

FreeCAD on the shape with modification:

1701100475825.png

You should prefer attaching metadata to "prototypes" unless you deliberately choose to do it the other way around and use instances. In the latter case, be prepared that some CAD tools would ignore such metadata.
 

frankpian

Looking around for some CAD
@Quaoar
Thank you for your reply. This structure doesn't seem to make a lot of sense. If I have a lot of shapes with different colors but the same structure, I need to copy multiple shapes, wasting a lot of space.
 

Quaoar

Administrator
Staff member
Why "wasting lot of space"? Attaching color to face prototype is no different from attaching color to its instances in terms of space.
 

frankpian

Looking around for some CAD
Why "wasting lot of space"? Attaching color to face prototype is no different from attaching color to its instances in terms of space.
If I have 10 shapes with the same structure in different locations and different colors, I just need to set the color for each REFERENCE. But setting a color for a prototype can only have one. All references point to the same prototype

I add multiple faces in assembly. Colors are still placed on the REFERENCE. Freecad is working fine😅.

C++:
TDF_Label aLabel = hShapeTool->NewShape();
TDataStd_Name::Set(aLabel, "testModel");
gp_Trsf aTrsf;
for (int i = 0; i < 100; ++i) {
aTrsf.SetTranslation(gp_Vec(0, 0 + i, 0));
TopoDS_Shape theShape = BRepBuilderAPI_Transform(face.Face(), aTrsf).Shape();
TDF_Label newLabel = hShapeTool->AddComponent(aLabel, theShape);
hColorTool->SetColor(newLabel, redColor, XCAFDoc_ColorSurf);
}
hShapeTool->UpdateAssemblies();
1701342446177.jpeg
1701342559774.jpeg
 
Top