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