2 #include "StdMeshersGUI_DistrPreview.h"
3 #include <Expr_NamedUnknown.hxx>
4 #include <Expr_GeneralExpression.hxx>
5 #include <CASCatch_CatchSignals.hxx>
6 #include <CASCatch_Failure.hxx>
7 #include <CASCatch_ErrorHandler.hxx>
10 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
16 myConv( CUT_NEGATIVE ),
20 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
21 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
22 myCurve1 = insertCurve( QString() );
23 myCurve2 = insertCurve( QString() );
24 myMsg = insertMarker( new QwtPlotMarker( this ) );
25 setMarkerPos( myMsg, 0.5, 0.5 );
26 setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
27 QFont f = markerFont( myMsg );
30 setMarkerFont( myMsg, f );
31 setCurvePen( myCurve1, QPen( Qt::red, 1 ) );
34 setCurvePen( myCurve2, QPen( dc, 1 ) );
35 setCurveSymbol( myCurve2, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
38 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
42 bool StdMeshersGUI_DistrPreview::isTableFunc() const
47 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
52 QString StdMeshersGUI_DistrPreview::function() const
57 int StdMeshersGUI_DistrPreview::nbSeg() const
62 int StdMeshersGUI_DistrPreview::pointsCount() const
67 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
74 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
77 myTableFunc = SMESH::double_array();
78 myFunction = func.isEmpty() ? "0" : func;
79 myPoints = points>0 ? points : 2;
80 myNbSeg = nbSeg>0 ? nbSeg : 1;
81 bool res = init( func );
87 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
91 if( myTableFunc.length()%2==1 )
92 myTableFunc.length( myTableFunc.length()-1 );
95 myPoints = myTableFunc.length()/2;
96 myNbSeg = nbSeg>0 ? nbSeg : 1;
101 return myTableFunc.length()>0;
104 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
106 if( myExpr.IsNull() )
112 const double xmin = 0.0, xmax = 1.0;
114 double d = (xmax-xmin)/double(myPoints-1);
115 func.length( 2*myPoints );
117 for( int i=0, j=0; i<myPoints; j++ )
120 double t = xmin + d*j, f = funcValue( t, ok );
130 func.length( func.length()-2*err );
134 void StdMeshersGUI_DistrPreview::update()
136 SMESH::double_array graph, distr;
143 myIsDone = createTable( graph );
145 if( graph.length()>=2 )
147 StdMeshers::StdMeshers_NumberOfSegments_var h =
148 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
150 if( !CORBA::is_nil( h.in() ) )
152 SMESH::double_array* arr = 0;
154 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
156 arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
165 bool correct = graph.length()>=2 && distr.length()>=2;
172 setMarkerLabel( myMsg, QString() );
174 int size = graph.length()/2;
175 double* x = new double[size], *y = new double[size];
176 double min_x, max_x, min_y, max_y;
177 for( int i=0; i<size; i++ )
181 if( !convert( y[i] ) )
183 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
184 delete[] x; delete[] y;
189 if( i==0 || y[i]<min_y )
191 if( i==0 || y[i]>max_y )
193 if( i==0 || x[i]<min_x )
195 if( i==0 || x[i]>max_x )
199 setAxisScale( curveXAxis( myCurve1 ), min_x, max_x );
200 setAxisScale( curveYAxis( myCurve1 ), min( 0.0, min_y ), max( 0.0, max_y ) );
201 setCurveData( myCurve1, x, y, size );
208 size = distr.length();
209 x = new double[size];
210 y = new double[size];
211 for( int i=0; i<size; i++ )
216 setCurveData( myCurve2, x, y, size );
223 void StdMeshersGUI_DistrPreview::showError()
225 setAxisScale( curveXAxis( myCurve1 ), 0.0, 1.0 );
226 setAxisScale( curveYAxis( myCurve1 ), 0.0, 1.0 );
227 setCurveData( myCurve1, 0, 0, 0 );
228 setCurveData( myCurve2, 0, 0, 0 );
229 setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
233 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
235 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
237 return sub->GetName()=="t";
240 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
242 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
243 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
246 if( name->GetName()!="t" )
250 res = isCorrectArg( sub );
255 bool StdMeshersGUI_DistrPreview::init( const QString& str )
257 CASCatch_CatchSignals aCatchSignals;
258 aCatchSignals.Activate();
260 bool parsed_ok = true;
263 myExpr = ExprIntrp_GenExp::Create();
264 myExpr->Process( ( Standard_CString ) str.latin1() );
266 CASCatch_CATCH(CASCatch_Failure)
268 aCatchSignals.Deactivate();
269 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
272 aCatchSignals.Deactivate();
274 bool syntax = false, args = false;
275 if( parsed_ok && myExpr->IsDone() )
278 args = isCorrectArg( myExpr->Expression() );
281 bool res = parsed_ok && syntax && args;
287 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
289 if( myExpr.IsNull() )
292 myValues.ChangeValue( 1 ) = t;
295 double res = calc( ok );
300 double StdMeshersGUI_DistrPreview::calc( bool& ok )
302 OSD::SetSignal( true );
305 CASCatch_CatchSignals aCatchSignals;
306 aCatchSignals.Activate();
310 res = myExpr->Expression()->Evaluate( myVars, myValues );
312 CASCatch_CATCH(CASCatch_Failure) {
313 aCatchSignals.Deactivate();
314 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
318 aCatchSignals.Deactivate();
322 bool StdMeshersGUI_DistrPreview::isDone() const
327 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
329 CASCatch_CatchSignals aCatchSignals;
330 aCatchSignals.Activate();
341 CASCatch_CATCH(CASCatch_Failure)
343 aCatchSignals.Deactivate();
344 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
356 aCatchSignals.Deactivate();