Salome HOME
Merge V9_dev branch into master
[plugins/netgenplugin.git] / src / GUI / NETGENPluginGUI_HypothesisCreator.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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
23 //  NETGENPlugin GUI: GUI for plugged-in mesher NETGENPlugin
24 //  File   : NETGENPluginGUI_HypothesisCreator.cxx
25 //  Author : Michael Zorin
26 //  Module : NETGENPlugin
27 //
28 #include "NETGENPluginGUI_HypothesisCreator.h"
29
30 #include <SMESHGUI_Utils.h>
31 #include <SMESHGUI_HypothesesUtils.h>
32 #include <SMESHGUI_SpinBox.h>
33 #include <GeomSelectionTools.h>
34
35 #include CORBA_SERVER_HEADER(NETGENPlugin_Algorithm)
36
37 #include <SUIT_FileDlg.h>
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_Session.h>
40
41 #include <SalomeApp_Tools.h>
42 #include <LightApp_SelectionMgr.h>
43 #include <SALOME_ListIO.hxx>
44
45 #include <QComboBox>
46 #include <QLabel>
47 #include <QGroupBox>
48 #include <QFrame>
49 #include <QLayout>
50 #include <QLineEdit>
51 #include <QCheckBox>
52 #include <QPixmap>
53 #include <QTableWidget>
54 #include <QHeaderView>
55 #include <QPushButton>
56
57 enum Fineness
58   {
59     VeryCoarse,
60     Coarse,
61     Moderate,
62     Fine,
63     VeryFine,
64     UserDefined
65   };
66
67 enum {
68   STD_TAB = 0,
69   LSZ_TAB
70 };
71
72 enum {
73   LSZ_ENTRY_COLUMN = 0,
74   LSZ_NAME_COLUMN,
75   LSZ_LOCALSIZE_COLUMN,
76   LSZ_NB_COLUMNS
77 };
78
79 enum {
80   LSZ_BTNS = 0,
81   LSZ_VERTEX_BTN,
82   LSZ_EDGE_BTN,
83   LSZ_FACE_BTN,
84   LSZ_SOLID_BTN,
85   LSZ_SEPARATOR2,
86   LSZ_REMOVE_BTN,
87   LSZ_FILE_LE = 9
88 };
89
90 NETGENPluginGUI_HypothesisCreator::NETGENPluginGUI_HypothesisCreator( const QString& theHypType )
91   : SMESHGUI_GenericHypothesisCreator( theHypType )
92 {
93   myGeomSelectionTools = NULL;
94   myLocalSizeMap.clear();
95   myIs2D   = ( theHypType.startsWith("NETGEN_Parameters_2D") ||
96                theHypType == "NETGEN_RemesherParameters_2D");
97   myIsONLY = ( theHypType == "NETGEN_Parameters_2D_ONLY" ||
98                theHypType == "NETGEN_Parameters_3D" ||
99                theHypType == "NETGEN_RemesherParameters_2D");
100 }
101
102 NETGENPluginGUI_HypothesisCreator::~NETGENPluginGUI_HypothesisCreator()
103 {
104 }
105
106 bool NETGENPluginGUI_HypothesisCreator::checkParams(QString& msg) const
107 {
108   NetgenHypothesisData data_old, data_new;
109   readParamsFromHypo( data_old );
110   readParamsFromWidgets( data_new );
111   bool res = storeParamsToHypo( data_new );
112   //storeParamsToHypo( data_old ); -- issue 0021364: Dump of netgen parameters has duplicate lines
113   
114   res = myMaxSize->isValid(msg,true) && res;
115   res = myMinSize->isValid(msg,true) && res;
116   res = myGrowthRate->isValid(msg,true) && res; ;
117   if ( myNbSegPerEdge )
118     res = myNbSegPerEdge->isValid(msg,true) && res;
119   if ( myNbSegPerRadius )
120     res = myNbSegPerRadius->isValid(msg,true) && res;
121   if ( myRidgeAngle )
122     res = myRidgeAngle->isValid(msg,true) && res;
123
124   if ( !res ) //  -- issue 0021364: Dump of netgen parameters has duplicate lines
125     storeParamsToHypo( data_old );
126
127   return res;
128 }
129
130 QFrame* NETGENPluginGUI_HypothesisCreator::buildFrame()
131 {
132   const bool isRemesher = ( hypType() == "NETGEN_RemesherParameters_2D" );
133
134   QFrame* fr = new QFrame( 0 );
135   fr->setObjectName( "myframe" );
136   QVBoxLayout* lay = new QVBoxLayout( fr );
137   lay->setMargin( 5 );
138   lay->setSpacing( 0 );
139
140   QTabWidget* tab = new QTabWidget( fr );
141   tab->setTabShape( QTabWidget::Rounded );
142   tab->setTabPosition( QTabWidget::North );
143   lay->addWidget( tab );
144   QWidget* GroupC1 = new QWidget();
145   tab->insertTab( STD_TAB, GroupC1, tr( "SMESH_ARGUMENTS" ) );
146   
147   QGridLayout* aGroupLayout = new QGridLayout( GroupC1 );
148   aGroupLayout->setSpacing( 6 );
149   aGroupLayout->setMargin( 11 );
150   
151   int row = 0;
152   myName = 0;
153   if( isCreation() )
154   {
155     aGroupLayout->addWidget( new QLabel( tr( "SMESH_NAME" ), GroupC1 ), row, 0 );
156     myName = new QLineEdit( GroupC1 );
157     myName->setMinimumWidth(160);
158     aGroupLayout->addWidget( myName, row, 1 );
159     row++;
160   }
161
162   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_MAX_SIZE" ), GroupC1 ), row, 0 );
163   myMaxSize = new SMESHGUI_SpinBox( GroupC1 );
164   myMaxSize->RangeStepAndValidator( 1e-07, 1e+06, 10., "length_precision" );
165   aGroupLayout->addWidget( myMaxSize, row, 1 );
166   row++;
167
168   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_MIN_SIZE" ), GroupC1 ), row, 0 );
169   myMinSize = new SMESHGUI_SpinBox( GroupC1 );
170   myMinSize->RangeStepAndValidator( 0.0, 1e+06, 10., "length_precision" );
171   aGroupLayout->addWidget( myMinSize, row, 1 );
172   row++;
173
174   mySecondOrder = 0;
175   if ( !myIsONLY )
176   {
177     mySecondOrder = new QCheckBox( tr( "NETGEN_SECOND_ORDER" ), GroupC1 );
178     aGroupLayout->addWidget( mySecondOrder, row, 0, 1, 2 );
179     row++;
180   }
181
182   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_FINENESS" ), GroupC1 ), row, 0 );
183   myFineness = new QComboBox( GroupC1 );
184   QStringList types;
185   types << tr( "NETGEN_VERYCOARSE" ) << tr( "NETGEN_COARSE" )   << tr( "NETGEN_MODERATE" ) <<
186     tr( "NETGEN_FINE" )       << tr( "NETGEN_VERYFINE" ) << tr( "NETGEN_CUSTOM" );
187   myFineness->addItems( types );
188   aGroupLayout->addWidget( myFineness, row, 1 );
189   connect( myFineness, SIGNAL( activated( int ) ), this, SLOT( onFinenessChanged() ) );
190   row++;
191
192   aGroupLayout->addWidget( new QLabel( tr( "NETGEN_GROWTH_RATE" ), GroupC1 ), row, 0 );
193   myGrowthRate = new SMESHGUI_SpinBox( GroupC1 );
194   myGrowthRate->RangeStepAndValidator( .0001, 10., .1, "parametric_precision" );
195   aGroupLayout->addWidget( myGrowthRate, row, 1 );
196   row++;
197
198   myNbSegPerEdge = 0;
199   myNbSegPerRadius = 0;
200   if ( !myIsONLY )
201   {
202     const double VALUE_MAX = 1.0e+6;
203
204     aGroupLayout->addWidget( new QLabel( tr( "NETGEN_SEG_PER_EDGE" ), GroupC1 ), row, 0 );
205     myNbSegPerEdge = new SMESHGUI_SpinBox( GroupC1 );
206     myNbSegPerEdge->RangeStepAndValidator( .2, VALUE_MAX, .1, "parametric_precision" );
207     aGroupLayout->addWidget( myNbSegPerEdge, row, 1 );
208     row++;
209
210     aGroupLayout->addWidget( new QLabel( tr( "NETGEN_SEG_PER_RADIUS" ), GroupC1 ), row, 0 );
211     myNbSegPerRadius = new SMESHGUI_SpinBox( GroupC1 );
212     myNbSegPerRadius->RangeStepAndValidator( .2, VALUE_MAX, .1, "parametric_precision" );
213     aGroupLayout->addWidget( myNbSegPerRadius, row, 1 );
214     row++;
215   }
216
217   myChordalErrorEnabled = 0;
218   myChordalError = 0;
219   if (( myIs2D && !isRemesher ) || !myIsONLY )
220   {
221     myChordalErrorEnabled = new QCheckBox( tr( "NETGEN_CHORDAL_ERROR" ), GroupC1 );
222     aGroupLayout->addWidget( myChordalErrorEnabled, row, 0 );
223     myChordalError = new SMESHGUI_SpinBox( GroupC1 );
224     myChordalError->RangeStepAndValidator( COORD_MIN, COORD_MAX, .1, "length_precision" );
225     aGroupLayout->addWidget( myChordalError, row, 1 );
226     connect( myChordalErrorEnabled, SIGNAL( stateChanged(int)), SLOT( onChordalErrorEnabled()));
227     row++;
228   }
229
230   myRidgeAngle = 0;
231   if ( isRemesher )
232   {
233     aGroupLayout->addWidget( new QLabel( tr( "NETGEN_RIDGE_ANGLE" ), GroupC1 ), row, 0 );
234     myRidgeAngle = new SMESHGUI_SpinBox( GroupC1 );
235     myRidgeAngle->RangeStepAndValidator( 0, 90, 10, "angle_precision" );
236     aGroupLayout->addWidget( myRidgeAngle, row, 1 );
237     row++;
238   }
239
240   mySurfaceCurvature = 0;
241   if (( myIs2D && !isRemesher ) || !myIsONLY )
242   {
243     mySurfaceCurvature = new QCheckBox( tr( "NETGEN_SURFACE_CURVATURE" ), GroupC1 );
244     aGroupLayout->addWidget( mySurfaceCurvature, row, 0, 1, 2 );
245     connect( mySurfaceCurvature, SIGNAL( stateChanged( int ) ), this, SLOT( onSurfaceCurvatureChanged() ) );
246     row++;
247   }
248
249   myAllowQuadrangles = 0;
250   if ( myIs2D || !myIsONLY ) // disable only for NETGEN 3D
251   {
252     myAllowQuadrangles = new QCheckBox( tr( "NETGEN_ALLOW_QUADRANGLES" ), GroupC1 );
253     aGroupLayout->addWidget( myAllowQuadrangles, row, 0, 1, 2 );
254     row++;
255   }
256
257   myOptimize = 0;
258   if ( !isRemesher )
259   {
260     myOptimize = new QCheckBox( tr( "NETGEN_OPTIMIZE" ), GroupC1 );
261     aGroupLayout->addWidget( myOptimize, row, 0, 1, 2 );
262     row++;
263   }
264
265   myFuseEdges = 0;
266   if ( !myIsONLY )
267   {
268     myFuseEdges = new QCheckBox( tr( "NETGEN_FUSE_EDGES" ), GroupC1 );
269     aGroupLayout->addWidget( myFuseEdges, row, 0, 1, 2 );
270     row++;
271   }
272
273   myLocalSizeTable = 0;
274   //if ( !myIsONLY )
275   {
276     QWidget* localSizeGroup = new QWidget();
277     QGridLayout* localSizeLayout = new QGridLayout(localSizeGroup);
278
279     myLocalSizeTable = new QTableWidget(0, LSZ_NB_COLUMNS, localSizeGroup);
280     localSizeLayout->addWidget(myLocalSizeTable, 1, 0, 8, 2);
281     QStringList localSizeHeaders;
282     localSizeHeaders << tr( "LSZ_ENTRY_COLUMN" )<< tr( "LSZ_NAME_COLUMN" ) << tr( "LSZ_LOCALSIZE_COLUMN" );
283     myLocalSizeTable->setHorizontalHeaderLabels(localSizeHeaders);
284     myLocalSizeTable->horizontalHeader()->hideSection(LSZ_ENTRY_COLUMN);
285 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
286     myLocalSizeTable->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
287 #else
288     myLocalSizeTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
289 #endif
290     myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
291     myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
292     myLocalSizeTable->setAlternatingRowColors(true);
293     myLocalSizeTable->verticalHeader()->hide();
294
295     QPushButton* addVertexButton = new QPushButton(tr("NETGEN_LSZ_VERTEX"), localSizeGroup);
296     localSizeLayout->addWidget(addVertexButton, LSZ_VERTEX_BTN, 2, 1, 1);
297     QPushButton* addEdgeButton = new QPushButton(tr("NETGEN_LSZ_EDGE"), localSizeGroup);
298     localSizeLayout->addWidget(addEdgeButton, LSZ_EDGE_BTN, 2, 1, 1);
299     QPushButton* addFaceButton = new QPushButton(tr("NETGEN_LSZ_FACE"), localSizeGroup);
300     localSizeLayout->addWidget(addFaceButton, LSZ_FACE_BTN, 2, 1, 1);
301     QPushButton* addSolidButton = new QPushButton(tr("NETGEN_LSZ_SOLID"), localSizeGroup);
302     localSizeLayout->addWidget(addSolidButton, LSZ_SOLID_BTN, 2, 1, 1);
303
304     QFrame *line2 = new QFrame(localSizeGroup);
305     line2->setFrameShape(QFrame::HLine);
306     line2->setFrameShadow(QFrame::Sunken);
307     localSizeLayout->addWidget(line2, LSZ_SEPARATOR2, 2, 1, 1);
308
309     QPushButton* removeButton = new QPushButton(tr("NETGEN_LSZ_REMOVE"), localSizeGroup);
310     localSizeLayout->addWidget(removeButton, LSZ_REMOVE_BTN, 2, 1, 1);
311
312     QPushButton* fileBtn = new QPushButton(tr("NETGEN_LSZ_FILE"), localSizeGroup);
313     myMeshSizeFile = new QLineEdit(localSizeGroup);
314     myMeshSizeFile->setReadOnly( true );
315     localSizeLayout->addWidget( fileBtn, LSZ_FILE_LE, 0, 1, 1);
316     localSizeLayout->addWidget( myMeshSizeFile, LSZ_FILE_LE, 1, 1, 2);
317
318     connect( addVertexButton, SIGNAL(clicked()), this, SLOT(onAddLocalSizeOnVertex()));
319     connect( addEdgeButton, SIGNAL(clicked()), this, SLOT(onAddLocalSizeOnEdge()));
320     connect( addFaceButton, SIGNAL(clicked()), this, SLOT(onAddLocalSizeOnFace()));
321     connect( addSolidButton, SIGNAL(clicked()), this, SLOT(onAddLocalSizeOnSolid()));
322     connect( removeButton, SIGNAL(clicked()), this, SLOT(onRemoveLocalSizeOnShape()));
323     connect( myLocalSizeTable, SIGNAL(cellChanged(int, int)), this, SLOT(onSetLocalSize(int, int)));
324     connect( fileBtn, SIGNAL(clicked()), this, SLOT(onSetSizeFile()));
325
326     tab->insertTab(LSZ_TAB, localSizeGroup, tr("NETGEN_LOCAL_SIZE"));
327   }
328   return fr;
329 }
330
331 void NETGENPluginGUI_HypothesisCreator::retrieveParams() const
332 {
333   NetgenHypothesisData data;
334   readParamsFromHypo( data );
335
336   if( myName )
337     myName->setText( data.myName );
338   if(data.myMaxSizeVar.isEmpty())
339     myMaxSize->setValue( data.myMaxSize );
340   else
341     myMaxSize->setText( data.myMaxSizeVar );
342
343   if(data.myMinSizeVar.isEmpty())
344     myMinSize->setValue( data.myMinSize );
345   else
346     myMinSize->setText( data.myMinSizeVar );
347
348   if ( mySecondOrder )
349     mySecondOrder->setChecked( data.mySecondOrder );
350   if ( myOptimize )
351     myOptimize->setChecked( data.myOptimize );
352   myFineness->setCurrentIndex( data.myFineness );
353
354   if(data.myGrowthRateVar.isEmpty())
355     myGrowthRate->setValue( data.myGrowthRate );
356   else
357     myGrowthRate->setText( data.myGrowthRateVar );
358
359   if ( myNbSegPerEdge )
360   {
361     if(data.myNbSegPerEdgeVar.isEmpty())
362       myNbSegPerEdge->setValue( data.myNbSegPerEdge );
363     else
364       myNbSegPerEdge->setText( data.myNbSegPerEdgeVar );
365   }
366   if ( myNbSegPerRadius )
367   {
368     if(data.myNbSegPerRadiusVar.isEmpty())
369       myNbSegPerRadius->setValue( data.myNbSegPerRadius );
370     else
371       myNbSegPerRadius->setText( data.myNbSegPerRadiusVar );
372   }
373   if ( myChordalError )
374   {
375     myChordalErrorEnabled->setChecked( data.myChordalErrorEnabled && data.myChordalError > 0 );
376     if(data.myChordalErrorVar.isEmpty())
377       myChordalError->setValue( data.myChordalError > 0 ? data.myChordalError : 0.1 );
378     else
379       myChordalError->setText( data.myChordalErrorVar );
380     myChordalError->setEnabled( myChordalErrorEnabled->isChecked() );
381   }
382   if ( myRidgeAngle )
383   {
384     if ( data.myRidgeAngleVar.isEmpty() )
385       myRidgeAngle->setValue( data.myRidgeAngle );
386     else
387       myRidgeAngle->setText( data.myRidgeAngleVar );
388   }
389   if (myAllowQuadrangles)
390     myAllowQuadrangles->setChecked( data.myAllowQuadrangles );
391
392   if (mySurfaceCurvature)
393     mySurfaceCurvature->setChecked( data.mySurfaceCurvature );
394
395   if (myFuseEdges)
396     myFuseEdges->setChecked( data.myFuseEdges );
397
398   // update widgets
399   ((NETGENPluginGUI_HypothesisCreator*) this )-> onSurfaceCurvatureChanged();
400
401   if ( myLocalSizeTable )
402   {
403     NETGENPluginGUI_HypothesisCreator* that = (NETGENPluginGUI_HypothesisCreator*)this;
404     QMapIterator<QString, QString> i(myLocalSizeMap);
405     GeomSelectionTools* geomSelectionTools = that->getGeomSelectionTools();
406     while (i.hasNext()) {
407       i.next();
408       const QString entry = i.key();
409       std::string shapeName = geomSelectionTools->getNameFromEntry(entry.toStdString());
410       const QString localSize = i.value();
411       int row = myLocalSizeTable->rowCount();
412       myLocalSizeTable->setRowCount(row+1);
413       myLocalSizeTable->setItem(row, LSZ_ENTRY_COLUMN, new QTableWidgetItem(entry));
414       myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN)->setFlags(0);
415       myLocalSizeTable->setItem(row, LSZ_NAME_COLUMN, new QTableWidgetItem(QString::fromStdString(shapeName)));
416       myLocalSizeTable->item(row, LSZ_NAME_COLUMN)->setFlags(0);
417       myLocalSizeTable->setItem(row, LSZ_LOCALSIZE_COLUMN, new QTableWidgetItem(localSize));
418       myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
419     }
420     myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
421     myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
422
423     myMeshSizeFile->setText( data.myMeshSizeFile );
424   }
425 }
426
427 QString NETGENPluginGUI_HypothesisCreator::storeParams() const
428 {
429   NetgenHypothesisData data;
430   readParamsFromWidgets( data );
431   storeParamsToHypo( data );
432
433   return QString();
434 }
435
436 bool NETGENPluginGUI_HypothesisCreator::readParamsFromHypo( NetgenHypothesisData& h_data ) const
437 {
438   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
439     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( initParamsHypothesis() );
440
441   h_data.myName = isCreation() ? hypName() : "";
442
443   h_data.myMaxSize     = h->GetMaxSize();
444   h_data.myMaxSizeVar  = getVariableName("SetMaxSize");
445   h_data.mySecondOrder = h->GetSecondOrder();
446   h_data.myOptimize    = h->GetOptimize();
447
448   h_data.myFineness            = (int) h->GetFineness();
449   h_data.myGrowthRate          = h->GetGrowthRate();
450   h_data.myGrowthRateVar       = getVariableName("SetGrowthRate");
451   h_data.myNbSegPerEdge        = h->GetNbSegPerEdge();
452   h_data.myNbSegPerEdgeVar     = getVariableName("SetNbSegPerEdge");
453   h_data.myNbSegPerRadius      = h->GetNbSegPerRadius();
454   h_data.myNbSegPerRadiusVar   = getVariableName("SetNbSegPerRadius");
455   h_data.myChordalError        = h->GetChordalError();
456   h_data.myChordalErrorVar     = getVariableName("SetChordalError");
457   h_data.myChordalErrorEnabled = h->GetChordalErrorEnabled();
458   h_data.myMinSize             = h->GetMinSize();
459   h_data.myMinSizeVar          = getVariableName("SetMinSize");
460   h_data.mySurfaceCurvature    = h->GetUseSurfaceCurvature();
461   h_data.myFuseEdges           = h->GetFuseEdges();
462   h_data.myMeshSizeFile        = h->GetMeshSizeFile();
463
464   //if ( myIs2D )
465   {
466     // NETGENPlugin::NETGENPlugin_Hypothesis_var h =
467     //   NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( initParamsHypothesis() );
468
469     if ( !h->_is_nil() )
470       h_data.myAllowQuadrangles = h->GetQuadAllowed();
471   }
472   if ( myIs2D )
473   {
474     NETGENPlugin::NETGENPlugin_RemesherHypothesis_2D_var rh =
475       NETGENPlugin::NETGENPlugin_RemesherHypothesis_2D::_narrow( h );
476
477     if ( !rh->_is_nil() )
478       h_data.myRidgeAngle = rh->GetRidgeAngle();
479   }
480
481   NETGENPluginGUI_HypothesisCreator*  that = (NETGENPluginGUI_HypothesisCreator*)this;
482   NETGENPlugin::string_array_var myEntries = h->GetLocalSizeEntries();
483   for ( size_t i = 0; i < myEntries->length(); i++ )
484   {
485     QString entry = myEntries[i].in();
486     if (myLocalSizeMap.contains(entry) &&
487         myLocalSizeMap[entry] == "__TO_DELETE__")
488       continue;
489     double val = h->GetLocalSizeOnEntry( myEntries[i] );
490     std::ostringstream tmp;
491     tmp << val;
492     that->myLocalSizeMap[entry] = tmp.str().c_str();
493   }
494
495   return true;
496 }
497
498 bool NETGENPluginGUI_HypothesisCreator::storeParamsToHypo( const NetgenHypothesisData& h_data ) const
499 {
500   NETGENPlugin::NETGENPlugin_Hypothesis_var h =
501     NETGENPlugin::NETGENPlugin_Hypothesis::_narrow( hypothesis() );
502
503   bool ok = true;
504   try
505   {
506     if( isCreation() )
507       SMESH::SetName( SMESH::FindSObject( h ), h_data.myName.toLatin1().data() );
508     h->SetVarParameter( h_data.myMaxSizeVar.toLatin1().constData(), "SetMaxSize");
509     h->SetMaxSize     ( h_data.myMaxSize );
510     if ( mySecondOrder )
511       h->SetSecondOrder ( h_data.mySecondOrder );
512     if ( myOptimize )
513       h->SetOptimize    ( h_data.myOptimize );
514     int fineness = h_data.myFineness;
515     h->SetFineness    ( fineness );
516
517     if( fineness==UserDefined )
518     {
519       h->SetVarParameter  ( h_data.myGrowthRateVar.toLatin1().constData(), "SetGrowthRate");
520       h->SetGrowthRate    ( h_data.myGrowthRate );
521       if ( myNbSegPerEdge )
522       {
523         h->SetVarParameter  ( h_data.myNbSegPerEdgeVar.toLatin1().constData(), "SetNbSegPerEdge");
524         h->SetNbSegPerEdge  ( h_data.myNbSegPerEdge );
525       }
526       if ( myNbSegPerRadius )
527       {
528         h->SetVarParameter  ( h_data.myNbSegPerRadiusVar.toLatin1().constData(), "SetNbSegPerRadius");
529         h->SetNbSegPerRadius( h_data.myNbSegPerRadius );
530       }
531     }
532     if ( myChordalError )
533     {
534       h->SetVarParameter       ( h_data.myChordalErrorVar.toLatin1().constData(), "SetChordalError");
535       h->SetChordalError       ( h_data.myChordalError );
536       h->SetChordalErrorEnabled( h_data.myChordalErrorEnabled );
537     }
538     h->SetVarParameter       ( h_data.myMinSizeVar.toLatin1().constData(), "SetMinSize");
539     h->SetMinSize            ( h_data.myMinSize );
540     if ( mySurfaceCurvature )
541       h->SetUseSurfaceCurvature( h_data.mySurfaceCurvature );
542     if ( myFuseEdges )
543       h->SetFuseEdges          ( h_data.myFuseEdges );
544     h->SetMeshSizeFile       ( h_data.myMeshSizeFile.toUtf8().constData() );
545
546     //if ( myIs2D )
547     {
548       // NETGENPlugin::NETGENPlugin_Hypothesis_2D_var h_2d =
549       //   NETGENPlugin::NETGENPlugin_Hypothesis_2D::_narrow( h );
550       // if ( !h_2d->_is_nil() )
551       //   h_2d->SetQuadAllowed( h_data.myAllowQuadrangles );
552       if ( myAllowQuadrangles )
553         h->SetQuadAllowed( h_data.myAllowQuadrangles );
554     }
555     if ( myIs2D )
556     {
557       NETGENPlugin::NETGENPlugin_RemesherHypothesis_2D_var rh =
558         NETGENPlugin::NETGENPlugin_RemesherHypothesis_2D::_narrow( h );
559       if ( !rh->_is_nil() )
560       {
561         rh->SetVarParameter( h_data.myRidgeAngleVar.toLatin1().constData(), "SetRidgeAngle");
562         rh->SetRidgeAngle  ( h_data.myRidgeAngle );
563       }
564     }
565     for ( QMapIterator<QString,QString> i(myLocalSizeMap); i.hasNext(); )
566     {
567       i.next();
568       const QString&     entry = i.key();
569       const QString& localSize = i.value();
570       if (localSize == "__TO_DELETE__")
571       {
572         h->UnsetLocalSizeOnEntry(entry.toLatin1().constData());
573       }
574       else
575       {
576         h->SetLocalSizeOnEntry(entry.toLatin1().constData(), localSize.toDouble());
577       }
578     }
579   }
580   catch(const SALOME::SALOME_Exception& ex)
581   {
582     SalomeApp_Tools::QtCatchCorbaException(ex);
583     ok = false;
584   }
585   return ok;
586 }
587
588 bool NETGENPluginGUI_HypothesisCreator::readParamsFromWidgets( NetgenHypothesisData& h_data ) const
589 {
590   h_data.myName           = myName ? myName->text() : "";
591   h_data.myMaxSize        = myMaxSize->value();
592   h_data.myMaxSizeVar     = myMaxSize->text();
593   h_data.myMinSize        = myMinSize->value();
594   h_data.myMinSizeVar     = myMinSize->text();
595   if ( mySecondOrder )
596     h_data.mySecondOrder  = mySecondOrder->isChecked();
597   if ( myOptimize )
598     h_data.myOptimize     = myOptimize->isChecked();
599   h_data.myFineness       = myFineness->currentIndex();
600   h_data.myGrowthRate     = myGrowthRate->value();
601   if ( myNbSegPerEdge )
602     h_data.myNbSegPerEdge = myNbSegPerEdge->value();
603   if ( myNbSegPerRadius )
604     h_data.myNbSegPerRadius = myNbSegPerRadius->value();
605
606   h_data.myGrowthRateVar  = myGrowthRate->text();
607   if ( myNbSegPerEdge )
608     h_data.myNbSegPerEdgeVar = myNbSegPerEdge->text();
609   if ( myNbSegPerRadius )
610     h_data.myNbSegPerRadiusVar = myNbSegPerRadius->text();
611   if ( myChordalError )
612   {
613     h_data.myChordalErrorVar = myChordalError->text();
614     h_data.myChordalError    = myChordalError->value();
615     h_data.myChordalErrorEnabled = myChordalError->isEnabled();
616   }
617   if ( myRidgeAngle )
618   {
619     h_data.myRidgeAngleVar = myRidgeAngle->text();
620     h_data.myRidgeAngle    = myRidgeAngle->value();
621   }
622
623   if ( myAllowQuadrangles )
624     h_data.myAllowQuadrangles = myAllowQuadrangles->isChecked();
625
626   if ( mySurfaceCurvature )
627     h_data.mySurfaceCurvature = mySurfaceCurvature->isChecked();
628
629   if ( myFuseEdges )
630     h_data.myFuseEdges = myFuseEdges->isChecked();
631
632   if ( myLocalSizeTable )
633   {
634     NETGENPluginGUI_HypothesisCreator* that = (NETGENPluginGUI_HypothesisCreator*)this;
635     int nbRows = myLocalSizeTable->rowCount();
636     for(int row=0 ; row < nbRows ; row++)
637     {
638       QString entry = myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN)->text();
639       QString localSize = myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN)->text().trimmed();
640       that->myLocalSizeMap[entry] = localSize;
641     }
642     h_data.myMeshSizeFile = myMeshSizeFile->text();
643   }
644   return true;
645 }
646
647 void NETGENPluginGUI_HypothesisCreator::onChordalErrorEnabled()
648 {
649   myChordalError->setEnabled( myChordalErrorEnabled->isChecked() );
650 }
651
652 void NETGENPluginGUI_HypothesisCreator::onSurfaceCurvatureChanged()
653 {
654   bool isSurfaceCurvature = (mySurfaceCurvature ? mySurfaceCurvature->isChecked() : true);
655   bool isCustom           = (myFineness->currentIndex() == UserDefined);
656   //myFineness->setEnabled(isSurfaceCurvature);
657   myGrowthRate->setEnabled(isCustom);
658   if ( myNbSegPerEdge )
659     myNbSegPerEdge->setEnabled(isCustom && isSurfaceCurvature);
660   if ( myNbSegPerRadius )
661     myNbSegPerRadius->setEnabled(isCustom && isSurfaceCurvature);
662   // if ( myChordalError )
663   // {
664   //   myChordalError->setEnabled( isSurfaceCurvature );
665   //   myChordalErrorEnabled->setEnabled( isSurfaceCurvature );
666   // }
667 }
668
669 void NETGENPluginGUI_HypothesisCreator::onFinenessChanged()
670 {
671   bool isCustom = (myFineness->currentIndex() == UserDefined);
672
673   myGrowthRate->setEnabled(isCustom);
674   if ( myNbSegPerEdge )
675     myNbSegPerEdge->setEnabled(isCustom);
676   if ( myNbSegPerRadius )
677     myNbSegPerRadius->setEnabled(isCustom);
678
679   if (!isCustom)
680   {
681     double aGrowthRate, aNbSegPerEdge, aNbSegPerRadius;
682
683     switch ( myFineness->currentIndex() )
684     {
685     case VeryCoarse:
686       aGrowthRate     = 0.7;
687       aNbSegPerEdge   = 0.3;
688       aNbSegPerRadius = 1;
689       break;
690     case Coarse:
691       aGrowthRate     = 0.5;
692       aNbSegPerEdge   = 0.5;
693       aNbSegPerRadius = 1.5;
694       break;
695     case Fine:
696       aGrowthRate     = 0.2;
697       aNbSegPerEdge   = 2;
698       aNbSegPerRadius = 3;
699       break;
700     case VeryFine:
701       aGrowthRate     = 0.1;
702       aNbSegPerEdge   = 3;
703       aNbSegPerRadius = 5;
704       break;
705     case Moderate:
706     default:
707       aGrowthRate     = 0.3;
708       aNbSegPerEdge   = 1;
709       aNbSegPerRadius = 2;
710       break;
711     }
712
713     myGrowthRate->setValue( aGrowthRate );
714     if ( myNbSegPerEdge )
715       myNbSegPerEdge->setValue( aNbSegPerEdge );
716     if ( myNbSegPerRadius )
717       myNbSegPerRadius->setValue( aNbSegPerRadius );
718   }
719 }
720
721 void NETGENPluginGUI_HypothesisCreator::onAddLocalSizeOnVertex()
722 {
723   addLocalSizeOnShape(TopAbs_VERTEX);
724 }
725
726 void NETGENPluginGUI_HypothesisCreator::onAddLocalSizeOnEdge()
727 {
728   addLocalSizeOnShape(TopAbs_EDGE);
729 }
730
731 void NETGENPluginGUI_HypothesisCreator::onAddLocalSizeOnFace()
732 {
733   addLocalSizeOnShape(TopAbs_FACE);
734 }
735
736 void NETGENPluginGUI_HypothesisCreator::onAddLocalSizeOnSolid()
737 {
738   addLocalSizeOnShape(TopAbs_SOLID);
739 }
740
741 void NETGENPluginGUI_HypothesisCreator::addLocalSizeOnShape(TopAbs_ShapeEnum typeShapeAsked)
742 {
743   NETGENPlugin::NETGENPlugin_Hypothesis_var h = NETGENPlugin::NETGENPlugin_Hypothesis::_narrow(initParamsHypothesis());
744   GeomSelectionTools* geomSelectionTools = getGeomSelectionTools();
745   LightApp_SelectionMgr* mySel = geomSelectionTools->selectionMgr();
746   SALOME_ListIO ListSelectedObjects;
747   mySel->selectedObjects(ListSelectedObjects, NULL, false );
748   SALOME_ListIteratorOfListIO Object_It(ListSelectedObjects);
749   for ( ; Object_It.More() ; Object_It.Next())
750   {
751     Handle(SALOME_InteractiveObject) anObject = Object_It.Value();
752     std::string          entry = geomSelectionTools->getEntryOfObject(anObject);
753     std::string      shapeName = anObject->getName();
754     TopAbs_ShapeEnum shapeType = geomSelectionTools->entryToShapeType(entry);
755     if (shapeType == TopAbs_SHAPE)
756     {
757       // E.A. if shapeType == TopAbs_SHAPE, it is NOT a TopoDS_Shape !!!
758       continue;
759     }
760     // --
761     if(shapeType != typeShapeAsked)
762     {
763       continue;
764     }
765     // --
766     myLocalSizeTable->setFocus();
767     QString shapeEntry = QString::fromStdString(entry);
768     if (myLocalSizeMap.contains(shapeEntry) &&
769         myLocalSizeMap[shapeEntry] != "__TO_DELETE__")
770       continue;
771
772     double phySize = h->GetMaxSize();
773     std::ostringstream oss;
774     oss << phySize;
775     QString localSize;
776     localSize  = QString::fromStdString(oss.str());
777     // --
778     int row = myLocalSizeTable->rowCount() ;
779     myLocalSizeTable->setRowCount(row+1);
780     myLocalSizeTable->setItem(row, LSZ_ENTRY_COLUMN, new QTableWidgetItem(shapeEntry));
781     myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN )->setFlags(0);
782     myLocalSizeTable->setItem(row, LSZ_NAME_COLUMN, new QTableWidgetItem(QString::fromStdString(shapeName)));
783     myLocalSizeTable->item(row, LSZ_NAME_COLUMN )->setFlags(0);
784     myLocalSizeTable->setItem(row, LSZ_LOCALSIZE_COLUMN, new QTableWidgetItem(localSize));
785     myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN )->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
786     myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
787     myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
788     myLocalSizeTable->clearSelection();
789     myLocalSizeTable->scrollToItem( myLocalSizeTable->item( row, LSZ_LOCALSIZE_COLUMN ) );
790     // --
791   }
792 }
793
794 void NETGENPluginGUI_HypothesisCreator::onRemoveLocalSizeOnShape()
795 {
796   QList<int> selectedRows;
797   QList<QTableWidgetItem*> selected = myLocalSizeTable->selectedItems();
798   QTableWidgetItem* item;
799   int row;
800   foreach(item, selected) {
801     row = item->row();
802     if (!selectedRows.contains(row))
803       selectedRows.append( row );
804   }
805   qSort( selectedRows );
806   QListIterator<int> it( selectedRows );
807   it.toBack();
808   while (it.hasPrevious())
809   {
810     row = it.previous();
811     QString entry = myLocalSizeTable->item(row,LSZ_ENTRY_COLUMN)->text();
812     if (myLocalSizeMap.contains(entry))
813     {
814       myLocalSizeMap[entry] = "__TO_DELETE__";
815     }
816     myLocalSizeTable->removeRow(row );
817   }
818   myLocalSizeTable->resizeColumnToContents(LSZ_NAME_COLUMN);
819   myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
820 }
821
822 void NETGENPluginGUI_HypothesisCreator::onSetLocalSize(int row,int col)
823 {
824   if (col == LSZ_LOCALSIZE_COLUMN) {
825     QString     entry = myLocalSizeTable->item(row, LSZ_ENTRY_COLUMN)->text();
826     QString localSize = myLocalSizeTable->item(row, LSZ_LOCALSIZE_COLUMN)->text().trimmed();
827     myLocalSizeMap[entry] = localSize;
828     myLocalSizeTable->resizeColumnToContents(LSZ_LOCALSIZE_COLUMN);
829   }
830 }
831
832 void NETGENPluginGUI_HypothesisCreator::onSetSizeFile()
833 {
834   QString dir = SUIT_FileDlg::getFileName( dlg(), QString(),
835                                            QStringList() << tr( "ALL_FILES_FILTER" ) + "  (*)");
836   myMeshSizeFile->setText( dir );
837 }
838
839 GeomSelectionTools* NETGENPluginGUI_HypothesisCreator::getGeomSelectionTools()
840 {
841   if (myGeomSelectionTools == NULL) {
842     delete myGeomSelectionTools;
843     myGeomSelectionTools = new GeomSelectionTools();
844   }
845   return myGeomSelectionTools;
846 }
847
848 QString NETGENPluginGUI_HypothesisCreator::caption() const
849 {
850   return tr( myIs2D ? "NETGEN_2D_TITLE" : "NETGEN_3D_TITLE");
851 }
852
853 QPixmap NETGENPluginGUI_HypothesisCreator::icon() const
854 {
855   QString hypIconName = tr( myIs2D ?
856                             "ICON_DLG_NETGEN_PARAMETERS_2D" :
857                             "ICON_DLG_NETGEN_PARAMETERS");
858   return SUIT_Session::session()->resourceMgr()->loadPixmap( "NETGENPlugin", hypIconName );
859 }
860
861 QString NETGENPluginGUI_HypothesisCreator::type() const
862 {
863   return tr( myIs2D ? "NETGEN_2D_HYPOTHESIS" : "NETGEN_3D_HYPOTHESIS");
864 }
865
866 QString NETGENPluginGUI_HypothesisCreator::helpPage() const
867 {
868   return "netgen_2d_3d_hypo_page.html";
869 }