1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_StreamOp.h"
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Shape.h"
27 #include "HYDROGUI_StreamDlg.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
31 #include <HYDROData_Document.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Profile.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
39 #include <SUIT_MessageBox.h>
40 #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>
55 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
56 : HYDROGUI_Operation( theModule ),
57 myIsEdit( theIsEdit ),
60 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
63 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
68 void HYDROGUI_StreamOp::startOperation()
70 HYDROGUI_Operation::startOperation();
72 // We start operation in the document
75 // Get panel and reset its state
76 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
79 // Get/create the edited object
81 myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82 if ( !myEditedObject.IsNull() && myEditedObject->IsMustBeUpdated() ) {
83 myEditedObject->Update();
86 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
89 // Get the edited object name
92 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
93 } else if ( !myEditedObject.IsNull() ) {
94 anObjectName = myEditedObject->GetName();
98 // set the edited object name
99 aPanel->setObjectName( anObjectName );
100 // set the existing 2D polylines names to the panel
101 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
102 // synchronize the panel state with the edited object state
109 void HYDROGUI_StreamOp::abortOperation()
114 HYDROGUI_Operation::abortOperation();
117 void HYDROGUI_StreamOp::commitOperation()
121 HYDROGUI_Operation::commitOperation();
124 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
126 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
128 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
129 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
130 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
131 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
132 this, SLOT( onAxisChanged( const QString& ) ) );
137 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
138 QString& theErrorMsg )
140 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
141 if ( !aPanel || myEditedObject.IsNull() ) {
145 // Check whether the object name is not empty
146 QString anObjectName = aPanel->getObjectName().simplified();
147 if ( anObjectName.isEmpty() ) {
148 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
152 // Check that there are no other objects with the same name in the document
153 if( myEditedObject->GetName() != anObjectName )
155 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
156 if( !anObject.IsNull() ) {
157 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
162 // Check if the axis is set
163 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
164 if ( aHydraulicAxis.IsNull() ) {
165 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
169 // Check if at least 2 profiles is set
170 HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
171 if ( aProfiles.Length() < 2 ) {
172 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
176 // Set the object name
177 myEditedObject->SetName( anObjectName );
181 myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
182 myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
188 // Show the object in case of creation mode of the operation
190 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
193 module()->setIsToUpdate( myEditedObject );
196 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
201 void HYDROGUI_StreamOp::createPreview()
203 if ( myEditedObject.IsNull() ) {
207 LightApp_Application* anApp = module()->getApp();
208 if ( !getPreviewManager() )
209 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
210 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
212 OCCViewer_ViewManager* aViewManager = getPreviewManager();
213 if ( aViewManager && !myPreviewPrs ) {
214 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
215 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
216 if ( !aCtx.IsNull() ) {
217 myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject, getPreviewZLayer() );
222 if ( myPreviewPrs ) {
223 myPreviewPrs->update( true );
227 void HYDROGUI_StreamOp::erasePreview()
235 void HYDROGUI_StreamOp::onAddProfiles()
237 if ( myEditedObject.IsNull() ) {
241 // Get the current profiles list
242 HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
244 // TODO: to be optimized
245 QStringList aCurrentProfiles;
246 for( int i = 1, n = aProfiles.Length(); i <= n; i++ ) {
247 Handle(HYDROData_Profile) aProfile =
248 Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
249 if ( !aProfile.IsNull() ) {
250 aCurrentProfiles << aProfile->GetName();
254 // Get the selected profiles ( in the Object Browser )
255 QStringList anInvalidProfiles, anExistingProfiles, aHasNoIntersectionProfiles;
257 HYDROData_SequenceOfObjects aVerifiedProfiles;
258 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
259 Handle(HYDROData_PolylineXY) aHydAxis = myEditedObject->GetHydraulicAxis();
260 if ( aHydAxis.IsNull() )
263 if(!myEditedObject->BuildFace(aHydAxis, aPlane))
265 Standard_Real aPar(.0);
266 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ ) {
267 Handle(HYDROData_Profile) aProfile =
268 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
269 if ( !aProfile.IsNull() ) {
270 QString aProfileName = aProfile->GetName();
272 // Check the profile, if all is ok - add it to the list
273 if ( !aProfile->IsValid() ) { // check whether the profile is valid
274 anInvalidProfiles << aProfileName;
275 } else if ( aCurrentProfiles.contains( aProfileName ) ) { // check whether the profile is already added
276 anExistingProfiles << aProfileName;
277 } else if ( !myEditedObject->HasIntersection( aProfile, aPlane, aPar ) ) { // check whether the profile has intersection
278 aHasNoIntersectionProfiles << aProfileName;
280 aVerifiedProfiles.Append( aProfile );
285 // Show message box with the ignored profiles
286 if ( !anInvalidProfiles.isEmpty() || !anExistingProfiles.isEmpty() ||
287 !aHasNoIntersectionProfiles.isEmpty() ) {
288 QString aMessage = tr( "IGNORED_PROFILES" );
289 if ( !anInvalidProfiles.isEmpty() ) {
290 aMessage.append( "\n\n" );
291 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
293 if ( !anExistingProfiles.isEmpty() ) {
294 aMessage.append( "\n\n" );
295 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
297 if ( !aHasNoIntersectionProfiles.isEmpty() ) {
298 aMessage.append( "\n\n" );
299 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
302 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
305 // Update the stream object
306 for( int i = 1, n = aVerifiedProfiles.Length(); i <= n; i++ ) {
307 Handle(HYDROData_Profile) aProfile =
308 Handle(HYDROData_Profile)::DownCast( aVerifiedProfiles.Value( i ) );
309 myEditedObject->AddProfile( aProfile );
311 myEditedObject->UpdatePrs();
320 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
322 if ( myEditedObject.IsNull() ) {
326 bool isRemoved = false;
328 // Take the Object Browser selection into account
329 HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() );
330 for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ ) {
331 Handle(HYDROData_Profile) aProfile =
332 Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
333 if ( !aProfile.IsNull() && !theProfilesToRemove.contains(aProfile->GetName()) ) {
334 if ( myEditedObject->RemoveProfile( aProfile ) ) {
341 foreach( const QString& aProfileName, theProfilesToRemove ) {
342 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
343 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
344 if ( myEditedObject->RemoveProfile( aProfile ) ) {
350 // Update the edited stream object
351 myEditedObject->UpdatePrs();
361 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
363 if ( myEditedObject.IsNull() ) {
368 Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
369 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
371 // Prepare data for intersection check
373 if ( !myEditedObject->BuildFace(anAxis, aPlane) ) {
374 SUIT_MessageBox::critical( module()->getApp()->desktop(),
375 tr( "BAD_SELECTED_POLYLINE_TLT" ),
376 tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
377 // To restore the old axis
382 Standard_Real aPar(.0);
384 // Get list of profiles which do not intersect the axis
385 QStringList aHasNoIntersectionProfiles;
386 HYDROData_SequenceOfObjects aCurrentProfiles = myEditedObject->GetProfiles();
387 for( int anIndex = 1, aLength = aCurrentProfiles.Length(); anIndex <= aLength; anIndex++ ) {
388 Handle(HYDROData_Profile) aProfile =
389 Handle(HYDROData_Profile)::DownCast( aCurrentProfiles.Value( anIndex ) );
390 if ( !aProfile.IsNull() ) {
391 if ( !HYDROData_Stream::HasIntersection( anAxis, aProfile, aPlane, aPar ) ) {
392 aHasNoIntersectionProfiles << aProfile->GetName();
397 // If there are profiles which don't intersect the new axis - show confirmation message box
398 bool isConfirmed = true;
399 if ( !aHasNoIntersectionProfiles.isEmpty() ) {
400 SUIT_MessageBox::StandardButtons aButtons =
401 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
403 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
404 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
405 tr( "CONFIRMATION" ),
406 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
408 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
411 // Check if the user has confirmed axis change
412 if ( !isConfirmed ) {
413 // To restore the old axis
417 myEditedObject->SetHydraulicAxis( anAxis );
418 myEditedObject->UpdatePrs();
428 void HYDROGUI_StreamOp::updatePanelData()
430 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
431 if ( !aPanel || myEditedObject.IsNull() ) {
436 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
437 if ( !aHydraulicAxis.IsNull() ) {
438 aPanel->setAxisName( aHydraulicAxis->GetName() );
440 aPanel->setAxisName( "" );
444 QStringList aProfiles;
446 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
447 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i ) {
448 Handle(HYDROData_Profile) aProfile =
449 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
450 if ( !aProfile.IsNull() ) {
451 QString aProfileName = aProfile->GetName();
452 aProfiles << aProfileName;
456 aPanel->setProfiles( aProfiles );