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