Salome HOME
PAL13409: EDF282 SMESH: Tetrahedron is added if we specify Mefisto.
[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.htm";
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     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
502                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
503                            arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(myHelpFileName),
504                            QObject::tr("BUT_OK"));
505   }
506 }
507
508 //=======================================================================
509 // name    : SMESHGUI_MeshPatternDlg::onSelectionDone
510 // Purpose : SLOT called when selection changed
511 //=======================================================================
512 void SMESHGUI_MeshPatternDlg::onSelectionDone()
513 {
514   if (myBusy)
515     return;
516
517   try {
518     if (mySelInput == Mesh) {
519       SALOME_ListIO aList;
520       mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
521       if (aList.Extent() != 1)
522         return;
523
524       // Retrieve mesh from selection
525       Handle(SALOME_InteractiveObject) anIO = aList.First();
526       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
527       if (aMesh->_is_nil())
528         return;
529
530       // Get geom object corresponding to the mesh
531       _PTR(SObject) aSO = SMESH::FindSObject(aMesh.in());
532       myMeshShape = SMESH::GetGeom(aSO);
533
534       // Clear fields of geom objects if mesh was changed
535       if (myMesh != aMesh) {
536         for (int i = Object; i <= Ids; i++) {
537           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();
538           mySelEdit[ i ]->setText("");
539         }
540       }
541
542       myMesh = aMesh;
543
544       // Set name of mesh in line edit
545       QString aName;
546       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
547       mySelEdit[ Mesh ]->setText(aName);
548
549     } else if (mySelInput == Ids) {
550       SALOME_ListIO aList;
551       mySelectionMgr->selectedObjects(aList,SVTK_Viewer::Type());
552       if (aList.Extent() != 1)
553         return;
554
555       QString anIds;
556       if (!SMESH::GetNameOfSelectedElements(mySelector, aList.First(), anIds))
557         anIds = "";
558
559       myBusy = true;
560       mySelEdit[ Ids ]->setText(anIds);
561       myBusy = false;
562
563     } else {
564       SALOME_ListIO aList;
565       mySelectionMgr->selectedObjects(aList, SVTK_Viewer::Type());
566       if (aList.Extent() != 1)
567         return;
568
569       // Get geom object from selection
570       Handle(SALOME_InteractiveObject) anIO = aList.First();
571       GEOM::GEOM_Object_var anObj = SMESH::IObjectToInterface<GEOM::GEOM_Object>(anIO);
572       if (anObj->_is_nil())
573         return;
574
575       // Clear fields of vertexes if face or 3d block was changed
576       if (anObj != myGeomObj[ mySelInput ] && mySelInput == Object) {
577         for (int i = Vertex1; i <= Vertex2; i++) {
578           myGeomObj[ i ] = GEOM::GEOM_Object::_nil();
579           mySelEdit[ i ]->setText("");
580         }
581       }
582
583       myGeomObj[ mySelInput ] = anObj;
584
585       // Set name of geom object in line edit
586       QString aName;
587       SMESH::GetNameOfSelectedIObjects(mySelectionMgr, aName);
588       mySelEdit[ mySelInput ]->setText(aName);
589     }
590   } catch (const SALOME::SALOME_Exception& S_ex) {
591     SalomeApp_Tools::QtCatchCorbaException(S_ex);
592     resetSelInput();
593   } catch (...) {
594     resetSelInput();
595   }
596
597   updateWgState();
598   displayPreview();
599 }
600
601 //=======================================================================
602 // name    : SMESHGUI_MeshPatternDlg::resetSelInput
603 // Purpose : Reset fields corresponding to the current selection input
604 //=======================================================================
605 void SMESHGUI_MeshPatternDlg::resetSelInput()
606 {
607   if (mySelInput == Mesh)
608   {
609     myMesh = SMESH::SMESH_Mesh::_nil();
610     myMeshShape = GEOM::GEOM_Object::_nil();
611   }
612
613   else
614     myGeomObj[ mySelInput ] = GEOM::GEOM_Object::_nil();
615
616   mySelEdit[ mySelInput ]->setText("");
617 }
618
619 //=======================================================================
620 // name    : SMESHGUI_MeshPatternDlg::onDeactivate
621 // Purpose : SLOT called when dialog must be deativated
622 //=======================================================================
623 void SMESHGUI_MeshPatternDlg::onDeactivate()
624 {
625   mySelectionMgr->clearFilters();
626   //if (myReverseChk->isChecked())
627   //  erasePreview();
628   disconnect(mySelectionMgr, 0, this, 0);
629   setEnabled(false);
630 }
631
632 //=======================================================================
633 // name    : SMESHGUI_MeshPatternDlg::enterEvent
634 // Purpose : Event filter
635 //=======================================================================
636 void SMESHGUI_MeshPatternDlg::enterEvent (QEvent*)
637 {
638   if (myIsCreateDlgOpen)
639     return;
640
641   if (myReverseChk->isChecked())
642     displayPreview();
643   mySMESHGUI->EmitSignalDeactivateDialog();
644   setEnabled(true);
645   activateSelection();
646   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
647   onTextChanged(mySelEdit[Ids]->text());
648 }
649
650 //=======================================================================
651 // name    : SMESHGUI_MeshPatternDlg::closeEvent
652 // Purpose :
653 //=======================================================================
654 void SMESHGUI_MeshPatternDlg::closeEvent (QCloseEvent*)
655 {
656   onClose();
657 }
658
659 //=======================================================================
660 // name    : SMESHGUI_MeshPatternDlg::onSelInputChanged
661 // Purpose : SLOT. Called when -> button clicked.
662 //           Change current selection input field
663 //=======================================================================
664 void SMESHGUI_MeshPatternDlg::onSelInputChanged()
665 {
666   const QObject* aSender = sender();
667   for (int i = Mesh; i <= Ids; i++)
668     if (aSender == mySelBtn[ i ])
669       mySelInput = i;
670
671   activateSelection();
672   onSelectionDone();
673 }
674
675 //=======================================================================
676 // name    : SMESHGUI_MeshPatternDlg::prepareFilters
677 // Purpose : Prepare filters for dialog
678 //=======================================================================
679 QStringList SMESHGUI_MeshPatternDlg::prepareFilters() const
680 {
681   static QStringList aList;
682   if (aList.isEmpty())
683   {
684     aList.append(tr("PATTERN_FILT"));
685     //aList.append(tr("ALL_FILES_FILTER"));
686   }
687
688   return aList;
689 }
690
691 //=======================================================================
692 // name    : SMESHGUI_MeshPatternDlg::autoExtension
693 // Purpose : Append extension to the file name
694 //=======================================================================
695 QString SMESHGUI_MeshPatternDlg::autoExtension (const QString& theFileName) const
696 {
697   QString anExt = theFileName.section('.', -1);
698   return anExt != "smp" && anExt != "SMP" ? theFileName + ".smp" : theFileName;
699 }
700
701 //=======================================================================
702 // name    : SMESHGUI_MeshPatternDlg::onOpen
703 // Purpose : SLOT. Called when "Open" button clicked.
704 //           Displays file open dialog
705 //=======================================================================
706 void SMESHGUI_MeshPatternDlg::onOpen()
707 {
708   SUIT_FileDlg* aDlg = new SUIT_FileDlg (this, true);
709   aDlg->setCaption(tr("LOAD_PATTERN"));
710   aDlg->setMode(QFileDialog::ExistingFile);
711   aDlg->setFilters(prepareFilters());
712   if (myName->text() != "")
713     aDlg->setSelection(myName->text() + ".smp");
714   QPushButton* anOkBtn = (QPushButton*)aDlg->child("OK", "QPushButton");
715   if (anOkBtn != 0)
716     anOkBtn->setText(tr("SMESH_BUT_OK"));
717
718   if (aDlg->exec() != Accepted)
719     return;
720
721   QString fName = aDlg->selectedFile();
722   if (fName.isEmpty())
723     return;
724
725   if (QFileInfo(fName).extension().isEmpty())
726     fName = autoExtension(fName);
727
728   fName = QDir::convertSeparators(fName);
729
730   QString prev = QDir::convertSeparators(myName->text());
731   if (prev == fName)
732     return;
733
734   // Read string from file
735   QFile aFile(fName);
736   if (!aFile.open(IO_ReadOnly)) {
737     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
738                              tr("ERROR_OF_OPENING"), QMessageBox::Ok);
739     return;
740   }
741
742   QByteArray aDataArray = aFile.readAll();
743   const char* aData = aDataArray.data();
744   if (aData == 0) {
745     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
746                              tr("ERROR_OF_READING"), QMessageBox::Ok);
747     return;
748   }
749
750   if (loadFromFile(aData))
751     myName->setText(QFileInfo(fName).baseName());
752
753   updateWgState();
754   displayPreview();
755 }
756
757 //=======================================================================
758 // name    : SMESHGUI_MeshPatternDlg::onCloseCreationDlg
759 // Purpose : SLOT. Called when "Pattern creation" dialog closed with "Close"
760 //=======================================================================
761 void SMESHGUI_MeshPatternDlg::onCloseCreationDlg()
762 {
763   setEnabled(true);
764   myIsCreateDlgOpen = false;
765 }
766
767 //=======================================================================
768 // name    : SMESHGUI_MeshPatternDlg::onOkCreationDlg
769 // Purpose : SLOT. Called when "Pattern creation" dialog closed with OK
770 //           or SAVE buttons. Initialize myPattern field. Redisplay preview
771 //=======================================================================
772 void SMESHGUI_MeshPatternDlg::onOkCreationDlg()
773 {
774   myPattern = SMESH::SMESH_Pattern::_duplicate(myCreationDlg->GetPattern());
775   myName->setText(myCreationDlg->GetPatternName());
776
777   updateWgState();
778   displayPreview();
779
780   setEnabled(true);
781   myIsCreateDlgOpen = false;
782 }
783
784 //=======================================================================
785 // name    : SMESHGUI_MeshPatternDlg::onNew
786 // Purpose : SLOT. Called when "New..." button clicked. Create new pattern
787 //=======================================================================
788 void SMESHGUI_MeshPatternDlg::onNew()
789 {
790   setEnabled(false);
791   myIsCreateDlgOpen = true;
792   if (myCreationDlg == 0)
793   {
794     myCreationDlg = new SMESHGUI_CreatePatternDlg( mySMESHGUI, myType);
795     connect(myCreationDlg, SIGNAL(NewPattern()), SLOT(onOkCreationDlg()));
796     connect(myCreationDlg, SIGNAL(Close()), SLOT(onCloseCreationDlg()));
797   }
798   else
799     myCreationDlg->Init(myType);
800
801   myCreationDlg->SetMesh(myMesh);
802   myCreationDlg->show();
803 }
804
805 //=======================================================================
806 // name    : SMESHGUI_MeshPatternDlg::onReverse
807 // Purpose : SLOT. Called when state of "Reverse order..." checkbox chaged
808 //           Calculate new points of the mesh to be created. Redisplay preview
809 //=======================================================================
810 void SMESHGUI_MeshPatternDlg::onReverse (bool)
811 {
812   displayPreview();
813 }
814
815 //=======================================================================
816
817 // name    : SMESHGUI_MeshPatternDlg::onPreview
818 // Purpose : SLOT. Called when state of "Preview" checkbox changed
819 //           Display/Erase preview
820 //=======================================================================
821 void SMESHGUI_MeshPatternDlg::onPreview (bool)
822 {
823   displayPreview();
824 }
825
826 //=======================================================================
827 // name    : SMESHGUI_MeshPatternDlg::displayPreview
828 // Purpose : Display preview
829 //=======================================================================
830 void SMESHGUI_MeshPatternDlg::displayPreview()
831 {
832   try {
833     // Redisplay preview in dialog
834     SMESH::point_array_var pnts = myPattern->GetPoints();
835     SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
836     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints(false);
837
838     if (pnts->length()       == 0 ||
839         keyPoints->length()  == 0 ||
840         elemPoints->length() == 0) {
841       erasePreview();
842       return;
843     } else {
844       PointVector aPoints(pnts->length());
845       QValueVector<int> aKeyPoints(keyPoints->length());
846       ConnectivityVector anElemPoints(elemPoints->length());
847
848       for (int i = 0, n = pnts->length(); i < n; i++)
849         aPoints[ i ] = pnts[ i ];
850
851       for (int i2 = 0, n2 = keyPoints->length(); i2 < n2; i2++)
852         aKeyPoints[ i2 ] = keyPoints[ i2 ];
853
854       for (int i3 = 0, n3 = elemPoints->length(); i3 < n3; i3++) {
855         QValueVector<int> aVec(elemPoints[ i3 ].length());
856         for (int i4 = 0, n4 = elemPoints[ i3 ].length(); i4 < n4; i4++)
857           aVec[ i4 ] = elemPoints[ i3 ][ i4 ];
858
859         anElemPoints[ i3 ] = aVec;
860       }
861
862       myPicture2d->SetPoints(aPoints, aKeyPoints, anElemPoints);
863     }
864
865     // Redisplay preview in 3D viewer
866     if (myPreviewActor != 0) {
867       if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView()) {
868         vf->RemoveActor(myPreviewActor);
869         vf->Repaint();
870       }
871       myPreviewActor->Delete();
872       myPreviewActor = 0;
873     }
874
875     if (!myPreviewChk->isChecked() || !isValid(false))
876       return;
877
878     vtkUnstructuredGrid* aGrid = getGrid();
879     if (aGrid == 0)
880       return;
881
882     // Create and display actor
883     vtkDataSetMapper* aMapper = vtkDataSetMapper::New();
884     aMapper->SetInput(aGrid);
885
886     myPreviewActor = SALOME_Actor::New();
887     myPreviewActor->PickableOff();
888     myPreviewActor->SetMapper(aMapper);
889
890     vtkProperty* aProp = vtkProperty::New();
891     aProp->SetRepresentationToWireframe();
892     aProp->SetColor(250, 0, 250);
893     if (SMESH::FindActorByObject(myMesh))
894       aProp->SetLineWidth( SMESH::GetFloat( "SMESH:element_width", 1 ) + 1 );
895     else
896       aProp->SetLineWidth(1);
897     myPreviewActor->SetProperty(aProp);
898
899     myPreviewActor->SetRepresentation(3);
900
901     SMESH::GetCurrentVtkView()->AddActor(myPreviewActor);
902     SMESH::GetCurrentVtkView()->Repaint();
903
904     aProp->Delete();
905     aGrid->Delete();
906   } catch (const SALOME::SALOME_Exception& S_ex) {
907     SalomeApp_Tools::QtCatchCorbaException(S_ex);
908     erasePreview();
909   } catch (...) {
910     erasePreview();
911   }
912 }
913
914 //=======================================================================
915 // name    : SMESHGUI_MeshPatternDlg::erasePreview
916 // Purpose : Erase preview
917 //=======================================================================
918 void SMESHGUI_MeshPatternDlg::erasePreview()
919 {
920   // Erase preview in 2D viewer
921   myPicture2d->SetPoints(PointVector(), QValueVector<int>(), ConnectivityVector());
922
923   // Erase preview in 3D viewer
924   if (myPreviewActor == 0)
925     return;
926
927
928   if (SVTK_ViewWindow* vf = SMESH::GetCurrentVtkView())
929   {
930     vf->RemoveActor(myPreviewActor);
931     vf->Repaint();
932   }
933   myPreviewActor->Delete();
934   myPreviewActor = 0;
935 }
936
937 //=======================================================================
938 // name    : SMESHGUI_MeshPatternDlg::updateWgState
939 // Purpose : Enable/disable selection widgets
940 //=======================================================================
941 void SMESHGUI_MeshPatternDlg::updateWgState()
942 {
943   if (myMesh->_is_nil()) {
944     for (int i = Object; i <= Ids; i++) {
945       mySelBtn [ i ]->setEnabled(false);
946       mySelEdit[ i ]->setEnabled(false);
947       mySelEdit[ i ]->setText("");
948     }
949     myNode1->setEnabled(false);
950     myNode2->setEnabled(false);
951     myNode1->setRange(0, 0);
952     myNode2->setRange(0, 0);
953   } else {
954     mySelBtn [ Object ]->setEnabled(true);
955     mySelEdit[ Object ]->setEnabled(true);
956     mySelBtn [ Ids ]   ->setEnabled(true);
957     mySelEdit[ Ids ]   ->setEnabled(true);
958
959     if (myGeomObj[ Object ]->_is_nil()) {
960       for (int i = Vertex1; i <= Vertex2; i++) {
961         mySelBtn [ i ]->setEnabled(false);
962         mySelEdit[ i ]->setEnabled(false);
963         mySelEdit[ i ]->setText("");
964       }
965     } else {
966       for (int i = Object; i <= Vertex2; i++) {
967         mySelBtn [ i ]->setEnabled(true);
968         mySelEdit[ i ]->setEnabled(true);
969       }
970     }
971
972     QValueList<int> ids;
973     if (!CORBA::is_nil(myPattern)/* && getIds(ids)*/) {
974       SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
975       if (keyPoints->length()) {
976         myNode1->setEnabled(true);
977         myNode2->setEnabled(true);
978         myNode1->setRange(1, keyPoints->length());
979         myNode2->setRange(1, keyPoints->length());
980         return;
981       }
982     }
983
984     myNode1->setEnabled(false);
985     myNode2->setEnabled(false);
986     myNode1->setRange(0, 0);
987     myNode2->setRange(0, 0);
988   }
989 }
990
991 //=======================================================================
992 // name    : SMESHGUI_MeshPatternDlg::activateSelection
993 // Purpose : Activate selection in accordance with current selection input
994 //=======================================================================
995 void SMESHGUI_MeshPatternDlg::activateSelection()
996 {
997   mySelectionMgr->clearFilters();
998   if (mySelInput == Ids) {
999     SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
1000     if (anActor)
1001       SMESH::SetPickable(anActor);
1002
1003     if (myType == Type_2d)
1004       {
1005         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1006           aViewWindow->SetSelectionMode(FaceSelection);
1007       }
1008     else
1009       {
1010         if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1011           aViewWindow->SetSelectionMode(CellSelection);
1012       }
1013   }
1014   else {
1015     SMESH::SetPickable();
1016     //mySelectionMgr->setSelectionModes(ActorSelection);
1017     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1018       aViewWindow->SetSelectionMode(ActorSelection);
1019   }
1020
1021   if (mySelInput == Object && !myMeshShape->_is_nil()) {
1022     if (myType == Type_2d) {
1023       if (myNbPoints > 0)
1024         mySelectionMgr->installFilter
1025           (new SMESH_NumberFilter ("GEOM", TopAbs_VERTEX, myNbPoints, TopAbs_FACE, myMeshShape));
1026       else
1027         mySelectionMgr->installFilter
1028           (new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, myNbPoints, TopAbs_FACE, myMeshShape));
1029     } else {
1030       TColStd_MapOfInteger aTypes;
1031       aTypes.Add(TopAbs_SHELL);
1032       aTypes.Add(TopAbs_SOLID);
1033       mySelectionMgr->installFilter
1034         (new SMESH_NumberFilter ("GEOM", TopAbs_FACE, 6, aTypes, myMeshShape, true));
1035     }
1036   } else if ((mySelInput == Vertex1 || mySelInput == Vertex2) && !myGeomObj[ Object ]->_is_nil()) {
1037     mySelectionMgr->installFilter
1038       (new SMESH_NumberFilter ("GEOM", TopAbs_SHAPE, 1, TopAbs_VERTEX, myGeomObj[ Object ]));
1039   } else {
1040   }
1041 }
1042
1043 //=======================================================================
1044 // name    : SMESHGUI_MeshPatternDlg::loadFromFile
1045 // Purpose : Load pattern from file
1046 //=======================================================================
1047 bool SMESHGUI_MeshPatternDlg::loadFromFile (const QString& theName)
1048 {
1049   try {
1050     SMESH::SMESH_Pattern_var aPattern = SMESH::GetPattern();
1051
1052     if (!aPattern->LoadFromFile(theName.latin1()) ||
1053         myType == Type_2d && !aPattern->Is2D()) {
1054       SMESH::SMESH_Pattern::ErrorCode aCode = aPattern->GetErrorCode();
1055       QString aMess;
1056       if      (aCode == SMESH::SMESH_Pattern::ERR_READ_NB_POINTS     ) aMess = tr("ERR_READ_NB_POINTS");
1057       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_POINT_COORDS  ) aMess = tr("ERR_READ_POINT_COORDS");
1058       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_TOO_FEW_POINTS) aMess = tr("ERR_READ_TOO_FEW_POINTS");
1059       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_3D_COORD      ) aMess = tr("ERR_READ_3D_COORD");
1060       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_NO_KEYPOINT   ) aMess = tr("ERR_READ_NO_KEYPOINT");
1061       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_INDEX     ) aMess = tr("ERR_READ_BAD_INDEX");
1062       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_ELEM_POINTS   ) aMess = tr("ERR_READ_ELEM_POINTS");
1063       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_NO_ELEMS      ) aMess = tr("ERR_READ_NO_ELEMS");
1064       else if (aCode == SMESH::SMESH_Pattern::ERR_READ_BAD_KEY_POINT ) aMess = tr("ERR_READ_BAD_KEY_POINT");
1065       else                                                             aMess = tr("ERROR_OF_LOADING");
1066
1067       QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"), aMess, QMessageBox::Ok);
1068       return false;
1069     } else {
1070       myPattern = aPattern;
1071       return true;
1072     }
1073   } catch (const SALOME::SALOME_Exception& S_ex) {
1074     SalomeApp_Tools::QtCatchCorbaException(S_ex);
1075     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_ERROR"),
1076                              tr("ERROR_OF_LOADING"), QMessageBox::Ok);
1077       return false;
1078   }
1079 }
1080
1081 //=======================================================================
1082 // name    : SMESHGUI_MeshPatternDlg::onTypeChanged
1083 // Purpose : SLOT. Called when pattern type changed.
1084 //           Change dialog's look and feel
1085 //=======================================================================
1086 void SMESHGUI_MeshPatternDlg::onTypeChanged (int theType)
1087 {
1088   if (myType == theType)
1089     return;
1090
1091   myType = theType;
1092
1093   myNbPoints = -1;
1094   myGeomObj[ Object  ] = GEOM::GEOM_Object::_nil();
1095   myGeomObj[ Vertex1 ] = GEOM::GEOM_Object::_nil();
1096   myGeomObj[ Vertex2 ] = GEOM::GEOM_Object::_nil();
1097   myPattern = SMESH::GetPattern();
1098
1099   myName->setText("");
1100   mySelEdit[ Object  ]->setText("");
1101   mySelEdit[ Vertex1 ]->setText("");
1102   mySelEdit[ Vertex2 ]->setText("");
1103   mySelEdit[ Ids ]    ->setText("");
1104   myCreatePolygonsChk->show();
1105   myCreatePolyedrsChk->show();
1106
1107   if (theType == Type_2d) {
1108     // Geom widgets
1109     mySelLbl [ Vertex2 ]->hide();
1110     mySelBtn [ Vertex2 ]->hide();
1111     mySelEdit[ Vertex2 ]->hide();
1112     myReverseChk->show();
1113     myPicture2d->show();
1114     myPicture3d->hide();
1115     mySelLbl[ Object  ]->setText(tr("FACE"));
1116     mySelLbl[ Vertex1 ]->setText(tr("VERTEX"));
1117     // Refine widgets
1118     mySelLbl[ Ids ]->setText(tr("MESH_FACES"));
1119     myNode2Lbl->hide();
1120     myNode2   ->hide();
1121   } else {
1122     // Geom widgets
1123     mySelLbl [ Vertex2 ]->show();
1124     mySelBtn [ Vertex2 ]->show();
1125     mySelEdit[ Vertex2 ]->show();
1126     myReverseChk->hide();
1127     myPicture2d->hide();
1128     myPicture3d->show();
1129     mySelLbl[ Object  ]->setText(tr("3D_BLOCK"));
1130     mySelLbl[ Vertex1 ]->setText(tr("VERTEX1"));
1131     mySelLbl[ Vertex2 ]->setText(tr("VERTEX2"));
1132     // Refine widgets
1133     mySelLbl[ Ids ]->setText(tr("MESH_VOLUMES"));
1134     myNode2Lbl->show();
1135     myNode2   ->show();
1136   }
1137
1138   mySelInput = Mesh;
1139   activateSelection();
1140   updateWgState();
1141   displayPreview();
1142 }
1143
1144 //=======================================================================
1145 // name    : SMESHGUI_MeshPatternDlg::getGrid
1146 // Purpose : Get unstructured grid for pattern
1147 //=======================================================================
1148 vtkUnstructuredGrid* SMESHGUI_MeshPatternDlg::getGrid()
1149 {
1150   try {
1151     // Get points from pattern
1152     SMESH::point_array_var pnts;
1153     QValueList<int> ids;
1154     if (isRefine() && getIds(ids)) {
1155       SMESH::long_array_var varIds = new SMESH::long_array();
1156       varIds->length(ids.count());
1157       int i = 0;
1158       for (QValueList<int>::iterator it = ids.begin(); it != ids.end(); ++it)
1159         varIds[i++] = *it;
1160       pnts = myType == Type_2d
1161         ? myPattern->ApplyToMeshFaces  (myMesh, varIds, getNode(false), myReverseChk->isChecked())
1162         : myPattern->ApplyToHexahedrons(myMesh, varIds, getNode(false), getNode(true));
1163     } else {
1164       pnts = myType == Type_2d
1165         ? myPattern->ApplyToFace   (myGeomObj[ Object ], myGeomObj[ Vertex1 ], myReverseChk->isChecked())
1166       : myPattern->ApplyTo3DBlock(myGeomObj[ Object ], myGeomObj[ Vertex1 ], myGeomObj[ Vertex2 ]);
1167     }
1168
1169     SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints(true);
1170
1171     if (pnts->length() == 0 || elemPoints->length() == 0)
1172       return 0;
1173
1174     // to do : to be removed /////////////////////////////////////////////
1175
1176 #ifdef DEB_SLN
1177     for (int i1 = 0, n1 = pnts->length(); i1 < n1; i1++)
1178       printf("%d: %g %g %g\n", i1, pnts[ i1 ].x, pnts[ i1 ].y, pnts[ i1 ].z);
1179
1180     printf("\nELEMENTS : \n");
1181     for (int i2 = 0, n2 = elemPoints->length(); i2 < n2; i2++)
1182     {
1183
1184       printf("%d: ", i2);
1185       for (int i3 = 0, n3 = elemPoints[ i2 ].length(); i3 < n3; i3++)
1186         printf("%d ", elemPoints[ i2 ][ i3 ]);
1187
1188       printf("\n");
1189
1190     }
1191 #endif
1192     //////////////////////////////////////////////////////////////////////
1193
1194     // Calculate number of points used for cell
1195     vtkIdType aNbCells = elemPoints->length();
1196     vtkIdType aCellsSize = 0;
1197     for (int i = 0, n = elemPoints->length(); i < n; i++)
1198       aCellsSize += elemPoints[ i ].length();
1199
1200     // Create unstructured grid and other  usefull arrays
1201     vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::New();
1202
1203     vtkCellArray* aConnectivity = vtkCellArray::New();
1204     aConnectivity->Allocate(aCellsSize, 0);
1205
1206     vtkPoints* aPoints = vtkPoints::New();
1207     aPoints->SetNumberOfPoints(pnts->length());
1208
1209     vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
1210     aCellTypesArray->SetNumberOfComponents(1);
1211     aCellTypesArray->Allocate(aNbCells * aCellTypesArray->GetNumberOfComponents());
1212
1213     vtkIdList *anIdList = vtkIdList::New();
1214
1215     // Fill array of points
1216     for (int p = 0, nbPnt = pnts->length(); p < nbPnt; p++)
1217       aPoints->SetPoint(p, pnts[ p ].x, pnts[ p ].y, pnts[ p ].z);
1218
1219     for (int e = 0, nbElem = elemPoints->length(); e < nbElem; e++) {
1220       int nbPoints = elemPoints[ e ].length();
1221       anIdList->SetNumberOfIds(nbPoints);
1222       for (int i = 0; i < nbPoints; i++)
1223         anIdList->SetId(i, elemPoints[ e ][ i ]);
1224
1225       aConnectivity->InsertNextCell(anIdList);
1226
1227       if      (nbPoints == 3) aCellTypesArray->InsertNextValue(VTK_TRIANGLE);
1228       else if (nbPoints == 5) aCellTypesArray->InsertNextValue(VTK_PYRAMID);
1229       else if (nbPoints == 6) aCellTypesArray->InsertNextValue(VTK_WEDGE);
1230       else if (nbPoints == 8) aCellTypesArray->InsertNextValue(VTK_HEXAHEDRON);
1231       else if (nbPoints == 4 && myType == Type_2d) aCellTypesArray->InsertNextValue(VTK_QUAD);
1232       else if (nbPoints == 4 && myType == Type_3d) aCellTypesArray->InsertNextValue(VTK_TETRA);
1233       else aCellTypesArray->InsertNextValue(VTK_EMPTY_CELL);
1234     }
1235
1236     VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
1237     aCellLocationsArray->SetNumberOfComponents(1);
1238     aCellLocationsArray->SetNumberOfTuples(aNbCells);
1239
1240     aConnectivity->InitTraversal();
1241     for (vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell(npts, pts); idType++)
1242       aCellLocationsArray->SetValue(idType, aConnectivity->GetTraversalLocation(npts));
1243
1244     aGrid->SetPoints(aPoints);
1245     aGrid->SetCells(aCellTypesArray, aCellLocationsArray,aConnectivity);
1246
1247     aConnectivity->Delete();
1248     aPoints->Delete();
1249     aCellTypesArray->Delete();
1250     anIdList->Delete();
1251     aCellLocationsArray->Delete();
1252
1253     return aGrid;
1254   } catch (...) {
1255     return 0;
1256   }
1257 }
1258
1259 //=======================================================================
1260 // name    : onModeToggled
1261 // Purpose :
1262 //=======================================================================
1263 void SMESHGUI_MeshPatternDlg::onModeToggled (bool on)
1264 {
1265   on ? myRefineGrp->show() : myRefineGrp->hide();
1266   on ? myGeomGrp->hide()   : myGeomGrp->show();
1267
1268   displayPreview();
1269 }
1270
1271 //=======================================================================
1272 // name    : isRefine
1273 // Purpose :
1274 //=======================================================================
1275 bool SMESHGUI_MeshPatternDlg::isRefine() const
1276 {
1277   return myRefine->isChecked();
1278 }
1279
1280 //=======================================================================
1281 // name    : onTextChanged
1282 // Purpose :
1283 //=======================================================================
1284 void SMESHGUI_MeshPatternDlg::onTextChanged (const QString& theNewText)
1285 {
1286   if (myBusy || !isRefine())
1287     return;
1288
1289   myBusy = true;
1290
1291   if (mySelInput != Ids) {
1292     mySelInput = Ids;
1293     activateSelection();
1294   }
1295
1296   // hilight entered elements/nodes
1297   SMDS_Mesh* aMesh = 0;
1298   SMESH_Actor* anActor = SMESH::FindActorByObject(myMesh);
1299   if (anActor)
1300     aMesh = anActor->GetObject()->GetMesh();
1301
1302   if (aMesh) {
1303     QStringList aListId = QStringList::split(" ", theNewText, false);
1304
1305     TColStd_MapOfInteger newIndices;
1306
1307     for (int i = 0; i < aListId.count(); i++) {
1308       const SMDS_MeshElement * e = aMesh->FindElement(aListId[ i ].toInt());
1309       if (e && e->GetType() == (myType == Type_2d ? SMDSAbs_Face : SMDSAbs_Volume))
1310         newIndices.Add(e->GetID());
1311     }
1312     mySelector->AddOrRemoveIndex( anActor->getIO(), newIndices, false);
1313     if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ))
1314       aViewWindow->highlight( anActor->getIO(), true, true );
1315   }
1316
1317   myBusy = false;
1318
1319   displayPreview();
1320 }
1321
1322 //=======================================================================
1323 // name    : onNodeChanged
1324 // Purpose :
1325 //=======================================================================
1326 void SMESHGUI_MeshPatternDlg::onNodeChanged (int value)
1327 {
1328   if (myType == Type_3d) {
1329     QSpinBox* first = (QSpinBox*)sender();
1330     QSpinBox* second = first == myNode1 ? myNode2 : myNode1;
1331     int secondVal = second->value();
1332     if (secondVal == value) {
1333       secondVal = value == second->maxValue() ? second->minValue() : value + 1;
1334       bool blocked = second->signalsBlocked();
1335       second->blockSignals(true);
1336       second->setValue(secondVal);
1337       second->blockSignals(blocked);
1338     }
1339   }
1340
1341   displayPreview();
1342 }
1343
1344 //=======================================================================
1345 // name    : getIds
1346 // Purpose :
1347 //=======================================================================
1348 bool SMESHGUI_MeshPatternDlg::getIds (QValueList<int>& ids) const
1349 {
1350   ids.clear();
1351   QStringList strIds = QStringList::split(" ", mySelEdit[Ids]->text());
1352   bool isOk;
1353   int val;
1354   for (QStringList::iterator it = strIds.begin(); it != strIds.end(); ++it) {
1355     val = (*it).toInt(&isOk);
1356     if (isOk)
1357       ids.append(val);
1358   }
1359
1360   return ids.count();
1361 }
1362
1363 //=======================================================================
1364 // name    : getNode1
1365 // Purpose :
1366 //=======================================================================
1367 int SMESHGUI_MeshPatternDlg::getNode (bool second) const
1368 {
1369   return second ? myNode2->value() - 1 : myNode1->value() - 1;
1370 }
1371
1372 //=================================================================================
1373 // function : keyPressEvent()
1374 // purpose  :
1375 //=================================================================================
1376 void SMESHGUI_MeshPatternDlg::keyPressEvent( QKeyEvent* e )
1377 {
1378   QDialog::keyPressEvent( e );
1379   if ( e->isAccepted() )
1380     return;
1381
1382   if ( e->key() == Key_F1 )
1383     {
1384       e->accept();
1385       onHelp();
1386     }
1387 }