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