Salome HOME
Merge from OCC_development_generic_2006
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 /**
21 *  SMESH SMESHGUI
22 *
23 *  Copyright (C) 2005  CEA/DEN, EDF R&D
24 *
25 *
26 *
27 *  File   : SMESHGUI_MeshOp.h
28 *  Author : Sergey LITONIN
29 *  Module : SMESHGUI
30 */
31
32 #include "SMESHGUI_MeshOp.h"
33 #include "SMESHGUI_MeshDlg.h"
34 #include "SMESHGUI_ShapeByMeshDlg.h"
35 #include "SMESH_TypeFilter.hxx"
36 #include "SMESHGUI.h"
37
38 #include "SMESHGUI_HypothesesUtils.h"
39 #include "SMESHGUI_Hypotheses.h"
40 #include "SMESHGUI_Utils.h"
41 #include "SMESHGUI_GEOMGenUtils.h"
42 #include "SMESHGUI_VTKUtils.h"
43
44 #include "SMESH_TypeFilter.hxx"
45 #include "SMESH_NumberFilter.hxx"
46
47 #include "GEOM_SelectionFilter.h"
48 #include "GEOMBase.h"
49 #include "GeometryGUI.h"
50
51 #include "SalomeApp_Tools.h"
52 #include "SALOMEDSClient_Study.hxx"
53 #include "SALOMEDSClient_AttributeIOR.hxx"
54 #include "SALOMEDSClient_AttributeName.hxx"
55 #include "SALOMEDS_SComponent.hxx"
56 #include "SALOMEDS_SObject.hxx"
57
58 #include "LightApp_SelectionMgr.h"
59 #include "LightApp_UpdateFlags.h"
60 #include "SUIT_MessageBox.h"
61 #include "SUIT_Desktop.h"
62 #include "SUIT_OverrideCursor.h"
63 #include "SALOME_InteractiveObject.hxx"
64 #include "SALOME_ListIO.hxx"
65
66 #include "utilities.h"
67
68 #include <qstringlist.h>
69 #include <qlineedit.h>
70
71 #include <TopoDS_Shape.hxx>
72 #include <TopExp_Explorer.hxx>
73
74 enum { GLOBAL_ALGO_TAG        =3,
75        GLOBAL_HYPO_TAG        =2,
76        LOCAL_ALGO_TAG         =2,
77        LOCAL_HYPO_TAG         =1,
78        SUBMESH_ON_EDGE_TAG    =5,
79        SUBMESH_ON_WIRE_TAG    =6,
80        SUBMESH_ON_FACE_TAG    =7,
81        SUBMESH_ON_SHELL_TAG   =8,
82        SUBMESH_ON_SOLID_TAG   =9,
83        SUBMESH_ON_COMPOUND_TAG=10 };
84        
85 //================================================================================
86 /*!
87  * \brief Constructor
88   * \param theToCreate - if this parameter is true then operation is used for creation,
89   * for editing otherwise
90  *
91  * Initialize operation
92 */
93 //================================================================================
94 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
95 : SMESHGUI_SelectionOp(),
96   myToCreate( theToCreate ),
97   myIsMesh( theIsMesh ),
98   myDlg( 0 ),
99   myShapeByMeshDlg( 0 )
100 {
101   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
102     GeometryGUI::InitGeomGen();
103 }
104
105 //================================================================================
106 /*!
107  * \brief Destructor
108 */
109 //================================================================================
110 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
111 {
112   if ( myDlg )
113     delete myDlg;
114 }
115
116 //================================================================================
117 /*!
118  * \brief Gets dialog of this operation
119   * \retval LightApp_Dialog* - pointer to dialog of this operation
120 */
121 //================================================================================
122 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
123 {
124   return myDlg;
125 }
126
127 //================================================================================
128 /*!
129  * \brief Creates or edits mesh
130   * \retval bool - TRUE if operation is performed successfully, FALSE otherwise
131  *
132  * Virtual slot redefined from the base class called when "Apply" button is clicked
133  * creates or edits mesh
134  */
135 //================================================================================
136 bool SMESHGUI_MeshOp::onApply()
137 {
138   if( isStudyLocked() )
139     return false;
140
141   QString aMess;
142   if ( !isValid( aMess ) )
143   {
144     if ( aMess != "" )
145       SUIT_MessageBox::warn1( myDlg,
146         tr( "SMESH_WRN_WARNING" ), aMess, tr( "SMESH_BUT_OK" ) );
147     return false;
148   }
149
150   bool aResult = false;
151   aMess = "";
152   try
153   {
154     if ( myToCreate && myIsMesh )
155       aResult = createMesh( aMess );
156     if ( myToCreate && !myIsMesh )
157       aResult = createSubMesh( aMess );
158     else if ( !myToCreate )
159       aResult = editMeshOrSubMesh( aMess );
160     if ( aResult )
161       update( UF_ObjBrowser | UF_Model );
162   }
163   catch ( const SALOME::SALOME_Exception& S_ex )
164   {
165     SalomeApp_Tools::QtCatchCorbaException( S_ex );
166     aResult = false;
167   }
168   catch ( ... )
169   {
170     aResult = false;
171   }
172
173   if ( aResult )
174   {
175     if ( myToCreate )
176       setDefaultName();
177   }
178   else
179   {
180     if ( aMess == "" )
181       aMess = tr( "SMESH_OPERATION_FAILED" );
182     SUIT_MessageBox::warn1( myDlg,
183       tr( "SMESH_ERROR" ), aMess, tr( "SMESH_BUT_OK" ) );
184   }
185
186   return aResult;
187 }
188
189 //================================================================================
190 /*!
191  * \brief Creates dialog if necessary and shows it
192  *
193  * Virtual method redefined from base class called when operation is started creates
194  * dialog if necessary and shows it, activates selection
195  */
196 //================================================================================
197 void SMESHGUI_MeshOp::startOperation()
198 {
199   if( !myDlg )
200   {
201     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
202     for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
203     {
204       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
205               this, SLOT( onCreateHyp( const int, const int) ) );
206       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
207               this, SLOT( onEditHyp( const int, const int) ) );
208     }
209     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
210     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
211   }
212   SMESHGUI_SelectionOp::startOperation();
213
214   // iterate through dimensions and get available and existing algoritms and hypotheses,
215   // set them to the dialog
216   int i, j;
217   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
218   for ( i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
219   {
220     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
221     QStringList anAvailable, anExisting;
222     for ( j = Algo; j <= AddHyp; j++ )
223     {
224       availableHyps( i, j, anAvailable );
225       existingHyps( i, j, aFather, anExisting, myExistingHyps[ i ][ j ] );
226
227       aTab->setAvailableHyps( j, anAvailable );
228       aTab->setExistingHyps( j, anExisting );
229     }
230   }
231   if ( myToCreate )
232   {
233     setDefaultName();
234     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
235   }
236   else
237     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
238
239   myDlg->setHypoSets( SMESH::GetHypothesesSets() );
240
241   selectionDone();
242
243   myDlg->setCurrentTab( SMESH::DIM_1D );
244   myDlg->show();
245 }
246
247 //================================================================================
248 /*!
249  * \brief Creates selection filter
250   * \param theId - identifier of current selection widget
251   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
252  *
253  * Creates selection filter in accordance with identifier of current selection widget
254  */
255 //================================================================================
256 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
257 {
258   if ( theId == SMESHGUI_MeshDlg::Geom )
259   {
260 //     TColStd_MapOfInteger allTypesMap;
261 //     for ( int i = 0; i < 10; i++ )
262 //       allTypesMap.Add( i );
263 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
264     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
265   }
266   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
267     return new SMESH_TypeFilter( MESHorSUBMESH );
268   else if ( theId == SMESHGUI_MeshDlg::Mesh )
269     return new SMESH_TypeFilter( MESH );
270   else
271     return 0;
272 }
273
274 //================================================================================
275 /*!
276  * \brief check if selected shape is a subshape of the shape to mesh
277   * \retval bool - check result
278  */
279 //================================================================================
280
281 bool SMESHGUI_MeshOp::isSubshapeOk() const
282 {
283   if ( !myToCreate || myIsMesh ) // not submesh creation
284     return false;
285
286   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
287   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
288   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
289   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
290   if ( pMesh && pGeom ) {
291     SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
292     if ( !mesh->_is_nil() ) {
293       GEOM::GEOM_Object_var mainGeom, subGeom;
294       mainGeom = mesh->GetShapeToMesh();
295       subGeom  = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
296       if ( !mainGeom->_is_nil() && !subGeom->_is_nil() ) {
297         TopoDS_Shape mainShape, subShape;
298         if ( GEOMBase::GetShape( mainGeom, mainShape ) &&
299              GEOMBase::GetShape( subGeom, subShape ) )
300         {
301           int index = GEOMBase::GetIndex( subShape, mainShape, 0 );
302           if ( index > 0 ) {
303             // 1 is index of mainShape itself
304             return index > 1; // it is a subshape
305           }
306           // is it a group?
307           GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
308           _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
309           if ( !geomGen->_is_nil() && aStudy ) {
310             GEOM::GEOM_IGroupOperations_var op =
311               geomGen->GetIGroupOperations( aStudy->StudyId() );
312             if ( ! op->_is_nil() ) {
313               GEOM::GEOM_Object_var mainObj = op->GetMainShape( subGeom );
314               if ( !mainObj->_is_nil() )
315                 return ( string( mainObj->GetEntry() ) == string( mainGeom->GetEntry() ));
316             }
317           }
318         }
319       }
320     }
321   }
322   return false;
323 }
324
325 //================================================================================
326 /*!
327  * \brief find an existing submesh by the selected shape
328   * \retval _PTR(SObject) - the found submesh SObject
329  */
330 //================================================================================
331
332 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
333 {
334   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
335   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
336   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
337   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
338   if ( pMesh && pGeom ) {
339     GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
340     if ( !geom->_is_nil() ) {
341       int tag = -1;
342       switch ( geom->GetShapeType() ) {
343       case GEOM::EDGE:     tag = SUBMESH_ON_EDGE_TAG    ; break;
344       case GEOM::WIRE:     tag = SUBMESH_ON_WIRE_TAG    ; break;
345       case GEOM::FACE:     tag = SUBMESH_ON_FACE_TAG    ; break;
346       case GEOM::SHELL:    tag = SUBMESH_ON_SHELL_TAG   ; break;
347       case GEOM::SOLID:    tag = SUBMESH_ON_SOLID_TAG   ; break;
348       case GEOM::COMPOUND: tag = SUBMESH_ON_COMPOUND_TAG; break;
349       default:;
350       }
351       _PTR(SObject) aSubmeshRoot;
352       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
353       if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
354       {
355         _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
356         for (; smIter->More(); smIter->Next() )
357         {
358           _PTR(SObject) aSmObj = smIter->Value();
359           _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
360           for (; anIter1->More(); anIter1->Next()) {
361             _PTR(SObject) pGeom2 = anIter1->Value();
362             if ( pGeom2->ReferencedObject( pGeom2 ) &&
363                  pGeom2->GetID() == pGeom->GetID() )
364               return aSmObj;
365           }
366         }
367       }
368     }     
369   }
370   return _PTR(SObject)();
371 }
372
373 //================================================================================
374 /*!
375  * \brief Updates dialog's look and feel
376  *
377  * Virtual method redefined from the base class updates dialog's look and feel
378  */
379 //================================================================================
380 void SMESHGUI_MeshOp::selectionDone()
381 {
382   if ( myShapeByMeshDlg && myShapeByMeshDlg->isShown() )
383     return;
384
385   SMESHGUI_SelectionOp::selectionDone();
386
387   try
388   {
389     // Enable tabs according to shape dimension
390
391     int shapeDim = 3;
392
393     GEOM::GEOM_Object_var aGeomVar;
394     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
395     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
396     if ( pGeom ) {
397       aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
398     }
399     else {
400       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
401       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
402       aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
403     }
404     if ( !aGeomVar->_is_nil() ) {
405       shapeDim = 0;
406       switch ( aGeomVar->GetShapeType() ) {
407       case GEOM::SOLID:
408       case GEOM::SHELL:  shapeDim = 3; break;
409       case GEOM::FACE:   shapeDim = 2; break;
410       case GEOM::WIRE:   
411       case GEOM::EDGE:   shapeDim = 1; break;
412       case GEOM::VERTEX: shapeDim = 0; break;
413       default:
414         TopoDS_Shape aShape;
415         if ( GEOMBase::GetShape(aGeomVar, aShape)) {
416           TopExp_Explorer exp( aShape, TopAbs_SHELL );
417           if ( exp.More() )
418             shapeDim = 3;
419           else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
420             shapeDim = 2;
421           else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
422             shapeDim = 1;
423           else
424             shapeDim = 0;
425         }
426       }
427     }
428     myDlg->setMaxHypoDim( shapeDim );
429
430     if ( !myToCreate ) // edition: read hypotheses
431     {
432       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
433       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
434       if ( pObj != 0 )
435       {
436         SMESH::SMESH_subMesh_var aVar =
437           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
438         myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
439         myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
440         myDlg->updateGeometry();
441         myDlg->adjustSize();
442         readMesh();
443       }
444       else
445         myDlg->reset();
446
447     }
448     else if ( !myIsMesh ) // submesh creation
449     {
450       // if a submesh on the selected shape already exist, pass to submesh edition mode
451       if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
452         SMESH::SMESH_subMesh_var sm = 
453           SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
454         bool editSubmesh = ( !sm->_is_nil() &&
455                              SUIT_MessageBox::question2( myDlg, tr( "SMESH_WARNING" ),
456                                                          tr( "EDIT_SUBMESH_QUESTION"),
457                                                          tr( "SMESH_BUT_YES" ),
458                                                          tr( "SMESH_BUT_NO" ), 1, 0, 0 ));
459         if ( editSubmesh )
460         {
461           selectionMgr()->clearFilters();
462           selectObject( pSubmesh );
463           SMESHGUI::GetSMESHGUI()->switchToOperation(704);
464           return;
465         }
466         else
467         {
468           selectObject( _PTR(SObject)() );
469           myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
470         }
471       }
472
473       // enable/disable popup for choice of geom selection way
474       bool enable = false;
475       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
476       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
477         SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
478         if ( !mesh->_is_nil() )
479           enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
480       }            
481       myDlg->setGeomPopupEnabled( enable );
482     }
483   }
484   catch ( const SALOME::SALOME_Exception& S_ex )
485   {
486     SalomeApp_Tools::QtCatchCorbaException( S_ex );
487   }
488   catch ( ... )
489   {
490   }
491 }
492
493 //================================================================================
494 /*!
495  * \brief Verifies validity of input data
496   * \param theMess - Output parameter intended for returning error message
497   * \retval bool  - TRUE if input data is valid, false otherwise
498  *
499  * Verifies validity of input data. This method is called when "Apply" or "OK" button
500  * is pressed before mesh creation or editing.
501  */
502 //================================================================================
503 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
504 {
505   // Selected object to be  edited
506   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
507   {
508     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
509     return false;
510   }
511
512   // Name
513   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
514   aMeshName = aMeshName.stripWhiteSpace();
515   if ( aMeshName == "" )
516   {
517     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
518     return false;
519   }
520
521   // Imported mesh, if create sub-mesh or edit mesh
522   if ( !myToCreate || ( myToCreate && !myIsMesh ))
523   {
524     QString aMeshEntry = myDlg->selectedObject
525       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
526     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
527       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
528       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
529         theMess = tr( "IMPORTED_MESH" );
530         return false;
531       }
532     }
533   }
534
535   // Geom
536   if ( myToCreate )
537   {
538     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
539     if ( aGeomEntry == "" )
540     {
541       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
542       return false;
543     }
544     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
545     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
546     {
547       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
548       return false;
549     }
550
551     // Mesh
552     if ( !myIsMesh ) // i.e sub-mesh creation,
553     {
554       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
555       if ( aMeshEntry == "" )
556       {
557         theMess = tr( "MESH_IS_NOT_DEFINED" );
558         return false;
559       }
560       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
561       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
562       {
563         theMess = tr( "MESH_IS_NULL" );
564         return false;
565       }
566       if ( !isSubshapeOk() )
567       {
568         theMess = tr( "INVALID_SUBSHAPE" );
569         return false;
570       }
571     }
572   }
573     
574   return true;
575 }
576
577 //================================================================================
578 /*!
579  * \brief Gets available hypotheses or algorithms
580   * \param theDim - specifies dimension of returned hypotheses/algorifms
581   * \param theHypType - specifies whether algorims or hypotheses or additional ones
582   * are retrieved (possible values are in HypType enumeration)
583   * \param theHyps - Output list of hypotheses' names
584  *
585  * Gets available hypotheses or algorithm in accordance with input parameters
586  */
587 //================================================================================
588 void SMESHGUI_MeshOp::availableHyps( const int theDim,
589                                      const int theHypType,
590                                      QStringList& theHyps ) const
591 {
592   theHyps.clear();
593   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses(
594     theHypType == Algo , theDim, theHypType == AddHyp );
595   QStringList::const_iterator anIter;
596   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
597   {
598     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
599     theHyps.append( aData->Label );
600   }
601 }
602
603 //================================================================================
604 /*!
605  * \brief Gets existing hypotheses or algorithms
606   * \param theDim - specifies dimension of returned hypotheses/algorifms
607   * \param theHypType - specifies whether algorims or hypotheses or additional ones
608   * are retrieved (possible values are in HypType enumeration)
609   * \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
610   * \param theHyps - output list of names.
611   * \param theHypVars - output list of variables.
612  *
613  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
614  * input parameters
615  */
616 //================================================================================
617 void SMESHGUI_MeshOp::existingHyps( const int theDim,
618                                     const int theHypType,
619                                     _PTR(SObject) theFather,
620                                     QStringList& theHyps,
621                                     QValueList<SMESH::SMESH_Hypothesis_var>& theHypVars )
622 {
623   // Clear hypoheses list
624   theHyps.clear();
625   theHypVars.clear();
626
627   if ( !theFather )
628     return;
629
630   _PTR(SObject)          aHypRoot;
631   _PTR(GenericAttribute) anAttr;
632   _PTR(AttributeName)    aName;
633   _PTR(AttributeIOR)     anIOR;
634
635   bool isMesh = !_CAST( SComponent, theFather );
636   int aPart = -1;
637   if ( isMesh )
638     aPart = theHypType == Algo ? GLOBAL_ALGO_TAG : GLOBAL_HYPO_TAG;
639   else
640     aPart = theHypType == Algo ? LOCAL_ALGO_TAG : LOCAL_HYPO_TAG;
641
642   if ( theFather->FindSubObject( aPart, aHypRoot ) )
643   {
644     _PTR(ChildIterator) anIter =
645       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
646     for (; anIter->More(); anIter->Next() )
647     {
648       _PTR(SObject) anObj = anIter->Value();
649       if ( isMesh ) // i.e. mesh or submesh
650       {
651         _PTR(SObject) aRefObj;
652         if ( anObj->ReferencedObject( aRefObj ) )
653           anObj = aRefObj;
654         else
655           continue;
656       }
657       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
658       {
659         aName = anAttr;
660         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
661         if ( !CORBA::is_nil( aVar ) )
662         {
663           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
664           if ( !aHypVar->_is_nil() )
665           {
666             QString aHypType( aHypVar->GetName() );
667             HypothesisData* aData = SMESH::GetHypothesisData( aHypType );
668             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
669                  ( theHypType == AddHyp ) == aData->IsAux )
670             {
671               theHyps.append( aName->Value().c_str() );
672               theHypVars.append( aHypVar );
673             }
674           }
675         }
676       }
677     }
678   }
679 }
680
681 //================================================================================
682 /*!
683  * \brief If create or edit a submesh, return a hypothesis holding parameters used
684  *        to mesh a subshape
685   * \param aHypType - The hypothesis type name
686   * \param aServerLib - Server library name
687   * \param hypData - The structure holding the hypothesis type etc.
688   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
689  */
690 //================================================================================
691
692 SMESH::SMESH_Hypothesis_var
693 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
694                                           const QString& aServerLib ) const
695 {
696   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
697     return SMESH::SMESH_Hypothesis::_nil();
698
699   const int nbColonsInMeshEntry = 3;
700   bool isSubMesh = myToCreate ?
701     !myIsMesh :
702     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).contains(':') > nbColonsInMeshEntry; 
703
704   if ( isSubMesh )
705   {
706     // get mesh and geom object
707     SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
708     GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
709
710     QString anEntry = myDlg->selectedObject
711       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
712     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.latin1() ))
713     {
714       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
715       if ( myToCreate ) // mesh and geom may be selected
716       {
717         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
718         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
719         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.latin1() ))
720           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
721       }
722       else // edition: sub-mesh may be selected
723       {
724         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
725         if ( !sm->_is_nil() ) {
726           aMeshVar = sm->GetFather();
727           aGeomVar = sm->GetSubShape();
728         }
729       }
730     }
731
732     if ( !aMeshVar->_is_nil() && !aGeomVar->_is_nil() )
733       return SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType,
734                                                                     aServerLib,
735                                                                     aMeshVar,
736                                                                     aGeomVar );
737   }
738   return SMESH::SMESH_Hypothesis::_nil();
739 }
740
741 //================================================================================
742 /*!
743  * \brief Calls plugin methods for hypothesis creation
744   * \param theHypType - specifies whether main hypotheses or additional ones
745   * are created
746   * \param theIndex - index of type of hypothesis to be cerated
747  *
748  * Speicfies dimension of hypothesis to be created (using sender() method), specifies
749  * its type and calls plugin methods for hypothesis creation
750  */
751 //================================================================================
752 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
753 {
754   // Speicfies dimension of hypothesis to be created
755   const QObject* aSender = sender();
756   int aDim = -1;
757   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
758     if ( aSender == myDlg->tab( i ) )
759       aDim = i;
760   if ( aDim == -1 )
761     return;
762
763   // Speicfies type of hypothesis to be created
764   QStringList aHypTypeNames = SMESH::GetAvailableHypotheses( false , aDim, theHypType == AddHyp );
765   if ( theIndex < 0 || theIndex >= aHypTypeNames.count() )
766     return;
767
768   QString aHypTypeName = aHypTypeNames[ theIndex ];
769   HypothesisData* aData = SMESH::GetHypothesisData( aHypTypeName.latin1() );
770   if ( aData == 0 )
771     return;
772
773   QString aClientLibName = aData->ClientLibName;
774   QStringList anOldHyps;
775   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
776   existingHyps( aDim, theHypType, aFather, anOldHyps, myExistingHyps[ aDim ][ theHypType ] );
777
778   if ( aClientLibName == "" )
779   {
780     // Call hypothesis creation server method (without GUI)
781     QString aHypName = aData->Label;
782     SMESH::CreateHypothesis( aHypTypeName, aHypName, false );
783   }
784   else
785   {
786     // Get hypotheses creator client (GUI)
787     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aHypTypeName );
788
789     // Create hypothesis
790     if ( aCreator )
791     {
792       // When create or edit a submesh, try to initialize a new hypothesis
793       // with values used to mesh a subshape
794       SMESH::SMESH_Hypothesis_var initParamHyp =
795         getInitParamsHypothesis( aHypTypeName, aData->ServerLibName );
796
797       if ( initParamHyp->_is_nil() )
798         aCreator->create( false, myDlg );
799       else
800         aCreator->create( initParamHyp, myDlg );
801     }
802     else
803     {
804       SMESH::CreateHypothesis( aHypTypeName, aData->Label, false );
805     }
806   }
807
808   QStringList aNewHyps;
809   aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
810   existingHyps( aDim, theHypType, aFather, aNewHyps, myExistingHyps[ aDim ][ theHypType ] );
811   if ( aNewHyps.count() > anOldHyps.count() )
812   {
813     for ( int i = anOldHyps.count(); i < aNewHyps.count(); i++ )
814       myDlg->tab( aDim )->addHyp( theHypType, aNewHyps[ i ] );
815   }
816 }
817
818 //================================================================================
819 /*!
820  * \brief Calls plugin methods for hypothesis editing
821   * \param theHypType - specifies whether main hypothesis or additional one
822   * is edited
823   * \param theIndex - index of existing hypothesis
824  *
825  * Calls plugin methods for hypothesis editing
826  */
827 //================================================================================
828 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
829 {
830   // Speicfies dimension of hypothesis to be created
831   const QObject* aSender = sender();
832   int aDim = -1;
833   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
834     if ( aSender == myDlg->tab( i ) )
835       aDim = i;
836   if ( aDim == -1 )
837     return;
838
839   QValueList<SMESH::SMESH_Hypothesis_var> aList = myExistingHyps[ aDim ][ theHypType ];
840   SMESH::SMESH_Hypothesis_var aHyp = aList[ theIndex - 1 ];
841   if ( aHyp->_is_nil() )
842     return;
843
844   char* aTypeName = aHyp->GetName();
845   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aTypeName );
846   if ( aCreator )
847     aCreator->edit( aHyp.in(), dlg() );
848 }
849
850 //================================================================================
851 /*!
852  * \brief Creates and selects hypothesis of hypotheses set 
853   * \param theSetName - The name of hypotheses set
854  */
855 //================================================================================
856
857 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
858 {
859   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet( theSetName );
860   if ( !aHypoSet ) return;
861
862   for ( int aHypType = Algo; aHypType < AddHyp; aHypType++ )
863   {
864     bool isAlgo = (aHypType == Algo);
865
866     // clear all hyps
867     for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
868       setCurrentHyp( dim, aHypType, -1 );
869
870     // set hyps from the set
871     
872     QStringList* aHypoList = isAlgo ? & aHypoSet->AlgoList : & aHypoSet->HypoList;
873     for ( int i = 0, n = aHypoList->count(); i < n; i++ )
874     {
875       const QString& aHypoTypeName = (*aHypoList)[ i ];
876       HypothesisData* aHypData = SMESH::GetHypothesisData( aHypoTypeName );
877       if ( !aHypData )
878         continue;
879
880       int aDim = aHypData->Dim[0];
881       // create or/and set
882       int index = -1;
883       if ( isAlgo )
884       {
885         QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, aDim );
886         index = aHypTypeNameList.findIndex( aHypoTypeName );
887         if ( index < 0 ) continue;
888         setCurrentHyp ( aDim, aHypType, index );
889       }
890       else
891       {
892         // try to find an existing hypo
893         QValueList<SMESH::SMESH_Hypothesis_var> & aList = myExistingHyps[ aDim ][ aHypType ];
894         int /*iHyp = 0,*/ nbHyp = aList.count();
895 //         for ( ; iHyp < nbHyp; ++iHyp )
896 //         {
897 //           SMESH::SMESH_Hypothesis_var aHyp = aList[ iHyp ];
898 //           if ( !aHyp->_is_nil() && aHypoTypeName == aHyp->GetName() ) {
899 //             index = iHyp;
900 //             break;
901 //           }
902 //         }
903         if ( index >= 0 ) // found
904         {
905           // select the found hypothesis
906           setCurrentHyp ( aDim, aHypType, index );
907         }
908         else
909         {
910           // create a hypothesis
911           QString aClientLibName = aHypData->ClientLibName;
912           if ( aClientLibName == "" ) {
913             // Call hypothesis creation server method (without GUI)
914             SMESH::CreateHypothesis( aHypoTypeName, aHypData->Label, isAlgo );
915           }
916           else {
917             // Get hypotheses creator client (GUI)
918             SMESHGUI_GenericHypothesisCreator* aCreator =
919               SMESH::GetHypothesisCreator( aHypoTypeName );
920             if ( aCreator )
921             {
922               // When create or edit a submesh, try to initialize a new hypothesis
923               // with values used to mesh a subshape
924               SMESH::SMESH_Hypothesis_var initParamHyp =
925                 getInitParamsHypothesis( aHypoTypeName, aHypData->ServerLibName );
926               aCreator->create( initParamHyp, myDlg );
927             }
928             else
929             {
930               SMESH::CreateHypothesis( aHypoTypeName, aHypData->Label, isAlgo );
931             }
932           }
933           QStringList aNewHyps;
934           _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
935           existingHyps( aDim, aHypType, aFather, aNewHyps, aList );
936           if ( aList.count() > nbHyp )
937           {
938             for ( int i = nbHyp; i < aNewHyps.count(); i++ )
939               myDlg->tab( aDim )->addHyp( aHypType, aNewHyps[ i ] );
940           }
941         }
942       }
943     } // loop on hypos in the set
944   } // loop on algo/hypo
945 }
946
947 //================================================================================
948 /*!
949  * \brief Creates mesh
950   * \param theMess - Output parameter intended for returning error message
951   * \retval bool  - TRUE if mesh is created, FALSE otherwise
952  *
953  * Creates mesh
954  */
955 //================================================================================
956 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
957 {
958   theMess = "";
959
960   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
961   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
962   GEOM::GEOM_Object_var aGeomVar =
963     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
964
965   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
966   if ( aSMESHGen->_is_nil() )
967     return false;
968
969   SUIT_OverrideCursor aWaitCursor;
970
971   // create mesh
972   SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
973   if ( aMeshVar->_is_nil() )
974     return false;
975   _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
976   if ( aMeshSO )
977     SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
978
979   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
980   {
981     if ( !isAccessibleDim( aDim )) continue;
982
983     // assign hypotheses
984     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
985     {
986       int aHypIndex = currentHyp( aDim, aHypType );
987       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
988       {
989         SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
990         if ( !aHypVar->_is_nil() )
991           SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
992       }
993     }
994     // find or create algorithm
995     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
996     if ( !anAlgoVar->_is_nil() )
997       SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
998   }
999   return true;
1000 }
1001
1002 //================================================================================
1003 /*!
1004  * \brief Creates sub-mesh
1005   * \param theMess - Output parameter intended for returning error message
1006   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1007  *
1008  * Creates sub-mesh
1009  */
1010 //================================================================================
1011 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
1012 {
1013   theMess = "";
1014
1015   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1016   if ( aSMESHGen->_is_nil() )
1017     return false;
1018
1019   // get mesh object
1020   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1021   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
1022   SMESH::SMESH_Mesh_var aMeshVar =
1023     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1024
1025   // get geom object
1026   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1027   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1028   GEOM::GEOM_Object_var aGeomVar =
1029     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1030
1031   SUIT_OverrideCursor aWaitCursor;
1032
1033   // create sub-mesh
1034   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
1035   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.latin1() );
1036
1037   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
1038   {
1039     if ( !isAccessibleDim( aDim )) continue;
1040
1041     // find or create algorithm
1042     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1043     if ( !anAlgoVar->_is_nil() )
1044       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1045     // assign hypotheses
1046     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1047     {
1048       int aHypIndex = currentHyp( aDim, aHypType );
1049       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1050       {
1051         SMESH::SMESH_Hypothesis_var aHypVar =
1052           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1053         if ( !aHypVar->_is_nil() )
1054           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1055       }
1056     }
1057   }
1058   return true;
1059 }
1060
1061 //================================================================================
1062 /*!
1063  * \brief Gets current hypothesis or algorithms
1064   * \param theDim - dimension of hypothesis or algorithm
1065   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1066   * \retval int - current hypothesis or algorithms
1067  *
1068  * Gets current hypothesis or algorithms
1069  */
1070 //================================================================================
1071 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1072 {
1073   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1074 }
1075
1076 //================================================================================
1077 /*!
1078  * \brief Returns true if hypotheses of given dim can be assigned
1079   * \param theDim - hypotheses dimension
1080   * \retval bool - result
1081  */
1082 //================================================================================
1083 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim) const
1084 {
1085   return myDlg->tab( theDim )->isEnabled();
1086 }
1087
1088 //================================================================================
1089 /*!
1090  * \brief Sets current hypothesis or algorithms
1091   * \param theDim - dimension of hypothesis or algorithm
1092   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1093   * \param theIndex - Index of hypothesis
1094  *
1095  * Gets current hypothesis or algorithms
1096  */
1097 //================================================================================
1098 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1099                                      const int theHypType,
1100                                      const int theIndex )
1101 {
1102   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1103 }
1104
1105 //================================================================================
1106 /*!
1107  * \brief Generates default and sets mesh/submesh name
1108  *
1109  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1110  */
1111 //================================================================================
1112 void SMESHGUI_MeshOp::setDefaultName() const
1113 {
1114   QString aResName;
1115
1116   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1117   int i = 1;
1118   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1119   _PTR(SObject) anObj;
1120   do
1121   {
1122     aResName = aPrefix + QString::number( i++ );
1123     anObj = aStudy->FindObject( aResName.latin1() );
1124   }
1125   while ( anObj );
1126
1127   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1128     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1129   aControl->setText( aResName );
1130 }
1131
1132 //================================================================================
1133 /*!
1134  * \brief Gets algorithm or creates it if necessary
1135   * \param theDim - specifies dimension of returned hypotheses/algorifms
1136   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1137  *
1138  * Gets algorithm or creates it if necessary
1139  */
1140 //================================================================================
1141 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1142 {
1143   SMESH::SMESH_Hypothesis_var anAlgoVar;
1144   int aHypIndex = currentHyp( theDim, Algo );
1145   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true, theDim, false );
1146   if ( aHypIndex < 0 || aHypIndex >= aHypTypeNameList.count() )
1147     return anAlgoVar;
1148   QString aHypName = aHypTypeNameList[ aHypIndex ];
1149   QValueList<SMESH::SMESH_Hypothesis_var>& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1150   QValueList<SMESH::SMESH_Hypothesis_var>::iterator anIter;
1151   for ( anIter = aHypVarList.begin(); anIter != aHypVarList.end(); anIter++ )
1152   {
1153     SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1154     if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1155     {
1156       anAlgoVar = aHypVar;
1157       break;
1158     }
1159   }
1160   if ( anAlgoVar->_is_nil() )
1161   {
1162     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1163     if ( aHypData )
1164     {
1165       QString aClientLibName = aHypData->ClientLibName;
1166       if ( aClientLibName == "" )
1167         SMESH::CreateHypothesis( aHypName, aHypData->Label, true );
1168       else
1169       {
1170         SMESHGUI_GenericHypothesisCreator* aCreator =
1171           SMESH::GetHypothesisCreator( aHypName );
1172         if ( aCreator )
1173           aCreator->create( true, myDlg );
1174       }
1175       QStringList tmpList;
1176       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
1177       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
1178     }
1179
1180     QValueList<SMESH::SMESH_Hypothesis_var>& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
1181     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
1182     {
1183       SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1184       if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1185       {
1186         anAlgoVar = aHypVar;
1187         break;
1188       }
1189     }
1190   }
1191
1192   return anAlgoVar._retn();
1193 }
1194
1195 //================================================================================
1196 /*!
1197  * \brief Reads parameters of edited mesh and assigns them to the dialog
1198  *
1199  * Reads parameters of edited mesh and assigns them to the dialog (called when
1200  * mesh is edited only)
1201  */
1202 //================================================================================
1203 void SMESHGUI_MeshOp::readMesh()
1204 {
1205   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1206   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1207   if ( !pObj )
1208     return;
1209
1210   // Get name of mesh if current object is sub-mesh
1211   SMESH::SMESH_subMesh_var aSubMeshVar =
1212       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1213   if ( !aSubMeshVar->_is_nil() )
1214   {
1215     SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1216     if ( !aMeshVar->_is_nil() )
1217     {
1218       _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1219       QString aMeshName = name( aMeshSO );
1220       myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
1221     }
1222   }
1223
1224   // Get name of geometry object
1225   GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1226   if ( !aGeomVar->_is_nil() )
1227   {
1228     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
1229     QString aShapeName = name( aGeomSO );
1230     myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
1231   }
1232
1233   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
1234   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1235   {
1236     // get algorithm
1237     QStringList anExisting;
1238     int aHypIndex = -1;
1239     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
1240     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
1241     {
1242       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first();
1243       QString aHypTypeName = aVar->GetName();
1244
1245       QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true , dim, false );
1246       for ( int i = 0, n = aHypTypeNameList.count(); i < n; i++ )
1247         if ( aHypTypeName == aHypTypeNameList[ i ] )
1248         {
1249           aHypIndex = i;
1250           break;
1251         }
1252     }
1253     setCurrentHyp( dim, Algo, aHypIndex );
1254
1255     // get hypotheses
1256     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1257     {
1258       // get hypotheses
1259       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1260       // find index of requered hypothesis among existing ones for this dimension
1261       // and hyp types
1262       int aHypIndex = -1;
1263       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1264         aHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1265                           myExistingHyps[ dim ][ hypType ] );
1266       setCurrentHyp( dim, hypType, aHypIndex );
1267     }
1268   }
1269 }
1270
1271 //================================================================================
1272 /*!
1273  * \brief Gets name of object
1274   * \param theSO - SObject
1275   * \retval QString - name of object
1276  *
1277  * Gets name of object
1278  */
1279 //================================================================================
1280 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1281 {
1282   QString aResName;
1283   if ( theSO )
1284   {
1285     _PTR(GenericAttribute) anAttr;
1286     _PTR(AttributeName)    aNameAttr;
1287     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1288     {
1289       aNameAttr = anAttr;
1290       aResName = aNameAttr->Value().c_str();
1291     }
1292   }
1293   return aResName;
1294 }
1295
1296 //================================================================================
1297 /*!
1298  * \brief Finds hypothesis in input list
1299   * \param theHyp - hypothesis to be found
1300   * \param theHypList - input list of hypotheses
1301   * \retval int - index of hypothesis or -1 if it is not found
1302  *
1303  * Finds position of hypothesis in input list
1304  */
1305 //================================================================================
1306 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1307                            const QValueList<SMESH::SMESH_Hypothesis_var>& theHypList ) const
1308 {
1309   int aRes = -1;
1310   if ( !theHyp->_is_nil() )
1311   {
1312     int i = 0;
1313     QValueList<SMESH::SMESH_Hypothesis_var>::const_iterator anIter;
1314     for ( anIter = theHypList.begin(); anIter != theHypList.end(); ++ anIter )
1315     {
1316       if ( theHyp->_is_equivalent( *anIter ) )
1317       {
1318         aRes = i;
1319         break;
1320       }
1321       i++;
1322     }
1323   }
1324   return aRes;
1325 }
1326
1327 //================================================================================
1328 /*!
1329  * \brief Edits mesh or sub-mesh
1330   * \param theMess - Output parameter intended for returning error message
1331   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1332  *
1333  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1334  */
1335 //================================================================================
1336 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1337 {
1338   theMess = "";
1339
1340   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1341   if ( aSMESHGen->_is_nil() )
1342     return false;
1343
1344   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1345   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1346   if ( !pObj )
1347     return false;
1348
1349   SUIT_OverrideCursor aWaitCursor;
1350
1351   // Set new name
1352   SMESH::SetName( pObj, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1353
1354   // Assign new hypotheses and algorithms
1355   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1356   {
1357     if ( !isAccessibleDim( dim )) continue;
1358
1359     // find or create algorithm
1360     bool toDelete = false, toAdd = true;
1361     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1362     if ( anAlgoVar->_is_nil() ) {
1363       toAdd = false;
1364     }
1365     if ( myObjHyps[ dim ][ Algo ].count() > 0 ) {
1366       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first();
1367       if ( toAdd ) {
1368         if ( strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) == 0 ) {
1369           toAdd = false;
1370         } else {
1371           toDelete = true;
1372         }
1373       } else {
1374         toDelete = true;
1375       }
1376     }
1377     // remove old algorithm
1378     if ( toDelete )
1379       SMESH::RemoveHypothesisOrAlgorithmOnMesh
1380         ( pObj, myObjHyps[ dim ][ Algo ].first() );
1381
1382     // assign new algorithm
1383     if ( toAdd ) {
1384       SMESH::SMESH_Mesh_var aMeshVar =
1385         SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1386       bool isMesh = !aMeshVar->_is_nil();
1387       if ( isMesh ) {
1388         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1389       } else {
1390         SMESH::SMESH_subMesh_var aVar =
1391           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1392         if ( !aVar->_is_nil() )
1393           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1394       }
1395     }
1396
1397     // assign hypotheses
1398     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1399     {
1400       int aNewHypIndex = currentHyp( dim, hypType );
1401       int anOldHypIndex = -1;
1402       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1403         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1404                               myExistingHyps[ dim ][ hypType ] );
1405       if ( aNewHypIndex != anOldHypIndex )
1406       {
1407         // remove old hypotheses
1408         if ( anOldHypIndex >= 0 )
1409           SMESH::RemoveHypothesisOrAlgorithmOnMesh(
1410             pObj, myExistingHyps[ dim ][ hypType ][ anOldHypIndex ] );
1411
1412         // assign new hypotheses
1413         if ( aNewHypIndex != -1 )
1414         {
1415           SMESH::SMESH_Mesh_var aMeshVar =
1416               SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1417           bool isMesh = !aMeshVar->_is_nil();
1418           if ( isMesh )
1419           {
1420             SMESH::AddHypothesisOnMesh(
1421               aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1422           }
1423           else
1424           {
1425             SMESH::SMESH_subMesh_var aVar =
1426               SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1427             if ( !aVar->_is_nil() )
1428               SMESH::AddHypothesisOnSubMesh(
1429                 aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1430           }
1431         }
1432         // reread all hypotheses of mesh if necessary
1433         QStringList anExisting;
1434         existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1435       }
1436     }
1437   }
1438
1439   return true;
1440 }
1441
1442 //================================================================================
1443 /*!
1444  * \brief Verifies whether given operator is valid for this one
1445   * \param theOtherOp - other operation
1446   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1447 *
1448 * method redefined from base class verifies whether given operator is valid for
1449 * this one (i.e. can be started "above" this operator). In current implementation method
1450 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1451 * elements.
1452 */
1453 //================================================================================
1454 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1455 {
1456   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1457 }
1458
1459 //================================================================================
1460 /*!
1461  * \brief SLOT. Is called when the user selects a way of geometry selection
1462   * \param theByMesh - true if the user wants to find geometry by mesh element
1463  */
1464 //================================================================================
1465
1466 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
1467 {
1468   if ( theByMesh ) {
1469     if ( !myShapeByMeshDlg ) {
1470       myShapeByMeshDlg = new SMESHGUI_ShapeByMeshDlg( SMESHGUI::GetSMESHGUI(), "ShapeByMeshDlg");
1471       connect(myShapeByMeshDlg, SIGNAL(PublishShape()), SLOT(onPublishShapeByMeshDlg()));
1472       connect(myShapeByMeshDlg, SIGNAL(Close()), SLOT(onCloseShapeByMeshDlg()));
1473     }
1474     // set mesh object to dlg
1475     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1476     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
1477       SMESH::SMESH_Mesh_var aMeshVar =
1478         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1479       if ( !aMeshVar->_is_nil() ) {
1480         myDlg->hide();
1481         myDlg->activateObject( SMESHGUI_MeshDlg::Mesh );
1482         myShapeByMeshDlg->Init();
1483         myShapeByMeshDlg->SetMesh( aMeshVar );
1484         myShapeByMeshDlg->show();
1485       }
1486     }
1487   }
1488 }
1489
1490 //================================================================================
1491 /*!
1492  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1493  */
1494 //================================================================================
1495
1496 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg()
1497 {
1498   if ( myShapeByMeshDlg ) {
1499     // Select a found geometry object
1500     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshDlg->GetShape();
1501     if ( !aGeomVar->_is_nil() )
1502     {
1503       QString ID = aGeomVar->GetStudyEntry();
1504       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID )) {
1505         SMESH::SMESH_Mesh_ptr aMeshPtr = myShapeByMeshDlg->GetMesh();
1506         if ( !CORBA::is_nil( aMeshPtr )) {
1507           if (_PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshPtr )) {
1508             myDlg->activateObject( SMESHGUI_MeshDlg::Mesh );
1509             myDlg->selectObject( aMeshSO->GetName(), SMESHGUI_MeshDlg::Mesh, aMeshSO->GetID() );
1510           }
1511         }
1512         myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
1513         selectObject( aGeomSO );
1514         //selectionDone();
1515       }
1516     }
1517     else {
1518       onCloseShapeByMeshDlg();
1519     }
1520   }
1521   myDlg->show();
1522 }
1523
1524 //================================================================================
1525 /*!
1526  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
1527  */
1528 //================================================================================
1529
1530 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg()
1531 {
1532   if ( myDlg ) {
1533     myDlg->show();
1534     myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
1535     myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
1536   }
1537 }
1538
1539 //================================================================================
1540 /*!
1541  * \brief Selects a SObject
1542   * \param theSObj - the SObject to select
1543  */
1544 //================================================================================
1545
1546 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
1547 {
1548   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
1549     SALOME_ListIO anIOList;
1550     if ( theSObj ) {
1551       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1552         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
1553       anIOList.Append( anIO );
1554     }
1555     sm->setSelectedObjects( anIOList, false );
1556   }
1557 }