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