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