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