Salome HOME
debug of profileOp
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.cxx
index b85cfb80a3f283f0a9029555f5cfe3dbcad33d8a..89f44d9997b97399b97559e2d379a1deb63634f7 100755 (executable)
@@ -1,12 +1,8 @@
-// Copyright (C) 2007-2013  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
-// version 2.1 of the License.
+// 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
 #include "HYDROGUI_StreamOp.h"
 
 #include "HYDROGUI_Module.h"
+#include <HYDROGUI_DataObject.h>
 #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>
@@ -38,6 +37,9 @@
 
 #include <SUIT_MessageBox.h>
 #include <SUIT_Desktop.h>
+#include <QColor>
+#include <QApplication>
+
 
 #include <OCCViewer_ViewManager.h>
 #include <OCCViewer_ViewModel.h>
 #include <TopoDS_Face.hxx>
 #include <TopoDS.hxx>
 #include <BRepBuilderAPI_MakeFace.hxx>
+
+void insertProfileInToOrder( const QString& theProfileName,
+                             const double&  theProfilePar,
+                             QStringList&   theProfiles,
+                             QList<double>& theProfileParams )
+{
+  bool anIsInserted = false;
+  for ( int k = 0; k < theProfileParams.length(); ++k )
+  {
+    const double& aParam = theProfileParams.value( k );
+    if ( theProfilePar < aParam )
+    {
+      theProfiles.insert( k, theProfileName );
+      theProfileParams.insert( k, theProfilePar );
+      anIsInserted = true;
+      break;
+    }
+  }
+  
+  if ( !anIsInserted )
+  {
+    theProfiles << theProfileName;
+    theProfileParams << theProfilePar;
+  }
+}
+
 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
 : HYDROGUI_Operation( theModule ), 
   myIsEdit( theIsEdit ),
@@ -69,36 +97,71 @@ void HYDROGUI_StreamOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
 
-  // We start operation in the document
-  startDocOperation();
+  if ( !myIsEdit || isApplyAndClose() )
+    myEditedObject.Nullify();
+  myHydAxis.clear();
+  myProfiles.clear();
+  myProfileParams.clear();
 
   // Get panel and reset its state
   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
   aPanel->reset();
 
-  // Get/create the edited object
-  if( myIsEdit ) {
-    myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
-    if ( !myEditedObject.IsNull() && myEditedObject->IsMustBeUpdated() ) {
-      myEditedObject->Update();
-    }
-  } else {
-    myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
-  }
+  QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
+  if ( myIsEdit )
+  {
+    if ( isApplyAndClose() )
+      myEditedObject =
+        Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
+    if ( !myEditedObject.IsNull() )
+    {
+      anObjectName = myEditedObject->GetName();
+
+      // Hydraulic axis
+      Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
+      
+      TopoDS_Face aPlane;
+      HYDROData_Stream::BuildRefFace( aPlane ) ;
+      if ( !aHydraulicAxis.IsNull() )
+        myHydAxis = aHydraulicAxis->GetName();
+
+      // 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())
+        {
+          Standard_Real aProfilePar = 0.0;
+          HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
+          myProfileParams << aProfilePar;
+        }
+
+      }      
 
-  // Get the edited object name
-  QString anObjectName; 
-  if ( !myIsEdit ) {
-    anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
-  } else if ( !myEditedObject.IsNull() ) {
-    anObjectName = myEditedObject->GetName();
+    }
   }
 
   // Update the panel
   // set the edited object name
   aPanel->setObjectName( anObjectName );
+
   // 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();
 
@@ -109,15 +172,12 @@ void HYDROGUI_StreamOp::startOperation()
 void HYDROGUI_StreamOp::abortOperation()
 {
   erasePreview();
-  abortDocOperation();
-
   HYDROGUI_Operation::abortOperation();
 }
 
 void HYDROGUI_StreamOp::commitOperation()
 {
   erasePreview();
-
   HYDROGUI_Operation::commitOperation();
 }
 
