Salome HOME
Merge remote-tracking branch 'origin/BR_SHAPE_RECOGNITION' into BR_v14_rc
[modules/hydro.git] / src / HYDROData / HYDROData_Region.cxx
index 6b2c19742fa59ae90f6aae0dc07edde96ca26ec5..0cbb266d27f1f5388730078fb6768ac00ee58b67 100644 (file)
@@ -1,11 +1,30 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "HYDROData_Region.h"
 
 #include "HYDROData_CalculationCase.h"
 #include "HYDROData_Document.h"
 #include "HYDROData_Iterator.h"
+#include "HYDROData_Object.h"
 #include "HYDROData_ShapesTool.h"
 #include "HYDROData_Zone.h"
+#include "HYDROData_Tool.h"
 
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
@@ -17,6 +36,7 @@
 #include <TopTools_ListOfShape.hxx>
 #include <TopTools_SequenceOfShape.hxx>
 #include <TopTools_ListIteratorOfListOfShape.hxx>
+#include <TopTools_IndexedMapOfShape.hxx>
 
 #include <BRep_Builder.hxx>
 #include <BRepAlgoAPI_Fuse.hxx>
@@ -25,6 +45,8 @@
 
 #include <QStringList>
 
+//#define DEB_GET_REGION_SHAPE
+
 IMPLEMENT_STANDARD_HANDLE(HYDROData_Region, HYDROData_Entity)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
 
@@ -38,25 +60,6 @@ HYDROData_Region::~HYDROData_Region()
 {
 }
 
-QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
-{
-  QStringList aResList = HYDROData_Entity::DumpToPython( theTreatedObjects );
-  QString aRegionName = GetName();
-
-  HYDROData_SequenceOfObjects aZones = GetZones();
-  HYDROData_SequenceOfObjects::Iterator anIter( aZones );
-  for ( ; anIter.More(); anIter.Next() )
-  {
-    Handle(HYDROData_Zone) aRefZone =
-      Handle(HYDROData_Zone)::DownCast( anIter.Value() );
-    if ( !aRefZone.IsNull() )
-      setPythonReferenceObject( theTreatedObjects, aResList, aRefZone, "AddZone" );
-  }
-  aResList << QString( "" );
-
-  return aResList;
-}
-
 bool HYDROData_Region::CanBeUpdated() const
 {
   return false;
@@ -90,6 +93,8 @@ HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
 
 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
 {
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
+
   if ( theZone.IsNull() )
     return false;
   
@@ -101,9 +106,12 @@ bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
   {
-    Handle(HYDROData_Zone) aNewZone = addNewZone();
+    Handle(HYDROData_Zone) aNewZone = addNewZone( aDocument, "", TopoDS_Face(), QStringList() );
     theZone->CopyTo( aNewZone );
 
+    // To prevent changing of stored shape
+    aNewZone->SetShape( theZone->GetShape() );
+
     aFatherRegion->RemoveZone( theZone );
 
     theZone->SetLabel( aNewZone->Label() );
@@ -146,7 +154,10 @@ void HYDROData_Region::RemoveZones()
   myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
 }
 
-Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
+Handle(HYDROData_Zone) HYDROData_Region::addNewZone( const Handle(HYDROData_Document)& theDoc,
+                                                     const QString& thePrefix,
+                                                     const TopoDS_Face& theFace,
+                                                     const QStringList& theRefObjects )
 {
   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
 
@@ -154,15 +165,91 @@ Handle(HYDROData_Zone) HYDROData_Region::addNewZone()
     Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
   AddZone( aNewZone );
 
+  QString aZoneName = HYDROData_Tool::GenerateObjectName( theDoc, thePrefix );
+  aNewZone->SetName( aZoneName );
+
+  aNewZone->SetShape( theFace );
+
+  // Add the reference object for zone
+  for ( int i = 0, n = theRefObjects.length(); i < n; ++i )
+  {
+    const QString& anObjName = theRefObjects.at( i );
+    Handle(HYDROData_Object) aRefObject = 
+      Handle(HYDROData_Object)::DownCast( theDoc->FindObjectByName( anObjName ) );
+    if ( aRefObject.IsNull() )
+      continue;
+
+    aNewZone->AddGeometryObject( aRefObject );
+  }
+
   return aNewZone;
 }
 
+void getUsedGroups( const TopoDS_Shape&                     theShape,
+                    HYDROData_ShapesGroup::SeqOfGroupsDefs& theOriGroups,
+                    HYDROData_ShapesGroup::SeqOfGroupsDefs& theUsedGroups )
+{
+#ifdef DEB_GET_REGION_SHAPE
+  HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Zone face edges:", theShape, TopAbs_EDGE );
+#endif
+
+  TopTools_IndexedMapOfShape aMapOfSubShapes;
+  TopExp::MapShapes( theShape, TopAbs_EDGE, aMapOfSubShapes );
+
+  HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anIter( theOriGroups );
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anIter.ChangeValue();
+    if ( anOriGroupDef.Shapes.IsEmpty() )
+      continue;
+
+    for ( int i = 1; i <= anOriGroupDef.Shapes.Length(); ++i )
+    {
+      TopoDS_Shape aGroupEdge = anOriGroupDef.Shapes.Value( i );
+      
+      int aShapeIndex = aMapOfSubShapes.FindIndex( aGroupEdge );
+      if ( aShapeIndex <= 0 )
+        continue;
+
+      anOriGroupDef.Shapes.Remove( i );
+      --i;
+
+      bool anIsAdded = false;
+
+      HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( theUsedGroups );
+      for ( ; aUsedIter.More(); aUsedIter.Next() )
+      {
+        HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.ChangeValue();
+        if ( aUsedGroupDef.Name != anOriGroupDef.Name )
+          continue;
+
+        aUsedGroupDef.Shapes.Append( aGroupEdge );
+        anIsAdded = true;
+        break;
+      }
+
+      if ( !anIsAdded )
+      {
+        HYDROData_ShapesGroup::GroupDefinition aUsedGroupDef;
+        aUsedGroupDef.Name = anOriGroupDef.Name;
+        aUsedGroupDef.Shapes.Append( aGroupEdge );
+        theUsedGroups.Append( aUsedGroupDef );
+      }
+    }
+  }
+}
+
 TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs* theSeqOfGroups ) const
 {
   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroups;
+  HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfUsedGroups;
   if ( theSeqOfGroups )
     aSeqOfGroups = *theSeqOfGroups;
 
+#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)
@@ -186,6 +273,8 @@ TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs*
 
     TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
     aRegionFacesList.Append( aZoneFace );
+
+    getUsedGroups( aZoneFace, aSeqOfGroups, aSeqOfUsedGroups );
   } // zones iterator
   
   if ( aRegionFacesList.IsEmpty() )
