+#ifdef DEB_GET_REGION_SHAPE
+ HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfGroups );
+#endif
+
+ TopoDS_Shape aResShape;
+
+ // Unite the region zones (each zone is a face) into one face (united face)
+ // If the zones can't be united into the single face - unite them into shell
+
+ // Collect the list of region faces
+ TopTools_ListOfShape aRegionFacesList;
+
+ HYDROData_SequenceOfObjects aZones = GetZones();
+ HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
+ for ( ; aZoneIter.More(); aZoneIter.Next() )
+ {
+ Handle(HYDROData_Zone) aZone =
+ Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
+ if ( aZone.IsNull() )
+ continue;
+
+ TopoDS_Shape aZoneShape = aZone->GetShape();
+ if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
+ continue;
+
+ TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
+ aRegionFacesList.Append( aZoneFace );
+
+ getUsedGroups( aZoneFace, aSeqOfGroups, aSeqOfUsedGroups );
+ } // zones iterator
+
+ if ( aRegionFacesList.IsEmpty() )
+ return aResShape;
+
+ // The unite region face
+ TopoDS_Face aRegionFace;
+
+ if ( aRegionFacesList.Extent() == 1 )
+ {
+ aRegionFace = TopoDS::Face( aRegionFacesList.First() );
+ }
+ else
+ {
+#ifdef DEB_GET_REGION_SHAPE
+ HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
+#endif
+
+ // Try to fuse all region faces into one common face
+ TopoDS_Shape aFuseShape;
+ TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
+ for ( ; aFaceIter.More(); aFaceIter.Next() )
+ {
+ if ( aFuseShape.IsNull() )
+ {
+ aFuseShape = aFaceIter.Value();
+ continue;
+ }
+
+ BRepAlgoAPI_Fuse aFuse( aFuseShape, aFaceIter.Value() );
+ if ( !aFuse.IsDone() )
+ {
+ aFuseShape.Nullify();
+ break;
+ }
+
+ aFuseShape = aFuse.Shape();
+ HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &aFuse );
+ } // faces iterator
+
+#ifdef DEB_GET_REGION_SHAPE
+ HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Fused face edges:", aFuseShape, TopAbs_EDGE );
+#endif
+
+ // Check the result of fuse operation
+ if ( !aFuseShape.IsNull() )
+ {
+ ShapeUpgrade_UnifySameDomain anUnifier( aFuseShape );
+ anUnifier.Build();
+
+ const TopoDS_Shape& anUnitedShape = anUnifier.Shape();
+
+ TopTools_SequenceOfShape aShapeFaces;
+ HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_FACE, aShapeFaces );
+ if ( aShapeFaces.Length() == 1 )
+ {
+ aRegionFace = TopoDS::Face( aShapeFaces.Value( 1 ) );
+
+#ifdef DEB_GET_REGION_SHAPE
+ HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Result face edges:", aRegionFace, TopAbs_EDGE );
+#endif
+
+ HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &anUnifier );
+
+ // Update the sequence of groups
+ if ( theSeqOfGroups )
+ {
+ HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( aSeqOfUsedGroups );
+ for ( ; aUsedIter.More(); aUsedIter.Next() )
+ {
+ const HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.Value();
+ if ( aUsedGroupDef.Shapes.IsEmpty() )
+ continue;
+
+ HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anOriIter( aSeqOfGroups );
+ for ( ; anOriIter.More(); anOriIter.Next() )
+ {
+ HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anOriIter.ChangeValue();
+ if ( anOriGroupDef.Name != aUsedGroupDef.Name )
+ continue;
+
+ HYDROData_ShapesTool::AddShapes( anOriGroupDef.Shapes, aUsedGroupDef.Shapes );
+ break;
+ }
+ }
+
+ *theSeqOfGroups = aSeqOfGroups;
+ }
+ }
+ }
+ }
+
+ if ( !aRegionFace.IsNull() )
+ {
+ // result shape is a face
+ aResShape = aRegionFace;
+ }
+ else
+ {
+ // result shape is a shell
+ TopoDS_Shell aShell;
+ BRep_Builder aBuilder;
+ aBuilder.MakeShell( aShell );
+
+ TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
+ for ( ; aFaceIter.More(); aFaceIter.Next() )
+ aBuilder.Add( aShell, aFaceIter.Value() );
+
+ aResShape = aShell;
+ }
+
+ return aResShape;