Salome HOME
Debug minor changes.
[modules/hydro.git] / src / HYDROData / HYDROData_Region.cxx
index 40d193b575b7cedd1e4050fa1d31caed30764211..090171f9e8b09619801de27ad7976baa15c1c306 100644 (file)
@@ -1,10 +1,23 @@
 
 #include "HYDROData_Region.h"
 
+#include "HYDROData_CalculationCase.h"
 #include "HYDROData_Document.h"
 #include "HYDROData_Iterator.h"
 #include "HYDROData_Zone.h"
 
+#include <TopoDS.hxx>
+#include <TopoDS_Shape.hxx>
+#include <TopoDS_Shell.hxx>
+#include <TopoDS_Face.hxx>
+#include <TopTools_ListOfShape.hxx>
+#include <TopTools_ListIteratorOfListOfShape.hxx>
+#include <TopTools_IndexedMapOfShape.hxx>
+#include <TopExp.hxx>
+#include <BRep_Builder.hxx>
+#include <BRepAlgoAPI_Fuse.hxx>
+#include <ShapeUpgrade_UnifySameDomain.hxx>
+
 #include <QStringList>
 
 #define PYTHON_REGION_ID "KIND_REGION"
@@ -26,7 +39,7 @@ QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjec
 {
   QStringList aResList;
 
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
   if ( aDocument.IsNull() )
     return aResList;
 
@@ -53,6 +66,17 @@ QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjec
   return aResList;
 }
 
+void HYDROData_Region::Remove()
+{
+  Handle(HYDROData_CalculationCase) aFatherCalc = 
+    Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
+
+  HYDROData_Entity::Remove();
+
+  if ( !aFatherCalc.IsNull() )
+    aFatherCalc->UpdateRegionsOrder();
+}
+
 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
 {
   if ( theZone.IsNull() )
@@ -73,8 +97,11 @@ bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
 
     theZone->SetLabel( aNewZone->Label() );
   }
+  else
+  {
+    AddReferenceObject( theZone, DataTag_Zone );
+  }
 
-  AddReferenceObject( theZone, DataTag_Zone );
   return true;
 }
 
@@ -119,3 +146,86 @@ Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
   return aNewZone;
 }
 
+TopoDS_Shape HYDROData_Region::GetShape() const
+{
+  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_Face aFace = TopoDS::Face( aZone->GetShape() );
+    if ( !aFace.IsNull() ) {
+      aRegionFacesList.Append( aFace );
+    }
+  } // zones iterator
+  
+  // Check number of faces
+  int aNbFaces = aRegionFacesList.Extent();
+  if ( aNbFaces > 0 ) {
+    // The unite region face
+    TopoDS_Face aRegionFace;
+  
+    if ( aNbFaces == 1 ) {
+      aRegionFace = TopoDS::Face( aRegionFacesList.First() );
+    } else {
+      // Fuse faces into one
+      TopoDS_Shape aFuseShape;
+      TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
+      for ( ; aFaceIter.More(); aFaceIter.Next() ) {
+        if ( aFuseShape.IsNull() ) {
+          aFuseShape = aFaceIter.Value();
+        } else {
+          BRepAlgoAPI_Fuse aFuse(aFuseShape, aFaceIter.Value());
+          if ( !aFuse.IsDone() ) {
+            aFuseShape.Nullify();
+            break;
+          }
+          aFuseShape = aFuse.Shape();
+        }
+      } // faces iterator
+
+      // Check the result of fuse operation
+      if ( !aFuseShape.IsNull() ) {
+        ShapeUpgrade_UnifySameDomain anUnifier( aFuseShape );
+        anUnifier.Build();
+        TopoDS_Shape anUnitedShape = anUnifier.Shape();
+
+        TopTools_IndexedMapOfShape aMapOfFaces;
+        TopExp::MapShapes( anUnitedShape, TopAbs_FACE, aMapOfFaces );
+        if ( aMapOfFaces.Extent() == 1 ) {
+          aRegionFace = TopoDS::Face( aMapOfFaces(1) );
+        }
+      }
+    }
+
+    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;
+}