Salome HOME
merge BR_v14_rc
[modules/hydro.git] / src / HYDROData / HYDROData_CalculationCase.cxx
index 1bd1057d6dd42ccc548a86640b9901e5ff4e2f9b..07397d1a64ee1dbee12cd9ff728e10e38106dc1c 100644 (file)
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
-#include "HYDROData_CalculationCase.h"
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
 
+#include "HYDROData_CalculationCase.h"
 #include "HYDROData_ArtificialObject.h"
 #include "HYDROData_IAltitudeObject.h"
 #include "HYDROData_Document.h"
 #include <BRepBuilderAPI_MakeVertex.hxx>
 #endif
 
+#ifdef WIN32
+  #pragma warning ( default: 4251 )
+#endif
+
 #define EXPORT_NAME "HYDRO_" + GetName()
 
+#include <SALOME_NamingService.hxx>
+#include <SALOME_LifeCycleCORBA.hxx>
+
+#define _DEVDEBUG_
+#include "HYDRO_trace.hxx"
+
 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
 
 HYDROData_CalculationCase::HYDROData_CalculationCase()
-: HYDROData_Entity()
+: HYDROData_Entity( Geom_2d_and_groups )
 {
 }
 
@@ -116,10 +129,14 @@ QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTre
 
   QString aCalculName = GetObjPyName();
 
-  AssignmentMode aMode = GetAssignmentMode();
+  AssignmentMode aMode = GetAssignmentMode();  
   QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
   aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
 
+  AssignmentMode aModeLC = GetAssignmentLandCoverMode();
+  QString aModeLCStr = aModeLC==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
+  aResList << QString( "%0.SetAssignmentLandCoverMode( %1 )" ).arg( aCalculName ).arg( aModeLCStr );
+
   HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
   HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
   for ( ; anIter.More(); anIter.Next() )
@@ -158,6 +175,8 @@ QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTre
 
   if( aMode==AUTOMATIC )
     DumpRulesToPython( aCalculName, aResList );
+  if( aModeLC==AUTOMATIC )
+    DumpLandCoverRulesToPython( aCalculName, aResList );
 
   aResList << QString( "" );
   aResList << "# Start the algorithm of the partition and assignment";
@@ -167,7 +186,12 @@ QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTre
   {
     // Now we restore the
     // - regions and zones order
-    DumpRegionsToPython( aResList, theTreatedObjects, GetRegions( false ) );
+    DumpRegionsToPython( aResList, theTreatedObjects, GetRegions( false ) );   
+  }
+
+  if( aModeLC==MANUAL )
+  {
+    // Now we restore the
     // - land cover regions and zones order
     DumpRegionsToPython( aResList, theTreatedObjects, GetRegions( true ) );
   }
@@ -308,14 +332,23 @@ void HYDROData_CalculationCase::Update()
   switch( GetAssignmentMode() )
   {
   case MANUAL:
-    CreateRegionsDef( aDocument, aZonesList, false );
+    CreateRegionsDef( aDocument, aZonesList, false );    
+    break;
+  case AUTOMATIC:
+    CreateRegionsAuto( aDocument, aZonesList, false );    
+    break;
+  }
+
+  switch( GetAssignmentLandCoverMode() )
+  {
+  case MANUAL:
     CreateRegionsDef( aDocument, aLandCoverZonesList, true );
     break;
   case AUTOMATIC:
-    CreateRegionsAuto( aDocument, aZonesList, false );
     CreateRegionsAuto( aDocument, aLandCoverZonesList, true );
     break;
   }
+
   CreateEdgeGroupsDef( aDocument, anEdgesList );
 }
 
@@ -343,6 +376,7 @@ void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Docume
                                                    const HYDROData_SplitToZonesTool::SplitDataList& theZones,
                                                    const bool theLandCover )
 {
+  DEBTRACE("HYDROData_CalculationCase::CreateRegionsAuto");
   QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
   QMap<QString, QString> aRegionNameToObjNameMap;
   QString aZonesPref = theLandCover ? CALCULATION_LANDCOVER_ZONES_PREF : CALCULATION_ZONES_PREF;
@@ -386,6 +420,7 @@ void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Docume
     if ( !theLandCover ) {
       Handle(HYDROData_Object) aMergeObject = Handle(HYDROData_Object)::DownCast( aMergeEntity );
       if ( !aMergeObject.IsNull() ) {
+       DEBTRACE("aMergeEntity " << aMergeEntity->GetName().toStdString());
         aMergeEntity = aMergeObject->GetAltitudeObject();
       }
     }
@@ -473,7 +508,7 @@ bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object
   AddReferenceObject( theObject, DataTag_GeometryObject );
   
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_2d );
 
   return true;
 }
