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>
41 #include <OCCViewer_ViewManager.h>
42 #include <OCCViewer_ViewModel.h>
43 #include <OCCViewer_ViewWindow.h>
51 #include <TopoDS_Face.hxx>
53 #include <BRepBuilderAPI_MakeFace.hxx>
55 void insertProfileInToOrder( const QString& theProfileName,
56 const double& theProfilePar,
57 QStringList& theProfiles,
58 QList<double>& theProfileParams )
60 bool anIsInserted = false;
61 for ( int k = 0; k < theProfileParams.length(); ++k )
63 const double& aParam = theProfileParams.value( k );
64 if ( theProfilePar < aParam )
66 theProfiles.insert( k, theProfileName );
67 theProfileParams.insert( k, theProfilePar );
75 theProfiles << theProfileName;
76 theProfileParams << theProfilePar;
80 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
81 : HYDROGUI_Operation( theModule ),
82 myIsEdit( theIsEdit ),
85 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
88 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
93 void HYDROGUI_StreamOp::startOperation()
95 HYDROGUI_Operation::startOperation();
97 if ( !myIsEdit || isApplyAndClose() )
98 myEditedObject.Nullify();
101 myProfileParams.clear();
103 // Get panel and reset its state
104 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
107 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
110 if ( isApplyAndClose() )
112 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
113 if ( !myEditedObject.IsNull() )
115 anObjectName = myEditedObject->GetName();
118 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
119 if ( !aHydraulicAxis.IsNull() )
121 myHydAxis = aHydraulicAxis->GetName();
124 HYDROData_Stream::BuildRefFace( aPlane ) ;
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();
137 Standard_Real aProfilePar = 0.0;
138 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
140 myProfiles << aProfileName;
141 myProfileParams << aProfilePar;
149 // set the edited object name
150 aPanel->setObjectName( anObjectName );
152 // set the existing 2D polylines names to the panel
153 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
157 aPanel->setDDZ( myEditedObject->GetDDZ() );
158 aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
161 // synchronize the panel state with the edited object state
168 void HYDROGUI_StreamOp::abortOperation()
171 HYDROGUI_Operation::abortOperation();
174 void HYDROGUI_StreamOp::commitOperation()
177 HYDROGUI_Operation::commitOperation();
180 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
182 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
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& ) ) );
190 connect( aPanel, SIGNAL( DDZValueChanged( double ) ), this, SLOT( onDDZValueChanged( double ) ) );
191 connect( aPanel, SIGNAL( SSValueChanged( double ) ), this, SLOT( onSSValueChanged( double ) ) );
195 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
196 QString& theErrorMsg,
197 QStringList& theBrowseObjectsEntries )
199 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
203 // Check whether the object name is not empty
204 QString anObjectName = aPanel->getObjectName().simplified();
205 if ( anObjectName.isEmpty() )
207 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
211 // Check that there are no other objects with the same name in the document
212 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
214 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
215 if( !anObject.IsNull() )
217 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
222 if ( myEditedObject.IsNull() ) // Create new data model object
223 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
225 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
226 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
227 //if ( aHydAxis.IsNull() )
229 // theErrorMsg = tr( "AXIS_NOT_DEFINED" );
233 // Check if at least 2 profiles is set
234 HYDROData_SequenceOfObjects aRefProfiles;
235 for ( int i = 0; i < myProfiles.length(); ++i )
237 QString aProfileName = myProfiles.value( i );
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 );
245 if ( aRefProfiles.Length() < 2 )
247 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
253 if (aHydAxis.IsNull())
256 myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
257 myEditedObject->SetName( anObjectName );
259 if (!aHydAxis.IsNull())
260 myEditedObject->SetHydraulicAxis( aHydAxis );
261 myEditedObject->SetProfiles( aRefProfiles, false );
262 myEditedObject->SetDDZ( aPanel->getDDZ() );
263 myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
265 if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
266 myEditedObject->Update();
270 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
271 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
277 // Show the object in case of creation mode of the operation
279 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
280 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
281 theBrowseObjectsEntries.append( anEntry );
284 module()->setIsToUpdate( myEditedObject );
287 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
292 void HYDROGUI_StreamOp::createPreview()
294 LightApp_Application* anApp = module()->getApp();
295 if ( !getPreviewManager() )
297 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
298 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
301 OCCViewer_ViewManager* aViewManager = getPreviewManager();
302 if ( aViewManager && !myPreviewPrs )
304 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
306 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
307 if ( !aCtx.IsNull() )
309 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
311 QColor aFillingColor = Qt::green;
312 QColor aBorderColor = Qt::transparent;
313 if ( !myEditedObject.IsNull() )
315 aFillingColor = myEditedObject->GetFillingColor();
316 aBorderColor = myEditedObject->GetBorderColor();
319 myPreviewPrs->setFillingColor( aFillingColor, false, false );
320 myPreviewPrs->setBorderColor( aBorderColor, false, false );
325 if ( !aViewManager || !myPreviewPrs )
328 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
329 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
331 HYDROData_SequenceOfObjects aRefProfiles;
332 //std::vector<Handle_HYDROData_Profile> aRefProfiles;
333 int plen = myProfiles.length();
334 for ( int i = 0; i < plen; ++i )
336 QString aProfileName = myProfiles.value( i );
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 );
344 HYDROData_Stream::PrsDefinition aPrsDef;
346 TopoDS_Shape Out3dPres;
347 TopoDS_Shape Out2dPres;
348 TopoDS_Shape OutLeftB;
349 TopoDS_Shape OutRightB;
350 TopoDS_Shape OutInlet;
351 TopoDS_Shape OutOutlet;
353 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
354 double ddz = aPanel->getDDZ();
355 double ss = aPanel->getSpatialStep();
357 std::set<int> InvInd;
359 HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, HYDROData_Bathymetry::AltitudePoints(), Out3dPres, Out2dPres, OutLeftB, OutRightB,
360 OutInlet, OutOutlet, true, true, InvInd);
362 //TODO!! aPanel->my---;
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;
371 myPreviewPrs->setShape( aPrsDef.myPrs2D );
374 void HYDROGUI_StreamOp::erasePreview()
383 void HYDROGUI_StreamOp::onAddProfiles()
385 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
386 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
387 //if ( aHydAxis.IsNull() )
391 HYDROData_Stream::BuildRefFace( aPlane );
393 // Get the current profiles list
394 QStringList aCurrentProfiles = myProfiles;
396 // Get the selected profiles ( in the Object Browser )
397 QStringList anInvalidProfiles;
398 QStringList anExistingProfiles;
399 QStringList aHasNoIntersectionProfiles;
400 QStringList aVerifiedProfiles;
402 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
404 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
406 Handle(HYDROData_Profile) aProfile =
407 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
408 if ( aProfile.IsNull() )
411 QString aProfileName = aProfile->GetName();
412 Standard_Real aProfilePar = 0.0;
414 // Check the profile, if all is ok - add it to the list
415 if ( !aProfile->IsValid() )
417 // check whether the profile is valid
418 anInvalidProfiles << aProfileName;
420 else if ( aCurrentProfiles.contains( aProfileName ) )
422 // check whether the profile is already added
423 anExistingProfiles << aProfileName;
425 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
427 // check whether the profile has intersection
428 aHasNoIntersectionProfiles << aProfileName;
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;
439 // Show message box with the ignored profiles
440 if ( !anInvalidProfiles.isEmpty() ||
441 !anExistingProfiles.isEmpty() ||
442 !aHasNoIntersectionProfiles.isEmpty() )
444 QString aMessage = tr( "IGNORED_PROFILES" );
445 if ( !anInvalidProfiles.isEmpty() )
447 aMessage.append( "\n\n" );
448 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
450 if ( !anExistingProfiles.isEmpty() )
452 aMessage.append( "\n\n" );
453 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
455 if ( !aHasNoIntersectionProfiles.isEmpty() )
457 aMessage.append( "\n\n" );
458 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
461 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
464 if ( aVerifiedProfiles.isEmpty() )
474 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
476 QStringList aToRemove = theProfilesToRemove;
478 aToRemove.removeDuplicates();
479 if ( aToRemove.isEmpty() )
482 bool isRemoved = false;
485 for ( int i = 0; i < aToRemove.length(); ++i )
487 const QString& aProfileName = aToRemove.value( i );
489 int aProfileId = myProfiles.indexOf( aProfileName );
490 if ( aProfileId < 0 )
493 myProfiles.removeAt( aProfileId );
494 myProfileParams.removeAt( aProfileId );
508 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
513 void HYDROGUI_StreamOp::onSSValueChanged( double d )
518 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
521 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
522 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
524 // Prepare data for intersection check
526 HYDROData_Stream::BuildRefFace( aPlane );
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();
536 QStringList aNewProfiles;
537 QList<double> aNewProfileParams;
538 QStringList aHasNoIntersectionProfiles;
540 // Get list of profiles which do not intersect the axis
541 for ( int i = 0; i < myProfiles.length(); ++i )
543 QString aProfileName = myProfiles.value( i );
545 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
546 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
547 if ( aProfile.IsNull() )
550 Standard_Real aProfilePar = 0.0;
551 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
553 // Insert profile in correct place
554 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
558 aHasNoIntersectionProfiles << aProfile->GetName();
562 // If there are profiles which don't intersect the new axis - show confirmation message box
563 bool isConfirmed = true;
564 if ( !aHasNoIntersectionProfiles.isEmpty() )
566 SUIT_MessageBox::StandardButtons aButtons =
567 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
569 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
570 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
571 tr( "CONFIRMATION" ),
572 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
574 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
577 // Check if the user has confirmed axis change
580 // To restore the old axis
586 myHydAxis = theNewAxis;
587 myProfiles = aNewProfiles;
588 myProfileParams = aNewProfileParams;
598 void HYDROGUI_StreamOp::updatePanelData()
600 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
604 aPanel->setAxisName( myHydAxis );
605 aPanel->setProfiles( myProfiles );