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 <QApplication>
44 #include <OCCViewer_ViewManager.h>
45 #include <OCCViewer_ViewModel.h>
46 #include <OCCViewer_ViewWindow.h>
54 #include <TopoDS_Face.hxx>
56 #include <BRepBuilderAPI_MakeFace.hxx>
58 void insertProfileInToOrder( const QString& theProfileName,
59 const double& theProfilePar,
60 QStringList& theProfiles,
61 QList<double>& theProfileParams )
63 bool anIsInserted = false;
64 for ( int k = 0; k < theProfileParams.length(); ++k )
66 const double& aParam = theProfileParams.value( k );
67 if ( theProfilePar < aParam )
69 theProfiles.insert( k, theProfileName );
70 theProfileParams.insert( k, theProfilePar );
78 theProfiles << theProfileName;
79 theProfileParams << theProfilePar;
83 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
84 : HYDROGUI_Operation( theModule ),
85 myIsEdit( theIsEdit ),
88 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
91 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
96 void HYDROGUI_StreamOp::startOperation()
98 HYDROGUI_Operation::startOperation();
100 if ( !myIsEdit || isApplyAndClose() )
101 myEditedObject.Nullify();
104 myProfileParams.clear();
106 // Get panel and reset its state
107 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
110 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
113 if ( isApplyAndClose() )
115 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
116 if ( !myEditedObject.IsNull() )
118 anObjectName = myEditedObject->GetName();
121 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
124 HYDROData_Stream::BuildRefFace( aPlane ) ;
125 if ( !aHydraulicAxis.IsNull() )
126 myHydAxis = aHydraulicAxis->GetName();
129 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
130 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
132 Handle(HYDROData_Profile) aProfile =
133 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
134 if ( aProfile.IsNull() )
137 QString aProfileName = aProfile->GetName();
138 myProfiles << aProfileName;
140 if (!aHydraulicAxis.IsNull())
142 Standard_Real aProfilePar = 0.0;
143 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
144 myProfileParams << aProfilePar;
153 // set the edited object name
154 aPanel->setObjectName( anObjectName );
156 // set the existing 2D polylines names to the panel
157 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
161 aPanel->setDDZ( myEditedObject->GetDDZ() );
162 aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
165 // synchronize the panel state with the edited object state
172 void HYDROGUI_StreamOp::abortOperation()
175 HYDROGUI_Operation::abortOperation();
178 void HYDROGUI_StreamOp::commitOperation()
181 HYDROGUI_Operation::commitOperation();
184 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
186 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
188 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
189 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
190 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
191 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
192 this, SLOT( onAxisChanged( const QString& ) ) );
194 connect( aPanel, SIGNAL( DDZValueChanged( double ) ), this, SLOT( onDDZValueChanged( double ) ) );
195 connect( aPanel, SIGNAL( SSValueChanged( double ) ), this, SLOT( onSSValueChanged( double ) ) );
199 void HYDROGUI_StreamOp::apply()
201 QApplication::setOverrideCursor( Qt::WaitCursor );
205 int anUpdateFlags = 0;
208 bool aResult = false;
209 QStringList aBrowseObjectsEntries;
213 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
215 catch ( Standard_Failure )
217 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
218 anErrorMsg = aFailure->GetMessageString();
226 QApplication::restoreOverrideCursor();
230 module()->update( anUpdateFlags );
231 commitDocOperation();
233 browseObjects( aBrowseObjectsEntries );
237 myEditedObject->Remove();
238 module()->setObjectRemoved( myEditedObject );
240 if ( isToAbortOnApply() )
244 SUIT_MessageBox::critical( module()->getApp()->desktop(),
245 tr( "CREATE_STREAM_ERROR" ),
251 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
252 QString& theErrorMsg,
253 QStringList& theBrowseObjectsEntries )
255 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
259 // Check whether the object name is not empty
260 QString anObjectName = aPanel->getObjectName().simplified();
261 if ( anObjectName.isEmpty() )
263 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
267 // Check that there are no other objects with the same name in the document
268 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
270 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
271 if( !anObject.IsNull() )
273 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
278 if ( myEditedObject.IsNull() ) // Create new data model object
279 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
281 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
282 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
283 //if ( aHydAxis.IsNull() )
285 // theErrorMsg = tr( "AXIS_NOT_DEFINED" );
289 // Check if at least 2 profiles is set
290 HYDROData_SequenceOfObjects aRefProfiles;
291 for ( int i = 0; i < myProfiles.length(); ++i )
293 QString aProfileName = myProfiles.value( i );
295 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
296 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
297 if ( !aProfile.IsNull() )
298 aRefProfiles.Append( aProfile );
301 if ( aRefProfiles.Length() < 2 )
303 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
309 if (aHydAxis.IsNull())
312 myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
313 myEditedObject->SetName( anObjectName );
315 if (!aHydAxis.IsNull())
316 myEditedObject->SetHydraulicAxis( aHydAxis );
317 myEditedObject->SetProfiles( aRefProfiles, false );
318 myEditedObject->SetDDZ( aPanel->getDDZ() );
319 myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
321 if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
322 myEditedObject->Update();
326 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
327 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
333 // Show the object in case of creation mode of the operation
335 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
336 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
337 theBrowseObjectsEntries.append( anEntry );
340 module()->setIsToUpdate( myEditedObject );
343 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
348 void HYDROGUI_StreamOp::createPreview()
350 LightApp_Application* anApp = module()->getApp();
351 if ( !getPreviewManager() )
353 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
354 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
357 OCCViewer_ViewManager* aViewManager = getPreviewManager();
358 if ( aViewManager && !myPreviewPrs )
360 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
362 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
363 if ( !aCtx.IsNull() )
365 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
367 QColor aFillingColor = Qt::green;
368 QColor aBorderColor = Qt::transparent;
369 if ( !myEditedObject.IsNull() )
371 aFillingColor = myEditedObject->GetFillingColor();
372 aBorderColor = myEditedObject->GetBorderColor();
375 myPreviewPrs->setFillingColor( aFillingColor, false, false );
376 myPreviewPrs->setBorderColor( aBorderColor, false, false );
381 if ( !aViewManager || !myPreviewPrs )
384 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
385 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
387 HYDROData_SequenceOfObjects aRefProfiles;
388 //std::vector<Handle_HYDROData_Profile> aRefProfiles;
389 int plen = myProfiles.length();
390 for ( int i = 0; i < plen; ++i )
392 QString aProfileName = myProfiles.value( i );
394 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
395 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
396 if ( !aProfile.IsNull() )
397 aRefProfiles.Append( aProfile );
400 HYDROData_Stream::PrsDefinition aPrsDef;
402 TopoDS_Shape Out3dPres;
403 TopoDS_Shape Out2dPres;
404 TopoDS_Shape OutLeftB;
405 TopoDS_Shape OutRightB;
406 TopoDS_Shape OutInlet;
407 TopoDS_Shape OutOutlet;
409 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
410 double ddz = aPanel->getDDZ();
411 double ss = aPanel->getSpatialStep();
413 std::set<int> InvInd;
416 const int MAX_POINTS_IN_PREVIEW = 50000;
418 const int MAX_POINTS_IN_PREVIEW = 500000;
421 HYDROData_Bathymetry::AltitudePoints points;
423 bool WireIntersections = false;
424 HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
425 OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW, WireIntersections );
427 aPanel->clearAllBackgroundColorsForProfileList();
428 for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
429 aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
431 aPrsDef.myInlet = TopoDS::Wire(OutInlet);
432 aPrsDef.myOutlet = TopoDS::Wire(OutOutlet);
433 aPrsDef.myLeftBank = TopoDS::Wire(OutLeftB);
434 aPrsDef.myRightBank = TopoDS::Wire(OutRightB);
435 if (!WireIntersections)
436 aPrsDef.myPrs2D = Out2dPres;
437 aPrsDef.myPrs3D = Out3dPres;
439 if (WireIntersections)
440 aPanel->addWarning(tr("STREAM_SELF_INTERSECTIONS"));
442 myPreviewPrs->setShape( aPrsDef.myPrs2D );
445 void HYDROGUI_StreamOp::erasePreview()
454 void HYDROGUI_StreamOp::onAddProfiles()
456 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
457 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
458 //if ( aHydAxis.IsNull() )
462 HYDROData_Stream::BuildRefFace( aPlane );
464 // Get the current profiles list
465 QStringList aCurrentProfiles = myProfiles;
467 // Get the selected profiles ( in the Object Browser )
468 QStringList anInvalidProfiles;
469 QStringList anExistingProfiles;
470 QStringList aHasNoIntersectionProfiles;
471 QStringList aVerifiedProfiles;
473 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
475 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
477 Handle(HYDROData_Profile) aProfile =
478 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
479 if ( aProfile.IsNull() )
482 QString aProfileName = aProfile->GetName();
483 Standard_Real aProfilePar = 0.0;
485 // Check the profile, if all is ok - add it to the list
486 if ( !aProfile->IsValid() )
488 // check whether the profile is valid
489 anInvalidProfiles << aProfileName;
491 else if ( aCurrentProfiles.contains( aProfileName ) )
493 // check whether the profile is already added
494 anExistingProfiles << aProfileName;
496 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
498 // check whether the profile has intersection
499 aHasNoIntersectionProfiles << aProfileName;
503 // Insert profile in correct place
504 // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
505 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
506 aVerifiedProfiles << aProfileName;
510 // Show message box with the ignored profiles
511 if ( !anInvalidProfiles.isEmpty() ||
512 !anExistingProfiles.isEmpty() ||
513 !aHasNoIntersectionProfiles.isEmpty() )
515 QString aMessage = tr( "IGNORED_PROFILES" );
516 if ( !anInvalidProfiles.isEmpty() )
518 aMessage.append( "\n\n" );
519 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
521 if ( !anExistingProfiles.isEmpty() )
523 aMessage.append( "\n\n" );
524 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
526 if ( !aHasNoIntersectionProfiles.isEmpty() )
528 aMessage.append( "\n\n" );
529 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
532 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
535 if ( aVerifiedProfiles.isEmpty() )
545 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
547 QStringList aToRemove = theProfilesToRemove;
549 aToRemove.removeDuplicates();
550 if ( aToRemove.isEmpty() )
553 bool isRemoved = false;
556 for ( int i = 0; i < aToRemove.length(); ++i )
558 const QString& aProfileName = aToRemove.value( i );
560 int aProfileId = myProfiles.indexOf( aProfileName );
561 if ( aProfileId < 0 )
564 myProfiles.removeAt( aProfileId );
565 myProfileParams.removeAt( aProfileId );
579 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
584 void HYDROGUI_StreamOp::onSSValueChanged( double d )
589 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
592 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
593 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
595 // Prepare data for intersection check
597 HYDROData_Stream::BuildRefFace( aPlane );
599 // SUIT_MessageBox::critical( module()->getApp()->desktop(),
600 // tr( "BAD_SELECTED_POLYLINE_TLT" ),
601 // tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
602 // // To restore the old axis
603 // updatePanelData();
607 QStringList aNewProfiles;
608 QList<double> aNewProfileParams;
609 QStringList aHasNoIntersectionProfiles;
611 // Get list of profiles which do not intersect the axis
612 for ( int i = 0; i < myProfiles.length(); ++i )
614 QString aProfileName = myProfiles.value( i );
616 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
617 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
618 if ( aProfile.IsNull() )
621 Standard_Real aProfilePar = 0.0;
622 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
624 // Insert profile in correct place
625 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
629 aHasNoIntersectionProfiles << aProfile->GetName();
633 // If there are profiles which don't intersect the new axis - show confirmation message box
634 bool isConfirmed = true;
635 if ( !aHasNoIntersectionProfiles.isEmpty() )
637 SUIT_MessageBox::StandardButtons aButtons =
638 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
640 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
641 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
642 tr( "CONFIRMATION" ),
643 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
645 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
648 // Check if the user has confirmed axis change
651 // To restore the old axis
657 myHydAxis = theNewAxis;
658 myProfiles = aNewProfiles;
659 myProfileParams = aNewProfileParams;
669 void HYDROGUI_StreamOp::updatePanelData()
671 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
675 aPanel->setAxisName( myHydAxis );
676 aPanel->setProfiles( myProfiles );