Salome HOME
PR: synchro V7_main tag mergefrom_V6_main_06Mar13
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreatePolyhedralVolumeDlg.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_CreatePolyhedralVolumeDlg.cxx
25 // Author : Michael ZORIN, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_CreatePolyhedralVolumeDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33 #include "SMESHGUI_MeshUtils.h"
34 #include "SMESHGUI_GroupUtils.h"
35 #include "SMESHGUI_IdValidator.h"
36
37 #include <SMESH_Actor.h>
38 #include <SMESH_ActorUtils.h>
39 #include <SMDS_Mesh.hxx>
40
41 // SALOME GUI includes
42 #include <SUIT_Desktop.h>
43 #include <SUIT_ResourceMgr.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_ViewManager.h>
47 #include <SUIT_OverrideCursor.h>
48
49 #include <SalomeApp_Application.h>
50 #include <LightApp_SelectionMgr.h>
51
52 #include <SVTK_ViewWindow.h>
53
54 // OCCT includes
55 #include <TColStd_ListOfInteger.hxx>
56 #include <TColStd_ListIteratorOfListOfInteger.hxx>
57
58 // VTK includes
59 #include <vtkCell.h>
60 #include <vtkIdList.h>
61 #include <vtkUnstructuredGrid.h>
62 #include <vtkDataSetMapper.h>
63 #include <vtkProperty.h>
64
65 // Qt includes
66 #include <QApplication>
67 #include <QButtonGroup>
68 #include <QComboBox>
69 #include <QGroupBox>
70 #include <QLabel>
71 #include <QLineEdit>
72 #include <QPushButton>
73 #include <QRadioButton>
74 #include <QCheckBox>
75 #include <QHBoxLayout>
76 #include <QVBoxLayout>
77 #include <QGridLayout>
78 #include <QListWidget>
79 #include <QKeyEvent>
80
81 // IDL includes
82 #include <SALOMEconfig.h>
83 #include CORBA_SERVER_HEADER(SMESH_MeshEditor)
84
85 #define SPACING 6
86 #define MARGIN  11
87
88 namespace SMESH
89 {
90   class TPolySimulation
91   {
92     SVTK_ViewWindow* myViewWindow;
93
94     SALOME_Actor *myPreviewActor;
95     vtkDataSetMapper* myMapper;
96     vtkUnstructuredGrid* myGrid;
97
98   public:
99
100     TPolySimulation(SalomeApp_Application* app)
101     {
102       SUIT_ViewManager* mgr = app->activeViewManager();
103       myViewWindow = mgr ? dynamic_cast<SVTK_ViewWindow*>( mgr->getActiveView() ) : NULL;
104
105       myGrid = vtkUnstructuredGrid::New();
106   
107       // Create and display actor
108       myMapper = vtkDataSetMapper::New();
109       myMapper->SetInputData( myGrid );
110
111       myPreviewActor = SALOME_Actor::New();
112       myPreviewActor->PickableOff();
113       myPreviewActor->VisibilityOff();
114       myPreviewActor->SetMapper( myMapper );
115       myPreviewActor->SetRepresentation( 3 );
116
117       double anRGB[3];
118       vtkProperty* aProp = vtkProperty::New();
119       GetColor( "SMESH", "selection_element_color", anRGB[0], anRGB[1], anRGB[2], QColor( 0, 170, 255 ) );
120       aProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
121       myPreviewActor->SetProperty( aProp );
122       double aFactor,aUnits;
123       myPreviewActor->SetResolveCoincidentTopology(true);
124       myPreviewActor->GetPolygonOffsetParameters(aFactor,aUnits);
125       myPreviewActor->SetPolygonOffsetParameters(aFactor,0.2*aUnits);
126       aProp->Delete();
127
128       myViewWindow->AddActor( myPreviewActor );
129       
130     }
131
132
133     typedef std::vector<vtkIdType> TVTKIds;
134     void SetPosition(SMESH_Actor* theActor, 
135                      vtkIdType theType, 
136                      const TVTKIds& theIds,
137                      bool theReset=true)
138     {
139       vtkUnstructuredGrid *aGrid = theActor->GetUnstructuredGrid();
140       myGrid->SetPoints(aGrid->GetPoints());
141
142       ResetGrid(theReset);
143       
144       vtkIdList *anIds = vtkIdList::New();
145
146       for (int i = 0, iEnd = theIds.size(); i < iEnd; i++)
147         anIds->InsertId(i,theIds[i]);
148
149       myGrid->InsertNextCell(theType,anIds);
150       if(theIds.size()!=0){
151         myGrid->InsertNextCell(theType,anIds);
152         myGrid->Modified();
153       }
154         
155       anIds->Delete();
156
157       SetVisibility(true);
158
159     }
160   
161     void ResetGrid(bool theReset=true){
162       if (theReset) myGrid->Reset();
163     }
164
165     void SetVisibility(bool theVisibility){
166       myPreviewActor->SetVisibility(theVisibility);
167       RepaintCurrentView();
168     }
169
170
171     ~TPolySimulation(){
172       if( myViewWindow )
173         myViewWindow->RemoveActor(myPreviewActor);
174
175       myPreviewActor->Delete();
176
177       myMapper->RemoveAllInputs();
178       myMapper->Delete();
179
180       myGrid->Delete();
181     }
182
183   };
184 }
185
186 //=================================================================================
187 // class    : SMESHGUI_CreatePolyhedralVolumeDlgDlg()
188 // purpose  : 
189 //=================================================================================
190 SMESHGUI_CreatePolyhedralVolumeDlg::SMESHGUI_CreatePolyhedralVolumeDlg( SMESHGUI* theModule )
191   : QDialog( SMESH::GetDesktop( theModule ) ),
192     mySMESHGUI( theModule ),
193     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
194 {
195   QPixmap image0( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH",tr("ICON_SELECT")));
196
197   setModal(false);
198   setAttribute(Qt::WA_DeleteOnClose, true);
199   setWindowTitle( tr( "SMESH_CREATE_POLYHEDRAL_VOLUME_TITLE" ) );
200   setSizeGripEnabled( true );
201
202   QVBoxLayout* topLayout = new QVBoxLayout( this ); 
203   topLayout->setSpacing( SPACING );
204   topLayout->setMargin( MARGIN );
205
206   /***************************************************************/
207   ConstructorsBox = new QGroupBox(tr( "SMESH_ELEMENTS_TYPE" ), this);
208   GroupConstructors = new QButtonGroup(this);
209   QHBoxLayout* ConstructorsBoxLayout = new QHBoxLayout( ConstructorsBox );
210   ConstructorsBoxLayout->setSpacing( SPACING );
211   ConstructorsBoxLayout->setMargin( MARGIN );
212
213   RadioButton1 = new QRadioButton( tr( "MESH_NODE" ),  ConstructorsBox );
214   RadioButton2 = new QRadioButton( tr( "SMESH_FACE" ), ConstructorsBox );
215
216   ConstructorsBoxLayout->addWidget( RadioButton1 );
217   ConstructorsBoxLayout->addWidget( RadioButton2 );
218   GroupConstructors->addButton(RadioButton1, 0);
219   GroupConstructors->addButton(RadioButton2, 1);
220   
221   /***************************************************************/
222   GroupContent = new QGroupBox( tr( "SMESH_CONTENT" ), this );
223   QGridLayout* GroupContentLayout = new QGridLayout( GroupContent );
224   GroupContentLayout->setSpacing( SPACING );
225   GroupContentLayout->setMargin( MARGIN );
226   
227   TextLabelIds = new QLabel( tr( "SMESH_ID_NODES" ), GroupContent );
228   SelectElementsButton  = new QPushButton( GroupContent );
229   SelectElementsButton->setIcon( image0 );
230   LineEditElements  = new QLineEdit( GroupContent );
231   LineEditElements->setValidator( new SMESHGUI_IdValidator( this ) );
232
233   myFacesByNodesLabel = new QLabel( tr( "FACES_BY_NODES" ), GroupContent );
234   myFacesByNodes = new QListWidget( GroupContent);
235   myFacesByNodes->setSelectionMode( QListWidget::ExtendedSelection );
236   myFacesByNodes->setMinimumHeight( 150);
237
238   AddButton = new QPushButton( tr( "SMESH_BUT_ADD" ), GroupContent );
239   RemoveButton = new QPushButton( tr( "SMESH_BUT_REMOVE" ), GroupContent );
240
241   Preview = new QCheckBox( tr( "SMESH_POLYEDRE_PREVIEW" ), GroupContent );
242
243   GroupContentLayout->addWidget( TextLabelIds,         0, 0 );
244   GroupContentLayout->addWidget( SelectElementsButton, 0, 1 );
245   GroupContentLayout->addWidget( LineEditElements,     0, 2, 1, 2 );
246   GroupContentLayout->addWidget( myFacesByNodesLabel,  1, 0 );
247   GroupContentLayout->addWidget( myFacesByNodes,       2, 0, 3, 3 );
248   GroupContentLayout->addWidget( AddButton,            2, 3 );
249   GroupContentLayout->addWidget( RemoveButton,         3, 3 );
250   GroupContentLayout->addWidget( Preview,              5, 0, 1, 4 );
251
252   /***************************************************************/
253   GroupGroups = new QGroupBox( tr( "SMESH_ADD_TO_GROUP" ), this );
254   GroupGroups->setCheckable( true );
255   QHBoxLayout* GroupGroupsLayout = new QHBoxLayout(GroupGroups);
256   GroupGroupsLayout->setSpacing(SPACING);
257   GroupGroupsLayout->setMargin(MARGIN);
258
259   TextLabel_GroupName = new QLabel( tr( "SMESH_GROUP" ), GroupGroups );
260   ComboBox_GroupName = new QComboBox( GroupGroups );
261   ComboBox_GroupName->setEditable( true );
262   ComboBox_GroupName->setInsertPolicy( QComboBox::NoInsert );
263
264   GroupGroupsLayout->addWidget( TextLabel_GroupName );
265   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
266
267   /***************************************************************/
268   GroupButtons = new QGroupBox( this );
269   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
270   GroupButtonsLayout->setSpacing( SPACING );
271   GroupButtonsLayout->setMargin( MARGIN );
272
273   buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
274   buttonOk->setAutoDefault( true );
275   buttonOk->setDefault( true );
276   buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
277   buttonApply->setAutoDefault( true );
278   buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
279   buttonCancel->setAutoDefault( true );
280   buttonHelp = new QPushButton( tr("SMESH_BUT_HELP" ), GroupButtons );
281   buttonHelp->setAutoDefault(true);
282
283   GroupButtonsLayout->addWidget( buttonOk );
284   GroupButtonsLayout->addSpacing( 10 );
285   GroupButtonsLayout->addWidget( buttonApply );
286   GroupButtonsLayout->addSpacing( 10 );
287   GroupButtonsLayout->addStretch();
288   GroupButtonsLayout->addWidget( buttonCancel );
289   GroupButtonsLayout->addWidget( buttonHelp);
290
291   /***************************************************************/
292   topLayout->addWidget( ConstructorsBox );
293   topLayout->addWidget( GroupContent );
294   topLayout->addWidget( GroupGroups );
295   topLayout->addWidget( GroupButtons );
296   
297   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
298   
299   RadioButton1->setChecked( true );
300  
301   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
302
303   myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
304   
305   Init();
306 }
307
308 //=================================================================================
309 // function : ~SMESHGUI_CreatePolyhedralVolumeDlg()
310 // purpose  : Destroys the object and frees any allocated resources
311 //=================================================================================
312 SMESHGUI_CreatePolyhedralVolumeDlg::~SMESHGUI_CreatePolyhedralVolumeDlg()
313 {
314   delete mySimulation;
315 }
316
317 static bool busy = false;
318
319 //=================================================================================
320 // function : Init()
321 // purpose  :
322 //=================================================================================
323 void SMESHGUI_CreatePolyhedralVolumeDlg::Init()
324 {
325   myEditCurrentArgument = LineEditElements;
326   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
327
328   /* reset "Add to group" control */
329   GroupGroups->setChecked( false );
330
331   myNbOkElements = 0;
332   myActor = 0;
333
334   mySimulation = new SMESH::TPolySimulation( dynamic_cast<SalomeApp_Application*>( mySMESHGUI->application() ) );
335
336   /* signals and slots connections */
337   connect(buttonOk,     SIGNAL( clicked() ), SLOT( ClickOnOk() ) );
338   connect(buttonCancel, SIGNAL( clicked() ), SLOT( reject() ) );
339   connect(buttonApply,  SIGNAL( clicked() ), SLOT( ClickOnApply() ) );
340   connect(buttonHelp,   SIGNAL( clicked() ), SLOT( ClickOnHelp() ) );
341
342   connect(GroupConstructors, SIGNAL(buttonClicked(int) ), SLOT( ConstructorsClicked(int) ) );
343   connect(SelectElementsButton, SIGNAL( clicked() ), SLOT( SetEditCurrentArgument() ) );
344   connect(LineEditElements, SIGNAL( textChanged(const QString&) ), SLOT(onTextChange(const QString&)));
345
346   connect(myFacesByNodes, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
347   connect(AddButton, SIGNAL(clicked()), this, SLOT(onAdd()));
348   connect(RemoveButton, SIGNAL(clicked()), this, SLOT(onRemove()));
349   
350   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
351   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
352   connect( Preview, SIGNAL(toggled(bool)), this, SLOT(ClickOnPreview(bool)));
353   /* to close dialog if study change */
354   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( reject() ) );
355   
356   ConstructorsClicked(0);
357   SelectionIntoArgument();
358 }
359
360
361 //=================================================================================
362 // function : ConstructorsClicked()
363 // purpose  : Radio button management
364 //=================================================================================
365 void SMESHGUI_CreatePolyhedralVolumeDlg::ConstructorsClicked(int constructorId)
366 {
367   //disconnect(mySelectionMgr, 0, this, 0);
368
369   SALOME_ListIO io;
370   mySelectionMgr->selectedObjects( io );
371   SALOME_ListIO aList;
372   mySelectionMgr->setSelectedObjects( aList );
373   myEditCurrentArgument->clear();
374   myNbOkElements = 0;
375   buttonApply->setEnabled(false);
376   buttonOk->setEnabled(false);
377   mySimulation->SetVisibility(false);
378
379   switch(constructorId)
380     {
381     case 0 :
382       { 
383         if ( myActor ){
384           myActor->SetPointRepresentation(true);
385         }
386         else
387           SMESH::SetPointRepresentation(true);
388         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
389           aViewWindow->SetSelectionMode(NodeSelection);
390         
391         AddButton->setEnabled(false);
392         RemoveButton->setEnabled(false);
393         TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
394         myFacesByNodesLabel->show();
395         myFacesByNodes->clear();
396         myFacesByNodes->show();
397         AddButton->show();
398         RemoveButton->show();
399         Preview->show();
400         break;
401       }
402     case 1 :
403       {
404         if( myActor ){
405           myActor->SetPointRepresentation(false);
406         } else {
407           SMESH::SetPointRepresentation(false);
408         }
409         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
410           aViewWindow->SetSelectionMode(FaceSelection);
411         
412         TextLabelIds->setText( tr( "SMESH_ID_FACES" ) );
413         myFacesByNodesLabel->hide();
414         myFacesByNodes->hide();
415         AddButton->hide();
416         RemoveButton->hide();
417         Preview->show();
418         break;
419       }
420     }
421   
422   //connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
423   mySelectionMgr->setSelectedObjects( io );
424
425   QApplication::instance()->processEvents();
426   updateGeometry();
427   resize(100,100);
428 }
429
430 //=================================================================================
431 // function : ClickOnPreview()
432 // purpose  :
433 //=================================================================================
434 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnPreview(bool theToggled){
435   Preview->setChecked(theToggled);
436   displaySimulation();
437 }
438
439 //=================================================================================
440 // function : ClickOnApply()
441 // purpose  :
442 //=================================================================================
443 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnApply()
444 {
445   if( !isValid() )
446     return;
447
448   if ( myNbOkElements>0 && !mySMESHGUI->isActiveStudyLocked())
449     {
450       if(checkEditLine(false) == -1) {return;}
451       busy = true;
452       long anElemId = -1;
453
454       bool addToGroup = GroupGroups->isChecked();
455       QString aGroupName;
456       
457       SMESH::SMESH_GroupBase_var aGroup;
458       int idx = 0;
459       if( addToGroup ) {
460         aGroupName = ComboBox_GroupName->currentText();
461         for ( int i = 1; i <= ComboBox_GroupName->count(); i++ ) {
462           QString aName = ComboBox_GroupName->itemText( i );
463           if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
464             idx = i;
465         }
466         if ( idx > 0 && idx <= myGroups.count() ) {
467           SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
468           if ( !aGeomGroup->_is_nil() ) {
469             int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
470                                                  tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
471                                                  tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
472             if ( res == 1 ) return;
473           }
474           aGroup = myGroups[idx-1];
475         }
476       }
477
478       if (GetConstructorId() == 0)
479         {
480           SMESH::long_array_var anIdsOfNodes = new SMESH::long_array;
481           SMESH::long_array_var aQuantities  = new SMESH::long_array;
482
483           aQuantities->length( myFacesByNodes->count() );
484
485           TColStd_ListOfInteger aNodesIds;
486
487           int aNbQuantities = 0;
488           for (int i = 0; i < myFacesByNodes->count(); i++ ) {
489             QStringList anIds = myFacesByNodes->item(i)->text().split( " ", QString::SkipEmptyParts );
490             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
491               aNodesIds.Append( (*it).toInt() );
492
493             aQuantities[aNbQuantities++] = anIds.count();
494           }
495
496           anIdsOfNodes->length(aNodesIds.Extent());
497
498           int aNbIdsOfNodes = 0;
499           TColStd_ListIteratorOfListOfInteger It;
500           It.Initialize(aNodesIds);
501           for( ;It.More();It.Next())
502             anIdsOfNodes[aNbIdsOfNodes++] = It.Value();
503             
504           try{
505             SUIT_OverrideCursor aWaitCursor;
506             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
507             anElemId = aMeshEditor->AddPolyhedralVolume(anIdsOfNodes, aQuantities);
508           }catch(SALOME::SALOME_Exception& exc){
509             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
510           }catch(std::exception& exc){
511             INFOS("Follow exception was cought:\n\t"<<exc.what());
512           }catch(...){
513             INFOS("Unknown exception was cought !!!");
514           }
515         }
516       else if (GetConstructorId() == 1)
517         {
518           SMESH::long_array_var anIdsOfFaces = new SMESH::long_array;
519           
520           QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
521           anIdsOfFaces->length(aListId.count());
522           for ( int i = 0; i < aListId.count(); i++ )
523             anIdsOfFaces[i] = aListId[i].toInt();
524           
525           try{
526             SUIT_OverrideCursor aWaitCursor;
527             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
528             anElemId = aMeshEditor->AddPolyhedralVolumeByFaces(anIdsOfFaces);
529           }catch(SALOME::SALOME_Exception& exc){
530             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
531           }catch(std::exception& exc){
532             INFOS("Follow exception was cought:\n\t"<<exc.what());
533           }catch(...){
534             INFOS("Unknown exception was cought !!!");
535           }
536         }
537
538       if ( anElemId != -1 && addToGroup && !aGroupName.isEmpty() ) {
539         SMESH::SMESH_Group_var aGroupUsed;
540         if ( aGroup->_is_nil() ) {
541           // create new group 
542           aGroupUsed = SMESH::AddGroup( myMesh, SMESH::VOLUME, aGroupName );
543           if ( !aGroupUsed->_is_nil() ) {
544             myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
545             ComboBox_GroupName->addItem( aGroupName );
546           }
547         }
548         else {
549           SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
550           if ( !aGeomGroup->_is_nil() ) {
551             aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
552             if ( !aGroupUsed->_is_nil() && idx > 0 ) {
553               myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
554               SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
555             }
556           }
557           else
558             aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
559         }
560         
561         if ( !aGroupUsed->_is_nil() ) {
562           SMESH::long_array_var anIdList = new SMESH::long_array;
563           anIdList->length( 1 );
564           anIdList[0] = anElemId;
565           aGroupUsed->Add( anIdList.inout() );
566         }
567       }
568
569       //SALOME_ListIO aList;
570       //mySelectionMgr->setSelectedObjects( aList );
571       SMESH::UpdateView();
572       if( myActor ){
573         unsigned int anEntityMode = myActor->GetEntityMode();
574         myActor->SetEntityMode(SMESH_Actor::eVolumes | anEntityMode);
575       }
576       //ConstructorsClicked( GetConstructorId() );
577       busy = false;
578
579       SMESHGUI::Modified();
580     }
581     myFacesByNodes->clear();
582 }
583
584 //=================================================================================
585 // function : ClickOnOk()
586 // purpose  :
587 //=================================================================================
588 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnOk()
589 {
590   if(checkEditLine(false) == -1) {return;}
591   ClickOnApply();
592   reject();
593 }
594
595         
596 //=================================================================================
597 // function : reject()
598 // purpose  :
599 //=================================================================================
600 void SMESHGUI_CreatePolyhedralVolumeDlg::reject()
601 {
602   mySelectionMgr->clearFilters();
603   //SALOME_ListIO aList;
604   //mySelectionMgr->setSelectedObjects( aList );
605   SMESH::SetPointRepresentation(false);
606   mySimulation->SetVisibility(false);
607   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
608     aViewWindow->SetSelectionMode( ActorSelection );
609   disconnect( mySelectionMgr, 0, this, 0 );
610   mySMESHGUI->ResetState();
611   QDialog::reject();
612 }
613
614 //=================================================================================
615 // function : ClickOnHelp()
616 // purpose  :
617 //=================================================================================
618 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnHelp()
619 {
620   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
621   if (app) 
622     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
623   else {
624     QString platform;
625 #ifdef WIN32
626     platform = "winapplication";
627 #else
628     platform = "application";
629 #endif
630     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
631                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
632                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
633                                                                  platform)).
634                              arg(myHelpFileName));
635   }
636 }
637
638 //=======================================================================
639 //function : onTextChange
640 //purpose  : 
641 //=======================================================================
642
643 void SMESHGUI_CreatePolyhedralVolumeDlg::onTextChange(const QString& theNewText)
644 {
645   if ( busy ) return;
646   if (checkEditLine() == -1) return;
647   busy = true;
648
649   mySimulation->SetVisibility(false);
650
651   SMDS_Mesh* aMesh = 0;
652   if ( myActor )
653     aMesh = myActor->GetObject()->GetMesh();
654
655   if (GetConstructorId() == 0)
656     {
657       if ( aMesh ) {
658         TColStd_MapOfInteger newIndices;
659       
660         QStringList aListId = theNewText.split( " ", QString::SkipEmptyParts );
661         for ( int i = 0; i < aListId.count(); i++ ) {
662           const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
663           if ( n ) {
664             newIndices.Add(n->GetID());
665             myNbOkElements++;
666           }
667         }
668       
669         mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
670       
671         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
672           aViewWindow->highlight( myActor->getIO(), true, true );
673       
674         if ( myNbOkElements>0 && aListId.count()>=3)
675           AddButton->setEnabled(true);
676         else
677           AddButton->setEnabled(false);
678       
679         displaySimulation();
680       }
681     } else if (GetConstructorId() == 1)
682       {
683         myNbOkElements = 0;
684         buttonOk->setEnabled( false );
685         buttonApply->setEnabled( false );
686       
687         // check entered ids of faces and hilight them
688         QStringList aListId;
689         if ( aMesh ) {
690           TColStd_MapOfInteger newIndices;
691       
692           aListId = theNewText.split( " ", QString::SkipEmptyParts );
693
694           for ( int i = 0; i < aListId.count(); i++ ) {
695             const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
696             if ( e ) {
697               newIndices.Add(e->GetID());
698               myNbOkElements++;  
699             }
700           }
701
702           mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
703           if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
704             aViewWindow->highlight( myActor->getIO(), true, true );
705       
706           if ( myNbOkElements ) {
707             if (aListId.count()>1){ 
708               buttonOk->setEnabled( true );
709               buttonApply->setEnabled( true );
710             }
711             else{
712               buttonOk->setEnabled( false );
713               buttonApply->setEnabled( false );
714             }
715             if(aListId.count()>1)
716               displaySimulation();
717           }
718         }
719       }
720   busy = false;
721 }
722
723 //=================================================================================
724 // function : SelectionIntoArgument()
725 // purpose  : Called when selection as changed or other case
726 //=================================================================================
727 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
728 {
729   if ( busy ) return;
730   
731   // clear
732   
733   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
734     {
735       myNbOkElements = 0;
736       AddButton->setEnabled(false);
737       buttonOk->setEnabled( false );
738       buttonApply->setEnabled( false );
739     }
740
741   myActor = 0;
742
743   busy = true;
744   myEditCurrentArgument->setText( "" );
745   busy = false;
746   if ( !GroupButtons->isEnabled() ) // inactive
747     return;
748   
749   mySimulation->SetVisibility(false);
750   
751   QString aCurrentEntry = myEntry;
752
753   // get selected mesh
754   
755   SALOME_ListIO selected;
756   mySelectionMgr->selectedObjects( selected );
757   int nbSel = selected.Extent();
758   if(nbSel != 1){
759     return;
760   }
761   
762   myEntry = selected.First()->getEntry();
763   myMesh = SMESH::GetMeshByIO( selected.First() );
764   if ( myMesh->_is_nil() )
765     return;
766   
767   // process groups
768   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
769     myGroups.clear();
770     ComboBox_GroupName->clear();
771     ComboBox_GroupName->addItem( QString() );
772     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
773     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
774       SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
775       if ( !aGroup->_is_nil() && aGroup->GetType() == SMESH::VOLUME ) {
776         QString aGroupName( aGroup->GetName() );
777         if ( !aGroupName.isEmpty() ) {
778           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
779           ComboBox_GroupName->addItem( aGroupName );
780         }
781       }
782     }
783   }
784
785   myActor = SMESH::FindActorByObject(myMesh);
786   if ( !myActor )
787     return;
788   
789   // get selected nodes/faces
790   QString aString = "";
791   int anbNodes=0,aNbFaces=0;
792   switch(GetConstructorId()){
793   case 0:{
794     anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
795     if (anbNodes >= 3)
796       AddButton->setEnabled(true);
797     else if (anbNodes < 3){
798       AddButton->setEnabled(false);
799     }
800     busy = true;
801     myEditCurrentArgument->setText( aString );
802     if (checkEditLine() == -1) {busy = false;return;}
803     busy = false;
804     break;
805   }
806   case 1:{
807     // get selected faces
808     aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
809     if (aNbFaces<=1){
810       buttonOk->setEnabled( false );
811       buttonApply->setEnabled( false );
812     } else {
813       buttonOk->setEnabled( true );
814       buttonApply->setEnabled( true );
815     }
816     busy = true;
817     myEditCurrentArgument->setText( aString );
818     if (checkEditLine() == -1) {busy = false;return;}
819     busy = false;
820     
821     // OK
822     myNbOkElements = 1;
823     break;
824   }
825   default: return;
826   }
827   if(anbNodes>2 || aNbFaces>1)
828     displaySimulation();
829 }
830
831 /*\brief int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine()
832  * Checking of indices in edit line.
833  * If incorecct indices in edit line warning message appear and myEditCurrentArgument remove last index.
834  * \retval 1 - if all ok(or no indices in edit line), -1 - if there are incorrect indices.
835  */
836 int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine(bool checkLast)
837 {
838   QString aString = "";
839   SMDS_Mesh* aMesh = 0;
840   
841   if(myMesh->_is_nil()) return 1;
842   if(!myActor){
843     myActor = SMESH::FindActorByObject(myMesh);
844     if(!myActor)
845       return 1;
846   }
847     
848   aMesh = myActor->GetObject()->GetMesh();
849
850   // checking for nodes
851   if (checkLast && myEditCurrentArgument->text().right(1) != QString(" ") ) return 1;
852   QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
853   for ( int i = 0; i < aListId.count(); i++ ){
854     switch (GetConstructorId()){
855     case 0:{ // nodes
856       const SMDS_MeshNode    * aNode = aMesh->FindNode( aListId[ i ].toInt() );
857       if( !aNode ){
858         SUIT_MessageBox::warning(this,
859                                  tr("SMESH_POLYEDRE_CREATE_ERROR"),
860                                  tr("The incorrect indices of nodes!"));
861         
862         myEditCurrentArgument->clear();
863         myEditCurrentArgument->setText( aString );
864         return -1;
865       }
866
867       break;
868     }
869     case 1:{ // faces
870       bool aElemIsOK = true;
871       const SMDS_MeshElement * aElem = aMesh->FindElement( aListId[ i ].toInt() );
872       if (!aElem)
873         {
874           aElemIsOK = false;
875         }
876       else
877         {
878           SMDSAbs_ElementType aType = aMesh->GetElementType( aElem->GetID(),true );
879           if (aType != SMDSAbs_Face){
880             aElemIsOK = false;
881           }
882         }
883       if (!aElemIsOK){
884         SUIT_MessageBox::warning(this,
885                                  tr("SMESH_POLYEDRE_CREATE_ERROR"),
886                                  tr("The incorrect indices of faces!"));
887         
888         myEditCurrentArgument->clear();
889         myEditCurrentArgument->setText( aString );
890         return -1;
891       }
892       break;
893     }
894     }
895     aString += aListId[ i ] + " "; 
896   }
897
898   return 1;
899 }
900
901 //=======================================================================
902 //function : displaySimulation
903 //purpose  : 
904 //=======================================================================
905 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
906 {
907   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
908     {
909       SMESH::TPolySimulation::TVTKIds aVTKIds;
910       vtkIdType aType = VTK_CONVEX_POINT_SET;
911       SMDS_Mesh* aMesh = 0;
912       if ( myActor ){
913         aMesh = myActor->GetObject()->GetMesh();
914       }
915       if (GetConstructorId() == 0 && aMesh){
916         if (!AddButton->isEnabled()){
917           mySimulation->ResetGrid(true);
918           for (int i = 0; i < myFacesByNodes->count(); i++) {
919             QStringList anIds = myFacesByNodes->item(i)->text().split( " ", QString::SkipEmptyParts );
920             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
921             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
922               const SMDS_MeshNode* aNode = aMesh->FindNode( (*it).toInt() );
923               if (!aNode) continue;
924               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() );
925               aVTKIds.push_back(aId);
926               aVTKIds_faces.push_back(aId);
927             }
928             if(!Preview->isChecked()){
929               aType = VTK_POLYGON;
930               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
931             }
932           }
933           if(myFacesByNodes->count() == 0){
934             mySimulation->SetVisibility(false);
935           } else {
936             mySimulation->SetVisibility(true);
937           }
938           if(Preview->isChecked()){
939             mySimulation->SetPosition(myActor, aType, aVTKIds);
940           }
941         } else {
942           // add ids from edit line
943           QStringList anEditIds = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
944           for ( int i = 0; i < anEditIds.count(); i++ )
945             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
946           aType = VTK_POLYGON;
947           mySimulation->SetPosition(myActor, aType, aVTKIds);
948         }
949       }else if(GetConstructorId() == 1 && aMesh){
950         QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
951         for ( int i = 0; i < aListId.count(); i++ )
952           {
953             const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
954             if ( !anElem ) continue;
955             SMDSAbs_ElementType aFaceType = aMesh->GetElementType( anElem->GetID(),true );
956             if (aFaceType != SMDSAbs_Face) continue;
957               
958             SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
959             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
960             while( anIter->more() )
961               if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
962                 vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() );
963                 aVTKIds.push_back(aId);
964                 aVTKIds_faces.push_back(aId);
965               }
966             if(!Preview->isChecked()){
967               aType = VTK_POLYGON;
968               mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
969             }
970           }
971         if(Preview->isChecked())
972           mySimulation->SetPosition(myActor, aType, aVTKIds);
973       }
974       SMESH::UpdateView();
975     }
976 }
977
978 //=================================================================================
979 // function : SetEditCurrentArgument()
980 // purpose  :
981 //=================================================================================
982 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
983 {
984   QPushButton* send = (QPushButton*)sender();
985   if(send == SelectElementsButton) {
986     LineEditElements->setFocus();
987     myEditCurrentArgument = LineEditElements;
988   }
989   SelectionIntoArgument();
990 }
991
992 //=================================================================================
993 // function : DeactivateActiveDialog()
994 // purpose  :
995 //=================================================================================
996 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
997 {
998   if ( ConstructorsBox->isEnabled() ) {
999     ConstructorsBox->setEnabled(false);
1000     GroupContent->setEnabled(false);
1001     GroupButtons->setEnabled(false);
1002     mySimulation->SetVisibility(false);
1003     mySMESHGUI->ResetState();    
1004     mySMESHGUI->SetActiveDialogBox(0);
1005   }
1006 }
1007
1008
1009 //=================================================================================
1010 // function : ActivateThisDialog()
1011 // purpose  :
1012 //=================================================================================
1013 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
1014 {
1015   /* Emit a signal to deactivate the active dialog */
1016   mySMESHGUI->EmitSignalDeactivateDialog();   
1017   ConstructorsBox->setEnabled(true);
1018   GroupContent->setEnabled(true);
1019   GroupButtons->setEnabled(true);
1020   
1021   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
1022
1023   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1024     aViewWindow->SetSelectionMode( FaceSelection );
1025   SelectionIntoArgument();
1026 }
1027
1028
1029 //=================================================================================
1030 // function : enterEvent()
1031 // purpose  :
1032 //=================================================================================
1033 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
1034 {
1035   if ( ConstructorsBox->isEnabled() )
1036     return;  
1037   ActivateThisDialog();
1038 }
1039
1040 //=================================================================================
1041 // function : GetConstructorId()
1042 // purpose  : 
1043 //=================================================================================
1044 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
1045
1046   return GroupConstructors->checkedId();
1047 }
1048
1049 //=================================================================================
1050 // function : onAdd()
1051 // purpose  :
1052 //=================================================================================
1053 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
1054 {
1055   SALOME_ListIO selected;
1056   mySelectionMgr->selectedObjects( selected );
1057   int aNbSel = selected.Extent();
1058   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
1059   
1060   if (checkEditLine(false) == -1) return;
1061
1062   busy = true;
1063   if ( !(myEditCurrentArgument->text().isEmpty()) )
1064     {
1065       myFacesByNodes->addItem(myEditCurrentArgument->text());
1066       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
1067       myNbOkElements = 1;
1068       myEditCurrentArgument->clear();
1069       AddButton->setEnabled(false);
1070       buttonApply->setEnabled( myFacesByNodes->count() > 1 );
1071       buttonOk->setEnabled( myFacesByNodes->count() > 1 );
1072     }
1073   busy = false;
1074   onListSelectionChanged();
1075   displaySimulation();
1076 }
1077
1078 //=================================================================================
1079 // function : onRemove()
1080 // purpose  :
1081 //=================================================================================
1082 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
1083 {
1084   busy = true;
1085   QList<QListWidgetItem*> selItems = myFacesByNodes->selectedItems();
1086   QListWidgetItem* anItem;
1087
1088   if ( selItems.count() > 0 ) myNbOkElements = 1;
1089
1090   foreach( anItem, selItems )
1091     delete anItem;
1092
1093   RemoveButton->setEnabled( myFacesByNodes->count() > 0 );
1094   buttonOk->setEnabled( myFacesByNodes->count() > 1 );
1095   buttonApply->setEnabled( myFacesByNodes->count() > 1 );
1096
1097   busy = false;
1098   displaySimulation();
1099 }
1100
1101 //=================================================================================
1102 // function : onListSelectionChanged()
1103 // purpose  : Called when selection in element list is changed
1104 //=================================================================================
1105 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
1106 {
1107   if (busy || !myActor) return;
1108   busy = true;
1109
1110   SALOME_ListIO aList;
1111   mySelectionMgr->setSelectedObjects( aList );
1112   TColStd_MapOfInteger aIndexes;
1113
1114   QList<QListWidgetItem*> selItems = myFacesByNodes->selectedItems();
1115   QListWidgetItem* anItem;
1116   foreach( anItem, selItems ) {
1117     QStringList anIds = anItem->text().split( " ", QString::SkipEmptyParts );
1118     for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
1119       aIndexes.Add((*it).toInt());
1120   }
1121   RemoveButton->setEnabled(selItems.count() > 0);
1122   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
1123   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1124     aViewWindow->highlight( myActor->getIO(), true, true );
1125   mySelectionMgr->clearFilters(); 
1126   aList.Append( myActor->getIO() );
1127   mySelectionMgr->setSelectedObjects( aList );
1128   
1129   busy = false;
1130 }
1131
1132 //=================================================================================
1133 // function : keyPressEvent()
1134 // purpose  :
1135 //=================================================================================
1136 void SMESHGUI_CreatePolyhedralVolumeDlg::keyPressEvent( QKeyEvent* e )
1137 {
1138   QDialog::keyPressEvent( e );
1139   if ( e->isAccepted() )
1140     return;
1141
1142   if ( e->key() == Qt::Key_F1 ) {
1143     e->accept();
1144     ClickOnHelp();
1145   }
1146 }
1147
1148 //=================================================================================
1149 // function : isValid
1150 // purpose  :
1151 //=================================================================================
1152 bool SMESHGUI_CreatePolyhedralVolumeDlg::isValid()
1153 {
1154   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
1155     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
1156     return false;
1157   }
1158   return true;
1159 }