Salome HOME
Merge from V5_1_main 10/06/2010
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
1 //  Copyright (C) 2007-2010  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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_AddMeshElementDlg.cxx
25 // Author : Nicolas REJNERI, Open CASCADE S.A.S.
26 //  SMESH includes
27 //
28 #include "SMESHGUI_AddMeshElementDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_GroupUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMESH_ActorUtils.h>
39 #include <SMESH_FaceOrientationFilter.h>
40 #include <SMDS_Mesh.hxx>
41
42 // SALOME GUI inclues
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_ResourceMgr.h>
46 #include <SUIT_MessageBox.h>
47 #include <SUIT_ViewManager.h>
48 #include <LightApp_SelectionMgr.h>
49 #include <SALOME_ListIO.hxx>
50 #include <SalomeApp_Application.h>
51 #include <SVTK_ViewModel.h>
52 #include <SVTK_ViewWindow.h>
53
54 // IDL incldues
55 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
56
57 // OCCT includes
58 #include <TColStd_MapOfInteger.hxx>
59
60 // VTK includes
61 #include <vtkCell.h>
62 #include <vtkIdList.h>
63 #include <vtkUnstructuredGrid.h>
64 #include <vtkDataSetMapper.h>
65 #include <vtkPolyDataMapper.h>
66 #include <vtkProperty.h>
67
68 // Qt includes
69 #include <QComboBox>
70 #include <QGroupBox>
71 #include <QLabel>
72 #include <QLineEdit>
73 #include <QPushButton>
74 #include <QRadioButton>
75 #include <QHBoxLayout>
76 #include <QVBoxLayout>
77 #include <QGridLayout>
78 #include <QVariant>
79 #include <QCheckBox>
80 #include <QKeyEvent>
81 #include <QButtonGroup>
82
83 #define SPACING 6
84 #define MARGIN  11
85
86 namespace SMESH
87 {
88   class TElementSimulation
89   {
90     SalomeApp_Application* myApplication;
91     SUIT_ViewWindow* myViewWindow;
92     SVTK_ViewWindow* myVTKViewWindow;
93
94     SALOME_Actor* myPreviewActor;
95     vtkDataSetMapper* myMapper;
96     vtkUnstructuredGrid* myGrid;
97
98     SALOME_Actor* myFaceOrientation;
99     vtkPolyDataMapper* myFaceOrientationDataMapper;
100     SMESH_FaceOrientationFilter* myFaceOrientationFilter;
101
102   public:
103     TElementSimulation (SalomeApp_Application* theApplication)
104     {
105       myApplication = theApplication;
106       SUIT_ViewManager* mgr = theApplication->activeViewManager();
107       if (!mgr) return;
108       myViewWindow = mgr->getActiveView();
109       myVTKViewWindow = GetVtkViewWindow(myViewWindow);
110
111       myGrid = vtkUnstructuredGrid::New();
112
113       // Create and display actor
114       myMapper = vtkDataSetMapper::New();
115       myMapper->SetInput(myGrid);
116
117       myPreviewActor = SALOME_Actor::New();
118       myPreviewActor->PickableOff();
119       myPreviewActor->VisibilityOff();
120       myPreviewActor->SetMapper(myMapper);
121
122       vtkFloatingPointType anRGB[3];
123       vtkProperty* aProp = vtkProperty::New();
124       GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
125       aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
126       myPreviewActor->SetProperty( aProp );
127       aProp->Delete();
128
129       vtkProperty* aBackProp = vtkProperty::New();
130       GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
131       aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
132       myPreviewActor->SetBackfaceProperty( aBackProp );
133       aBackProp->Delete();
134
135       myVTKViewWindow->AddActor(myPreviewActor);
136
137       // Orientation of faces
138       myFaceOrientationFilter = SMESH_FaceOrientationFilter::New();
139       myFaceOrientationFilter->SetInput(myGrid);
140
141       myFaceOrientationDataMapper = vtkPolyDataMapper::New();
142       myFaceOrientationDataMapper->SetInput(myFaceOrientationFilter->GetOutput());
143
144       myFaceOrientation = SALOME_Actor::New();
145       myFaceOrientation->PickableOff();
146       myFaceOrientation->VisibilityOff();
147       myFaceOrientation->SetMapper(myFaceOrientationDataMapper);
148
149       vtkProperty* anOrientationProp = vtkProperty::New();
150       GetColor( "SMESH", "orientation_color", anRGB[0], anRGB[1], anRGB[2], QColor( 255, 255, 255 ) );
151       anOrientationProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
152       myFaceOrientation->SetProperty( anOrientationProp );
153       anOrientationProp->Delete();
154
155       myVTKViewWindow->AddActor(myFaceOrientation);
156     }
157
158     typedef std::vector<vtkIdType> TVTKIds;
159     void SetPosition (SMESH_Actor* theActor,
160                       vtkIdType theType,
161                       const TVTKIds& theIds)
162     {
163       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
164       myGrid->SetPoints(aGrid->GetPoints());
165
166       const int* aConn = NULL;
167       switch (theType) {
168       case VTK_TETRA:
169         {
170           static int anIds[] = {0,2,1,3};
171           aConn = anIds;
172           break;
173         }
174       case VTK_PYRAMID:
175         {
176           static int anIds[] = {0,3,2,1,4};
177           aConn = anIds;
178           break;
179         }
180       case VTK_HEXAHEDRON:
181         {
182           static int anIds[] = {0,3,2,1,4,7,6,5};
183           aConn = anIds;
184           break;
185         }
186       }
187
188       myGrid->Reset();
189       vtkIdList *anIds = vtkIdList::New();
190
191       if(aConn)
192         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
193           anIds->InsertId(i,theIds[aConn[i]]);
194       else
195         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
196           anIds->InsertId(i,theIds[i]);
197
198       myGrid->InsertNextCell(theType,anIds);
199       anIds->Delete();
200
201       myGrid->Modified();
202
203       SetVisibility(true, theActor->GetFacesOriented());
204     }
205
206
207     void SetVisibility (bool theVisibility, bool theShowOrientation = false)
208     {
209       myPreviewActor->SetVisibility(theVisibility);
210       myFaceOrientation->SetVisibility(theShowOrientation);
211       RepaintCurrentView();
212     }
213
214
215     ~TElementSimulation()
216     {
217       if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
218         myVTKViewWindow->RemoveActor(myPreviewActor);
219         myVTKViewWindow->RemoveActor(myFaceOrientation);
220       }
221       myPreviewActor->Delete();
222       myFaceOrientation->Delete();
223
224       myMapper->RemoveAllInputs();
225       myMapper->Delete();
226
227       myFaceOrientationFilter->Delete();
228
229       myFaceOrientationDataMapper->RemoveAllInputs();
230       myFaceOrientationDataMapper->Delete();
231
232       myGrid->Delete();
233     }
234   };
235 }
236
237 //=================================================================================
238 // function : SMESHGUI_AddMeshElementDlg()
239 // purpose  : constructor
240 //=================================================================================
241 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
242                                                         SMDSAbs_ElementType ElementType,
243                                                         int nbNodes )
244   : QDialog( SMESH::GetDesktop( theModule ) ),
245     mySMESHGUI( theModule ),
246     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
247 {
248   setModal( false );
249   setAttribute( Qt::WA_DeleteOnClose, true );
250
251   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
252     (SUIT_Session::session()->activeApplication());
253   myIsPoly = false;
254   mySimulation = new SMESH::TElementSimulation (anApp);
255   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
256
257   // verify nb nodes and type
258   myNbNodes = nbNodes;
259   myElementType = ElementType;
260   switch (ElementType) {
261   case SMDSAbs_0DElement:
262     if (myNbNodes != 1)
263       myNbNodes = 1;
264     break;
265   case SMDSAbs_Face:
266     //     if (myNbNodes != 3 && myNbNodes != 4)
267     //       myNbNodes = 3;
268     //     break;
269   case SMDSAbs_Volume:
270     //     if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
271     //       myNbNodes = 4;
272     break;
273   default:
274     myElementType = SMDSAbs_Edge;
275     myNbNodes = 2;
276   }
277
278   QString elemName;
279   if (myNbNodes == 1) {
280     elemName = "ELEM0D";
281     myHelpFileName = "adding_nodes_and_elements_page.html#adding_0delems_anchor";
282   }
283   else if (myNbNodes == 2) {
284     elemName = "EDGE";
285     myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
286   }
287   else if (myNbNodes == 3) {
288     elemName = "TRIANGLE";
289     myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
290   }
291   else if (myNbNodes == 4) {
292     if (myElementType == SMDSAbs_Face) {
293       elemName = "QUADRANGLE";
294       myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
295     }
296     else {
297       elemName = "TETRAS";
298       myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
299     }
300   }
301   else if (myNbNodes == 8) {
302     elemName = "HEXAS";
303     myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
304   }
305   else if (myElementType == SMDSAbs_Face) {
306     elemName = "POLYGON";
307     myIsPoly = true;
308     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
309   }
310   else if (myElementType == SMDSAbs_Volume) {
311     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
312   }
313
314   QString iconName      = tr(QString("ICON_DLG_%1").arg(elemName).toLatin1().data());
315   QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName).toLatin1().data());
316   QString caption       = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName).toLatin1().data());
317   QString grBoxTitle    = tr(QString("SMESH_ADD_%1").arg(elemName).toLatin1().data());
318
319   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
320   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
321
322   setWindowTitle(caption);
323   setSizeGripEnabled(true);
324
325   QVBoxLayout* aTopLayout = new QVBoxLayout(this);
326   aTopLayout->setSpacing(SPACING);
327   aTopLayout->setMargin(MARGIN);
328
329   /***************************************************************/
330   GroupConstructors = new QGroupBox(buttonGrTitle, this);
331   QButtonGroup* ButtonGroup = new QButtonGroup(this);
332   QHBoxLayout* GroupConstructorsLayout = new QHBoxLayout(GroupConstructors);
333   GroupConstructorsLayout->setSpacing(SPACING);
334   GroupConstructorsLayout->setMargin(MARGIN);
335
336   Constructor1 = new QRadioButton(GroupConstructors);
337   Constructor1->setIcon(image0);
338   Constructor1->setChecked(true);
339
340   GroupConstructorsLayout->addWidget(Constructor1);
341   ButtonGroup->addButton( Constructor1, 0 );
342
343   /***************************************************************/
344   GroupC1 = new QGroupBox(grBoxTitle, this);
345   QGridLayout* GroupC1Layout = new QGridLayout(GroupC1);
346   GroupC1Layout->setSpacing(SPACING);
347   GroupC1Layout->setMargin(MARGIN);
348
349   TextLabelC1A1 = new QLabel(tr("SMESH_ID_NODES"), GroupC1);
350   SelectButtonC1A1 = new QPushButton(GroupC1);
351   SelectButtonC1A1->setIcon(image1);
352   LineEditC1A1 = new QLineEdit(GroupC1);
353   //  LineEditC1A1->setReadOnly(true);
354   if (!myIsPoly)
355     LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, myNbNodes));
356
357   Reverse = myElementType == SMDSAbs_Face ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
358
359   GroupC1Layout->addWidget(TextLabelC1A1,    0, 0);
360   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
361   GroupC1Layout->addWidget(LineEditC1A1,     0, 2);
362   if ( Reverse ) GroupC1Layout->addWidget(Reverse, 1, 0, 1, 3);
363
364   /***************************************************************/
365   GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
366   GroupGroups->setCheckable( true );
367   QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
368   GroupGroupsLayout->setSpacing(SPACING);
369   GroupGroupsLayout->setMargin(MARGIN);
370
371   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
372   ComboBox_GroupName = new QComboBox( GroupGroups );
373   ComboBox_GroupName->setEditable( true );
374
375   GroupGroupsLayout->addWidget( TextLabel_GroupName );
376   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
377
378   /***************************************************************/
379   GroupButtons = new QGroupBox(this);
380   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
381   GroupButtonsLayout->setSpacing(SPACING);
382   GroupButtonsLayout->setMargin(MARGIN);
383
384   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
385   buttonOk->setAutoDefault(true);
386   buttonOk->setDefault(true);
387   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
388   buttonApply->setAutoDefault(true);
389   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
390   buttonCancel->setAutoDefault(true);
391   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
392   buttonHelp->setAutoDefault(true);
393
394   GroupButtonsLayout->addWidget(buttonOk);
395   GroupButtonsLayout->addSpacing(10);
396   GroupButtonsLayout->addWidget(buttonApply);
397   GroupButtonsLayout->addSpacing(10);
398   GroupButtonsLayout->addStretch();
399   GroupButtonsLayout->addWidget(buttonCancel);
400   GroupButtonsLayout->addWidget(buttonHelp);
401
402   /***************************************************************/
403   aTopLayout->addWidget(GroupConstructors);
404   aTopLayout->addWidget(GroupC1);
405   aTopLayout->addWidget(GroupGroups);
406   aTopLayout->addWidget(GroupButtons);
407
408   Init(); /* Initialisations */
409 }
410
411 //=================================================================================
412 // function : ~SMESHGUI_AddMeshElementDlg()
413 // purpose  : Destroys the object and frees any allocated resources
414 //=================================================================================
415 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
416 {
417   delete mySimulation;
418 }
419
420 //=================================================================================
421 // function : Init()
422 // purpose  :
423 //=================================================================================
424 void SMESHGUI_AddMeshElementDlg::Init()
425 {
426   GroupC1->show();
427   Constructor1->setChecked(true);
428   myEditCurrentArgument = LineEditC1A1;
429   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
430
431   /* reset "Add to group" control */
432   GroupGroups->setChecked( false );
433   GroupGroups->setVisible( myElementType != SMDSAbs_0DElement );
434
435   myNbOkNodes = 0;
436   myActor = 0;
437
438   /* signals and slots connections */
439   connect(buttonOk, SIGNAL(clicked()),     SLOT(ClickOnOk()));
440   connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
441   connect(buttonApply, SIGNAL(clicked()),  SLOT(ClickOnApply()));
442   connect(buttonHelp, SIGNAL(clicked()),   SLOT(ClickOnHelp()));
443
444   connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
445   connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
446   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
447   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
448   /* to close dialog if study frame change */
449   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
450   connect(mySMESHGUI, SIGNAL (SignalCloseAllDialogs()), SLOT(ClickOnCancel()));    
451
452   if (Reverse)
453     connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
454
455   // set selection mode
456   SMESH::SetPointRepresentation(true);
457
458   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
459     aViewWindow->SetSelectionMode( NodeSelection );
460
461   myBusy = false;
462
463   SelectionIntoArgument();
464 }
465
466 //=================================================================================
467 // function : ClickOnApply()
468 // purpose  :
469 //=================================================================================
470 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
471 {
472   if( !isValid() )
473     return;
474
475   if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
476     myBusy = true;
477     SMESH::long_array_var anArrayOfIndices = new SMESH::long_array;
478     anArrayOfIndices->length(myNbNodes);
479     bool reverse = (Reverse && Reverse->isChecked());
480     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
481     for (int i = 0; i < aListId.count(); i++)
482       if (reverse)
483         anArrayOfIndices[i] = aListId[ myNbNodes - i - 1 ].toInt();
484       else
485         anArrayOfIndices[i] = aListId[ i ].toInt();
486
487     bool addToGroup = GroupGroups->isChecked();
488     QString aGroupName;
489
490     SMESH::SMESH_GroupBase_var aGroup;
491     int idx = 0;
492     if( addToGroup ) {
493       aGroupName = ComboBox_GroupName->currentText();
494       for ( int i = 1; i < ComboBox_GroupName->count(); i++ ) {
495         QString aName = ComboBox_GroupName->itemText( i );
496         if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
497           idx = i;
498       }
499       if ( idx > 0 ) {
500         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
501         if ( !aGeomGroup->_is_nil() ) {
502           int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
503                                                tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
504                                                tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
505           if ( res == 1 ) return;
506         }
507         aGroup = myGroups[idx-1];
508       }
509     }
510
511     long anElemId = -1;
512     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
513     switch (myElementType) {
514     case SMDSAbs_0DElement:
515       anElemId = aMeshEditor->Add0DElement(anArrayOfIndices[0]); break;
516     case SMDSAbs_Edge:
517       anElemId = aMeshEditor->AddEdge(anArrayOfIndices.inout()); break;
518     case SMDSAbs_Face: {
519       if(myIsPoly)
520         anElemId = aMeshEditor->AddPolygonalFace(anArrayOfIndices.inout());
521       else
522         anElemId = aMeshEditor->AddFace(anArrayOfIndices.inout());
523       break;
524     }
525     case SMDSAbs_Volume:
526       anElemId = aMeshEditor->AddVolume(anArrayOfIndices.inout()); break;
527     default: break;
528     }
529
530     if ( anElemId != -1 && addToGroup && !aGroupName.isEmpty() ) {
531       SMESH::SMESH_Group_var aGroupUsed;
532       if ( aGroup->_is_nil() ) {
533         // create new group 
534         aGroupUsed = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
535         if ( !aGroupUsed->_is_nil() ) {
536           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
537           ComboBox_GroupName->addItem( aGroupName );
538         }
539       }
540       else {
541         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
542         if ( !aGeomGroup->_is_nil() ) {
543           aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
544           if ( !aGroupUsed->_is_nil() && idx > 0 ) {
545             myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
546             SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
547           }
548         }
549         else
550           aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
551       }
552
553       if ( !aGroupUsed->_is_nil() ) {
554         SMESH::long_array_var anIdList = new SMESH::long_array;
555         anIdList->length( 1 );
556         anIdList[0] = anElemId;
557         aGroupUsed->Add( anIdList.inout() );
558       }
559     }
560
561     SALOME_ListIO aList; aList.Append( myActor->getIO() );
562     mySelector->ClearIndex();
563     mySelectionMgr->setSelectedObjects( aList, false );
564
565     SMESH::UpdateView();
566     mySimulation->SetVisibility(false);
567
568     buttonOk->setEnabled(false);
569     buttonApply->setEnabled(false);
570
571     myEditCurrentArgument->setText("");
572
573     myBusy = false;
574
575     SMESHGUI::Modified();
576   }
577 }
578
579 //=================================================================================
580 // function : ClickOnOk()
581 // purpose  :
582 //=================================================================================
583 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
584 {
585   ClickOnApply();
586   ClickOnCancel();
587 }
588
589 //=================================================================================
590 // function : ClickOnCancel()
591 // purpose  :
592 //=================================================================================
593 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
594 {
595   //mySelectionMgr->clearSelected();
596   mySimulation->SetVisibility(false);
597   SMESH::SetPointRepresentation(false);
598   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
599     aViewWindow->SetSelectionMode( ActorSelection );
600   disconnect(mySelectionMgr, 0, this, 0);
601   mySMESHGUI->ResetState();
602   reject();
603 }
604
605 //=================================================================================
606 // function : ClickOnHelp()
607 // purpose  :
608 //=================================================================================
609 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
610 {
611   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
612   if (app)
613     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
614                              myHelpFileName);
615   else {
616     QString platform;
617 #ifdef WIN32
618     platform = "winapplication";
619 #else
620     platform = "application";
621 #endif
622     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
623                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
624                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
625                                                                  platform)).
626                              arg(myHelpFileName));
627   }
628 }
629
630 //=================================================================================
631 // function : onTextChange()
632 // purpose  :
633 //=================================================================================
634 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
635 {
636   if (myBusy) return;
637   myBusy = true;
638
639   myNbOkNodes = 0;
640
641   buttonOk->setEnabled(false);
642   buttonApply->setEnabled(false);
643
644   mySimulation->SetVisibility(false);
645
646   // hilight entered nodes
647   SMDS_Mesh* aMesh = 0;
648   if (myActor)
649     aMesh = myActor->GetObject()->GetMesh();
650
651   if (aMesh) {
652     TColStd_MapOfInteger newIndices;
653
654     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
655     bool allOk = true;
656     for (int i = 0; i < aListId.count(); i++) {
657       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
658         {
659           newIndices.Add( n->GetID() );
660           myNbOkNodes++;
661         }
662       else
663         allOk = false;  
664     }
665
666     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
667     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
668       aViewWindow->highlight( myActor->getIO(), true, true );
669
670     myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
671
672     if (myIsPoly)
673       {
674         if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
675           myNbOkNodes = 0;
676         else
677           myNbOkNodes = aListId.count();
678       }
679   }
680
681   if(myNbOkNodes) {
682     buttonOk->setEnabled(true);
683     buttonApply->setEnabled(true);
684     displaySimulation();
685   }
686
687   myBusy = false;
688 }
689
690 //=================================================================================
691 // function : SelectionIntoArgument()
692 // purpose  : Called when selection has changed
693 //=================================================================================
694 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
695 {
696   if (myBusy) return;
697
698   // clear
699   myNbOkNodes = 0;
700   myActor = 0;
701
702   myBusy = true;
703   myEditCurrentArgument->setText("");
704   myBusy = false;
705
706   if (!GroupButtons->isEnabled()) // inactive
707     return;
708
709   buttonOk->setEnabled(false);
710   buttonApply->setEnabled(false);
711
712   mySimulation->SetVisibility(false);
713   //  SMESH::SetPointRepresentation(true);
714
715   QString aCurrentEntry = myEntry;
716
717   // get selected mesh
718   SALOME_ListIO aList;
719   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
720
721   if (aList.Extent() != 1)
722     return;
723
724   Handle(SALOME_InteractiveObject) anIO = aList.First();
725   myEntry = anIO->getEntry();
726   myMesh = SMESH::GetMeshByIO(anIO);
727   if (myMesh->_is_nil())
728     return;
729
730   // process groups
731   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
732     myGroups.clear();
733     ComboBox_GroupName->clear();
734     ComboBox_GroupName->addItem( QString() );
735     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
736     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
737       SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
738       if ( !aGroup->_is_nil() && aGroup->GetType() == (SMESH::ElementType)myElementType ) {
739         QString aGroupName( aGroup->GetName() );
740         if ( !aGroupName.isEmpty() ) {
741           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
742           ComboBox_GroupName->addItem( aGroupName );
743         }
744       }
745     }
746   }
747
748   myActor = SMESH::FindActorByEntry(anIO->getEntry());
749   if (!myActor)
750     return;
751
752   // get selected nodes
753   QString aString = "";
754   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
755   myBusy = true;
756   myEditCurrentArgument->setText(aString);
757   myBusy = false;
758   if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
759     myNbNodes = nbNodes;
760   } else if (myNbNodes != nbNodes) {
761     return;
762   }
763
764   // OK
765   myNbOkNodes = nbNodes;
766
767   buttonOk->setEnabled(true);
768   buttonApply->setEnabled(true);
769
770   displaySimulation();
771 }
772
773 //=================================================================================
774 // function : displaySimulation()
775 // purpose  :
776 //=================================================================================
777 void SMESHGUI_AddMeshElementDlg::displaySimulation()
778 {
779   if (myNbOkNodes && GroupButtons->isEnabled()) {
780     SMESH::TElementSimulation::TVTKIds anIds;
781     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
782     for (int i = 0; i < aListId.count(); i++)
783       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
784
785     if (Reverse && Reverse->isChecked())
786       reverse(anIds.begin(),anIds.end());
787
788     vtkIdType aType = 0;
789     if (myIsPoly)
790       switch ( myElementType ) {
791       case SMDSAbs_Face  : aType = VTK_POLYGON; break;
792       default: return;
793       }
794     else {
795       switch (myNbNodes) {
796       case 2: aType = VTK_LINE; break;
797       case 3: aType = VTK_TRIANGLE; break;
798       case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
799       case 8: aType = VTK_HEXAHEDRON; break;
800       default: return;
801       }
802     }
803
804     mySimulation->SetPosition(myActor,aType,anIds);
805     SMESH::UpdateView();
806   }
807 }
808
809 //=================================================================================
810 // function : SetEditCurrentArgument()
811 // purpose  :
812 //=================================================================================
813 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
814 {
815   QPushButton* send = (QPushButton*)sender();
816   if (send == SelectButtonC1A1) {
817     LineEditC1A1->setFocus();
818     myEditCurrentArgument = LineEditC1A1;
819   }
820   SelectionIntoArgument();
821 }
822
823 //=================================================================================
824 // function : DeactivateActiveDialog()
825 // purpose  :
826 //=================================================================================
827 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
828 {
829   if (GroupConstructors->isEnabled()) {
830     GroupConstructors->setEnabled(false);
831     GroupC1->setEnabled(false);
832     GroupButtons->setEnabled(false);
833     mySimulation->SetVisibility(false);
834     mySMESHGUI->ResetState();
835     mySMESHGUI->SetActiveDialogBox(0);
836   }
837 }
838
839 //=================================================================================
840 // function : ActivateThisDialog()
841 // purpose  :
842 //=================================================================================
843 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
844 {
845   /* Emit a signal to deactivate the active dialog */
846   mySMESHGUI->EmitSignalDeactivateDialog();
847
848   GroupConstructors->setEnabled(true);
849   GroupC1->setEnabled(true);
850   GroupButtons->setEnabled(true);
851
852   SMESH::SetPointRepresentation(true);
853
854   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
855     aViewWindow->SetSelectionMode( NodeSelection );
856   SelectionIntoArgument();
857 }
858
859 //=================================================================================
860 // function : enterEvent()
861 // purpose  :
862 //=================================================================================
863 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
864 {
865   if (GroupConstructors->isEnabled())
866     return;
867   ActivateThisDialog();
868 }
869
870 //=================================================================================
871 // function : closeEvent()
872 // purpose  :
873 //=================================================================================
874 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
875 {
876   /* same than click on cancel button */
877   ClickOnCancel();
878 }
879
880 //=================================================================================
881 // function : hideEvent()
882 // purpose  : caused by ESC key
883 //=================================================================================
884 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
885 {
886   if (!isMinimized())
887     ClickOnCancel();
888 }
889
890 //=================================================================================
891 // function : CheckBox()
892 // purpose  :
893 //=================================================================================
894 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
895 {
896   if (!myNbOkNodes)
897     return;
898
899   if (state >= 0) {
900     mySimulation->SetVisibility(false);
901     displaySimulation();
902   }
903 }
904
905 //=================================================================================
906 // function : keyPressEvent()
907 // purpose  :
908 //=================================================================================
909 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
910 {
911   QDialog::keyPressEvent( e );
912   if ( e->isAccepted() )
913     return;
914
915   if ( e->key() == Qt::Key_F1 ) {
916     e->accept();
917     ClickOnHelp();
918   }
919 }
920
921 //=================================================================================
922 // function : isValid
923 // purpose  :
924 //=================================================================================
925 bool SMESHGUI_AddMeshElementDlg::isValid()
926 {
927   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
928     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
929     return false;
930   }
931   return true;
932 }