Salome HOME
0052235: TC7.3.0: continious fatal errors after splitting into 24 tetrahedra
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MultiEditDlg.cxx
1 // Copyright (C) 2007-2013  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 // File   : SMESHGUI_MultiEditDlg.cxx
24 // Author : Sergey LITONIN, Open CASCADE S.A.S.
25 // SMESH includes
26
27 #include "SMESHGUI_MultiEditDlg.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Filter.h"
31 #include "SMESHGUI_FilterDlg.h"
32 #include "SMESHGUI_Utils.h"
33 #include "SMESHGUI_VTKUtils.h"
34 #include "SMESHGUI_MeshUtils.h"
35 #include "SMESHGUI_FilterUtils.h"
36 #include "SMESHGUI_SpinBox.h"
37 #include "SMESHGUI_MeshEditPreview.h"
38
39 #include <SMESH_Actor.h>
40 #include <SMESH_TypeFilter.hxx>
41 #include <SMDS_Mesh.hxx>
42
43 // SALOME GUI includes
44 #include <SUIT_Desktop.h>
45 #include <SUIT_MessageBox.h>
46 #include <SUIT_OverrideCursor.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_Session.h>
49
50 #include <LightApp_SelectionMgr.h>
51 #include <LightApp_Application.h>
52 #include <SALOME_ListIO.hxx>
53 #include <SALOME_ListIteratorOfListIO.hxx>
54 #include <SalomeApp_Tools.h>
55
56 #include <SVTK_Selector.h>
57 #include <SVTK_ViewWindow.h>
58 #include <VTKViewer_CellLocationsArray.h>
59
60 // OCCT includes
61 #include <TColStd_IndexedMapOfInteger.hxx>
62 #include <TColStd_DataMapOfIntegerInteger.hxx>
63 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
64
65 // VTK includes
66 #include <vtkIdList.h>
67 #include <vtkCellArray.h>
68 #include <vtkUnsignedCharArray.h>
69 #include <vtkUnstructuredGrid.h>
70 #include <vtkDataSetMapper.h>
71 #include <vtkProperty.h>
72
73 // Qt includes
74 #include <QLabel>
75 #include <QVBoxLayout>
76 #include <QHBoxLayout>
77 #include <QGridLayout>
78 #include <QListWidget>
79 #include <QCheckBox>
80 #include <QComboBox>
81 #include <QGroupBox>
82 #include <QLineEdit>
83 #include <QPushButton>
84 #include <QButtonGroup>
85 #include <QRadioButton>
86 #include <QKeyEvent>
87
88 // IDL includes
89 #include <SALOMEconfig.h>
90 #include CORBA_SERVER_HEADER(SMESH_Group)
91
92 #define SPACING 6
93 #define MARGIN  11
94
95 /*!
96  *  Class       : SMESHGUI_MultiEditDlg
97  *  Description : Description : Inversion of the diagonal of a pseudo-quadrangle formed by
98  *                2 neighboring triangles with 1 common edge
99  */
100
101 //=======================================================================
102 // name    : SMESHGUI_MultiEditDlg::SMESHGUI_MultiEditDlg
103 // Purpose : Constructor
104 //=======================================================================
105 SMESHGUI_MultiEditDlg
106 ::SMESHGUI_MultiEditDlg(SMESHGUI* theModule,
107                         const int theMode,
108                         const bool the3d2d):
109   SMESHGUI_PreviewDlg(theModule),
110   mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
111   mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
112   mySMESHGUI(theModule)
113 {
114   setModal(false);
115   setAttribute(Qt::WA_DeleteOnClose, true);
116
117   myFilterDlg = 0;
118   myEntityType = 0;
119
120   myFilterType = theMode;
121   QVBoxLayout* aDlgLay = new QVBoxLayout(this);
122   aDlgLay->setMargin(MARGIN);
123   aDlgLay->setSpacing(SPACING);
124
125   QWidget* aMainFrame = createMainFrame  (this, the3d2d);
126   QWidget* aBtnFrame  = createButtonFrame(this);
127
128   aDlgLay->addWidget(aMainFrame);
129   aDlgLay->addWidget(aBtnFrame);
130
131   Init();
132 }
133
134 //=======================================================================
135 // name    : SMESHGUI_MultiEditDlg::createMainFrame
136 // Purpose : Create frame containing dialog's input fields
137 //=======================================================================
138 QWidget* SMESHGUI_MultiEditDlg::createMainFrame (QWidget* theParent, const bool the3d2d)
139 {
140   QWidget* aMainGrp = new QWidget(theParent);
141   QVBoxLayout* aMainGrpLayout = new QVBoxLayout(aMainGrp);
142   aMainGrpLayout->setMargin(0);
143   aMainGrpLayout->setSpacing(SPACING);
144
145   QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
146
147   // "Selected cells" group
148   mySelGrp = new QGroupBox(aMainGrp);
149   
150   QGridLayout* mySelGrpLayout = new QGridLayout(mySelGrp);
151   mySelGrpLayout->setMargin(MARGIN);
152   mySelGrpLayout->setSpacing(SPACING);
153
154   myEntityTypeGrp = 0;
155   if (the3d2d) {
156     QGroupBox* aEntityTypeGrp = new QGroupBox(tr("SMESH_ELEMENTS_TYPE"), mySelGrp);
157     myEntityTypeGrp = new QButtonGroup(mySelGrp);
158     QHBoxLayout* aEntityLayout = new QHBoxLayout(aEntityTypeGrp);
159     aEntityLayout->setMargin(MARGIN);
160     aEntityLayout->setSpacing(SPACING);
161
162     QRadioButton* aFaceRb = new QRadioButton(tr("SMESH_FACE"), aEntityTypeGrp);
163     QRadioButton* aVolumeRb = new QRadioButton(tr("SMESH_VOLUME"), aEntityTypeGrp);
164
165     aEntityLayout->addWidget(aFaceRb);
166     aEntityLayout->addWidget(aVolumeRb);
167
168     myEntityTypeGrp->addButton(aFaceRb, 0);
169     myEntityTypeGrp->addButton(aVolumeRb, 1);
170     aFaceRb->setChecked(true);
171     myEntityType = myEntityTypeGrp->checkedId();
172
173     mySelGrpLayout->addWidget(aEntityTypeGrp, 0, 0, 1, 2);
174   }
175
176   myListBox = new QListWidget(mySelGrp);
177   myListBox->setSelectionMode(QListWidget::ExtendedSelection);
178   myListBox->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
179   myListBox->installEventFilter(this);
180
181   myFilterBtn = new QPushButton(tr("FILTER"),    mySelGrp);
182   myAddBtn    = new QPushButton(tr("ADD"),       mySelGrp);
183   myRemoveBtn = new QPushButton(tr("REMOVE"),    mySelGrp);
184   mySortBtn   = new QPushButton(tr("SORT_LIST"), mySelGrp);
185
186   int row = mySelGrpLayout->rowCount();
187   mySelGrpLayout->addWidget(myListBox,   row,   0, 6, 1);
188   mySelGrpLayout->addWidget(myFilterBtn, row,   1);
189   mySelGrpLayout->addWidget(myAddBtn,    row+2, 1);
190   mySelGrpLayout->addWidget(myRemoveBtn, row+3, 1);
191   mySelGrpLayout->addWidget(mySortBtn,   row+5, 1);
192   mySelGrpLayout->setRowMinimumHeight(row+1, 10);
193   mySelGrpLayout->setRowMinimumHeight(row+4, 10);
194   mySelGrpLayout->setRowStretch(row+1, 5);
195   mySelGrpLayout->setRowStretch(row+4, 5);
196
197   myToAllChk = new QCheckBox(tr("TO_ALL"), mySelGrp);
198   mySelGrpLayout->addWidget(myToAllChk, mySelGrpLayout->rowCount(), 0, 
199                             1, mySelGrpLayout->columnCount());
200
201   // Split/Join criterion group
202   myCriterionGrp = new QGroupBox(tr("SPLIT_JOIN_CRITERION"), aMainGrp);
203   QVBoxLayout* aCriterionLayout = new QVBoxLayout(myCriterionGrp);
204   aCriterionLayout->setMargin(MARGIN);
205   aCriterionLayout->setSpacing(SPACING);
206   
207   myChoiceWidget = new QWidget(myCriterionGrp);
208   myGroupChoice = new QButtonGroup(myChoiceWidget);
209   QVBoxLayout* aGroupChoiceLayout = new QVBoxLayout(myChoiceWidget);
210   aGroupChoiceLayout->setMargin(0);
211   aGroupChoiceLayout->setSpacing(SPACING);
212
213   QRadioButton* aDiag13RB  = new QRadioButton(tr("USE_DIAGONAL_1_3"), myChoiceWidget);
214   QRadioButton* aDiag24RB  = new QRadioButton(tr("USE_DIAGONAL_2_4"), myChoiceWidget);
215   QRadioButton* aNumFuncRB = new QRadioButton(tr("USE_NUMERIC_FUNC"), myChoiceWidget);
216
217   aGroupChoiceLayout->addWidget(aDiag13RB);
218   aGroupChoiceLayout->addWidget(aDiag24RB);
219   aGroupChoiceLayout->addWidget(aNumFuncRB);
220   myGroupChoice->addButton(aDiag13RB,  0);
221   myGroupChoice->addButton(aDiag24RB,  1);
222   myGroupChoice->addButton(aNumFuncRB, 2);
223   aDiag13RB->setChecked(true);
224
225   myComboBoxFunctor = new QComboBox(myCriterionGrp);
226   myComboBoxFunctor->addItem(tr("ASPECTRATIO_ELEMENTS"));
227   myComboBoxFunctor->addItem(tr("MINIMUMANGLE_ELEMENTS"));
228   myComboBoxFunctor->addItem(tr("SKEW_ELEMENTS"));
229   //myComboBoxFunctor->addItem(tr("AREA_ELEMENTS"));
230   //myComboBoxFunctor->addItem(tr("LENGTH2D_EDGES")); // for existing elements only
231   //myComboBoxFunctor->addItem(tr("MULTI2D_BORDERS")); // for existing elements only
232   myComboBoxFunctor->setCurrentIndex(0);
233
234   aCriterionLayout->addWidget(myChoiceWidget);
235   aCriterionLayout->addWidget(myComboBoxFunctor);
236
237   myCriterionGrp->hide();
238   myChoiceWidget->hide();
239   myComboBoxFunctor->setEnabled(false);
240
241   // "Select from" group
242   QGroupBox* aGrp = new QGroupBox(tr("SELECT_FROM"), aMainGrp);
243   QGridLayout* aGrpLayout = new QGridLayout(aGrp);
244   aGrpLayout->setMargin(MARGIN);
245   aGrpLayout->setSpacing(SPACING);
246
247   mySubmeshChk = new QCheckBox(tr("SMESH_SUBMESH"), aGrp);
248   mySubmeshBtn = new QPushButton(aGrp);
249   mySubmesh = new QLineEdit(aGrp);
250   mySubmesh->setReadOnly(true);
251   mySubmeshBtn->setIcon(aPix);
252
253   myGroupChk = new QCheckBox(tr("SMESH_GROUP"), aGrp);
254   myGroupBtn = new QPushButton(aGrp);
255   myGroup = new QLineEdit(aGrp);
256   myGroup->setReadOnly(true);
257   myGroupBtn->setIcon(aPix);
258
259   aGrpLayout->addWidget(mySubmeshChk,0,0);
260   aGrpLayout->addWidget(mySubmeshBtn,0,1);
261   aGrpLayout->addWidget(mySubmesh,0,2);
262   aGrpLayout->addWidget(myGroupChk,1,0);
263   aGrpLayout->addWidget(myGroupBtn,1,1);
264   aGrpLayout->addWidget(myGroup,1,2);
265   
266   aMainGrpLayout->addWidget(mySelGrp);
267   aMainGrpLayout->addWidget(myCriterionGrp);
268   aMainGrpLayout->addWidget(aGrp);
269
270   return aMainGrp;
271 }
272
273 //=======================================================================
274 // name    : SMESHGUI_MultiEditDlg::createButtonFrame
275 // Purpose : Create frame containing buttons
276 //=======================================================================
277 QWidget* SMESHGUI_MultiEditDlg::createButtonFrame (QWidget* theParent)
278 {
279   QGroupBox* aFrame = new QGroupBox(theParent);
280
281   myOkBtn     = new QPushButton (tr("SMESH_BUT_APPLY_AND_CLOSE"), aFrame);
282   myApplyBtn  = new QPushButton (tr("SMESH_BUT_APPLY"), aFrame);
283   myCloseBtn  = new QPushButton (tr("SMESH_BUT_CLOSE"), aFrame);
284   myHelpBtn   = new QPushButton (tr("SMESH_BUT_HELP"),  aFrame);
285
286   QHBoxLayout* aLay = new QHBoxLayout (aFrame);
287   aLay->setMargin(MARGIN);
288   aLay->setSpacing(SPACING);
289
290   aLay->addWidget(myOkBtn);
291   aLay->addSpacing(10);
292   aLay->addWidget(myApplyBtn);
293   aLay->addSpacing(10);
294   aLay->addStretch();
295   aLay->addWidget(myCloseBtn);
296   aLay->addWidget(myHelpBtn);
297
298   return aFrame;
299 }
300
301 //=======================================================================
302 // name    : SMESHGUI_MultiEditDlg::isValid
303 // Purpose : Verify validity of input data
304 //=======================================================================
305 bool SMESHGUI_MultiEditDlg::isValid (const bool /*theMess*/)
306 {
307   return (!myMesh->_is_nil() &&
308           (myListBox->count() > 0 || (myToAllChk->isChecked()/* && myActor*/)));
309 }
310
311 //=======================================================================
312 // name    : SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg
313 // Purpose : Destructor
314 //=======================================================================
315 SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg()
316 {
317   if (myFilterDlg != 0)
318   {
319     myFilterDlg->setParent(0);
320     delete myFilterDlg;
321   }
322 }
323
324 //=======================================================================
325 // name    : SMESHGUI_MultiEditDlg::eventFilter
326 // Purpose : event filter
327 //=======================================================================
328 bool SMESHGUI_MultiEditDlg::eventFilter (QObject* object, QEvent* event)
329 {
330   if (object == myListBox && event->type() == QEvent::KeyPress) {
331     QKeyEvent* ke = (QKeyEvent*)event;
332     if (ke->key() == Qt::Key_Delete)
333       onRemoveBtn();
334   }
335   return QDialog::eventFilter(object, event);
336 }
337
338 //=======================================================================
339 // name    : SMESHGUI_MultiEditDlg::getNumericalFunctor
340 // Purpose :
341 //=======================================================================
342 SMESH::NumericalFunctor_ptr SMESHGUI_MultiEditDlg::getNumericalFunctor()
343 {
344   SMESH::NumericalFunctor_var aNF = SMESH::NumericalFunctor::_nil();
345
346   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
347   if (aFilterMgr->_is_nil())
348     return aNF._retn();
349
350   if (myComboBoxFunctor->currentText() == tr("ASPECTRATIO_ELEMENTS"))
351     aNF = aFilterMgr->CreateAspectRatio();
352   else if (myComboBoxFunctor->currentText() == tr("WARP_ELEMENTS"))
353     aNF = aFilterMgr->CreateWarping();
354   else if (myComboBoxFunctor->currentText() == tr("MINIMUMANGLE_ELEMENTS"))
355     aNF = aFilterMgr->CreateMinimumAngle();
356   else if (myComboBoxFunctor->currentText() == tr("TAPER_ELEMENTS"))
357     aNF = aFilterMgr->CreateTaper();
358   else if (myComboBoxFunctor->currentText() == tr("SKEW_ELEMENTS"))
359     aNF = aFilterMgr->CreateSkew();
360   else if (myComboBoxFunctor->currentText() == tr("AREA_ELEMENTS"))
361     aNF = aFilterMgr->CreateArea();
362   else if (myComboBoxFunctor->currentText() == tr("LENGTH2D_EDGES"))
363     aNF = aFilterMgr->CreateLength2D();
364   else if (myComboBoxFunctor->currentText() == tr("MULTI2D_BORDERS"))
365     aNF = aFilterMgr->CreateMultiConnection2D();
366   else if (myComboBoxFunctor->currentText() == tr("MIN_DIAG_ELEMENTS"))
367     aNF = aFilterMgr->CreateMaxElementLength2D();
368   else;
369
370   return aNF._retn();
371 }
372
373 //=======================================================================
374 // name    : SMESHGUI_MultiEditDlg::Init
375 // Purpose : Init dialog fields, connect signals and slots, show dialog
376 //=======================================================================
377 void SMESHGUI_MultiEditDlg::Init()
378 {
379   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
380   myListBox->clear();
381   myIds.Clear();
382   myBusy = false;
383   myActor = 0;
384   emit ListContensChanged();
385
386   // main buttons
387   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
388   connect(myCloseBtn, SIGNAL(clicked()), SLOT(reject()));
389   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
390   connect(myHelpBtn,  SIGNAL(clicked()), SLOT(onHelp()));
391
392   // selection and SMESHGUI
393   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
394   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
395   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(reject()));
396
397   // dialog controls
398   connect(myFilterBtn, SIGNAL(clicked()), SLOT(onFilterBtn()  ));
399   connect(myAddBtn,    SIGNAL(clicked()), SLOT(onAddBtn()     ));
400   connect(myRemoveBtn, SIGNAL(clicked()), SLOT(onRemoveBtn()  ));
401   connect(mySortBtn,   SIGNAL(clicked()), SLOT(onSortListBtn()));
402
403   connect(mySubmeshChk, SIGNAL(stateChanged(int)), SLOT(onSubmeshChk()));
404   connect(myGroupChk,   SIGNAL(stateChanged(int)), SLOT(onGroupChk()  ));
405   connect(myToAllChk,   SIGNAL(stateChanged(int)), SLOT(onToAllChk()  ));
406
407   if (myEntityTypeGrp)
408     connect(myEntityTypeGrp, SIGNAL(buttonClicked(int)), SLOT(on3d2dChanged(int)));
409
410   connect(myListBox, SIGNAL(itemSelectionChanged()), SLOT(onListSelectionChanged()));
411
412   onSelectionDone();
413
414   // set selection mode
415   setSelectionMode();
416   updateButtons();
417 }
418
419 //=======================================================================
420 // name    : SMESHGUI_MultiEditDlg::onOk
421 // Purpose : SLOT called when "Ok" button pressed.
422 //           Assign filters VTK viewer and close dialog
423 //=======================================================================
424 void SMESHGUI_MultiEditDlg::onOk()
425 {
426   if (onApply())
427     reject();
428 }
429
430 //=======================================================================
431 // name    : SMESHGUI_MultiEditDlg::getIds
432 // Purpose : Retrive identifiers from list box or the whole object
433 //=======================================================================
434
435 SMESH::long_array_var SMESHGUI_MultiEditDlg::getIds(SMESH::SMESH_IDSource_var& obj)
436 {
437   SMESH::long_array_var anIds = new SMESH::long_array;
438
439   if (myToAllChk->isChecked())
440   {
441     myIds.Clear();
442     obj = SMESH::SMESH_IDSource::_narrow( myMesh );
443   }
444
445   anIds->length(myIds.Extent());
446   TColStd_MapIteratorOfMapOfInteger anIter(myIds);
447   for (int i = 0; anIter.More(); anIter.Next() )
448   {
449     anIds[ i++ ] = anIter.Key();
450   }
451   return anIds._retn();
452 }
453
454 //=======================================================================
455 // name    : SMESHGUI_MultiEditDlg::reject
456 // Purpose : SLOT called when "Close" button pressed. Close dialog
457 //=======================================================================
458 void SMESHGUI_MultiEditDlg::reject()
459 {
460   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
461     aViewWindow->SetSelectionMode(ActorSelection);
462   disconnect(mySelectionMgr, 0, this, 0);
463   disconnect(mySMESHGUI, 0, this, 0);
464   mySMESHGUI->ResetState();
465
466   SMESH::RemoveFilters();
467   SMESH::SetPickable();
468
469   //mySelectionMgr->clearSelected();
470   mySelectionMgr->clearFilters();
471
472   QDialog::reject();
473 }
474
475 //=================================================================================
476 // function : onHelp()
477 // purpose  :
478 //=================================================================================
479 void SMESHGUI_MultiEditDlg::onHelp()
480 {
481   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
482   if (app) 
483     app->onHelpContextModule(mySMESHGUI ? app->moduleName(mySMESHGUI->moduleName()) : QString(""), myHelpFileName);
484   else {
485     QString platform;
486 #ifdef WIN32
487     platform = "winapplication";
488 #else
489     platform = "application";
490 #endif
491     SUIT_MessageBox::warning(this, tr("WRN_WARNING"),
492                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
493                              arg(app->resourceMgr()->stringValue("ExternalBrowser", 
494                                                                  platform)).
495                              arg(myHelpFileName));
496   }
497 }
498
499 //=======================================================================
500 // name    : SMESHGUI_MultiEditDlg::onSelectionDone
501 // Purpose : SLOT called when selection changed
502 //=======================================================================
503 void SMESHGUI_MultiEditDlg::onSelectionDone()
504 {
505   if (myBusy || !isEnabled()) return;
506   myBusy = true;
507
508   const SALOME_ListIO& aList = mySelector->StoredIObjects();
509
510   int nbSel = aList.Extent();
511   myListBox->clearSelection();
512
513   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
514     QLineEdit* aNameEdit = mySubmeshChk->isChecked() ? mySubmesh : myGroup;
515     if (nbSel == 1) {
516       Handle(SALOME_InteractiveObject) anIO = aList.First();
517       QString aName = "";
518       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
519       anIO.IsNull() ? aNameEdit->clear() : aNameEdit->setText(aName);
520
521       if (mySubmeshChk->isChecked()) {
522         SMESH::SMESH_subMesh_var aSubMesh =
523           SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIO);
524         if (!aSubMesh->_is_nil())
525           myMesh = aSubMesh->GetFather();
526       } else {
527         SMESH::SMESH_GroupBase_var aGroup =
528           SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIO);
529         if (!aGroup->_is_nil())
530           myMesh = aGroup->GetMesh();
531       }
532     } else if (nbSel > 1) {
533       QString aStr = mySubmeshChk->isChecked() ?
534         tr("SMESH_SUBMESH_SELECTED") : tr("SMESH_GROUP_SELECTED");
535       aNameEdit->setText(aStr.arg(nbSel));
536     } else {
537       aNameEdit->clear();
538     }
539   } else if (nbSel > 0) {
540     QString aListStr = "";
541     Handle(SALOME_InteractiveObject) anIO = aList.First();
542     int aNbItems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aListStr);
543     if (aNbItems > 0) {
544       QStringList anElements = aListStr.split(" ", QString::SkipEmptyParts);
545       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
546         QList<QListWidgetItem*> items = myListBox->findItems(*it, Qt::MatchExactly);
547         QListWidgetItem* anItem;
548         foreach(anItem, items)
549           anItem->setSelected(true);
550       }
551     }
552     SMESH::SMESH_Mesh_var aSelMesh = SMESH::GetMeshByIO(anIO);
553     if (!aSelMesh->_is_nil())
554       myMesh = aSelMesh;
555   }
556
557   if (nbSel > 0) {
558     myActor = SMESH::FindActorByEntry(aList.First()->getEntry());
559     if (!myActor)
560       myActor = SMESH::FindActorByObject(myMesh);
561     SVTK_Selector* aSelector = SMESH::GetSelector();
562     Handle(VTKViewer_Filter) aFilter = aSelector->GetFilter(myFilterType);
563     if (!aFilter.IsNull())
564       aFilter->SetActor(myActor);
565   }
566   myBusy = false;
567
568   updateButtons();
569 }
570
571 //=======================================================================
572 // name    : SMESHGUI_MultiEditDlg::onDeactivate
573 // Purpose : SLOT called when dialog must be deativated
574 //=======================================================================
575 void SMESHGUI_MultiEditDlg::onDeactivate()
576 {
577   setEnabled(false);
578 }
579
580 //=======================================================================
581 // name    : SMESHGUI_MultiEditDlg::enterEvent
582 // Purpose : Event filter
583 //=======================================================================
584 void SMESHGUI_MultiEditDlg::enterEvent (QEvent*)
585 {
586   if (!isEnabled()) {
587     mySMESHGUI->EmitSignalDeactivateDialog();
588     setEnabled(true);
589     setSelectionMode();
590   }
591 }
592
593 //=======================================================================
594 // name    : SMESHGUI_MultiEditDlg::onFilterBtn
595 // Purpose : SLOT. Called when "Filter" button pressed.
596 //           Start "Selection filters" dialog
597 //=======================================================================
598 void SMESHGUI_MultiEditDlg::onFilterBtn()
599 {
600   if (myFilterDlg == 0) {
601     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, entityType() ? SMESH::VOLUME : SMESH::FACE);
602     connect(myFilterDlg, SIGNAL(Accepted()), SLOT(onFilterAccepted()));
603   } else {
604     myFilterDlg->Init(entityType() ? SMESH::VOLUME : SMESH::FACE);
605   }
606
607   myFilterDlg->SetSelection();
608   myFilterDlg->SetMesh(myMesh);
609   myFilterDlg->SetSourceWg(myListBox, false);
610
611   myFilterDlg->show();
612 }
613
614 //=======================================================================
615 // name    : onFilterAccepted()
616 // Purpose : SLOT. Called when Filter dlg closed with OK button.
617 //            Uncheck "Select submesh" and "Select group" checkboxes
618 //=======================================================================
619 void SMESHGUI_MultiEditDlg::onFilterAccepted()
620 {
621   myIds.Clear();
622   for (int i = 0, n = myListBox->count(); i < n; i++)
623     myIds.Add(myListBox->item(i)->text().toInt());
624
625   emit ListContensChanged();
626
627   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
628     mySubmeshChk->blockSignals(true);
629     myGroupChk->blockSignals(true);
630     mySubmeshChk->setChecked(false);
631     myGroupChk->setChecked(false);
632     mySubmeshChk->blockSignals(false);
633     myGroupChk->blockSignals(false);
634   }
635   updateButtons();
636 }
637
638 //=======================================================================
639 // name    : SMESHGUI_MultiEditDlg::isIdValid
640 // Purpose : Verify whether Id of element satisfies to filters from viewer
641 //=======================================================================
642 bool SMESHGUI_MultiEditDlg::isIdValid (const int theId) const
643 {
644   SVTK_Selector* aSelector = SMESH::GetSelector();
645   Handle(SMESHGUI_Filter) aFilter =
646     Handle(SMESHGUI_Filter)::DownCast(aSelector->GetFilter(myFilterType));
647
648   return (!aFilter.IsNull() && aFilter->IsObjValid(theId));
649 }
650
651 //=======================================================================
652 // name    : SMESHGUI_MultiEditDlg::onAddBtn
653 // Purpose : SLOT. Called when "Add" button pressed.
654 //           Add selected in viewer entities in list box
655 //=======================================================================
656 void SMESHGUI_MultiEditDlg::onAddBtn()
657 {
658   const SALOME_ListIO& aList = mySelector->StoredIObjects();
659
660   int nbSelected = aList.Extent();
661   if (nbSelected == 0)
662     return;
663
664   TColStd_IndexedMapOfInteger toBeAdded;
665
666   if (!mySubmeshChk->isChecked() && !myGroupChk->isChecked()) {
667     if (nbSelected > 0)
668       mySelector->GetIndex(aList.First(),toBeAdded);
669   } else if (mySubmeshChk->isChecked()) {
670     SALOME_ListIteratorOfListIO anIter(aList);
671     for ( ; anIter.More(); anIter.Next()) {
672       SMESH::SMESH_subMesh_var aSubMesh =
673         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIter.Value());
674       if (!aSubMesh->_is_nil()) {
675         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
676           SMESH::long_array_var anIds = aSubMesh->GetElementsId();
677           for (int i = 0, n = anIds->length(); i < n; i++) {
678             if (isIdValid(anIds[ i ]))
679               toBeAdded.Add(anIds[ i ]);
680           }
681         }
682       }
683     }
684   } else if (myGroupChk->isChecked()) {
685     SALOME_ListIteratorOfListIO anIter(aList);
686     for ( ; anIter.More(); anIter.Next()) {
687       SMESH::SMESH_GroupBase_var aGroup =
688         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
689       if (!aGroup->_is_nil() && ((aGroup->GetType() == SMESH::FACE && entityType() == 0) || 
690                                  (aGroup->GetType() == SMESH::VOLUME && entityType() == 1))) {
691         if (aGroup->GetMesh()->GetId() == myMesh->GetId()) {
692           SMESH::long_array_var anIds = aGroup->GetListOfID();
693           for (int i = 0, n = anIds->length(); i < n; i++) {
694             if (isIdValid(anIds[ i ]))
695               toBeAdded.Add(anIds[ i ]);
696           }
697         }
698       }
699     }
700   } else {
701   }
702
703   myBusy = true;
704   bool isGroupOrSubmesh = (mySubmeshChk->isChecked() || myGroupChk->isChecked());
705   mySubmeshChk->setChecked(false);
706   myGroupChk->setChecked(false);
707   QStringList items;
708   for(int i = 1; i <= toBeAdded.Extent(); i++)
709     if (myIds.Add(toBeAdded(i))) {
710       items.append(QString("%1").arg(toBeAdded(i)));
711     }
712   myListBox->addItems(items);
713   myListBox->selectAll();
714   myBusy = false;
715
716   emit ListContensChanged();
717
718   if (isGroupOrSubmesh)
719     onListSelectionChanged();
720
721   updateButtons();
722 }
723
724 //=======================================================================
725 // name    : SMESHGUI_MultiEditDlg::updateButtons
726 // Purpose : Enable/disable buttons of dialog in accordance with current state
727 //=======================================================================
728 void SMESHGUI_MultiEditDlg::updateButtons()
729 {
730   bool isOk = isValid(false);
731   myOkBtn->setEnabled(isOk);
732   myApplyBtn->setEnabled(isOk);
733
734   bool isListBoxNonEmpty = myListBox->count() > 0;
735   bool isToAll = myToAllChk->isChecked();
736   myFilterBtn->setEnabled(!isToAll);
737   myRemoveBtn->setEnabled(isListBoxNonEmpty && !isToAll);
738   mySortBtn->setEnabled(isListBoxNonEmpty &&!isToAll);
739
740   const SALOME_ListIO& aList = mySelector->StoredIObjects();
741
742   if (isToAll ||
743       myMesh->_is_nil() ||
744       aList.Extent() < 1 ||
745       (SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(aList.First())->_is_nil() &&
746        SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First())->_is_nil() &&
747        SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(aList.First())->_is_nil()))
748     myAddBtn->setEnabled(false);
749   else
750     myAddBtn->setEnabled(true);
751
752   mySubmeshChk->setEnabled(!isToAll);
753   mySubmeshBtn->setEnabled(mySubmeshChk->isChecked());
754   mySubmesh->setEnabled(mySubmeshChk->isChecked());
755
756   myGroupChk->setEnabled(!isToAll);
757   myGroupBtn->setEnabled(myGroupChk->isChecked());
758   myGroup->setEnabled(myGroupChk->isChecked());
759
760   if (!mySubmeshChk->isChecked())
761     mySubmesh->clear();
762   if (!myGroupChk->isChecked())
763     myGroup->clear();
764
765 }
766
767 //=======================================================================
768 // name    : SMESHGUI_MultiEditDlg::onRemoveBtn
769 // Purpose : SLOT. Called when "Remove" button pressed.
770 //           Remove selected in list box entities
771 //=======================================================================
772 void SMESHGUI_MultiEditDlg::onRemoveBtn()
773 {
774   myBusy = true;
775
776   QList<QListWidgetItem*> selItems = myListBox->selectedItems();
777   QListWidgetItem* item;
778   foreach(item, selItems)
779   {
780     myIds.Remove(item->text().toInt());
781     delete item;
782   }
783
784   myBusy = false;
785
786   emit ListContensChanged();
787   updateButtons();
788 }
789
790 //=======================================================================
791 // name    : SMESHGUI_MultiEditDlg::onSortListBtn
792 // Purpose : SLOT. Called when "Sort list" button pressed.
793 //           Sort entities of list box
794 //=======================================================================
795 void SMESHGUI_MultiEditDlg::onSortListBtn()
796 {
797   myBusy = true;
798
799   int i, k = myListBox->count();
800   if (k > 0)
801   {
802     QList<int> aSelected;
803     std::vector<int> anArray(k);
804     for (i = 0; i < k; i++)
805     {
806       int id = myListBox->item(i)->text().toInt();
807       anArray[ i ] = id;
808       if (myListBox->item(i)->isSelected())
809         aSelected.append(id);
810     }
811
812     std::sort(anArray.begin(), anArray.end());
813
814     myListBox->clear();
815     for (i = 0; i < k; i++) {
816       QListWidgetItem* item = new QListWidgetItem(QString::number(anArray[i]));
817       myListBox->addItem(item);
818       item->setSelected(aSelected.contains(anArray[i]));
819     }
820   }
821   myBusy = false;
822 }
823
824 //=======================================================================
825 // name    : SMESHGUI_MultiEditDlg::onListSelectionChanged
826 // Purpose : SLOT. Called when selection in list box changed.
827 //           Highlight in selected entities
828 //=======================================================================
829 void SMESHGUI_MultiEditDlg::onListSelectionChanged()
830 {
831   if (myActor == 0 || myBusy)
832     return;
833
834   if (mySubmeshChk->isChecked() || myGroupChk->isChecked())
835     return;
836
837   SMESH_Actor * anActor = SMESH::FindActorByObject(myMesh);
838   if (!anActor)
839     anActor = myActor;
840   TVisualObjPtr anObj = anActor->GetObject();
841
842   TColStd_MapOfInteger anIndexes;
843   int total = myListBox->count();
844   for (int i = 0; i < total; i++)
845   {
846     if (myListBox->item(i)->isSelected())
847     {
848       int anId = myListBox->item(i)->text().toInt();
849       if (anObj->GetElemVTKId(anId) >= 0) // avoid exception in hilight
850         anIndexes.Add(anId);
851     }
852   }
853
854   mySelector->AddOrRemoveIndex(anActor->getIO(),anIndexes,false);
855   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
856     aViewWindow->highlight(anActor->getIO(),true,true);
857 }
858
859 //=======================================================================
860 // name    : SMESHGUI_MultiEditDlg::onSubmeshChk
861 // Purpose : SLOT. Called when state of "SubMesh" check box changed.
862 //           Activate/deactivate selection of submeshes
863 //=======================================================================
864 void SMESHGUI_MultiEditDlg::onSubmeshChk()
865 {
866   bool isChecked = mySubmeshChk->isChecked();
867   mySubmeshBtn->setEnabled(isChecked);
868   mySubmesh->setEnabled(isChecked);
869   if (!isChecked)
870     mySubmesh->clear();
871   if (isChecked && myGroupChk->isChecked())
872       myGroupChk->setChecked(false);
873
874   setSelectionMode();
875 }
876
877 //=======================================================================
878 // name    : SMESHGUI_MultiEditDlg::onGroupChk
879 // Purpose : SLOT. Called when state of "Group" check box changed.
880 //           Activate/deactivate selection of groupes
881 //=======================================================================
882 void SMESHGUI_MultiEditDlg::onGroupChk()
883 {
884   bool isChecked = myGroupChk->isChecked();
885   myGroupBtn->setEnabled(isChecked);
886   myGroup->setEnabled(isChecked);
887   if (!isChecked)
888     myGroup->clear();
889   if (isChecked && mySubmeshChk->isChecked())
890       mySubmeshChk->setChecked(false);
891
892   setSelectionMode();
893 }
894
895 //=======================================================================
896 // name    : SMESHGUI_MultiEditDlg::onToAllChk
897 // Purpose : SLOT. Called when state of "Apply to all" check box changed.
898 //           Activate/deactivate selection
899 //=======================================================================
900 void SMESHGUI_MultiEditDlg::onToAllChk()
901 {
902   bool isChecked = myToAllChk->isChecked();
903
904   if (isChecked)
905     myListBox->clear();
906
907   myIds.Clear();
908
909   emit ListContensChanged();
910
911   updateButtons();
912   setSelectionMode();
913 }
914
915 //=======================================================================
916 // name    : SMESHGUI_MultiEditDlg::setSelectionMode
917 // Purpose : Set selection mode
918 //=======================================================================
919 void SMESHGUI_MultiEditDlg::setSelectionMode()
920 {
921   SMESH::RemoveFilters();
922
923   //  mySelectionMgr->clearSelected();
924   mySelectionMgr->clearFilters();
925
926   if (mySubmeshChk->isChecked()) {
927     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
928       aViewWindow->SetSelectionMode(ActorSelection);
929     mySelectionMgr->installFilter(new SMESH_TypeFilter(SMESH::SUBMESH));
930   }
931   else if (myGroupChk->isChecked()) {
932     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
933       aViewWindow->SetSelectionMode(ActorSelection);
934     mySelectionMgr->installFilter(new SMESH_TypeFilter(SMESH::GROUP));
935   }
936
937   if (entityType()) {
938     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
939       aViewWindow->SetSelectionMode(VolumeSelection);
940     SMESH::SetFilter(new SMESHGUI_VolumesFilter());
941   } else {
942     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
943       aViewWindow->SetSelectionMode(FaceSelection);
944     if (myFilterType == SMESH::TriaFilter)
945       SMESH::SetFilter(new SMESHGUI_TriangleFilter());
946     else if (myFilterType == SMESH::QuadFilter)
947       SMESH::SetFilter(new SMESHGUI_QuadrangleFilter());
948     else
949       SMESH::SetFilter(new SMESHGUI_FacesFilter());
950   }
951 }
952
953 //=======================================================================
954 // name    : SMESHGUI_MultiEditDlg::onApply
955 // Purpose : SLOT. Called when "Apply" button clicked.
956 //=======================================================================
957 bool SMESHGUI_MultiEditDlg::onApply()
958 {
959   if (mySMESHGUI->isActiveStudyLocked())
960     return false;
961   if (!isValid(true))
962     return false;
963
964   SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
965   if (aMeshEditor->_is_nil())
966     return false;
967
968   myBusy = true;
969
970   SUIT_OverrideCursor aWaitCursor;
971
972   SMESH::SMESH_IDSource_var obj;
973   SMESH::long_array_var anIds = getIds(obj);
974
975   bool aResult = process(aMeshEditor, anIds.inout(), obj);
976   if (aResult) {
977     if (myActor) {
978       SALOME_ListIO sel;
979       mySelectionMgr->selectedObjects( sel );
980       mySelector->ClearIndex();
981       mySelectionMgr->setSelectedObjects( sel );
982       SMESH::UpdateView();
983       SMESHGUI::Modified();
984     }
985
986     myListBox->clear();
987     myIds.Clear();
988     emit ListContensChanged();
989
990     updateButtons();
991   }
992   obj._retn(); // else myMesh is deleted by ~obj
993
994   myBusy = false;
995   return aResult;
996 }
997
998 //=======================================================================
999 // name    : SMESHGUI_MultiEditDlg::on3d2dChanged
1000 // Purpose :
1001 //=======================================================================
1002 void SMESHGUI_MultiEditDlg::on3d2dChanged (int type)
1003 {
1004   if (myEntityType != type) {
1005     myEntityType = type;
1006
1007     myListBox->clear();
1008     myIds.Clear();
1009
1010     emit ListContensChanged();
1011
1012     if (type)
1013       myFilterType = SMESH::VolumeFilter;
1014     else
1015       myFilterType = SMESH::FaceFilter;
1016
1017     updateButtons();
1018     setSelectionMode();
1019   }
1020 }
1021
1022 //=======================================================================
1023 // name    : SMESHGUI_MultiEditDlg::entityType
1024 // Purpose :
1025 //=======================================================================
1026 int SMESHGUI_MultiEditDlg::entityType()
1027 {
1028   return myEntityType;
1029 }
1030
1031 //=================================================================================
1032 // function : keyPressEvent()
1033 // purpose  :
1034 //=================================================================================
1035 void SMESHGUI_MultiEditDlg::keyPressEvent( QKeyEvent* e )
1036 {
1037   QDialog::keyPressEvent( e );
1038   if ( e->isAccepted() )
1039     return;
1040
1041   if ( e->key() == Qt::Key_F1 ) {
1042     e->accept();
1043     onHelp();
1044   }
1045 }
1046
1047 /*!
1048  *  Class       : SMESHGUI_ChangeOrientationDlg
1049  *  Description : Modification of orientation of faces
1050  */
1051
1052 SMESHGUI_ChangeOrientationDlg
1053 ::SMESHGUI_ChangeOrientationDlg(SMESHGUI* theModule):
1054   SMESHGUI_MultiEditDlg(theModule, SMESH::FaceFilter, true)
1055 {
1056   setWindowTitle(tr("CAPTION"));
1057   myHelpFileName = "changing_orientation_of_elements_page.html";
1058 }
1059
1060 SMESHGUI_ChangeOrientationDlg::~SMESHGUI_ChangeOrientationDlg()
1061 {
1062 }
1063
1064 bool SMESHGUI_ChangeOrientationDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1065                                              const SMESH::long_array&    theIds,
1066                                              SMESH::SMESH_IDSource_ptr   obj)
1067 {
1068   if ( CORBA::is_nil( obj ))
1069     return theEditor->Reorient(theIds);
1070   else
1071     return theEditor->ReorientObject( obj );
1072 }
1073
1074 /*!
1075  *  Class       : SMESHGUI_UnionOfTrianglesDlg
1076  *  Description : Construction of quadrangles by automatic association of triangles
1077  */
1078
1079 SMESHGUI_UnionOfTrianglesDlg
1080 ::SMESHGUI_UnionOfTrianglesDlg(SMESHGUI* theModule):
1081   SMESHGUI_MultiEditDlg(theModule, SMESH::TriaFilter, false)
1082 {
1083   setWindowTitle(tr("CAPTION"));
1084
1085   //Preview check box
1086   myPreviewCheckBox = new QCheckBox(tr("PREVIEW"), mySelGrp);
1087   QGridLayout* aLay = (QGridLayout*)(mySelGrp->layout());
1088   aLay->addWidget(myPreviewCheckBox, aLay->rowCount(), 0, 1, aLay->columnCount());
1089
1090   myComboBoxFunctor->setEnabled(true);  
1091   myComboBoxFunctor->addItem(tr("AREA_ELEMENTS"));
1092   myComboBoxFunctor->addItem(tr("WARP_ELEMENTS")); // for quadrangles only
1093   myComboBoxFunctor->addItem(tr("TAPER_ELEMENTS")); // for quadrangles only
1094
1095   // Maximum angle
1096   QWidget* aMaxAngleGrp = new QWidget(myCriterionGrp);
1097   QHBoxLayout* aMaxAngleGrpLayout = new QHBoxLayout(aMaxAngleGrp);
1098   aMaxAngleGrpLayout->setMargin(0);
1099   aMaxAngleGrpLayout->setSpacing(SPACING);
1100
1101   QLabel* aLab = new QLabel (tr("MAXIMUM_ANGLE"), aMaxAngleGrp);
1102   myMaxAngleSpin = new SMESHGUI_SpinBox (aMaxAngleGrp);
1103   myMaxAngleSpin->RangeStepAndValidator(0, 180.0, 1.0, "angle_precision");
1104   myMaxAngleSpin->SetValue(30.0);
1105
1106   aMaxAngleGrpLayout->addWidget(aLab);
1107   aMaxAngleGrpLayout->addWidget(myMaxAngleSpin);
1108
1109   ((QVBoxLayout*)(myCriterionGrp->layout()))->addWidget(aMaxAngleGrp);
1110   myCriterionGrp->show();
1111
1112   connect(myComboBoxFunctor, SIGNAL(activated(int)),       this, SLOT(toDisplaySimulation()));
1113   connect(myMaxAngleSpin,    SIGNAL(valueChanged(int)),    this, SLOT(toDisplaySimulation()));
1114   connect(this,              SIGNAL(ListContensChanged()), this, SLOT(toDisplaySimulation()));
1115   connectPreviewControl(); //To Connect preview check box
1116
1117   myPreviewCheckBox->setChecked(false);
1118   onDisplaySimulation(false);
1119
1120   myHelpFileName = "uniting_set_of_triangles_page.html";
1121 }
1122
1123 SMESHGUI_UnionOfTrianglesDlg::~SMESHGUI_UnionOfTrianglesDlg()
1124 {
1125 }
1126
1127 bool SMESHGUI_UnionOfTrianglesDlg::isValid (const bool theMess)
1128 {
1129   bool ok = SMESHGUI_MultiEditDlg::isValid( theMess );
1130   if( !ok )
1131     return false;
1132
1133   QString msg;
1134   ok = myMaxAngleSpin->isValid( msg, theMess );
1135   if( !ok ) {
1136     if( theMess ) {
1137       QString str( tr( "SMESH_INCORRECT_INPUT" ) );
1138       if ( !msg.isEmpty() )
1139         str += "\n" + msg;
1140       SUIT_MessageBox::critical( this, tr( "SMESH_ERROR" ), str );
1141     }
1142     return false;
1143   }
1144
1145   return ok;
1146 }
1147
1148 bool SMESHGUI_UnionOfTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1149                                             const SMESH::long_array&    theIds,
1150                                             SMESH::SMESH_IDSource_ptr   obj)
1151 {
1152   {
1153     QStringList aParameters;
1154     aParameters << myMaxAngleSpin->text();
1155     myMesh->SetParameters( aParameters.join(":").toLatin1().constData() );
1156   }
1157   SMESH::NumericalFunctor_var aCriterion = getNumericalFunctor();
1158   double aMaxAngle = myMaxAngleSpin->GetValue() * M_PI / 180.0;
1159   bool ok;
1160   if ( CORBA::is_nil( obj ))
1161     ok = theEditor->TriToQuad(theIds, aCriterion, aMaxAngle);
1162   else
1163     ok = theEditor->TriToQuadObject(obj, aCriterion, aMaxAngle);
1164   return ok;
1165 }
1166   
1167 void SMESHGUI_UnionOfTrianglesDlg::onDisplaySimulation( bool toDisplayPreview )
1168 {
1169   if ( myPreviewCheckBox->isChecked() && toDisplayPreview ) {
1170     if ( isValid( true ) ) {
1171       try{
1172         SUIT_OverrideCursor aWaitCursor;
1173         // get Ids of elements
1174         SMESH::SMESH_IDSource_var obj;
1175         SMESH::long_array_var anElemIds = getIds( obj );
1176
1177         SMESH::NumericalFunctor_var aCriterion  = getNumericalFunctor();
1178         SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditPreviewer();
1179
1180         double aMaxAngle = myMaxAngleSpin->GetValue() * M_PI / 180.0;
1181       
1182         if ( CORBA::is_nil( obj ) )
1183           aMeshEditor->TriToQuad( anElemIds.inout(), aCriterion, aMaxAngle );
1184         else
1185           aMeshEditor->TriToQuadObject( obj, aCriterion, aMaxAngle );
1186       
1187         SMESH::MeshPreviewStruct_var aMeshPreviewStruct = aMeshEditor->GetPreviewData();
1188
1189         vtkProperty* aProp = vtkProperty::New();
1190         aProp->SetRepresentationToWireframe();
1191         aProp->SetColor( 250, 0, 250 );
1192         aProp->SetLineWidth( SMESH::GetFloat( "SMESH:element_width", 1 ) + 3 );
1193         mySimulation->GetActor()->SetProperty( aProp );
1194         aProp->Delete();
1195
1196         mySimulation->SetData( aMeshPreviewStruct._retn() );
1197       } catch ( ... ) {
1198         hidePreview();
1199       }
1200     } else {
1201       hidePreview();
1202     }
1203   } else {
1204     hidePreview();
1205   }
1206 }
1207
1208 /*!
1209  *  Class       : SMESHGUI_CuttingOfQuadsDlg
1210  *  Description : Automatic splitting of quadrangles into triangles
1211  */
1212
1213 SMESHGUI_CuttingOfQuadsDlg
1214 ::SMESHGUI_CuttingOfQuadsDlg(SMESHGUI* theModule):
1215   SMESHGUI_MultiEditDlg(theModule, SMESH::QuadFilter, false)
1216 {
1217   setWindowTitle(tr("CAPTION"));
1218   myPreviewActor = 0;
1219
1220   myPreviewChk = new QCheckBox (tr("PREVIEW"), mySelGrp);
1221   QGridLayout* aLay = (QGridLayout*)(mySelGrp->layout());
1222   aLay->addWidget(myPreviewChk, aLay->rowCount(), 0, 1, aLay->columnCount());
1223
1224   // "split to 4 tria" option
1225
1226   QRadioButton* to4TriaRB = new QRadioButton(tr("TO_4_TRIA"), myChoiceWidget);
1227   ((QVBoxLayout*)(myCriterionGrp->layout()))->insertWidget(0, to4TriaRB);
1228   myGroupChoice->addButton(to4TriaRB, 3);
1229   to4TriaRB->setChecked(true);
1230   onCriterionRB();
1231
1232   myCriterionGrp->show();
1233   myChoiceWidget->show();
1234   myComboBoxFunctor->insertItem(0, tr("MIN_DIAG_ELEMENTS"));
1235   myComboBoxFunctor->setCurrentIndex(0);
1236   myComboBoxFunctor->setEnabled(false);
1237
1238   connect(myPreviewChk,      SIGNAL(stateChanged(int)),    this, SLOT(onPreviewChk()));
1239   connect(myGroupChoice,     SIGNAL(buttonClicked(int)),   this, SLOT(onCriterionRB()));
1240   connect(myComboBoxFunctor, SIGNAL(activated(int)),       this, SLOT(onPreviewChk()));
1241   connect(this,              SIGNAL(ListContensChanged()), this, SLOT(onPreviewChk()));
1242
1243   myHelpFileName = "cutting_quadrangles_page.html";
1244 }
1245
1246 SMESHGUI_CuttingOfQuadsDlg::~SMESHGUI_CuttingOfQuadsDlg()
1247 {
1248 }
1249
1250 void SMESHGUI_CuttingOfQuadsDlg::reject()
1251 {
1252   erasePreview();
1253   SMESHGUI_MultiEditDlg::reject();
1254 }
1255
1256 bool SMESHGUI_CuttingOfQuadsDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1257                                           const SMESH::long_array&    theIds,
1258                                           SMESH::SMESH_IDSource_ptr   obj)
1259 {
1260   bool hasObj = (! CORBA::is_nil( obj ));
1261   switch (myGroupChoice->checkedId()) {
1262   case 0: // use diagonal 1-3
1263     return hasObj ? theEditor->SplitQuadObject(obj, true) : theEditor->SplitQuad(theIds, true);
1264   case 1: // use diagonal 2-4
1265     return hasObj ? theEditor->SplitQuadObject(obj, false) : theEditor->SplitQuad(theIds, false);
1266   case 3: // split to 4 tria
1267     {
1268       if ( hasObj )
1269         return theEditor->QuadTo4Tri( obj ), true;
1270       SMESH::SMESH_IDSource_wrap elems = theEditor->MakeIDSource( theIds, SMESH::FACE );
1271       theEditor->QuadTo4Tri( elems );
1272       return true;
1273     }
1274   default: // use numeric functor
1275     break;
1276   }
1277
1278   SMESH::NumericalFunctor_var aCrit = getNumericalFunctor();
1279   return hasObj ? theEditor->QuadToTriObject(obj, aCrit) : theEditor->QuadToTri(theIds, aCrit);
1280 }
1281
1282 void SMESHGUI_CuttingOfQuadsDlg::onCriterionRB()
1283 {
1284   if (myGroupChoice->checkedId() == 2) // Use numeric functor
1285     myComboBoxFunctor->setEnabled(true);
1286   else
1287     myComboBoxFunctor->setEnabled(false);
1288
1289   if (myGroupChoice->checkedId() == 3) // To 4 tria
1290   {
1291     if ( myPreviewChk->isChecked() )
1292       myPreviewChk->setChecked( false );
1293     myPreviewChk->setEnabled( false );
1294   }
1295   else
1296   {
1297     myPreviewChk->setEnabled( true );
1298   }
1299   onPreviewChk();
1300 }
1301
1302 void SMESHGUI_CuttingOfQuadsDlg::onPreviewChk()
1303 {
1304   myPreviewChk->isChecked() ? displayPreview() : erasePreview();
1305 }
1306
1307 void SMESHGUI_CuttingOfQuadsDlg::erasePreview()
1308 {
1309   if (myPreviewActor == 0)
1310     return;
1311
1312   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
1313     vf->RemoveActor(myPreviewActor);
1314     vf->Repaint();
1315   }
1316   myPreviewActor->Delete();
1317   myPreviewActor = 0;
1318 }
1319   
1320 void SMESHGUI_CuttingOfQuadsDlg::displayPreview()
1321 {
1322   if (myActor == 0)
1323     return;
1324
1325   if (myPreviewActor != 0)
1326     erasePreview();
1327
1328   SUIT_OverrideCursor aWaitCursor;
1329   // get Ids of elements
1330   SMESH::SMESH_IDSource_var obj;
1331   SMESH::long_array_var anElemIds = getIds(obj);
1332   if (anElemIds->length() == 0 && obj->_is_nil() )
1333     return;
1334
1335   SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh();
1336   if (aMesh == 0)
1337     return;
1338
1339   // 0 - use diagonal 1-3, 1 - use diagonal 2-4, 2 - use numerical functor
1340   int aChoice = myGroupChoice->checkedId();
1341   SMESH::NumericalFunctor_var aCriterion  = SMESH::NumericalFunctor::_nil();
1342   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH::SMESH_MeshEditor::_nil();
1343   if (aChoice == 2) {
1344     aCriterion  = getNumericalFunctor();
1345     aMeshEditor = myMesh->GetMeshEditor();
1346     if (aMeshEditor->_is_nil())
1347       return;
1348   }
1349
1350   if ( anElemIds->length() == 0 ) {
1351     anElemIds = obj->GetIDs();
1352   }
1353
1354   //Create grid
1355   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
1356
1357   vtkIdType aNbCells = anElemIds->length() * 2;
1358   vtkIdType aCellsSize = 4 * aNbCells;
1359   vtkCellArray* aConnectivity = vtkCellArray::New();
1360   aConnectivity->Allocate(aCellsSize, 0);
1361
1362   vtkPoints* aPoints = vtkPoints::New();
1363   aPoints->SetNumberOfPoints(anElemIds->length() * 4);
1364
1365   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
1366   aCellTypesArray->SetNumberOfComponents(1);
1367   aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
1368
1369   vtkIdList *anIdList = vtkIdList::New();
1370   anIdList->SetNumberOfIds(3);
1371
1372   TColStd_DataMapOfIntegerInteger anIdToVtk;
1373
1374   int aNodes[ 4 ];
1375   int nbPoints = -1;
1376   for (int i = 0, n = anElemIds->length(); i < n; i++)
1377   {
1378     const SMDS_MeshElement* anElem = aMesh->FindElement(anElemIds[ i ]);
1379     if (anElem == 0 || anElem->NbNodes() != 4)
1380       continue;
1381
1382     SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
1383     int k = 0;
1384     while (anIter->more()) {
1385       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIter->next());
1386       if (aNode)
1387       {
1388         if (!anIdToVtk.IsBound(aNode->GetID()))
1389         {
1390           aPoints->SetPoint(++nbPoints, aNode->X(), aNode->Y(), aNode->Z());
1391           anIdToVtk.Bind(aNode->GetID(), nbPoints);
1392         }
1393
1394         aNodes[ k++ ] = aNode->GetID();
1395       }
1396     }
1397
1398     if (k != 4)
1399       continue;
1400
1401     bool isDiag13 = true;
1402     if (aChoice == 0) // use diagonal 1-3
1403     {
1404       isDiag13 = true;
1405     }
1406     else if (aChoice == 1) // use diagonal 2-4
1407     {
1408       isDiag13 = false;
1409     }
1410     else // use numerical functor
1411     {
1412       // compare two sets of possible triangles
1413       int diag = aMeshEditor->BestSplit(anElemIds[i], aCriterion);
1414       if (diag == 1) // 1-3
1415         isDiag13 = true;
1416       else if (diag == 2) // 2-4
1417         isDiag13 = false;
1418       else // error
1419         continue;
1420     }
1421
1422     if (isDiag13)
1423     {
1424       anIdList->SetId(0, anIdToVtk(aNodes[ 0 ]));
1425       anIdList->SetId(1, anIdToVtk(aNodes[ 1 ]));
1426       anIdList->SetId(2, anIdToVtk(aNodes[ 2 ]));
1427       aConnectivity->InsertNextCell(anIdList);
1428       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1429
1430       anIdList->SetId(0, anIdToVtk(aNodes[ 2 ]));
1431       anIdList->SetId(1, anIdToVtk(aNodes[ 3 ]));
1432       anIdList->SetId(2, anIdToVtk(aNodes[ 0 ]));
1433       aConnectivity->InsertNextCell(anIdList);
1434       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1435     }
1436     else
1437     {
1438       anIdList->SetId(0, anIdToVtk(aNodes[ 1 ]));
1439       anIdList->SetId(1, anIdToVtk(aNodes[ 2 ]));
1440       anIdList->SetId(2, anIdToVtk(aNodes[ 3 ]));
1441       aConnectivity->InsertNextCell(anIdList);
1442       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1443
1444       anIdList->SetId(0, anIdToVtk(aNodes[ 3 ]));
1445       anIdList->SetId(1, anIdToVtk(aNodes[ 0 ]));
1446       anIdList->SetId(2, anIdToVtk(aNodes[ 1 ]));
1447       aConnectivity->InsertNextCell(anIdList);
1448       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1449     }
1450   }
1451
1452   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
1453   aCellLocationsArray->SetNumberOfComponents(1);
1454   aCellLocationsArray->SetNumberOfTuples(aNbCells);
1455
1456   aConnectivity->InitTraversal();
1457   for(vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
1458     aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
1459
1460   aGrid->SetPoints(aPoints);
1461   aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
1462
1463   // Create and display actor
1464   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
1465   aMapper->SetInputData(aGrid);
1466
1467   myPreviewActor = SALOME_Actor::New();
1468   myPreviewActor->PickableOff();
1469   myPreviewActor->SetMapper(aMapper);
1470
1471   vtkProperty* aProp = vtkProperty::New();
1472   aProp->SetRepresentationToWireframe();
1473   aProp->SetColor(250, 0, 250);
1474   aProp->SetLineWidth(myActor->GetLineWidth() + 1);
1475   myPreviewActor->SetProperty(aProp);
1476
1477   SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
1478   SMESH::GetCurrentVtkView()->Repaint();
1479
1480   aProp->Delete();
1481   aPoints->Delete();
1482   aConnectivity->Delete();
1483   aGrid->Delete();
1484   aMapper->Delete();
1485   anIdList->Delete();
1486   aCellTypesArray->Delete();
1487   aCellLocationsArray->Delete();
1488 }
1489
1490 /*!
1491  *  Class       : SMESHGUI_CuttingIntoTetraDlg
1492  *  Description : Modification of orientation of faces
1493  */
1494
1495 SMESHGUI_CuttingIntoTetraDlg::SMESHGUI_CuttingIntoTetraDlg(SMESHGUI* theModule)
1496   : SMESHGUI_MultiEditDlg(theModule, SMESH::VolumeFilter, false)
1497 {
1498   setWindowTitle(tr("CAPTION"));
1499   myHelpFileName = "split_to_tetra_page.html";
1500   myEntityType = 1;
1501
1502   myToAllChk->setChecked( true ); //aplly to the whole mesh by default
1503
1504   bool hasHexa = true;//myMesh->_is_nil() ? false : myMesh->NbHexas();
1505
1506   if ( hasHexa )
1507   {
1508     myGroupChoice->button(0)->setText( tr("SPLIT_HEX_TO_5_TETRA"));
1509     myGroupChoice->button(1)->setText( tr("SPLIT_HEX_TO_6_TETRA"));
1510     myGroupChoice->button(2)->setText( tr("SPLIT_HEX_TO_24_TETRA"));
1511
1512     myCriterionGrp->setTitle( tr("SPLIT_METHOD"));
1513     myCriterionGrp->show();
1514     myComboBoxFunctor->hide();
1515     myChoiceWidget->show();
1516   }
1517   setSelectionMode();
1518   updateButtons();
1519 }
1520
1521 SMESHGUI_CuttingIntoTetraDlg::~SMESHGUI_CuttingIntoTetraDlg()
1522 {
1523 }
1524
1525 bool SMESHGUI_CuttingIntoTetraDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1526                                             const SMESH::long_array&    theIds,
1527                                             SMESH::SMESH_IDSource_ptr   theObj)
1528 {
1529   SMESH::SMESH_IDSource_wrap obj = theObj;
1530   if ( CORBA::is_nil( obj ))
1531     obj = theEditor->MakeIDSource( theIds, myEntityType ? SMESH::VOLUME : SMESH::FACE );
1532   else
1533     obj->Register();
1534   try {
1535     theEditor->SplitVolumesIntoTetra( obj, myGroupChoice->checkedId()+1 );
1536   }
1537   catch ( const SALOME::SALOME_Exception& S_ex ) {
1538     SalomeApp_Tools::QtCatchCorbaException( S_ex );
1539     return false;
1540   }
1541   catch(...) {
1542     return false;
1543   }
1544   return true;
1545 }