@@ -491,7 +526,7 @@ void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Obj
   RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
 
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 void HYDROData_CalculationCase::RemoveGeometryObjects()
@@ -499,7 +534,7 @@ void HYDROData_CalculationCase::RemoveGeometryObjects()
   ClearReferenceObjects( DataTag_GeometryObject );
 
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
@@ -513,7 +548,7 @@ bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesG
   AddReferenceObject( theGroup, DataTag_GeometryGroup );
   
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_Groups );
 
   return true;
 }
@@ -531,7 +566,7 @@ void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_Shap
   RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
 
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_Groups );
 }
 
 void HYDROData_CalculationCase::RemoveGeometryGroups()
@@ -539,7 +574,7 @@ void HYDROData_CalculationCase::RemoveGeometryGroups()
   ClearReferenceObjects( DataTag_GeometryGroup );
 
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_Groups );
 }
 
 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
@@ -549,7 +584,8 @@ void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_Poly
   SetReferenceObject( thePolyline, DataTag_Polyline );
 
   // Indicate model of the need to update zones splitting
-  SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
+  if( !IsEqual( aPrevPolyline, thePolyline ) )
+    Changed( Geom_2d );
 }
 
 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
@@ -565,7 +601,7 @@ void HYDROData_CalculationCase::RemoveBoundaryPolyline()
   ClearReferenceObjects( DataTag_Polyline );
 
   // Indicate model of the need to update zones splitting
-  SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
+  Changed( Geom_2d );
 }
 
 void HYDROData_CalculationCase::SetStricklerTable( const Handle(HYDROData_StricklerTable)& theStricklerTable )
@@ -575,7 +611,8 @@ void HYDROData_CalculationCase::SetStricklerTable( const Handle(HYDROData_Strick
   SetReferenceObject( theStricklerTable, DataTag_StricklerTable );
 
   // Indicate model of the need to update land covers partition
-  SetToUpdate( !IsEqual( aPrevStricklerTable, theStricklerTable ) || IsMustBeUpdated() );
+  if( !IsEqual( aPrevStricklerTable, theStricklerTable ) )
+    Changed( Geom_No );
 }
 
 Handle(HYDROData_StricklerTable) HYDROData_CalculationCase::GetStricklerTable() const
@@ -591,7 +628,7 @@ void HYDROData_CalculationCase::RemoveStricklerTable()
   ClearReferenceObjects( DataTag_StricklerTable );
 
   // Indicate model of the need to update land covers partition
-  SetToUpdate( !aPrevStricklerTable.IsNull() || IsMustBeUpdated() );
+  Changed( Geom_No );
 }
 
 bool HYDROData_CalculationCase::AddLandCover( const Handle(HYDROData_LandCover)& theLandCover )
@@ -602,7 +639,7 @@ bool HYDROData_CalculationCase::AddLandCover( const Handle(HYDROData_LandCover)&
   AddReferenceObject( theLandCover, DataTag_LandCover );
   
   // Indicate model of the need to update land covers partition
-  SetToUpdate( true );
+  Changed( Geom_No );
 
   return true;
 }
@@ -620,7 +657,7 @@ void HYDROData_CalculationCase::RemoveLandCover( const Handle(HYDROData_LandCove
   RemoveReferenceObject( theLandCover->Label(), DataTag_LandCover );
 
   // Indicate model of the need to update land cover partition
-  SetToUpdate( true );
+  Changed( Geom_No );
 }
 
 void HYDROData_CalculationCase::RemoveLandCovers()
@@ -628,7 +665,7 @@ void HYDROData_CalculationCase::RemoveLandCovers()
   ClearReferenceObjects( DataTag_LandCover );
 
   // Indicate model of the need to update land cover partition
-  SetToUpdate( true );
+  Changed( Geom_No );
 }
 
 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone,
