]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_StreamOp.cxx
Salome HOME
Merge branch 'BR_HYDRO_IMPS_2016' of ssh://gitolite3@git.salome-platform.org/modules...
[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
29 #include <HYDROData_Document.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_DTM.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
37
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40
41 #include <OCCViewer_ViewManager.h>
42 #include <OCCViewer_ViewModel.h>
43 #include <OCCViewer_ViewWindow.h>
44 #include <gp_Ax1.hxx>
45 #include <gp_Ax2.hxx>
46 #include <gp_Ax3.hxx>
47 #include <gp_Vec.hxx>
48 #include <gp_Pnt.hxx>
49 #include <gp_Pln.hxx>
50 #include <gp.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS.hxx>
53 #include <BRepBuilderAPI_MakeFace.hxx>
54
55 void insertProfileInToOrder( const QString& theProfileName,
56                              const double&  theProfilePar,
57                              QStringList&   theProfiles,
58                              QList<double>& theProfileParams )
59 {
60   bool anIsInserted = false;
61   for ( int k = 0; k < theProfileParams.length(); ++k )
62   {
63     const double& aParam = theProfileParams.value( k );
64     if ( theProfilePar < aParam )
65     {
66       theProfiles.insert( k, theProfileName );
67       theProfileParams.insert( k, theProfilePar );
68       anIsInserted = true;
69       break;
70     }
71   }
72   
73   if ( !anIsInserted )
74   {
75     theProfiles << theProfileName;
76     theProfileParams << theProfilePar;
77   }
78 }
79
80 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
81 : HYDROGUI_Operation( theModule ), 
82   myIsEdit( theIsEdit ),
83   myPreviewPrs( NULL )
84 {
85   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
86 }
87
88 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
89 {
90   erasePreview();
91 }
92
93 void HYDROGUI_StreamOp::startOperation()
94 {
95   HYDROGUI_Operation::startOperation();
96
97   if ( !myIsEdit || isApplyAndClose() )
98     myEditedObject.Nullify();
99   myHydAxis.clear();
100   myProfiles.clear();
101   myProfileParams.clear();
102
103   // Get panel and reset its state
104   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
105   aPanel->reset();
106
107   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
108   if ( myIsEdit )
109   {
110     if ( isApplyAndClose() )
111       myEditedObject =
112         Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
113     if ( !myEditedObject.IsNull() )
114     {
115       anObjectName = myEditedObject->GetName();
116
117       // Hydraulic axis
118       Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
119       if ( !aHydraulicAxis.IsNull() )
120       {
121         myHydAxis = aHydraulicAxis->GetName();
122
123         TopoDS_Face aPlane;
124         if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
125         {
126           // Stream profiles
127           HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
128           for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
129           {
130             Handle(HYDROData_Profile) aProfile = 
131               Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
132             if ( aProfile.IsNull() )
133               continue;
134
135             QString aProfileName = aProfile->GetName();
136
137             Standard_Real aProfilePar = 0.0;
138             HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
139
140             myProfiles      << aProfileName;
141             myProfileParams << aProfilePar;
142           }
143         }
144       }
145     }
146   }
147
148   // Update the panel
149   // set the edited object name
150   aPanel->setObjectName( anObjectName );
151
152   // set the existing 2D polylines names to the panel
153   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
154
155   if (myIsEdit)
156   {
157     aPanel->setDDZ( myEditedObject->GetDDZ() );
158     aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
159   }
160
161   // synchronize the panel state with the edited object state
162   updatePanelData();
163
164   // Create preview
165   createPreview();
166 }
167
168 void HYDROGUI_StreamOp::abortOperation()
169 {
170   erasePreview();
171   HYDROGUI_Operation::abortOperation();
172 }
173
174 void HYDROGUI_StreamOp::commitOperation()
175 {
176   erasePreview();
177   HYDROGUI_Operation::commitOperation();
178 }
179
180 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
181 {
182   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
183
184   connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
185   connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ), 
186            this, SLOT( onRemoveProfiles( const QStringList& ) ) );
187   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
188            this, SLOT( onAxisChanged( const QString& ) ) );
189
190   connect( aPanel, SIGNAL( DDZValueChanged( double ) ),  this, SLOT( onDDZValueChanged( double ) ) );
191   connect( aPanel, SIGNAL( SSValueChanged( double ) ),  this, SLOT( onSSValueChanged( double ) ) );
192   return aPanel;
193 }
194
195 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
196                                       QString& theErrorMsg,
197                                       QStringList& theBrowseObjectsEntries )
198 {
199   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
200   if ( !aPanel )
201     return false;
202
203   // Check whether the object name is not empty
204   QString anObjectName = aPanel->getObjectName().simplified();
205   if ( anObjectName.isEmpty() )
206   {
207     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
208     return false;
209   }
210
211   // Check that there are no other objects with the same name in the document
212   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
213   {
214     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
215     if( !anObject.IsNull() )
216     {
217       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
218       return false;
219     }
220   }
221
222   if ( myEditedObject.IsNull() ) // Create new data model object
223     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
224
225   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
226     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
227  //if ( aHydAxis.IsNull() )
228  //{
229  //  theErrorMsg = tr( "AXIS_NOT_DEFINED" );
230  //  return false;
231  //}
232
233   // Check if at least 2 profiles is set
234   HYDROData_SequenceOfObjects aRefProfiles;
235   for ( int i = 0; i < myProfiles.length(); ++i )
236   {
237     QString aProfileName = myProfiles.value( i );
238
239     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
240       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
241     if ( !aProfile.IsNull() )
242       aRefProfiles.Append( aProfile );
243   }
244
245   if ( aRefProfiles.Length() < 2 )
246   {
247     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
248     return false;
249   }
250
251   // Set the object name
252   myEditedObject->SetName( anObjectName );
253
254   myEditedObject->SetHydraulicAxis( aHydAxis );
255   myEditedObject->SetProfiles( aRefProfiles, false );
256   myEditedObject->SetDDZ( aPanel->getDDZ() );
257   myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
258
259   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
260     myEditedObject->Update();
261
262   if ( !myIsEdit )
263   {
264     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
265     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
266   }
267
268   // Erase preview
269   erasePreview();
270
271   // Show the object in case of creation mode of the operation
272   if( !myIsEdit ) {
273     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
274     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
275     theBrowseObjectsEntries.append( anEntry );
276   }
277
278   module()->setIsToUpdate( myEditedObject );
279
280   // Set update flags
281   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
282
283   return true;
284 }
285
286 void HYDROGUI_StreamOp::createPreview()
287 {
288   LightApp_Application* anApp = module()->getApp();
289   if ( !getPreviewManager() )
290   {
291     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
292                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
293   }
294
295   OCCViewer_ViewManager* aViewManager = getPreviewManager();
296   if ( aViewManager && !myPreviewPrs )
297   {
298     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
299     {
300       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
301       if ( !aCtx.IsNull() )
302       {
303         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
304
305         QColor aFillingColor = Qt::green;
306         QColor aBorderColor = Qt::transparent;
307         if ( !myEditedObject.IsNull() )
308         {
309           aFillingColor = myEditedObject->GetFillingColor();
310           aBorderColor = myEditedObject->GetBorderColor();
311         }
312
313         myPreviewPrs->setFillingColor( aFillingColor, false, false );
314         myPreviewPrs->setBorderColor( aBorderColor, false, false );
315       }
316     }
317   }
318
319   if ( !aViewManager || !myPreviewPrs )
320     return;
321
322   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
323     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
324
325   HYDROData_SequenceOfObjects aRefProfiles;
326   //std::vector<Handle_HYDROData_Profile> aRefProfiles;
327   int plen = myProfiles.length();
328   for ( int i = 0; i < plen; ++i )
329   {
330     QString aProfileName = myProfiles.value( i );
331
332     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
333       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
334     if ( !aProfile.IsNull() )
335       aRefProfiles.Append( aProfile );
336   }
337
338   HYDROData_Stream::PrsDefinition aPrsDef;
339
340   TopoDS_Shape Out3dPres;
341   TopoDS_Shape Out2dPres;
342   TopoDS_Shape OutLeftB;
343   TopoDS_Shape OutRightB;
344   TopoDS_Shape OutInlet;
345   TopoDS_Shape OutOutlet;
346
347   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
348   double ddz = aPanel->getDDZ();
349   double ss = aPanel->getSpatialStep();
350     
351   HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, HYDROData_Bathymetry::AltitudePoints(), Out3dPres, Out2dPres, OutLeftB, OutRightB,
352     OutInlet, OutOutlet, true, true);
353
354   aPrsDef.myInlet = TopoDS::Wire(OutInlet);
355   aPrsDef.myOutlet = TopoDS::Wire(OutOutlet);
356   aPrsDef.myLeftBank = TopoDS::Wire(OutLeftB);
357   aPrsDef.myRightBank = TopoDS::Wire(OutRightB);
358   aPrsDef.myPrs2D = Out2dPres;
359   aPrsDef.myPrs3D = Out3dPres;
360
361   myPreviewPrs->setShape( aPrsDef.myPrs2D );
362 }
363
364 void HYDROGUI_StreamOp::erasePreview()
365 {
366   if( myPreviewPrs )
367   {
368     delete myPreviewPrs;
369     myPreviewPrs = 0;
370   }
371 }
372
373 void HYDROGUI_StreamOp::onAddProfiles()
374 {
375   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
376     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
377   //if ( aHydAxis.IsNull() )
378    // return;
379
380   TopoDS_Face aPlane;
381
382   if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
383     return;
384
385   // Get the current profiles list
386   QStringList aCurrentProfiles = myProfiles;
387     
388   // Get the selected profiles ( in the Object Browser )
389   QStringList anInvalidProfiles;
390   QStringList anExistingProfiles;
391   QStringList aHasNoIntersectionProfiles;
392   QStringList aVerifiedProfiles;
393
394   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
395
396   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
397   {
398     Handle(HYDROData_Profile) aProfile = 
399       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
400     if ( aProfile.IsNull() )
401       continue;
402
403     QString aProfileName = aProfile->GetName();
404     Standard_Real aProfilePar = 0.0;
405
406     // Check the profile, if all is ok - add it to the list
407     if ( !aProfile->IsValid() )
408     {
409       // check whether the profile is valid
410       anInvalidProfiles << aProfileName;
411     }
412     else if ( aCurrentProfiles.contains( aProfileName ) )
413     {
414       // check whether the profile is already added
415       anExistingProfiles << aProfileName;
416     }
417     else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
418     {
419       // check whether the profile has intersection
420       aHasNoIntersectionProfiles << aProfileName;
421     }
422     else
423     {
424       // Insert profile in correct place
425       // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
426       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
427       aVerifiedProfiles << aProfileName;
428     }
429   }
430  
431   // Show message box with the ignored profiles
432   if ( !anInvalidProfiles.isEmpty() ||
433        !anExistingProfiles.isEmpty() ||
434        !aHasNoIntersectionProfiles.isEmpty() )
435   {
436     QString aMessage = tr( "IGNORED_PROFILES" );
437     if ( !anInvalidProfiles.isEmpty() )
438     {
439       aMessage.append( "\n\n" );
440       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
441     }
442     if ( !anExistingProfiles.isEmpty() )
443     {
444       aMessage.append( "\n\n" );
445       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
446     }
447     if ( !aHasNoIntersectionProfiles.isEmpty() )
448     {
449       aMessage.append( "\n\n" );
450       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
451     }
452
453     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
454   }
455
456   if ( aVerifiedProfiles.isEmpty() )
457     return;
458
459   // Update the panel
460   updatePanelData();
461
462   // Update preview
463   createPreview();
464 }
465
466 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
467 {
468   QStringList aToRemove = theProfilesToRemove;
469   
470   aToRemove.removeDuplicates();
471   if ( aToRemove.isEmpty() )
472     return;
473
474   bool isRemoved = false;
475
476   // Remove profiles
477   for ( int i = 0; i < aToRemove.length(); ++i )
478   {
479     const QString& aProfileName = aToRemove.value( i );
480
481     int aProfileId = myProfiles.indexOf( aProfileName );
482     if ( aProfileId < 0 )
483       continue;
484
485     myProfiles.removeAt( aProfileId );
486     myProfileParams.removeAt( aProfileId );
487     isRemoved = true;
488   }
489
490   if ( isRemoved )
491   {
492     // Update the panel
493     updatePanelData();
494
495     // Update preview
496     createPreview();
497   }
498 }
499
500 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
501 {
502    createPreview();
503 }
504
505 void HYDROGUI_StreamOp::onSSValueChanged( double d )
506 {
507    createPreview();
508 }
509
510 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
511 {
512   // Get axis object   
513   Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
514     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
515
516   // Prepare data for intersection check
517   TopoDS_Face aPlane;
518   if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
519   {
520     SUIT_MessageBox::critical( module()->getApp()->desktop(), 
521                               tr( "BAD_SELECTED_POLYLINE_TLT" ),
522                               tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
523     // To restore the old axis
524     updatePanelData();
525     return;
526   }
527
528   QStringList   aNewProfiles;
529   QList<double> aNewProfileParams;
530   QStringList   aHasNoIntersectionProfiles;
531
532   // Get list of profiles which do not intersect the axis
533   for ( int i = 0; i < myProfiles.length(); ++i )
534   {
535     QString aProfileName = myProfiles.value( i );
536
537     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
538       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
539     if ( aProfile.IsNull() )
540       continue;
541       
542     Standard_Real aProfilePar = 0.0;
543     if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
544     {
545       // Insert profile in correct place
546       insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
547     }
548     else
549     {
550       aHasNoIntersectionProfiles << aProfile->GetName();
551     }
552   }
553
554   // If there are profiles which don't intersect the new axis - show confirmation message box
555   bool isConfirmed = true;
556   if ( !aHasNoIntersectionProfiles.isEmpty() )
557   {
558     SUIT_MessageBox::StandardButtons aButtons = 
559       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
560
561     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
562     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
563                                              tr( "CONFIRMATION" ),
564                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
565                                              aButtons, 
566                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
567   }
568
569   // Check if the user has confirmed axis change
570   if ( !isConfirmed )
571   {
572     // To restore the old axis
573     updatePanelData();
574   }
575   else
576   {
577     // Update data
578     myHydAxis = theNewAxis;
579     myProfiles = aNewProfiles;
580     myProfileParams = aNewProfileParams;
581
582     // Update the panel
583     updatePanelData();
584
585     // Update preview
586     createPreview();
587   }
588 }
589
590 void HYDROGUI_StreamOp::updatePanelData()
591 {
592   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
593   if ( !aPanel )
594     return;
595
596   aPanel->setAxisName( myHydAxis );
597   aPanel->setProfiles( myProfiles );
598 }