Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_CreatePatternDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : SMESHGUI_CreatePatternDlg.cxx
23 // Author : Sergey LITONIN, Open CASCADE S.A.S.
24 // SMESH includes
25 //
26 #include "SMESHGUI_CreatePatternDlg.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_PatternWidget.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_VTKUtils.h"
32 #include "SMESHGUI_PatternUtils.h"
33 #include "SMESHGUI_GEOMGenUtils.h"
34
35 #include <SMESH_NumberFilter.hxx>
36
37 // SALOME GUI includes
38 #include <SUIT_ResourceMgr.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_FileDlg.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_MessageBox.h>
43
44 #include <LightApp_Application.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <SalomeApp_Tools.h>
47
48 #include <SALOME_ListIO.hxx>
49 #include <SVTK_ViewModel.h>
50 #include <SVTK_ViewWindow.h>
51
52 // SALOME KERNEL includes
53 #include <SALOMEDS_SObject.hxx>
54
55 // OCCT includes
56 #include <TColStd_MapOfInteger.hxx>
57
58 // Qt includes
59 #include <QFrame>
60 #include <QVBoxLayout>
61 #include <QHBoxLayout>
62 #include <QLineEdit>
63 #include <QPushButton>
64 #include <QGroupBox>
65 #include <QLabel>
66 #include <QRadioButton>
67 #include <QCheckBox>
68 #include <QButtonGroup>
69 #include <QApplication>
70 #include <QKeyEvent>
71 #include <QFile>
72 #include <QDir>
73
74 #define SPACING 6
75 #define MARGIN  11
76
77 /*!
78  *  Class       : SMESHGUI_CreatePatternDlg
79  *  Description : Dialog to specify filters for VTK viewer
80  */
81
82 //=======================================================================
83 // function : SMESHGUI_CreatePatternDlg()
84 // purpose  : Constructor
85 //=======================================================================
86 SMESHGUI_CreatePatternDlg::SMESHGUI_CreatePatternDlg( SMESHGUI*   theModule,
87                                                       const int   theType )
88   : QDialog( SMESH::GetDesktop( theModule ) ),
89     mySMESHGUI( theModule ),
90     mySelectionMgr( SMESH::GetSelectionMgr( theModule ) )
91 {
92   setModal( false );
93   setWindowTitle( tr( "CAPTION" ) );
94
95   QVBoxLayout* aDlgLay = new QVBoxLayout( this );
96   aDlgLay->setMargin( MARGIN );
97   aDlgLay->setSpacing( SPACING );
98
99   QWidget* aMainFrame = createMainFrame( this );
100   QWidget* aBtnFrame  = createButtonFrame( this );
101
102   aDlgLay->addWidget( aMainFrame );
103   aDlgLay->addWidget( aBtnFrame );
104
105   aDlgLay->setStretchFactor( aMainFrame, 1 );
106
107   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
108     mySelector = aViewWindow->GetSelector();
109
110   myHelpFileName = "pattern_mapping_page.html";
111
112   Init( theType );
113 }
114
115 //=======================================================================
116 // function : createMainFrame()
117 // purpose  : Create frame containing dialog's input fields
118 //=======================================================================
119 QWidget* SMESHGUI_CreatePatternDlg::createMainFrame( QWidget* theParent )
120 {
121   QPixmap iconSlct    ( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_SELECT" ) ) );
122   QPixmap icon2d      ( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_PATTERN_2d" ) ) );
123   QPixmap icon3d      ( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_PATTERN_3d" ) ) );
124   QPixmap iconSample2d( SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap( "SMESH", tr( "ICON_PATTERN_SAMPLE_2D" ) ) );
125
126   QWidget* aMainGrp = new QWidget( theParent );
127   QVBoxLayout* aMainGrpLayout = new QVBoxLayout( aMainGrp );
128   aMainGrpLayout->setMargin( 0 );
129   aMainGrpLayout->setSpacing( SPACING );
130
131   // Pattern type group
132
133   QGroupBox* aTypeGrp = new QGroupBox( tr( "PATTERN_TYPE" ), aMainGrp );
134   QHBoxLayout* aTypeGrpLayout = new QHBoxLayout( aTypeGrp );
135   aTypeGrpLayout->setMargin( MARGIN );
136   aTypeGrpLayout->setSpacing( SPACING );
137
138   mySwitch2d = new QRadioButton( aTypeGrp );
139   mySwitch3d = new QRadioButton( aTypeGrp );
140   mySwitch2d->setIcon( icon2d );
141   mySwitch3d->setIcon( icon3d );
142
143   myTypeGrp = new QButtonGroup( aMainGrp );
144   myTypeGrp->addButton( mySwitch2d, Type_2d );
145   myTypeGrp->addButton( mySwitch3d, Type_3d );
146
147   // ... layout widgets
148
149   aTypeGrpLayout->addWidget( mySwitch2d );
150   aTypeGrpLayout->addWidget( mySwitch3d );
151
152   // Mesh and pattern name group
153
154   QGroupBox* aPatternGrp = new QGroupBox( tr( "PATTERN" ), aMainGrp );
155   QGridLayout* aPatternGrpLayout = new QGridLayout( aPatternGrp );
156   aPatternGrpLayout->setMargin( MARGIN );
157   aPatternGrpLayout->setSpacing( SPACING );
158
159   QLabel* aMeshLab = new QLabel( tr( "MESH_OR_SUBMESH" ), aPatternGrp );
160
161   QPushButton* aSelBtn = new QPushButton( aPatternGrp );
162   aSelBtn->setIcon( iconSlct );
163   myMeshEdit = new QLineEdit( aPatternGrp );
164   myMeshEdit->setReadOnly( true );
165
166   QLabel* aNameLab = new QLabel( tr( "PATTERN_NAME" ), aPatternGrp );
167   myName = new QLineEdit( aPatternGrp );
168
169   // Picture 2d
170
171   myPicture2d = new SMESHGUI_PatternWidget( aPatternGrp ),
172   myPicture2d->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
173
174   // Project check box
175
176   myProjectChk = new QCheckBox( tr( "PROJECT" ), aPatternGrp );
177
178   // ... layout widgets
179
180   aPatternGrpLayout->addWidget( aMeshLab,     0, 0 );
181   aPatternGrpLayout->addWidget( aSelBtn,      0, 1 );
182   aPatternGrpLayout->addWidget( myMeshEdit,   0, 2 );
183   aPatternGrpLayout->addWidget( aNameLab,     1, 0 );
184   aPatternGrpLayout->addWidget( myName,       1, 2 );
185   aPatternGrpLayout->addWidget( myPicture2d,  2, 0, 1, 3 );
186   aPatternGrpLayout->addWidget( myProjectChk, 3, 0, 1, 3 );
187
188   // main layout
189
190   aMainGrpLayout->addWidget( aTypeGrp );
191   aMainGrpLayout->addWidget( aPatternGrp );
192
193   // Connect signals and slots
194
195   connect( myTypeGrp,    SIGNAL( buttonClicked( int )  ),
196            this,         SLOT( onTypeChanged( int ) ) );
197   connect( myProjectChk, SIGNAL( toggled( bool ) ),
198            this,         SLOT( onProject( bool ) ) );
199   connect( aSelBtn,      SIGNAL( clicked() ),
200            this,         SLOT( onSelBtnClicked() ) );
201
202   return aMainGrp;
203 }
204
205 //=======================================================================
206 // function : createButtonFrame()
207 // purpose  : Create frame containing buttons
208 //=======================================================================
209 QWidget* SMESHGUI_CreatePatternDlg::createButtonFrame( QWidget* theParent )
210 {
211   QFrame* aFrame = new QFrame( theParent );
212   aFrame->setFrameStyle( QFrame::Box | QFrame::Sunken );
213
214   QPushButton* myOkBtn    = new QPushButton( tr( "SMESH_BUT_OK"    ), aFrame );
215   QPushButton* mySaveBtn  = new QPushButton( tr( "SAVE"            ), aFrame );
216   QPushButton* myCloseBtn = new QPushButton( tr( "SMESH_BUT_CANCEL"), aFrame );
217   QPushButton* myHelpBtn  = new QPushButton( tr( "SMESH_BUT_HELP"),   aFrame );
218
219   QHBoxLayout* aLay = new QHBoxLayout( aFrame );
220   aLay->setMargin( MARGIN );
221   aLay->setSpacing( SPACING );
222
223   aLay->addWidget( myOkBtn );
224   aLay->addSpacing( 10 );
225   aLay->addWidget( mySaveBtn );
226   aLay->addSpacing( 10 );
227   aLay->addStretch();
228   aLay->addWidget( myCloseBtn );
229   aLay->addWidget( myHelpBtn );
230
231   connect( myOkBtn,    SIGNAL( clicked() ), this, SLOT( onOk() ) );
232   connect( myCloseBtn, SIGNAL( clicked() ), this, SLOT( onClose() ) );
233   connect( mySaveBtn,  SIGNAL( clicked() ), this, SLOT( onSave() ) );
234   connect( myHelpBtn,  SIGNAL( clicked() ), this, SLOT( onHelp() ) );
235
236   return aFrame;
237 }
238
239 //=======================================================================
240 // function : ~SMESHGUI_CreatePatternDlg()
241 // purpose  : Destructor
242 //=======================================================================
243 SMESHGUI_CreatePatternDlg::~SMESHGUI_CreatePatternDlg()
244 {
245 }
246
247 //=======================================================================
248 // function : onProject()
249 // purpose  : SLOT. Called when state of "Project nodes on ther face"
250 //            checkbox is changed
251 //=======================================================================
252 void SMESHGUI_CreatePatternDlg::onProject( bool )
253 {
254   loadFromObject( false );
255   displayPreview();
256 }
257
258 //=======================================================================
259 // function : Init()
260 // purpose  : Init dialog fields, connect signals and slots, show dialog
261 //=======================================================================
262 void SMESHGUI_CreatePatternDlg::Init( const int theType )
263 {
264   myIsLoaded     = false;
265   myType         = -1;
266   mySubMesh      = SMESH::SMESH_subMesh::_nil();
267   myMesh         = SMESH::SMESH_Mesh::_nil();
268   myGeomObj      = GEOM::GEOM_Object::_nil();
269   myPattern      = SMESH::SMESH_Pattern::_nil();
270
271   erasePreview();
272
273   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
274
275   // selection and SMESHGUI
276   connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ),
277            this,           SLOT( onSelectionDone() ) );
278   connect( mySMESHGUI,     SIGNAL( SignalDeactivateActiveDialog() ),
279            this,           SLOT( onDeactivate() ) );
280   connect( mySMESHGUI,     SIGNAL( SignalCloseAllDialogs() ),
281            this,           SLOT( onClose() ) );
282
283   mySwitch2d->setEnabled( theType == Type_2d );
284   mySwitch3d->setEnabled( theType == Type_3d );
285
286   myTypeGrp->button( theType )->setChecked( true );
287   onTypeChanged( theType );
288
289   myName->setText( getDefaultName() );
290   myMeshEdit->setText( "" );
291
292   QApplication::instance()->processEvents();
293   updateGeometry();
294   resize( minimumSize() );
295
296   activateSelection();
297   onSelectionDone();
298
299   //show();
300 }
301
302 //=======================================================================
303 // function : SetMesh()
304 // purpose  : Set mesh to dialog
305 //=======================================================================
306 void SMESHGUI_CreatePatternDlg::SetMesh( SMESH::SMESH_Mesh_ptr thePtr )
307 {
308   myMesh = SMESH::SMESH_Mesh::_duplicate( thePtr );
309   mySubMesh = SMESH::SMESH_subMesh::_nil();
310
311   bool isValidMesh = false;
312   if ( !myMesh->_is_nil() ) {
313     _PTR(SObject) aSobj = SMESH::FindSObject( myMesh.in() );
314     //Handle(SALOME_InteractiveObject) anIObj =
315     //  new SALOME_InteractiveObject(aSobj->GetID().c_str(), "SMESH");
316     SUIT_DataOwnerPtr anIObj( new LightApp_DataOwner( aSobj->GetID().c_str() ) );
317
318     isValidMesh = mySelectionMgr->isOk( anIObj );
319   }
320
321   if ( isValidMesh ) {
322     _PTR(SObject) aSO = SMESH::FindSObject( myMesh.in() );
323     myMeshEdit->setText( aSO->GetName().c_str() );
324     myGeomObj = SMESH::GetGeom( aSO );
325   } 
326   else {
327     myMeshEdit->setText( "" );
328     myGeomObj = GEOM::GEOM_Object::_nil();
329   }
330
331   if ( myType == Type_2d ) {
332     loadFromObject( false );
333     displayPreview();
334   }
335 }
336
337 //=======================================================================
338 // function : isValid()
339 // purpose  : Verify validity of entry data
340 //=======================================================================
341 bool SMESHGUI_CreatePatternDlg::isValid()
342 {
343   if ( myGeomObj->_is_nil() ) {
344     SUIT_MessageBox::information( this,
345                                   tr( "SMESH_INSUFFICIENT_DATA" ),
346                                   tr( "SMESHGUI_INVALID_PARAMETERS" ) );
347     return false;
348   }
349   return true;
350 }
351
352 //=======================================================================
353 // function : getDefaultName()
354 // purpose  : Get default pattern name
355 //=======================================================================
356 QString SMESHGUI_CreatePatternDlg::getDefaultName() const
357 {
358   return myType == Type_2d ? tr( "DEFAULT_2D" ) : tr( "DEFAULT_3D" );
359 }
360
361 //=======================================================================
362 // function : onSave()
363 // purpose  : SLOT called when "Save" button pressed. Build pattern and
364 //           save it to disk
365 //=======================================================================
366 void SMESHGUI_CreatePatternDlg::onSave()
367 {
368   try {
369     if ( !isValid() )
370       return;
371
372     if ( !myIsLoaded )
373       loadFromObject( true );
374
375     // Load pattern from object
376     if ( !myIsLoaded )
377       return;
378
379     ///////////////////////////////////////////////////////
380     SUIT_FileDlg* aDlg = new SUIT_FileDlg( this, false );
381     aDlg->setWindowTitle( tr( "SAVE_PATTERN" ) );
382     aDlg->setFileMode( QFileDialog::AnyFile );
383     aDlg->setFilter( tr( "PATTERN_FILT" ) );
384     if ( myName->text() != "" )
385       aDlg->selectFile( myName->text() );
386
387     if ( aDlg->exec() != Accepted )
388       return;
389
390     QString fName = aDlg->selectedFile();
391     if ( fName.isEmpty() )
392       return;
393
394     if ( QFileInfo( fName ).suffix().isEmpty() )
395       fName = autoExtension( fName );
396
397     fName = QDir::convertSeparators( fName );
398
399     QString aData( myPattern->GetString() );
400     long aLen = aData.length();
401
402     QFile aFile( fName );
403     aFile.open( QIODevice::WriteOnly );
404     long aWritten = aFile.write( aData.toLatin1(), aLen );
405     aFile.close();
406
407     if ( aWritten != aLen ) {
408       SUIT_MessageBox::information( this,
409                                     tr( "SMESH_ERROR" ),
410                                     tr( "ERROR_OF_SAVING" ) );
411     } 
412     else {
413       //SUIT_Application::getDesktop()->setSelectionModes(ActorSelection);
414       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
415         aViewWindow->SetSelectionMode( ActorSelection );
416       disconnect( mySelectionMgr, 0, this, 0 );
417       disconnect( mySMESHGUI, 0, this, 0 );
418       mySMESHGUI->ResetState();
419       accept();
420       emit NewPattern();
421     }
422   } 
423   catch ( const SALOME::SALOME_Exception& S_ex ) {
424     SalomeApp_Tools::QtCatchCorbaException( S_ex );
425   } 
426   catch (...) {
427   }
428 }
429
430 //=======================================================================
431 // function : GetPatternName()
432 // purpose  : Get name of pattern
433 //=======================================================================
434 QString SMESHGUI_CreatePatternDlg::GetPatternName() const
435 {
436   return myName->text();
437 }
438
439 //=======================================================================
440 // function : GetPattern()
441 // purpose  : Get result pattern
442 //=======================================================================
443 SMESH::SMESH_Pattern_ptr SMESHGUI_CreatePatternDlg::GetPattern()
444 {
445   return myPattern.in();
446 }
447
448 //=======================================================================
449 // function : onOk()
450 // purpose  : SLOT called when "Ok" button pressed.
451 //=======================================================================
452 void SMESHGUI_CreatePatternDlg::onOk()
453 {
454   try {
455     if ( !isValid() )
456       return;
457
458     if ( !myIsLoaded )
459       loadFromObject( true );
460
461     // Load pattern from object
462     if ( !myIsLoaded ) {
463       return;
464     }
465     else {
466       //SUIT_Application::getDesktop()->setSelectionModes(ActorSelection);
467       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
468         aViewWindow->SetSelectionMode( ActorSelection );
469       disconnect( mySelectionMgr, 0, this, 0 );
470       disconnect( mySMESHGUI, 0, this, 0 );
471       mySMESHGUI->ResetState();
472       accept();
473       emit NewPattern();
474     }
475   } 
476   catch ( const SALOME::SALOME_Exception& S_ex ) {
477     SalomeApp_Tools::QtCatchCorbaException( S_ex );
478   } 
479   catch (...) {
480   }
481 }
482
483 //=======================================================================
484 // function : onClose()
485 // purpose  : SLOT called when "Close" button pressed. Close dialog
486 //=======================================================================
487 void SMESHGUI_CreatePatternDlg::onClose()
488 {
489   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
490     aViewWindow->SetSelectionMode( ActorSelection );
491   disconnect( mySelectionMgr, 0, this, 0 );
492   disconnect( mySMESHGUI, 0, this, 0 );
493   mySMESHGUI->ResetState();
494   reject();
495   emit Close();
496 }
497
498 //=================================================================================
499 // function : onHelp()
500 // purpose  :
501 //=================================================================================
502 void SMESHGUI_CreatePatternDlg::onHelp()
503 {
504   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
505   if ( app ) 
506     app->onHelpContextModule( mySMESHGUI ? app->moduleName( mySMESHGUI->moduleName() ) : QString( "" ), myHelpFileName );
507   else {
508     QString platform;
509 #ifdef WIN32
510     platform = "winapplication";
511 #else
512     platform = "application";
513 #endif
514     SUIT_MessageBox::warning( this, 
515                               tr( "WRN_WARNING" ),
516                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
517                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", 
518                                                                     platform ) ).
519                               arg( myHelpFileName ) );
520   }
521 }
522
523 //=======================================================================
524 // function : loadFromObject()
525 // purpose  : Load pattern from geom object corresponding to the mesh/submesh
526 //=======================================================================
527 bool SMESHGUI_CreatePatternDlg::loadFromObject( const bool theMess )
528 {
529   try {
530     myIsLoaded = false;
531
532     if ( myPattern->_is_nil() )
533       myPattern = SMESH::GetPattern();
534
535     if ( myMesh->_is_nil() && mySubMesh->_is_nil() || myGeomObj->_is_nil() )
536       return false;
537
538     SMESH::SMESH_Mesh_ptr aMesh = mySubMesh->_is_nil() ? myMesh.in() : mySubMesh->GetFather();
539
540     myIsLoaded = myType == Type_2d
541       ? myPattern->LoadFromFace( aMesh, myGeomObj, myProjectChk->isChecked() )
542       : myPattern->LoadFrom3DBlock( aMesh, myGeomObj );
543
544     if ( !myIsLoaded && theMess ) {
545       QString aMess;
546       SMESH::SMESH_Pattern::ErrorCode aCode = myPattern->GetErrorCode();
547
548       if      ( aCode == SMESH::SMESH_Pattern::ERR_LOAD_EMPTY_SUBMESH )   aMess = tr( "ERR_LOAD_EMPTY_SUBMESH" );
549       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADF_NARROW_FACE )    aMess = tr( "ERR_LOADF_NARROW_FACE" );
550       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADF_CLOSED_FACE )    aMess = tr( "ERR_LOADF_CLOSED_FACE" );
551       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADF_CANT_PROJECT )   aMess = tr( "ERR_LOADF_CANT_PROJECT" );
552       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADV_BAD_SHAPE )      aMess = tr( "ERR_LOADV_BAD_SHAPE" );
553       else if ( aCode == SMESH::SMESH_Pattern::ERR_LOADV_COMPUTE_PARAMS ) aMess = tr( "ERR_LOADV_COMPUTE_PARAMS" );
554       else                                                                aMess = tr( "ERROR_OF_CREATION" );
555
556       SUIT_MessageBox::information( this, tr( "SMESH_ERROR" ), aMess );
557     }
558   } 
559   catch ( const SALOME::SALOME_Exception& S_ex ) {
560     SalomeApp_Tools::QtCatchCorbaException( S_ex );
561   }
562
563   return myIsLoaded;
564 }
565
566 //=======================================================================
567 // function : onSelectionDone()
568 // purpose  : SLOT called when selection changed
569 //=======================================================================
570 void SMESHGUI_CreatePatternDlg::onSelectionDone()
571 {
572   try {
573     SALOME_ListIO aList;
574     mySelectionMgr->selectedObjects( aList, SVTK_Viewer::Type() );
575     if ( aList.Extent() != 1 )
576       return;
577
578     // Get mesh or sub-mesh from selection
579     Handle(SALOME_InteractiveObject) anIO = aList.First();
580     SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>( anIO );
581     SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>( anIO );
582     if ( aMesh->_is_nil() && aSubMesh->_is_nil() )
583       return;
584
585     // Get geom object corresponding to the mesh
586     _PTR(SObject) aSO;
587     if ( !aMesh->_is_nil() )
588       aSO = SMESH::FindSObject( aMesh.in() );
589     else
590       aSO = SMESH::FindSObject( aSubMesh.in() );
591
592     GEOM::GEOM_Object_var aGeomObj = SMESH::GetGeom( aSO );
593     if ( aGeomObj->_is_nil() )
594       return;
595
596     myGeomObj = aGeomObj;
597
598     // init class fields
599     if ( !aMesh->_is_nil() ) {
600       myMesh = aMesh;
601       mySubMesh = SMESH::SMESH_subMesh::_nil();
602     } 
603     else {
604       mySubMesh = aSubMesh;
605       myMesh = SMESH::SMESH_Mesh::_nil();
606     }
607
608     QString aName;
609     SMESH::GetNameOfSelectedIObjects( mySelectionMgr, aName );
610     myMeshEdit->setText( aName );
611
612     if ( myType == Type_2d ) {
613       loadFromObject( true );
614       displayPreview();
615     }
616   } 
617   catch (...) {
618     myMesh = SMESH::SMESH_Mesh::_nil();
619     mySubMesh = SMESH::SMESH_subMesh::_nil();
620     myGeomObj = GEOM::GEOM_Object::_nil();
621     erasePreview();
622   }
623 }
624
625 //=======================================================================
626 // function : onDeactivate()
627 // purpose  : SLOT called when dialog must be deativated
628 //=======================================================================
629 void SMESHGUI_CreatePatternDlg::onDeactivate()
630 {
631   disconnect( mySelectionMgr, 0, this, 0 );
632   setEnabled( false );
633 }
634
635 //=======================================================================
636 // function : enterEvent()
637 // purpose  : Event filter
638 //=======================================================================
639 void SMESHGUI_CreatePatternDlg::enterEvent( QEvent* )
640 {
641   // there is a stange problem that enterEvent() comes after onSave()
642   if ( isVisible () ) {
643     mySMESHGUI->EmitSignalDeactivateDialog();
644     setEnabled( true );
645     activateSelection();
646     connect( mySelectionMgr, SIGNAL( currentSelectionChanged() ), SLOT( onSelectionDone() ) );
647   }
648 }
649
650 //=================================================================================
651 // function : closeEvent()
652 // purpose  : Close dialog box
653 //=================================================================================
654 void SMESHGUI_CreatePatternDlg::closeEvent( QCloseEvent* )
655 {
656   onClose();
657 }
658
659 //=======================================================================
660 // function : onSelBtnClicked()
661 // purpose  : SLOT. Called when -> button clicked.
662 //=======================================================================
663 void SMESHGUI_CreatePatternDlg::onSelBtnClicked()
664 {
665   onSelectionDone();
666 }
667
668 //================================================================
669 // function : autoExtension()
670 // purpose  : Append extension to the file name
671 //================================================================
672 QString SMESHGUI_CreatePatternDlg::autoExtension( const QString& theFileName ) const
673 {
674   QString anExt = theFileName.section('.', -1);
675   return anExt != "smp" && anExt != "SMP" ? theFileName + ".smp" : theFileName;
676 }
677
678 //=======================================================================
679 // function : displayPreview()
680 // purpose  : Display preview
681 //=======================================================================
682 void SMESHGUI_CreatePatternDlg::displayPreview()
683 {
684   // Redisplay preview in dialog
685   try {
686     if ( !myIsLoaded ) {
687       erasePreview();
688     }
689     else {
690       SMESH::point_array_var pnts = myPattern->GetPoints();
691       SMESH::long_array_var keyPoints = myPattern->GetKeyPoints();
692       SMESH::array_of_long_array_var elemPoints = myPattern->GetElementPoints( false );
693
694       if ( pnts->length()       == 0 ||
695            keyPoints->length()  == 0 ||
696            elemPoints->length() == 0 ) {
697         myIsLoaded = false;
698         erasePreview();
699         return;
700       }
701
702       PointVector aPoints( pnts->length() );
703       QVector<int> aKeyPoints( keyPoints->length() );
704       ConnectivityVector anElemPoints( elemPoints->length() );
705
706       for ( int i = 0, n = pnts->length(); i < n; i++ )
707         aPoints[ i ] = pnts[ i ];
708
709       for ( int i2 = 0, n2 = keyPoints->length(); i2 < n2; i2++ )
710         aKeyPoints[ i2 ] = keyPoints[ i2 ];
711
712       for (int i3 = 0, n3 = elemPoints->length(); i3 < n3; i3++) {
713         QVector<int> aVec (elemPoints[ i3 ].length());
714         for (int i4 = 0, n4 = elemPoints[ i3 ].length(); i4 < n4; i4++)
715           aVec[ i4 ] = elemPoints[ i3 ][ i4 ];
716
717         anElemPoints[ i3 ] = aVec;
718       }
719
720       myPicture2d->SetPoints( aPoints, aKeyPoints, anElemPoints );
721     }
722
723     return;
724
725   } 
726   catch ( const SALOME::SALOME_Exception& S_ex ) {
727     SalomeApp_Tools::QtCatchCorbaException( S_ex );
728   }
729   catch (...) {
730   }
731   erasePreview();
732 }
733
734 //=======================================================================
735 // function : erasePreview()
736 // purpose  : Erase preview
737 //=======================================================================
738 void SMESHGUI_CreatePatternDlg::erasePreview()
739 {
740   // Erase preview in 2D viewer
741   myPicture2d->SetPoints( PointVector(), QVector<int>(), ConnectivityVector() );
742 }
743
744 //=======================================================================
745 // function : activateSelection()
746 // purpose  : Activate selection in accordance with current pattern type
747 //=======================================================================
748 void SMESHGUI_CreatePatternDlg::activateSelection()
749 {
750   mySelectionMgr->clearFilters();
751   //SUIT_Application::getDesktop()->setSelectionModes(ActorSelection);
752   if ( SVTK_ViewWindow* aViewWindow = SMESH::GetViewWindow( mySMESHGUI ) )
753     aViewWindow->SetSelectionMode( ActorSelection );
754
755   if ( myType == Type_2d ) {
756     mySelectionMgr->installFilter( new SMESH_NumberFilter( "SMESH",
757                                                            TopAbs_SHAPE,
758                                                            -1,
759                                                            TopAbs_FACE ) );
760   } 
761   else {
762     TColStd_MapOfInteger aTypes;
763     aTypes.Add( TopAbs_SHELL );
764     aTypes.Add( TopAbs_SOLID );
765     mySelectionMgr->installFilter( new SMESH_NumberFilter( "SMESH",
766                                                            TopAbs_FACE,
767                                                            6,
768                                                            aTypes,
769                                                            GEOM::GEOM_Object::_nil(),
770                                                            true ) );
771   }
772 }
773
774 //=======================================================================
775 // function : onTypeChanged()
776 // purpose  : SLOT. Called when pattern type changed.
777 //            Change dialog's look and feel
778 //=======================================================================
779 void SMESHGUI_CreatePatternDlg::onTypeChanged( int theType )
780 {
781   if ( myType == theType )
782     return;
783
784   myType = theType;
785
786   myPicture2d->setVisible( theType == Type_2d );
787   myProjectChk->setVisible( theType == Type_2d );
788 }
789
790 //=================================================================================
791 // function : keyPressEvent()
792 // purpose  :
793 //=================================================================================
794 void SMESHGUI_CreatePatternDlg::keyPressEvent( QKeyEvent* e )
795 {
796   QDialog::keyPressEvent( e );
797   if ( e->isAccepted() )
798     return;
799
800   if ( e->key() == Qt::Key_F1 ){
801     e->accept();
802     onHelp();
803   }
804 }