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