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