]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_StreamOp.cxx
Salome HOME
getting nb of intersections (non-gui) + add of profiles of stream to tree
[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         HYDROData_Stream::BuildRefFace( 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   bool ToOrder = true;
252
253   if (aHydAxis.IsNull())
254     ToOrder = false;
255
256   myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
257   myEditedObject->SetName( anObjectName );
258
259   if (!aHydAxis.IsNull())
260     myEditedObject->SetHydraulicAxis( aHydAxis );
261   myEditedObject->SetProfiles( aRefProfiles, false );
262   myEditedObject->SetDDZ( aPanel->getDDZ() );
263   myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
264
265   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
266     myEditedObject->Update();
267
268   if ( !myIsEdit )
269   {
270     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
271     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
272   }
273
274   // Erase preview
275   erasePreview();
276
277   // Show the object in case of creation mode of the operation
278   if( !myIsEdit ) {
279     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
280     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
281     theBrowseObjectsEntries.append( anEntry );
282   }
283
284   module()->setIsToUpdate( myEditedObject );
285
286   // Set update flags
287   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
288
289   return true;
290 }
291
292 void HYDROGUI_StreamOp::createPreview()
293 {
294   LightApp_Application* anApp = module()->getApp();
295   if ( !getPreviewManager() )
296   {
297     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
298                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
299   }
300
301   OCCViewer_ViewManager* aViewManager = getPreviewManager();
302   if ( aViewManager && !myPreviewPrs )
303   {
304     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
305     {
306       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
307       if ( !aCtx.IsNull() )
308       {
309         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
310
311         QColor aFillingColor = Qt::green;
312         QColor aBorderColor = Qt::transparent;
313         if ( !myEditedObject.IsNull() )
314         {
315           aFillingColor = myEditedObject->GetFillingColor();
316           aBorderColor = myEditedObject->GetBorderColor();
317         }
318
319         myPreviewPrs->setFillingColor( aFillingColor, false, false );
320         myPreviewPrs->setBorderColor( aBorderColor, false, false );
321       }
322     }
323   }
324
325   if ( !aViewManager || !myPreviewPrs )
326     return;
327
328   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
329     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
330
331   HYDROData_SequenceOfObjects aRefProfiles;
332   //std::vector<Handle_HYDROData_Profile> aRefProfiles;
333   int plen = myProfiles.length();
334   for ( int i = 0; i < plen; ++i )
335   {
336     QString aProfileName = myProfiles.value( i );
337
338     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
339       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
340     if ( !aProfile.IsNull() )
341       aRefProfiles.Append( aProfile );
342   }
343
344   HYDROData_Stream::PrsDefinition aPrsDef;
345
346   TopoDS_Shape Out3dPres;
347   TopoDS_Shape Out2dPres;
348   TopoDS_Shape OutLeftB;
349   TopoDS_Shape OutRightB;
350   TopoDS_Shape OutInlet;
351   TopoDS_Shape OutOutlet;
352
353   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
354   double ddz = aPanel->getDDZ();
355   double ss = aPanel->getSpatialStep();
356
357   std::set<int> InvInd;
358     
359   HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, HYDROData_Bathymetry::AltitudePoints(), Out3dPres, Out2dPres, OutLeftB, OutRightB,
360     OutInlet, OutOutlet, true, true, InvInd);
361
362   //TODO!! aPanel->my---;
363
364   aPrsDef.myInlet = TopoDS::Wire(OutInlet);
365   aPrsDef.myOutlet = TopoDS::Wire(OutOutlet);
366   aPrsDef.myLeftBank = TopoDS::Wire(OutLeftB);
367   aPrsDef.myRightBank = TopoDS::Wire(OutRightB);
368   aPrsDef.myPrs2D = Out2dPres;
369   aPrsDef.myPrs3D = Out3dPres;
370
371   myPreviewPrs->setShape( aPrsDef.myPrs2D );
372 }
373
374 void HYDROGUI_StreamOp::erasePreview()
375 {
376   if( myPreviewPrs )
377   {
378     delete myPreviewPrs;
379     myPreviewPrs = 0;
380   }
381 }
382
383 void HYDROGUI_StreamOp::onAddProfiles()
384 {
385   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
386     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
387   //if ( aHydAxis.IsNull() )
388    // return;
389
390   TopoDS_Face aPlane;
391   HYDROData_Stream::BuildRefFace( aPlane );
392
393   // Get the current profiles list
394   QStringList aCurrentProfiles = myProfiles;
395     
396   // Get the selected profiles ( in the Object Browser )
397   QStringList anInvalidProfiles;
398   QStringList anExistingProfiles;
399   QStringList aHasNoIntersectionProfiles;
400   QStringList aVerifiedProfiles;
401
402   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
403
404   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
405   {
406     Handle(HYDROData_Profile) aProfile = 
407       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
408     if ( aProfile.IsNull() )
409       continue;
410
411     QString aProfileName = aProfile->GetName();
412     Standard_Real aProfilePar = 0.0;
413
414     // Check the profile, if all is ok - add it to the list
415     if ( !aProfile->IsValid() )
416     {
417       // check whether the profile is valid
418       anInvalidProfiles << aProfileName;
419     }
420     else if ( aCurrentProfiles.contains( aProfileName ) )
421     {
422       // check whether the profile is already added
423       anExistingProfiles << aProfileName;
424     }
425     else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
426     {
427       // check whether the profile has intersection
428       aHasNoIntersectionProfiles << aProfileName;
429     }
430     else
431     {
432       // Insert profile in correct place
433       // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
434       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
435       aVerifiedProfiles << aProfileName;
436     }
437   }
438  
439   // Show message box with the ignored profiles
440   if ( !anInvalidProfiles.isEmpty() ||
441        !anExistingProfiles.isEmpty() ||
442        !aHasNoIntersectionProfiles.isEmpty() )
443   {
444     QString aMessage = tr( "IGNORED_PROFILES" );
445     if ( !anInvalidProfiles.isEmpty() )
446     {
447       aMessage.append( "\n\n" );
448       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
449     }
450     if ( !anExistingProfiles.isEmpty() )
451     {
452       aMessage.append( "\n\n" );
453       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
454     }
455     if ( !aHasNoIntersectionProfiles.isEmpty() )
456     {
457       aMessage.append( "\n\n" );
458       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
459     }
460
461     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
462   }
463
464   if ( aVerifiedProfiles.isEmpty() )
465     return;
466
467   // Update the panel
468   updatePanelData();
469
470   // Update preview
471   createPreview();
472 }
473
474 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
475 {
476   QStringList aToRemove = theProfilesToRemove;
477   
478   aToRemove.removeDuplicates();
479   if ( aToRemove.isEmpty() )
480     return;
481
482   bool isRemoved = false;
483
484   // Remove profiles
485   for ( int i = 0; i < aToRemove.length(); ++i )
486   {
487     const QString& aProfileName = aToRemove.value( i );
488
489     int aProfileId = myProfiles.indexOf( aProfileName );
490     if ( aProfileId < 0 )
491       continue;
492
493     myProfiles.removeAt( aProfileId );
494     myProfileParams.removeAt( aProfileId );
495     isRemoved = true;
496   }
497
498   if ( isRemoved )
499   {
500     // Update the panel
501     updatePanelData();
502
503     // Update preview
504     createPreview();
505   }
506 }
507
508 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
509 {
510    createPreview();
511 }
512
513 void HYDROGUI_StreamOp::onSSValueChanged( double d )
514 {
515    createPreview();
516 }
517
518 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
519 {
520   // Get axis object   
521   Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
522     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
523
524   // Prepare data for intersection check
525   TopoDS_Face aPlane;
526   HYDROData_Stream::BuildRefFace( aPlane );
527   //{
528   //  SUIT_MessageBox::critical( module()->getApp()->desktop(), 
529   //                            tr( "BAD_SELECTED_POLYLINE_TLT" ),
530   //                            tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
531   //  // To restore the old axis
532   //  updatePanelData();
533   //  return;
534   //}
535
536   QStringList   aNewProfiles;
537   QList<double> aNewProfileParams;
538   QStringList   aHasNoIntersectionProfiles;
539
540   // Get list of profiles which do not intersect the axis
541   for ( int i = 0; i < myProfiles.length(); ++i )
542   {
543     QString aProfileName = myProfiles.value( i );
544
545     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
546       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
547     if ( aProfile.IsNull() )
548       continue;
549       
550     Standard_Real aProfilePar = 0.0;
551     if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
552     {
553       // Insert profile in correct place
554       insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
555     }
556     else
557     {
558       aHasNoIntersectionProfiles << aProfile->GetName();
559     }
560   }
561
562   // If there are profiles which don't intersect the new axis - show confirmation message box
563   bool isConfirmed = true;
564   if ( !aHasNoIntersectionProfiles.isEmpty() )
565   {
566     SUIT_MessageBox::StandardButtons aButtons = 
567       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
568
569     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
570     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
571                                              tr( "CONFIRMATION" ),
572                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
573                                              aButtons, 
574                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
575   }
576
577   // Check if the user has confirmed axis change
578   if ( !isConfirmed )
579   {
580     // To restore the old axis
581     updatePanelData();
582   }
583   else
584   {
585     // Update data
586     myHydAxis = theNewAxis;
587     myProfiles = aNewProfiles;
588     myProfileParams = aNewProfileParams;
589
590     // Update the panel
591     updatePanelData();
592
593     // Update preview
594     createPreview();
595   }
596 }
597
598 void HYDROGUI_StreamOp::updatePanelData()
599 {
600   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
601   if ( !aPanel )
602     return;
603
604   aPanel->setAxisName( myHydAxis );
605   aPanel->setProfiles( myProfiles );
606 }