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