Salome HOME
Update for occt 7.5.0
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_StreamOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include <HYDROGUI_DataObject.h>
23 #include "HYDROGUI_Shape.h"
24 #include "HYDROGUI_StreamDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_Tool2.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_OCCDisplayer.h"
29
30 #include <HYDROData_Document.h>
31 #include <HYDROData_PolylineXY.h>
32 #include <HYDROData_Profile.h>
33 #include <HYDROData_DTM.h>
34
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
38
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
41 #include <QColor>
42 #include <QApplication>
43
44
45 #include <OCCViewer_ViewManager.h>
46 #include <OCCViewer_ViewModel.h>
47 #include <OCCViewer_ViewWindow.h>
48 #include <SVTK_ViewManager.h>
49 #include <SVTK_ViewModel.h>
50 #include <gp_Ax1.hxx>
51 #include <gp_Ax2.hxx>
52 #include <gp_Ax3.hxx>
53 #include <gp_Vec.hxx>
54 #include <gp_Pnt.hxx>
55 #include <gp_Pln.hxx>
56 #include <gp.hxx>
57 #include <TopoDS_Face.hxx>
58 #include <TopoDS.hxx>
59 #include <BRepBuilderAPI_MakeFace.hxx>
60 #include <QSet>
61 #include <HYDROData_StreamLinearInterpolation.h>
62
63 //#define _DEVDEBUG_
64 #include "HYDRO_trace.hxx"
65
66 void insertProfileInToOrder( const QString& theProfileName,
67                              const double&  theProfilePar,
68                              QStringList&   theProfiles,
69                              QList<double>& theProfileParams )
70 {
71   DEBTRACE("insertProfileInToOrder");
72   bool anIsInserted = false;
73   for ( int k = 0; k < theProfileParams.length(); ++k )
74   {
75     const double& aParam = theProfileParams.value( k );
76     if ( theProfilePar < aParam )
77     {
78       theProfiles.insert( k, theProfileName );
79       theProfileParams.insert( k, theProfilePar );
80       anIsInserted = true;
81       break;
82     }
83   }
84   
85   if ( !anIsInserted )
86   {
87     theProfiles << theProfileName;
88     theProfileParams << theProfilePar;
89   }
90 }
91
92 void HYDROGUI_StreamOp::hideBathy(HYDROGUI_Module *theModule)
93 {
94     DEBTRACE("HYDROGUI_StreamOp::hideBathy");
95     Handle(HYDROData_Stream) editedObject =
96     Handle(HYDROData_Stream)::DownCast(HYDROGUI_Tool::GetSelectedObject(theModule));
97     if (!editedObject.IsNull())
98     {
99         QString bathyName = editedObject->GetBathyName();
100         DEBTRACE("bathyName " << bathyName.toStdString());
101         if (!bathyName.isEmpty())
102         {
103             Handle(HYDROData_Entity) aBathyObject = HYDROGUI_Tool::FindObjectByName(theModule, bathyName);
104             if (!aBathyObject.IsNull())
105             {
106                 DEBTRACE("Hide " << bathyName.toStdString());
107                 int anUpdateFlags = UF_OCCViewer;
108                 size_t aViewerId = HYDROGUI_Tool::GetActiveOCCViewId(theModule);
109                 theModule->setObjectVisible(aViewerId, aBathyObject, false);
110                 theModule->update( anUpdateFlags );
111             }
112         }
113     }
114 }
115
116 HYDROGUI_StreamOp::HYDROGUI_StreamOp(HYDROGUI_Module *theModule, bool theIsEdit) :
117         HYDROGUI_Operation(theModule), myIsEdit(theIsEdit), myPreviewPrs( NULL)
118 {
119     DEBTRACE("HYDROGUI_StreamOp");
120     setName(theIsEdit ? tr("EDIT_STREAM") : tr("CREATE_STREAM"));
121 }
122
123 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
124 {
125   DEBTRACE("~HYDROGUI_StreamOp");
126   erasePreview();
127 }
128
129 void HYDROGUI_StreamOp::startOperation()
130 {
131   DEBTRACE("startOperation");
132   HYDROGUI_Operation::startOperation();
133   int mode = 0; //DTM mode by def
134
135   if ( !myIsEdit || isApplyAndClose() )
136     myEditedObject.Nullify();
137   myHydAxis.clear();
138   myProfiles.clear();
139   myProfileParams.clear();
140
141   // Get panel and reset its state
142   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
143   aPanel->reset();
144
145   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
146   if ( myIsEdit )
147   {
148     if ( isApplyAndClose() )
149       myEditedObject =
150         Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
151     if ( !myEditedObject.IsNull() )
152     {          
153       mode = myEditedObject->GetInterpolationMethod();
154       anObjectName = myEditedObject->GetName();
155       DEBTRACE("anObjectName " << anObjectName.toStdString());
156       //aPanel->setMode(mode);
157
158       // Hydraulic axis
159       Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
160       
161       TopoDS_Face aPlane;
162       HYDROData_Stream::BuildRefFace( aPlane ) ;
163       if ( !aHydraulicAxis.IsNull() )
164         myHydAxis = aHydraulicAxis->GetName();
165
166       // Stream profiles
167       HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
168       for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
169       {
170         Handle(HYDROData_Profile) aProfile = 
171           Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
172         if ( aProfile.IsNull() )
173           continue;
174
175         QString aProfileName = aProfile->GetName();        
176         myProfiles      << aProfileName;
177
178         if (!aHydraulicAxis.IsNull())
179         {
180           Standard_Real aProfilePar = 0.0;
181           HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
182           myProfileParams << aProfilePar;
183         }
184       }      
185     }
186   }
187
188   // Update the panel
189   // set the edited object name
190   aPanel->setObjectName( anObjectName );
191
192   // set the existing 2D polylines names to the panel
193   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
194
195   aPanel->setLeftBankNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
196   aPanel->setRightBankNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
197
198   bool isBlocked = aPanel->blockSignals( true );
199   if (myIsEdit)
200   {
201     if (mode == 0)
202     {
203       aPanel->setDDZ( myEditedObject->GetDDZ() );
204     }
205     else
206     {
207       aPanel->setNbProfilePoints( myEditedObject->GetNbProfilePoints() );
208       aPanel->setLeftBankName(myEditedObject->GetLeftBank()->GetName());
209       aPanel->setRightBankName(myEditedObject->GetRightBank()->GetName());
210     }
211     aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
212   }  
213   
214   aPanel->setMode(mode);
215   aPanel->blockSignals( isBlocked );
216
217   // synchronize the panel state with the edited object state
218   updatePanelData();
219   // Create preview
220   createPreview();
221 }
222
223 void HYDROGUI_StreamOp::abortOperation()
224 {
225   DEBTRACE("abortOperation");
226   erasePreview();
227   HYDROGUI_Operation::abortOperation();
228 }
229
230 void HYDROGUI_StreamOp::commitOperation()
231 {
232   DEBTRACE("commitOperation");
233   erasePreview();
234   HYDROGUI_Operation::commitOperation();
235 }
236
237 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
238 {
239   DEBTRACE("createInputPanel");
240   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
241
242   connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
243   connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ), 
244            this, SLOT( onRemoveProfiles( const QStringList& ) ) );
245   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
246            this, SLOT( onAxisChanged( const QString& ) ) );
247
248   connect( aPanel, SIGNAL( DDZValueChanged( double ) ),  this, SLOT( onDDZValueChanged( double ) ) );
249   connect( aPanel, SIGNAL( SSValueChanged( double ) ),  this, SLOT( onSSValueChanged( double ) ) );
250
251   connect( aPanel, SIGNAL( LeftBankChanged( const QString& ) ), 
252     this, SLOT( onLeftBankChanged( const QString& ) ) );
253
254   connect( aPanel, SIGNAL( RightBankChanged( const QString& ) ), 
255     this, SLOT( onRightBankChanged( const QString& ) ) );
256
257   connect( aPanel, SIGNAL( ProfilePointsChanged( int ) ),  this, SLOT( onProfilePointsChanged( int ) ) );
258
259   connect( aPanel, SIGNAL( ModeChanged( bool ) ), this, SLOT( onModeChanged( bool ) ) );
260
261   return aPanel;
262 }
263
264 void HYDROGUI_StreamOp::apply()
265 {
266   DEBTRACE("apply");
267   QApplication::setOverrideCursor( Qt::WaitCursor );
268   if (myEditedObject)
269   {
270       QString anObjectName = myEditedObject->GetName();
271       DEBTRACE("anObjectName " << anObjectName.toStdString());
272   }
273
274   startDocOperation();
275
276   int anUpdateFlags = 0;
277   QString anErrorMsg;
278
279   bool aResult = false;
280   QStringList aBrowseObjectsEntries;
281
282   try
283   {
284     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
285   }
286   catch ( Standard_Failure& e )
287   {
288     anErrorMsg = e.GetMessageString();
289     aResult = false;
290   }
291   catch ( ... )
292   {
293     aResult = false;
294   }
295   
296   QApplication::restoreOverrideCursor();
297
298   if ( aResult )
299   {
300     module()->update( anUpdateFlags );
301     commitDocOperation();
302     commit();
303     browseObjects( aBrowseObjectsEntries );
304   }
305   else
306   {
307     myEditedObject->Remove();
308     module()->setObjectRemoved( myEditedObject );  
309
310     if ( isToAbortOnApply() )
311       abortDocOperation();
312
313     abort();
314     SUIT_MessageBox::critical( module()->getApp()->desktop(),
315                                tr( "CREATE_STREAM_ERROR" ),
316                                anErrorMsg ); 
317
318   } 
319 }
320 //#include <HYDROData_StreamLinearInterpolation.h>
321 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
322                                       QString& theErrorMsg,
323                                       QStringList& theBrowseObjectsEntries )
324 {
325   DEBTRACE("processApply");
326   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
327   if ( !aPanel )
328     return false;
329
330   // Check whether the object name is not empty
331   QString anObjectName = aPanel->getObjectName().simplified();
332   if ( anObjectName.isEmpty() )
333   {
334     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
335     return false;
336   }
337
338   // Check that there are no other objects with the same name in the document
339   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
340   {
341     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
342     if( !anObject.IsNull() )
343     {
344       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
345       return false;
346     }
347   }
348
349   if ( myEditedObject.IsNull() ) // Create new data model object
350     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
351
352   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
353     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
354  //if ( aHydAxis.IsNull() )
355  //{
356  //  theErrorMsg = tr( "AXIS_NOT_DEFINED" );
357  //  return false;
358  //}
359
360   // Check if at least 2 profiles is set
361   HYDROData_SequenceOfObjects aRefProfiles;
362   for ( int i = 0; i < myProfiles.length(); ++i )
363   {
364     QString aProfileName = myProfiles.value( i );
365
366     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
367       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
368     if ( !aProfile.IsNull() )
369       aRefProfiles.Append( aProfile );
370   }
371
372   if ( aRefProfiles.Length() < 2 )
373   {
374     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
375     return false;
376   }
377
378   int InterpMethod = aPanel->getMode();
379
380   myEditedObject->SetInterpolationMethod(InterpMethod);
381
382   if (InterpMethod == 0)
383   {
384     //DTM
385     bool ToOrder = !aHydAxis.IsNull();
386     myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
387     myEditedObject->SetName( anObjectName );
388
389     if (!aHydAxis.IsNull())
390       myEditedObject->SetHydraulicAxis( aHydAxis );
391     myEditedObject->SetProfiles( aRefProfiles, false );
392     myEditedObject->SetDDZ( aPanel->getDDZ() );
393     myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
394   }
395   else if (InterpMethod == 1)
396   {
397     //LISM
398     myEditedObject->SetProfiles (aRefProfiles, false) ;
399     myEditedObject->SetName( anObjectName );
400     if (!aHydAxis.IsNull())
401       myEditedObject->SetHydraulicAxis( aHydAxis );
402     Handle(HYDROData_PolylineXY) aLeftBank = Handle(HYDROData_PolylineXY)::DownCast(
403       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getLeftBankName(), KIND_POLYLINEXY ) );
404     Handle(HYDROData_PolylineXY) aRightBank = Handle(HYDROData_PolylineXY)::DownCast(
405       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRightBankName(), KIND_POLYLINEXY ) );
406     myEditedObject->SetLeftBank(aLeftBank);
407     myEditedObject->SetRightBank(aRightBank);
408
409     myEditedObject->SetNbProfilePoints( aPanel->getNbProfilePoints() );
410     myEditedObject->SetHaxStep( aPanel->getSpatialStep() );
411   }
412
413   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
414   {
415     myEditedObject->Update();
416     //NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
417     //myEditedObject->GetWarnings(warnings);
418     //QString totalWarning;
419    //for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
420    //{
421    //  const QSet<QString>& V = it.Value();
422    //  if (V.empty())
423    //    continue;
424    //  const Handle(HYDROData_Profile)& K = it.Key();
425    //  QString ProfName = K->GetName();
426    //  foreach (QString str, V)
427    //  {
428    //    totalWarning + "Profile: " + ProfName + ":  " + str + "\n";    
429    //  }
430    //}
431    //if (totalWarning != "")
432    //  SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), totalWarning);
433   }
434
435   if ( !myIsEdit )
436   {
437     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
438     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
439   }
440
441   // Erase preview
442   erasePreview();
443
444   // Show the object in case of creation mode of the operation
445   if( !myIsEdit ) {
446     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
447     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
448     theBrowseObjectsEntries.append( anEntry );
449   }
450
451   module()->setIsToUpdate( myEditedObject );
452
453   // Set update flags
454   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
455
456   return true;
457 }
458
459 void HYDROGUI_StreamOp::createPreview()
460 {
461   DEBTRACE("createPreview");
462   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
463   int mode = aPanel->getMode();
464
465   LightApp_Application* anApp = module()->getApp();
466   if ( !getPreviewManager() )
467   {
468     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
469       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
470   }
471
472   OCCViewer_ViewManager* aViewManager = getPreviewManager();
473   if ( aViewManager && !myPreviewPrs )
474   {
475     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
476     {
477       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
478       if ( !aCtx.IsNull() )
479       {
480         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
481
482         QColor aFillingColor = Qt::green;
483         QColor aBorderColor = Qt::transparent;
484         if ( !myEditedObject.IsNull() )
485         {
486           aFillingColor = myEditedObject->GetFillingColor();
487           aBorderColor = myEditedObject->GetBorderColor();
488         }
489
490         myPreviewPrs->setFillingColor( aFillingColor, false, false );
491         myPreviewPrs->setBorderColor( aBorderColor, false, false );
492       }
493     }
494   }
495
496   if ( !aViewManager || !myPreviewPrs )
497     return;
498
499   if (mode == 0)
500   {
501     Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
502       HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
503
504     HYDROData_SequenceOfObjects aRefProfiles;
505     //std::vector<Handle(HYDROData_Profile)> aRefProfiles;
506     int plen = myProfiles.length();
507     for ( int i = 0; i < plen; ++i )
508     {
509       QString aProfileName = myProfiles.value( i );
510
511       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
512         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
513       if ( !aProfile.IsNull() )
514         aRefProfiles.Append( aProfile );
515     }
516
517     HYDROData_Stream::PrsDefinition aPrsDef;
518
519     TopoDS_Shape Out3dPres;
520     TopoDS_Shape Out2dPres;
521     TopoDS_Shape OutLeftB;
522     TopoDS_Shape OutRightB;
523     TopoDS_Shape OutInlet;
524     TopoDS_Shape OutOutlet;
525
526     double ddz = aPanel->getDDZ();
527     double ss = aPanel->getSpatialStep();
528
529     std::set<int> InvInd;
530
531 #ifdef _DEBUG
532     const int MAX_POINTS_IN_PREVIEW = 50000;
533 #else
534     const int MAX_POINTS_IN_PREVIEW = 500000;
535 #endif
536
537     HYDROData_Bathymetry::AltitudePoints points;
538
539     bool ProjStat = true;
540     NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
541     bool ToEstimateWarnings = false;
542     HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
543       OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW, ProjStat, warnings, ToEstimateWarnings );
544
545     aPanel->clearAllBackgroundColorsForProfileList();
546     // for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
547     //   aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
548
549     aPrsDef.myInlet = OutInlet;
550     aPrsDef.myOutlet = OutOutlet;
551     aPrsDef.myLeftBank = OutLeftB;
552     aPrsDef.myRightBank = OutRightB;
553     if (ProjStat)
554       aPrsDef.myPrs2D = Out2dPres;
555     aPrsDef.myPrs3D = Out3dPres;
556
557     aPanel->clearWarnings();
558     if (!ProjStat)
559       aPanel->addWarning(tr("STREAM_PROJECTION_FAILED"));
560
561     QVector<QString> warningsStr;
562
563     for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
564     {
565       QSet<QString> V = it.Value();
566       if (V.empty())
567         continue;   
568       const Handle(HYDROData_Profile)& K = it.Key();
569       QString ProfName = K->GetName();
570       foreach (QString str, V)
571       {
572         warningsStr.append(ProfName + ":  " + str);
573         //aPanel->addWarning(ProfName + ":  " + str);    
574       }
575
576       aPanel->setBackgroundColorForProfileList(ProfName, QColor(Qt::yellow));
577     }
578
579     std::sort(warningsStr.begin(), warningsStr.end());
580     for (int k=0;k<warningsStr.size();k++)
581       aPanel->addWarning(warningsStr[k]);
582
583     myPreviewPrs->setShape( aPrsDef.myPrs2D );
584   }
585   else //mode == 1
586   {
587     aPanel->clearAllBackgroundColorsForProfileList();
588     aPanel->clearWarnings();
589     int nbProfPoints = aPanel->getNbProfilePoints();
590     double ss = aPanel->getSpatialStep();
591     QString axisName = aPanel->getAxisName();
592     QString leftBankName = aPanel->getLeftBankName();
593     QString rightBankName = aPanel->getRightBankName();
594     QList<Handle(HYDROData_Entity)> listProf = aPanel->getProfiles();
595     HYDROData_SequenceOfObjects seqProf;
596     foreach (Handle(HYDROData_Entity) ent, listProf)
597       seqProf.Append(ent);
598
599     Handle(HYDROData_PolylineXY) axis = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), axisName, KIND_POLYLINEXY ) );
600     Handle(HYDROData_PolylineXY) LB = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), leftBankName, KIND_POLYLINEXY ) );
601     Handle(HYDROData_PolylineXY) RB = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), rightBankName, KIND_POLYLINEXY ) );
602     HYDROData_Bathymetry::AltitudePoints outBathypoints;
603
604     std::vector<std::string> warnings;
605     HYDROData_Stream::PrsDefinition prsDef;
606
607     HYDROData_StreamLinearInterpolation::Perform(seqProf, 500, 1, axis, LB, RB, outBathypoints, true, true, prsDef, &warnings); 
608     //TODO for prs3d??
609     myPreviewPrs->setShape( prsDef.myPrs2D );
610     for (int i=0;i<warnings.size();i++)
611       aPanel->addWarning(warnings[i].c_str());
612   }
613 }
614
615 void HYDROGUI_StreamOp::erasePreview()
616 {
617   DEBTRACE("erasePreview");
618   if( myPreviewPrs )
619   {
620     delete myPreviewPrs;
621     myPreviewPrs = 0;
622   }
623 }
624
625 void HYDROGUI_StreamOp::onAddProfiles()
626 {
627   DEBTRACE("onAddProfiles");
628   //TODO skip if mode == 1??
629   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
630     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
631   //if ( aHydAxis.IsNull() )
632    // return;
633
634   TopoDS_Face aPlane;
635   HYDROData_Stream::BuildRefFace( aPlane );
636
637   // Get the current profiles list
638   QStringList aCurrentProfiles = myProfiles;
639     
640   // Get the selected profiles ( in the Object Browser )
641   QStringList anInvalidProfiles;
642   QStringList anExistingProfiles;
643   QStringList aHasNoIntersectionProfiles;
644   QStringList aVerifiedProfiles;
645
646   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
647
648   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
649   {
650     Handle(HYDROData_Profile) aProfile = 
651       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
652     if ( aProfile.IsNull() )
653       continue;
654
655     QString aProfileName = aProfile->GetName();
656     Standard_Real aProfilePar = 0.0;
657
658     // Check the profile, if all is ok - add it to the list
659     if ( !aProfile->IsValid() )
660     {
661       // check whether the profile is valid
662       anInvalidProfiles << aProfileName;
663     }
664     else if ( aCurrentProfiles.contains( aProfileName ) )
665     {
666       // check whether the profile is already added
667       anExistingProfiles << aProfileName;
668     }
669     else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
670     {
671       // check whether the profile has intersection
672       aHasNoIntersectionProfiles << aProfileName;
673     }
674     else
675     {
676       // Insert profile in correct place
677       // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
678       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
679       aVerifiedProfiles << aProfileName;
680     }
681   }
682  
683   // Show message box with the ignored profiles
684   if ( !anInvalidProfiles.isEmpty() ||
685        !anExistingProfiles.isEmpty() ||
686        !aHasNoIntersectionProfiles.isEmpty() )
687   {
688     QString aMessage = tr( "IGNORED_PROFILES" );
689     if ( !anInvalidProfiles.isEmpty() )
690     {
691       aMessage.append( "\n\n" );
692       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
693     }
694     if ( !anExistingProfiles.isEmpty() )
695     {
696       aMessage.append( "\n\n" );
697       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
698     }
699     if ( !aHasNoIntersectionProfiles.isEmpty() )
700     {
701       aMessage.append( "\n\n" );
702       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
703     }
704
705     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
706   }
707
708   if ( aVerifiedProfiles.isEmpty() )
709     return;
710
711   // Update the panel
712   updatePanelData();
713
714   // Update preview
715   createPreview();
716 }
717
718 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
719 {
720   DEBTRACE("onRemoveProfiles");
721   QStringList aToRemove = theProfilesToRemove;
722   
723   aToRemove.removeDuplicates();
724   if ( aToRemove.isEmpty() )
725     return;
726
727   bool isRemoved = false;
728
729   // Remove profiles
730   for ( int i = 0; i < aToRemove.length(); ++i )
731   {
732     const QString& aProfileName = aToRemove.value( i );
733
734     int aProfileId = myProfiles.indexOf( aProfileName );
735     if ( aProfileId < 0 )
736       continue;
737
738     myProfiles.removeAt( aProfileId );
739     myProfileParams.removeAt( aProfileId );
740     isRemoved = true;
741   }
742
743   if ( isRemoved )
744   {
745     // Update the panel
746     updatePanelData();
747
748     // Update preview
749     createPreview();
750   }
751 }
752
753 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
754 {
755   DEBTRACE("onDDZValueChanged");
756   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
757   if (!aPanel)
758     return;
759   if (aPanel->getMode() == 0)
760     createPreview();
761 }
762
763 void HYDROGUI_StreamOp::onSSValueChanged( double d )
764 {
765   DEBTRACE("onSSValueChanged");
766   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
767   if (!aPanel)
768     return;
769    createPreview();
770 }
771
772 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
773 {
774   DEBTRACE("onAxisChanged");
775   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
776   if (!aPanel)
777     return;
778   // Get axis object   
779   Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
780     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
781
782   // Prepare data for intersection check
783   TopoDS_Face aPlane;
784   HYDROData_Stream::BuildRefFace( aPlane );
785   //{
786   //  SUIT_MessageBox::critical( module()->getApp()->desktop(), 
787   //                            tr( "BAD_SELECTED_POLYLINE_TLT" ),
788   //                            tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
789   //  // To restore the old axis
790   //  updatePanelData();
791   //  return;
792   //}
793
794   QStringList   aNewProfiles;
795   QList<double> aNewProfileParams;
796   QStringList   aHasNoIntersectionProfiles;
797
798   // Get list of profiles which do not intersect the axis
799   for ( int i = 0; i < myProfiles.length(); ++i )
800   {
801     QString aProfileName = myProfiles.value( i );
802
803     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
804       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
805     if ( aProfile.IsNull() )
806       continue;
807       
808     Standard_Real aProfilePar = 0.0;
809     if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
810     {
811       // Insert profile in correct place
812       insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
813     }
814     else
815     {
816       aHasNoIntersectionProfiles << aProfile->GetName();
817     }
818   }
819
820   // If there are profiles which don't intersect the new axis - show confirmation message box
821   bool isConfirmed = true;
822   if ( !aHasNoIntersectionProfiles.isEmpty() )
823   {
824     SUIT_MessageBox::StandardButtons aButtons = 
825       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
826
827     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
828     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
829                                              tr( "CONFIRMATION" ),
830                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
831                                              aButtons, 
832                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
833   }
834
835   // Check if the user has confirmed axis change
836   if ( !isConfirmed )
837   {
838     // To restore the old axis
839     updatePanelData();
840   }
841   else
842   {
843     // Update data
844     myHydAxis = theNewAxis;
845     myProfiles = aNewProfiles;
846     myProfileParams = aNewProfileParams;
847
848     // Update the panel
849     updatePanelData();
850
851     // Update preview
852     createPreview();
853   }
854 }
855
856 void HYDROGUI_StreamOp::onLeftBankChanged( const QString& theNewAxis )
857 {
858   DEBTRACE("onLeftBankChanged");
859   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
860   if (!aPanel)
861     return;
862   if (aPanel->getMode() == 1)
863     createPreview();
864 }
865
866 void HYDROGUI_StreamOp::onRightBankChanged( const QString& theNewAxis )
867 {
868   DEBTRACE("onRightBankChanged");
869   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
870   if (!aPanel)
871     return;
872   if (aPanel->getMode() == 1)
873     createPreview();
874 }
875
876 void HYDROGUI_StreamOp::onProfilePointsChanged( int d )
877 {
878   DEBTRACE("onProfilePointsChanged");
879   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
880   if (!aPanel)
881     return;
882   if (aPanel->getMode() == 1)
883     createPreview();
884 }
885
886 void HYDROGUI_StreamOp::onModeChanged( bool mode )
887 {
888   DEBTRACE("onModeChanged");
889   createPreview();
890 }
891
892 void HYDROGUI_StreamOp::updatePanelData()
893 {
894   DEBTRACE("updatePanelData");
895   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
896   if ( !aPanel )
897     return;
898
899   aPanel->setAxisName( myHydAxis );
900   aPanel->setProfiles( myProfiles );
901
902 }