Salome HOME
Merge from V6_main 01/04/2013
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
1 // Copyright (C) 2007-2013  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->SetInputData(myGrid);
117
118       myPreviewActor = SALOME_Actor::New();
119       myPreviewActor->PickableOff();
120       myPreviewActor->VisibilityOff();
121       myPreviewActor->SetMapper(myMapper);
122
123       double 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->SetInputData(myGrid);
141
142       myFaceOrientationDataMapper = vtkPolyDataMapper::New();
143       myFaceOrientationDataMapper->SetInputConnection(myFaceOrientationFilter->GetOutputPort());
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
347     (new SMESHGUI_IdValidator(this, ( myIsPoly || myNbNodes == 1 ) ? 1000 : myNbNodes));
348
349   Reverse = (myElementType == SMDSAbs_Face || myElementType == SMDSAbs_Volume ) ? new QCheckBox(tr("SMESH_REVERSE"), GroupC1) : 0;
350
351   DiameterSpinBox = ( myGeomType == SMDSEntity_Ball ) ? new SMESHGUI_SpinBox(GroupC1) : 0;
352   QLabel* diameterLabel = DiameterSpinBox ? new QLabel( tr("BALL_DIAMETER"),GroupC1) : 0;
353
354   GroupC1Layout->addWidget(TextLabelC1A1,    0, 0);
355   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
356   GroupC1Layout->addWidget(LineEditC1A1,     0, 2);
357   if ( Reverse ) {
358     GroupC1Layout->addWidget(Reverse, 1, 0, 1, 3);
359   }
360   if ( DiameterSpinBox ) {
361     GroupC1Layout->addWidget(diameterLabel,   1, 0);
362     GroupC1Layout->addWidget(DiameterSpinBox, 1, 1, 1, 2);
363
364     DiameterSpinBox->RangeStepAndValidator( 1e-7, 1e+9, 0.1 );
365     DiameterSpinBox->SetValue( 1. );
366   }
367   /* Add to group ************************************************/
368   GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
369   GroupGroups->setCheckable( true );
370   QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
371   GroupGroupsLayout->setSpacing(SPACING);
372   GroupGroupsLayout->setMargin(MARGIN);
373
374   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
375   ComboBox_GroupName = new QComboBox( GroupGroups );
376   ComboBox_GroupName->setEditable( true );
377   ComboBox_GroupName->setInsertPolicy( QComboBox::NoInsert );
378
379   GroupGroupsLayout->addWidget( TextLabel_GroupName );
380   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
381
382   /* Apply etc ***************************************************/
383   GroupButtons = new QGroupBox(this);
384   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout(GroupButtons);
385   GroupButtonsLayout->setSpacing(SPACING);
386   GroupButtonsLayout->setMargin(MARGIN);
387
388   buttonOk = new QPushButton(tr("SMESH_BUT_APPLY_AND_CLOSE"), GroupButtons);
389   buttonOk->setAutoDefault(true);
390   buttonOk->setDefault(true);
391   buttonApply = new QPushButton(tr("SMESH_BUT_APPLY"), GroupButtons);
392   buttonApply->setAutoDefault(true);
393   buttonCancel = new QPushButton(tr("SMESH_BUT_CLOSE"), GroupButtons);
394   buttonCancel->setAutoDefault(true);
395   buttonHelp = new QPushButton(tr("SMESH_BUT_HELP"), GroupButtons);
396   buttonHelp->setAutoDefault(true);
397
398   GroupButtonsLayout->addWidget(buttonOk);
399   GroupButtonsLayout->addSpacing(10);
400   GroupButtonsLayout->addWidget(buttonApply);
401   GroupButtonsLayout->addSpacing(10);
402   GroupButtonsLayout->addStretch();
403   GroupButtonsLayout->addWidget(buttonCancel);
404   GroupButtonsLayout->addWidget(buttonHelp);
405
406   /***************************************************************/
407   aTopLayout->addWidget(GroupConstructors);
408   aTopLayout->addWidget(GroupC1);
409   aTopLayout->addWidget(GroupGroups);
410   aTopLayout->addWidget(GroupButtons);
411
412   Init(); /* Initialisations */
413 }
414
415 //=================================================================================
416 // function : ~SMESHGUI_AddMeshElementDlg()
417 // purpose  : Destroys the object and frees any allocated resources
418 //=================================================================================
419 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
420 {
421   delete mySimulation;
422 }
423
424 //=================================================================================
425 // function : Init()
426 // purpose  :
427 //=================================================================================
428 void SMESHGUI_AddMeshElementDlg::Init()
429 {
430   GroupC1->show();
431   Constructor1->setChecked(true);
432   myEditCurrentArgument = LineEditC1A1;
433   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
434
435   /* reset "Add to group" control */
436   GroupGroups->setChecked( false );
437   //GroupGroups->setVisible( myElementType != SMDSAbs_0DElement );
438
439   myNbOkNodes = 0;
440   myActor = 0;
441
442   /* signals and slots connections */
443   connect(buttonOk,        SIGNAL(clicked()),                     SLOT(ClickOnOk()));
444   connect(buttonCancel,    SIGNAL(clicked()),                     SLOT(reject()));
445   connect(buttonApply,     SIGNAL(clicked()),                     SLOT(ClickOnApply()));
446   connect(buttonHelp,      SIGNAL(clicked()),                     SLOT(ClickOnHelp()));
447
448   connect(SelectButtonC1A1,SIGNAL(clicked()),                     SLOT(SetEditCurrentArgument()));
449   connect(LineEditC1A1,    SIGNAL(textChanged(const QString&)),   SLOT(onTextChange(const QString&)));
450   connect(mySMESHGUI,      SIGNAL(SignalDeactivateActiveDialog()),SLOT(DeactivateActiveDialog()));
451   connect(mySelectionMgr,  SIGNAL(currentSelectionChanged()),     SLOT(SelectionIntoArgument()));
452   /* to close dialog if study frame change */
453   connect(mySMESHGUI,      SIGNAL(SignalStudyFrameChanged()),     SLOT(reject()));
454   connect(mySMESHGUI,      SIGNAL(SignalCloseAllDialogs()),       SLOT(reject()));    
455
456   if (Reverse)
457     connect(Reverse,       SIGNAL(stateChanged(int)),             SLOT(CheckBox(int)));
458
459   // set selection mode
460   SMESH::SetPointRepresentation(true);
461
462   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
463     aViewWindow->SetSelectionMode( NodeSelection );
464
465   myBusy = false;
466
467   SelectionIntoArgument();
468 }
469
470 //=================================================================================
471 // function : ClickOnApply()
472 // purpose  :
473 //=================================================================================
474 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
475 {
476   if( !isValid() )
477     return;
478
479   if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
480     myBusy = true;
481     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
482     SMESH::long_array_var anArrayOfIndices = new SMESH::long_array;
483     anArrayOfIndices->length(aListId.count());
484     const std::vector<int>& revIndex = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
485     if ( Reverse && Reverse->isChecked() && !revIndex.empty() )
486       for (int i = 0; i < aListId.count(); i++)
487         anArrayOfIndices[i] = aListId[ revIndex[i] ].toInt();
488     else if ( Reverse && Reverse->isChecked() && revIndex.empty() ) // polygon
489       for (int i = 0; i < aListId.count(); i++)
490         anArrayOfIndices[i] = aListId[ aListId.count()-1 - i ].toInt();
491     else
492       for (int i = 0; i < aListId.count(); i++)
493         anArrayOfIndices[i] = aListId[ i ].toInt();
494
495     bool addToGroup = GroupGroups->isChecked();
496     QString aGroupName;
497
498     SMESH::SMESH_GroupBase_var aGroup;
499     int idx = 0;
500     if( addToGroup ) {
501       aGroupName = ComboBox_GroupName->currentText();
502       for ( int i = 1; i <= ComboBox_GroupName->count(); i++ ) {
503         QString aName = ComboBox_GroupName->itemText( i );
504         if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
505           idx = i;
506       }
507       if ( idx > 0 && idx <= myGroups.count() ) {
508         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
509         if ( !aGeomGroup->_is_nil() ) {
510           int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
511                                                tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
512                                                tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
513           if ( res == 1 ) return;
514         }
515         aGroup = myGroups[idx-1];
516       }
517     }
518
519     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
520     SMESH::long_array_var anIdList = new SMESH::long_array;
521     anIdList->length( 1 );
522     anIdList[0] = -1;
523
524     switch (myElementType) {
525     case SMDSAbs_0DElement:
526       anIdList->length( anArrayOfIndices->length() );
527       for ( size_t i = 0; i < anArrayOfIndices->length(); ++i )
528         anIdList[i] = aMeshEditor->Add0DElement(anArrayOfIndices[i]);
529       break;
530     case SMDSAbs_Ball:
531       if ( myGeomType == SMDSEntity_Ball ) {
532         anIdList->length( anArrayOfIndices->length() );
533         for ( size_t i = 0; i < anArrayOfIndices->length(); ++i )
534           anIdList[i] = aMeshEditor->AddBall(anArrayOfIndices[i],
535                                              DiameterSpinBox->GetValue());
536       }
537       break;
538     case SMDSAbs_Edge:
539       anIdList[0] = aMeshEditor->AddEdge(anArrayOfIndices.inout()); break;
540     case SMDSAbs_Face:
541       if ( myIsPoly )
542         anIdList[0] = aMeshEditor->AddPolygonalFace(anArrayOfIndices.inout());
543       else
544         anIdList[0] = aMeshEditor->AddFace(anArrayOfIndices.inout());
545       break;
546     default:
547       anIdList[0] = aMeshEditor->AddVolume(anArrayOfIndices.inout()); break;
548     }
549
550     if ( anIdList[0] > 0 && addToGroup && !aGroupName.isEmpty() ) {
551       SMESH::SMESH_Group_var aGroupUsed;
552       if ( aGroup->_is_nil() ) {
553         // create new group 
554         aGroupUsed = SMESH::AddGroup( myMesh, (SMESH::ElementType)myElementType, aGroupName );
555         if ( !aGroupUsed->_is_nil() ) {
556           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
557           ComboBox_GroupName->addItem( aGroupName );
558         }
559       }
560       else {
561         SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
562         if ( !aGeomGroup->_is_nil() ) {
563           aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
564           if ( !aGroupUsed->_is_nil() && idx > 0 ) {
565             myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
566             SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
567           }
568         }
569         else
570           aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
571       }
572
573       if ( !aGroupUsed->_is_nil() )
574         aGroupUsed->Add( anIdList.inout() );
575     }
576
577     SALOME_ListIO aList; aList.Append( myActor->getIO() );
578     mySelector->ClearIndex();
579     mySelectionMgr->setSelectedObjects( aList, false );
580
581     SMESH::UpdateView();
582     mySimulation->SetVisibility(false);
583
584     buttonOk->setEnabled(false);
585     buttonApply->setEnabled(false);
586
587     myEditCurrentArgument->setText("");
588
589     myBusy = false;
590
591     SMESHGUI::Modified();
592   }
593 }
594
595 //=================================================================================
596 // function : ClickOnOk()
597 // purpose  :
598 //=================================================================================
599 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
600 {
601   ClickOnApply();
602   reject();
603 }
604
605 //=================================================================================
606 // function : reject()
607 // purpose  :
608 //=================================================================================
609 void SMESHGUI_AddMeshElementDlg::reject()
610 {
611   //mySelectionMgr->clearSelected();
612   mySimulation->SetVisibility(false);
613   SMESH::SetPointRepresentation(false);
614   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
615     aViewWindow->SetSelectionMode( ActorSelection );
616   disconnect(mySelectionMgr, 0, this, 0);
617   mySMESHGUI->ResetState();
618   QDialog::reject();
619 }
620
621 //=================================================================================
622 // function : ClickOnHelp()
623 // purpose  :
624 //=================================================================================
625 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
626 {
627   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
628   if (app)
629     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""),
630                              myHelpFileName);
631   else {
632     QString platform;
633 #ifdef WIN32
634     platform = "winapplication";
635 #else
636     platform = "application";
637 #endif
638     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
639                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
640                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
641                                                                  platform)).
642                              arg(myHelpFileName));
643   }
644 }
645
646 //=================================================================================
647 // function : onTextChange()
648 // purpose  :
649 //=================================================================================
650 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
651 {
652   if (myBusy) return;
653   myBusy = true;
654
655   myNbOkNodes = 0;
656
657   buttonOk->setEnabled(false);
658   buttonApply->setEnabled(false);
659
660   mySimulation->SetVisibility(false);
661
662   // hilight entered nodes
663   SMDS_Mesh* aMesh = 0;
664   if (myActor)
665     aMesh = myActor->GetObject()->GetMesh();
666
667   if (aMesh) {
668     TColStd_MapOfInteger newIndices;
669
670     QStringList aListId = theNewText.split(" ", QString::SkipEmptyParts);
671     bool allOk = true;
672     for (int i = 0; i < aListId.count(); i++) {
673       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
674         {
675           newIndices.Add( n->GetID() );
676           myNbOkNodes++;
677         }
678       else
679         allOk = false;  
680     }
681
682     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
683     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
684       aViewWindow->highlight( myActor->getIO(), true, true );
685
686     myNbOkNodes = ( allOk && ( myNbNodes == aListId.count() || myNbNodes == 1 ));
687
688     if (myIsPoly)
689       {
690         if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
691           myNbOkNodes = 0;
692         else
693           myNbOkNodes = aListId.count();
694       }
695   }
696
697   if(myNbOkNodes) {
698     buttonOk->setEnabled(true);
699     buttonApply->setEnabled(true);
700     displaySimulation();
701   }
702
703   myBusy = false;
704 }
705
706 //=================================================================================
707 // function : SelectionIntoArgument()
708 // purpose  : Called when selection has changed
709 //=================================================================================
710 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
711 {
712   if (myBusy) return;
713
714   // clear
715   myNbOkNodes = 0;
716   myActor = 0;
717
718   myBusy = true;
719   myEditCurrentArgument->setText("");
720   myBusy = false;
721
722   if (!GroupButtons->isEnabled()) // inactive
723     return;
724
725   buttonOk->setEnabled(false);
726   buttonApply->setEnabled(false);
727
728   mySimulation->SetVisibility(false);
729   //  SMESH::SetPointRepresentation(true);
730
731   QString aCurrentEntry = myEntry;
732
733   // get selected mesh
734   SALOME_ListIO aList;
735   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
736
737   if (aList.Extent() != 1)
738     return;
739
740   Handle(SALOME_InteractiveObject) anIO = aList.First();
741   myEntry = anIO->getEntry();
742   myMesh = SMESH::GetMeshByIO(anIO);
743   if (myMesh->_is_nil())
744     return;
745
746   // process groups
747   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
748     myGroups.clear();
749     ComboBox_GroupName->clear();
750     ComboBox_GroupName->addItem( QString() );
751     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
752     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
753       SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
754       if ( !aGroup->_is_nil() && aGroup->GetType() == (SMESH::ElementType)myElementType ) {
755         QString aGroupName( aGroup->GetName() );
756         if ( !aGroupName.isEmpty() ) {
757           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
758           ComboBox_GroupName->addItem( aGroupName );
759         }
760       }
761     }
762   }
763
764   myActor = SMESH::FindActorByEntry(anIO->getEntry());
765   if (!myActor)
766     return;
767
768   // get selected nodes
769   QString aString = "";
770   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
771   myBusy = true;
772   myEditCurrentArgument->setText(aString);
773   myBusy = false;
774   if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
775     myNbNodes = nbNodes;
776   } else if (myNbNodes != nbNodes && myNbNodes != 1) {
777     return;
778   }
779
780   // OK
781   myNbOkNodes = nbNodes;
782
783   buttonOk->setEnabled(true);
784   buttonApply->setEnabled(true);
785
786   displaySimulation();
787 }
788
789 //=================================================================================
790 // function : displaySimulation()
791 // purpose  :
792 //=================================================================================
793 void SMESHGUI_AddMeshElementDlg::displaySimulation()
794 {
795   if (myNbOkNodes && GroupButtons->isEnabled()) {
796     SMESH::TElementSimulation::TVTKIds anIds;
797     QStringList aListId = myEditCurrentArgument->text().split(" ", QString::SkipEmptyParts);
798     for (int i = 0; i < aListId.count(); i++)
799       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
800
801     if (Reverse && Reverse->isChecked())
802     {
803       const std::vector<int>& i = SMDS_MeshCell::reverseSmdsOrder( myGeomType );
804       if ( i.empty() ) // polygon
805         std::reverse( anIds.begin(), anIds.end() );
806       else
807         SMDS_MeshCell::applyInterlace( i, anIds );
808     }
809
810     vtkIdType aType = SMDS_MeshCell::toVtkType( myGeomType );
811     mySimulation->SetPosition(myActor,aType,anIds);
812     SMESH::UpdateView();
813   }
814 }
815
816 //=================================================================================
817 // function : SetEditCurrentArgument()
818 // purpose  :
819 //=================================================================================
820 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
821 {
822   QPushButton* send = (QPushButton*)sender();
823   if (send == SelectButtonC1A1) {
824     LineEditC1A1->setFocus();
825     myEditCurrentArgument = LineEditC1A1;
826   }
827   SelectionIntoArgument();
828 }
829
830 //=================================================================================
831 // function : DeactivateActiveDialog()
832 // purpose  :
833 //=================================================================================
834 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
835 {
836   if (GroupConstructors->isEnabled()) {
837     GroupConstructors->setEnabled(false);
838     GroupC1->setEnabled(false);
839     GroupButtons->setEnabled(false);
840     mySimulation->SetVisibility(false);
841     mySMESHGUI->ResetState();
842     mySMESHGUI->SetActiveDialogBox(0);
843   }
844 }
845
846 //=================================================================================
847 // function : ActivateThisDialog()
848 // purpose  :
849 //=================================================================================
850 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
851 {
852   /* Emit a signal to deactivate the active dialog */
853   mySMESHGUI->EmitSignalDeactivateDialog();
854
855   GroupConstructors->setEnabled(true);
856   GroupC1->setEnabled(true);
857   GroupButtons->setEnabled(true);
858
859   SMESH::SetPointRepresentation(true);
860
861   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
862     aViewWindow->SetSelectionMode( NodeSelection );
863   SelectionIntoArgument();
864 }
865
866 //=================================================================================
867 // function : enterEvent()
868 // purpose  :
869 //=================================================================================
870 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
871 {
872   if (GroupConstructors->isEnabled())
873     return;
874   ActivateThisDialog();
875 }
876
877 //=================================================================================
878 // function : CheckBox()
879 // purpose  :
880 //=================================================================================
881 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
882 {
883   if (!myNbOkNodes)
884     return;
885
886   if (state >= 0) {
887     mySimulation->SetVisibility(false);
888     displaySimulation();
889   }
890 }
891
892 //=================================================================================
893 // function : keyPressEvent()
894 // purpose  :
895 //=================================================================================
896 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
897 {
898   QDialog::keyPressEvent( e );
899   if ( e->isAccepted() )
900     return;
901
902   if ( e->key() == Qt::Key_F1 ) {
903     e->accept();
904     ClickOnHelp();
905   }
906 }
907
908 //=================================================================================
909 // function : isValid
910 // purpose  :
911 //=================================================================================
912 bool SMESHGUI_AddMeshElementDlg::isValid()
913 {
914   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
915     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
916     return false;
917   }
918   return true;
919 }