Obtien colors using XDE

Hi,
I'm tried to obtain the colors associated with every shape from a IGES file. but the result seems wrong, all the colors for different shape are same.

code
====================================================
Handle (XCAFDoc_ShapeTool) l_Assembly = XCAFDoc_DocumentTool::ShapeTool (Doc->Main());
TDF_LabelSequence Lab_Shapes;
l_Assembly->GetShapes(Lab_Shapes);

Handle(XCAFDoc_ColorTool) l_Colors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
Quantity_Color l_Color;

for (int i=1 ; i {
TopoDS_Shape l_Shape = l_Assembly->GetShape (Lab_Shapes.Value(i));
l_Colors->GetColor(l_Shape, XCAFDoc_ColorGen , l_Color);

// show the value of the color
std::cout std::cout std::cout std::cout ====================================================
Thanks in advance for you help!!

Yilin

Matthias Teich's picture

I am trying exactely the same, without success.
It would be nice if someone could provide some help here...

Matthias Teich's picture

I tried the following:

Handle(TDocStd_Document) Doc;
Handle(XCAFDoc_ColorTool) Colors;
Handle (XCAFDoc_ShapeTool) Assembly;

XCAFApp_Application::GetApplication()->NewDocument("MDTV-XCAF", Doc);
Assembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
Colors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
STEPCAFControl_Reader reader;
IFSelect_ReturnStatus readstat = reader.ReadFile((const Standard_CString)path.c_str());
if ( !reader.Transfer ( Doc ) )
cout << "Cannot read any relevant data from the STEP file" << endl;

TDF_LabelSequence frshapes;
Assembly->GetShapes(frshapes);
if(frshapes.Length() == 0)
cout << "No Shapes found" << endl;
if(frshapes.Length() == 1)
{
TopoDS_Shape result = Assembly->GetShape(frshapes.Value(1));
shape = result;
}
else
{
TopoDS_Compound C;
BRep_Builder B;
B.MakeCompound(C);
for(Standard_Integer i=1; i<=frshapes.Length(); i++)
{
TopoDS_Shape S = Assembly->GetShape(frshapes.Value(i));
B.Add(C,S);
}
shape = C;
}

for (TopExp_Explorer ex(shape,TopAbs_FACE); ex.More(); ex.Next())
{
Quantity_Color color;
const TopoDS_Face &face = TopoDS::Face(ex.Current());
if ( Colors->IsSet(face, XCAFDoc_ColorGen) || Colors->IsSet(face, XCAFDoc_ColorSurf) || Colors->IsSet(face, XCAFDoc_ColorCurv))
{
Colors->GetColor(face, XCAFDoc_ColorGen, color);
cout << color.Red() << " " << color.Green() << " " << color.Blue() << endl;
}
else
{
cout << "Standard color" << endl;
}
}

I would be very glad if anyone could give me a hint why I always get "Standard color" and not the color in the step file.

Best Regards,
Matthias

Patrik Mueller's picture

Hi,

you only try to get a color for XCAFDoc_ColorGen. Try also getting colors for XCAFDoc_ColorSurf and XCAFDoc_ColorCurv.

Greets,

Patrik

Matthias Teich's picture

In my code I use
if ( Colors->IsSet(face, XCAFDoc_ColorGen) || Colors->IsSet(face, XCAFDoc_ColorSurf) || Colors->IsSet(face, XCAFDoc_ColorCurv))
to find out if any color is set, but it always returns false.

Patrik Mueller's picture

oh - my failure. But you only check the faces - no compounds or other shapes.

Greets,

Patrik

Adrian's picture

Hi Matthias,

how about that piece of code:

XCAFApp_Application::GetApplication()->NewDocument("MDTV-XCAF", Doc);
Assembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
Colors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
STEPCAFControl_Reader reader;
[..]
reader.SetColorMode(true);
reader.SetNameMode(true);
reader.SetLayerMode(true);
[..]
reader.Transfer(Doc);
[..]

TDF_LabelSequence Lab_Shapes;
Assembly->GetShapes(Lab_Shapes);
[..]

Quantity_Color l_Color;

for (int i=1 ; i<= Lab_Shapes.Length(); i++) {
TDF_Label label = Lab_Shapes.Value(i);
colorTool->GetColor(l_Shape, XCAFDoc_ColorGen, l_Color);
// color is stored in l_Color....
}

Adrian's picture

sorry, forgot l_Shape :

for...
TopoDS_Shape l_Shape = shapeTool->GetShape(label);
endfor

Matthias Teich's picture

Does anyone know why Adrians code does work with IGES-files, but not with STEP-files?
I renamed the STEPCAFControl_Reader to IGESCAFControl_Reader and everything is fine with IGES-files.
When I use the STEPCAFControl_Reader with STEP-files, the reader does not find any colors.

Patrik Mueller's picture

Hi,

sounds stupid - but are you sure the STEP files have colors?

Greets,

Patrik

Matthias Teich's picture

Yes, when I convert the model to iges and use the IGESCafReader everything is fine...

Patrik Mueller's picture

Hi,

is it possible to get the STEP file?

Matthias Teich's picture
LIU's picture

I have tested these 17 STEP files.

These 4 files has a color BLACK :
1.IRB4400_60_m2000_rev1_01-9.STEP
2.IRB4400_60_m2000_rev1_01-11.STEP
3.IRB4400_60_m2000_rev1_01-14.STEP
4.IRB4400_60_m2000_rev1_01-15.STEP

The other files has a color CHOCOLATE2.
Is it right??

Matthias Teich's picture

Hi!
Yes, that fits.
Would it be possible for you to send me the piece of code that you used to read the colors? It would really help me, I cant find the mistake in my code...

The following does not work:
Handle(TDocStd_Document) Doc;
XCAFApp_Application::GetApplication()->NewDocument("MDTV-XCAF", Doc);
TDF_LabelSequence frshapes;
Handle(XCAFDoc_ColorTool) Colors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
Handle (XCAFDoc_ShapeTool) Assembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
STEPCAFControl_Reader reader;
reader.SetColorMode(true);
reader.SetNameMode(true);
reader.SetLayerMode(true);
IFSelect_ReturnStatus readstat = reader.ReadFile((const Standard_CString)path.c_str());
if ( !reader.Transfer ( Doc ) ) {
QMessageBox::information( 0, "Problem", "Cannot read any relevant data from the STEP file" );
}

Assembly->GetFreeShapes(frshapes);
if(frshapes.Length() == 0)
QMessageBox::information( 0, "Problem", "No Shapes found" );
if(frshapes.Length() == 1)
{
shape = Assembly->GetShape(frshapes.Value(1));
}
else
{
TopoDS_Compound C;
BRep_Builder B;
B.MakeCompound(C);
for(Standard_Integer i=1; i<=frshapes.Length(); i++) {
TopoDS_Shape S = Assembly->GetShape(frshapes.Value(i));
B.Add(C,S);
}
shape = C;
}

Quantity_Color color;
for (int i=1 ; i<= frshapes.Length(); i++)
{
TDF_Label label = frshapes.Value(i);
TopoDS_Shape cshape = Assembly->GetShape(label);
if(Colors->GetColor(cshape, XCAFDoc_ColorGen, color)){
QMessageBox::information( 0, "Color", color.StringName(color.Name()) );
}
}

LIU's picture

Hi,
First, I have tested the type of color, apparently it is XCAFDoc_ColorSurf, so you can try to replace Colors->GetColor(cshape, XCAFDoc_ColorGen, color) by Colors->GetColor(cshape, XCAFDoc_ColorSurf, color).

the following is my code
=======================================================================
STEPCAFControl_Reader reader;
int l_stat = reader.ReadFile("filename");
Handle(TDocStd_Document) Doc;
XCAFApp_Application::GetApplication()->NewDocument("MDTV-XCAF", Doc);
if (!reader.Transfer(Doc))
{
std::cout<<"Cannot read any relevant data from the STEP file"<Main());
Handle(XCAFDoc_ColorTool) l_ColorTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
TDF_LabelSequence Lab_Shapes;
l_ShapeTool ->GetShapes(Lab_Shapes);
for (int i=1 ; i<= Lab_Shapes.Length() ; i++)
{
TDF_Label l_ColorLabel;
if( l_ColorTool->GetColor(Lab_Shapes.Value(i), XCAFDoc_ColorGen, l_ColorLabel) ||
l_ColorTool->GetColor(Lab_Shapes.Value(i), XCAFDoc_ColorSurf, l_ColorLabel) ||
l_ColorTool->GetColor(Lab_Shapes.Value(i), XCAFDoc_ColorCurv, l_ColorLabel) )

{
// top-level (i) has a color ...
}
if (Lab_Shapes.Value(i).HasChild())
{
TDF_ChildIterator it;
for(it.Initialize(Lab_Shapes.Value(i)); it.More; it.Next)
{
TDF_Label l_SubColorLabel;
if(l_ColorTool->GetColor(it.Value(), XCAFDoc_ColorGen, l_SubColorLabel) ||
l_ColorTool->GetColor(it.Value(), XCAFDoc_ColorSurf, l_SubColorLabel) ||
l_ColorTool->GetColor(it.Value(), XCAFDoc_ColorCurv, l_SubColorLabel) )
{
// SubShape has a color ...
}

}

}
}
=========================================================================

LIU's picture

Hi, Matthias

I've settled my problem by searching all the subshapes of top-level shapes, because Assembly->GetShapes(frshapes);

just obtain all top-level shapes, but sometimes the colors are attached to the subshapes.

Code for obtaining the child labels of a top-level shape
=================================================================================
if (frshapes.Value(i).HasChild())
{
TDF_ChildIterator iterator;
for (iterator.Initialize(frshapes.Value(i)) ; iterator.More(); iterator.Next())
{
...
}
}
=================================================================================

I'm a beginner OpenCASCADE, my opinion may be wrong. but I hope this can help you !!!

Alejandro's picture

def colour_chercher(label,color_tool,shape_tool):
c=Quantity_Color()
if( color_tool.GetInstanceColor(shape_tool.GetShape(label),0,c) or
color_tool.GetInstanceColor(shape_tool.GetShape(label),1,c) or
color_tool.GetInstanceColor(shape_tool.GetShape(label),2,c)):

......

Try with this, it works for my

570143426_157742's picture

void visit(const TDF_Label& theLabel)

{

//theLabel.EntryDump(std::cout);

Handle(TDataStd_Name) aName;

if (theLabel.FindAttribute(TDataStd_Name::GetID(), aName))

{

std::cout << "  Name: " << aName->Get() << std::endl;

}

/*if (aColorTool->IsSet(theLabel, XCAFDoc_ColorGen))*/

if (aColorTool->IsSet(theLabel, XCAFDoc_ColorGen) || aColorTool->IsSet(theLabel, XCAFDoc_ColorSurf) || aColorTool->IsSet(theLabel, XCAFDoc_ColorCurv))

{

TopoDS_Shape ashape = aShapeTool->GetShape(theLabel);  

Quantity_Color aColor;

aColorTool->GetColor(ashape, XCAFDoc_ColorGen, aColor);

std::cout << "  Color: " << Quantity_Color::StringName(aColor.Name()) << std::endl;

//ColorStore[i] = string(Quantity_Color::StringName(aColor.Name()));

}

for (TDF_ChildIterator c(theLabel); c.More(); c.Next())

{

visit(c.Value());

}

}

try this