@@ -664,7 +701,7 @@ bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRe
   if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
   {
     Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF, theLandCover );
-    theRegion->CopyTo( aNewRegion );
+    theRegion->CopyTo( aNewRegion, true );
 
     aFatherCalc->RemoveRegion( theRegion, theLandCover );
 
@@ -690,7 +727,8 @@ void HYDROData_CalculationCase::UpdateRegionsOrder()
     return;
 
   HYDROData_SequenceOfObjects aRegions = GetRegions( false );
-  aRegions.Append( GetRegions( true ) );
+  HYDROData_SequenceOfObjects aRegionsLC = GetRegions( true );
+  aRegions.Append( aRegionsLC );
 
   HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
   for ( ; anIter.More(); anIter.Next() )
@@ -764,10 +802,20 @@ double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&
   Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint, false );
   if ( !aZone.IsNull() )
   {
+    //DEBTRACE("GetAltitudeForPoint Region " << theRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString());
     Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
     if ( IsEqual( aRefRegion, theRegion ) )
       aResAltitude = GetAltitudeForPoint( thePoint, aZone );
+    else
+      {
+        DEBTRACE("GetAltitudeForPoint Region " << aRefRegion->GetName().toStdString() << " Zone " << aZone->GetName().toStdString() << " ---------------------------");
+        aResAltitude = GetAltitudeForPoint( thePoint, aZone );
+      }
   }
+  else
+    {
+      DEBTRACE(" --- GetAltitudeForPoint No Zone ---");
+    }
 
   return aResAltitude;
 }
@@ -775,17 +823,24 @@ double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&
 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&                  thePoint,
                                                        const Handle(HYDROData_Zone)& theZone ) const
 {
+  //DEBTRACE("GetAltitudeForPoint Zone " << theZone->GetName().toStdString());
   double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
   if ( theZone.IsNull() )
+  {
+       DEBTRACE("Zone nulle");
     return aResAltitude;
+  }
 
   HYDROData_Zone::MergeType aZoneMergeType = theZone->GetMergeType();
+  //DEBTRACE("aZoneMergeType " << aZoneMergeType);
   if ( !theZone->IsMergingNeed() )
   {
     aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
+    //DEBTRACE("---");
   }
   else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
   {
+       DEBTRACE("GetAltitudeForPoint Zone " << theZone->GetName().toStdString() << " Merge_UNKNOWN");
     return aResAltitude;
   }
 
@@ -798,15 +853,20 @@ double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&
     {
       if ( aZoneInterpolator != NULL )
       {
+       DEBTRACE("aZoneInterpolator != NULL");
         aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
         aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
       }
       else
+      {
+       DEBTRACE("aZoneInterpolator == NULL");
         aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
+      }
     }
   }
   else
   {
+       //DEBTRACE("aZoneMergeType != HYDROData_Zone::Merge_Object");
     HYDROData_SequenceOfObjects aZoneObjects = theZone->GetObjects();
     HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
     for ( ; anIter.More(); anIter.Next() )
@@ -823,11 +883,15 @@ double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY&
       double aPointAltitude = 0.0;
       if ( aZoneInterpolator != NULL )
       {
+       DEBTRACE("aZoneInterpolator != NULL");
         aZoneInterpolator->SetAltitudeObject( anObjAltitude );
         aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
       }
       else
+      {
+       //DEBTRACE("aZoneInterpolator == NULL");
         aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
+      }
 
       if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
         continue;
@@ -863,6 +927,7 @@ NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
   const NCollection_Sequence<gp_XY>& thePoints,
   const Handle(HYDROData_Region)&    theRegion ) const
 {
+  //DEBTRACE("HYDROData_CalculationCase::GetAltitudesForPoints " << theRegion->GetName().toStdString());
   NCollection_Sequence<double> aResSeq;
 
   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
@@ -893,6 +958,38 @@ NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
   return aResSeq;
 }
 
