Sat, 09/21/2024 - 01:47
I am working on importing a STEP file and then exporting it back but changing their appearance using color and material. I am able to export color information for Compounds and Solids but when I try to do it for Faces it doesn't export. I have verified that the face label created has the color information before exporting. The below code I am adding a color for each face but it doesn't get exported.
Here's my export code:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ShapeNode::Export(std::string fileName)
{
Handle(XCAFApp_Application) app = XCAFApp_Application::GetApplication();
// Create a new XDE document
Handle(TDocStd_Document) doc;
app->NewDocument("MDTV-XCAF", doc);
// Access the shape, color, and material tools
Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->Main());
Handle(XCAFDoc_ColorTool) colorTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
Handle(XCAFDoc_MaterialTool) materialTool = XCAFDoc_DocumentTool::MaterialTool(doc->Main());
// Add the root shape to the document
TDF_Label shapeLabel = shapeTool->AddShape(shape);
// Set the name using TDataStd_Name attribute
TDataStd_Name::Set(shapeLabel, name.c_str());
if (material.GetName() != "")
{
std::vector<double> color;
if (material.GetAttribute(color, "Color"))
{
Quantity_Color col(color[0], color[1], color[2], Quantity_TOC_RGB);
// Set the color
colorTool->SetColor(shapeLabel, col, XCAFDoc_ColorGen);
}
Handle(TCollection_HAsciiString) matName = new TCollection_HAsciiString(material.GetName().c_str());
Handle(TCollection_HAsciiString) matDes = new TCollection_HAsciiString(material.GetName().c_str());
double density = 0.0;
if (material.GetAttribute(density, "Density"))
{
TDF_Label matLabel = materialTool->AddMaterial(matName, matDes, density, matDes, matDes);
materialTool->SetMaterial(shapeLabel, matLabel);
}
}
for (TopExp_Explorer itf(shape, TopAbs_FACE); itf.More(); itf.Next())
{
TopoDS_Face face = TopoDS::Face(itf.Current());
Quantity_Color color;
if (!colorTool->SetColor(face, color, XCAFDoc_ColorSurf))
{
TDF_Label faceLabel;
if (!shapeTool->FindSubShape(shapeLabel, face, faceLabel))
{
faceLabel = shapeTool->AddSubShape(shapeLabel, face);
}
colorTool->SetColor(faceLabel, Quantity_Color(1, 0, 0, Quantity_TOC_RGB), XCAFDoc_ColorSurf);
}
}
// Write to STEP file
STEPCAFControl_Writer writer;
writer.Transfer(doc, STEPControl_AsIs);
if (writer.Write(fileName.c_str()) != IFSelect_RetDone) {
std::cout << "Failed to write STEP file." << std::endl;
}
}