@@ -131,55 +191,140 @@ 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 )
+                                      QString& theErrorMsg,
+                                      QStringList& theBrowseObjectsEntries )
 {
   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
-  if ( !aPanel || myEditedObject.IsNull() ) {
+  if ( !aPanel )
     return false;
-  }
 
   // Check whether the object name is not empty
   QString anObjectName = aPanel->getObjectName().simplified();
-  if ( anObjectName.isEmpty() ) {
+  if ( anObjectName.isEmpty() )
+  {
     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
     return false;
   }
 
   // Check that there are no other objects with the same name in the document
-  if( myEditedObject->GetName() != anObjectName )
+  if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
   {
     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
-    if( !anObject.IsNull() ) {
+    if( !anObject.IsNull() )
+    {
       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
       return false;
     }
   }
 
-  // Check if the axis is set
-  Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
-  if ( aHydraulicAxis.IsNull() ) {
-    theErrorMsg = tr( "AXIS_NOT_DEFINED" );
-    return false;
-  }
+  if ( myEditedObject.IsNull() ) // Create new data model object
+    myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
+
+  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;
+ //}
 
   // Check if at least 2 profiles is set
-  HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
-  if ( aProfiles.Length() < 2 ) {
+  HYDROData_SequenceOfObjects aRefProfiles;
+  for ( int i = 0; i < myProfiles.length(); ++i )
+  {
+    QString aProfileName = myProfiles.value( i );
+
+    Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
+      HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
+    if ( !aProfile.IsNull() )
+      aRefProfiles.Append( aProfile );
+  }
+
+  if ( aRefProfiles.Length() < 2 )
+  {
     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
     return false;
   }
 
-  // Set the object name
+  bool ToOrder = true;
+
+  if (aHydAxis.IsNull())
+    ToOrder = false;
+
+  myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
   myEditedObject->SetName( anObjectName );
 
+  if (!aHydAxis.IsNull())
+    myEditedObject->SetHydraulicAxis( aHydAxis );
+  myEditedObject->SetProfiles( aRefProfiles, false );
+  myEditedObject->SetDDZ( aPanel->getDDZ() );
+  myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
+
+  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
@@ -188,6 +333,8 @@ bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
   // Show the object in case of creation mode of the operation
   if( !myIsEdit ) {
     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
+    QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
+    theBrowseObjectsEntries.append( anEntry );
   }
 
   module()->setIsToUpdate( myEditedObject );
@@ -200,33 +347,105 @@ bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
 
 void HYDROGUI_StreamOp::createPreview()
 {
-  if ( myEditedObject.IsNull() ) {
-    return;
-  }
-
   LightApp_Application* anApp = module()->getApp();
   if ( !getPreviewManager() )
+  {
     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+  }
 
   OCCViewer_ViewManager* aViewManager = getPreviewManager();
-  if ( aViewManager && !myPreviewPrs ) {
-    if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
+  if ( aViewManager && !myPreviewPrs )
+  {
+    if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
+    {
       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
-      if ( !aCtx.IsNull() ) {
-        myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject, getPreviewZLayer() );
+      if ( !aCtx.IsNull() )
+      {
+        myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
+
+        QColor aFillingColor = Qt::green;
+        QColor aBorderColor = Qt::transparent;
+        if ( !myEditedObject.IsNull() )
+        {
+          aFillingColor = myEditedObject->GetFillingColor();
+          aBorderColor = myEditedObject->GetBorderColor();
+        }
+
+        myPreviewPrs->setFillingColor( aFillingColor, false, false );
+        myPreviewPrs->setBorderColor( aBorderColor, false, false );
       }
     }
   }
 
-  if ( myPreviewPrs ) {
-    myPreviewPrs->update( true );
+  if ( !aViewManager || !myPreviewPrs )
+    return;
+
+  Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
+    HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
+
+  HYDROData_SequenceOfObjects aRefProfiles;
+  //std::vector<Handle(HYDROData_Profile)> aRefProfiles;
+  int plen = myProfiles.length();
+  for ( int i = 0; i < plen; ++i )
+  {
+    QString aProfileName = myProfiles.value( i );
+
+    Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
+      HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
+    if ( !aProfile.IsNull() )
+      aRefProfiles.Append( aProfile );
   }
+
+  HYDROData_Stream::PrsDefinition aPrsDef;
+
+  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 );
 }
 
 void HYDROGUI_StreamOp::erasePreview()
 {
-  if( myPreviewPrs ) {
+  if( myPreviewPrs )
+  {
     delete myPreviewPrs;
     myPreviewPrs = 0;
   }
@@ -234,67 +453,78 @@ void HYDROGUI_StreamOp::erasePreview()
 
 void HYDROGUI_StreamOp::onAddProfiles()
 {
-  if ( myEditedObject.IsNull() ) {
-    return;
-  }
+  Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
+    HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
+  //if ( aHydAxis.IsNull() )
+   // return;
+
+  TopoDS_Face aPlane;
+  HYDROData_Stream::BuildRefFace( aPlane );
 
   // Get the current profiles list
-  HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
-
-  // TODO: to be optimized
-  QStringList aCurrentProfiles;
-  for( int i = 1, n = aProfiles.Length(); i <= n; i++ ) {
-    Handle(HYDROData_Profile) aProfile =
-      Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
-    if ( !aProfile.IsNull() ) {
-      aCurrentProfiles << aProfile->GetName();
-    }
-  }
+  QStringList aCurrentProfiles = myProfiles;
     
   // Get the selected profiles ( in the Object Browser )
-  QStringList anInvalidProfiles, anExistingProfiles, aHasNoIntersectionProfiles;
+  QStringList anInvalidProfiles;
+  QStringList anExistingProfiles;
+  QStringList aHasNoIntersectionProfiles;
+  QStringList aVerifiedProfiles;
 
-  HYDROData_SequenceOfObjects aVerifiedProfiles;
   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
-  Handle(HYDROData_PolylineXY) aHydAxis = myEditedObject->GetHydraulicAxis();
-  if ( aHydAxis.IsNull() )
-    return; 
-  TopoDS_Face aPlane;
-  if(!myEditedObject->BuildFace(aHydAxis, aPlane))
-    return;
-  Standard_Real aPar(.0);
-  for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ ) {
+
+  for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
+  {
     Handle(HYDROData_Profile) aProfile = 
       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
-    if ( !aProfile.IsNull() ) {
-      QString aProfileName = aProfile->GetName();
-
-      // Check the profile, if all is ok - add it to the list
-      if ( !aProfile->IsValid() ) { // check whether the profile is valid
-        anInvalidProfiles << aProfileName;
-      } else if ( aCurrentProfiles.contains( aProfileName ) ) { // check whether the profile is already added
-        anExistingProfiles << aProfileName;
-      } else if ( !myEditedObject->HasIntersection( aProfile, aPlane, aPar ) ) {  // check whether the profile has intersection
-        aHasNoIntersectionProfiles << aProfileName;
-      } else {
-        aVerifiedProfiles.Append( aProfile );
-      }
+    if ( aProfile.IsNull() )
+      continue;
+
+    QString aProfileName = aProfile->GetName();
+    Standard_Real aProfilePar = 0.0;
+
+    // Check the profile, if all is ok - add it to the list
+    if ( !aProfile->IsValid() )
+    {
+      // check whether the profile is valid
+      anInvalidProfiles << aProfileName;
+    }
+    else if ( aCurrentProfiles.contains( aProfileName ) )
+    {
+      // check whether the profile is already added
+      anExistingProfiles << aProfileName;
+    }
+    else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
+    {
+      // check whether the profile has intersection
+      aHasNoIntersectionProfiles << aProfileName;
+    }
+    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;
     }
   }
  
   // Show message box with the ignored profiles
-  if ( !anInvalidProfiles.isEmpty() || !anExistingProfiles.isEmpty() ||
-       !aHasNoIntersectionProfiles.isEmpty() ) {
+  if ( !anInvalidProfiles.isEmpty() ||
+       !anExistingProfiles.isEmpty() ||
+       !aHasNoIntersectionProfiles.isEmpty() )
+  {
     QString aMessage = tr( "IGNORED_PROFILES" );
-    if ( !anInvalidProfiles.isEmpty() ) {
+    if ( !anInvalidProfiles.isEmpty() )
+    {
       aMessage.append( "\n\n" );
       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
     }
-    if ( !anExistingProfiles.isEmpty() ) {
+    if ( !anExistingProfiles.isEmpty() )
+    {
       aMessage.append( "\n\n" );
       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
     }
-    if ( !aHasNoIntersectionProfiles.isEmpty() ) {
+    if ( !aHasNoIntersectionProfiles.isEmpty() )
+    {
       aMessage.append( "\n\n" );
       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
     }
@@ -302,13 +532,8 @@ void HYDROGUI_StreamOp::onAddProfiles()
     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
   }
 
-  // Update the stream object
-  for( int i = 1, n = aVerifiedProfiles.Length(); i <= n; i++ ) {
-    Handle(HYDROData_Profile) aProfile =
-      Handle(HYDROData_Profile)::DownCast( aVerifiedProfiles.Value( i ) );
-    myEditedObject->AddProfile( aProfile );
-  }
-  myEditedObject->UpdatePrs();
+  if ( aVerifiedProfiles.isEmpty() )
+    return;
 
   // Update the panel
   updatePanelData();
@@ -319,37 +544,30 @@ void HYDROGUI_StreamOp::onAddProfiles()
 
 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
 {
-  if ( myEditedObject.IsNull() ) {
+  QStringList aToRemove = theProfilesToRemove;
+  
+  aToRemove.removeDuplicates();
+  if ( aToRemove.isEmpty() )
     return;
-  }
 
   bool isRemoved = false;
 
-  // 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() && !theProfilesToRemove.contains(aProfile->GetName()) ) {
-      if ( myEditedObject->RemoveProfile( aProfile ) ) {
-        isRemoved = true;
-      }
-    }
-  }
-
   // Remove profiles
-  foreach( const QString& aProfileName, theProfilesToRemove ) {
-    Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
-      HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
-    if ( myEditedObject->RemoveProfile( aProfile ) ) {
-      isRemoved = true;
-    }
-  }
+  for ( int i = 0; i < aToRemove.length(); ++i )
+  {
+    const QString& aProfileName = aToRemove.value( i );
 
-  if ( isRemoved ) {
-    // Update the edited stream object
-    myEditedObject->UpdatePrs();
+    int aProfileId = myProfiles.indexOf( aProfileName );
+    if ( aProfileId < 0 )
+      continue;
+
+    myProfiles.removeAt( aProfileId );
+    myProfileParams.removeAt( aProfileId );
+    isRemoved = true;
+  }
 
+  if ( isRemoved )
+  {
     // Update the panel
     updatePanelData();
 
@@ -358,45 +576,64 @@ void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove
   }
 }
 
-void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
+void HYDROGUI_StreamOp::onDDZValueChanged( double d )
 {
-  if ( myEditedObject.IsNull() ) {
-    return;
-  }
+   createPreview();
+}
 
+void HYDROGUI_StreamOp::onSSValueChanged( double d )
+{
+   createPreview();
+}
+
+void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
+{
   // Get axis object   
-  Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
+  Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
 
   // Prepare data for intersection check
   TopoDS_Face aPlane;
-  if ( !myEditedObject->BuildFace(anAxis, 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;
-  }
-
-  Standard_Real aPar(.0);
+  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;
+  QStringList   aHasNoIntersectionProfiles;
 
   // Get list of profiles which do not intersect the axis
-  QStringList aHasNoIntersectionProfiles;
-  HYDROData_SequenceOfObjects aCurrentProfiles = myEditedObject->GetProfiles();
-  for( int anIndex = 1, aLength = aCurrentProfiles.Length(); anIndex <= aLength; anIndex++ ) {
-    Handle(HYDROData_Profile) aProfile =
-      Handle(HYDROData_Profile)::DownCast( aCurrentProfiles.Value( anIndex ) );
-    if ( !aProfile.IsNull() ) {
-      if ( !HYDROData_Stream::HasIntersection( anAxis, aProfile, aPlane, aPar ) ) {
-        aHasNoIntersectionProfiles << aProfile->GetName();
-      }
+  for ( int i = 0; i < myProfiles.length(); ++i )
+  {
+    QString aProfileName = myProfiles.value( i );
+
+    Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
+      HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
+    if ( aProfile.IsNull() )
+      continue;
+      
+    Standard_Real aProfilePar = 0.0;
+    if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
+    {
+      // Insert profile in correct place
+      insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
+    }
+    else
+    {
+      aHasNoIntersectionProfiles << aProfile->GetName();
     }
   }
 
   // If there are profiles which don't intersect the new axis - show confirmation message box
   bool isConfirmed = true;
-  if ( !aHasNoIntersectionProfiles.isEmpty() ) {
+  if ( !aHasNoIntersectionProfiles.isEmpty() )
+  {
     SUIT_MessageBox::StandardButtons aButtons = 
       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
 
@@ -409,13 +646,17 @@ void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
   }
 
   // Check if the user has confirmed axis change
-  if ( !isConfirmed ) {
+  if ( !isConfirmed )
+  {
     // To restore the old axis
     updatePanelData();
-  } else {
-    // Set axis
-    myEditedObject->SetHydraulicAxis( anAxis );
-    myEditedObject->UpdatePrs();
+  }
+  else
+  {
+    // Update data
+    myHydAxis = theNewAxis;
+    myProfiles = aNewProfiles;
+    myProfileParams = aNewProfileParams;
 
     // Update the panel
     updatePanelData();
@@ -428,30 +669,9 @@ void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
 void HYDROGUI_StreamOp::updatePanelData()
 {
   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
-  if ( !aPanel || myEditedObject.IsNull() ) {
+  if ( !aPanel )
     return;
-  }
-
-  // Hydraulic axis
-  Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
-  if ( !aHydraulicAxis.IsNull() ) {
-    aPanel->setAxisName( aHydraulicAxis->GetName() );
-  } else {
-    aPanel->setAxisName( "" );
-  }
-
-  // Stream profiles
-  QStringList aProfiles;
-
-  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() ) {
-      QString aProfileName = aProfile->GetName();
-      aProfiles << aProfileName;      
-    }
-  }
 
-  aPanel->setProfiles( aProfiles );
+  aPanel->setAxisName( myHydAxis );
+  aPanel->setProfiles( myProfiles );
 }