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_ProfileInterpolateOp.h>
21 #include "HYDROGUI_Tool.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_DataObject.h"
24 #include "HYDROGUI_UpdateFlags.h"
25 #include "HYDROGUI_ProfileInterpolateDlg.h"
27 #include <HYDROData_Stream.h>
28 #include <HYDROData_Profile.h>
29 #include <HYDROData_Document.h>
30 #include <HYDROData_ProfileUZ.h>
31 #include <HYDROData_IPolyline.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_InterpolatorsFactory.h>
34 #include <HYDROData_IProfilesInterpolator.h>
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewPort3d.h>
42 #include <OCCViewer_ViewWindow.h>
43 #include <OCCViewer_ViewManager.h>
44 #include <OCCViewer_AISSelector.h>
46 #include <AIS_ListOfInteractive.hxx>
47 #include <AIS_ListIteratorOfListOfInteractive.hxx>
49 #include <BRep_Builder.hxx>
50 #include <TopoDS_Compound.hxx>
52 #include <QMessageBox>
54 HYDROGUI_ProfileInterpolateOp::HYDROGUI_ProfileInterpolateOp( HYDROGUI_Module* theModule )
55 : HYDROGUI_Operation( theModule )
57 setName( tr( "PROFILE_INTERPOLATION" ) );
60 HYDROGUI_ProfileInterpolateOp::~HYDROGUI_ProfileInterpolateOp()
64 void HYDROGUI_ProfileInterpolateOp::startOperation()
66 HYDROGUI_Operation::startOperation();
68 if ( isApplyAndClose() )
70 HYDROGUI_ProfileInterpolateDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
74 aPanel->setInterpolators( interpolators() );
79 void HYDROGUI_ProfileInterpolateOp::abortOperation()
81 if ( !myPreview.IsNull() )
83 HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
84 if ( aDlg && !aDlg->getAISContext().IsNull() )
86 aDlg->getAISContext()->Remove( myPreview, false );
91 HYDROGUI_Operation::abortOperation();
94 void HYDROGUI_ProfileInterpolateOp::commitOperation()
96 if ( !myPreview.IsNull() )
98 HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
99 if ( aDlg && !aDlg->getAISContext().IsNull() )
101 aDlg->getAISContext()->Remove( myPreview, false );
106 HYDROGUI_Operation::commitOperation();
109 HYDROGUI_InputPanel* HYDROGUI_ProfileInterpolateOp::createInputPanel() const
111 HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
113 connect( aDlg, SIGNAL( riverChanged( const QString& ) ), this, SLOT( onRiverChanged( const QString& ) ) );
114 connect( aDlg, SIGNAL( interpolatorChanged( const QString& ) ), this, SLOT( onInterpolatorChanged( const QString& ) ) );
115 connect( aDlg, SIGNAL( interpolatorParametersChanged( const QString& ) ), this, SLOT( updatePreview() ) );
116 connect( aDlg, SIGNAL( profileNumberChanged( int ) ), this, SLOT( updatePreview() ) );
117 connect( aDlg, SIGNAL( profileStartChanged( const QString& ) ), this, SLOT( updatePreview() ) );
118 connect( aDlg, SIGNAL( profileFinishChanged( const QString& ) ), this, SLOT( updatePreview() ) );
123 bool HYDROGUI_ProfileInterpolateOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
124 QStringList& theBrowseObjectsEntries )
126 HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
131 HYDROData_IProfilesInterpolator* anIterp = interpolator( aDlg->interpolator() );
134 updateInterpolator( anIterp );
135 Handle(HYDROData_Stream) aRiver = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), aDlg->river(), KIND_STREAM ) );
136 if ( !aRiver.IsNull() )
139 aRiver->Interpolate( anIterp );
140 if ( anIterp->GetErrorCode() == OK )
141 commitDocOperation();
144 errMessage = tr( "CALCULATE_ERROR" ), QString( HYDROGUI_Tool::ToQString( anIterp->GetErrorMessage() ) );
149 errMessage = tr( "CANT_GET_STREAM_OBJECT" ).arg( aDlg->river() );
151 if ( !errMessage.isEmpty() )
152 QMessageBox::critical( aDlg->topLevelWidget(), tr( "INTERPOLATION_ERROR" ), errMessage, QMessageBox::Ok );
157 bool res = errMessage.isEmpty();
160 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
165 void HYDROGUI_ProfileInterpolateOp::updatePreview()
167 HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
171 Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
175 if ( !myPreview.IsNull() )
176 aCtx->Remove( myPreview, false );
180 HYDROData_IProfilesInterpolator* anIterp = interpolator( aDlg->interpolator() );
183 updateInterpolator( anIterp );
184 anIterp->Calculate();
185 if ( anIterp->GetErrorCode() == OK )
187 TopoDS_Shape aShape = previewShape( anIterp );
188 if ( !aShape.IsNull() )
190 myPreview = new AIS_Shape( aShape );
191 myPreview->SetColor( Quantity_NOC_RED );
192 aCtx->Display( myPreview, 1, -1, false );
197 aCtx->UpdateCurrentViewer();
200 void HYDROGUI_ProfileInterpolateOp::updateInterpolator( HYDROData_IProfilesInterpolator* theInt )
202 HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
203 if ( !aDlg || !theInt )
207 theInt->SetResultProfilesNumber( aDlg->profileNumber() );
208 theInt->SetProfiles( profile( aDlg->profileStart() ), profile( aDlg->profileFinish() ) );
209 ParamsList aParams = parameters( aDlg->interpolatorParameters() );
210 for ( ParamsList::Iterator it = aParams.begin(); it != aParams.end(); ++it ) {
211 theInt->SetParameter( HYDROGUI_Tool::ToAsciiString( (*it).first ),
212 HYDROGUI_Tool::ToAsciiString( (*it).second ) );
216 HYDROData_Profile::ProfilePoints HYDROGUI_ProfileInterpolateOp::profile( const QString& theName ) const
218 HYDROData_Profile::ProfilePoints aPoints;
219 Handle(HYDROData_Profile) aProf = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_PROFILE ) );
220 if ( !aProf.IsNull() )
222 aPoints = aProf->GetProfilePoints();
227 HYDROGUI_ProfileInterpolateOp::ParamsList HYDROGUI_ProfileInterpolateOp::parameters( const QString& theStr ) const
229 // Regular expression for parsing parameters string of kind: <param_name> <param_value> <param_name> <param_value> ..."
230 // QRegExp rx( "([A-Za-z_\\d]+)\\s+([A-Za-z_\\d]+)\\s*" );
232 // Regular expression for parsing parameters string of kind: <param_name> = <param_value> <param_name> = <param_value> ..."
233 QRegExp rx( "([A-Za-z_\\d]+)\\s*=\\s*([A-Za-z_\\d]+)\\s*" );
235 ParamsList aParamList;
236 for ( int i = 0; i >= 0; )
238 i = rx.indexIn( theStr, i );
241 aParamList.append( QPair<QString, QString>( rx.cap( 1 ), rx.cap( 2 ) ) );
242 i += rx.matchedLength();
249 QString HYDROGUI_ProfileInterpolateOp::parameters( const ParamsList& theParamList ) const
251 QStringList paramList;
252 for ( ParamsList::const_iterator it = theParamList.begin(); it != theParamList.end(); ++it )
253 paramList.append( QString( "%1 = %2" ).arg( (*it).first ).arg( (*it).second ) );
254 return paramList.join( " " );
257 QStringList HYDROGUI_ProfileInterpolateOp::interpolators() const
259 HYDROData_InterpolatorsFactory* anIFactory = 0;
260 Handle(HYDROData_Document) aDoc = doc();
261 if ( !aDoc.IsNull() )
262 anIFactory = aDoc->GetInterpolatorsFactory();
267 NCollection_Sequence<TCollection_AsciiString> iNames = anIFactory->GetInterpolatorNames();
268 for ( int i = 1, n = iNames.Size(); i <= n; ++i ) {
269 const TCollection_AsciiString& anInterpName = iNames.Value( i );
270 aNames.append( HYDROGUI_Tool::ToQString( anInterpName ) );
277 HYDROData_IProfilesInterpolator* HYDROGUI_ProfileInterpolateOp::interpolator( const QString& theName ) const
279 HYDROData_InterpolatorsFactory* anIFactory = 0;
280 Handle(HYDROData_Document) aDoc = doc();
281 if ( !aDoc.IsNull() )
282 anIFactory = aDoc->GetInterpolatorsFactory();
284 HYDROData_IProfilesInterpolator* aRes = 0;
286 aRes = anIFactory->GetInterpolator( HYDROGUI_Tool::ToAsciiString( theName ) );
290 TopoDS_Shape HYDROGUI_ProfileInterpolateOp::previewShape( HYDROData_IProfilesInterpolator* theInterp ) const
292 TopoDS_Compound aPreviewShape;
295 BRep_Builder aBuilder;
296 aBuilder.MakeCompound( aPreviewShape );
297 for ( int i = 0; i < theInterp->GetCalculatedProfilesNumber(); i++ )
299 NCollection_Sequence<gp_XYZ> pointSeq = theInterp->GetResultProfilePoints( i );
301 TopoDS_Shape aWire = HYDROData_PolylineXY::BuildWire( HYDROData_IPolyline::SECTION_SPLINE, false, pointSeq );
302 if ( !aWire.IsNull() )
303 aBuilder.Add( aPreviewShape, aWire );
307 return aPreviewShape;
310 void HYDROGUI_ProfileInterpolateOp::onInterpolatorChanged( const QString& theInterpName )
312 HYDROGUI_ProfileInterpolateDlg* aPanel = dynamic_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
313 HYDROData_IProfilesInterpolator* anInterp = interpolator( theInterpName );
314 if ( !aPanel || !anInterp )
317 aPanel->setInterpolatorDescription( HYDROGUI_Tool::ToQString( anInterp->GetDescription() ) );
322 void HYDROGUI_ProfileInterpolateOp::onRiverChanged( const QString& theRiver )
324 HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
328 Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
329 if ( !aCtx.IsNull() )
331 AIS_ListOfInteractive aList;
332 aCtx->DisplayedObjects( aList );
333 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
335 Handle(AIS_Shape) anObj = Handle(AIS_Shape)::DownCast( it.Value() );
336 if ( !anObj.IsNull() )
337 aCtx->Remove( anObj, false );
340 Handle(HYDROData_Stream) aStream = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theRiver, KIND_STREAM ) );
341 if ( !aStream.IsNull() )
343 HYDROData_SequenceOfObjects aSeq = aStream->GetProfiles();
344 for ( int i = aSeq.Lower(); i <= aSeq.Upper(); i++ )
346 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( aSeq.Value( i ) );
347 if ( !aProfile.IsNull() )
349 Handle(AIS_Shape) aPrs = new AIS_Shape( aProfile->GetShape3D() );
350 aPrs->SetOwner( aProfile );
351 aPrs->SetColor( Quantity_NOC_BLACK );
352 aCtx->Display( aPrs, 1, 0, false );
355 OCCViewer_ViewManager* vm = aDlg->viewManager();
358 QVector<SUIT_ViewWindow*> winList = vm->getViews();
359 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
361 OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
364 OCCViewer_ViewPort3d* vp = occWin->getViewPort();