Can't get TDF_Label from TopoDS_Shape (version OCC 7.5.0)

Hi everyone! As for the title, I want to get the TDF_Label from TopoDS_Shape, so I use the XCAFDoc_ShapeTool::FindShape, but it doesn't work, there is my code:

// Get shape
TopoDS_Shape shape = myAISContext()->DetectedShape(); 

// shapeL gets from TDoc_Std_Document::Main()
Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(shapeL);

// becasue I know the type of shape I choose is edge, so I transform to TopoDS_Edge
const TopoDS_Edge edge = TopoDS::Edge(shape);

TDF_Label aLabel = myAssembly->FindShape(edge);

if (aLabel.IsNull()) 
{ 
    cout << "No Label" << endl;
} 
else
{ 
    cout << "Get Label" << endl;
}

And the result returns always "No Label".

Could anyone help me? Thanks.

HSIEN-SHUN HSU's picture

Thanks, everyone, I solve it by myself. There is my code:

TCollection_AsciiString findName(const TDF_Label& theLabel,  Handle(XCAFDoc_ShapeTool) aShapeTool, const TopoDS_Shape& shape)
    {
        for (TDF_ChildIterator Father(theLabel, Standard_True); Father.More(); Father.Next())
        {
            TDF_Label Child = Father.Value();
            if (aShapeTool->IsShape(Child))
            {
                TopoDS_Shape aShape = aShapeTool->GetShape(Child);
                if (shape.IsSame(aShape))
                {
                    TCollection_AsciiString node;
                    TDF_Tool::Entry(Child, node);
                    return node;
                }
            }
        }

        return "null";
    }
Mikhail MPV's picture

It is hard to understand the source of the problem in your initial example, because we don't know the content of original file and the shape you have got from AISContext. I may only guess that the problem in the second argument of FindShape method. "findInstance = Standard_False" by default, so, it removes the location of the searched shape. Perhaps, if you set it to True, it will work in the same way as your method, but in some more optimized way.