Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreatePolyhedralVolumeDlg.cxx
1 // Copyright (C) 2007-2011  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->SetInput( myGrid );
110
111       myPreviewActor = SALOME_Actor::New();
112       myPreviewActor->PickableOff();
113       myPreviewActor->VisibilityOff();
114       myPreviewActor->SetMapper( myMapper );
115       myPreviewActor->SetRepresentation( 3 );
116
117       vtkFloatingPointType 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       vtkFloatingPointType 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
263   GroupGroupsLayout->addWidget( TextLabel_GroupName );
264   GroupGroupsLayout->addWidget( ComboBox_GroupName, 1 );
265
266   /***************************************************************/
267   GroupButtons = new QGroupBox( this );
268   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
269   GroupButtonsLayout->setSpacing( SPACING );
270   GroupButtonsLayout->setMargin( MARGIN );
271
272   buttonOk = new QPushButton( tr( "SMESH_BUT_APPLY_AND_CLOSE" ), GroupButtons );
273   buttonOk->setAutoDefault( true );
274   buttonOk->setDefault( true );
275   buttonApply = new QPushButton( tr( "SMESH_BUT_APPLY" ), GroupButtons );
276   buttonApply->setAutoDefault( true );
277   buttonCancel = new QPushButton( tr( "SMESH_BUT_CLOSE" ), GroupButtons );
278   buttonCancel->setAutoDefault( true );
279   buttonHelp = new QPushButton( tr("SMESH_BUT_HELP" ), GroupButtons );
280   buttonHelp->setAutoDefault(true);
281
282   GroupButtonsLayout->addWidget( buttonOk );
283   GroupButtonsLayout->addSpacing( 10 );
284   GroupButtonsLayout->addWidget( buttonApply );
285   GroupButtonsLayout->addSpacing( 10 );
286   GroupButtonsLayout->addStretch();
287   GroupButtonsLayout->addWidget( buttonCancel );
288   GroupButtonsLayout->addWidget( buttonHelp);
289
290   /***************************************************************/
291   topLayout->addWidget( ConstructorsBox );
292   topLayout->addWidget( GroupContent );
293   topLayout->addWidget( GroupGroups );
294   topLayout->addWidget( GroupButtons );
295   
296   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
297   
298   RadioButton1->setChecked( true );
299  
300   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
301
302   myHelpFileName = "adding_nodes_and_elements_page.html#adding_polyhedrons_anchor";
303   
304   Init();
305 }
306
307 //=================================================================================
308 // function : ~SMESHGUI_CreatePolyhedralVolumeDlg()
309 // purpose  : Destroys the object and frees any allocated resources
310 //=================================================================================
311 SMESHGUI_CreatePolyhedralVolumeDlg::~SMESHGUI_CreatePolyhedralVolumeDlg()
312 {
313   delete mySimulation;
314 }
315
316 static bool busy = false;
317
318 //=================================================================================
319 // function : Init()
320 // purpose  :
321 //=================================================================================
322 void SMESHGUI_CreatePolyhedralVolumeDlg::Init()
323 {
324   myEditCurrentArgument = LineEditElements;
325   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
326
327   /* reset "Add to group" control */
328   GroupGroups->setChecked( false );
329
330   myNbOkElements = 0;
331   myActor = 0;
332
333   mySimulation = new SMESH::TPolySimulation( dynamic_cast<SalomeApp_Application*>( mySMESHGUI->application() ) );
334
335   /* signals and slots connections */
336   connect(buttonOk,     SIGNAL( clicked() ), SLOT( ClickOnOk() ) );
337   connect(buttonCancel, SIGNAL( clicked() ), SLOT( ClickOnCancel() ) );
338   connect(buttonApply,  SIGNAL( clicked() ), SLOT( ClickOnApply() ) );
339   connect(buttonHelp,   SIGNAL( clicked() ), SLOT( ClickOnHelp() ) );
340
341   connect(GroupConstructors, SIGNAL(buttonClicked(int) ), SLOT( ConstructorsClicked(int) ) );
342   connect(SelectElementsButton, SIGNAL( clicked() ), SLOT( SetEditCurrentArgument() ) );
343   connect(LineEditElements, SIGNAL( textChanged(const QString&) ), SLOT(onTextChange(const QString&)));
344
345   connect(myFacesByNodes, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
346   connect(AddButton, SIGNAL(clicked()), this, SLOT(onAdd()));
347   connect(RemoveButton, SIGNAL(clicked()), this, SLOT(onRemove()));
348   
349   connect( mySMESHGUI, SIGNAL ( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog() ) );
350   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
351   connect( Preview, SIGNAL(toggled(bool)), this, SLOT(ClickOnPreview(bool)));
352   /* to close dialog if study change */
353   connect( mySMESHGUI, SIGNAL ( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
354   
355   ConstructorsClicked(0);
356   SelectionIntoArgument();
357 }
358
359
360 //=================================================================================
361 // function : ConstructorsClicked()
362 // purpose  : Radio button management
363 //=================================================================================
364 void SMESHGUI_CreatePolyhedralVolumeDlg::ConstructorsClicked(int constructorId)
365 {
366   //disconnect(mySelectionMgr, 0, this, 0);
367
368   SALOME_ListIO io;
369   mySelectionMgr->selectedObjects( io );
370   SALOME_ListIO aList;
371   mySelectionMgr->setSelectedObjects( aList );
372   myEditCurrentArgument->clear();
373   myNbOkElements = 0;
374   buttonApply->setEnabled(false);
375   buttonOk->setEnabled(false);
376   mySimulation->SetVisibility(false);
377
378   switch(constructorId)
379     {
380     case 0 :
381       { 
382         if ( myActor ){
383           myActor->SetPointRepresentation(true);
384         }
385         else
386           SMESH::SetPointRepresentation(true);
387         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
388           aViewWindow->SetSelectionMode(NodeSelection);
389         
390         AddButton->setEnabled(false);
391         RemoveButton->setEnabled(false);
392         TextLabelIds->setText( tr( "SMESH_ID_NODES" ) );
393         myFacesByNodesLabel->show();
394         myFacesByNodes->clear();
395         myFacesByNodes->show();
396         AddButton->show();
397         RemoveButton->show();
398         Preview->show();
399         break;
400       }
401     case 1 :
402       {
403         if( myActor ){
404           myActor->SetPointRepresentation(false);
405         } else {
406           SMESH::SetPointRepresentation(false);
407         }
408         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
409           aViewWindow->SetSelectionMode(FaceSelection);
410         
411         TextLabelIds->setText( tr( "SMESH_ID_FACES" ) );
412         myFacesByNodesLabel->hide();
413         myFacesByNodes->hide();
414         AddButton->hide();
415         RemoveButton->hide();
416         Preview->show();
417         break;
418       }
419     }
420   
421   //connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
422   mySelectionMgr->setSelectedObjects( io );
423
424   QApplication::instance()->processEvents();
425   updateGeometry();
426   resize(100,100);
427 }
428
429 //=================================================================================
430 // function : ClickOnPreview()
431 // purpose  :
432 //=================================================================================
433 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnPreview(bool theToggled){
434   Preview->setChecked(theToggled);
435   displaySimulation();
436 }
437
438 //=================================================================================
439 // function : ClickOnApply()
440 // purpose  :
441 //=================================================================================
442 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnApply()
443 {
444   if( !isValid() )
445     return;
446
447   if ( myNbOkElements>0 && !mySMESHGUI->isActiveStudyLocked())
448     {
449       if(checkEditLine(false) == -1) {return;}
450       busy = true;
451       long anElemId = -1;
452
453       bool addToGroup = GroupGroups->isChecked();
454       QString aGroupName;
455       
456       SMESH::SMESH_GroupBase_var aGroup;
457       int idx = 0;
458       if( addToGroup ) {
459         aGroupName = ComboBox_GroupName->currentText();
460         for ( int i = 1; i < ComboBox_GroupName->count(); i++ ) {
461           QString aName = ComboBox_GroupName->itemText( i );
462           if ( aGroupName == aName && ( i == ComboBox_GroupName->currentIndex() || idx == 0 ) )
463             idx = i;
464         }
465         if ( idx > 0 ) {
466           SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( myGroups[idx-1] );
467           if ( !aGeomGroup->_is_nil() ) {
468             int res = SUIT_MessageBox::question( this, tr( "SMESH_WRN_WARNING" ),
469                                                  tr( "MESH_STANDALONE_GRP_CHOSEN" ).arg( aGroupName ),
470                                                  tr( "SMESH_BUT_YES" ), tr( "SMESH_BUT_NO" ), 0, 1 );
471             if ( res == 1 ) return;
472           }
473           aGroup = myGroups[idx-1];
474         }
475       }
476
477       if (GetConstructorId() == 0)
478         {
479           SMESH::long_array_var anIdsOfNodes = new SMESH::long_array;
480           SMESH::long_array_var aQuantities  = new SMESH::long_array;
481
482           aQuantities->length( myFacesByNodes->count() );
483
484           TColStd_ListOfInteger aNodesIds;
485
486           int aNbQuantities = 0;
487           for (int i = 0; i < myFacesByNodes->count(); i++ ) {
488             QStringList anIds = myFacesByNodes->item(i)->text().split( " ", QString::SkipEmptyParts );
489             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
490               aNodesIds.Append( (*it).toInt() );
491
492             aQuantities[aNbQuantities++] = anIds.count();
493           }
494
495           anIdsOfNodes->length(aNodesIds.Extent());
496
497           int aNbIdsOfNodes = 0;
498           TColStd_ListIteratorOfListOfInteger It;
499           It.Initialize(aNodesIds);
500           for( ;It.More();It.Next())
501             anIdsOfNodes[aNbIdsOfNodes++] = It.Value();
502             
503           try{
504             SUIT_OverrideCursor aWaitCursor;
505             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
506             anElemId = aMeshEditor->AddPolyhedralVolume(anIdsOfNodes, aQuantities);
507           }catch(SALOME::SALOME_Exception& exc){
508             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
509           }catch(std::exception& exc){
510             INFOS("Follow exception was cought:\n\t"<<exc.what());
511           }catch(...){
512             INFOS("Unknown exception was cought !!!");
513           }
514         }
515       else if (GetConstructorId() == 1)
516         {
517           SMESH::long_array_var anIdsOfFaces = new SMESH::long_array;
518           
519           QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
520           anIdsOfFaces->length(aListId.count());
521           for ( int i = 0; i < aListId.count(); i++ )
522             anIdsOfFaces[i] = aListId[i].toInt();
523           
524           try{
525             SUIT_OverrideCursor aWaitCursor;
526             SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
527             anElemId = aMeshEditor->AddPolyhedralVolumeByFaces(anIdsOfFaces);
528           }catch(SALOME::SALOME_Exception& exc){
529             INFOS("Follow exception was cought:\n\t"<<exc.details.text);
530           }catch(std::exception& exc){
531             INFOS("Follow exception was cought:\n\t"<<exc.what());
532           }catch(...){
533             INFOS("Unknown exception was cought !!!");
534           }
535         }
536
537       if ( anElemId != -1 && addToGroup && !aGroupName.isEmpty() ) {
538         SMESH::SMESH_Group_var aGroupUsed;
539         if ( aGroup->_is_nil() ) {
540           // create new group 
541           aGroupUsed = SMESH::AddGroup( myMesh, SMESH::VOLUME, aGroupName );
542           if ( !aGroupUsed->_is_nil() ) {
543             myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroupUsed));
544             ComboBox_GroupName->addItem( aGroupName );
545           }
546         }
547         else {
548           SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
549           if ( !aGeomGroup->_is_nil() ) {
550             aGroupUsed = myMesh->ConvertToStandalone( aGeomGroup );
551             if ( !aGroupUsed->_is_nil() && idx > 0 ) {
552               myGroups[idx-1] = SMESH::SMESH_GroupBase::_duplicate(aGroupUsed);
553               SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser();
554             }
555           }
556           else
557             aGroupUsed = SMESH::SMESH_Group::_narrow( aGroup );
558         }
559         
560         if ( !aGroupUsed->_is_nil() ) {
561           SMESH::long_array_var anIdList = new SMESH::long_array;
562           anIdList->length( 1 );
563           anIdList[0] = anElemId;
564           aGroupUsed->Add( anIdList.inout() );
565         }
566       }
567
568       //SALOME_ListIO aList;
569       //mySelectionMgr->setSelectedObjects( aList );
570       SMESH::UpdateView();
571       if( myActor ){
572         unsigned int anEntityMode = myActor->GetEntityMode();
573         myActor->SetEntityMode(SMESH_Actor::eVolumes | anEntityMode);
574       }
575       //ConstructorsClicked( GetConstructorId() );
576       busy = false;
577
578       SMESHGUI::Modified();
579     }
580     myFacesByNodes->clear();
581 }
582
583 //=================================================================================
584 // function : ClickOnOk()
585 // purpose  :
586 //=================================================================================
587 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnOk()
588 {
589   if(checkEditLine(false) == -1) {return;}
590   ClickOnApply();
591   ClickOnCancel();
592 }
593
594         
595 //=================================================================================
596 // function : ClickOnCancel()
597 // purpose  :
598 //=================================================================================
599 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnCancel()
600 {
601   mySelectionMgr->clearFilters();
602   //SALOME_ListIO aList;
603   //mySelectionMgr->setSelectedObjects( aList );
604   SMESH::SetPointRepresentation(false);
605   mySimulation->SetVisibility(false);
606   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
607     aViewWindow->SetSelectionMode( ActorSelection );
608   disconnect( mySelectionMgr, 0, this, 0 );
609   mySMESHGUI->ResetState();
610   reject();
611 }
612
613 //=================================================================================
614 // function : ClickOnHelp()
615 // purpose  :
616 //=================================================================================
617 void SMESHGUI_CreatePolyhedralVolumeDlg::ClickOnHelp()
618 {
619   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
620   if (app) 
621     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
622   else {
623     QString platform;
624 #ifdef WIN32
625     platform = "winapplication";
626 #else
627     platform = "application";
628 #endif
629     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
630                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
631                              arg(app->resourceMgr()->stringValue("ExternalBrowser",
632                                                                  platform)).
633                              arg(myHelpFileName));
634   }
635 }
636
637 //=======================================================================
638 //function : onTextChange
639 //purpose  : 
640 //=======================================================================
641
642 void SMESHGUI_CreatePolyhedralVolumeDlg::onTextChange(const QString& theNewText)
643 {
644   if ( busy ) return;
645   if (checkEditLine() == -1) return;
646   busy = true;
647
648   mySimulation->SetVisibility(false);
649
650   SMDS_Mesh* aMesh = 0;
651   if ( myActor )
652     aMesh = myActor->GetObject()->GetMesh();
653
654   if (GetConstructorId() == 0)
655     {
656       if ( aMesh ) {
657         TColStd_MapOfInteger newIndices;
658       
659         QStringList aListId = theNewText.split( " ", QString::SkipEmptyParts );
660         for ( int i = 0; i < aListId.count(); i++ ) {
661           const SMDS_MeshNode * n = aMesh->FindNode( aListId[ i ].toInt() );
662           if ( n ) {
663             newIndices.Add(n->GetID());
664             myNbOkElements++;
665           }
666         }
667       
668         mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
669       
670         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
671           aViewWindow->highlight( myActor->getIO(), true, true );
672       
673         if ( myNbOkElements>0 && aListId.count()>=3)
674           AddButton->setEnabled(true);
675         else
676           AddButton->setEnabled(false);
677       
678         displaySimulation();
679       }
680     } else if (GetConstructorId() == 1)
681       {
682         myNbOkElements = 0;
683         buttonOk->setEnabled( false );
684         buttonApply->setEnabled( false );
685       
686         // check entered ids of faces and hilight them
687         QStringList aListId;
688         if ( aMesh ) {
689           TColStd_MapOfInteger newIndices;
690       
691           aListId = theNewText.split( " ", QString::SkipEmptyParts );
692
693           for ( int i = 0; i < aListId.count(); i++ ) {
694             const SMDS_MeshElement * e = aMesh->FindElement( aListId[ i ].toInt() );
695             if ( e ) {
696               newIndices.Add(e->GetID());
697               myNbOkElements++;  
698             }
699           }
700
701           mySelector->AddOrRemoveIndex( myActor->getIO(), newIndices, false );
702           if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
703             aViewWindow->highlight( myActor->getIO(), true, true );
704       
705           if ( myNbOkElements ) {
706             if (aListId.count()>1){ 
707               buttonOk->setEnabled( true );
708               buttonApply->setEnabled( true );
709             }
710             else{
711               buttonOk->setEnabled( false );
712               buttonApply->setEnabled( false );
713             }
714             if(aListId.count()>1)
715               displaySimulation();
716           }
717         }
718       }
719   busy = false;
720 }
721
722 //=================================================================================
723 // function : SelectionIntoArgument()
724 // purpose  : Called when selection as changed or other case
725 //=================================================================================
726 void SMESHGUI_CreatePolyhedralVolumeDlg::SelectionIntoArgument()
727 {
728   if ( busy ) return;
729   
730   // clear
731   
732   if (GetConstructorId() == 1 || myFacesByNodes->count() <= 1)
733     {
734       myNbOkElements = 0;
735       AddButton->setEnabled(false);
736       buttonOk->setEnabled( false );
737       buttonApply->setEnabled( false );
738     }
739
740   myActor = 0;
741
742   busy = true;
743   myEditCurrentArgument->setText( "" );
744   busy = false;
745   if ( !GroupButtons->isEnabled() ) // inactive
746     return;
747   
748   mySimulation->SetVisibility(false);
749   
750   QString aCurrentEntry = myEntry;
751
752   // get selected mesh
753   
754   SALOME_ListIO selected;
755   mySelectionMgr->selectedObjects( selected );
756   int nbSel = selected.Extent();
757   if(nbSel != 1){
758     return;
759   }
760   
761   myEntry = selected.First()->getEntry();
762   myMesh = SMESH::GetMeshByIO( selected.First() );
763   if ( myMesh->_is_nil() )
764     return;
765   
766   // process groups
767   if ( !myMesh->_is_nil() && myEntry != aCurrentEntry ) {
768     myGroups.clear();
769     ComboBox_GroupName->clear();
770     ComboBox_GroupName->addItem( QString() );
771     SMESH::ListOfGroups aListOfGroups = *myMesh->GetGroups();
772     for ( int i = 0, n = aListOfGroups.length(); i < n; i++ ) {
773       SMESH::SMESH_GroupBase_var aGroup = aListOfGroups[i];
774       if ( !aGroup->_is_nil() && aGroup->GetType() == SMESH::VOLUME ) {
775         QString aGroupName( aGroup->GetName() );
776         if ( !aGroupName.isEmpty() ) {
777           myGroups.append(SMESH::SMESH_GroupBase::_duplicate(aGroup));
778           ComboBox_GroupName->addItem( aGroupName );
779         }
780       }
781     }
782   }
783
784   myActor = SMESH::FindActorByObject(myMesh);
785   if ( !myActor )
786     return;
787   
788   // get selected nodes/faces
789   QString aString = "";
790   int anbNodes=0,aNbFaces=0;
791   switch(GetConstructorId()){
792   case 0:{
793     anbNodes = SMESH::GetNameOfSelectedNodes(mySelector, myActor->getIO(), aString);
794     if (anbNodes >= 3)
795       AddButton->setEnabled(true);
796     else if (anbNodes < 3){
797       AddButton->setEnabled(false);
798     }
799     busy = true;
800     myEditCurrentArgument->setText( aString );
801     if (checkEditLine() == -1) {busy = false;return;}
802     busy = false;
803     break;
804   }
805   case 1:{
806     // get selected faces
807     aNbFaces = SMESH::GetNameOfSelectedElements(mySelector, myActor->getIO(), aString);
808     if (aNbFaces<=1){
809       buttonOk->setEnabled( false );
810       buttonApply->setEnabled( false );
811     } else {
812       buttonOk->setEnabled( true );
813       buttonApply->setEnabled( true );
814     }
815     busy = true;
816     myEditCurrentArgument->setText( aString );
817     if (checkEditLine() == -1) {busy = false;return;}
818     busy = false;
819     
820     // OK
821     myNbOkElements = 1;
822     break;
823   }
824   default: return;
825   }
826   if(anbNodes>2 || aNbFaces>1)
827     displaySimulation();
828 }
829
830 /*\brief int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine()
831  * Checking of indices in edit line.
832  * If incorecct indices in edit line warning message appear and myEditCurrentArgument remove last index.
833  * \retval 1 - if all ok(or no indices in edit line), -1 - if there are incorrect indices.
834  */
835 int SMESHGUI_CreatePolyhedralVolumeDlg::checkEditLine(bool checkLast)
836 {
837   QString aString = "";
838   SMDS_Mesh* aMesh = 0;
839   
840   if(myMesh->_is_nil()) return 1;
841   if(!myActor){
842     myActor = SMESH::FindActorByObject(myMesh);
843     if(!myActor)
844       return 1;
845   }
846     
847   aMesh = myActor->GetObject()->GetMesh();
848
849   // checking for nodes
850   if (checkLast && myEditCurrentArgument->text().right(1) != QString(" ") ) return 1;
851   QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
852   for ( int i = 0; i < aListId.count(); i++ ){
853     switch (GetConstructorId()){
854     case 0:{ // nodes
855       const SMDS_MeshNode    * aNode = aMesh->FindNode( aListId[ i ].toInt() );
856       if( !aNode ){
857         SUIT_MessageBox::warning(this,
858                                  tr("SMESH_POLYEDRE_CREATE_ERROR"),
859                                  tr("The incorrect indices of nodes!"));
860         
861         myEditCurrentArgument->clear();
862         myEditCurrentArgument->setText( aString );
863         return -1;
864       }
865
866       break;
867     }
868     case 1:{ // faces
869       bool aElemIsOK = true;
870       const SMDS_MeshElement * aElem = aMesh->FindElement( aListId[ i ].toInt() );
871       if (!aElem)
872         {
873           aElemIsOK = false;
874         }
875       else
876         {
877           SMDSAbs_ElementType aType = aMesh->GetElementType( aElem->GetID(),true );
878           if (aType != SMDSAbs_Face){
879             aElemIsOK = false;
880           }
881         }
882       if (!aElemIsOK){
883         SUIT_MessageBox::warning(this,
884                                  tr("SMESH_POLYEDRE_CREATE_ERROR"),
885                                  tr("The incorrect indices of faces!"));
886         
887         myEditCurrentArgument->clear();
888         myEditCurrentArgument->setText( aString );
889         return -1;
890       }
891       break;
892     }
893     }
894     aString += aListId[ i ] + " "; 
895   }
896
897   return 1;
898 }
899
900 //=======================================================================
901 //function : displaySimulation
902 //purpose  : 
903 //=======================================================================
904 void SMESHGUI_CreatePolyhedralVolumeDlg::displaySimulation()
905 {
906   if ( (myNbOkElements || AddButton->isEnabled()) && GroupButtons->isEnabled() && myActor)
907     {
908       SMESH::TPolySimulation::TVTKIds aVTKIds;
909       vtkIdType aType = VTK_CONVEX_POINT_SET;
910       SMDS_Mesh* aMesh = 0;
911       if ( myActor ){
912         aMesh = myActor->GetObject()->GetMesh();
913       }
914       if (GetConstructorId() == 0 && aMesh){
915         if (!AddButton->isEnabled()){
916           mySimulation->ResetGrid(true);
917           for (int i = 0; i < myFacesByNodes->count(); i++) {
918             QStringList anIds = myFacesByNodes->item(i)->text().split( " ", QString::SkipEmptyParts );
919             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
920             for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it){
921               const SMDS_MeshNode* aNode = aMesh->FindNode( (*it).toInt() );
922               if (!aNode) continue;
923               vtkIdType aId = myActor->GetObject()->GetNodeVTKId( (*it).toInt() );
924               aVTKIds.push_back(aId);
925               aVTKIds_faces.push_back(aId);
926             }
927             if(!Preview->isChecked()){
928               aType = VTK_POLYGON;
929               mySimulation->SetPosition(myActor, aType, aVTKIds_faces,false);
930             }
931           }
932           if(myFacesByNodes->count() == 0){
933             mySimulation->SetVisibility(false);
934           } else {
935             mySimulation->SetVisibility(true);
936           }
937           if(Preview->isChecked()){
938             mySimulation->SetPosition(myActor, aType, aVTKIds);
939           }
940         } else {
941           // add ids from edit line
942           QStringList anEditIds = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
943           for ( int i = 0; i < anEditIds.count(); i++ )
944             aVTKIds.push_back( myActor->GetObject()->GetNodeVTKId( anEditIds[ i ].toInt() ));
945           aType = VTK_POLYGON;
946           mySimulation->SetPosition(myActor, aType, aVTKIds);
947         }
948       }else if(GetConstructorId() == 1 && aMesh){
949         QStringList aListId = myEditCurrentArgument->text().split( " ", QString::SkipEmptyParts );
950         for ( int i = 0; i < aListId.count(); i++ )
951           {
952             const SMDS_MeshElement * anElem = aMesh->FindElement( aListId[ i ].toInt() );
953             if ( !anElem ) continue;
954             SMDSAbs_ElementType aFaceType = aMesh->GetElementType( anElem->GetID(),true );
955             if (aFaceType != SMDSAbs_Face) continue;
956               
957             SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
958             SMESH::TPolySimulation::TVTKIds aVTKIds_faces;
959             while( anIter->more() )
960               if ( const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next() ){
961                 vtkIdType aId = myActor->GetObject()->GetNodeVTKId( aNode->GetID() );
962                 aVTKIds.push_back(aId);
963                 aVTKIds_faces.push_back(aId);
964               }
965             if(!Preview->isChecked()){
966               aType = VTK_POLYGON;
967               mySimulation->SetPosition(myActor, aType, aVTKIds_faces);
968             }
969           }
970         if(Preview->isChecked())
971           mySimulation->SetPosition(myActor, aType, aVTKIds);
972       }
973       SMESH::UpdateView();
974     }
975 }
976
977 //=================================================================================
978 // function : SetEditCurrentArgument()
979 // purpose  :
980 //=================================================================================
981 void SMESHGUI_CreatePolyhedralVolumeDlg::SetEditCurrentArgument()
982 {
983   QPushButton* send = (QPushButton*)sender();
984   if(send == SelectElementsButton) {
985     LineEditElements->setFocus();
986     myEditCurrentArgument = LineEditElements;
987   }
988   SelectionIntoArgument();
989 }
990
991 //=================================================================================
992 // function : DeactivateActiveDialog()
993 // purpose  :
994 //=================================================================================
995 void SMESHGUI_CreatePolyhedralVolumeDlg::DeactivateActiveDialog()
996 {
997   if ( ConstructorsBox->isEnabled() ) {
998     ConstructorsBox->setEnabled(false);
999     GroupContent->setEnabled(false);
1000     GroupButtons->setEnabled(false);
1001     mySimulation->SetVisibility(false);
1002     mySMESHGUI->ResetState();    
1003     mySMESHGUI->SetActiveDialogBox(0);
1004   }
1005 }
1006
1007
1008 //=================================================================================
1009 // function : ActivateThisDialog()
1010 // purpose  :
1011 //=================================================================================
1012 void SMESHGUI_CreatePolyhedralVolumeDlg::ActivateThisDialog()
1013 {
1014   /* Emit a signal to deactivate the active dialog */
1015   mySMESHGUI->EmitSignalDeactivateDialog();   
1016   ConstructorsBox->setEnabled(true);
1017   GroupContent->setEnabled(true);
1018   GroupButtons->setEnabled(true);
1019   
1020   mySMESHGUI->SetActiveDialogBox( (QDialog*)this );
1021
1022   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1023     aViewWindow->SetSelectionMode( FaceSelection );
1024   SelectionIntoArgument();
1025 }
1026
1027
1028 //=================================================================================
1029 // function : enterEvent()
1030 // purpose  :
1031 //=================================================================================
1032 void SMESHGUI_CreatePolyhedralVolumeDlg::enterEvent(QEvent* e)
1033 {
1034   if ( ConstructorsBox->isEnabled() )
1035     return;  
1036   ActivateThisDialog();
1037 }
1038
1039
1040 //=================================================================================
1041 // function : closeEvent()
1042 // purpose  :
1043 //=================================================================================
1044 void SMESHGUI_CreatePolyhedralVolumeDlg::closeEvent( QCloseEvent* e )
1045 {
1046   /* same than click on cancel button */
1047   ClickOnCancel();
1048 }
1049
1050
1051 //=======================================================================
1052 //function : hideEvent
1053 //purpose  : caused by ESC key
1054 //=======================================================================
1055
1056 void SMESHGUI_CreatePolyhedralVolumeDlg::hideEvent ( QHideEvent * e )
1057 {
1058   if ( !isMinimized() )
1059     ClickOnCancel();
1060 }
1061
1062
1063 //=================================================================================
1064 // function : GetConstructorId()
1065 // purpose  : 
1066 //=================================================================================
1067 int SMESHGUI_CreatePolyhedralVolumeDlg::GetConstructorId()
1068
1069   return GroupConstructors->checkedId();
1070 }
1071
1072 //=================================================================================
1073 // function : onAdd()
1074 // purpose  :
1075 //=================================================================================
1076 void SMESHGUI_CreatePolyhedralVolumeDlg::onAdd()
1077 {
1078   SALOME_ListIO selected;
1079   mySelectionMgr->selectedObjects( selected );
1080   int aNbSel = selected.Extent();
1081   if (aNbSel == 0 || !myActor || myMesh->_is_nil()) return;
1082   
1083   if (checkEditLine(false) == -1) return;
1084
1085   busy = true;
1086   if ( !(myEditCurrentArgument->text().isEmpty()) )
1087     {
1088       myFacesByNodes->addItem(myEditCurrentArgument->text());
1089       //myFacesByNodes->setSelected(myFacesByNodes->count() - 1, true);
1090       myNbOkElements = 1;
1091       myEditCurrentArgument->clear();
1092       AddButton->setEnabled(false);
1093       buttonApply->setEnabled( myFacesByNodes->count() > 1 );
1094       buttonOk->setEnabled( myFacesByNodes->count() > 1 );
1095     }
1096   busy = false;
1097   onListSelectionChanged();
1098   displaySimulation();
1099 }
1100
1101 //=================================================================================
1102 // function : onRemove()
1103 // purpose  :
1104 //=================================================================================
1105 void SMESHGUI_CreatePolyhedralVolumeDlg::onRemove()
1106 {
1107   busy = true;
1108   QList<QListWidgetItem*> selItems = myFacesByNodes->selectedItems();
1109   QListWidgetItem* anItem;
1110
1111   if ( selItems.count() > 0 ) myNbOkElements = 1;
1112
1113   foreach( anItem, selItems )
1114     delete anItem;
1115
1116   RemoveButton->setEnabled( myFacesByNodes->count() > 0 );
1117   buttonOk->setEnabled( myFacesByNodes->count() > 1 );
1118   buttonApply->setEnabled( myFacesByNodes->count() > 1 );
1119
1120   busy = false;
1121   displaySimulation();
1122 }
1123
1124 //=================================================================================
1125 // function : onListSelectionChanged()
1126 // purpose  : Called when selection in element list is changed
1127 //=================================================================================
1128 void SMESHGUI_CreatePolyhedralVolumeDlg::onListSelectionChanged()
1129 {
1130   if (busy || !myActor) return;
1131   busy = true;
1132
1133   SALOME_ListIO aList;
1134   mySelectionMgr->setSelectedObjects( aList );
1135   TColStd_MapOfInteger aIndexes;
1136
1137   QList<QListWidgetItem*> selItems = myFacesByNodes->selectedItems();
1138   QListWidgetItem* anItem;
1139   foreach( anItem, selItems ) {
1140     QStringList anIds = anItem->text().split( " ", QString::SkipEmptyParts );
1141     for (QStringList::iterator it = anIds.begin(); it != anIds.end(); ++it)
1142       aIndexes.Add((*it).toInt());
1143   }
1144   RemoveButton->setEnabled(selItems.count() > 0);
1145   mySelector->AddOrRemoveIndex(myActor->getIO(), aIndexes, true );
1146   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1147     aViewWindow->highlight( myActor->getIO(), true, true );
1148   mySelectionMgr->clearFilters(); 
1149   aList.Append( myActor->getIO() );
1150   mySelectionMgr->setSelectedObjects( aList );
1151   
1152   busy = false;
1153 }
1154
1155 //=================================================================================
1156 // function : keyPressEvent()
1157 // purpose  :
1158 //=================================================================================
1159 void SMESHGUI_CreatePolyhedralVolumeDlg::keyPressEvent( QKeyEvent* e )
1160 {
1161   QDialog::keyPressEvent( e );
1162   if ( e->isAccepted() )
1163     return;
1164
1165   if ( e->key() == Qt::Key_F1 ) {
1166     e->accept();
1167     ClickOnHelp();
1168   }
1169 }
1170
1171 //=================================================================================
1172 // function : isValid
1173 // purpose  :
1174 //=================================================================================
1175 bool SMESHGUI_CreatePolyhedralVolumeDlg::isValid()
1176 {
1177   if( GroupGroups->isChecked() && ComboBox_GroupName->currentText().isEmpty() ) {
1178     SUIT_MessageBox::warning( this, tr( "SMESH_WRN_WARNING" ), tr( "GROUP_NAME_IS_EMPTY" ) );
1179     return false;
1180   }
1181   return true;
1182 }