@@ -200,6 +289,10 @@ TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs*
   }
   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 );
@@ -219,9 +312,13 @@ TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs*
       }
 
       aFuseShape = aFuse.Shape();
-      HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfGroups, &aFuse );
+      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() )
     {
@@ -235,11 +332,37 @@ TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs*
       if ( aShapeFaces.Length() == 1 )
       {
         aRegionFace = TopoDS::Face( aShapeFaces.Value( 1 ) );
-        HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfGroups, &anUnifier );
 
-        // temporary commented because of needs of testing
-        //if ( theSeqOfGroups )
-          //*theSeqOfGroups = aSeqOfGroups;
+#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;
+        }
       }
     }
   }
@@ -265,3 +388,68 @@ TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs*
   
   return aResShape;
 }
+
+QStringList HYDROData_Region::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+{
+  QStringList aResList;
+
+  // Find region
+  findPythonReferenceObject( theTreatedObjects, aResList );
+
+  // Add zones
+  HYDROData_SequenceOfObjects aZones = GetZones();
+  HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
+  for ( ; aZonesIter.More(); aZonesIter.Next() ) {
+    Handle(HYDROData_Zone) aZone =
+      Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
+    if ( aZone.IsNull() ) {
+      continue;
+    }
+    
+    // find zone
+    aZone->findPythonReferenceObject( theTreatedObjects, aResList );
+    theTreatedObjects.insert( aZone->GetName(), aZone );
+    
+    // set zone merge type
+    QString aMergeTypeStr;
+    HYDROData_Zone::MergeAltitudesType aMergeType = aZone->GetMergeType();
+    if ( aMergeType == HYDROData_Zone::Merge_ZMIN ) {
+      aMergeTypeStr = "HYDROData_Zone.Merge_ZMIN";
+    } else if ( aMergeType == HYDROData_Zone::Merge_ZMAX ) {
+      aMergeTypeStr = "HYDROData_Zone.Merge_ZMAX";
+    } else if ( aMergeType == HYDROData_Zone::Merge_Object ) {
+      aMergeTypeStr = "HYDROData_Zone.Merge_Object";
+    }
+
+    if ( !aMergeTypeStr.isEmpty() ) {
+      aResList << QString( "%1.SetMergeType( %2 )" ).arg( aZone->GetObjPyName() ).arg( aMergeTypeStr );
+    }
+    if ( aMergeType == HYDROData_Zone::Merge_Object ) {
+      Handle(HYDROData_IAltitudeObject) aMergeAltitude = aZone->GetMergeAltitude();
+      if ( !aMergeAltitude.IsNull() ) {
+        aMergeAltitude->findPythonReferenceObject( theTreatedObjects, aResList );
+        aResList << QString( "%1.SetMergeAltitude( %2 )" ).arg( aZone->GetObjPyName() )
+                                                          .arg( aMergeAltitude->GetObjPyName() );
+      }
+    }
+
+    // add zone
+    setPythonReferenceObject( theTreatedObjects, aResList, aZone, "AddZone" );
+  }
+
+  return aResList;
+}
+
+bool HYDROData_Region::IsSubmersible() const
+{
+  HYDROData_SequenceOfObjects aZones = GetZones();
+  HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
+  for ( ; aZonesIter.More(); aZonesIter.Next() )
+  {
+    Handle(HYDROData_Zone) aZone =
+      Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
+    if ( !aZone->IsSubmersible() )
+      return false; //if one of zones is not submersible the region is considered as not submersible
+  }
+  return true;
+}