Salome HOME
PAL13473 (Build repetitive mesh):
[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/ or email : webmaster.salome@opencascade.com
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 "SalomeApp_Application.h"
53 #include "SALOMEDSClient_Study.hxx"
54 #include "SALOMEDSClient_AttributeIOR.hxx"
55 #include "SALOMEDSClient_AttributeName.hxx"
56 #include "SALOMEDS_SComponent.hxx"
57 #include "SALOMEDS_SObject.hxx"
58
59 #include "LightApp_SelectionMgr.h"
60 #include "LightApp_UpdateFlags.h"
61 #include "SUIT_MessageBox.h"
62 #include "SUIT_Desktop.h"
63 #include "SUIT_OverrideCursor.h"
64 #include "SALOME_InteractiveObject.hxx"
65 #include "SALOME_ListIO.hxx"
66
67 #include "utilities.h"
68
69 #include <qstringlist.h>
70 #include <qlineedit.h>
71
72 #include <TopoDS_Shape.hxx>
73 #include <TopExp_Explorer.hxx>
74
75 enum { GLOBAL_ALGO_TAG        =3,
76        GLOBAL_HYPO_TAG        =2,
77        LOCAL_ALGO_TAG         =2,
78        LOCAL_HYPO_TAG         =1,
79        SUBMESH_ON_EDGE_TAG    =5,
80        SUBMESH_ON_WIRE_TAG    =6,
81        SUBMESH_ON_FACE_TAG    =7,
82        SUBMESH_ON_SHELL_TAG   =8,
83        SUBMESH_ON_SOLID_TAG   =9,
84        SUBMESH_ON_COMPOUND_TAG=10 };
85
86 //================================================================================
87 /*!
88  * \brief Constructor
89   * \param theToCreate - if this parameter is true then operation is used for creation,
90   * for editing otherwise
91  *
92  * Initialize operation
93 */
94 //================================================================================
95 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
96 : SMESHGUI_SelectionOp(),
97   myToCreate( theToCreate ),
98   myIsMesh( theIsMesh ),
99   myDlg( 0 ),
100   myShapeByMeshOp( 0 )
101 {
102   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
103     GeometryGUI::InitGeomGen();
104 }
105
106 //================================================================================
107 /*!
108  * \brief Destructor
109 */
110 //================================================================================
111 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
112 {
113   if ( myDlg )
114     delete myDlg;
115 }
116
117 //================================================================================
118 /*!
119  * \brief Gets dialog of this operation
120   * \retval LightApp_Dialog* - pointer to dialog of this operation
121 */
122 //================================================================================
123 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
124 {
125   return myDlg;
126 }
127
128 //================================================================================
129 /*!
130  * \brief Creates or edits mesh
131   * \retval bool - TRUE if operation is performed successfully, FALSE otherwise
132  *
133  * Virtual slot redefined from the base class called when "Apply" button is clicked
134  * creates or edits mesh
135  */
136 //================================================================================
137 bool SMESHGUI_MeshOp::onApply()
138 {
139   if( isStudyLocked() )
140     return false;
141
142   QString aMess;
143   if ( !isValid( aMess ) )
144   {
145     dlg()->show();
146     if ( aMess != "" )
147       SUIT_MessageBox::warn1( myDlg,
148         tr( "SMESH_WRN_WARNING" ), aMess, tr( "SMESH_BUT_OK" ) );
149     return false;
150   }
151
152   bool aResult = false;
153   aMess = "";
154   try
155   {
156     if ( myToCreate && myIsMesh )
157       aResult = createMesh( aMess );
158     if ( myToCreate && !myIsMesh )
159       aResult = createSubMesh( aMess );
160     else if ( !myToCreate )
161       aResult = editMeshOrSubMesh( aMess );
162     if ( aResult )
163       update( UF_ObjBrowser | UF_Model );
164   }
165   catch ( const SALOME::SALOME_Exception& S_ex )
166   {
167     SalomeApp_Tools::QtCatchCorbaException( S_ex );
168     aResult = false;
169   }
170   catch ( ... )
171   {
172     aResult = false;
173   }
174
175   if ( aResult )
176   {
177     if ( myToCreate )
178       setDefaultName();
179   }
180   else
181   {
182     if ( aMess == "" )
183       aMess = tr( "SMESH_OPERATION_FAILED" );
184     SUIT_MessageBox::warn1( myDlg,
185       tr( "SMESH_ERROR" ), aMess, tr( "SMESH_BUT_OK" ) );
186   }
187
188   return aResult;
189 }
190
191 //================================================================================
192 /*!
193  * \brief Creates dialog if necessary and shows it
194  *
195  * Virtual method redefined from base class called when operation is started creates
196  * dialog if necessary and shows it, activates selection
197  */
198 //================================================================================
199 void SMESHGUI_MeshOp::startOperation()
200 {
201   if( !myDlg )
202   {
203     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
204     for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
205     {
206       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
207               this, SLOT( onCreateHyp( const int, const int ) ) );
208       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
209               this, SLOT( onEditHyp( const int, const int ) ) );
210       connect( myDlg->tab( i ), SIGNAL( selectAlgo( const int ) ),
211               this, SLOT( onAlgoSelected( const int ) ) );
212     }
213     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
214     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
215
216     if ( myToCreate )
217       if ( myIsMesh ) myHelpFileName = "/files/constructing_meshes.htm";
218       else myHelpFileName = "/files/constructing_submeshes.htm";
219     else myHelpFileName = "files/reassigning_hypotheses_and_algorithms.htm";
220   }
221   SMESHGUI_SelectionOp::startOperation();
222
223   // iterate through dimensions and get available algoritms,
224   // set them to the dialog
225   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
226   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
227   {
228     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
229     QStringList hypList;
230     // clear available hypotheses
231     aTab->setAvailableHyps( MainHyp, hypList );
232     aTab->setAvailableHyps( AddHyp, hypList );
233     aTab->setExistingHyps( MainHyp, hypList );
234     aTab->setExistingHyps( AddHyp, hypList );
235     myExistingHyps[ i ][ MainHyp ].clear();
236     myExistingHyps[ i ][ AddHyp ].clear();
237     // set algos
238     availableHyps( i, Algo, hypList, myAvailableHypData[i][Algo] );
239     aTab->setAvailableHyps( Algo, hypList );
240   }
241   if ( myToCreate )
242   {
243     setDefaultName();
244     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
245   }
246   else
247     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
248
249   myDlg->setHypoSets( SMESH::GetHypothesesSets() );
250
251   myDlg->setCurrentTab( SMESH::DIM_3D );
252   myDlg->show();
253
254   selectionDone();
255
256   myIgnoreAlgoSelection = false;
257 }
258
259 //================================================================================
260 /*!
261  * \brief Creates selection filter
262   * \param theId - identifier of current selection widget
263   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
264  *
265  * Creates selection filter in accordance with identifier of current selection widget
266  */
267 //================================================================================
268 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
269 {
270   if ( theId == SMESHGUI_MeshDlg::Geom )
271   {
272 //     TColStd_MapOfInteger allTypesMap;
273 //     for ( int i = 0; i < 10; i++ )
274 //       allTypesMap.Add( i );
275 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
276     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
277   }
278   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
279     return new SMESH_TypeFilter( MESHorSUBMESH );
280   else if ( theId == SMESHGUI_MeshDlg::Mesh )
281     return new SMESH_TypeFilter( MESH );
282   else
283     return 0;
284 }
285
286 //================================================================================
287 /*!
288  * \brief check if selected shape is a subshape of the shape to mesh
289   * \retval bool - check result
290  */
291 //================================================================================
292
293 bool SMESHGUI_MeshOp::isSubshapeOk() const
294 {
295   if ( !myToCreate || myIsMesh ) // not submesh creation
296     return false;
297
298   // mesh
299   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
300   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
301   if (!pMesh) return false;
302
303   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
304   if (mesh->_is_nil()) return false;
305
306   // main shape of the mesh
307   GEOM::GEOM_Object_var mainGeom = mesh->GetShapeToMesh();
308   if (mainGeom->_is_nil()) return false;
309
310   // geometry
311   QStringList aGEOMs;
312   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
313
314   if (aGEOMs.count() > 0) {
315     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
316     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
317     if (geomGen->_is_nil() || !aStudy) return false;
318
319     GEOM::GEOM_IGroupOperations_var op =
320         geomGen->GetIGroupOperations(aStudy->StudyId());
321     if (op->_is_nil()) return false;
322
323     // check all selected shapes
324     QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
325     for (; aSubShapesIter != aGEOMs.end(); aSubShapesIter++) {
326       QString aSubGeomEntry = (*aSubShapesIter);
327       _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.latin1());
328       if (!pSubGeom) return false;
329
330       GEOM::GEOM_Object_var aSubGeomVar =
331         GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
332       if (aSubGeomVar->_is_nil()) return false;
333
334       GEOM::GEOM_Object_var mainObj = op->GetMainShape(aSubGeomVar);
335       if (mainObj->_is_nil() ||
336           string(mainObj->GetEntry()) != string(mainGeom->GetEntry())) return false;
337     }
338     return true;
339   }
340
341   return false;
342 }
343
344 //================================================================================
345 /*!
346  * \brief find an existing submesh by the selected shape
347   * \retval _PTR(SObject) - the found submesh SObject
348  */
349 //================================================================================
350
351 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
352 {
353   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
354   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
355   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
356   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
357   if ( pMesh && pGeom ) {
358     GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
359     if ( !geom->_is_nil() ) {
360       int tag = -1;
361       switch ( geom->GetShapeType() ) {
362       case GEOM::EDGE:     tag = SUBMESH_ON_EDGE_TAG    ; break;
363       case GEOM::WIRE:     tag = SUBMESH_ON_WIRE_TAG    ; break;
364       case GEOM::FACE:     tag = SUBMESH_ON_FACE_TAG    ; break;
365       case GEOM::SHELL:    tag = SUBMESH_ON_SHELL_TAG   ; break;
366       case GEOM::SOLID:    tag = SUBMESH_ON_SOLID_TAG   ; break;
367       case GEOM::COMPOUND: tag = SUBMESH_ON_COMPOUND_TAG; break;
368       default:;
369       }
370       _PTR(GenericAttribute) anAttr;
371       _PTR(SObject) aSubmeshRoot;
372       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
373       if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
374       {
375         _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
376         for (; smIter->More(); smIter->Next() )
377         {
378           _PTR(SObject) aSmObj = smIter->Value();
379           if ( ! aSmObj->FindAttribute( anAttr, "AttributeIOR" ))
380             continue;
381           _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
382           for (; anIter1->More(); anIter1->Next()) {
383             _PTR(SObject) pGeom2 = anIter1->Value();
384             if ( pGeom2->ReferencedObject( pGeom2 ) &&
385                  pGeom2->GetID() == pGeom->GetID() )
386               return aSmObj;
387           }
388         }
389       }
390     }
391   }
392   return _PTR(SObject)();
393 }
394
395 //================================================================================
396 /*!
397  * \brief Updates dialog's look and feel
398  *
399  * Virtual method redefined from the base class updates dialog's look and feel
400  */
401 //================================================================================
402 void SMESHGUI_MeshOp::selectionDone()
403 {
404   if ( !dlg()->isShown() )
405     return;
406
407   SMESHGUI_SelectionOp::selectionDone();
408
409   try
410   {
411     // Enable tabs according to shape dimension
412
413     int shapeDim = 3;
414
415     QStringList aGEOMs;
416     myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
417     GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
418
419     if (aGEOMs.count() > 0) {
420       // one or more GEOM shape selected
421       aSeq->length(aGEOMs.count());
422       QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
423       int iSubSh = 0;
424       for (; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
425         QString aSubGeomEntry = (*aSubShapesIter);
426         _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.latin1());
427         GEOM::GEOM_Object_var aSubGeomVar =
428           GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
429         aSeq[iSubSh] = aSubGeomVar;
430       }
431     } else {
432       // get geometry by selected sub-mesh
433       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
434       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
435       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
436       if (!aGeomVar->_is_nil()) {
437         aSeq->length(1);
438         aSeq[0] = aGeomVar;
439       }
440     }
441
442     if (aSeq->length() > 0) {
443       shapeDim = 0;
444       for (int iss = 0; iss < aSeq->length() && shapeDim < 3; iss++) {
445         GEOM::GEOM_Object_var aGeomVar = aSeq[iss];
446         switch ( aGeomVar->GetShapeType() ) {
447         case GEOM::SOLID:
448         case GEOM::SHELL:  shapeDim = 3; break;
449         case GEOM::FACE:   shapeDim = (shapeDim < 2) ? 2 : shapeDim; break;
450         case GEOM::WIRE:
451         case GEOM::EDGE:   shapeDim = (shapeDim < 1) ? 1 : shapeDim; break;
452         case GEOM::VERTEX: break;
453         default:
454           TopoDS_Shape aShape;
455           if ( GEOMBase::GetShape(aGeomVar, aShape)) {
456             TopExp_Explorer exp( aShape, TopAbs_SHELL );
457             if ( exp.More() )
458               shapeDim = 3;
459             else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
460               shapeDim = (shapeDim < 2) ? 2 : shapeDim;
461             else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
462               shapeDim = (shapeDim < 1) ? 1 : shapeDim;
463             else
464               ;//shapeDim = 0;
465           }
466         }
467       }
468     }
469     myDlg->setMaxHypoDim( shapeDim );
470
471
472     if ( !myToCreate ) // edition: read hypotheses
473     {
474       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
475       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
476       if ( pObj != 0 )
477       {
478         SMESH::SMESH_subMesh_var aVar =
479           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
480         myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
481         myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
482         myDlg->updateGeometry();
483         myDlg->adjustSize();
484         readMesh();
485       }
486       else
487         myDlg->reset();
488
489     }
490     else if ( !myIsMesh ) // submesh creation
491     {
492       // if a submesh on the selected shape already exist, pass to submesh edition mode
493       if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
494         SMESH::SMESH_subMesh_var sm =
495           SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
496         bool editSubmesh = ( !sm->_is_nil() &&
497                              SUIT_MessageBox::question2( myDlg, tr( "SMESH_WARNING" ),
498                                                          tr( "EDIT_SUBMESH_QUESTION"),
499                                                          tr( "SMESH_BUT_YES" ),
500                                                          tr( "SMESH_BUT_NO" ), 1, 0, 0 ));
501         if ( editSubmesh )
502         {
503           selectionMgr()->clearFilters();
504           selectObject( pSubmesh );
505           SMESHGUI::GetSMESHGUI()->switchToOperation(704);
506           return;
507         }
508         else
509         {
510           myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
511           selectObject( _PTR(SObject)() );
512           selectionDone();
513         }
514       }
515
516       // enable/disable popup for choice of geom selection way
517       bool enable = false;
518       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
519       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
520         SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
521         if ( !mesh->_is_nil() )
522           enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
523       }
524       myDlg->setGeomPopupEnabled( enable );
525     }
526   }
527   catch ( const SALOME::SALOME_Exception& S_ex )
528   {
529     SalomeApp_Tools::QtCatchCorbaException( S_ex );
530   }
531   catch ( ... )
532   {
533   }
534 }
535
536 //================================================================================
537 /*!
538  * \brief Verifies validity of input data
539   * \param theMess - Output parameter intended for returning error message
540   * \retval bool  - TRUE if input data is valid, false otherwise
541  *
542  * Verifies validity of input data. This method is called when "Apply" or "OK" button
543  * is pressed before mesh creation or editing.
544  */
545 //================================================================================
546 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
547 {
548   // Selected object to be  edited
549   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
550   {
551     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
552     return false;
553   }
554
555   // Name
556   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
557   aMeshName = aMeshName.stripWhiteSpace();
558   if ( aMeshName == "" )
559   {
560     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
561     return false;
562   }
563
564   // Imported mesh, if create sub-mesh or edit mesh
565   if ( !myToCreate || ( myToCreate && !myIsMesh ))
566   {
567     QString aMeshEntry = myDlg->selectedObject
568       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
569     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
570       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
571       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
572         theMess = tr( "IMPORTED_MESH" );
573         return false;
574       }
575     }
576   }
577
578   // Geom
579   if ( myToCreate )
580   {
581     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
582     if ( aGeomEntry == "" )
583     {
584       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
585       return false;
586     }
587     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
588     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
589     {
590       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
591       return false;
592     }
593
594     // Mesh
595     if ( !myIsMesh ) // i.e sub-mesh creation,
596     {
597       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
598       if ( aMeshEntry == "" )
599       {
600         theMess = tr( "MESH_IS_NOT_DEFINED" );
601         return false;
602       }
603       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
604       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
605       {
606         theMess = tr( "MESH_IS_NULL" );
607         return false;
608       }
609       if ( !isSubshapeOk() )
610       {
611         theMess = tr( "INVALID_SUBSHAPE" );
612         return false;
613       }
614     }
615   }
616
617   return true;
618 }
619
620 //================================================================================
621 /*!
622  * \brief check compatibility of the algorithm and another algorithm or hypothesis
623   * \param theAlgoData - algorithm data
624   * \param theHypData - hypothesis data
625   * \param theHypType - hypothesis type
626   * \param theHypTypeName - hypothesis type name, must be provided if 2-nd arg is not algo
627   * \retval bool - check result
628  */
629 //================================================================================
630
631 static bool isCompatible(const HypothesisData* theAlgoData,
632                          const HypothesisData* theHypData,
633                          const int             theHypType)
634 {
635   if ( !theAlgoData )
636     return true;
637
638   if ( theHypType == SMESHGUI_MeshOp::Algo )
639     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
640
641   bool isOptional;
642   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
643 }
644
645 //================================================================================
646 /*!
647  * \brief Gets available hypotheses or algorithms
648   * \param theDim - specifies dimension of returned hypotheses/algorifms
649   * \param theHypType - specifies whether algorims or hypotheses or additional ones
650   * are retrieved (possible values are in HypType enumeration)
651   * \param theHyps - Output list of hypotheses' names
652   * \param theAlgoData - to select hypos able to be used by this algo (optional)
653  *
654  * Gets available hypotheses or algorithm in accordance with input parameters
655  */
656 //================================================================================
657 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
658                                      const int       theHypType,
659                                      QStringList&    theHyps,
660                                      THypDataList&   theDataList,
661                                      HypothesisData* theAlgoData ) const
662 {
663   theDataList.clear();
664   theHyps.clear();
665   bool isAlgo = ( theHypType == Algo );
666   bool isAux  = ( theHypType == AddHyp );
667   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux );
668
669   QStringList::const_iterator anIter;
670   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
671   {
672     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
673     if ( isCompatible ( theAlgoData, aData, theHypType )) {
674       theDataList.append( aData );
675       theHyps.append( aData->Label );
676     }
677   }
678 }
679
680 //================================================================================
681 /*!
682  * \brief Gets existing hypotheses or algorithms
683   * \param theDim - specifies dimension of returned hypotheses/algorifms
684   * \param theHypType - specifies whether algorims or hypotheses or additional ones
685   * are retrieved (possible values are in HypType enumeration)
686   * \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
687   * \param theDataList - output list of hypotheses data
688   * \param theHyps - output list of names.
689   * \param theHypVars - output list of variables.
690   * \param theAlgoData - to select hypos able to be used by this algo (optional)
691  *
692  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
693  * input parameters
694  */
695 //================================================================================
696 void SMESHGUI_MeshOp::existingHyps( const int theDim,
697                                     const int theHypType,
698                                     _PTR(SObject) theFather,
699                                     QStringList& theHyps,
700                                     QValueList<SMESH::SMESH_Hypothesis_var>& theHypVars,
701                                     HypothesisData* theAlgoData)
702 {
703   // Clear hypoheses list
704   theHyps.clear();
705   theHypVars.clear();
706
707   if ( !theFather )
708     return;
709
710   const bool isAux  = ( theHypType == AddHyp );
711
712   _PTR(SObject)          aHypRoot;
713   _PTR(GenericAttribute) anAttr;
714   _PTR(AttributeName)    aName;
715   _PTR(AttributeIOR)     anIOR;
716
717   bool isMesh = !_CAST( SComponent, theFather );
718   int aPart = -1;
719   if ( isMesh )
720     aPart = theHypType == Algo ? GLOBAL_ALGO_TAG : GLOBAL_HYPO_TAG;
721   else
722     aPart = theHypType == Algo ? LOCAL_ALGO_TAG : LOCAL_HYPO_TAG;
723
724   if ( theFather->FindSubObject( aPart, aHypRoot ) )
725   {
726     _PTR(ChildIterator) anIter =
727       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
728     for (; anIter->More(); anIter->Next() )
729     {
730       _PTR(SObject) anObj = anIter->Value();
731       if ( isMesh ) // i.e. mesh or submesh
732       {
733         _PTR(SObject) aRefObj;
734         if ( anObj->ReferencedObject( aRefObj ) )
735           anObj = aRefObj;
736         else
737           continue;
738       }
739       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
740       {
741         aName = anAttr;
742         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
743         if ( !CORBA::is_nil( aVar ) )
744         {
745           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
746           if ( !aHypVar->_is_nil() )
747           {
748             QString aHypType( aHypVar->GetName() );
749             HypothesisData* aData = SMESH::GetHypothesisData( aHypType );
750             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
751                  ( isCompatible ( theAlgoData, aData, theHypType )) &&
752                  ( isAux == aData->IsAux ))
753             {
754               //theDataList.append( aData );
755               theHyps.append( aName->Value().c_str() );
756               theHypVars.append( aHypVar );
757             }
758           }
759         }
760       }
761     }
762   }
763 }
764
765 //================================================================================
766 /*!
767  * \brief If create or edit a submesh, return a hypothesis holding parameters used
768  *        to mesh a subshape
769   * \param aHypType - The hypothesis type name
770   * \param aServerLib - Server library name
771   * \param hypData - The structure holding the hypothesis type etc.
772   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
773  */
774 //================================================================================
775
776 SMESH::SMESH_Hypothesis_var
777 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
778                                           const QString& aServerLib ) const
779 {
780   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
781     return SMESH::SMESH_Hypothesis::_nil();
782
783   const int nbColonsInMeshEntry = 3;
784   bool isSubMesh = myToCreate ?
785     !myIsMesh :
786     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).contains(':') > nbColonsInMeshEntry;
787
788   if ( isSubMesh )
789   {
790     // get mesh and geom object
791     SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
792     GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
793
794     QString anEntry = myDlg->selectedObject
795       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
796     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.latin1() ))
797     {
798       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
799       if ( myToCreate ) // mesh and geom may be selected
800       {
801         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
802         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
803         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.latin1() ))
804           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
805       }
806       else // edition: sub-mesh may be selected
807       {
808         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
809         if ( !sm->_is_nil() ) {
810           aMeshVar = sm->GetFather();
811           aGeomVar = sm->GetSubShape();
812         }
813       }
814     }
815
816     if ( !aMeshVar->_is_nil() && !aGeomVar->_is_nil() )
817       return SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType,
818                                                                     aServerLib,
819                                                                     aMeshVar,
820                                                                     aGeomVar );
821   }
822   return SMESH::SMESH_Hypothesis::_nil();
823 }
824
825 //================================================================================
826 /*!
827  * \Brief Returns tab dimention
828   * \param tab - the tab in the dlg
829   * \param dlg - my dialogue
830   * \retval int - dimention
831  */
832 //================================================================================
833
834 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
835 {
836   int aDim = -1;
837   for (int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++)
838     if (tab == dlg->tab(i))
839       aDim = i;
840   return aDim;
841 }
842
843 //================================================================================
844 /*!
845  * \brief Create hypothesis
846   * \param theHypType - hypothesis category (main or additional)
847   * \param theIndex - index of type of hypothesis to be cerated
848  *
849  * Specifies dimension of hypothesis to be created (using sender() method),
850  * specifies its type and calls method for hypothesis creation
851  */
852 //================================================================================
853 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
854 {
855   // Specifies dimension of hypothesis to be created
856   int aDim = getTabDim( sender(), myDlg );
857   if (aDim == -1)
858     return;
859
860   // Specifies type of hypothesis to be created
861   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
862   if (theIndex < 0 || theIndex >= dataList.count())
863     return;
864   QString aHypTypeName = dataList[ theIndex ]->TypeName;
865
866   // Create hypothesis
867   createHypothesis(aDim, theHypType, aHypTypeName);
868 }
869
870 //================================================================================
871 /*!
872  *  Create hypothesis and update dialog.
873  *  \param theDim - dimension of hypothesis to be created
874  *  \param theType - hypothesis category (algorithm, hypothesis, additional hypothesis)
875  *  \param theTypeName - specifies hypothesis to be created
876  */
877 //================================================================================
878 void SMESHGUI_MeshOp::createHypothesis (const int theDim,
879                                         const int theType,
880                                         const QString& theTypeName)
881 {
882   // During a hypothesis creation we might need to select some objects.
883   // Main dialog must not update it's own selected objects in this case.
884   dlg()->deactivateAll();
885
886   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName.latin1());
887   if (!aData)
888     return;
889
890   // existing hypos
891   int nbHyp = myExistingHyps[theDim][theType].count();
892
893   QString aClientLibName = aData->ClientLibName;
894   if (aClientLibName == "") {
895     // Call hypothesis creation server method (without GUI)
896     SMESH::CreateHypothesis(theTypeName, aData->Label, false);
897   } else {
898     // Get hypotheses creator client (GUI)
899     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
900
901     // Create hypothesis
902     if (aCreator) {
903       // When create or edit a submesh, try to initialize a new hypothesis
904       // with values used to mesh a subshape
905       SMESH::SMESH_Hypothesis_var initParamHyp =
906         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
907       aCreator->create(initParamHyp, myDlg);
908     } else {
909       SMESH::CreateHypothesis(theTypeName, aData->Label, false);
910     }
911   }
912
913   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
914
915   HypothesisData* algoData = hypData( theDim, Algo, currentHyp( theDim, Algo ));
916   QStringList aNewHyps;
917   existingHyps(theDim, theType, aFather, aNewHyps, myExistingHyps[theDim][theType], algoData);
918   if (aNewHyps.count() > nbHyp) {
919     for (int i = nbHyp; i < aNewHyps.count(); i++)
920       myDlg->tab(theDim)->addHyp(theType, aNewHyps[i]);
921   }
922 }
923
924 //================================================================================
925 /*!
926  * \brief Calls plugin methods for hypothesis editing
927   * \param theHypType - specifies whether main hypothesis or additional one
928   * is edited
929   * \param theIndex - index of existing hypothesis
930  *
931  * Calls plugin methods for hypothesis editing
932  */
933 //================================================================================
934 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
935 {
936   // Speicfies dimension of hypothesis to be created
937   int aDim = getTabDim( sender(), myDlg );
938   if (aDim == -1)
939     return;
940
941   QValueList<SMESH::SMESH_Hypothesis_var> aList = myExistingHyps[ aDim ][ theHypType ];
942   if ( theIndex < 0 || theIndex >= aList.count() )
943     return;
944   SMESH::SMESH_Hypothesis_var aHyp = aList[ theIndex ];
945   if ( aHyp->_is_nil() )
946     return;
947
948   char* aTypeName = aHyp->GetName();
949   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aTypeName );
950   if ( aCreator )
951     aCreator->edit( aHyp.in(), dlg() );
952 }
953
954 //================================================================================
955 /*!
956  * \brief access to hypothesis data
957   * \param theDim - hyp dimension
958   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
959   * \param theIndex - index in the list
960   * \retval HypothesisData* - result data, may be 0
961  */
962 //================================================================================
963
964 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
965                                           const int theHypType,
966                                           const int theIndex)
967 {
968   if ( theDim     > -1 && theDim     < 3 &&
969        theHypType > -1 && theHypType < NbHypTypes &&
970        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
971     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
972   return 0;
973 }
974
975 //================================================================================
976 /*!
977  * \brief Set available algos and hypos according to the selected algorithm
978   * \param theIndex - algorithm index
979  */
980 //================================================================================
981
982 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
983                                       const int theDim )
984 {
985   if ( myIgnoreAlgoSelection )
986     return;
987
988   int aDim = theDim < 0 ? getTabDim( sender(), myDlg ): theDim;
989   if (aDim == -1)
990     return;
991
992   // find highest available dimension, all algos of this dimension are available for choice
993   int aTopDim = -1;
994   for (int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++)
995     if (isAccessibleDim( i ))
996       aTopDim = i;
997   if (aTopDim == -1)
998     return;
999
1000   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1001
1002   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1003   HypothesisData* algoByDim[3];
1004   algoByDim[ aDim ] = algoData;
1005
1006   QStringList anAvailable;
1007   if ( !algoData ) { // all algos becomes available
1008     availableHyps( aDim, Algo, anAvailable, myAvailableHypData[ aDim ][ Algo ]);
1009     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1010   }
1011
1012   // check that algorithms of other dimentions are compatible with
1013   // the selected one
1014
1015    // 2 loops: backward and forward from algo dimension
1016   for ( int forward = false; forward <= true; ++forward )
1017   {
1018     int dim = aDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
1019     if ( !forward ) {
1020       dim = aDim - 1; lastDim = SMESH::DIM_1D; dir = -1;
1021     }
1022     HypothesisData* prevAlgo = algoData;
1023     bool noCompatible = false;
1024     for ( ; dim * dir <= lastDim * dir ; dim += dir )
1025     {
1026       if ( !isAccessibleDim( dim ))
1027         continue;
1028       if ( noCompatible ) { // the selected algo has no compatible ones
1029         anAvailable.clear();
1030         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1031         myAvailableHypData[dim][Algo].clear();
1032         algoByDim[ dim ] = 0;
1033         continue;
1034       }
1035       // get currently selected algo
1036       int algoIndex = currentHyp( dim, Algo );
1037       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
1038       if ( curAlgo ) { // some algo selected
1039         if ( !isCompatible( prevAlgo, curAlgo, Algo ))
1040           curAlgo = 0;
1041       }
1042       // set new available algoritms
1043       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
1044       HypothesisData* soleCompatible = 0;
1045       if ( anAvailable.count() == 1 )
1046         soleCompatible = myAvailableHypData[dim][Algo][0];
1047       if ( dim == aTopDim && prevAlgo ) // all available algoritms should be selectable any way
1048         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], 0 );
1049       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1050       noCompatible = anAvailable.isEmpty();
1051
1052       // restore previously selected algo
1053       algoIndex = myAvailableHypData[dim][Algo].findIndex( curAlgo );
1054       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward )
1055         // select the sole compatible algo
1056         algoIndex = myAvailableHypData[dim][Algo].findIndex( soleCompatible );
1057       setCurrentHyp( dim, Algo, algoIndex );
1058
1059       // remember current algo
1060       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1061     }
1062   }
1063
1064   // set hypotheses corresponding to the found algoritms
1065
1066   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1067
1068   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1069   {
1070     if ( !isAccessibleDim( dim ))
1071       continue;
1072     for ( int type = MainHyp; type < NbHypTypes; type++ )
1073     {
1074       myAvailableHypData[ dim ][ type ].clear();
1075       QStringList anAvailable, anExisting;
1076
1077       HypothesisData* curAlgo = algoByDim[ dim ];
1078       int hypIndex = currentHyp( dim, type );
1079
1080       SMESH::SMESH_Hypothesis_var curHyp;
1081       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1082         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ];
1083
1084       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
1085         // try to find algo by selected hypothesis in order to keep it selected
1086         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1087         QString curHypType = curHyp->GetName();
1088         if ( !algoDeselectedByUser &&
1089              myObjHyps[ dim ][ type ].count() > 0 &&
1090              curHypType == myObjHyps[ dim ][ type ][ 0 ]->GetName())
1091         {
1092           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1093           for ( int i = 0 ; i < myAvailableHypData[ dim ][ Algo ].count(); ++i ) {
1094             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1095             if ( curAlgo && hypData && isCompatible( curAlgo, hypData, type ))
1096               break;
1097             else
1098               curAlgo = 0;
1099           }
1100         }
1101       }
1102       // get hyps compatible with curAlgo
1103       if ( curAlgo )
1104       {
1105         // check if a selected hyp is compatible with the curAlgo
1106         if ( !curHyp->_is_nil() ) {
1107           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1108           if ( !isCompatible( curAlgo, hypData, type ))
1109             curHyp = SMESH::SMESH_Hypothesis::_nil();
1110         }
1111         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
1112         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
1113       }
1114       // set list of hypotheses
1115       myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1116       myDlg->tab( dim )->setExistingHyps( type, anExisting );
1117
1118       // set current existing hypothesis
1119       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1120         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1121       else
1122         hypIndex = -1;
1123       if ( !isSubmesh && hypIndex < 0 && anExisting.count() == 1 ) {
1124         // none is yet selected => select the sole existing if it is not optional
1125         QString hypTypeName = myExistingHyps[ dim ][ type ][ 0 ]->GetName();
1126         bool isOptional = true;
1127         if ( algoByDim[ dim ] &&
1128              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName, isOptional ) &&
1129              !isOptional )
1130           hypIndex = 0;
1131       }
1132       setCurrentHyp( dim, type, hypIndex );
1133     }
1134   }
1135 }
1136
1137 //================================================================================
1138 /*!
1139  * \brief Creates and selects hypothesis of hypotheses set
1140  * \param theSetName - The name of hypotheses set
1141  */
1142 //================================================================================
1143 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1144 {
1145   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet(theSetName);
1146   if (!aHypoSet) return;
1147
1148   // clear all hyps
1149   for (int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++) {
1150     setCurrentHyp(dim, Algo, -1);
1151     setCurrentHyp(dim, AddHyp, -1);
1152     setCurrentHyp(dim, MainHyp, -1);
1153   }
1154
1155   for (int aHypType = Algo; aHypType < AddHyp; aHypType++) {
1156     bool isAlgo = (aHypType == Algo);
1157
1158     // set hyps from the set
1159     QStringList* aHypoList = isAlgo ? &aHypoSet->AlgoList : &aHypoSet->HypoList;
1160     for (int i = 0, n = aHypoList->count(); i < n; i++) {
1161       const QString& aHypoTypeName = (*aHypoList)[ i ];
1162       HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
1163       if (!aHypData)
1164         continue;
1165
1166       int aDim = aHypData->Dim[0];
1167       // create or/and set
1168       if (isAlgo) {
1169         int index = myAvailableHypData[aDim][Algo].findIndex( aHypData );
1170         if ( index < 0 ) {
1171           QStringList anAvailable;
1172           availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
1173           myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1174           index = myAvailableHypData[aDim][Algo].findIndex( aHypData );
1175         }
1176         setCurrentHyp( aDim, Algo, index );
1177         onAlgoSelected( index, aDim );
1178       }
1179       else {
1180         bool mainHyp = true;
1181         int index = myAvailableHypData[aDim][MainHyp].findIndex( aHypData );
1182         if ( index < 0 ) {
1183           mainHyp = false;
1184           index = myAvailableHypData[aDim][AddHyp].findIndex( aHypData );
1185         }
1186         if (index >= 0)
1187           createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
1188       }
1189     } // loop on hypos in the set
1190   } // loop on algo/hypo
1191 }
1192
1193 //================================================================================
1194 /*!
1195  * \brief Creates mesh
1196   * \param theMess - Output parameter intended for returning error message
1197   * \retval bool  - TRUE if mesh is created, FALSE otherwise
1198  *
1199  * Creates mesh
1200  */
1201 //================================================================================
1202 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
1203 {
1204   theMess = "";
1205
1206   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1207   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1208
1209   QStringList aList;
1210   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1211   QStringList::Iterator it = aList.begin();
1212   for(; it!=aList.end(); it++) {
1213
1214     QString aGeomEntry = *it;
1215     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1216     GEOM::GEOM_Object_var aGeomVar =
1217       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1218
1219     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1220     if ( aSMESHGen->_is_nil() )
1221       return false;
1222
1223     SUIT_OverrideCursor aWaitCursor;
1224
1225     // create mesh
1226     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
1227     if ( aMeshVar->_is_nil() )
1228       return false;
1229     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1230     if ( aMeshSO )
1231       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1232
1233     for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ ) {
1234       if ( !isAccessibleDim( aDim )) continue;
1235
1236       // assign hypotheses
1237       for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ ) {
1238         int aHypIndex = currentHyp( aDim, aHypType );
1239         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() ) {
1240           SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1241           if ( !aHypVar->_is_nil() )
1242             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
1243         }
1244       }
1245       // find or create algorithm
1246       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1247       if ( !anAlgoVar->_is_nil() )
1248         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1249     }
1250
1251   }
1252   return true;
1253 }
1254
1255 //================================================================================
1256 /*!
1257  * \brief Creates sub-mesh
1258   * \param theMess - Output parameter intended for returning error message
1259   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1260  *
1261  * Creates sub-mesh
1262  */
1263 //================================================================================
1264 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
1265 {
1266   theMess = "";
1267
1268   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1269   if ( aSMESHGen->_is_nil() )
1270     return false;
1271
1272   // get mesh object
1273   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1274   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
1275   SMESH::SMESH_Mesh_var aMeshVar =
1276     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1277   if (aMeshVar->_is_nil())
1278     return false;
1279
1280   // GEOM shape of the main mesh
1281   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
1282
1283   // Name for the new sub-mesh
1284   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
1285
1286   // get geom object
1287   GEOM::GEOM_Object_var aGeomVar;
1288   QStringList aGEOMs;
1289   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
1290   if (aGEOMs.count() == 1)
1291   {
1292     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1293     QString aGeomEntry = aGEOMs.first();
1294     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1295     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1296   }
1297   else if (aGEOMs.count() > 1)
1298   {
1299     // create a GEOM group
1300     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1301     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1302     if (!geomGen->_is_nil() && aStudy) {
1303       GEOM::GEOM_IGroupOperations_var op =
1304         geomGen->GetIGroupOperations(aStudy->StudyId());
1305       if (!op->_is_nil()) {
1306         // check and add all selected GEOM objects: they must be
1307         // a sub-shapes of the main GEOM and must be of one type
1308         int iSubSh = 0;
1309         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1310         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
1311         aSeq->length(aGEOMs.count());
1312         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
1313         for (; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
1314           QString aSubGeomEntry = (*aSubShapesIter);
1315           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.latin1());
1316           GEOM::GEOM_Object_var aSubGeomVar =
1317             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
1318           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
1319           if (iSubSh == 0) {
1320             aGroupType = aSubShapeType;
1321           } else {
1322             if (aSubShapeType != aGroupType)
1323               aGroupType = TopAbs_SHAPE;
1324           }
1325           aSeq[iSubSh] = aSubGeomVar;
1326         }
1327         // create a group
1328         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(mainGeom, aGroupType);
1329         op->UnionList(aGroupVar, aSeq);
1330
1331         if (op->IsDone()) {
1332           aGeomVar = aGroupVar;
1333
1334           // publish the GEOM group in study
1335           QString aNewGeomGroupName ("Auto_group_for_");
1336           aNewGeomGroupName += aName;
1337           SALOMEDS::SObject_var aNewGroupSO =
1338             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGeomVar, aNewGeomGroupName, mainGeom);
1339         }
1340       }
1341     }
1342   }
1343   else {
1344   }
1345   if (aGeomVar->_is_nil())
1346     return false;
1347
1348   SUIT_OverrideCursor aWaitCursor;
1349
1350   // create sub-mesh
1351   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.latin1() );
1352
1353   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
1354   {
1355     if ( !isAccessibleDim( aDim )) continue;
1356
1357     // find or create algorithm
1358     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1359     if ( !anAlgoVar->_is_nil() )
1360       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1361     // assign hypotheses
1362     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1363     {
1364       int aHypIndex = currentHyp( aDim, aHypType );
1365       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1366       {
1367         SMESH::SMESH_Hypothesis_var aHypVar =
1368           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1369         if ( !aHypVar->_is_nil() )
1370           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1371       }
1372     }
1373   }
1374
1375   // deselect geometry: next submesh sould be created on other subshape
1376   myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
1377   selectObject( _PTR(SObject)() );
1378   selectionDone();
1379
1380   return true;
1381 }
1382
1383 //================================================================================
1384 /*!
1385  * \brief Gets current hypothesis or algorithms
1386   * \param theDim - dimension of hypothesis or algorithm
1387   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1388   * \retval int - current hypothesis or algorithms
1389  *
1390  * Gets current hypothesis or algorithms
1391  */
1392 //================================================================================
1393 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1394 {
1395   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1396 }
1397
1398 //================================================================================
1399 /*!
1400  * \brief Returns true if hypotheses of given dim can be assigned
1401   * \param theDim - hypotheses dimension
1402   * \retval bool - result
1403  */
1404 //================================================================================
1405 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
1406 {
1407   return myDlg->tab( theDim )->isEnabled();
1408 }
1409
1410 //================================================================================
1411 /*!
1412  * \brief Sets current hypothesis or algorithms
1413   * \param theDim - dimension of hypothesis or algorithm
1414   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1415   * \param theIndex - Index of hypothesis
1416  *
1417  * Gets current hypothesis or algorithms
1418  */
1419 //================================================================================
1420 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1421                                      const int theHypType,
1422                                      const int theIndex )
1423 {
1424   myIgnoreAlgoSelection = true;
1425   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1426   myIgnoreAlgoSelection = false;
1427 }
1428
1429 //================================================================================
1430 /*!
1431  * \brief Generates default and sets mesh/submesh name
1432  *
1433  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1434  */
1435 //================================================================================
1436 void SMESHGUI_MeshOp::setDefaultName() const
1437 {
1438   QString aResName;
1439
1440   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1441   int i = 1;
1442   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1443   _PTR(SObject) anObj;
1444   do
1445   {
1446     aResName = aPrefix + QString::number( i++ );
1447     anObj = aStudy->FindObject( aResName.latin1() );
1448   }
1449   while ( anObj );
1450
1451   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1452     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1453   aControl->setText( aResName );
1454 }
1455
1456 //================================================================================
1457 /*!
1458  * \brief Gets algorithm or creates it if necessary
1459   * \param theDim - specifies dimension of returned hypotheses/algorifms
1460   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1461  *
1462  * Gets algorithm or creates it if necessary
1463  */
1464 //================================================================================
1465 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1466 {
1467   SMESH::SMESH_Hypothesis_var anAlgoVar;
1468
1469   // get type of the selected algo
1470   int aHypIndex = currentHyp( theDim, Algo );
1471   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
1472   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
1473     return anAlgoVar;
1474   QString aHypName = dataList[ aHypIndex ]->TypeName;
1475
1476   // get existing algoritms
1477   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1478   QStringList tmp;
1479   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
1480
1481   // look for anexisting algo of such a type
1482   QValueList<SMESH::SMESH_Hypothesis_var>& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1483   QValueList<SMESH::SMESH_Hypothesis_var>::iterator anIter;
1484   for ( anIter = aHypVarList.begin(); anIter != aHypVarList.end(); anIter++ )
1485   {
1486     SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1487     if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1488     {
1489       anAlgoVar = aHypVar;
1490       break;
1491     }
1492   }
1493
1494   if (anAlgoVar->_is_nil()) {
1495     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1496     if (aHypData) {
1497       QString aClientLibName = aHypData->ClientLibName;
1498       if (aClientLibName == "") {
1499         // Call hypothesis creation server method (without GUI)
1500         SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1501       } else {
1502         // Get hypotheses creator client (GUI)
1503         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
1504
1505         // Create algorithm
1506         if (aCreator)
1507           aCreator->create(true, myDlg);
1508         else
1509           SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1510       }
1511       QStringList tmpList;
1512       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
1513       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
1514     }
1515
1516     QValueList<SMESH::SMESH_Hypothesis_var>& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
1517     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
1518     {
1519       SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1520       if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1521       {
1522         anAlgoVar = aHypVar;
1523         break;
1524       }
1525     }
1526   }
1527
1528   return anAlgoVar._retn();
1529 }
1530
1531 //================================================================================
1532 /*!
1533  * \brief Reads parameters of edited mesh and assigns them to the dialog
1534  *
1535  * Reads parameters of edited mesh and assigns them to the dialog (called when
1536  * mesh is edited only)
1537  */
1538 //================================================================================
1539 void SMESHGUI_MeshOp::readMesh()
1540 {
1541   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1542   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1543   if ( !pObj )
1544     return;
1545
1546   // Get name of mesh if current object is sub-mesh
1547   SMESH::SMESH_subMesh_var aSubMeshVar =
1548       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1549   if ( !aSubMeshVar->_is_nil() )
1550   {
1551     SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1552     if ( !aMeshVar->_is_nil() )
1553     {
1554       _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1555       QString aMeshName = name( aMeshSO );
1556       myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
1557     }
1558   }
1559
1560   // Get name of geometry object
1561   GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1562   if ( !aGeomVar->_is_nil() )
1563   {
1564     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
1565     QString aShapeName = name( aGeomSO );
1566     myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
1567   }
1568
1569   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
1570   QStringList anExisting;
1571   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1572   {
1573     // get algorithm
1574     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
1575     // find algo index among available ones
1576     int aHypIndex = -1;
1577     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
1578     {
1579       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first();
1580       QString aHypTypeName = aVar->GetName();
1581       HypothesisData* algoData = SMESH::GetHypothesisData( aHypTypeName );
1582       aHypIndex = myAvailableHypData[ dim ][ Algo ].findIndex ( algoData );
1583 //       if ( aHypIndex < 0 && algoData ) {
1584 //         // assigned algo is incompatible with other algorithms
1585 //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
1586 //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
1587 //       }
1588     }
1589     setCurrentHyp( dim, Algo, aHypIndex );
1590     // set existing and available hypothesis according to the selected algo
1591     onAlgoSelected( aHypIndex, dim );
1592   }
1593
1594   // get hypotheses
1595   bool hypWithoutAlgo = false;
1596   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1597   {
1598     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1599     {
1600       // get hypotheses
1601       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1602       // find index of requered hypothesis among existing ones for this dimension and type
1603       int aHypIndex = -1;
1604       if ( myObjHyps[ dim ][ hypType ].count() > 0 ) {
1605         aHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1606                           myExistingHyps[ dim ][ hypType ] );
1607         if ( aHypIndex < 0 ) {
1608           // assigned hypothesis is incompatible with the algorithm
1609           if ( currentHyp( dim, Algo ) < 0 )
1610           { // none algo selected; it is edition for sure, of submesh maybe
1611             hypWithoutAlgo = true;
1612             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ].first() );
1613             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
1614             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
1615           }
1616         }
1617       }
1618       setCurrentHyp( dim, hypType, aHypIndex );
1619     }
1620   }
1621   // make available other hyps of same type as one without algo
1622   if ( hypWithoutAlgo )
1623     onAlgoSelected( currentHyp( 0, Algo ), 0 );
1624 }
1625
1626 //================================================================================
1627 /*!
1628  * \brief Gets name of object
1629   * \param theSO - SObject
1630   * \retval QString - name of object
1631  *
1632  * Gets name of object
1633  */
1634 //================================================================================
1635 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1636 {
1637   QString aResName;
1638   if ( theSO )
1639   {
1640     _PTR(GenericAttribute) anAttr;
1641     _PTR(AttributeName)    aNameAttr;
1642     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1643     {
1644       aNameAttr = anAttr;
1645       aResName = aNameAttr->Value().c_str();
1646     }
1647   }
1648   return aResName;
1649 }
1650
1651 //================================================================================
1652 /*!
1653  * \brief Finds hypothesis in input list
1654   * \param theHyp - hypothesis to be found
1655   * \param theHypList - input list of hypotheses
1656   * \retval int - index of hypothesis or -1 if it is not found
1657  *
1658  * Finds position of hypothesis in input list
1659  */
1660 //================================================================================
1661 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1662                            const QValueList<SMESH::SMESH_Hypothesis_var>& theHypList ) const
1663 {
1664   int aRes = -1;
1665   if ( !theHyp->_is_nil() )
1666   {
1667     int i = 0;
1668     QValueList<SMESH::SMESH_Hypothesis_var>::const_iterator anIter;
1669     for ( anIter = theHypList.begin(); anIter != theHypList.end(); ++ anIter )
1670     {
1671       if ( theHyp->_is_equivalent( *anIter ) )
1672       {
1673         aRes = i;
1674         break;
1675       }
1676       i++;
1677     }
1678   }
1679   return aRes;
1680 }
1681
1682 //================================================================================
1683 /*!
1684  * \brief Edits mesh or sub-mesh
1685   * \param theMess - Output parameter intended for returning error message
1686   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1687  *
1688  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1689  */
1690 //================================================================================
1691 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1692 {
1693   theMess = "";
1694
1695   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1696   if ( aSMESHGen->_is_nil() )
1697     return false;
1698
1699   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1700   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1701   if ( !pObj )
1702     return false;
1703
1704   SUIT_OverrideCursor aWaitCursor;
1705
1706   // Set new name
1707   SMESH::SetName( pObj, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1708
1709   // Assign new hypotheses and algorithms
1710   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1711   {
1712     if ( !isAccessibleDim( dim )) continue;
1713
1714     // find or create algorithm
1715     bool toDelete = false, toAdd = true;
1716     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1717     if ( anAlgoVar->_is_nil() ) {
1718       toAdd = false;
1719     }
1720     if ( myObjHyps[ dim ][ Algo ].count() > 0 ) {
1721       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first();
1722       if ( toAdd ) {
1723         if ( strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) == 0 ) {
1724           toAdd = false;
1725         } else {
1726           toDelete = true;
1727         }
1728       } else {
1729         toDelete = true;
1730       }
1731     }
1732     // remove old algorithm
1733     if ( toDelete ) {
1734       SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first() );
1735       myObjHyps[ dim ][ Algo ].clear();
1736     }
1737
1738     // assign new algorithm
1739     if ( toAdd ) {
1740       SMESH::SMESH_Mesh_var aMeshVar =
1741         SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1742       bool isMesh = !aMeshVar->_is_nil();
1743       if ( isMesh ) {
1744         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1745       } else {
1746         SMESH::SMESH_subMesh_var aVar =
1747           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1748         if ( !aVar->_is_nil() )
1749           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1750       }
1751       myObjHyps[ dim ][ Algo ].append( anAlgoVar );
1752     }
1753
1754     // assign hypotheses
1755     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1756     {
1757       int aNewHypIndex = currentHyp( dim, hypType );
1758       int anOldHypIndex = -1;
1759       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1760         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1761                               myExistingHyps[ dim ][ hypType ] );
1762       if ( aNewHypIndex != anOldHypIndex )
1763       {
1764         // remove old hypotheses
1765         if ( anOldHypIndex >= 0 ) {
1766           SMESH::RemoveHypothesisOrAlgorithmOnMesh(
1767             pObj, myExistingHyps[ dim ][ hypType ][ anOldHypIndex ] );
1768           myObjHyps[ dim ][ hypType ].clear();
1769         }
1770
1771         // assign new hypotheses
1772         if ( aNewHypIndex != -1 )
1773         {
1774           SMESH::SMESH_Mesh_var aMeshVar =
1775               SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1776           bool isMesh = !aMeshVar->_is_nil();
1777           if ( isMesh )
1778           {
1779             SMESH::AddHypothesisOnMesh(
1780               aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1781           }
1782           else
1783           {
1784             SMESH::SMESH_subMesh_var aVar =
1785               SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1786             if ( !aVar->_is_nil() )
1787               SMESH::AddHypothesisOnSubMesh(
1788                 aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1789           }
1790         }
1791         // reread all hypotheses of mesh if necessary
1792         QStringList anExisting;
1793         existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1794       }
1795     }
1796   }
1797
1798   return true;
1799 }
1800
1801 //================================================================================
1802 /*!
1803  * \brief Verifies whether given operator is valid for this one
1804   * \param theOtherOp - other operation
1805   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1806 *
1807 * method redefined from base class verifies whether given operator is valid for
1808 * this one (i.e. can be started "above" this operator). In current implementation method
1809 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1810 * elements.
1811 */
1812 //================================================================================
1813 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1814 {
1815   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1816 }
1817
1818 //================================================================================
1819 /*!
1820  * \brief SLOT. Is called when the user selects a way of geometry selection
1821   * \param theByMesh - true if the user wants to find geometry by mesh element
1822  */
1823 //================================================================================
1824
1825 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
1826 {
1827   if ( theByMesh ) {
1828     if ( !myShapeByMeshOp ) {
1829       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
1830       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
1831               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
1832       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
1833               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
1834     }
1835     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
1836     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1837     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
1838       SMESH::SMESH_Mesh_var aMeshVar =
1839         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1840       if ( !aMeshVar->_is_nil() ) {
1841         myDlg->hide(); // stop processing selection
1842         myShapeByMeshOp->setModule( getSMESHGUI() );
1843         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
1844         myShapeByMeshOp->SetMesh( aMeshVar );
1845         myShapeByMeshOp->start();
1846       }
1847     }
1848   }
1849 }
1850
1851 //================================================================================
1852 /*!
1853  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1854  */
1855 //================================================================================
1856
1857 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
1858 {
1859   if ( myShapeByMeshOp == op ) {
1860     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
1861     myDlg->show();
1862     // Select a found geometry object
1863     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
1864     if ( !aGeomVar->_is_nil() )
1865     {
1866       QString ID = aGeomVar->GetStudyEntry();
1867       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.latin1() )) {
1868         selectObject( aGeomSO );
1869         selectionDone();
1870       }
1871     }
1872   }
1873 }
1874
1875 //================================================================================
1876 /*!
1877  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
1878  */
1879 //================================================================================
1880
1881 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
1882 {
1883   if ( myShapeByMeshOp == op && myDlg ) {
1884     myDlg->show();
1885   }
1886 }
1887
1888 //================================================================================
1889 /*!
1890  * \brief Selects a SObject
1891   * \param theSObj - the SObject to select
1892  */
1893 //================================================================================
1894
1895 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
1896 {
1897   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
1898     SALOME_ListIO anIOList;
1899     if ( theSObj ) {
1900       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1901         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
1902       anIOList.Append( anIO );
1903     }
1904     sm->setSelectedObjects( anIOList, false );
1905   }
1906 }