Salome HOME
refs #1832
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.cxx
index 2d453d5b37cb4c68ac8c64a43bb19bdefcebb422..89f44d9997b97399b97559e2d379a1deb63634f7 100755 (executable)
@@ -1,8 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// 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
 #include "HYDROGUI_Shape.h"
 #include "HYDROGUI_StreamDlg.h"
 #include "HYDROGUI_Tool.h"
+#include "HYDROGUI_Tool2.h"
 #include "HYDROGUI_UpdateFlags.h"
 
 #include <HYDROData_Document.h>
 #include <HYDROData_PolylineXY.h>
 #include <HYDROData_Profile.h>
+#include <HYDROData_DTM.h>
 
 #include <LightApp_Application.h>
 #include <LightApp_SelectionMgr.h>
@@ -39,6 +37,9 @@
 
 #include <SUIT_MessageBox.h>
 #include <SUIT_Desktop.h>
+#include <QColor>
+#include <QApplication>
+
 
 #include <OCCViewer_ViewManager.h>
 #include <OCCViewer_ViewModel.h>
@@ -118,32 +119,33 @@ void HYDROGUI_StreamOp::startOperation()
 
       // Hydraulic axis
       Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
+      
+      TopoDS_Face aPlane;
+      HYDROData_Stream::BuildRefFace( aPlane ) ;
       if ( !aHydraulicAxis.IsNull() )
-      {
         myHydAxis = aHydraulicAxis->GetName();
 
-        TopoDS_Face aPlane;
-        if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
+      // Stream profiles
+      HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
+      for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
+      {
+        Handle(HYDROData_Profile) aProfile = 
+          Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
+        if ( aProfile.IsNull() )
+          continue;
+
+        QString aProfileName = aProfile->GetName();        
+        myProfiles      << aProfileName;
+
+        if (!aHydraulicAxis.IsNull())
         {
-          // Stream profiles
-          HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
-          for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
-          {
-            Handle(HYDROData_Profile) aProfile = 
-              Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
-            if ( aProfile.IsNull() )
-              continue;
-
-            QString aProfileName = aProfile->GetName();
-
-            Standard_Real aProfilePar = 0.0;
-            HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
-
-            myProfiles      << aProfileName;
-            myProfileParams << aProfilePar;
-          }
+          Standard_Real aProfilePar = 0.0;
+          HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
+          myProfileParams << aProfilePar;
         }
-      }
+
+      }      
+
     }
   }
 
@@ -154,6 +156,12 @@ void HYDROGUI_StreamOp::startOperation()
   // set the existing 2D polylines names to the panel
   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
 
+  if (myIsEdit)
+  {
+    aPanel->setDDZ( myEditedObject->GetDDZ() );
+    aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
+  }
+
   // synchronize the panel state with the edited object state
   updatePanelData();
 
@@ -183,9 +191,63 @@ HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
            this, SLOT( onAxisChanged( const QString& ) ) );
 
+  connect( aPanel, SIGNAL( DDZValueChanged( double ) ),  this, SLOT( onDDZValueChanged( double ) ) );
+  connect( aPanel, SIGNAL( SSValueChanged( double ) ),  this, SLOT( onSSValueChanged( double ) ) );
   return aPanel;
 }
 
+void HYDROGUI_StreamOp::apply()
+{
+  QApplication::setOverrideCursor( Qt::WaitCursor );
+
+  startDocOperation();
+
+  int anUpdateFlags = 0;
+  QString anErrorMsg;
+
+  bool aResult = false;
+  QStringList aBrowseObjectsEntries;
+
+  try
+  {
+    aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
+  }
+  catch ( Standard_Failure )
+  {
+    Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
+    anErrorMsg = aFailure->GetMessageString();
+    aResult = false;
+  }
+  catch ( ... )
+  {
+    aResult = false;
+  }
+  
+  QApplication::restoreOverrideCursor();
+
+  if ( aResult )
+  {
+    module()->update( anUpdateFlags );
+    commitDocOperation();
+    commit();
+    browseObjects( aBrowseObjectsEntries );
+  }
+  else
+  {
+    myEditedObject->Remove();
+    module()->setObjectRemoved( myEditedObject );  
+
+    if ( isToAbortOnApply() )
+      abortDocOperation();
+
+    abort();
+    SUIT_MessageBox::critical( module()->getApp()->desktop(),
+                               tr( "CREATE_STREAM_ERROR" ),
+                               anErrorMsg ); 
+
+  } 
+}
+
 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
                                       QString& theErrorMsg,
                                       QStringList& theBrowseObjectsEntries )
@@ -216,14 +278,13 @@ bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
   if ( myEditedObject.IsNull() ) // Create new data model object
     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
 
-  // Check if the axis is set
   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
 if ( aHydAxis.IsNull() )
 {
   theErrorMsg = tr( "AXIS_NOT_DEFINED" );
   return false;
 }
//if ( aHydAxis.IsNull() )
//{
//  theErrorMsg = tr( "AXIS_NOT_DEFINED" );
//  return false;
//}
 
   // Check if at least 2 profiles is set
   HYDROData_SequenceOfObjects aRefProfiles;
@@ -243,19 +304,27 @@ bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
     return false;
   }
 
-  // Set the object name
+  bool ToOrder = true;
+
+  if (aHydAxis.IsNull())
+    ToOrder = false;
+
+  myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
   myEditedObject->SetName( anObjectName );
 
-  myEditedObject->SetHydraulicAxis( aHydAxis );
+  if (!aHydAxis.IsNull())
+    myEditedObject->SetHydraulicAxis( aHydAxis );
   myEditedObject->SetProfiles( aRefProfiles, false );
