Salome HOME
Merge branch 'BR_v14_rc' into BR_quadtree
[modules/hydro.git] / src / HYDROData / HYDROData_CalculationCase.cxx
index f8640f813db6e919e8599a72bc4114aa716b3894..8814d9aaa28b5c31c4411c0303dfa03aa8c9a5b9 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)
 
@@ -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 );
 }
 
@@ -690,7 +723,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 +798,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,6 +819,7 @@ 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() )
     return aResAltitude;
@@ -863,6 +908,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 )
@@ -1038,14 +1084,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;
 
@@ -1070,6 +1117,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 );
@@ -1079,12 +1127,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,
@@ -1145,7 +1206,7 @@ bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var
   // Publish the sewed shape
   QString aName = EXPORT_NAME;
   GEOM::GEOM_Object_ptr aMainShape = 
-    publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
+    HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
 
   if ( aMainShape->_is_nil() )  
     return false;
@@ -1242,52 +1303,6 @@ bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var
   return true;
 }
 
-GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM( 
-  GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
-  const TopoDS_Shape& theShape, const QString& theName,
-  QString& theGeomObjEntry ) const
-{
-  theGeomObjEntry = "";
-  GEOM::GEOM_Object_var aGeomObj;
-
-  if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
-       theShape.IsNull() ) {
-    return aGeomObj._retn();
-  }
-
-  std::ostringstream aStreamShape;
-  // Write TopoDS_Shape in ASCII format to the stream
-  BRepTools::Write( theShape, aStreamShape );
-  // Returns the number of bytes that have been stored in the stream's buffer.
-  int aSize = aStreamShape.str().size();
-  // Allocate octect buffer of required size
-  CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
-  // Copy ostrstream content to the octect buffer
-  memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
-  // Create TMPFile
-  SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
-
-  // Restore shape from the stream and get the GEOM object
-  GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
-  aGeomObj = anInsOp->RestoreShape( aSeqFile );
-  
-  // Puplish the GEOM object
-  if ( !aGeomObj->_is_nil() ) {
-    QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
-
-    SALOMEDS::SObject_var aResultSO = 
-      theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
-                                     aGeomObj, qPrintable( aName ) );
-    if ( aResultSO->_is_nil() ) {
-      aGeomObj = GEOM::GEOM_Object::_nil();
-    }
-    else
-      theGeomObjEntry = aResultSO->GetID();
-  }
-
-  return aGeomObj._retn();
-}
-
 void HYDROData_CalculationCase::ClearRules( HYDROData_CalculationCase::DataTag theDataTag,
                                             const bool theIsSetToUpdate )
 {
@@ -1319,6 +1334,12 @@ 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 );
@@ -1345,6 +1366,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;