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