+  myEditedObject->SetDDZ( aPanel->getDDZ() );
+  myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
 
-  if ( myEditedObject->IsMustBeUpdated() )
+  if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
     myEditedObject->Update();
 
   if ( !myIsEdit )
   {
-    myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
-    myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
+    myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
+    myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
   }
 
   // Erase preview
@@ -295,8 +364,8 @@ void HYDROGUI_StreamOp::createPreview()
       {
         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
 
-        QColor aFillingColor = HYDROData_Stream::DefaultFillingColor();
-        QColor aBorderColor = HYDROData_Stream::DefaultBorderColor();
+        QColor aFillingColor = Qt::green;
+        QColor aBorderColor = Qt::transparent;
         if ( !myEditedObject.IsNull() )
         {
           aFillingColor = myEditedObject->GetFillingColor();
@@ -316,7 +385,9 @@ void HYDROGUI_StreamOp::createPreview()
     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
 
   HYDROData_SequenceOfObjects aRefProfiles;
-  for ( int i = 0; i < myProfiles.length(); ++i )
+  //std::vector<Handle(HYDROData_Profile)> aRefProfiles;
+  int plen = myProfiles.length();
+  for ( int i = 0; i < plen; ++i )
   {
     QString aProfileName = myProfiles.value( i );
 
@@ -327,11 +398,46 @@ void HYDROGUI_StreamOp::createPreview()
   }
 
   HYDROData_Stream::PrsDefinition aPrsDef;
-  if ( !HYDROData_Stream::CreatePresentations( aHydAxis, aRefProfiles, aPrsDef ) )
-  {
-    erasePreview();
-    return;
-  }
+
+  TopoDS_Shape Out3dPres;
+  TopoDS_Shape Out2dPres;
+  TopoDS_Shape OutLeftB;
+  TopoDS_Shape OutRightB;
+  TopoDS_Shape OutInlet;
+  TopoDS_Shape OutOutlet;
+
+  HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
+  double ddz = aPanel->getDDZ();
+  double ss = aPanel->getSpatialStep();
+
+  std::set<int> InvInd;
+
+#ifdef _DEBUG
+  const int MAX_POINTS_IN_PREVIEW = 50000;
+#else
+  const int MAX_POINTS_IN_PREVIEW = 500000;
+#endif
+
+  HYDROData_Bathymetry::AltitudePoints points;
+
+  bool ProjStat = true;
+  HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
+    OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW, ProjStat );
+
+  aPanel->clearAllBackgroundColorsForProfileList();
+  for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
+    aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
+
+  aPrsDef.myInlet = OutInlet;
+  aPrsDef.myOutlet = OutOutlet;
+  aPrsDef.myLeftBank = OutLeftB;
+  aPrsDef.myRightBank = OutRightB;
+  if (ProjStat)
+    aPrsDef.myPrs2D = Out2dPres;
+  aPrsDef.myPrs3D = Out3dPres;
+
+  if (!ProjStat)
+    aPanel->addWarning(tr("STREAM_PROJECTION_FAILED"));
 
   myPreviewPrs->setShape( aPrsDef.myPrs2D );
 }
@@ -349,12 +455,11 @@ void HYDROGUI_StreamOp::onAddProfiles()
 {
   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
-  if ( aHydAxis.IsNull() )
-    return;
+  //if ( aHydAxis.IsNull() )
+   // return;
 
   TopoDS_Face aPlane;
-  if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
-    return;
+  HYDROData_Stream::BuildRefFace( aPlane );
 
   // Get the current profiles list
   QStringList aCurrentProfiles = myProfiles;
@@ -396,6 +501,7 @@ void HYDROGUI_StreamOp::onAddProfiles()
     else
     {
       // Insert profile in correct place
+      // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
       aVerifiedProfiles << aProfileName;
     }
@@ -439,20 +545,7 @@ void HYDROGUI_StreamOp::onAddProfiles()
 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
 {
   QStringList aToRemove = theProfilesToRemove;
-
-  // Take the Object Browser selection into account
-  HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() ); 
-  for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ )
-  {
-    Handle(HYDROData_Profile) aProfile = 
-      Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
-    if ( aProfile.IsNull() )
-      continue;
-
-    QString aProfileName = aProfile->GetName();
-    aToRemove.append( aProfileName );
-  }
-
+  
   aToRemove.removeDuplicates();
   if ( aToRemove.isEmpty() )
     return;
@@ -483,6 +576,16 @@ void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove
   }
 }
 
+void HYDROGUI_StreamOp::onDDZValueChanged( double d )
+{
+   createPreview();
+}
+
+void HYDROGUI_StreamOp::onSSValueChanged( double d )
+{
+   createPreview();
+}
+
 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
 {
   // Get axis object   
@@ -491,15 +594,15 @@ void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
 
   // Prepare data for intersection check
   TopoDS_Face aPlane;
-  if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
-  {
-    SUIT_MessageBox::critical( module()->getApp()->desktop(), 
-                              tr( "BAD_SELECTED_POLYLINE_TLT" ),
-                              tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
-    // To restore the old axis
-    updatePanelData();
-    return;
-  }
+  HYDROData_Stream::BuildRefFace( aPlane );
+  //{
+  //  SUIT_MessageBox::critical( module()->getApp()->desktop(), 
+  //                            tr( "BAD_SELECTED_POLYLINE_TLT" ),
+  //                            tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
+  //  // To restore the old axis
+  //  updatePanelData();
+  //  return;
+  //}
 
   QStringList   aNewProfiles;
   QList<double> aNewProfileParams;