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