Finding connected faces of a CAD model

I have two types of sheet metal CAD models Faces; ModelFace (PLANAR faces) and ModelBend (NON-PLANAR, exist between ModelFaces and represent the bends of a sheetmetal). These faces are stored in seperate vectors. What I want to do is to find the faces that each bend connects to. Each face and bend is assigned an positive  and non zero integer ID. The desired output is something like this :

    F1----B1-----F2 Angle : 90  Radius : 4
    F2----B2-----F3 Angle : 90  Radius : 4

Meaning that bend 1, of radius 4mm connects face 1 and face 2 at a 90 degree angle and so on. However, I am getting a result shown below for most models regardless of their format (step/igs) :

    F1---B1---F2 Angle : 90 Radius : 4
    F3---B2---F0 Angle : 0 Radius : 4
    F4---B3---F5 Angle : 90 Radius : 1
    F0---B4---F0 Angle : 0 Radius : 1

A result like `F3---B2---F0` means that the program has detected that B2 is connected to face 3 but fails to identify the other connected face hence F0 value. Also for some model an output may be `F0---B2---F0` which means that no face was found that connects to bend 2.

A bend has two straight line edges that connect to faces as shown in the diagram below:

[![enter image description here][1]][1]
This was my initial solution buts it doesn't capture all the connections e.g I am getting information that some bends are connected to a face on one edge and not on the other edge:

```
for (auto& bend: mModelBends){
  for (auto& edge: bend.getFaceEdges()){
    if (edge.getEdgeType() == EdgeType::LINE) {
      TopoDS_Edge anEdge = edge.getTModelEdge(); // returns a TopoDS_Edge from the edge object

      for (auto& face: mModelFaces)
      {
        if (face.getFaceType() == FaceType::NONE) {
          TopoDS_Face aFace = face.getTModelFace(); // returns a TopoDS_Face from the face object

          for( TopExp_Explorer anExp(aFace, TopAbs_EDGE); anExp.More(); anExp.Next()) {
            if(anExp.Current().IsSame(anEdge)) {
              // Do something.........
            }
          }

        }
      }
    }
  }
}
```

Another solution found on [OpenCascade Forum][2]:

```
TopTools_IndexedDataMapOfShapeListOfShape edgeFaceMap;

// mModelShape is a TopoDS_Shape with the entire model
TopExp::MapShapesAndAncestors(mModelShape, TopAbs_EDGE, TopAbs_FACE, edgeFaceMap);

for (auto& bend: mModelBends){
  for (auto& edge: bend.getFaceEdges()){
    if (edge.getEdgeType() == EdgeType::LINE) {
      TopoDS_Edge anEdge = edge.getTModelEdge();
      TopoDS_Shape anAdjFaceObj;

      // Find adjacent face
      for (auto& a : mModelFaces)
      {
        bool faceFound = TopOpeBRepBuild_Tools::GetAdjacentFace(a.getTModelFace(), anEdge,
        edgeFaceMap, anAdjFaceObj);

        if (faceFound)
        {
           // Do something.........
        }
      }
    }
  }
}
```
The second solution crashes after executing GetAdjacentFace() for the first time.

The problem might because I am not understanding what the docs are saying about MapShapesAndUniqueAncestors() and GetAdjacentFace():

```
 TopExp::MapShapesAndUniqueAncestors(const TopoDS_Shape &S,
         const TopAbs_ShapeEnum  TS,
         const TopAbs_ShapeEnum  TA,
        TopTools_IndexedDataMapOfShapeListOfShape &M,
         const Standard_Boolean  useOrientation = Standard_False
      )     
```
> Stores in the map M all the subshape of S of type TS for each
> one append to the list all unique ancestors of type TA. For example
> map all the edges and bind the list of faces. useOrientation = True :
> taking account the ancestor orientation Warning: The map is not
> cleared at first.

Need help with a correction or better solution and/or clarity on the docs. Thanks in advance.

Attachments: 
chandrasekhar yammanuri's picture

How you gathered planar and non planar faces seperately ?