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