+// Keep only the shapes with minimal shape type
+static void keepTopLevelShapes(ListOfShape& theShapes, const TopoDS_Shape& theRoot,
+ const GeomShapePtr& theResultShape = GeomShapePtr())
+{
+ GeomAPI_Shape::ShapeType aKeepShapeType = GeomAPI_Shape::SHAPE;
+ ListOfShape::iterator anIt = theShapes.begin();
+ while (anIt != theShapes.end()) {
+ TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>();
+ if (theRoot.IsSame(aNewShape) || (theResultShape &&
+ (!theResultShape->isSubShape(*anIt, false) || theResultShape->isSame(*anIt)))) {
+ ListOfShape::iterator aRemoveIt = anIt++;
+ theShapes.erase(aRemoveIt);
+ } else {
+ GeomAPI_Shape::ShapeType aType = (*anIt)->shapeType();
+ if (aType < aKeepShapeType) {
+ // found a shape with lesser shape type => remove all previous shapes
+ aKeepShapeType = aType;
+ theShapes.erase(theShapes.begin(), anIt);
+ ++anIt;
+ } else if (aType > aKeepShapeType) {
+ // shapes with greater shape type should be removed from the list
+ ListOfShape::iterator aRemoveIt = anIt++;
+ theShapes.erase(aRemoveIt);
+ } else
+ ++anIt;
+ }
+ }
+}
+