Yesterday I got the question of how to colorize part's faces and save the colored part to STEP. Here's how one can do this using XDE/OCAF framework of OpenCascade:
C++:
TopoDS_Shape partShape = // Your shape
/* Create application and document of the XDE format */
Handle(TDocStd_Application) app = new TDocStd_Application;
BinXCAFDrivers::DefineFormat(app);
Handle(TDocStd_Document) doc;
app->NewDocument("BinXCAF", doc);
//
if ( doc.IsNull() )
{
// Cannot create an OCAF document.
return 1;
}
/* Populate XDE document */
Handle(XCAFDoc_ShapeTool)
stool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
TDF_Label rootLab = stool->AddShape(partShape);
/* Set colors */
TopTools_IndexedMapOfShape allFaces;
TopExp::MapShapes(partShape, TopAbs_FACE, allFaces);
// I use some random indices here for test.
TDF_Label ssLab[3];
ssLab[0] = stool->AddSubShape(rootLab, allFaces(1));
ssLab[1] = stool->AddSubShape(rootLab, allFaces(2));
ssLab[2] = stool->AddSubShape(rootLab, allFaces(3));
Handle(XCAFDoc_ColorTool)
ctool = XCAFDoc_DocumentTool::ColorTool( doc->Main() );
ctool->SetColor(ssLab[0], Quantity_NOC_RED, XCAFDoc_ColorSurf);
ctool->SetColor(ssLab[1], Quantity_NOC_GREEN, XCAFDoc_ColorSurf);
ctool->SetColor(ssLab[2], Quantity_NOC_BLUE1, XCAFDoc_ColorSurf);
/* Write STEP file */
STEPCAFControl_Writer writer;
if ( !writer.Transfer(doc) )
{
// Cannot transfer data.
return 1;
}
if ( writer.Write( filename.c_str() ) != IFSelect_RetDone )
{
// Cannot write file.
return 1;
}