Salome HOME
HYDROData_Obstacle has been wrapped for Python.
[modules/hydro.git] / src / HYDROData / HYDROData_Region.cxx
index 7f4fae7a41224cdc8258ca42ce07eb77c5c95faf..12103c787be22250d261167419a95149ea3ec36f 100644 (file)
@@ -1,13 +1,24 @@
 
 #include "HYDROData_Region.h"
 
+#include "HYDROData_CalculationCase.h"
 #include "HYDROData_Document.h"
 #include "HYDROData_Iterator.h"
 #include "HYDROData_Zone.h"
 
-#include <QStringList>
+#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 <TDataStd_Integer.hxx>
+#include <QStringList>
 
 #define PYTHON_REGION_ID "KIND_REGION"
 
@@ -18,7 +29,6 @@ IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
 HYDROData_Region::HYDROData_Region()
  : HYDROData_Entity()
 {
-  SetMergeType( Merge_ZMIN );
 }
 
 HYDROData_Region::~HYDROData_Region()
@@ -29,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;
 
@@ -56,69 +66,63 @@ QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjec
   return aResList;
 }
 
-Handle(HYDROData_Zone) HYDROData_Region::AddNewZone()
+bool HYDROData_Region::CanBeUpdated() const
 {
-  TDF_Label aNewLab = myLab.FindChild( ChildTag_Zone ).NewChild();
-
-  return Handle(HYDROData_Zone)::DownCast(
-    HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
+  return false;
 }
 
-void HYDROData_Region::RemoveChildZones()
+void HYDROData_Region::Remove()
 {
-  myLab.FindChild( ChildTag_Zone ).ForgetAllAttributes( true );
+  Handle(HYDROData_CalculationCase) aFatherCalc = 
+    Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
+
+  HYDROData_Entity::Remove();
+
+  if ( !aFatherCalc.IsNull() )
+    aFatherCalc->UpdateRegionsOrder();
 }
 
-void HYDROData_Region::SetMergeType( const MergeBathymetriesType& theType )
+bool HYDROData_Region::CanRemove()
 {
-  Handle(TDataStd_Integer) anInt;
-  if ( !myLab.FindChild( DataTag_MergeType ).FindAttribute( TDataStd_Integer::GetID(), anInt ) )
-    anInt = TDataStd_Integer::Set( myLab, 0 );
-  anInt->Set( (int)theType );
+  return false;
 }
 
-HYDROData_Region::MergeBathymetriesType HYDROData_Region::GetMergeType() const
+HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
 {
-  MergeBathymetriesType aMergeType = Merge_ZMIN;
-  
-  Handle(TDataStd_Integer) anInt;
-  if ( myLab.FindChild( DataTag_MergeType ).FindAttribute( TDataStd_Integer::GetID(), anInt ) )
-    aMergeType = (MergeBathymetriesType)anInt->Get();
+  HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
 
-  return aMergeType;
-}
+  HYDROData_SequenceOfObjects aSeqOfZones = GetZones();
+  aResSeq.Append( aSeqOfZones );
 
-int HYDROData_Region::NbZones() const
-{
-  return NbReferenceObjects( DataTag_Zone );
+  return aResSeq;
 }
 
-void HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
+bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
 {
-  AddReferenceObject( theZone, DataTag_Zone );
-}
+  if ( theZone.IsNull() )
+    return false;
+  
+  if ( HasReference( theZone, DataTag_Zone ) )
+    return false; // Object is already in reference list
 
-void HYDROData_Region::SetZone( const int                     theIndex,
-                                const Handle(HYDROData_Zone)& theZone )
-{
-  SetReferenceObject( theZone, DataTag_Zone, theIndex );
-}
+  // Move the zone from other region
+  Handle(HYDROData_Region) aFatherRegion = 
+    Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
+  if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
+  {
+    Handle(HYDROData_Zone) aNewZone = addNewZone();
+    theZone->CopyTo( aNewZone );
 
-void HYDROData_Region::InsertZone( const int                     theBeforeIndex,
-                                   const Handle(HYDROData_Zone)& theZone )
-{
-  InsertReferenceObject( theZone, DataTag_Zone, theBeforeIndex );
-}
+    aFatherRegion->RemoveZone( theZone );
 
-void HYDROData_Region::SetZones( const HYDROData_SequenceOfObjects& theZones )
-{
-  SetReferenceObjects( theZones, DataTag_Zone );
-}
+    theZone->SetLabel( aNewZone->Label() );
+  }
+  else
+  {
+    AddReferenceObject( theZone, DataTag_Zone );
+  }
 
-Handle(HYDROData_Zone) HYDROData_Region::GetZone( const int theIndex ) const
-{
-  return Handle(HYDROData_Zone)::DownCast( 
-           GetReferenceObject( DataTag_Zone, theIndex ) );
+  return true;
 }
 
 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
@@ -132,16 +136,116 @@ void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
     return;
 
   RemoveReferenceObject( theZone->Label(), DataTag_Zone );
-}
 
-void HYDROData_Region::RemoveZone( const int theIndex )
-{
-  RemoveReferenceObject( DataTag_Zone, theIndex );
+  // Remove zone from data model
+  Handle(HYDROData_Region) aFatherRegion = 
+    Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
+  if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
+    theZone->Remove();
+
+  // If the last zone has been removed from region we remove this region
+  HYDROData_SequenceOfObjects aRefZones = GetZones();
+  if ( aRefZones.IsEmpty() )
+    Remove();
 }
 
 void HYDROData_Region::RemoveZones()
 {
   ClearReferenceObjects( DataTag_Zone );
+  myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
+}
+
+Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
+{
+  TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
+
+  Handle(HYDROData_Zone) aNewZone =
+    Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
+  AddZone( aNewZone );
+
+  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;
+}