Salome HOME
PAL10332 - references are interpreted as original objects in filters, dialogs, etc.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MultiEditDlg.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_MultiEditDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_MultiEditDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Filter.h"
32 #include "SMESHGUI_FilterDlg.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_VTKUtils.h"
35 #include "SMESHGUI_MeshUtils.h"
36 #include "SMESHGUI_FilterUtils.h"
37 #include "SMESHGUI_SpinBox.h"
38
39 #include "SMESH_Actor.h"
40 #include "SMESH_TypeFilter.hxx"
41 #include "SMDS_Mesh.hxx"
42 #include "SMDS_MeshElement.hxx"
43
44 #include "SUIT_ResourceMgr.h"
45 #include "SUIT_Desktop.h"
46
47 #include "SalomeApp_SelectionMgr.h"
48 #include "SALOME_ListIO.hxx"
49 #include "SALOME_ListIteratorOfListIO.hxx"
50
51 #include "SVTK_Selector.h"
52 #include "SVTK_ViewModel.h"
53 #include "SVTK_ViewWindow.h"
54 #include "SVTK_InteractorStyle.h"
55
56 // OCCT Includes
57 #include <Precision.hxx>
58 #include <TColStd_IndexedMapOfInteger.hxx>
59 #include <TColStd_DataMapOfIntegerInteger.hxx>
60 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
61
62 // VTK Includes
63 #include <vtkCell3D.h>
64 #include <vtkQuad.h>
65 #include <vtkTriangle.h>
66 #include <vtkPolygon.h>
67 #include <vtkConvexPointSet.h>
68 #include <vtkIdList.h>
69 #include <vtkIntArray.h>
70 #include <vtkCellArray.h>
71 #include <vtkUnsignedCharArray.h>
72 #include <vtkUnstructuredGrid.h>
73 #include <vtkDataSetMapper.h>
74
75 // QT Includes
76 #include <qframe.h>
77 #include <qlabel.h>
78 #include <qlayout.h>
79 #include <qlistbox.h>
80 #include <qcheckbox.h>
81 #include <qcombobox.h>
82 #include <qgroupbox.h>
83 #include <qlineedit.h>
84 #include <qpushbutton.h>
85 #include <qapplication.h>
86 #include <qradiobutton.h>
87 #include <qhbuttongroup.h>
88
89 // IDL Headers
90 #include "SALOMEconfig.h"
91 #include CORBA_SERVER_HEADER(SMESH_Group)
92
93 #define SPACING 5
94 #define MARGIN  10
95
96 /*!
97  *  Class       : SMESHGUI_MultiEditDlg
98  *  Description : Description : Inversion of the diagonal of a pseudo-quadrangle formed by
99  *                2 neighboring triangles with 1 common edge
100  */
101
102 //=======================================================================
103 // name    : SMESHGUI_MultiEditDlg::SMESHGUI_MultiEditDlg
104 // Purpose : Constructor
105 //=======================================================================
106 SMESHGUI_MultiEditDlg
107 ::SMESHGUI_MultiEditDlg(SMESHGUI* theModule,
108                         const int theMode,
109                         const bool the3d2d,
110                         const char* theName):
111   QDialog(SMESH::GetDesktop(theModule),
112           theName,
113           false,
114           WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
115     mySelector(SMESH::GetViewWindow(theModule)->GetSelector()),
116     mySelectionMgr(SMESH::GetSelectionMgr(theModule)),
117     mySMESHGUI(theModule)
118 {
119   myFilterDlg = 0;
120   myEntityType = 0;
121
122   myFilterType = theMode;
123   QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
124
125   QFrame* aMainFrame = createMainFrame  (this, the3d2d);
126   QFrame* aBtnFrame  = createButtonFrame(this);
127
128   aDlgLay->addWidget(aMainFrame);
129   aDlgLay->addWidget(aBtnFrame);
130
131   aDlgLay->setStretchFactor(aMainFrame, 1);
132   aDlgLay->setStretchFactor(aBtnFrame, 0);
133   Init();
134 }
135
136 //=======================================================================
137 // name    : SMESHGUI_MultiEditDlg::createMainFrame
138 // Purpose : Create frame containing dialog's input fields
139 //=======================================================================
140 QFrame* SMESHGUI_MultiEditDlg::createMainFrame (QWidget* theParent, const bool the3d2d)
141 {
142   QGroupBox* aMainGrp = new QGroupBox(1, Qt::Horizontal, theParent);
143   aMainGrp->setFrameStyle(QFrame::NoFrame);
144   aMainGrp->setInsideMargin(0);
145
146   QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
147
148   // "Selected cells" group
149   mySelGrp = new QGroupBox(1, Qt::Horizontal,  aMainGrp);
150
151   myEntityTypeGrp = 0;
152   if (the3d2d) {
153     myEntityTypeGrp = new QHButtonGroup(tr("SMESH_ELEMENTS_TYPE"), mySelGrp);
154     (new QRadioButton(tr("SMESH_FACE"),   myEntityTypeGrp))->setChecked(true);
155     (new QRadioButton(tr("SMESH_VOLUME"), myEntityTypeGrp));
156     myEntityType = myEntityTypeGrp->id(myEntityTypeGrp->selected());
157   }
158
159   QFrame* aFrame = new QFrame(mySelGrp);
160
161   myListBox = new QListBox(aFrame);
162   myListBox->setSelectionMode(QListBox::Extended);
163   myListBox->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
164   myListBox->installEventFilter(this);
165
166   myFilterBtn = new QPushButton(tr("FILTER")   , aFrame);
167   myAddBtn    = new QPushButton(tr("ADD")      , aFrame);
168   myRemoveBtn = new QPushButton(tr("REMOVE")   , aFrame);
169   mySortBtn   = new QPushButton(tr("SORT_LIST"), aFrame);
170
171   QGridLayout* aLay = new QGridLayout(aFrame, 5, 2, 0, 5);
172   aLay->addMultiCellWidget(myListBox, 0, 4, 0, 0);
173   aLay->addWidget(myFilterBtn, 0, 1);
174   aLay->addWidget(myAddBtn, 1, 1);
175   aLay->addWidget(myRemoveBtn, 2, 1);
176   aLay->addWidget(mySortBtn, 3, 1);
177
178   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
179   aLay->addItem(aSpacer, 4, 1);
180
181   myToAllChk = new QCheckBox(tr("TO_ALL"), mySelGrp);
182
183   // Split/Join criterion group
184   myCriterionGrp = new QGroupBox(3, Qt::Vertical, tr("SPLIT_JOIN_CRITERION"), aMainGrp);
185
186   myGroupChoice = new QButtonGroup(3, Qt::Vertical, myCriterionGrp);
187   myGroupChoice->setInsideMargin(0);
188   myGroupChoice->setFrameStyle(QFrame::NoFrame);
189   (new QRadioButton(tr("USE_DIAGONAL_1_3"), myGroupChoice))->setChecked(true);
190   (new QRadioButton(tr("USE_DIAGONAL_2_4"), myGroupChoice));
191   (new QRadioButton(tr("USE_NUMERIC_FUNC"), myGroupChoice));
192
193   myComboBoxFunctor = new QComboBox(myCriterionGrp);
194   myComboBoxFunctor->insertItem(tr("ASPECTRATIO_ELEMENTS"));
195   myComboBoxFunctor->insertItem(tr("MINIMUMANGLE_ELEMENTS"));
196   myComboBoxFunctor->insertItem(tr("SKEW_ELEMENTS"));
197   myComboBoxFunctor->insertItem(tr("AREA_ELEMENTS"));
198   //myComboBoxFunctor->insertItem(tr("LENGTH2D_EDGES")); // for existing elements only
199   //myComboBoxFunctor->insertItem(tr("MULTI2D_BORDERS")); // for existing elements only
200   myComboBoxFunctor->setCurrentItem(0);
201
202   myCriterionGrp->hide();
203   myGroupChoice->hide();
204   myComboBoxFunctor->setEnabled(false);
205
206   // "Select from" group
207   QGroupBox* aGrp = new QGroupBox(3, Qt::Horizontal, tr("SELECT_FROM"), aMainGrp);
208
209   mySubmeshChk = new QCheckBox(tr("SMESH_SUBMESH"), aGrp);
210   mySubmeshBtn = new QPushButton(aGrp);
211   mySubmesh = new QLineEdit(aGrp);
212   mySubmesh->setReadOnly(true);
213   mySubmeshBtn->setPixmap(aPix);
214
215   myGroupChk = new QCheckBox(tr("SMESH_GROUP"), aGrp);
216   myGroupBtn = new QPushButton(aGrp);
217   myGroup = new QLineEdit(aGrp);
218   myGroup->setReadOnly(true);
219   myGroupBtn->setPixmap(aPix);
220
221   return aMainGrp;
222 }
223
224 //=======================================================================
225 // name    : SMESHGUI_MultiEditDlg::createButtonFrame
226 // Purpose : Create frame containing buttons
227 //=======================================================================
228 QFrame* SMESHGUI_MultiEditDlg::createButtonFrame (QWidget* theParent)
229 {
230   QFrame* aFrame = new QFrame (theParent);
231   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
232
233   myOkBtn     = new QPushButton (tr("SMESH_BUT_OK"   ), aFrame);
234   myApplyBtn  = new QPushButton (tr("SMESH_BUT_APPLY"), aFrame);
235   myCloseBtn  = new QPushButton (tr("SMESH_BUT_CLOSE"), aFrame);
236
237   QSpacerItem* aSpacer = new QSpacerItem (0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
238
239   QHBoxLayout* aLay = new QHBoxLayout (aFrame, MARGIN, SPACING);
240
241   aLay->addWidget(myOkBtn);
242   aLay->addWidget(myApplyBtn);
243   aLay->addItem(aSpacer);
244   aLay->addWidget(myCloseBtn);
245
246   return aFrame;
247 }
248
249 //=======================================================================
250 // name    : SMESHGUI_MultiEditDlg::isValid
251 // Purpose : Verify validity of input data
252 //=======================================================================
253 bool SMESHGUI_MultiEditDlg::isValid (const bool /*theMess*/) const
254 {
255   return (!myMesh->_is_nil() &&
256           (myListBox->count() > 0 || (myToAllChk->isChecked() && myActor)));
257 }
258
259 //=======================================================================
260 // name    : SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg
261 // Purpose : Destructor
262 //=======================================================================
263 SMESHGUI_MultiEditDlg::~SMESHGUI_MultiEditDlg()
264 {
265   if (myFilterDlg != 0)
266   {
267     myFilterDlg->reparent(0, QPoint());
268     delete myFilterDlg;
269   }
270 }
271
272 //=======================================================================
273 // name    : SMESHGUI_MultiEditDlg::eventFilter
274 // Purpose : event filter
275 //=======================================================================
276 bool SMESHGUI_MultiEditDlg::eventFilter (QObject* object, QEvent* event)
277 {
278   if (object == myListBox && event->type() == QEvent::KeyPress) {
279     QKeyEvent* ke = (QKeyEvent*)event;
280     if (ke->key() == Key_Delete)
281       onRemoveBtn();
282   }
283   return QDialog::eventFilter(object, event);
284 }
285
286 //=======================================================================
287 // name    : SMESHGUI_MultiEditDlg::getNumericalFunctor
288 // Purpose :
289 //=======================================================================
290 SMESH::NumericalFunctor_ptr SMESHGUI_MultiEditDlg::getNumericalFunctor()
291 {
292   SMESH::NumericalFunctor_var aNF = SMESH::NumericalFunctor::_nil();
293
294   SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
295   if (aFilterMgr->_is_nil())
296     return aNF._retn();
297
298   if (myComboBoxFunctor->currentText() == tr("ASPECTRATIO_ELEMENTS"))
299     aNF = aFilterMgr->CreateAspectRatio();
300   else if (myComboBoxFunctor->currentText() == tr("WARP_ELEMENTS"))
301     aNF = aFilterMgr->CreateWarping();
302   else if (myComboBoxFunctor->currentText() == tr("MINIMUMANGLE_ELEMENTS"))
303     aNF = aFilterMgr->CreateMinimumAngle();
304   else if (myComboBoxFunctor->currentText() == tr("TAPER_ELEMENTS"))
305     aNF = aFilterMgr->CreateTaper();
306   else if (myComboBoxFunctor->currentText() == tr("SKEW_ELEMENTS"))
307     aNF = aFilterMgr->CreateSkew();
308   else if (myComboBoxFunctor->currentText() == tr("AREA_ELEMENTS"))
309     aNF = aFilterMgr->CreateArea();
310   else if (myComboBoxFunctor->currentText() == tr("LENGTH2D_EDGES"))
311     aNF = aFilterMgr->CreateLength2D();
312   else if (myComboBoxFunctor->currentText() == tr("MULTI2D_BORDERS"))
313     aNF = aFilterMgr->CreateMultiConnection2D();
314   else ;
315
316   return aNF._retn();
317 }
318
319 //=======================================================================
320 // name    : SMESHGUI_MultiEditDlg::Init
321 // Purpose : Init dialog fields, connect signals and slots, show dialog
322 //=======================================================================
323 void SMESHGUI_MultiEditDlg::Init()
324 {
325   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
326   myListBox->clear();
327   myIds.Clear();
328   myBusy = false;
329   myActor = 0;
330   emit ListContensChanged();
331
332   // main buttons
333   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
334   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
335   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
336
337   // selection and SMESHGUI
338   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
339   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
340   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(onClose()));
341
342   // dialog controls
343   connect(myFilterBtn, SIGNAL(clicked()), SLOT(onFilterBtn()  ));
344   connect(myAddBtn   , SIGNAL(clicked()), SLOT(onAddBtn()     ));
345   connect(myRemoveBtn, SIGNAL(clicked()), SLOT(onRemoveBtn()  ));
346   connect(mySortBtn  , SIGNAL(clicked()), SLOT(onSortListBtn()));
347
348   connect(mySubmeshChk, SIGNAL(stateChanged(int)), SLOT(onSubmeshChk()));
349   connect(myGroupChk  , SIGNAL(stateChanged(int)), SLOT(onGroupChk()  ));
350   connect(myToAllChk  , SIGNAL(stateChanged(int)), SLOT(onToAllChk()  ));
351
352   if (myEntityTypeGrp)
353     connect(myEntityTypeGrp, SIGNAL(clicked(int)), SLOT(on3d2dChanged(int)));
354
355   connect(myListBox, SIGNAL(selectionChanged()), SLOT(onListSelectionChanged()));
356
357   onSelectionDone();
358
359   // set selection mode
360   setSelectionMode();
361   updateButtons();
362 }
363
364 //=======================================================================
365 // name    : SMESHGUI_MultiEditDlg::onOk
366 // Purpose : SLOT called when "Ok" button pressed.
367 //           Assign filters VTK viewer and close dialog
368 //=======================================================================
369 void SMESHGUI_MultiEditDlg::onOk()
370 {
371   if (onApply())
372     onClose();
373 }
374
375 //=======================================================================
376 // name    : SMESHGUI_MultiEditDlg::getIds
377 // Purpose : Retrive identifiers from list box
378 //=======================================================================
379 SMESH::long_array_var SMESHGUI_MultiEditDlg::getIds()
380 {
381   SMESH::long_array_var anIds = new SMESH::long_array;
382
383   if (myToAllChk->isChecked())
384   {
385     myIds.Clear();
386     SMESH_Actor * anActor = SMESH::FindActorByObject(myMesh);
387     if (!anActor)
388       anActor = myActor;
389     if (anActor != 0)
390     {
391       TVisualObjPtr aVisualObj = anActor->GetObject();
392       vtkUnstructuredGrid* aGrid = aVisualObj->GetUnstructuredGrid();
393       if (aGrid != 0) {
394         for (int i = 0, n = aGrid->GetNumberOfCells(); i < n; i++) {
395           vtkCell* aCell = aGrid->GetCell(i);
396           if (aCell != 0) {
397             vtkTriangle* aTri = vtkTriangle::SafeDownCast(aCell);
398             vtkQuad*     aQua = vtkQuad::SafeDownCast(aCell);
399             vtkPolygon*  aPG  = vtkPolygon::SafeDownCast(aCell);
400
401             vtkCell3D*   a3d  = vtkCell3D::SafeDownCast(aCell);
402             vtkConvexPointSet* aPH = vtkConvexPointSet::SafeDownCast(aCell);
403
404             if (aTri && myFilterType == SMESHGUI_TriaFilter ||
405                 aQua && myFilterType == SMESHGUI_QuadFilter ||
406                 (aTri || aQua || aPG) && myFilterType == SMESHGUI_FaceFilter ||
407                 (a3d || aPH) && myFilterType == SMESHGUI_VolumeFilter) {
408               int anObjId = aVisualObj->GetElemObjId(i);
409               myIds.Add(anObjId);
410             }
411           }
412         }
413       }
414     }
415   }
416
417   anIds->length(myIds.Extent());
418   TColStd_MapIteratorOfMapOfInteger anIter(myIds);
419   for (int i = 0; anIter.More(); anIter.Next() )
420   {
421     anIds[ i++ ] = anIter.Key();
422   }
423   return anIds._retn();
424 }
425
426 //=======================================================================
427 // name    : SMESHGUI_MultiEditDlg::onClose
428 // Purpose : SLOT called when "Close" button pressed. Close dialog
429 //=======================================================================
430 void SMESHGUI_MultiEditDlg::onClose()
431 {
432   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
433     aViewWindow->SetSelectionMode(ActorSelection);
434   disconnect(mySelectionMgr, 0, this, 0);
435   disconnect(mySMESHGUI, 0, this, 0);
436   mySMESHGUI->ResetState();
437
438   SMESH::RemoveFilters();
439   SMESH::SetPickable();
440
441   mySelectionMgr->clearSelected();
442   mySelectionMgr->clearFilters();
443
444   reject();
445 }
446
447 //=======================================================================
448 // name    : SMESHGUI_MultiEditDlg::onSelectionDone
449 // Purpose : SLOT called when selection changed
450 //=======================================================================
451 void SMESHGUI_MultiEditDlg::onSelectionDone()
452 {
453   if (myBusy || !isEnabled()) return;
454   myBusy = true;
455
456   const SALOME_ListIO& aList = mySelector->StoredIObjects();
457
458   int nbSel = aList.Extent();
459   myListBox->clearSelection();
460
461   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
462     QLineEdit* aNameEdit = mySubmeshChk->isChecked() ? mySubmesh : myGroup;
463     if (nbSel == 1) {
464       Handle(SALOME_InteractiveObject) anIO = aList.First();
465       QString aName = "";
466       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
467       anIO.IsNull() ? aNameEdit->clear() : aNameEdit->setText(aName);
468
469       if (mySubmeshChk->isChecked()) {
470         SMESH::SMESH_subMesh_var aSubMesh =
471           SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIO);
472         if (!aSubMesh->_is_nil())
473           myMesh = aSubMesh->GetFather();
474       } else {
475         SMESH::SMESH_GroupBase_var aGroup =
476           SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIO);
477         if (!aGroup->_is_nil())
478           myMesh = aGroup->GetMesh();
479       }
480     } else if (nbSel > 1) {
481       QString aStr = mySubmeshChk->isChecked() ?
482         tr("SMESH_SUBMESH_SELECTED") : tr("SMESH_GROUP_SELECTED");
483       aNameEdit->setText(aStr.arg(nbSel));
484     } else {
485       aNameEdit->clear();
486     }
487   } else if (nbSel == 1) {
488     QString aListStr = "";
489     Handle(SALOME_InteractiveObject) anIO = aList.First();
490     int aNbItems = SMESH::GetNameOfSelectedElements(mySelector,anIO,aListStr);
491     if (aNbItems > 0) {
492       QStringList anElements = QStringList::split(" ", aListStr);
493       QListBoxItem* anItem = 0;
494       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
495         anItem = myListBox->findItem(*it, Qt::ExactMatch);
496         if (anItem) myListBox->setSelected(anItem, true);
497       }
498     }
499
500     myMesh = SMESH::GetMeshByIO(anIO);
501   }
502
503   if (nbSel == 1) {
504     myActor = SMESH::FindActorByEntry(aList.First()->getEntry());
505     if (!myActor)
506       myActor = SMESH::FindActorByObject(myMesh);
507     SVTK_InteractorStyle* aStyle = SMESH::GetInteractorStyle();
508     Handle(VTKViewer_Filter) aFilter = aStyle->GetFilter(myFilterType);
509     if (!aFilter.IsNull())
510       aFilter->SetActor(myActor);
511   }
512   myBusy = false;
513
514   updateButtons();
515 }
516
517 //=======================================================================
518 // name    : SMESHGUI_MultiEditDlg::onDeactivate
519 // Purpose : SLOT called when dialog must be deativated
520 //=======================================================================
521 void SMESHGUI_MultiEditDlg::onDeactivate()
522 {
523   setEnabled(false);
524 }
525
526 //=======================================================================
527 // name    : SMESHGUI_MultiEditDlg::enterEvent
528 // Purpose : Event filter
529 //=======================================================================
530 void SMESHGUI_MultiEditDlg::enterEvent (QEvent*)
531 {
532   if (!isEnabled()) {
533     mySMESHGUI->EmitSignalDeactivateDialog();
534     setEnabled(true);
535     setSelectionMode();
536   }
537 }
538
539 //=======================================================================
540 // name    : SMESHGUI_MultiEditDlg::closeEvent
541 // Purpose :
542 //=======================================================================
543 void SMESHGUI_MultiEditDlg::closeEvent (QCloseEvent*)
544 {
545   onClose();
546 }
547 //=======================================================================
548 // name    : SMESHGUI_MultiEditDlg::hideEvent
549 // Purpose : caused by ESC key
550 //=======================================================================
551 void SMESHGUI_MultiEditDlg::hideEvent (QHideEvent*)
552 {
553   if (!isMinimized())
554     onClose();
555 }
556
557 //=======================================================================
558 // name    : SMESHGUI_MultiEditDlg::onFilterBtn
559 // Purpose : SLOT. Called when "Filter" button pressed.
560 //           Start "Selection filters" dialog
561 //=======================================================================
562 void SMESHGUI_MultiEditDlg::onFilterBtn()
563 {
564   if (myFilterDlg == 0) {
565     myFilterDlg = new SMESHGUI_FilterDlg( mySMESHGUI, entityType() ? SMESH::VOLUME : SMESH::FACE);
566     connect(myFilterDlg, SIGNAL(Accepted()), SLOT(onFilterAccepted()));
567   } else {
568     myFilterDlg->Init(entityType() ? SMESH::VOLUME : SMESH::FACE);
569   }
570
571   myFilterDlg->SetSelection();
572   myFilterDlg->SetMesh(myMesh);
573   myFilterDlg->SetSourceWg(myListBox);
574
575   myFilterDlg->show();
576 }
577
578 //=======================================================================
579 // name    : onFilterAccepted()
580 // Purpose : SLOT. Called when Filter dlg closed with OK button.
581 //            Uncheck "Select submesh" and "Select group" checkboxes
582 //=======================================================================
583 void SMESHGUI_MultiEditDlg::onFilterAccepted()
584 {
585   myIds.Clear();
586   for (int i = 0, n = myListBox->count(); i < n; i++)
587     myIds.Add(myListBox->text(i).toInt());
588
589   emit ListContensChanged();
590
591   if (mySubmeshChk->isChecked() || myGroupChk->isChecked()) {
592     mySubmeshChk->blockSignals(true);
593     myGroupChk->blockSignals(true);
594     mySubmeshChk->setChecked(false);
595     myGroupChk->setChecked(false);
596     mySubmeshChk->blockSignals(false);
597     myGroupChk->blockSignals(false);
598   }
599   updateButtons();
600 }
601
602 //=======================================================================
603 // name    : SMESHGUI_MultiEditDlg::isIdValid
604 // Purpose : Verify whether Id of element satisfies to filters from viewer
605 //=======================================================================
606 bool SMESHGUI_MultiEditDlg::isIdValid (const int theId) const
607 {
608   SVTK_InteractorStyle* aStyle = SMESH::GetInteractorStyle();
609   Handle(SMESHGUI_Filter) aFilter =
610     Handle(SMESHGUI_Filter)::DownCast(aStyle->GetFilter(myFilterType));
611
612   return (!aFilter.IsNull() && aFilter->IsObjValid(theId));
613 }
614
615 //=======================================================================
616 // name    : SMESHGUI_MultiEditDlg::onAddBtn
617 // Purpose : SLOT. Called when "Add" button pressed.
618 //           Add selected in viewer entities in list box
619 //=======================================================================
620 void SMESHGUI_MultiEditDlg::onAddBtn()
621 {
622   const SALOME_ListIO& aList = mySelector->StoredIObjects();
623
624   int nbSelected = aList.Extent();
625   if (nbSelected == 0)
626     return;
627
628   TColStd_IndexedMapOfInteger toBeAdded;
629
630   if (!mySubmeshChk->isChecked() && !myGroupChk->isChecked()) {
631     if (nbSelected == 1)
632       mySelector->GetIndex(aList.First(),toBeAdded);
633   } else if (mySubmeshChk->isChecked()) {
634     SALOME_ListIteratorOfListIO anIter(aList);
635     for (; anIter.More(); anIter.Next()) {
636       SMESH::SMESH_subMesh_var aSubMesh =
637         SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIter.Value());
638       if (!aSubMesh->_is_nil()) {
639         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
640           SMESH::long_array_var anIds = aSubMesh->GetElementsId();
641           for (int i = 0, n = anIds->length(); i < n; i++) {
642             if (isIdValid(anIds[ i ]))
643               toBeAdded.Add(anIds[ i ]);
644           }
645         }
646       }
647     }
648   } else if (myGroupChk->isChecked()) {
649     SALOME_ListIteratorOfListIO anIter(aList);
650     for (; anIter.More(); anIter.Next()) {
651       SMESH::SMESH_GroupBase_var aGroup =
652         SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(anIter.Value());
653       if (!aGroup->_is_nil() && (aGroup->GetType() == SMESH::FACE &&
654                                  entityType() == 0 || aGroup->GetType() == SMESH::VOLUME &&
655                                  entityType() == 1)) {
656         if (aGroup->GetMesh()->GetId() == myMesh->GetId()) {
657           SMESH::long_array_var anIds = aGroup->GetListOfID();
658           for (int i = 0, n = anIds->length(); i < n; i++) {
659             if (isIdValid(anIds[ i ]))
660               toBeAdded.Add(anIds[ i ]);
661           }
662         }
663       }
664     }
665   } else {
666   }
667
668   myBusy = true;
669   bool isGroupOrSubmesh = (mySubmeshChk->isChecked() || myGroupChk->isChecked());
670   mySubmeshChk->setChecked(false);
671   myGroupChk->setChecked(false);
672   for(int i = 1; i <= toBeAdded.Extent(); i++)
673     if (myIds.Add(toBeAdded(i))) {
674       QListBoxItem * item = new QListBoxText(QString("%1").arg(toBeAdded(i)));
675       myListBox->insertItem(item);
676       myListBox->setSelected(item, true);
677     }
678   myBusy = false;
679
680   emit ListContensChanged();
681
682   if (isGroupOrSubmesh)
683     onListSelectionChanged();
684
685   updateButtons();
686 }
687
688 //=======================================================================
689 // name    : SMESHGUI_MultiEditDlg::updateButtons
690 // Purpose : Enable/disable buttons of dialog in accordance with current state
691 //=======================================================================
692 void SMESHGUI_MultiEditDlg::updateButtons()
693 {
694   bool isOk = isValid(false);
695   myOkBtn->setEnabled(isOk);
696   myApplyBtn->setEnabled(isOk);
697
698   bool isListBoxNonEmpty = myListBox->count() > 0;
699   bool isToAll = myToAllChk->isChecked();
700   myFilterBtn->setEnabled(!isToAll);
701   myRemoveBtn->setEnabled(isListBoxNonEmpty && !isToAll);
702   mySortBtn->setEnabled(isListBoxNonEmpty &&!isToAll);
703
704   const SALOME_ListIO& aList = mySelector->StoredIObjects();
705
706   if (isToAll ||
707       myMesh->_is_nil() ||
708       aList.Extent() != 1 ||
709       (SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(aList.First())->_is_nil() &&
710        SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First())->_is_nil() &&
711        SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(aList.First())->_is_nil()))
712     myAddBtn->setEnabled(false);
713   else
714     myAddBtn->setEnabled(true);
715
716   mySubmeshChk->setEnabled(!isToAll);
717   mySubmeshBtn->setEnabled(mySubmeshChk->isChecked());
718   mySubmesh->setEnabled(mySubmeshChk->isChecked());
719
720   myGroupChk->setEnabled(!isToAll);
721   myGroupBtn->setEnabled(myGroupChk->isChecked());
722   myGroup->setEnabled(myGroupChk->isChecked());
723
724   if (!mySubmeshChk->isChecked())
725     mySubmesh->clear();
726   if (!myGroupChk->isChecked())
727     myGroup->clear();
728
729 }
730
731 //=======================================================================
732 // name    : SMESHGUI_MultiEditDlg::onRemoveBtn
733 // Purpose : SLOT. Called when "Remove" button pressed.
734 //           Remove selected in list box entities
735 //=======================================================================
736 void SMESHGUI_MultiEditDlg::onRemoveBtn()
737 {
738   myBusy = true;
739
740   for (int i = 0, n = myListBox->count(); i < n; i++)
741   {
742     for (int i = myListBox->count(); i > 0; i--) {
743       if (myListBox->isSelected(i - 1))
744       {
745         int anId = myListBox->text(i - 1).toInt();
746         myIds.Remove(anId);
747         myIds.Remove(anId);
748               myListBox->removeItem(i-1);
749       }
750     }
751   }
752   myBusy = false;
753
754   emit ListContensChanged();
755   updateButtons();
756 }
757
758 //=======================================================================
759 // name    : SMESHGUI_MultiEditDlg::onSortListBtn
760 // Purpose : SLOT. Called when "Sort list" button pressed.
761 //           Sort entities of list box
762 //=======================================================================
763 void SMESHGUI_MultiEditDlg::onSortListBtn()
764 {
765   myBusy = true;
766
767   int i, k = myListBox->count();
768   if (k > 0)
769   {
770     QStringList aSelected;
771     std::vector<int> anArray(k);
772     QListBoxItem* anItem;
773     for (anItem = myListBox->firstItem(), i = 0; anItem != 0; anItem = anItem->next(), i++)
774     {
775       anArray[ i ] = anItem->text().toInt();
776       if (anItem->isSelected())
777         aSelected.append(anItem->text());
778     }
779
780     std::sort(anArray.begin(), anArray.end());
781     myListBox->clear();
782     for (i = 0; i < k; i++)
783       myListBox->insertItem(QString::number(anArray[ i ]));
784
785     for (QStringList::iterator it = aSelected.begin(); it != aSelected.end(); ++it)
786     {
787       anItem = myListBox->findItem(*it, Qt::ExactMatch);
788       if (anItem)
789         myListBox->setSelected(anItem, true);
790     }
791   }
792   myBusy = false;
793 }
794
795 //=======================================================================
796 // name    : SMESHGUI_MultiEditDlg::onListSelectionChanged
797 // Purpose : SLOT. Called when selection in list box changed.
798 //           Highlight in selected entities
799 //=======================================================================
800 void SMESHGUI_MultiEditDlg::onListSelectionChanged()
801 {
802   if (myActor == 0 || myBusy)
803     return;
804
805   if (mySubmeshChk->isChecked() || myGroupChk->isChecked())
806     return;
807
808   SMESH_Actor * anActor = SMESH::FindActorByObject(myMesh);
809   if (!anActor)
810     anActor = myActor;
811   TVisualObjPtr anObj = anActor->GetObject();
812
813   TColStd_MapOfInteger anIndexes;
814   for (QListBoxItem* anItem = myListBox->firstItem(); anItem != 0; anItem = anItem->next())
815   {
816     if (anItem->isSelected())
817     {
818       int anId = anItem->text().toInt();
819       if (anObj->GetElemVTKId(anId) >= 0) // avoid exception in hilight
820         anIndexes.Add(anId);
821     }
822   }
823
824   mySelector->AddOrRemoveIndex(anActor->getIO(),anIndexes,false);
825   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
826     aViewWindow->highlight(anActor->getIO(),true,true);
827 }
828
829 //=======================================================================
830 // name    : SMESHGUI_MultiEditDlg::onSubmeshChk
831 // Purpose : SLOT. Called when state of "SubMesh" check box changed.
832 //           Activate/deactivate selection of submeshes
833 //=======================================================================
834 void SMESHGUI_MultiEditDlg::onSubmeshChk()
835 {
836   bool isChecked = mySubmeshChk->isChecked();
837   mySubmeshBtn->setEnabled(isChecked);
838   mySubmesh->setEnabled(isChecked);
839   if (!isChecked)
840     mySubmesh->clear();
841   if (isChecked && myGroupChk->isChecked())
842       myGroupChk->setChecked(false);
843
844   setSelectionMode();
845 }
846
847 //=======================================================================
848 // name    : SMESHGUI_MultiEditDlg::onGroupChk
849 // Purpose : SLOT. Called when state of "Group" check box changed.
850 //           Activate/deactivate selection of groupes
851 //=======================================================================
852 void SMESHGUI_MultiEditDlg::onGroupChk()
853 {
854   bool isChecked = myGroupChk->isChecked();
855   myGroupBtn->setEnabled(isChecked);
856   myGroup->setEnabled(isChecked);
857   if (!isChecked)
858     myGroup->clear();
859   if (isChecked && mySubmeshChk->isChecked())
860       mySubmeshChk->setChecked(false);
861
862   setSelectionMode();
863 }
864
865 //=======================================================================
866 // name    : SMESHGUI_MultiEditDlg::onToAllChk
867 // Purpose : SLOT. Called when state of "Apply to all" check box changed.
868 //           Activate/deactivate selection
869 //=======================================================================
870 void SMESHGUI_MultiEditDlg::onToAllChk()
871 {
872   bool isChecked = myToAllChk->isChecked();
873
874   if (isChecked)
875     myListBox->clear();
876
877   myIds.Clear();
878
879   emit ListContensChanged();
880
881   updateButtons();
882   setSelectionMode();
883 }
884
885 //=======================================================================
886 // name    : SMESHGUI_MultiEditDlg::setSelectionMode
887 // Purpose : Set selection mode
888 //=======================================================================
889 void SMESHGUI_MultiEditDlg::setSelectionMode()
890 {
891   SMESH::RemoveFilters();
892
893   mySelectionMgr->clearSelected();
894   mySelectionMgr->clearFilters();
895
896   if (mySubmeshChk->isChecked()) {
897     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
898       aViewWindow->SetSelectionMode(ActorSelection);
899     mySelectionMgr->installFilter(new SMESH_TypeFilter(SUBMESH));
900   }
901   else if (myGroupChk->isChecked()) {
902     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
903       aViewWindow->SetSelectionMode(ActorSelection);
904     mySelectionMgr->installFilter(new SMESH_TypeFilter(GROUP));
905   }
906
907   if (entityType()) {
908     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
909       aViewWindow->SetSelectionMode(VolumeSelection);
910     SMESH::SetFilter(new SMESHGUI_VolumesFilter());
911   } else {
912     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
913       aViewWindow->SetSelectionMode(FaceSelection);
914     if (myFilterType == SMESHGUI_TriaFilter)
915       SMESH::SetFilter(new SMESHGUI_TriangleFilter());
916     else if (myFilterType == SMESHGUI_QuadFilter)
917       SMESH::SetFilter(new SMESHGUI_QuadrangleFilter());
918     else
919       SMESH::SetFilter(new SMESHGUI_FacesFilter());
920   }
921 }
922
923 //=======================================================================
924 // name    : SMESHGUI_MultiEditDlg::onApply
925 // Purpose : SLOT. Called when "Apply" button clicked.
926 //=======================================================================
927 bool SMESHGUI_MultiEditDlg::onApply()
928 {
929   if (mySMESHGUI->isActiveStudyLocked())
930     return false;
931   if (!isValid(true))
932     return false;
933
934   SMESH::SMESH_MeshEditor_var aMeshEditor = myMesh->GetMeshEditor();
935   if (aMeshEditor->_is_nil())
936     return false;
937
938   myBusy = true;
939
940   SMESH::long_array_var anIds = getIds();
941
942   bool aResult = process(aMeshEditor, anIds.inout());
943   if (aResult) {
944     if (myActor) {
945       mySelectionMgr->clearSelected();
946       SMESH::UpdateView();
947     }
948
949     myListBox->clear();
950     myIds.Clear();
951     emit ListContensChanged();
952
953     updateButtons();
954   }
955
956   myBusy = false;
957   return aResult;
958 }
959
960 //=======================================================================
961 // name    : SMESHGUI_MultiEditDlg::on3d2dChanged
962 // Purpose :
963 //=======================================================================
964 void SMESHGUI_MultiEditDlg::on3d2dChanged (int type)
965 {
966   if (myEntityType != type) {
967     myEntityType = type;
968
969     myListBox->clear();
970     myIds.Clear();
971
972     emit ListContensChanged();
973
974     if (type)
975       myFilterType = SMESHGUI_VolumeFilter;
976     else
977       myFilterType = SMESHGUI_FaceFilter;
978
979     updateButtons();
980     setSelectionMode();
981   }
982 }
983
984 //=======================================================================
985 // name    : SMESHGUI_MultiEditDlg::entityType
986 // Purpose :
987 //=======================================================================
988 int SMESHGUI_MultiEditDlg::entityType()
989 {
990   return myEntityType;
991 }
992
993 /*!
994  *  Class       : SMESHGUI_ChangeOrientationDlg
995  *  Description : Modification of orientation of faces
996  */
997
998 SMESHGUI_ChangeOrientationDlg
999 ::SMESHGUI_ChangeOrientationDlg(SMESHGUI* theModule,
1000                                 const char* theName):
1001   SMESHGUI_MultiEditDlg(theModule, SMESHGUI_FaceFilter, true, theName)
1002 {
1003   setCaption(tr("CAPTION"));
1004 }
1005
1006 SMESHGUI_ChangeOrientationDlg::~SMESHGUI_ChangeOrientationDlg()
1007 {
1008 }
1009
1010 bool SMESHGUI_ChangeOrientationDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1011                                              const SMESH::long_array&    theIds)
1012 {
1013   return theEditor->Reorient(theIds);
1014 }
1015
1016 /*!
1017  *  Class       : SMESHGUI_UnionOfTrianglesDlg
1018  *  Description : Construction of quadrangles by automatic association of triangles
1019  */
1020
1021 SMESHGUI_UnionOfTrianglesDlg
1022 ::SMESHGUI_UnionOfTrianglesDlg(SMESHGUI* theModule,
1023                                const char* theName):
1024   SMESHGUI_MultiEditDlg(theModule, SMESHGUI_TriaFilter, false, theName)
1025 {
1026   setCaption(tr("CAPTION"));
1027
1028   myComboBoxFunctor->setEnabled(true);
1029   myComboBoxFunctor->insertItem(tr("WARP_ELEMENTS")); // for quadrangles only
1030   myComboBoxFunctor->insertItem(tr("TAPER_ELEMENTS")); // for quadrangles only
1031
1032   // Maximum angle
1033   QGroupBox* aMaxAngleGrp = new QGroupBox (2, Qt::Horizontal, myCriterionGrp);
1034   aMaxAngleGrp->setInsideMargin(0);
1035   aMaxAngleGrp->setFrameStyle(QFrame::NoFrame);
1036   new QLabel (tr("MAXIMUM_ANGLE"), aMaxAngleGrp);
1037   myMaxAngleSpin = new SMESHGUI_SpinBox (aMaxAngleGrp);
1038   myMaxAngleSpin->RangeStepAndValidator(0, 180.0, 1.0, 3);
1039   myMaxAngleSpin->SetValue(30.0);
1040
1041   myCriterionGrp->show();
1042 }
1043
1044 SMESHGUI_UnionOfTrianglesDlg::~SMESHGUI_UnionOfTrianglesDlg()
1045 {
1046 }
1047
1048 bool SMESHGUI_UnionOfTrianglesDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1049                                             const SMESH::long_array&    theIds)
1050 {
1051   SMESH::NumericalFunctor_var aCriterion = getNumericalFunctor();
1052   double aMaxAngle = myMaxAngleSpin->GetValue() * PI / 180.0;
1053   return theEditor->TriToQuad(theIds, aCriterion, aMaxAngle);
1054 }
1055
1056
1057 /*!
1058  *  Class       : SMESHGUI_CuttingOfQuadsDlg
1059  *  Description : Automatic splitting of quadrangles into triangles
1060  */
1061
1062 SMESHGUI_CuttingOfQuadsDlg
1063 ::SMESHGUI_CuttingOfQuadsDlg(SMESHGUI* theModule,
1064                              const char* theName):
1065   SMESHGUI_MultiEditDlg(theModule, SMESHGUI_QuadFilter, false, theName)
1066 {
1067   setCaption(tr("CAPTION"));
1068   myPreviewActor = 0;
1069
1070   myPreviewChk = new QCheckBox (tr("PREVIEW"), mySelGrp);
1071
1072   myCriterionGrp->show();
1073   myGroupChoice->show();
1074   myComboBoxFunctor->setEnabled(false);
1075
1076   connect(myPreviewChk     , SIGNAL(stateChanged(int))   , this, SLOT(onPreviewChk()));
1077   connect(myGroupChoice    , SIGNAL(clicked(int))        , this, SLOT(onCriterionRB()));
1078   connect(myComboBoxFunctor, SIGNAL(activated(int))      , this, SLOT(onPreviewChk()));
1079   connect(this             , SIGNAL(ListContensChanged()), this, SLOT(onPreviewChk()));
1080 }
1081
1082 SMESHGUI_CuttingOfQuadsDlg::~SMESHGUI_CuttingOfQuadsDlg()
1083 {
1084 }
1085
1086 void SMESHGUI_CuttingOfQuadsDlg::onClose()
1087 {
1088   erasePreview();
1089   SMESHGUI_MultiEditDlg::onClose();
1090 }
1091
1092 bool SMESHGUI_CuttingOfQuadsDlg::process (SMESH::SMESH_MeshEditor_ptr theEditor,
1093                                           const SMESH::long_array&    theIds)
1094 {
1095   switch (myGroupChoice->id(myGroupChoice->selected())) {
1096   case 0: // use diagonal 1-3
1097     return theEditor->SplitQuad(theIds, true);
1098   case 1: // use diagonal 2-4
1099     return theEditor->SplitQuad(theIds, false);
1100   default: // use numeric functor
1101     break;
1102   }
1103
1104   SMESH::NumericalFunctor_var aCriterion = getNumericalFunctor();
1105   return theEditor->QuadToTri(theIds, aCriterion);
1106 }
1107
1108 void SMESHGUI_CuttingOfQuadsDlg::onCriterionRB()
1109 {
1110   if (myGroupChoice->id(myGroupChoice->selected()) == 2) // Use numeric functor
1111     myComboBoxFunctor->setEnabled(true);
1112   else
1113     myComboBoxFunctor->setEnabled(false);
1114
1115   onPreviewChk();
1116 }
1117
1118 void SMESHGUI_CuttingOfQuadsDlg::onPreviewChk()
1119 {
1120   myPreviewChk->isChecked() ? displayPreview() : erasePreview();
1121 }
1122
1123 void SMESHGUI_CuttingOfQuadsDlg::erasePreview()
1124 {
1125   if (myPreviewActor == 0)
1126     return;
1127
1128   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
1129     vf->RemoveActor(myPreviewActor);
1130     vf->Repaint();
1131   }
1132   myPreviewActor->Delete();
1133   myPreviewActor = 0;
1134 }
1135   
1136 void SMESHGUI_CuttingOfQuadsDlg::displayPreview()
1137 {
1138   if (myActor == 0)
1139     return;
1140
1141   if (myPreviewActor != 0)
1142     erasePreview();
1143
1144   // get Ids of elements
1145   SMESH::long_array_var anElemIds = getIds();
1146   if (getIds()->length() == 0)
1147     return;
1148
1149   SMDS_Mesh* aMesh = myActor->GetObject()->GetMesh();
1150   if (aMesh == 0)
1151     return;
1152
1153   // 0 - use diagonal 1-3, 1 - use diagonal 2-4, 2 - use numerical functor
1154   int aChoice = myGroupChoice->id(myGroupChoice->selected());
1155   SMESH::NumericalFunctor_var aCriterion  = SMESH::NumericalFunctor::_nil();
1156   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH::SMESH_MeshEditor::_nil();
1157   if (aChoice == 2) {
1158     aCriterion  = getNumericalFunctor();
1159     aMeshEditor = myMesh->GetMeshEditor();
1160     if (aMeshEditor->_is_nil())
1161       return;
1162   }
1163
1164   //Create grid
1165   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
1166
1167   vtkIdType aNbCells = anElemIds->length() * 2;
1168   vtkIdType aCellsSize = 4 * aNbCells;
1169   vtkCellArray* aConnectivity = vtkCellArray::New();
1170   aConnectivity->Allocate(aCellsSize, 0);
1171
1172   vtkPoints* aPoints = vtkPoints::New();
1173   aPoints->SetNumberOfPoints(anElemIds->length() * 4);
1174
1175   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
1176   aCellTypesArray->SetNumberOfComponents(1);
1177   aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
1178
1179   vtkIdList *anIdList = vtkIdList::New();
1180   anIdList->SetNumberOfIds(3);
1181
1182   TColStd_DataMapOfIntegerInteger anIdToVtk;
1183
1184   int aNodes[ 4 ];
1185   int nbPoints = -1;
1186   for (int i = 0, n = anElemIds->length(); i < n; i++)
1187   {
1188     const SMDS_MeshElement* anElem = aMesh->FindElement(anElemIds[ i ]);
1189     if (anElem == 0 || anElem->NbNodes() != 4)
1190       continue;
1191
1192     SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
1193     int k = 0;
1194     while (anIter->more()) {
1195       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIter->next());
1196       if (aNode)
1197       {
1198         if (!anIdToVtk.IsBound(aNode->GetID()))
1199         {
1200           aPoints->SetPoint(++nbPoints, aNode->X(), aNode->Y(), aNode->Z());
1201           anIdToVtk.Bind(aNode->GetID(), nbPoints);
1202         }
1203
1204         aNodes[ k++ ] = aNode->GetID();
1205       }
1206     }
1207
1208     if (k != 4)
1209       continue;
1210
1211     bool isDiag13 = true;
1212     if (aChoice == 0) // use diagonal 1-3
1213     {
1214       isDiag13 = true;
1215     }
1216     else if (aChoice == 1) // use diagonal 2-4
1217     {
1218       isDiag13 = false;
1219     }
1220     else // use numerical functor
1221     {
1222       // compare two sets of possible triangles
1223       int diag = aMeshEditor->BestSplit(anElemIds[i], aCriterion);
1224       if (diag == 1) // 1-3
1225         isDiag13 = true;
1226       else if (diag == 2) // 2-4
1227         isDiag13 = false;
1228       else // error
1229         continue;
1230     }
1231
1232     if (isDiag13)
1233     {
1234       anIdList->SetId(0, anIdToVtk(aNodes[ 0 ]));
1235       anIdList->SetId(1, anIdToVtk(aNodes[ 1 ]));
1236       anIdList->SetId(2, anIdToVtk(aNodes[ 2 ]));
1237       aConnectivity->InsertNextCell(anIdList);
1238       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1239
1240       anIdList->SetId(0, anIdToVtk(aNodes[ 2 ]));
1241       anIdList->SetId(1, anIdToVtk(aNodes[ 3 ]));
1242       anIdList->SetId(2, anIdToVtk(aNodes[ 0 ]));
1243       aConnectivity->InsertNextCell(anIdList);
1244       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1245     }
1246     else
1247     {
1248       anIdList->SetId(0, anIdToVtk(aNodes[ 1 ]));
1249       anIdList->SetId(1, anIdToVtk(aNodes[ 2 ]));
1250       anIdList->SetId(2, anIdToVtk(aNodes[ 3 ]));
1251       aConnectivity->InsertNextCell(anIdList);
1252       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1253
1254       anIdList->SetId(0, anIdToVtk(aNodes[ 3 ]));
1255       anIdList->SetId(1, anIdToVtk(aNodes[ 0 ]));
1256       anIdList->SetId(2, anIdToVtk(aNodes[ 1 ]));
1257       aConnectivity->InsertNextCell(anIdList);
1258       aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1259     }
1260   }
1261
1262   vtkIntArray* aCellLocationsArray = vtkIntArray::New();
1263   aCellLocationsArray->SetNumberOfComponents(1);
1264   aCellLocationsArray->SetNumberOfTuples(aNbCells);
1265
1266   aConnectivity->InitTraversal();
1267   for(vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
1268     aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
1269
1270   aGrid->SetPoints(aPoints);
1271   aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
1272
1273   // Create and display actor
1274   vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
1275   aMapper->SetInput(aGrid);
1276
1277   myPreviewActor = SALOME_Actor::New();
1278   myPreviewActor->PickableOff();
1279   myPreviewActor->SetMapper(aMapper);
1280
1281   vtkProperty* aProp = vtkProperty::New();
1282   aProp->SetRepresentationToWireframe();
1283   aProp->SetColor(250, 0, 250);
1284   aProp->SetLineWidth(myActor->GetLineWidth() + 1);
1285   myPreviewActor->SetProperty(aProp);
1286
1287   SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
1288   SMESH::GetCurrentVtkView()->Repaint();
1289
1290   aProp->Delete();
1291   aPoints->Delete();
1292   aConnectivity->Delete();
1293   aGrid->Delete();
1294   aMapper->Delete();
1295   anIdList->Delete();
1296   aCellTypesArray->Delete();
1297   aCellLocationsArray->Delete();
1298 }