- NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
- while(!anUngroupedShapes.IsEmpty()) {
- NCollection_List<TopoDS_Shape> aGroupedShapes;
- aGroupedShapes.Append(anUngroupedShapes.First());
- anUngroupedShapes.RemoveFirst();
- for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes);
+ // Iterate over all shapes and find shapes with shared vertices.
+ TopTools_ListOfShape aMapOrder;
+ BOPCol_DataMapOfShapeListOfShape aVertexShapesMap;
+ for(NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
+ aShapesIt.More();
+ aShapesIt.Next()) {
+ const TopoDS_Shape& aShape = aShapesIt.Value();
+ for(TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
+ aShapeExp.More();
+ aShapeExp.Next()) {
+ const TopoDS_Shape& aVertex = aShapeExp.Current();
+ if (!aVertexShapesMap.IsBound(aVertex)) {
+ NCollection_List<TopoDS_Shape> aList;
+ aList.Append(aShape);
+ aMapOrder.Append(aVertex);
+ aVertexShapesMap.Bind(aVertex, aList);
+ } else {
+ if(!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
+ aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
+ }
+ }
+ }
+ }
+
+ // Iterate over the map and group shapes.
+ NCollection_Vector<TopTools_ListOfShape> aGroups;
+ while (!aMapOrder.IsEmpty()) {
+ // Get first group of shapes in map, and then unbind it.
+ const TopoDS_Shape& aKey = aMapOrder.First();
+ TopTools_ListOfShape aGroupedShapes = aVertexShapesMap.Find(aKey);
+ aVertexShapesMap.UnBind(aKey);
+ aMapOrder.Remove(aKey);
+ // Iterate over shapes in this group and add to it shapes from groups in map.
+ for(TopTools_ListOfShape::Iterator aGroupIt(aGroupedShapes);