Getting the TDF_Label from Selected Shape

I am having trouble figuring out how to get the TDF_Label from the selected object.

I am trying to display the label when a user selects a shape in the viewer.

TopoDS_Shape selectedshape = myContext->SelectedShape();

Gets the selected shape but I cannot figure out how to get the label from the TopoDS_Shape.

I've also tried to iterate over the labels and get the shape from there. Unfortunately the TDF_Label doesn't seem to tie to the displayed ais_interactiveobjects in a way that I can determine.

TDF_LabelSequence aLabels;
aShapeTool->getShapes(aLabels);

for (standard_integer aLabIter = 1; aLabIter <= aLabels.Length(); ++aLabIter) {
  // Check if matching shape is found
  const TDF_Label& aLabel = aLabels.Value(aLabIter);
  TopoDS_Shape shape;
  aShapeTool->GetShape(aLabel, shape);

  //check if same - no match found here
  if (shape.IsSame(selectedshape)) {
    std::cout << "Selected shape matches.\n";
  }

  // Get node name
  Handle(TDataStd_Name) aNodeName;
  TCollection_AsciiString aName;
  {
      if (aLabel.FindAttribute(TDataStd_Name::GetID(), aNodeName)) {
        aName = aNodeName->Get();
      }
      if (aName.IsEmpty()) {
        TDF_Label aRefLabel;
        if (XCAFDoc_ShapeTool::GetReferredShape(aLabel, aRefLabel) && aRefLabel.FindAttribute(TDataStd_Name::GetID(), aNodeName)) {
           aName = aNodeName->Get();
        }
      }
  }

  // Check if we found our matching label we were trying to print out
  if (aName.IsEqual("TableLeg") {
    std::cout << "Found matching TableLeg label \n";
  }
}

Can someone please help me figure out how to get a label from a selected object?

Thank you

Jeff Foster's picture

Looking at it from the reverse, trying to get the TDF_Label from a AIS_InteractiveObject, I am not having any luck being able to map these two pieces.

MapOfPrsForShapes  myMap;  //set up in displayWithChildren function
if (aName.IsEqual("TableLeg")) {
  TDF_Label label = aNodeName->Label();
  Handle(AIS_InteractiveObject) anAis;
  if (myMap.Find (aLabel, anAis)) {  // myMap is 
    std::cout << " Found it you monster\n";
    Handle(AIS_InteractiveObject) theAis;
    theAis = myMap.Find(aLabel);
    //theAis->DumpJson(std::cout);
    std::cout << "Display status: " << theAis->DisplayStatus() <<  "\n"; // prints 2 meaning not displayed     
    // So highlighting here doesn't work          
    myContext->AddSelected(theAis);
    myContext->HilightSelected(Standard_True);
  }
}

Below I can iterate through all the interactive objects on the screen and it will highlight them but I am not able to check their labels or find the label

AIS_ListOfInteractive aisList;
myContext->ObjectsInside(aisList);

int i = 0;
std::cout << "List Of Interactive size: " << aisList.Size() << "\n";
//Iterate over objects
if (aisList.Size() > 0) {
  for (AIS_ListIteratorOfListOfInteractive aLI(aisList); aLI.More(); aLI.Next()) {
    if (aLI.Value()->Type() == AIS_KindOfInteractive::AIS_KindOfInteractive_Object) {
      i++;
      if (i == 77) { // limit output
        aLI.Value()->DumpJson(std::cout);
      }
      myContext->AddSelect(aLI.Value()); //highlight all objects for fun
    }           
  } 
} else {
  std::cout << "No objects inside?\n";
}
myContext->HilightSelected(Standard_True);

This highlights everything but dumping the data or using any functions on the AIS_InteractiveObject I can't see how to just get the one TDF_Label I want to highlight.

Any thoughts?

David Swall's picture

there is no method get TDF_Label from shape