How can i find ancestors of a TopoDS_Vertex

I use AIS_InteractiveContext's Vertex Mode to get a TopoDS_Vertex By MouseClick。

But i don't no how to get it's parent shape.

1.What I do now is to traverse every known TopoDS_ Shape, and get child Vertex to compare with my click Vertex.
However, this method will birng serious efficiency problem when there are a lot of TopoDS_ Shape.

2. I try to call MapShapesAndAncestors Mthod to build a map. But i don't know the rootShape of the selected TopoDS_Vertex.(Actuallly the rootShape is what i'm looking for.)

Similar Question: https://dev.opencascade.org/content/how-many-parent-face-edge

Does anyone know how I can get this information ?

Thank you.

Kirill Gavrilov's picture

TopoDS_Shape stores only children shapes, not parents. If you would like to find parent(s) of a picked shape, you will need to explode the displayed shape using TopExp_Explorer or using TopExp::MapShapesAndAncestors() as suggested in linked question.

Of course, the same TopoDS_Vertex would normally belong to more than a one parent shape, as they are shared between connected TopoDS_Edge in the TopoDS_Wire.

wang yufeng's picture

Thank you for your reply!

If i use TopExp::MapShapesAndAncestors() Method,i should get [Rootshape] to establish mapping relationship.

But my goal is to get RootShape(TopoDS_Vertex's Parent Shape) QQ

It seems that the only way I can do should be [explode the displayed shape using TopExp_Explorer]?

Kirill Gavrilov's picture

TopExp::MapShapesAndAncestors() wouldn't return a "root shape" - that wouldn't make any sense, as the root would be always the shape passed to function itself. What it returns is the collection of direct ancestors of sub-shapes of specific type. For instance, the parent of TopoDS_Vertex would normally be a TopoDS_Edge, so that map can be used like that:

Handle(AIS_Shape) theShapePrs;
Handle(AIS_InteractiveContex) theCtx;
//
const TopoDS_Shape aRootShape = theShapePrs->Shape();
TopTools_IndexedDataMapOfShapeListOfShape aVertMap;
TopExp::MapShapesAndAncestors (aRootShape, TopAbs_VERTEX, TopAbs_EDGE, aVertMap);
//
const Handle(SelectMgr_EntityOwner)& aDetOwner = theCtx->DetectedOwner()
if (Handle(StdSelect_BRepOwner) aPickedBRepOwner = Handle(StdSelect_BRepOwner)::DownCast (aDetOwner))
{
  const TopoDS_Shape& aPickedShape = aPickedBRepOwner->Shape();
  if (aPickedShape.ShapeType() == TopAbs_VERTEX)
  {
    if (TopTools_ListOfShape* aParentEdges = aVertMap.Seek (aPickedShape))
    {
      for (TopTools_ListOfShape::Iterator anEdgeIter (*aParentEdges); anEdgeIter.More(); anEdgeIter.Next())
      {
        const TopoDS_Edge anEdge = TopoDS::Edge (anEdgeIter.Value());
      }
    }
  }
}

Internally TopExp::MapShapesAndAncestors()  uses the same TopExp_Explorer / TopoDS_Iterator tools to fill in the mape, so that this map is useful for multiple lookups (performance optimization) or just for convenience.