Salome HOME
a3a862b00609ce6a7074cfde7e6087f2f619ca39
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshPatternDlg.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_MeshPatternDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_MeshPatternDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_SpinBox.h"
32 #include "SMESHGUI_CreatePatternDlg.h"
33 #include "SMESHGUI_PatternWidget.h"
34 #include "SMESHGUI_Utils.h"
35 #include "SMESHGUI_VTKUtils.h"
36 #include "SMESHGUI_PatternUtils.h"
37 #include "SMESHGUI_GEOMGenUtils.h"
38
39 #include "SMESH_Actor.h"
40 #include "SMESH_ActorUtils.h"
41 #include "SMESH_NumberFilter.hxx"
42
43 #include "SMDS_Mesh.hxx"
44 #include "SMDS_MeshElement.hxx"
45
46 #include "SUIT_ResourceMgr.h"
47 #include "SUIT_Desktop.h"
48 #include "SUIT_FileDlg.h"
49
50 #include "LightApp_SelectionMgr.h"
51 #include "SalomeApp_Tools.h"
52 #include "SalomeApp_Study.h"
53
54 #include "SALOMEDS_SObject.hxx"
55
56 #include "SALOME_ListIO.hxx"
57 #include "SVTK_Selection.h"
58
59 #include "SVTK_ViewModel.h"
60 #include "SVTK_Selector.h"
61 #include "SVTK_ViewWindow.h"
62
63 // OCCT Includes
64 #include <TColStd_MapOfInteger.hxx>
65 #include <TColStd_IndexedMapOfInteger.hxx>
66
67 // QT Includes
68 #include <qframe.h>
69 #include <qlayout.h>
70 #include <qlineedit.h>
71 #include <qpushbutton.h>
72 #include <qgroupbox.h>
73 #include <qlabel.h>
74 #include <qradiobutton.h>
75 #include <qcheckbox.h>
76 #include <qbuttongroup.h>
77 #include <qmessagebox.h>
78 #include <qcstring.h>
79 #include <qspinbox.h>
80 #include <qvaluelist.h>
81 #include <qdir.h>
82 #include <qfile.h>
83 #include <qfileinfo.h>
84 #include <qfiledialog.h>
85
86 // VTK Includes
87 #include <vtkCell.h>
88 #include <vtkIdList.h>
89 #include <vtkIntArray.h>
90 #include <vtkCellArray.h>
91 #include <vtkUnsignedCharArray.h>
92 #include <vtkUnstructuredGrid.h>
93 #include <vtkDataSetMapper.h>
94
95 #define SPACING 5
96 #define MARGIN  10
97
98 /*!
99  *  Class       : SMESHGUI_MeshPatternDlg
100  *  Description : Dialog to specify filters for VTK viewer
101  */
102
103 //=======================================================================
104 // name    : SMESHGUI_MeshPatternDlg::SMESHGUI_MeshPatternDlg
105 // Purpose : Constructor
106 //=======================================================================
107 SMESHGUI_MeshPatternDlg::SMESHGUI_MeshPatternDlg( SMESHGUI*   theModule,
108                                                   const char* theName )
109      : QDialog( SMESH::GetDesktop( theModule ), theName, false, WStyle_Customize |
110                 WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
111        myBusy(false),
112        mySMESHGUI( theModule ),
113        mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
114 {
115   setCaption(tr("CAPTION"));
116
117   QVBoxLayout* aDlgLay = new QVBoxLayout(this, MARGIN, SPACING);
118
119   QFrame* aMainFrame = createMainFrame  (this);
120   QFrame* aBtnFrame  = createButtonFrame(this);
121
122   aDlgLay->addWidget(aMainFrame);
123   aDlgLay->addWidget(aBtnFrame);
124
125   aDlgLay->setStretchFactor(aMainFrame, 1);
126
127   myCreationDlg = 0;
128
129   mySelector = (SMESH::GetViewWindow( mySMESHGUI ))->GetSelector();
130
131   Init();
132 }
133
134 //=======================================================================
135 // name    : SMESHGUI_MeshPatternDlg::createMainFrame
136 // Purpose : Create frame containing dialog's input fields
137 //=======================================================================
138 QFrame* SMESHGUI_MeshPatternDlg::createMainFrame (QWidget* theParent)
139 {
140   SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( mySMESHGUI );
141   QPixmap iconSlct ( mgr->loadPixmap("SMESH", tr("ICON_SELECT")));
142   QPixmap icon2d   ( mgr->loadPixmap("SMESH", tr("ICON_PATTERN_2d")));
143   QPixmap icon3d   ( mgr->loadPixmap("SMESH", tr("ICON_PATTERN_3d")));
144   QPixmap iconOpen ( mgr->loadPixmap("STD", tr("ICON_FILE_OPEN")));
145
146   QPixmap iconSample2d ( mgr->loadPixmap("SMESH", tr("ICON_PATTERN_SAMPLE_2D")));
147   QPixmap iconSample3d ( mgr->loadPixmap("SMESH", tr("ICON_PATTERN_SAMPLE_3D")));
148
149   QGroupBox* aMainGrp = new QGroupBox (1, Qt::Horizontal, theParent);
150   aMainGrp->setFrameStyle(QFrame::NoFrame);
151   aMainGrp->setInsideMargin(0);
152
153   // Pattern type group
154
155   myTypeGrp = new QButtonGroup (1, Qt::Vertical, tr("PATTERN_TYPE"), aMainGrp);
156   mySwitch2d = new QRadioButton (myTypeGrp);
157   mySwitch3d = new QRadioButton (myTypeGrp);
158   mySwitch2d->setPixmap(icon2d);
159   mySwitch3d->setPixmap(icon3d);
160   myTypeGrp->insert(mySwitch2d, Type_2d);
161   myTypeGrp->insert(mySwitch3d, Type_3d);
162
163   // Mesh group
164
165   QGroupBox* aMeshGrp = new QGroupBox(1, Qt::Vertical, tr("SMESH_MESH"), aMainGrp);
166   new QLabel(tr("SMESH_MESH"), aMeshGrp);
167   mySelBtn[ Mesh ] = new QPushButton(aMeshGrp);
168   mySelBtn[ Mesh ]->setPixmap(iconSlct);
169   mySelEdit[ Mesh ] = new QLineEdit(aMeshGrp);
170   mySelEdit[ Mesh ]->setReadOnly(true);
171
172   // Pattern group
173
174   QGroupBox* aPatGrp = new QGroupBox(1, Qt::Horizontal, tr("PATTERN"), aMainGrp);
175
176   // pattern name
177   QGroupBox* aNameGrp = new QGroupBox(1, Qt::Vertical, aPatGrp);
178   aNameGrp->setFrameStyle(QFrame::NoFrame);
179   aNameGrp->setInsideMargin(0);
180   new QLabel(tr("PATTERN"), aNameGrp);
181   myName = new QLineEdit(aNameGrp);
182   myName->setReadOnly(true);
183   myOpenBtn = new QPushButton(aNameGrp);
184   myOpenBtn->setPixmap(iconOpen);
185   myNewBtn = new QPushButton(tr("NEW"), aNameGrp);
186
187   // Mode selection check box
188   myRefine = new QCheckBox(tr("REFINE"), aPatGrp);
189
190   // selection widgets for Apply to geom mode
191   myGeomGrp = new QGroupBox(3, Qt::Horizontal, aPatGrp);
192   myGeomGrp->setFrameStyle(QFrame::NoFrame);
193   myGeomGrp->setInsideMargin(0);
194
195   for (int i = Object; i <= Vertex2; i++)
196   {
197     mySelLbl[ i ] = new QLabel(myGeomGrp);
198     mySelBtn[ i ] = new QPushButton(myGeomGrp);
199     mySelBtn[ i ]->setPixmap(iconSlct);
200     mySelEdit[ i ] = new QLineEdit(myGeomGrp);
201     mySelEdit[ i ]->setReadOnly(true);
202   }
203
204   // Widgets for refinement of existing mesh elements
205   myRefineGrp = new QFrame(aPatGrp);
206   myRefineGrp->setFrameStyle(QFrame::NoFrame);
207   QGridLayout* aRefGrid = new QGridLayout(myRefineGrp, 3, 3, 0, 5);
208
209   mySelLbl[ Ids ] = new QLabel(myRefineGrp);
210   mySelBtn[ Ids ] = new QPushButton(myRefineGrp);
211   mySelBtn[ Ids ]->setPixmap(iconSlct);
212   mySelEdit[ Ids ] = new QLineEdit(myRefineGrp);
213
214   QLabel* aNodeLbl = new QLabel(tr("NODE_1"), myRefineGrp);
215   myNode1          = new QSpinBox(myRefineGrp);
216   myNode2Lbl       = new QLabel(tr("NODE_2"), myRefineGrp);
217   myNode2          = new QSpinBox(myRefineGrp);
218
219   aRefGrid->addWidget(mySelLbl [ Ids ], 0, 0);
220   aRefGrid->addWidget(mySelBtn [ Ids ], 0, 1);
221   aRefGrid->addWidget(mySelEdit[ Ids ], 0, 2);
222   aRefGrid->addWidget(aNodeLbl, 1, 0);
223   aRefGrid->addMultiCellWidget(myNode1, 1, 1, 1, 2);
224   aRefGrid->addWidget(myNode2Lbl, 2, 0);
225   aRefGrid->addMultiCellWidget(myNode2, 2, 2, 1, 2);
226
227   // reverse check box
228   myReverseChk = new QCheckBox(tr("REVERSE"), aPatGrp);
229
230   // CreatePoly check box
231   myCreatePolygonsChk = new QCheckBox( tr( "CREATE_POLYGONS_NEAR_BOUNDARY" ), aPatGrp );
232   myCreatePolyedrsChk = new QCheckBox( tr( "CREATE_POLYEDRS_NEAR_BOUNDARY" ), aPatGrp );
233
234   // Pictures 2d and 3d
235   for (int i = 0; i < 2; i++) {
236     if (i == 0) {
237       myPicture2d = new SMESHGUI_PatternWidget(aPatGrp),
238       myPicture2d->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
239     } else {
240       myPicture3d = new QFrame(aPatGrp),
241       myPreview3d = new QLabel(myPicture3d);
242       myPreview3d->setPixmap(iconSample3d);
243       QGridLayout* aLay = new QGridLayout(myPicture3d, 3, 3, 0, 0);
244       QSpacerItem* aSpacerH1 = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
245       QSpacerItem* aSpacerH2 = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
246       QSpacerItem* aSpacerV1 = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
247       QSpacerItem* aSpacerV2 = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
248       aLay->addItem(aSpacerH1, 1, 0);
249       aLay->addItem(aSpacerH2, 1, 2);
250       aLay->addItem(aSpacerV1, 0, 1);
251       aLay->addItem(aSpacerV2, 2, 1);
252       aLay->addWidget(myPreview3d, 1, 1);
253     }
254   }
255
256   myPreviewChk = new QCheckBox(tr("PREVIEW"), aPatGrp);
257
258   // Connect signals and slots
259
260   connect(myTypeGrp, SIGNAL(clicked(int)), SLOT(onTypeChanged(int)));
261   connect(myOpenBtn, SIGNAL(clicked()),    SLOT(onOpen()));
262   connect(myNewBtn,  SIGNAL(clicked()),    SLOT(onNew()));
263
264   connect(myReverseChk, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
265   connect(myPreviewChk, SIGNAL(toggled(bool)), SLOT(onPreview(bool)));
266   connect(myRefine,     SIGNAL(toggled(bool)), SLOT(onModeToggled(bool)));
267
268   connect(myNode1, SIGNAL(valueChanged(int)), SLOT(onNodeChanged(int)));
269   connect(myNode2, SIGNAL(valueChanged(int)), SLOT(onNodeChanged(int)));
270
271   connect(mySelEdit[Ids], SIGNAL(textChanged(const QString&)), SLOT(onTextChanged(const QString&)));
272
273   QMap< int, QPushButton* >::iterator anIter;
274   for (anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter)
275     connect(*anIter, SIGNAL(clicked()), SLOT(onSelInputChanged()));
276
277   return aMainGrp;
278 }
279
280 //=======================================================================
281 // name    : SMESHGUI_MeshPatternDlg::createButtonFrame
282 // Purpose : Create frame containing buttons
283 //=======================================================================
284 QFrame* SMESHGUI_MeshPatternDlg::createButtonFrame (QWidget* theParent)
285 {
286   QFrame* aFrame = new QFrame(theParent);
287   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
288
289   myOkBtn     = new QPushButton(tr("SMESH_BUT_OK"   ), aFrame);
290   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
291   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
292
293   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
294
295   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
296
297   aLay->addWidget(myOkBtn);
298   aLay->addWidget(myApplyBtn);
299   aLay->addItem(aSpacer);
300   aLay->addWidget(myCloseBtn);
301
302   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
303   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
304   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
305
306   return aFrame;
307 }
308
309 //=======================================================================
310 // name    : SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg
311 // Purpose : Destructor
312 //=======================================================================
313 SMESHGUI_MeshPatternDlg::~SMESHGUI_MeshPatternDlg()
314 {
315 }
316
317 //=======================================================================
318 // name    : SMESHGUI_MeshPatternDlg::Init
319 // Purpose : Init dialog fields, connect signals and slots, show dialog
320 //=======================================================================
321 void SMESHGUI_MeshPatternDlg::Init()
322 {
323   myPattern = SMESH::GetPattern();
324   myPreviewActor = 0;
325   myIsCreateDlgOpen = false;
326   mySelInput = Mesh;
327   myType = -1;
328   myNbPoints = -1;
329   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
330   myMesh = SMESH::SMESH_Mesh::_nil();
331
332   myMeshShape = GEOM::GEOM_Object::_nil();
333   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();
334   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();
335   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();
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   myTypeGrp->setButton(Type_2d);
343   onTypeChanged(Type_2d);
344   onModeToggled(isRefine());
345
346   updateGeometry();
347
348   resize(minimumSize());
349
350   activateSelection();
351   onSelectionDone();
352
353   int x, y;
354   mySMESHGUI->DefineDlgPosition(this, x, y);
355   this->move(x, y);
356   this->show();
357 }
358
359 //=======================================================================
360 // name    : SMESHGUI_MeshPatternDlg::isValid
361 // Purpose : Verify validity of entry data
362 //=======================================================================
363 bool SMESHGUI_MeshPatternDlg::isValid (const bool theMess)
364 {
365   QValueList<int> ids;
366   if ((isRefine() &&
367        (myMesh->_is_nil() || !getIds(ids) || getNode(false) < 0 ||
368         myType == Type_3d && (getNode(true) < 0 || getNode(false) == getNode(true))))
369       ||
370       (!isRefine() &&
371        (myMesh->_is_nil() || myMeshShape->_is_nil() || myGeomObj[ Object ]->_is_nil() ||
372         myGeomObj[ Vertex1 ]->_is_nil() || myType == Type_3d && myGeomObj[ Vertex2 ]->_is_nil())))
373   {
374     if (theMess)
375       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
376                                tr("SMESHGUI_INVALID_PARAMETERS"), QMessageBox::Ok);
377     return false;
378   }
379
380   return true;
381 }
382
383 //=======================================================================
384 // name    : SMESHGUI_MeshPatternDlg::onApply
385 // Purpose : SLOT called when "Apply" button pressed.
386 //=======================================================================
387 bool SMESHGUI_MeshPatternDlg::onApply()
388 {
389   try {
390     if (!isValid())
391       return false;
392
393     erasePreview();
394
395     if (isRefine()) { // Refining existing mesh elements
396       QValueList<int> ids;
397       getIds(ids);
398       SMESH::long_array_var varIds = new SMESH::long_array();
399       varIds->length(ids.count());
400       int i = 0;
401       for (QValueList<int>::iterator it = ids.begin(); it != ids.end(); ++it)
402         varIds[i++] = *it;
403       myType == Type_2d
404         ? myPattern->ApplyToMeshFaces  (myMesh, varIds, getNode(false), myReverseChk->isChecked())
405         : myPattern->ApplyToHexahedrons(myMesh, varIds, getNode(false), getNode(true));
406
407     } else { // Applying a pattern to geometrical object
408       if (myType == Type_2d)
409         myPattern->ApplyToFace(myGeomObj[Object], myGeomObj[Vertex1], myReverseChk->isChecked());
410       else
411         myPattern->ApplyTo3DBlock(myGeomObj[Object], myGeomObj[Vertex1], myGeomObj[Vertex2]);
412     }
413
414     bool toCreatePolygons = myCreatePolygonsChk->isChecked();
415     bool toCreatePolyedrs = myCreatePolyedrsChk->isChecked();
416     if ( myPattern->MakeMesh( myMesh, toCreatePolygons, toCreatePolyedrs ) ) {
417       mySelectionMgr->clearSelected();
418       bool autoUpdate = SMESHGUI::automaticUpdate();
419       if (!isRefine() && autoUpdate) {
420         _PTR(SObject) aSO = SMESH::FindSObject(myMesh.in());
421         SMESH_Actor* anActor = SMESH::FindActorByEntry(aSO->GetID().c_str());
422         if (!anActor) {
423           anActor = SMESH::CreateActor(aSO->GetStudy(), aSO->GetID().c_str());
424           if (anActor) {
425             SMESH::DisplayActor(SMESH::GetActiveWindow(), anActor);
426             SMESH::FitAll();
427           }
428         }
429       }
430       SMESH::UpdateView();
431
432       mySMESHGUI->updateObjBrowser(true);
433       return true;
434     } else {
435       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
436                                tr("SMESH_OPERATION_FAILED"), QMessageBox::Ok);
437       return false;
438     }
439   } catch (const SALOME::SALOME_Exception& S_ex) {
440     SalomeApp_Tools::QtCatchCorbaException(S_ex);
441   } catch (...) {
442   }
443
444   return false;
445 }
446
447 //=======================================================================
448 // name    : SMESHGUI_MeshPatternDlg::onOk
449 // Purpose : SLOT called when "Ok" button pressed.
450 //=======================================================================
451 void SMESHGUI_MeshPatternDlg::onOk()
452 {
453   if (onApply())
454     onClose();
455 }
456
457 //=======================================================================
458 // name    : SMESHGUI_MeshPatternDlg::onClose
459 // Purpose : SLOT called when "Close" button pressed. Close dialog
460 //=======================================================================
461 void SMESHGUI_MeshPatternDlg::onClose()
462 {
463   mySelectionMgr->clearFilters();
464   SMESH::SetPickable();
465   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
466     aViewWindow->SetSelectionMode(ActorSelection);
467   disconnect(mySelectionMgr, 0, this, 0);
468   disconnect(mySMESHGUI, 0, this, 0);
469   mySMESHGUI->ResetState();
470   erasePreview();
471   reject();
472 }
473
474 //=======================================================================
475 // name    : SMESHGUI_MeshPatternDlg::onSelectionDone
476 // Purpose : SLOT called when selection changed
477 //=======================================================================
478 void SMESHGUI_MeshPatternDlg::onSelectionDone()
479 {
480   if (myBusy)
481     return;
482
483   try {
484     if (mySelInput == Mesh) {
485       SALOME_ListIO aList;
486       mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
487       if (aList.Extent() != 1)
488         return;
489
490       // Retrieve mesh from selection
491       Handle(SALOME_InteractiveObject) anIO = aList.First();
492       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
493       if (aMesh->_is_nil())
494         return;
495
496       // Get geom object corresponding to the mesh
497       _PTR(SObject) aSO = SMESH::FindSObject(aMesh.in());
498       myMeshShape = SMESH::GetGeom(aSO);
499
500       // Clear fields of geom objects if mesh was changed
501       if (myMesh != aMesh) {
502         for (int i = Object; i <= Ids; i++) {
503           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();
504           mySelEdit[ i ]->setText("");
505         }
506       }
507
508       myMesh = aMesh;
509
510       // Set name of mesh in line edit
511       QString aName;
512       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
513       mySelEdit[ Mesh ]->setText(aName);
514
515     } else if (mySelInput == Ids) {
516       SALOME_ListIO aList;
517       mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
518       if (aList.Extent() != 1)
519         return;
520
521       QString anIds;
522       if (!SMESH::GetNameOfSelectedElements(mySelector, aList.First(), anIds))
523         anIds = "";
524
525       myBusy = true;
526       mySelEdit[ Ids ]->setText(anIds);
527       myBusy = false;
528
529     } else {
530       SALOME_ListIO aList;
531       mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
532       if (aList.Extent() != 1)
533         return;
534
535       // Get geom object from selection
536       Handle(SALOME_InteractiveObject) anIO = aList.First();
537       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
538       if (anObj->_is_nil())
539         return;
540
541       // Clear fields of vertexes if face or 3d block was changed
542       if (anObj != myGeomObj[ mySelInput ] && mySelInput == Object) {
543         for (int i = Vertex1; i <= Vertex2; i++) {
544           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();
545           mySelEdit[ i ]->setText("");
546         }
547       }
548
549       myGeomObj[ mySelInput ] = anObj;
550
551       // Set name of geom object in line edit
552       QString aName;
553       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
554       mySelEdit[ mySelInput ]->setText(aName);
555     }
556   } catch (const SALOME::SALOME_Exception& S_ex) {
557     SalomeApp_Tools::QtCatchCorbaException(S_ex);
558     resetSelInput();
559   } catch (...) {
560     resetSelInput();
561   }
562
563   updateWgState();
564   displayPreview();
565 }
566
567 //=======================================================================
568 // name    : SMESHGUI_MeshPatternDlg::resetSelInput
569 // Purpose : Reset fields corresponding to the current selection input
570 //=======================================================================
571 void SMESHGUI_MeshPatternDlg::resetSelInput()
572 {
573   if (mySelInput == Mesh)
574   {
575     myMesh = SMESH::SMESH_Mesh::_nil();
576     myMeshShape = GEOM::GEOM_Object::_nil();
577   }
578
579   else
580     myGeomObj[ mySelInput ] = GEOM::GEOM_Object::_nil();
581
582   mySelEdit[ mySelInput ]->setText("");
583 }
584
585 //=======================================================================
586 // name    : SMESHGUI_MeshPatternDlg::onDeactivate
587 // Purpose : SLOT called when dialog must be deativated
588 //=======================================================================
589 void SMESHGUI_MeshPatternDlg::onDeactivate()
590 {
591   mySelectionMgr->clearFilters();
592   //if (myReverseChk->isChecked())
593   //  erasePreview();
594   disconnect(mySelectionMgr, 0, this, 0);
595   setEnabled(false);
596 }
597
598 //=======================================================================
599 // name    : SMESHGUI_MeshPatternDlg::enterEvent
600 // Purpose : Event filter
601 //=======================================================================
602 void SMESHGUI_MeshPatternDlg::enterEvent (QEvent*)
603 {
604   if (myIsCreateDlgOpen)
605     return;
606
607   if (myReverseChk->isChecked())
608     displayPreview();
609   mySMESHGUI->EmitSignalDeactivateDialog();
610   setEnabled(true);
611   activateSelection();
612   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
613   onTextChanged(mySelEdit[Ids]->text());
614 }
615
616 //=======================================================================
617 // name    : SMESHGUI_MeshPatternDlg::closeEvent
618 // Purpose :
619 //=======================================================================
620 void SMESHGUI_MeshPatternDlg::closeEvent (QCloseEvent*)
621 {
622   onClose();
623 }
624
625 //=======================================================================
626 // name    : SMESHGUI_MeshPatternDlg::onSelInputChanged
627 // Purpose : SLOT. Called when -> button clicked.
628 //           Change current selection input field
629 //=======================================================================
630 void SMESHGUI_MeshPatternDlg::onSelInputChanged()
631 {
632   const QObject* aSender = sender();
633   for (int i = Mesh; i <= Ids; i++)
634     if (aSender == mySelBtn[ i ])
635       mySelInput = i;
636
637   activateSelection();
638   onSelectionDone();
639 }
640
641 //=======================================================================
642 // name    : SMESHGUI_MeshPatternDlg::prepareFilters
643 // Purpose : Prepare filters for dialog
644 //=======================================================================
645 QStringList SMESHGUI_MeshPatternDlg::prepareFilters() const
646 {
647   static QStringList aList;
648   if (aList.isEmpty())
649   {
650     aList.append(tr("PATTERN_FILT"));
651     //aList.append(tr("ALL_FILES_FILTER"));
652   }
653
654   return aList;
655 }
656
657 //=======================================================================
658 // name    : SMESHGUI_MeshPatternDlg::autoExtension
659 // Purpose : Append extension to the file name
660 //=======================================================================
661 QString SMESHGUI_MeshPatternDlg::autoExtension (const QString& theFileName) const
662 {
663   QString anExt = theFileName.section('.', -1);
664   return anExt != "smp" && anExt != "SMP" ? theFileName + ".smp" : theFileName;
665 }
666
667 //=======================================================================
668 // name    : SMESHGUI_MeshPatternDlg::onOpen
669 // Purpose : SLOT. Called when "Open" button clicked.
670 //           Displays file open dialog
671 //=======================================================================
672 void SMESHGUI_MeshPatternDlg::onOpen()
673 {
674   SUIT_FileDlg* aDlg = new SUIT_FileDlg (this, true);
675   aDlg->setCaption(tr("LOAD_PATTERN"));
676   aDlg->setMode(QFileDialog::ExistingFile);
677   aDlg->setFilters(prepareFilters());
678   if (myName->text() != "")
679     aDlg->setSelection(myName->text() + ".smp");
680   QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
681   if (anOkBtn != 0)
682     anOkBtn->setText(tr("SMESH_BUT_OK"));
683
684   if (aDlg->exec() != Accepted)
685     return;
686
687   QString fName = aDlg->selectedFile();
688   if (fName.isEmpty())
689     return;
690
691   if (QFileInfo(fName).extension().isEmpty())
692     fName = autoExtension(fName);
693
694   fName = QDir::convertSeparators(fName);
695
696   QString prev = QDir::convertSeparators(myName->text());
697   if (prev == fName)
698     return;
699
700   // Read string from file
701   QFile aFile(fName);
702   if (!aFile.open(IO_ReadOnly)) {
703     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
704                              tr("ERROR_OF_OPENING"), QMessageBox::Ok);
705     return;
706   }
707
708   QByteArray aDataArray = aFile.readAll();
709   const char* aData = aDataArray.data();
710   if (aData == 0) {
711     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
712                              tr("ERROR_OF_READING"), QMessageBox::Ok);
713     return;
714   }
715
716   if (loadFromFile(aData))
717     myName->setText(QFileInfo(fName).baseName());
718
719   updateWgState();
720   displayPreview();
721 }
722
723 //=======================================================================
724 // name    : SMESHGUI_MeshPatternDlg::onCloseCreationDlg
725 // Purpose : SLOT. Called when "Pattern creation" dialog closed with "Close"
726 //=======================================================================
727 void SMESHGUI_MeshPatternDlg::onCloseCreationDlg()
728 {
729   setEnabled(true);
730   myIsCreateDlgOpen = false;
731 }
732
733 //=======================================================================
734 // name    : SMESHGUI_MeshPatternDlg::onOkCreationDlg
735 // Purpose : SLOT. Called when "Pattern creation" dialog closed with OK
736 //           or SAVE buttons. Initialize myPattern field. Redisplay preview
737 //=======================================================================
738 void SMESHGUI_MeshPatternDlg::onOkCreationDlg()
739 {
740   myPattern = SMESH::SMESH_Pattern::_duplicate(myCreationDlg->GetPattern());
741   myName->setText(myCreationDlg->GetPatternName());
742
743   updateWgState();
744   displayPreview();
745
746   setEnabled(true);
747   myIsCreateDlgOpen = false;
748 }
749
750 //=======================================================================
751 // name    : SMESHGUI_MeshPatternDlg::onNew
752 // Purpose : SLOT. Called when "New..." button clicked. Create new pattern
753 //=======================================================================
754 void SMESHGUI_MeshPatternDlg::onNew()
755 {
756   setEnabled(false);
757   myIsCreateDlgOpen = true;
758   if (myCreationDlg == 0)
759   {
760     myCreationDlg = new SMESHGUI_CreatePatternDlg( mySMESHGUI, myType);
761     connect(myCreationDlg, SIGNAL(NewPattern()), SLOT(onOkCreationDlg()));
762     connect(myCreationDlg, SIGNAL(Close()), SLOT(onCloseCreationDlg()));
763   }
764   else
765     myCreationDlg->Init(myType);
766
767   myCreationDlg->SetMesh(myMesh);
768   myCreationDlg->show();
769 }
770
771 //=======================================================================
772 // name    : SMESHGUI_MeshPatternDlg::onReverse
773 // Purpose : SLOT. Called when state of "Reverse order..." checkbox chaged
774 //           Calculate new points of the mesh to be created. Redisplay preview
775 //=======================================================================
776 void SMESHGUI_MeshPatternDlg::onReverse (bool)
777 {
778   displayPreview();
779 }
780
781 //=======================================================================
782
783 // name    : SMESHGUI_MeshPatternDlg::onPreview
784 // Purpose : SLOT. Called when state of "Preview" checkbox changed
785 //           Display/Erase preview
786 //=======================================================================
787 void SMESHGUI_MeshPatternDlg::onPreview (bool)
788 {
789   displayPreview();
790 }
791
792 //=======================================================================
793 // name    : SMESHGUI_MeshPatternDlg::displayPreview
794 // Purpose : Display preview
795 //=======================================================================
796 void SMESHGUI_MeshPatternDlg::displayPreview()
797 {
798   try {
799     // Redisplay preview in dialog
800     SMESH::point_array_var pnts = myPattern->GetPoints();
801     SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
802     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints(false);
803
804     if (pnts->length()       == 0 ||
805         keyPoints->length()  == 0 ||
806         elemPoints->length() == 0) {
807       erasePreview();
808       return;
809     } else {
810       PointVector aPoints(pnts->length());
811       QValueVector<int> aKeyPoints(keyPoints->length());
812       ConnectivityVector anElemPoints(elemPoints->length());
813
814       for (int i = 0, n = pnts->length(); i < n; i++)
815         aPoints[ i ] = pnts[ i ];
816
817       for (int i2 = 0, n2 = keyPoints->length(); i2 < n2; i2++)
818         aKeyPoints[ i2 ] = keyPoints[ i2 ];
819
820       for (int i3 = 0, n3 = elemPoints->length(); i3 < n3; i3++) {
821         QValueVector<int> aVec(elemPoints[ i3 ].length());
822         for (int i4 = 0, n4 = elemPoints[ i3 ].length(); i4 < n4; i4++)
823           aVec[ i4 ] = elemPoints[ i3 ][ i4 ];
824
825         anElemPoints[ i3 ] = aVec;
826       }
827
828       myPicture2d->SetPoints(aPoints, aKeyPoints, anElemPoints);
829     }
830
831     // Redisplay preview in 3D viewer
832     if (myPreviewActor != 0) {
833       if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
834         vf->RemoveActor(myPreviewActor);
835         vf->Repaint();
836       }
837       myPreviewActor->Delete();
838       myPreviewActor = 0;
839     }
840
841     if (!myPreviewChk->isChecked() || !isValid(false))
842       return;
843
844     vtkUnstructuredGrid* aGrid = getGrid();
845     if (aGrid == 0)
846       return;
847
848     // Create and display actor
849     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
850     aMapper->SetInput(aGrid);
851
852     myPreviewActor = SALOME_Actor::New();
853     myPreviewActor->PickableOff();
854     myPreviewActor->SetMapper(aMapper);
855
856     vtkProperty* aProp = vtkProperty::New();
857     aProp->SetRepresentationToWireframe();
858     aProp->SetColor(250, 0, 250);
859     if (SMESH::FindActorByObject(myMesh))
860       aProp->SetLineWidth( SMESH::GetFloat( "SMESH:element_width", 1 ) + 1 );
861     else
862       aProp->SetLineWidth(1);
863     myPreviewActor->SetProperty(aProp);
864
865     myPreviewActor->SetRepresentation(3);
866
867     SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
868     SMESH::GetCurrentVtkView()->Repaint();
869
870     aProp->Delete();
871     aGrid->Delete();
872   } catch (const SALOME::SALOME_Exception& S_ex) {
873     SalomeApp_Tools::QtCatchCorbaException(S_ex);
874     erasePreview();
875   } catch (...) {
876     erasePreview();
877   }
878 }
879
880 //=======================================================================
881 // name    : SMESHGUI_MeshPatternDlg::erasePreview
882 // Purpose : Erase preview
883 //=======================================================================
884 void SMESHGUI_MeshPatternDlg::erasePreview()
885 {
886   // Erase preview in 2D viewer
887   myPicture2d->SetPoints(PointVector(), QValueVector<int>(), ConnectivityVector());
888
889   // Erase preview in 3D viewer
890   if (myPreviewActor == 0)
891     return;
892
893
894   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView())
895   {
896     vf->RemoveActor(myPreviewActor);
897     vf->Repaint();
898   }
899   myPreviewActor->Delete();
900   myPreviewActor = 0;
901 }
902
903 //=======================================================================
904 // name    : SMESHGUI_MeshPatternDlg::updateWgState
905 // Purpose : Enable/disable selection widgets
906 //=======================================================================
907 void SMESHGUI_MeshPatternDlg::updateWgState()
908 {
909   if (myMesh->_is_nil()) {
910     for (int i = Object; i <= Ids; i++) {
911       mySelBtn [ i ]->setEnabled(false);
912       mySelEdit[ i ]->setEnabled(false);
913       mySelEdit[ i ]->setText("");
914     }
915     myNode1->setEnabled(false);
916     myNode2->setEnabled(false);
917     myNode1->setRange(0, 0);
918     myNode2->setRange(0, 0);
919   } else {
920     mySelBtn [ Object ]->setEnabled(true);
921     mySelEdit[ Object ]->setEnabled(true);
922     mySelBtn [ Ids ]   ->setEnabled(true);
923     mySelEdit[ Ids ]   ->setEnabled(true);
924
925     if (myGeomObj[ Object ]->_is_nil()) {
926       for (int i = Vertex1; i <= Vertex2; i++) {
927         mySelBtn [ i ]->setEnabled(false);
928         mySelEdit[ i ]->setEnabled(false);
929         mySelEdit[ i ]->setText("");
930       }
931     } else {
932       for (int i = Object; i <= Vertex2; i++) {
933         mySelBtn [ i ]->setEnabled(true);
934         mySelEdit[ i ]->setEnabled(true);
935       }
936     }
937
938     QValueList<int> ids;
939     if (!CORBA::is_nil(myPattern)/* && getIds(ids)*/) {
940       SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
941       if (keyPoints->length()) {
942         myNode1->setEnabled(true);
943         myNode2->setEnabled(true);
944         myNode1->setRange(1, keyPoints->length());
945         myNode2->setRange(1, keyPoints->length());
946         return;
947       }
948     }
949
950     myNode1->setEnabled(false);
951     myNode2->setEnabled(false);
952     myNode1->setRange(0, 0);
953     myNode2->setRange(0, 0);
954   }
955 }
956
957 //=======================================================================
958 // name    : SMESHGUI_MeshPatternDlg::activateSelection
959 // Purpose : Activate selection in accordance with current selection input
960 //=======================================================================
961 void SMESHGUI_MeshPatternDlg::activateSelection()
962 {
963   mySelectionMgr->clearFilters();
964   if (mySelInput == Ids) {
965     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
966     if (anActor)
967       SMESH::SetPickable(anActor);
968
969     if (myType == Type_2d)
970       {
971         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
972           aViewWindow->SetSelectionMode(FaceSelection);
973       }
974     else
975       {
976         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
977           aViewWindow->SetSelectionMode(CellSelection);
978       }
979   }
980   else {
981     SMESH::SetPickable();
982     //mySelectionMgr->setSelectionModes(ActorSelection);
983     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
984       aViewWindow->SetSelectionMode(ActorSelection);
985   }
986
987   if (mySelInput == Object && !myMeshShape->_is_nil()) {
988     if (myType == Type_2d) {
989       if (myNbPoints > 0)
990         mySelectionMgr->installFilter
991           (new SMESH_NumberFilter ("GEOM", TopAbs_VERTEX, myNbPoints, TopAbs_FACE, myMeshShape));
992       else
993         mySelectionMgr->installFilter
994           (new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, myNbPoints, TopAbs_FACE, myMeshShape));
995     } else {
996       TColStd_MapOfInteger aTypes;
997       aTypes.Add(TopAbs_SHELL);
998       aTypes.Add(TopAbs_SOLID);
999       mySelectionMgr->installFilter
1000         (new SMESH_NumberFilter ("GEOM", TopAbs_FACE, 6, aTypes, myMeshShape, true));
1001     }
1002   } else if ((mySelInput == Vertex1 || mySelInput == Vertex2) && !myGeomObj[ Object ]->_is_nil()) {
1003     mySelectionMgr->installFilter
1004       (new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 1, TopAbs_VERTEX, myGeomObj[ Object ]));
1005   } else {
1006   }
1007 }
1008
1009 //=======================================================================
1010 // name    : SMESHGUI_MeshPatternDlg::loadFromFile
1011 // Purpose : Load pattern from file
1012 //=======================================================================
1013 bool SMESHGUI_MeshPatternDlg::loadFromFile (const QString& theName)
1014 {
1015   try {
1016     SMESH::SMESH_Pattern_var aPattern = SMESH::GetPattern();
1017
1018     if (!aPattern->LoadFromFile(theName.latin1()) ||
1019         myType == Type_2d && !aPattern->Is2D()) {
1020       SMESH::SMESH_Pattern::ErrorCode aCode = aPattern->GetErrorCode();
1021       QString aMess;
1022       if      (aCode == SMESH::SMESH_Pattern::ERR_READ_NB_POINTS     ) aMess = tr("ERR_READ_NB_POINTS");
1023       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_POINT_COORDS  ) aMess = tr("ERR_READ_POINT_COORDS");
1024       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_TOO_FEW_POINTS) aMess = tr("ERR_READ_TOO_FEW_POINTS");
1025       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_3D_COORD      ) aMess = tr("ERR_READ_3D_COORD");
1026       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_NO_KEYPOINT   ) aMess = tr("ERR_READ_NO_KEYPOINT");
1027       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_INDEX     ) aMess = tr("ERR_READ_BAD_INDEX");
1028       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_ELEM_POINTS   ) aMess = tr("ERR_READ_ELEM_POINTS");
1029       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_NO_ELEMS      ) aMess = tr("ERR_READ_NO_ELEMS");
1030       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_KEY_POINT ) aMess = tr("ERR_READ_BAD_KEY_POINT");
1031       else                                                             aMess = tr("ERROR_OF_LOADING");
1032
1033       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"), aMess, QMessageBox::Ok);
1034       return false;
1035     } else {
1036       myPattern = aPattern;
1037       return true;
1038     }
1039   } catch (const SALOME::SALOME_Exception& S_ex) {
1040     SalomeApp_Tools::QtCatchCorbaException(S_ex);
1041     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1042                              tr("ERROR_OF_LOADING"), QMessageBox::Ok);
1043       return false;
1044   }
1045 }
1046
1047 //=======================================================================
1048 // name    : SMESHGUI_MeshPatternDlg::onTypeChanged
1049 // Purpose : SLOT. Called when pattern type changed.
1050 //           Change dialog's look and feel
1051 //=======================================================================
1052 void SMESHGUI_MeshPatternDlg::onTypeChanged (int theType)
1053 {
1054   if (myType == theType)
1055     return;
1056
1057   myType = theType;
1058
1059   myNbPoints = -1;
1060   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();
1061   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();
1062   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();
1063   myPattern = SMESH::GetPattern();
1064
1065   myName->setText("");
1066   mySelEdit[ Object  ]->setText("");
1067   mySelEdit[ Vertex1 ]->setText("");
1068   mySelEdit[ Vertex2 ]->setText("");
1069   mySelEdit[ Ids ]    ->setText("");
1070   myCreatePolygonsChk->show();
1071   myCreatePolyedrsChk->show();
1072
1073   if (theType == Type_2d) {
1074     // Geom widgets
1075     mySelLbl [ Vertex2 ]->hide();
1076     mySelBtn [ Vertex2 ]->hide();
1077     mySelEdit[ Vertex2 ]->hide();
1078     myReverseChk->show();
1079     myPicture2d->show();
1080     myPicture3d->hide();
1081     mySelLbl[ Object  ]->setText(tr("FACE"));
1082     mySelLbl[ Vertex1 ]->setText(tr("VERTEX"));
1083     // Refine widgets
1084     mySelLbl[ Ids ]->setText(tr("MESH_FACES"));
1085     myNode2Lbl->hide();
1086     myNode2   ->hide();
1087   } else {
1088     // Geom widgets
1089     mySelLbl [ Vertex2 ]->show();
1090     mySelBtn [ Vertex2 ]->show();
1091     mySelEdit[ Vertex2 ]->show();
1092     myReverseChk->hide();
1093     myPicture2d->hide();
1094     myPicture3d->show();
1095     mySelLbl[ Object  ]->setText(tr("3D_BLOCK"));
1096     mySelLbl[ Vertex1 ]->setText(tr("VERTEX1"));
1097     mySelLbl[ Vertex2 ]->setText(tr("VERTEX2"));
1098     // Refine widgets
1099     mySelLbl[ Ids ]->setText(tr("MESH_VOLUMES"));
1100     myNode2Lbl->show();
1101     myNode2   ->show();
1102   }
1103
1104   mySelInput = Mesh;
1105   activateSelection();
1106   updateWgState();
1107   displayPreview();
1108 }
1109
1110 //=======================================================================
1111 // name    : SMESHGUI_MeshPatternDlg::getGrid
1112 // Purpose : Get unstructured grid for pattern
1113 //=======================================================================
1114 vtkUnstructuredGrid* SMESHGUI_MeshPatternDlg::getGrid()
1115 {
1116   try {
1117     // Get points from pattern
1118     SMESH::point_array_var pnts;
1119     QValueList<int> ids;
1120     if (isRefine() && getIds(ids)) {
1121       SMESH::long_array_var varIds = new SMESH::long_array();
1122       varIds->length(ids.count());
1123       int i = 0;
1124       for (QValueList<int>::iterator it = ids.begin(); it != ids.end(); ++it)
1125         varIds[i++] = *it;
1126       pnts = myType == Type_2d
1127         ? myPattern->ApplyToMeshFaces  (myMesh, varIds, getNode(false), myReverseChk->isChecked())
1128         : myPattern->ApplyToHexahedrons(myMesh, varIds, getNode(false), getNode(true));
1129     } else {
1130       pnts = myType == Type_2d
1131         ? myPattern->ApplyToFace   (myGeomObj[ Object ], myGeomObj[ Vertex1 ], myReverseChk->isChecked())
1132       : myPattern->ApplyTo3DBlock(myGeomObj[ Object ], myGeomObj[ Vertex1 ], myGeomObj[ Vertex2 ]);
1133     }
1134
1135     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints(true);
1136
1137     if (pnts->length() == 0 || elemPoints->length() == 0)
1138       return 0;
1139
1140     // to do : to be removed /////////////////////////////////////////////
1141
1142 #ifdef DEB_SLN
1143     for (int i1 = 0, n1 = pnts->length(); i1 < n1; i1++)
1144       printf("%d: %g %g %g\n", i1, pnts[ i1 ].x, pnts[ i1 ].y, pnts[ i1 ].z);
1145
1146     printf("\nELEMENTS : \n");
1147     for (int i2 = 0, n2 = elemPoints->length(); i2 < n2; i2++)
1148     {
1149
1150       printf("%d: ", i2);
1151       for (int i3 = 0, n3 = elemPoints[ i2 ].length(); i3 < n3; i3++)
1152         printf("%d ", elemPoints[ i2 ][ i3 ]);
1153
1154       printf("\n");
1155
1156     }
1157 #endif
1158     //////////////////////////////////////////////////////////////////////
1159
1160     // Calculate number of points used for cell
1161     vtkIdType aNbCells = elemPoints->length();
1162     vtkIdType aCellsSize = 0;
1163     for (int i = 0, n = elemPoints->length(); i < n; i++)
1164       aCellsSize += elemPoints[ i ].length();
1165
1166     // Create unstructured grid and other  usefull arrays
1167     vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
1168
1169     vtkCellArray* aConnectivity = vtkCellArray::New();
1170     aConnectivity->Allocate(aCellsSize, 0);
1171
1172     vtkPoints* aPoints = vtkPoints::New();
1173     aPoints->SetNumberOfPoints(pnts->length());
1174
1175     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
1176     aCellTypesArray->SetNumberOfComponents(1);
1177     aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
1178
1179     vtkIdList *anIdList = vtkIdList::New();
1180
1181     // Fill array of points
1182     for (int p = 0, nbPnt = pnts->length(); p < nbPnt; p++)
1183       aPoints->SetPoint(p, pnts[ p ].x, pnts[ p ].y, pnts[ p ].z);
1184
1185     for (int e = 0, nbElem = elemPoints->length(); e < nbElem; e++) {
1186       int nbPoints = elemPoints[ e ].length();
1187       anIdList->SetNumberOfIds(nbPoints);
1188       for (int i = 0; i < nbPoints; i++)
1189         anIdList->SetId(i, elemPoints[ e ][ i ]);
1190
1191       aConnectivity->InsertNextCell(anIdList);
1192
1193       if      (nbPoints == 3) aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1194       else if (nbPoints == 5) aCellTypesArray->InsertNextValue(VTK_PYRAMID);
1195       else if (nbPoints == 6) aCellTypesArray->InsertNextValue(VTK_WEDGE);
1196       else if (nbPoints == 8) aCellTypesArray->InsertNextValue(VTK_HEXAHEDRON);
1197       else if (nbPoints == 4 && myType == Type_2d) aCellTypesArray->InsertNextValue(VTK_QUAD);
1198       else if (nbPoints == 4 && myType == Type_3d) aCellTypesArray->InsertNextValue(VTK_TETRA);
1199       else aCellTypesArray->InsertNextValue(VTK_EMPTY_CELL);
1200     }
1201
1202     vtkIntArray* aCellLocationsArray = vtkIntArray::New();
1203     aCellLocationsArray->SetNumberOfComponents(1);
1204     aCellLocationsArray->SetNumberOfTuples(aNbCells);
1205
1206     aConnectivity->InitTraversal();
1207     for (vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
1208       aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
1209
1210     aGrid->SetPoints(aPoints);
1211     aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
1212
1213     aConnectivity->Delete();
1214     aPoints->Delete();
1215     aCellTypesArray->Delete();
1216     anIdList->Delete();
1217     aCellLocationsArray->Delete();
1218
1219     return aGrid;
1220   } catch (...) {
1221     return 0;
1222   }
1223 }
1224
1225 //=======================================================================
1226 // name    : onModeToggled
1227 // Purpose :
1228 //=======================================================================
1229 void SMESHGUI_MeshPatternDlg::onModeToggled (bool on)
1230 {
1231   on ? myRefineGrp->show() : myRefineGrp->hide();
1232   on ? myGeomGrp->hide()   : myGeomGrp->show();
1233
1234   displayPreview();
1235 }
1236
1237 //=======================================================================
1238 // name    : isRefine
1239 // Purpose :
1240 //=======================================================================
1241 bool SMESHGUI_MeshPatternDlg::isRefine() const
1242 {
1243   return myRefine->isChecked();
1244 }
1245
1246 //=======================================================================
1247 // name    : onTextChanged
1248 // Purpose :
1249 //=======================================================================
1250 void SMESHGUI_MeshPatternDlg::onTextChanged (const QString& theNewText)
1251 {
1252   if (myBusy || !isRefine())
1253     return;
1254
1255   myBusy = true;
1256
1257   if (mySelInput != Ids) {
1258     mySelInput = Ids;
1259     activateSelection();
1260   }
1261
1262   // hilight entered elements/nodes
1263   SMDS_Mesh* aMesh = 0;
1264   SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
1265   if (anActor)
1266     aMesh = anActor->GetObject()->GetMesh();
1267
1268   if (aMesh) {
1269     QStringList aListId = QStringList::split(" ", theNewText, false);
1270
1271     TColStd_MapOfInteger newIndices;
1272
1273     for (int i = 0; i < aListId.count(); i++) {
1274       const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt());
1275       if (e && e->GetType() == (myType == Type_2d ? SMDSAbs_Face : SMDSAbs_Volume))
1276         newIndices.Add(e->GetID());
1277     }
1278     mySelector->AddOrRemoveIndex( anActor->getIO(), newIndices, false);
1279     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1280       aViewWindow->highlight( anActor->getIO(), true, true );
1281   }
1282
1283   myBusy = false;
1284
1285   displayPreview();
1286 }
1287
1288 //=======================================================================
1289 // name    : onNodeChanged
1290 // Purpose :
1291 //=======================================================================
1292 void SMESHGUI_MeshPatternDlg::onNodeChanged (int value)
1293 {
1294   if (myType == Type_3d) {
1295     QSpinBox* first = (QSpinBox*)sender();
1296     QSpinBox* second = first == myNode1 ? myNode2 : myNode1;
1297     int secondVal = second->value();
1298     if (secondVal == value) {
1299       secondVal = value == second->maxValue() ? second->minValue() : value + 1;
1300       bool blocked = second->signalsBlocked();
1301       second->blockSignals(true);
1302       second->setValue(secondVal);
1303       second->blockSignals(blocked);
1304     }
1305   }
1306
1307   displayPreview();
1308 }
1309
1310 //=======================================================================
1311 // name    : getIds
1312 // Purpose :
1313 //=======================================================================
1314 bool SMESHGUI_MeshPatternDlg::getIds (QValueList<int>& ids) const
1315 {
1316   ids.clear();
1317   QStringList strIds = QStringList::split(" ", mySelEdit[Ids]->text());
1318   bool isOk;
1319   int val;
1320   for (QStringList::iterator it = strIds.begin(); it != strIds.end(); ++it) {
1321     val = (*it).toInt(&isOk);
1322     if (isOk)
1323       ids.append(val);
1324   }
1325
1326   return ids.count();
1327 }
1328
1329 //=======================================================================
1330 // name    : getNode1
1331 // Purpose :
1332 //=======================================================================
1333 int SMESHGUI_MeshPatternDlg::getNode (bool second) const
1334 {
1335   return second ? myNode2->value() - 1 : myNode1->value() - 1;
1336 }