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