Hey, digging up some old bones here :)
As the OP above, I am trying to keep track of colors during geometry creation. The application is receiving a csg tree to create a solid. Each node of the csg tree (i.e. box, torus, extrude, etc...) has a color. My strategy was to use the XCAF document as you mentioned above. It works well with basic csg tree such as a unique box node with color blue. However, the tricky part comes with boolean operations (again, as the OP mentioned).
Here is the method to register a shape in the XCAF document:
void GeometryCreationContext::RegisterShape(const TopoDS_Shape &shape, const Quantity_Color &color, const std::string &name) const
{
const TDF_Label shapeLabel = m_shapeTool->AddShape(shape, false);
const std::string shapeIdentifier = name + std::to_string(rand() % 1000);
TDataStd_Name::Set(shapeLabel, shapeIdentifier.data());
TopTools_IndexedMapOfShape faces;
TopExp::MapShapes(shape, TopAbs_FACE, faces);
for (int i = 1; i <= faces.Extent(); ++i)
{
TopoDS_Face face = TopoDS::Face(faces(i));
TDF_Label faceLabel = m_shapeTool->AddSubShape(shapeLabel, face);
if (faceLabel.IsNull())
{
LOG_WARN("Label of sub-shape {} is null for shape {} ", i, name);
continue;
}
m_colorTool->SetColor(faceLabel, color, XCAFDoc_ColorGen);
LOG_TRACE("Created sub-shape {} with color ({}, {}, {}) in {} ", i, color.Red(), color.Green(), color.Blue(), name);
TDataStd_Name::Set(faceLabel, ("face-" + std::to_string(i)).data());
}
}
The boolean operation history is great when it comes to identify the shape that were generated, deleted or modified. But I didn't see any functionality to see which face was. Did I missed it?
For reference, this is my operation node (cut, fuse and common) main method:
void OperatorNode::Do(const TopoDS_Shape &left, const TopoDS_Shape &right, GeometryCreationContext& context, TopoDS_Shape &outShape) const
{
// ...
BRepAlgoAPI_BooleanOperation booleanOperation;
booleanOperation.SetOperation(m_operationType);
TopTools_ListOfShape aLS;
aLS.Append(left);
TopTools_ListOfShape aLT;
aLT.Append(right);
booleanOperation.SetArguments(aLS);
booleanOperation.SetTools(aLT);
booleanOperation.SetFuzzyValue(1e-45);
booleanOperation.SetRunParallel(true);
booleanOperation.Build();
outShape = booleanOperation.Shape();
const Handle(BRepTools_History) history = booleanOperation.History();
// ... trying to register my shape here
// context.RegisterShape(outShape, Quantity_NOC_MAGENTA, m_name + "_outShape");
}
The way I see it, OCCT is just not suitable for color tracking during geometry construction. I might have to live with that. If you have any example proving otherwise, I'd me more than happy to look at them.
Have a good one!