+const TopoDS_Shape findCommonShape(
+ const TopAbs_ShapeEnum theType, const TopTools_ListOfShape& theList)
+{
+ if(theList.Extent() < 1) {
+ return TopoDS_Shape();
+ } else if (theList.Extent() == 1) { // check that sub-shape is bounded by this alone shape
+ TopTools_MapOfShape aSubsInShape;
+ TopExp_Explorer anExp(theList.First(), theType);
+ for(; anExp.More(); anExp.Next()) {
+ if (aSubsInShape.Contains(anExp.Current())) { // found duplicate
+ return anExp.Current();
+ }
+ aSubsInShape.Add(anExp.Current());
+ }
+ }
+
+ // Store in maps sub-shapes from each face.
+ std::vector<TopTools_MapOfShape> aVec;
+ for(TopTools_ListIteratorOfListOfShape anIt(theList); anIt.More(); anIt.Next()) {
+ const TopoDS_Shape aFace = anIt.Value();
+ TopTools_MapOfShape aMap;
+ for(TopExp_Explorer anExp(aFace, theType); anExp.More(); anExp.Next()) {
+ const TopoDS_Shape& aSubShape = anExp.Current();
+ aMap.Add(anExp.Current());
+ }
+ aVec.push_back(aMap);
+ }
+
+ // Find sub-shape shared between all faces.
+ TopoDS_Shape aSharedShape;
+ for(TopTools_MapIteratorOfMapOfShape anIt(aVec[0]); anIt.More(); anIt.Next()) {
+ const TopoDS_Shape& aSubShape = anIt.Value();
+ int aSharedNb = 1;
+ for(int anIndex = 1; anIndex < aVec.size(); ++anIndex) {
+ if(aVec[anIndex].Contains(aSubShape)) {
+ ++aSharedNb;
+ }
+ }
+ if(aSharedNb == theList.Extent()) {
+ if(aSharedShape.IsNull()) {
+ aSharedShape = aSubShape;
+ } else {
+ // More than one shape shared between all faces, return null shape in this case.
+ return TopoDS_Shape();
+ }
+ }
+ }
+
+ return aSharedShape;
+}
+