+double HYDROData_CalculationCase::GetStricklerCoefficientForPoint( const gp_XY& thePoint ) const
+{
+    double aCoeff = 0;
+    Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
+    if ( !aDocument.IsNull() )
+        aCoeff = aDocument->GetDefaultStricklerCoefficient();
+
+    Handle(HYDROData_LandCover) aLandCover;
+    Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint, Standard_True );
+    if ( !aZone.IsNull() )
+    {
+        HYDROData_SequenceOfObjects anObjList = aZone->GetObjects();
+        if ( anObjList.Length() == 1 )
+            aLandCover = Handle(HYDROData_LandCover)::DownCast( anObjList.First() );
+        else
+            aLandCover = Handle(HYDROData_LandCover)::DownCast( aZone->GetMergeObject() );
+    }
+
+    if ( !aLandCover.IsNull() )
+    {
+        QString aType = aLandCover->GetStricklerType();
+        Handle(HYDROData_StricklerTable) aTable = GetStricklerTable();
+        if ( !aTable.IsNull() )
+        {
+            if ( aTable->GetTypes().contains( aType ) )
+                aCoeff = aTable->Get( aType, aCoeff );
+        }
+    }
+
+    return aCoeff;
+}
+
 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint,
                                                                         const bool theLandCover ) const
 {
@@ -1006,14 +1103,15 @@ QString HYDROData_CalculationCase::Export( int theStudyId ) const
   GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
   
-  QString aGeomObjEntry;
-  bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
+  QString aGeomObjEntry, anErrorMsg;
+  bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry, anErrorMsg );
   return isOK ? aGeomObjEntry : QString();
 }
 
 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
                                         SALOMEDS::Study_ptr theStudy,
-                                        QString& theGeomObjEntry ) const
+                                        QString& theGeomObjEntry,
+                                        QString& theErrorMsg ) const
 {
   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
 
@@ -1038,6 +1136,7 @@ bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
   }
   
   // Get faces
+  bool isAllNotSubmersible = true;
   TopTools_ListOfShape aFaces;
   HYDROData_SequenceOfObjects aCaseRegions = GetRegions( false );
   HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
@@ -1047,12 +1146,25 @@ bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var  theGeomEngine,
       Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
     if( aRegion.IsNull() || !aRegion->IsSubmersible() )
       continue;
+    
+    if ( isAllNotSubmersible )
+      isAllNotSubmersible = false;
 
     TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
     aFaces.Append( aRegionShape );
   }
 
-  return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
+  bool aRes = false;
+
+  if ( aCaseRegions.IsEmpty() ) {
+    theErrorMsg = QString("the list of regions is empty.");
+  } else if ( isAllNotSubmersible ) {
+    theErrorMsg = QString("there are no submersible regions.");
+  } else {
+    aRes = Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );;
+  }
+
+  return aRes;
 }
 
 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var                            theGeomEngine,
@@ -1218,7 +1330,7 @@ void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag t
 
   // Indicate model of the need to update splitting
   if ( theIsSetToUpdate ) {
-    SetToUpdate( true );
+    Changed( Geom_2d );
   }
 }
 
@@ -1232,7 +1344,7 @@ void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Entity)&    theO
   HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
 
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 QString HYDROData_CalculationCase::DumpRules() const
@@ -1241,13 +1353,19 @@ QString HYDROData_CalculationCase::DumpRules() const
   return HYDROData_PriorityQueue::DumpRules( aRulesLab );
 }
 
+QString HYDROData_CalculationCase::DumpLandCoverRules() const
+{
+  TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomLandCoverRules );
+  return HYDROData_PriorityQueue::DumpRules( aRulesLab );
+}
+
 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
 {
   TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
 
   // Indicate model of the need to update splitting
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
@@ -1267,6 +1385,13 @@ void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseNam
   HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
 }
 
+void HYDROData_CalculationCase::DumpLandCoverRulesToPython( const QString& theCalcCaseName,
+                                                            QStringList& theScript ) const
+{
+  TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomLandCoverRules );
+  HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
+}
+
 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
 {
   return myLastWarning;
@@ -1343,7 +1468,7 @@ void HYDROData_CalculationCase::SetAssignmentLandCoverMode( AssignmentMode theMo
   TDataStd_Integer::Set( aModeLab, ( int ) theMode );
 
   // Indicate model of the need to update land covers partition
-  SetToUpdate( true );
+  Changed( Geom_No );
 }
 
 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentLandCoverMode() const