]> SALOME platform Git repositories - plugins/netgenplugin.git/blob - src/GUI/NETGENPluginGUI_HypothesisCreator.cxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[plugins/netgenplugin.git] / src / GUI / NETGENPluginGUI_HypothesisCreator.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  NETGENPlugin GUI: GUI for plugged-in mesher NETGENPlugin
23 //  File   : NETGENPluginGUI_HypothesisCreator.cxx
24 //  Author : Michael Zorin
25 //  Module : NETGENPlugin
26 //  $Header: 
27 //
28 #include "NETGENPluginGUI_HypothesisCreator.h"
29
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_HypothesesUtils.h>
32 #include <SMESHGUI.h>
33
34 #include CORBA_SERVER_HEADER(NETGENPlugin_Algorithm)
35
36 #include <SUIT_Session.h>
37 #include <SUIT_ResourceMgr.h>
38
39 #include <SalomeApp_Tools.h>
40
41 #include <SalomeApp_DoubleSpinBox.h>
42
43 #include <QComboBox>
44 #include <QLabel>
45 #include <QGroupBox>
46 #include <QFrame>
47 #include <QLayout>
48 #include <QLineEdit>
49 #include <QCheckBox>
50 #include <QPixmap>
51
52  enum Fineness
53    {
54      VeryCoarse,
55      Coarse,
56      Moderate,
57      Fine,
58      VeryFine,
59      UserDefined
60    };
61
62 NETGENPluginGUI_HypothesisCreator::NETGENPluginGUI_HypothesisCreator( const QString& theHypType )
63 : SMESHGUI_GenericHypothesisCreator( theHypType ),
64   myIs2D(false)
65 {
66 }
67
68 NETGENPluginGUI_HypothesisCreator::~NETGENPluginGUI_HypothesisCreator()
69 {
70 }
71
72 bool NETGENPluginGUI_HypothesisCreator::checkParams(QString& msg) const
73 {
74   NetgenHypothesisData data_old, data_new;
75   readParamsFromHypo( data_old );
76   readParamsFromWidgets( data_new );
77   bool res = storeParamsToHypo( data_new );
78   storeParamsToHypo( data_old );
79   
80   res = myMaxSize->isValid(msg,true) && res;
81   res = myGrowthRate->isValid(msg,true) && res; ;
82   res = myNbSegPerEdge->isValid(msg,true) && res;
83   res = myNbSegPerRadius->isValid(msg,true) && res;
84   return res;
85 }
86
87 QFrame* NETGENPluginGUI_HypothesisCreator::buildFrame()
88 {
89   QFrame* fr = new QFrame( 0 );
90   fr->setObjectName( "myframe" );
91   QVBoxLayout* lay = new QVBoxLayout( fr );
92   lay->setMargin( 5 );
93   lay->setSpacing( 0 );
94
95   QGroupBox* GroupC1 = new QGroupBox( tr( "SMESH_ARGUMENTS" ), fr );
96   lay->addWidget( GroupC1 );
97   
98   QGridLayout* aGroupLayout = new QGridLayout( GroupC1 );
99   aGroupLayout->setSpacing( 6 );
100   aGroupLayout->setMargin( 11 );
101   
102   int row = 0;
103   myName = 0;
104   if( isCreation() )
105   {
106     aGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
107     myName = new QLineEdit( GroupC1 );
108     aGroupLayout->addWidget( myName, row, 1 );
109     row++;
110   }
111
112   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_MAX_SIZE" ), GroupC1 ), row, 0 );
113   myMaxSize = new SalomeApp_DoubleSpinBox( GroupC1 );
114   myMaxSize->setDecimals( 7 );
115   myMaxSize->setMinimum( 1e-07 );
116   myMaxSize->setMaximum( 1e+06 );
117   myMaxSize->setSingleStep( 10 );
118   aGroupLayout->addWidget( myMaxSize, row, 1 );
119   row++;
120   
121   mySecondOrder = new QCheckBox( tr( "NETGEN_SECOND_ORDER" ), GroupC1 );
122   aGroupLayout->addWidget( mySecondOrder, row, 0 );
123   row++;
124   
125   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_FINENESS" ), GroupC1 ), row, 0 );
126   myFineness = new QComboBox( GroupC1 );
127   QStringList types;
128   types << tr( "NETGEN_VERYCOARSE" ) << tr( "NETGEN_COARSE" )   << tr( "NETGEN_MODERATE" ) <<
129            tr( "NETGEN_FINE" )       << tr( "NETGEN_VERYFINE" ) << tr( "NETGEN_CUSTOM" );
130   myFineness->addItems( types );
131   aGroupLayout->addWidget( myFineness, row, 1 );
132   row++;
133
134   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_GROWTH_RATE" ), GroupC1 ), row, 0 );
135   myGrowthRate = new SalomeApp_DoubleSpinBox( GroupC1 );
136   myGrowthRate->setMinimum( 0.1 );
137   myGrowthRate->setMaximum( 10 );
138   myGrowthRate->setSingleStep( 0.1 );
139   aGroupLayout->addWidget( myGrowthRate, row, 1 );
140   row++;
141
142   const double VALUE_MAX = 1.0e+6;
143
144   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_SEG_PER_EDGE" ), GroupC1 ), row, 0 );
145   myNbSegPerEdge = new SalomeApp_DoubleSpinBox( GroupC1 );
146   myNbSegPerEdge->setMinimum( 0.2 );
147   myNbSegPerEdge->setMaximum( VALUE_MAX ); // (PAL14890) max value in native netgen gui is 5
148   aGroupLayout->addWidget( myNbSegPerEdge, row, 1 );
149   row++;
150   
151   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_SEG_PER_RADIUS" ), GroupC1 ), row, 0 );
152   myNbSegPerRadius = new SalomeApp_DoubleSpinBox( GroupC1 );
153   myNbSegPerRadius->setMinimum( 0.2 );
154   myNbSegPerRadius->setMaximum( VALUE_MAX ); // (PAL14890) max value in native netgen gui is 5
155   aGroupLayout->addWidget( myNbSegPerRadius, row, 1 );
156   row++;
157
158   if ( hypType()=="NETGEN_Parameters_2D" )
159   {
160     myAllowQuadrangles = new QCheckBox( tr( "NETGEN_ALLOW_QUADRANGLES" ), GroupC1 );
161     aGroupLayout->addWidget( myAllowQuadrangles, row, 0 );
162     myIs2D = true;
163     row++;
164   }
165
166   myOptimize = new QCheckBox( tr( "NETGEN_OPTIMIZE" ), GroupC1 );
167   aGroupLayout->addWidget( myOptimize, row, 0 );
168   row++;
169   
170   connect( myFineness, SIGNAL( activated( int ) ), this, SLOT( onFinenessChanged() ) );
171   
172   return fr;
173 }
174
175 void NETGENPluginGUI_HypothesisCreator::retrieveParams() const
176 {
177   NetgenHypothesisData data;
178   readParamsFromHypo( data );
179
180   if( myName )
181     myName->setText( data.myName );
182   if(data.myMaxSizeVar.isEmpty())
183     myMaxSize->setValue( data.myMaxSize );
184   else
185     myMaxSize->setText( data.myMaxSizeVar );
186   
187   mySecondOrder->setChecked( data.mySecondOrder );
188   myOptimize->setChecked( data.myOptimize );
189   myFineness->setCurrentIndex( data.myFineness );
190
191   if(data.myGrowthRateVar.isEmpty())
192     myGrowthRate->setValue( data.myGrowthRate );
193   else
194     myGrowthRate->setText( data.myGrowthRateVar );
195
196   if(data.myNbSegPerEdgeVar.isEmpty())
197     myNbSegPerEdge->setValue( data.myNbSegPerEdge );
198   else
199     myNbSegPerEdge->setText( data.myNbSegPerEdgeVar );
200   
201   if(data.myNbSegPerRadiusVar.isEmpty())
202     myNbSegPerRadius->setValue( data.myNbSegPerRadius );
203   else
204     myNbSegPerRadius->setText( data.myNbSegPerRadiusVar );
205   
206   if (myIs2D)
207     myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
208
209   // update widgets
210   bool isCustom = (myFineness->currentIndex() == UserDefined);
211   myGrowthRate->setEnabled(isCustom);
212   myNbSegPerEdge->setEnabled(isCustom);
213   myNbSegPerRadius->setEnabled(isCustom);
214 }
215
216 QString NETGENPluginGUI_HypothesisCreator::storeParams() const
217 {
218   NetgenHypothesisData data;
219   readParamsFromWidgets( data );
220   storeParamsToHypo( data );
221   
222   QString valStr = tr("NETGEN_MAX_SIZE") + " = " + QString::number( data.myMaxSize ) + "; ";
223   if ( data.mySecondOrder )
224     valStr +=  tr("NETGEN_SECOND_ORDER") + "; ";
225   if ( data.myOptimize )
226     valStr +=  tr("NETGEN_OPTIMIZE") + "; ";
227   valStr += myFineness->currentText() + "(" +  QString::number( data.myGrowthRate )     + ", " +
228                                                QString::number( data.myNbSegPerEdge )   + ", " +
229                                                QString::number( data.myNbSegPerRadius ) + ")";
230
231   if ( myIs2D && data.myAllowQuadrangles )
232     valStr += "; " + tr("NETGEN_ALLOW_QUADRANGLES");
233   
234   return valStr;
235 }
236
237 bool NETGENPluginGUI_HypothesisCreator::readParamsFromHypo( NetgenHypothesisData& h_data ) const
238 {
239   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
240     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( initParamsHypothesis() );
241
242   HypothesisData* data = SMESH::GetHypothesisData( hypType() );
243   h_data.myName = isCreation() && data ? data->Label : "";
244
245   SMESH::ListOfParameters_var aParameters = h->GetLastParameters();
246
247   h_data.myMaxSize = h->GetMaxSize();
248   h_data.myMaxSizeVar = (aParameters->length() > 0) ? QString(aParameters[0].in()) : QString("");
249   h_data.mySecondOrder = h->GetSecondOrder();
250   h_data.myOptimize = h->GetOptimize();
251
252   h_data.myFineness = (int) h->GetFineness();
253   h_data.myGrowthRate = h->GetGrowthRate();
254   h_data.myGrowthRateVar = (aParameters->length() > 1) ? QString(aParameters[1].in()) : QString("");
255   h_data.myNbSegPerEdge = h->GetNbSegPerEdge();
256   h_data.myNbSegPerEdgeVar  = (aParameters->length() > 2) ? QString(aParameters[2].in()) : QString("");
257   h_data.myNbSegPerRadius = h->GetNbSegPerRadius();
258   h_data.myNbSegPerRadiusVar = (aParameters->length() > 3) ? QString(aParameters[3].in()) : QString("");
259
260   if ( myIs2D )
261     {
262       NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
263         NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( initParamsHypothesis() );
264
265       if ( !h_2d->_is_nil() )
266         h_data.myAllowQuadrangles = h_2d->GetQuadAllowed();
267     }
268   
269   return true;
270 }
271
272 bool NETGENPluginGUI_HypothesisCreator::storeParamsToHypo( const NetgenHypothesisData& h_data ) const
273 {
274   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
275     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( hypothesis() );
276
277   bool ok = true;
278   try
279   {
280     if( isCreation() )
281       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
282     QStringList aVariablesList;
283     h->SetMaxSize( h_data.myMaxSize );
284     aVariablesList.append(h_data.myMaxSizeVar);
285     h->SetSecondOrder( h_data.mySecondOrder );
286     h->SetOptimize( h_data.myOptimize );
287     int fineness = h_data.myFineness;
288     h->SetFineness( fineness );
289
290     if( fineness==UserDefined )
291       {
292         h->SetGrowthRate( h_data.myGrowthRate );
293         h->SetNbSegPerEdge( h_data.myNbSegPerEdge );
294         h->SetNbSegPerRadius( h_data.myNbSegPerRadius );
295         
296         aVariablesList.append(h_data.myGrowthRateVar);
297         aVariablesList.append(h_data.myNbSegPerEdgeVar);
298         aVariablesList.append(h_data.myNbSegPerRadiusVar);
299       }
300     
301     if ( myIs2D )
302       {
303         NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
304           NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( h );
305         
306         if ( !h_2d->_is_nil() )
307           h_2d->SetQuadAllowed( h_data.myAllowQuadrangles );
308       }
309
310     h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
311     if( fineness==UserDefined )
312       {
313         h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
314         h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
315         h->SetParameters(SMESHGUI::JoinObjectParameters(aVariablesList));
316       }
317         
318   }
319   catch(const SALOME::SALOME_Exception& ex)
320   {
321     SalomeApp_Tools::QtCatchCorbaException(ex);
322     ok = false;
323   }
324   return ok;
325 }
326
327 bool NETGENPluginGUI_HypothesisCreator::readParamsFromWidgets( NetgenHypothesisData& h_data ) const
328 {
329   h_data.myName           = myName ? myName->text() : "";
330   h_data.myMaxSize        = myMaxSize->value();
331   h_data.myMaxSizeVar     = myMaxSize->text();
332   h_data.mySecondOrder    = mySecondOrder->isChecked();
333   h_data.myOptimize       = myOptimize->isChecked();
334   h_data.myFineness       = myFineness->currentIndex();
335   h_data.myGrowthRate     = myGrowthRate->value();
336   h_data.myNbSegPerEdge   = myNbSegPerEdge->value();
337   h_data.myNbSegPerRadius = myNbSegPerRadius->value();
338
339   h_data.myGrowthRateVar     = myGrowthRate->text();
340   h_data.myNbSegPerEdgeVar   = myNbSegPerEdge->text();
341   h_data.myNbSegPerRadiusVar = myNbSegPerRadius->text();
342
343   
344   if ( myIs2D )
345     h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
346   
347   return true;
348 }
349
350 void NETGENPluginGUI_HypothesisCreator::onFinenessChanged()
351 {
352   bool isCustom = (myFineness->currentIndex() == UserDefined);
353   
354   myGrowthRate->setEnabled(isCustom);
355   myNbSegPerEdge->setEnabled(isCustom);
356   myNbSegPerRadius->setEnabled(isCustom);
357
358   if (!isCustom)
359     {
360       double aGrowthRate, aNbSegPerEdge, aNbSegPerRadius;
361       
362       switch ( myFineness->currentIndex() )
363         {
364         case VeryCoarse:
365           aGrowthRate = 0.7;
366           aNbSegPerEdge = 0.3;
367           aNbSegPerRadius = 1;
368           break;
369         case Coarse:
370           aGrowthRate = 0.5;
371           aNbSegPerEdge = 0.5;
372           aNbSegPerRadius = 1.5;
373           break;
374         case Fine:
375           aGrowthRate = 0.2;
376           aNbSegPerEdge = 2;
377           aNbSegPerRadius = 3;
378           break;
379         case VeryFine:
380           aGrowthRate = 0.1;
381           aNbSegPerEdge = 3;
382           aNbSegPerRadius = 5;
383           break;
384         case Moderate:
385         default:
386           aGrowthRate = 0.3;
387           aNbSegPerEdge = 1;
388           aNbSegPerRadius = 2;
389           break;
390         }
391       
392       myGrowthRate->setValue( aGrowthRate );
393       myNbSegPerEdge->setValue( aNbSegPerEdge );
394       myNbSegPerRadius->setValue( aNbSegPerRadius );
395     }
396 }
397
398 QString NETGENPluginGUI_HypothesisCreator::caption() const
399 {
400   return tr( QString( "NETGEN_%1_TITLE" ).arg(myIs2D?QString("2D"):QString("3D")).toLatin1().data() );
401 }
402
403 QPixmap NETGENPluginGUI_HypothesisCreator::icon() const
404 {
405   QString hypIconName = tr( QString("ICON_DLG_NETGEN_PARAMETERS%1").arg(myIs2D?QString("_2D"):QString("")).toLatin1().data() );
406   return SUIT_Session::session()->resourceMgr()->loadPixmap( "NETGENPlugin", hypIconName );
407 }
408
409 QString NETGENPluginGUI_HypothesisCreator::type() const
410 {
411   return tr( QString( "NETGEN_%1_HYPOTHESIS" ).arg(myIs2D?QString("2D"):QString("3D")).toLatin1().data() );
412 }
413
414 QString NETGENPluginGUI_HypothesisCreator::helpPage() const
415 {
416   return "netgen_2d_3d_hypo_page.html";
417 }