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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_StreamOp.h"
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"
29 #include <HYDROData_Document.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_DTM.h>
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
42 #include <OCCViewer_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
52 #include <TopoDS_Face.hxx>
54 #include <BRepBuilderAPI_MakeFace.hxx>
56 void insertProfileInToOrder( const QString& theProfileName,
57 const double& theProfilePar,
58 QStringList& theProfiles,
59 QList<double>& theProfileParams )
61 bool anIsInserted = false;
62 for ( int k = 0; k < theProfileParams.length(); ++k )
64 const double& aParam = theProfileParams.value( k );
65 if ( theProfilePar < aParam )
67 theProfiles.insert( k, theProfileName );
68 theProfileParams.insert( k, theProfilePar );
76 theProfiles << theProfileName;
77 theProfileParams << theProfilePar;
81 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
82 : HYDROGUI_Operation( theModule ),
83 myIsEdit( theIsEdit ),
86 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
89 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
94 void HYDROGUI_StreamOp::startOperation()
96 HYDROGUI_Operation::startOperation();
98 if ( !myIsEdit || isApplyAndClose() )
99 myEditedObject.Nullify();
102 myProfileParams.clear();
104 // Get panel and reset its state
105 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
108 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
111 if ( isApplyAndClose() )
113 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
114 if ( !myEditedObject.IsNull() )
116 anObjectName = myEditedObject->GetName();
119 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
122 HYDROData_Stream::BuildRefFace( aPlane ) ;
123 if ( !aHydraulicAxis.IsNull() )
124 myHydAxis = aHydraulicAxis->GetName();
127 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
128 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
130 Handle(HYDROData_Profile) aProfile =
131 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
132 if ( aProfile.IsNull() )
135 QString aProfileName = aProfile->GetName();
136 myProfiles << aProfileName;
138 if (!aHydraulicAxis.IsNull())
140 Standard_Real aProfilePar = 0.0;
141 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
142 myProfileParams << aProfilePar;
151 // set the edited object name
152 aPanel->setObjectName( anObjectName );
154 // set the existing 2D polylines names to the panel
155 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
159 aPanel->setDDZ( myEditedObject->GetDDZ() );
160 aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
163 // synchronize the panel state with the edited object state
170 void HYDROGUI_StreamOp::abortOperation()
173 HYDROGUI_Operation::abortOperation();
176 void HYDROGUI_StreamOp::commitOperation()
179 HYDROGUI_Operation::commitOperation();
182 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
184 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
186 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
187 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
188 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
189 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
190 this, SLOT( onAxisChanged( const QString& ) ) );
192 connect( aPanel, SIGNAL( DDZValueChanged( double ) ), this, SLOT( onDDZValueChanged( double ) ) );
193 connect( aPanel, SIGNAL( SSValueChanged( double ) ), this, SLOT( onSSValueChanged( double ) ) );
197 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
198 QString& theErrorMsg,
199 QStringList& theBrowseObjectsEntries )
201 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
205 // Check whether the object name is not empty
206 QString anObjectName = aPanel->getObjectName().simplified();
207 if ( anObjectName.isEmpty() )
209 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
213 // Check that there are no other objects with the same name in the document
214 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
216 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
217 if( !anObject.IsNull() )
219 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
224 if ( myEditedObject.IsNull() ) // Create new data model object
225 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
227 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
228 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
229 //if ( aHydAxis.IsNull() )
231 // theErrorMsg = tr( "AXIS_NOT_DEFINED" );
235 // Check if at least 2 profiles is set
236 HYDROData_SequenceOfObjects aRefProfiles;
237 for ( int i = 0; i < myProfiles.length(); ++i )
239 QString aProfileName = myProfiles.value( i );
241 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
242 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
243 if ( !aProfile.IsNull() )
244 aRefProfiles.Append( aProfile );
247 if ( aRefProfiles.Length() < 2 )
249 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
255 if (aHydAxis.IsNull())
258 myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
259 myEditedObject->SetName( anObjectName );
261 if (!aHydAxis.IsNull())
262 myEditedObject->SetHydraulicAxis( aHydAxis );
263 myEditedObject->SetProfiles( aRefProfiles, false );
264 myEditedObject->SetDDZ( aPanel->getDDZ() );
265 myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
267 if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
268 myEditedObject->Update();
272 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
273 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
279 // Show the object in case of creation mode of the operation
281 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
282 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
283 theBrowseObjectsEntries.append( anEntry );
286 module()->setIsToUpdate( myEditedObject );
289 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
294 void HYDROGUI_StreamOp::createPreview()
296 LightApp_Application* anApp = module()->getApp();
297 if ( !getPreviewManager() )
299 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
300 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
303 OCCViewer_ViewManager* aViewManager = getPreviewManager();
304 if ( aViewManager && !myPreviewPrs )
306 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
308 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
309 if ( !aCtx.IsNull() )
311 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
313 QColor aFillingColor = Qt::green;
314 QColor aBorderColor = Qt::transparent;
315 if ( !myEditedObject.IsNull() )
317 aFillingColor = myEditedObject->GetFillingColor();
318 aBorderColor = myEditedObject->GetBorderColor();
321 myPreviewPrs->setFillingColor( aFillingColor, false, false );
322 myPreviewPrs->setBorderColor( aBorderColor, false, false );
327 if ( !aViewManager || !myPreviewPrs )
330 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
331 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
333 HYDROData_SequenceOfObjects aRefProfiles;
334 //std::vector<Handle_HYDROData_Profile> aRefProfiles;
335 int plen = myProfiles.length();
336 for ( int i = 0; i < plen; ++i )
338 QString aProfileName = myProfiles.value( i );
340 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
341 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
342 if ( !aProfile.IsNull() )
343 aRefProfiles.Append( aProfile );
346 HYDROData_Stream::PrsDefinition aPrsDef;
348 TopoDS_Shape Out3dPres;
349 TopoDS_Shape Out2dPres;
350 TopoDS_Shape OutLeftB;
351 TopoDS_Shape OutRightB;
352 TopoDS_Shape OutInlet;
353 TopoDS_Shape OutOutlet;
355 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
356 double ddz = aPanel->getDDZ();
357 double ss = aPanel->getSpatialStep();
359 std::set<int> InvInd;
362 const int MAX_POINTS_IN_PREVIEW = 50000;
364 const int MAX_POINTS_IN_PREVIEW = 500000;
367 HYDROData_Bathymetry::AltitudePoints points;
369 bool WireIntersections = false;
370 HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
371 OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW, WireIntersections );
373 aPanel->clearAllBackgroundColorsForProfileList();
374 for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
375 aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
377 aPrsDef.myInlet = TopoDS::Wire(OutInlet);
378 aPrsDef.myOutlet = TopoDS::Wire(OutOutlet);
379 aPrsDef.myLeftBank = TopoDS::Wire(OutLeftB);
380 aPrsDef.myRightBank = TopoDS::Wire(OutRightB);
381 if (!WireIntersections)
382 aPrsDef.myPrs2D = Out2dPres;
383 aPrsDef.myPrs3D = Out3dPres;
385 if (WireIntersections)
386 aPanel->addWarning(tr("STREAM_SELF_INTERSECTIONS"));
388 myPreviewPrs->setShape( aPrsDef.myPrs2D );
391 void HYDROGUI_StreamOp::erasePreview()
400 void HYDROGUI_StreamOp::onAddProfiles()
402 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
403 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
404 //if ( aHydAxis.IsNull() )
408 HYDROData_Stream::BuildRefFace( aPlane );
410 // Get the current profiles list
411 QStringList aCurrentProfiles = myProfiles;
413 // Get the selected profiles ( in the Object Browser )
414 QStringList anInvalidProfiles;
415 QStringList anExistingProfiles;
416 QStringList aHasNoIntersectionProfiles;
417 QStringList aVerifiedProfiles;
419 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
421 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
423 Handle(HYDROData_Profile) aProfile =
424 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
425 if ( aProfile.IsNull() )
428 QString aProfileName = aProfile->GetName();
429 Standard_Real aProfilePar = 0.0;
431 // Check the profile, if all is ok - add it to the list
432 if ( !aProfile->IsValid() )
434 // check whether the profile is valid
435 anInvalidProfiles << aProfileName;
437 else if ( aCurrentProfiles.contains( aProfileName ) )
439 // check whether the profile is already added
440 anExistingProfiles << aProfileName;
442 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
444 // check whether the profile has intersection
445 aHasNoIntersectionProfiles << aProfileName;
449 // Insert profile in correct place
450 // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
451 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
452 aVerifiedProfiles << aProfileName;
456 // Show message box with the ignored profiles
457 if ( !anInvalidProfiles.isEmpty() ||
458 !anExistingProfiles.isEmpty() ||
459 !aHasNoIntersectionProfiles.isEmpty() )
461 QString aMessage = tr( "IGNORED_PROFILES" );
462 if ( !anInvalidProfiles.isEmpty() )
464 aMessage.append( "\n\n" );
465 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
467 if ( !anExistingProfiles.isEmpty() )
469 aMessage.append( "\n\n" );
470 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
472 if ( !aHasNoIntersectionProfiles.isEmpty() )
474 aMessage.append( "\n\n" );
475 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
478 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
481 if ( aVerifiedProfiles.isEmpty() )
491 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
493 QStringList aToRemove = theProfilesToRemove;
495 aToRemove.removeDuplicates();
496 if ( aToRemove.isEmpty() )
499 bool isRemoved = false;
502 for ( int i = 0; i < aToRemove.length(); ++i )
504 const QString& aProfileName = aToRemove.value( i );
506 int aProfileId = myProfiles.indexOf( aProfileName );
507 if ( aProfileId < 0 )
510 myProfiles.removeAt( aProfileId );
511 myProfileParams.removeAt( aProfileId );
525 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
530 void HYDROGUI_StreamOp::onSSValueChanged( double d )
535 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
538 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
539 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
541 // Prepare data for intersection check
543 HYDROData_Stream::BuildRefFace( aPlane );
545 // SUIT_MessageBox::critical( module()->getApp()->desktop(),
546 // tr( "BAD_SELECTED_POLYLINE_TLT" ),
547 // tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
548 // // To restore the old axis
549 // updatePanelData();
553 QStringList aNewProfiles;
554 QList<double> aNewProfileParams;
555 QStringList aHasNoIntersectionProfiles;
557 // Get list of profiles which do not intersect the axis
558 for ( int i = 0; i < myProfiles.length(); ++i )
560 QString aProfileName = myProfiles.value( i );
562 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
563 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
564 if ( aProfile.IsNull() )
567 Standard_Real aProfilePar = 0.0;
568 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
570 // Insert profile in correct place
571 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
575 aHasNoIntersectionProfiles << aProfile->GetName();
579 // If there are profiles which don't intersect the new axis - show confirmation message box
580 bool isConfirmed = true;
581 if ( !aHasNoIntersectionProfiles.isEmpty() )
583 SUIT_MessageBox::StandardButtons aButtons =
584 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
586 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
587 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
588 tr( "CONFIRMATION" ),
589 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
591 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
594 // Check if the user has confirmed axis change
597 // To restore the old axis
603 myHydAxis = theNewAxis;
604 myProfiles = aNewProfiles;
605 myProfileParams = aNewProfileParams;
615 void HYDROGUI_StreamOp::updatePanelData()
617 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
621 aPanel->setAxisName( myHydAxis );
622 aPanel->setProfiles( myProfiles );