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