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