Salome HOME
IPAL21120 SIGSEGV on Meshing attached Compound with Automatic Hexadralization
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_AddMeshElementDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESHGUI : GUI for SMESH component
23 //  File   : SMESHGUI_AddMeshElementDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : SMESH
26 //  $Header$
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_IdValidator.h"
35 #include "SMESH_ActorUtils.h"
36
37 #include "SMDS_Mesh.hxx"
38 #include "SMESH_Actor.h"
39
40 #include "SUIT_Session.h"
41 #include "SUIT_MessageBox.h"
42 #include "LightApp_Application.h"
43
44 #include "SVTK_Selection.h"
45 #include "SVTK_Selector.h"
46 #include "SALOME_ListIO.hxx"
47 #include "SALOME_ListIteratorOfListIO.hxx"
48
49 #include "SalomeApp_Study.h"
50 #include "SalomeApp_Application.h"
51
52 #include "SVTK_ViewModel.h"
53 #include "SVTK_ViewWindow.h"
54
55 #include "utilities.h"
56
57 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
58
59 // OCCT Includes
60 #include <TColStd_MapOfInteger.hxx>
61 #include <TColStd_IndexedMapOfInteger.hxx>
62
63 // VTK Includes
64 #include <vtkCell.h>
65 #include <vtkIdList.h>
66 #include <vtkIntArray.h>
67 #include <vtkCellArray.h>
68 #include <vtkUnsignedCharArray.h>
69 #include <vtkUnstructuredGrid.h>
70 #include <vtkDataSetMapper.h>
71 #include <vtkProperty.h>
72
73 // QT Includes
74 #include <qbuttongroup.h>
75 #include <qgroupbox.h>
76 #include <qlabel.h>
77 #include <qlineedit.h>
78 #include <qpushbutton.h>
79 #include <qradiobutton.h>
80 #include <qlayout.h>
81 #include <qvariant.h>
82 #include <qtooltip.h>
83 #include <qwhatsthis.h>
84 #include <qimage.h>
85 #include <qpixmap.h>
86 #include <qcheckbox.h>
87 #include <qregexp.h>
88
89 // STL includes
90 #include <list>
91
92 using namespace std;
93
94 namespace SMESH {
95
96   class TElementSimulation {
97     SalomeApp_Application* myApplication;
98     SUIT_ViewWindow* myViewWindow;
99     SVTK_ViewWindow* myVTKViewWindow;
100
101     SALOME_Actor* myPreviewActor;
102     vtkDataSetMapper* myMapper;
103     vtkUnstructuredGrid* myGrid;
104
105   public:
106     TElementSimulation (SalomeApp_Application* theApplication)
107     {
108       myApplication = theApplication;
109       SUIT_ViewManager* mgr = theApplication->activeViewManager();
110       if (!mgr) return;
111       myViewWindow = mgr->getActiveView();
112       myVTKViewWindow = GetVtkViewWindow(myViewWindow);
113
114       myGrid = vtkUnstructuredGrid::New();
115
116       // Create and display actor
117       myMapper = vtkDataSetMapper::New();
118       myMapper->SetInput(myGrid);
119
120       myPreviewActor = SALOME_Actor::New();
121       myPreviewActor->PickableOff();
122       myPreviewActor->VisibilityOff();
123       myPreviewActor->SetMapper(myMapper);
124
125       vtkFloatingPointType anRGB[3];
126       vtkProperty* aProp = vtkProperty::New();
127       GetColor( "SMESH", "fill_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
128       aProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
129       myPreviewActor->SetProperty( aProp );
130       aProp->Delete();
131
132       vtkProperty* aBackProp = vtkProperty::New();
133       GetColor( "SMESH", "backface_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 0, 255 ) );
134       aBackProp->SetColor( anRGB[0], anRGB[1], anRGB[2] );
135       myPreviewActor->SetBackfaceProperty( aBackProp );
136       aBackProp->Delete();
137
138       myVTKViewWindow->AddActor(myPreviewActor);
139     }
140
141     typedef std::vector<vtkIdType> TVTKIds;
142     void SetPosition (SMESH_Actor* theActor,
143                       vtkIdType theType,
144                       const TVTKIds& theIds)
145     {
146       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
147       myGrid->SetPoints(aGrid->GetPoints());
148
149       const int* aConn = NULL;
150       switch (theType) {
151       case VTK_TETRA:
152         {
153           static int anIds[] = {0,2,1,3};
154           aConn = anIds;
155           break;
156         }
157       case VTK_PYRAMID:
158         {
159           static int anIds[] = {0,3,2,1,4};
160           aConn = anIds;
161           break;
162         }
163       case VTK_HEXAHEDRON:
164         {
165           static int anIds[] = {0,3,2,1,4,7,6,5};
166           aConn = anIds;
167           break;
168         }
169       }
170
171       myGrid->Reset();
172       vtkIdList *anIds = vtkIdList::New();
173
174       if(aConn)
175         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
176           anIds->InsertId(i,theIds[aConn[i]]);
177       else
178         for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
179           anIds->InsertId(i,theIds[i]);
180
181       myGrid->InsertNextCell(theType,anIds);
182       anIds->Delete();
183
184       myGrid->Modified();
185
186       SetVisibility(true);
187     }
188
189
190     void SetVisibility (bool theVisibility)
191     {
192       myPreviewActor->SetVisibility(theVisibility);
193       RepaintCurrentView();
194     }
195
196
197     ~TElementSimulation()
198     {
199       if (FindVtkViewWindow(myApplication->activeViewManager(), myViewWindow)) {
200         myVTKViewWindow->RemoveActor(myPreviewActor);
201       }
202       myPreviewActor->Delete();
203
204       myMapper->RemoveAllInputs();
205       myMapper->Delete();
206
207       myGrid->Delete();
208     }
209   };
210 }
211
212 //=================================================================================
213 // function : SMESHGUI_AddMeshElementDlg()
214 // purpose  : constructor
215 //=================================================================================
216 SMESHGUI_AddMeshElementDlg::SMESHGUI_AddMeshElementDlg( SMESHGUI* theModule,
217                                                         const char* name,
218                                                         SMDSAbs_ElementType ElementType, int nbNodes,
219                                                         bool modal, WFlags fl)
220      : QDialog( SMESH::GetDesktop( theModule ), name, modal, WStyle_Customize | WStyle_NormalBorder |
221                 WStyle_Title | WStyle_SysMenu | Qt::WDestructiveClose),
222      mySMESHGUI( theModule ),
223      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
224 {
225   SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
226     (SUIT_Session::session()->activeApplication());
227   myIsPoly = false;
228   mySimulation = new SMESH::TElementSimulation (anApp);
229   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
230
231   // verify nb nodes and type
232   myNbNodes = nbNodes;
233   myElementType = ElementType;
234   switch (ElementType) {
235   case SMDSAbs_Face:
236 //     if (myNbNodes != 3 && myNbNodes != 4)
237 //       myNbNodes = 3;
238 //     break;
239   case SMDSAbs_Volume:
240 //     if (myNbNodes != 4 && myNbNodes != 8) //(nbNodes < 4 || nbNodes > 8 || nbNodes == 7)
241 //       myNbNodes = 4;
242     break;
243   default:
244     myElementType = SMDSAbs_Edge;
245     myNbNodes = 2;
246   }
247
248   QString elemName;
249   if (myNbNodes == 2) {
250     elemName = "EDGE";
251     myHelpFileName = "adding_nodes_and_elements_page.html#adding_edges_anchor";
252   }
253   else if (myNbNodes == 3) {
254     elemName = "TRIANGLE";
255     myHelpFileName = "adding_nodes_and_elements_page.html#adding_triangles_anchor";
256   }
257   else if (myNbNodes == 4)
258     if (myElementType == SMDSAbs_Face) {
259       elemName = "QUADRANGLE";
260       myHelpFileName = "adding_nodes_and_elements_page.html#adding_quadrangles_anchor";
261     }
262     else {
263       elemName = "TETRAS";
264       myHelpFileName = "adding_nodes_and_elements_page.html#adding_tetrahedrons_anchor";
265     }
266   else if (myNbNodes == 8) {
267     elemName = "HEXAS";
268     myHelpFileName = "adding_nodes_and_elements_page.html#adding_hexahedrons_anchor";
269   }
270   else if (myElementType == SMDSAbs_Face) {
271     elemName = "POLYGON";
272     myIsPoly = true;
273     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polygons_anchor";
274   }
275   else if (myElementType == SMDSAbs_Volume) {
276     myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
277   }
278   
279   QString iconName      = tr(QString("ICON_DLG_%1").arg(elemName));
280   QString buttonGrTitle = tr(QString("SMESH_%1").arg(elemName));
281   QString caption       = tr(QString("SMESH_ADD_%1_TITLE").arg(elemName));
282   QString grBoxTitle    = tr(QString("SMESH_ADD_%1").arg(elemName));
283
284   QPixmap image0 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", iconName));
285   QPixmap image1 (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
286
287   if (!name)
288     setName("SMESHGUI_AddMeshElementDlg");
289   resize(303, 185);
290   setCaption(caption);
291
292   setSizeGripEnabled(TRUE);
293   SMESHGUI_AddMeshElementDlgLayout = new QGridLayout(this);
294   SMESHGUI_AddMeshElementDlgLayout->setSpacing(6);
295   SMESHGUI_AddMeshElementDlgLayout->setMargin(11);
296
297   /***************************************************************/
298   GroupConstructors = new QButtonGroup(this, "GroupConstructors");
299   GroupConstructors->setTitle(buttonGrTitle);
300
301   GroupConstructors->setExclusive(TRUE);
302   GroupConstructors->setColumnLayout(0, Qt::Vertical);
303   GroupConstructors->layout()->setSpacing(0);
304   GroupConstructors->layout()->setMargin(0);
305   GroupConstructorsLayout = new QGridLayout(GroupConstructors->layout());
306   GroupConstructorsLayout->setAlignment(Qt::AlignTop);
307   GroupConstructorsLayout->setSpacing(6);
308   GroupConstructorsLayout->setMargin(11);
309   Constructor1 = new QRadioButton(GroupConstructors, "Constructor1");
310   Constructor1->setText(tr("" ));
311   Constructor1->setPixmap(image0);
312   Constructor1->setChecked(TRUE);
313   Constructor1->setSizePolicy(QSizePolicy((QSizePolicy::SizeType)1, (QSizePolicy::SizeType)0, Constructor1->sizePolicy().hasHeightForWidth()));
314   Constructor1->setMinimumSize(QSize(50, 0));
315   GroupConstructorsLayout->addWidget(Constructor1, 0, 0);
316   QSpacerItem* spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
317   GroupConstructorsLayout->addItem(spacer, 0, 1);
318   SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupConstructors, 0, 0);
319
320   /***************************************************************/
321   GroupButtons = new QGroupBox(this, "GroupButtons");
322   GroupButtons->setGeometry(QRect(10, 10, 281, 48));
323   GroupButtons->setTitle(tr("" ));
324   GroupButtons->setColumnLayout(0, Qt::Vertical);
325   GroupButtons->layout()->setSpacing(0);
326   GroupButtons->layout()->setMargin(0);
327   GroupButtonsLayout = new QGridLayout(GroupButtons->layout());
328   GroupButtonsLayout->setAlignment(Qt::AlignTop);
329   GroupButtonsLayout->setSpacing(6);
330   GroupButtonsLayout->setMargin(11);
331   buttonCancel = new QPushButton(GroupButtons, "buttonCancel");
332   buttonCancel->setText(tr("SMESH_BUT_CLOSE" ));
333   buttonCancel->setAutoDefault(TRUE);
334   GroupButtonsLayout->addWidget(buttonCancel, 0, 3);
335   buttonApply = new QPushButton(GroupButtons, "buttonApply");
336   buttonApply->setText(tr("SMESH_BUT_APPLY" ));
337   buttonApply->setAutoDefault(TRUE);
338   GroupButtonsLayout->addWidget(buttonApply, 0, 1);
339   QSpacerItem* spacer_9 = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
340   GroupButtonsLayout->addItem(spacer_9, 0, 2);
341   buttonOk = new QPushButton(GroupButtons, "buttonOk");
342   buttonOk->setText(tr("SMESH_BUT_APPLY_AND_CLOSE" ));
343   buttonOk->setAutoDefault(TRUE);
344   buttonOk->setDefault(TRUE);
345   GroupButtonsLayout->addWidget(buttonOk, 0, 0);
346   buttonHelp = new QPushButton(GroupButtons, "buttonHelp");
347   buttonHelp->setText(tr("SMESH_BUT_HELP" ));
348   buttonHelp->setAutoDefault(TRUE);
349   GroupButtonsLayout->addWidget(buttonHelp, 0, 4);
350
351   SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupButtons, 2, 0);
352
353   /***************************************************************/
354   GroupC1 = new QGroupBox(this, "GroupC1");
355   GroupC1->setTitle(grBoxTitle);
356
357   GroupC1->setMinimumSize(QSize(0, 0));
358   GroupC1->setFrameShape(QGroupBox::Box);
359   GroupC1->setFrameShadow(QGroupBox::Sunken);
360   GroupC1->setColumnLayout(0, Qt::Vertical);
361   GroupC1->layout()->setSpacing(0);
362   GroupC1->layout()->setMargin(0);
363   GroupC1Layout = new QGridLayout(GroupC1->layout());
364   GroupC1Layout->setAlignment(Qt::AlignTop);
365   GroupC1Layout->setSpacing(6);
366   GroupC1Layout->setMargin(11);
367   TextLabelC1A1 = new QLabel(GroupC1, "TextLabelC1A1");
368   TextLabelC1A1->setText(tr("SMESH_ID_NODES" ));
369   TextLabelC1A1->setMinimumSize(QSize(50, 0));
370   TextLabelC1A1->setFrameShape(QLabel::NoFrame);
371   TextLabelC1A1->setFrameShadow(QLabel::Plain);
372   GroupC1Layout->addWidget(TextLabelC1A1, 0, 0);
373   SelectButtonC1A1 = new QPushButton(GroupC1, "SelectButtonC1A1");
374   SelectButtonC1A1->setText(tr("" ));
375   SelectButtonC1A1->setPixmap(image1);
376   SelectButtonC1A1->setToggleButton(FALSE);
377   GroupC1Layout->addWidget(SelectButtonC1A1, 0, 1);
378   LineEditC1A1 = new QLineEdit(GroupC1, "LineEditC1A1");
379 //  LineEditC1A1->setReadOnly(TRUE);
380   if (!myIsPoly)
381     LineEditC1A1->setValidator(new SMESHGUI_IdValidator(this, "validator", myNbNodes));
382   GroupC1Layout->addWidget(LineEditC1A1, 0, 2);
383
384   if (myElementType == SMDSAbs_Face) {
385     Reverse = new QCheckBox(GroupC1, "Reverse");
386     Reverse->setText(tr("SMESH_REVERSE" ));
387     GroupC1Layout->addWidget(Reverse, 1, 0);
388   }
389   else
390     Reverse = 0;
391
392   SMESHGUI_AddMeshElementDlgLayout->addWidget(GroupC1, 1, 0);
393
394   Init(); /* Initialisations */
395 }
396
397 //=================================================================================
398 // function : ~SMESHGUI_AddMeshElementDlg()
399 // purpose  : Destroys the object and frees any allocated resources
400 //=================================================================================
401 SMESHGUI_AddMeshElementDlg::~SMESHGUI_AddMeshElementDlg()
402 {
403   // no need to delete child widgets, Qt does it all for us
404   delete mySimulation;
405 }
406
407 //=================================================================================
408 // function : Init()
409 // purpose  :
410 //=================================================================================
411 void SMESHGUI_AddMeshElementDlg::Init()
412 {
413   GroupC1->show();
414   Constructor1->setChecked(TRUE);
415   myEditCurrentArgument = LineEditC1A1;
416   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
417
418   myNbOkNodes = 0;
419   myActor = 0;
420
421   /* signals and slots connections */
422   connect(buttonOk, SIGNAL(clicked()),     SLOT(ClickOnOk()));
423   connect(buttonCancel, SIGNAL(clicked()), SLOT(ClickOnCancel()));
424   connect(buttonApply, SIGNAL(clicked()),  SLOT(ClickOnApply()));
425   connect(buttonHelp, SIGNAL(clicked()),   SLOT(ClickOnHelp()));
426
427   connect(SelectButtonC1A1, SIGNAL(clicked()), SLOT(SetEditCurrentArgument()));
428   connect(LineEditC1A1, SIGNAL(textChanged(const QString&)), SLOT(onTextChange(const QString&)));
429   connect(mySMESHGUI, SIGNAL (SignalDeactivateActiveDialog()), SLOT(DeactivateActiveDialog()));
430   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(SelectionIntoArgument()));
431   /* to close dialog if study frame change */
432   connect(mySMESHGUI, SIGNAL (SignalStudyFrameChanged()), SLOT(ClickOnCancel()));
433
434   if (Reverse)
435     connect(Reverse, SIGNAL(stateChanged(int)), SLOT(CheckBox(int)));
436
437   this->show(); // displays Dialog
438
439   // set selection mode
440   SMESH::SetPointRepresentation(true);
441
442   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
443     aViewWindow->SetSelectionMode( NodeSelection );
444
445   myBusy = false;
446
447   SelectionIntoArgument();
448 }
449
450 //=================================================================================
451 // function : ClickOnApply()
452 // purpose  :
453 //=================================================================================
454 void SMESHGUI_AddMeshElementDlg::ClickOnApply()
455 {
456   if (myNbOkNodes && !mySMESHGUI->isActiveStudyLocked()) {
457     myBusy = true;
458     SMESH::long_array_var anArrayOfIdeces = new SMESH::long_array;
459     anArrayOfIdeces->length(myNbNodes);
460     bool reverse = (Reverse && Reverse->isChecked());
461     QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
462     for (int i = 0; i < aListId.count(); i++)
463       if (reverse)
464         anArrayOfIdeces[i] = aListId[ myNbNodes - i - 1 ].toInt();
465       else
466         anArrayOfIdeces[i] = aListId[ i ].toInt();
467
468     SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
469     switch (myElementType) {
470     case SMDSAbs_Edge:
471       aMeshEditor->AddEdge(anArrayOfIdeces.inout()); break;
472     case SMDSAbs_Face:{
473       if(myIsPoly)
474         aMeshEditor->AddPolygonalFace(anArrayOfIdeces.inout());
475       else
476         aMeshEditor->AddFace(anArrayOfIdeces.inout());
477       break;
478
479     }
480     case SMDSAbs_Volume:
481       aMeshEditor->AddVolume(anArrayOfIdeces.inout()); break;
482     default:;
483     }
484
485     SALOME_ListIO aList; aList.Append( myActor->getIO() );
486     mySelector->ClearIndex();
487     mySelectionMgr->setSelectedObjects( aList, false );
488
489     SMESH::UpdateView();
490     mySimulation->SetVisibility(false);
491
492     buttonOk->setEnabled(false);
493     buttonApply->setEnabled(false);
494
495     myEditCurrentArgument->setText("");
496
497     myBusy = false;
498   }
499 }
500
501 //=================================================================================
502 // function : ClickOnOk()
503 // purpose  :
504 //=================================================================================
505 void SMESHGUI_AddMeshElementDlg::ClickOnOk()
506 {
507   this->ClickOnApply();
508   this->ClickOnCancel();
509   return;
510 }
511
512 //=================================================================================
513 // function : ClickOnCancel()
514 // purpose  :
515 //=================================================================================
516 void SMESHGUI_AddMeshElementDlg::ClickOnCancel()
517 {
518   //mySelectionMgr->clearSelected();
519   mySimulation->SetVisibility(false);
520   SMESH::SetPointRepresentation(false);
521   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
522     aViewWindow->SetSelectionMode( ActorSelection );
523   disconnect(mySelectionMgr, 0, this, 0);
524   mySMESHGUI->ResetState();
525   reject();
526   return;
527 }
528
529 //=================================================================================
530 // function : ClickOnHelp()
531 // purpose  :
532 //=================================================================================
533 void SMESHGUI_AddMeshElementDlg::ClickOnHelp()
534 {
535   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
536   if (app) 
537     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
538   else {
539                 QString platform;
540 #ifdef WIN32
541                 platform = "winapplication";
542 #else
543                 platform = "application";
544 #endif
545     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
546                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
547                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(myHelpFileName),
548                            QObject::tr("BUT_OK"));
549   }
550 }
551
552 //=================================================================================
553 // function : onTextChange()
554 // purpose  :
555 //=================================================================================
556 void SMESHGUI_AddMeshElementDlg::onTextChange (const QString& theNewText)
557 {
558   if (myBusy) return;
559   myBusy = true;
560
561   myNbOkNodes = 0;
562
563   buttonOk->setEnabled(false);
564   buttonApply->setEnabled(false);
565
566   mySimulation->SetVisibility(false);
567
568   // hilight entered nodes
569   SMDS_Mesh* aMesh = 0;
570   if (myActor)
571     aMesh = myActor->GetObject()->GetMesh();
572
573   if (aMesh) {
574     TColStd_MapOfInteger newIndices;
575     
576     QStringList aListId = QStringList::split(" ", theNewText, false);
577     bool allOk = true;
578     for (int i = 0; i < aListId.count(); i++) {
579       if( const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() ) )
580       {
581         newIndices.Add( n->GetID() );
582         myNbOkNodes++;
583       }
584       else
585         allOk = false;  
586     }
587     
588     mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
589     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
590       aViewWindow->highlight( myActor->getIO(), true, true );
591     
592     myNbOkNodes = ( allOk && myNbNodes == aListId.count() );
593     
594     if (myIsPoly)
595       {
596         if ( !allOk || myElementType != SMDSAbs_Face || aListId.count() < 3 )
597           myNbOkNodes = 0;
598         else
599           myNbOkNodes = aListId.count();
600       }
601   }
602   
603   if(myNbOkNodes) {
604     buttonOk->setEnabled(true);
605     buttonApply->setEnabled(true);
606     displaySimulation();
607   }
608   
609   myBusy = false;
610 }
611
612 //=================================================================================
613 // function : SelectionIntoArgument()
614 // purpose  : Called when selection has changed
615 //=================================================================================
616 void SMESHGUI_AddMeshElementDlg::SelectionIntoArgument()
617 {
618   if (myBusy) return;
619
620   // clear
621   myNbOkNodes = 0;
622   myActor = 0;
623
624   myBusy = true;
625   myEditCurrentArgument->setText("");
626   myBusy = false;
627
628   if (!GroupButtons->isEnabled()) // inactive
629     return;
630
631   buttonOk->setEnabled(false);
632   buttonApply->setEnabled(false);
633
634   mySimulation->SetVisibility(false);
635 //  SMESH::SetPointRepresentation(true);
636
637   // get selected mesh
638   SALOME_ListIO aList;
639   mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
640
641   if (aList.Extent() != 1)
642     return;
643
644   Handle(SALOME_InteractiveObject) anIO = aList.First();
645   myMesh = SMESH::GetMeshByIO(anIO);
646   if (myMesh->_is_nil())
647     return;
648
649   myActor = SMESH::FindActorByEntry(anIO->getEntry());
650   if (!myActor)
651     return;
652
653   // get selected nodes
654   QString aString = "";
655   int nbNodes = SMESH::GetNameOfSelectedNodes(mySelector,myActor->getIO(),aString);
656   myBusy = true;
657   myEditCurrentArgument->setText(aString);
658   myBusy = false;
659   if (myIsPoly && myElementType == SMDSAbs_Face && nbNodes >= 3 ) {
660     myNbNodes = nbNodes;
661   } else if (myNbNodes != nbNodes) {
662     return;
663   }
664
665   // OK
666   myNbOkNodes = nbNodes;
667
668   buttonOk->setEnabled(true);
669   buttonApply->setEnabled(true);
670
671   displaySimulation();
672 }
673
674 //=================================================================================
675 // function : displaySimulation()
676 // purpose  :
677 //=================================================================================
678 void SMESHGUI_AddMeshElementDlg::displaySimulation()
679 {
680   if (myNbOkNodes && GroupButtons->isEnabled()) {
681     SMESH::TElementSimulation::TVTKIds anIds;
682     QStringList aListId = QStringList::split(" ", myEditCurrentArgument->text(), false);
683     for (int i = 0; i < aListId.count(); i++)
684       anIds.push_back(myActor->GetObject()->GetNodeVTKId(aListId[ i ].toInt()));
685
686     if (Reverse && Reverse->isChecked())
687       reverse(anIds.begin(),anIds.end());
688
689     vtkIdType aType = 0;
690     if (myIsPoly)
691       switch ( myElementType ) {
692       case SMDSAbs_Face  : aType = VTK_POLYGON; break;
693       default: return;
694       }
695     else {
696       switch (myNbNodes) {
697       case 2: aType = VTK_LINE; break;
698       case 3: aType = VTK_TRIANGLE; break;
699       case 4: aType = myElementType == SMDSAbs_Face ? VTK_QUAD : VTK_TETRA; break;
700       case 8: aType = VTK_HEXAHEDRON; break;
701       default: return;
702       }
703     }
704
705     mySimulation->SetPosition(myActor,aType,anIds);
706     SMESH::UpdateView();
707   }
708 }
709
710 //=================================================================================
711 // function : SetEditCurrentArgument()
712 // purpose  :
713 //=================================================================================
714 void SMESHGUI_AddMeshElementDlg::SetEditCurrentArgument()
715 {
716   QPushButton* send = (QPushButton*)sender();
717   if (send == SelectButtonC1A1) {
718     LineEditC1A1->setFocus();
719     myEditCurrentArgument = LineEditC1A1;
720   }
721   SelectionIntoArgument();
722 }
723
724 //=================================================================================
725 // function : DeactivateActiveDialog()
726 // purpose  :
727 //=================================================================================
728 void SMESHGUI_AddMeshElementDlg::DeactivateActiveDialog()
729 {
730   if (GroupConstructors->isEnabled()) {
731     GroupConstructors->setEnabled(false);
732     GroupC1->setEnabled(false);
733     GroupButtons->setEnabled(false);
734     mySimulation->SetVisibility(false);
735     mySMESHGUI->ResetState();
736     mySMESHGUI->SetActiveDialogBox(0);
737   }
738 }
739
740 //=================================================================================
741 // function : ActivateThisDialog()
742 // purpose  :
743 //=================================================================================
744 void SMESHGUI_AddMeshElementDlg::ActivateThisDialog()
745 {
746   /* Emit a signal to deactivate the active dialog */
747   mySMESHGUI->EmitSignalDeactivateDialog();
748
749   GroupConstructors->setEnabled(true);
750   GroupC1->setEnabled(true);
751   GroupButtons->setEnabled(true);
752
753   SMESH::SetPointRepresentation(true);
754
755   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
756     aViewWindow->SetSelectionMode( NodeSelection );
757   SelectionIntoArgument();
758 }
759
760 //=================================================================================
761 // function : enterEvent()
762 // purpose  :
763 //=================================================================================
764 void SMESHGUI_AddMeshElementDlg::enterEvent (QEvent*)
765 {
766   if (GroupConstructors->isEnabled())
767     return;
768   ActivateThisDialog();
769   return;
770 }
771
772 //=================================================================================
773 // function : closeEvent()
774 // purpose  :
775 //=================================================================================
776 void SMESHGUI_AddMeshElementDlg::closeEvent (QCloseEvent*)
777 {
778   /* same than click on cancel button */
779   this->ClickOnCancel();
780   return;
781 }
782
783 //=================================================================================
784 // function : hideEvent()
785 // purpose  : caused by ESC key
786 //=================================================================================
787 void SMESHGUI_AddMeshElementDlg::hideEvent (QHideEvent*)
788 {
789   if (!isMinimized())
790     ClickOnCancel();
791 }
792
793 //=================================================================================
794 // function : CheckBox()
795 // purpose  :
796 //=================================================================================
797 void SMESHGUI_AddMeshElementDlg::CheckBox (int state)
798 {
799   if (!myNbOkNodes)
800     return;
801
802   if (state >= 0) {
803     mySimulation->SetVisibility(false);
804     displaySimulation();
805   }
806 }
807
808 //=================================================================================
809 // function : keyPressEvent()
810 // purpose  :
811 //=================================================================================
812 void SMESHGUI_AddMeshElementDlg::keyPressEvent( QKeyEvent* e )
813 {
814   QDialog::keyPressEvent( e );
815   if ( e->isAccepted() )
816     return;
817   
818   if ( e->key() == Key_F1 )
819     {
820       e->accept();
821       ClickOnHelp();
822     }
823 }