Salome HOME
Merging with WPdev
[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() || !myDlg->isEnabled() )
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       myDlg->setEnabled( false );
908       aCreator->create(initParamHyp, myDlg);
909       myDlg->setEnabled( true );
910     } else {
911       SMESH::CreateHypothesis(theTypeName, aData->Label, false);
912     }
913   }
914
915   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
916
917   HypothesisData* algoData = hypData( theDim, Algo, currentHyp( theDim, Algo ));
918   QStringList aNewHyps;
919   existingHyps(theDim, theType, aFather, aNewHyps, myExistingHyps[theDim][theType], algoData);
920   if (aNewHyps.count() > nbHyp) {
921     for (int i = nbHyp; i < aNewHyps.count(); i++)
922       myDlg->tab(theDim)->addHyp(theType, aNewHyps[i]);
923   }
924 }
925
926 //================================================================================
927 /*!
928  * \brief Calls plugin methods for hypothesis editing
929   * \param theHypType - specifies whether main hypothesis or additional one
930   * is edited
931   * \param theIndex - index of existing hypothesis
932  *
933  * Calls plugin methods for hypothesis editing
934  */
935 //================================================================================
936 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
937 {
938   // Speicfies dimension of hypothesis to be created
939   int aDim = getTabDim( sender(), myDlg );
940   if (aDim == -1)
941     return;
942
943   QValueList<SMESH::SMESH_Hypothesis_var> aList = myExistingHyps[ aDim ][ theHypType ];
944   if ( theIndex < 0 || theIndex >= aList.count() )
945     return;
946   SMESH::SMESH_Hypothesis_var aHyp = aList[ theIndex ];
947   if ( aHyp->_is_nil() )
948     return;
949
950   char* aTypeName = aHyp->GetName();
951   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aTypeName );
952   if ( aCreator ) {
953     myDlg->setEnabled( false );
954     aCreator->edit( aHyp.in(), dlg() );
955     myDlg->setEnabled( true );
956   }
957 }
958
959 //================================================================================
960 /*!
961  * \brief access to hypothesis data
962   * \param theDim - hyp dimension
963   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
964   * \param theIndex - index in the list
965   * \retval HypothesisData* - result data, may be 0
966  */
967 //================================================================================
968
969 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
970                                           const int theHypType,
971                                           const int theIndex)
972 {
973   if ( theDim     > -1 && theDim     < 3 &&
974        theHypType > -1 && theHypType < NbHypTypes &&
975        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
976     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
977   return 0;
978 }
979
980 //================================================================================
981 /*!
982  * \brief Set available algos and hypos according to the selected algorithm
983   * \param theIndex - algorithm index
984  */
985 //================================================================================
986
987 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
988                                       const int theDim )
989 {
990   if ( myIgnoreAlgoSelection )
991     return;
992
993   int aDim = theDim < 0 ? getTabDim( sender(), myDlg ): theDim;
994   if (aDim == -1)
995     return;
996
997   // find highest available dimension, all algos of this dimension are available for choice
998   int aTopDim = -1;
999   for (int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++)
1000     if (isAccessibleDim( i ))
1001       aTopDim = i;
1002   if (aTopDim == -1)
1003     return;
1004
1005   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1006
1007   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1008   HypothesisData* algoByDim[3];
1009   algoByDim[ aDim ] = algoData;
1010
1011   QStringList anAvailable;
1012   if ( !algoData ) { // all algos becomes available
1013     availableHyps( aDim, Algo, anAvailable, myAvailableHypData[ aDim ][ Algo ]);
1014     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1015   }
1016
1017   // check that algorithms of other dimentions are compatible with
1018   // the selected one
1019
1020    // 2 loops: backward and forward from algo dimension
1021   for ( int forward = false; forward <= true; ++forward )
1022   {
1023     int dim = aDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
1024     if ( !forward ) {
1025       dim = aDim - 1; lastDim = SMESH::DIM_1D; dir = -1;
1026     }
1027     HypothesisData* prevAlgo = algoData;
1028     bool noCompatible = false;
1029     for ( ; dim * dir <= lastDim * dir ; dim += dir )
1030     {
1031       if ( !isAccessibleDim( dim ))
1032         continue;
1033       if ( noCompatible ) { // the selected algo has no compatible ones
1034         anAvailable.clear();
1035         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1036         myAvailableHypData[dim][Algo].clear();
1037         algoByDim[ dim ] = 0;
1038         continue;
1039       }
1040       // get currently selected algo
1041       int algoIndex = currentHyp( dim, Algo );
1042       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
1043       if ( curAlgo ) { // some algo selected
1044         if ( !isCompatible( prevAlgo, curAlgo, Algo ))
1045           curAlgo = 0;
1046       }
1047       // set new available algoritms
1048       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
1049       HypothesisData* soleCompatible = 0;
1050       if ( anAvailable.count() == 1 )
1051         soleCompatible = myAvailableHypData[dim][Algo][0];
1052       if ( dim == aTopDim && prevAlgo ) // all available algoritms should be selectable any way
1053         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], 0 );
1054       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1055       noCompatible = anAvailable.isEmpty();
1056
1057       // restore previously selected algo
1058       algoIndex = myAvailableHypData[dim][Algo].findIndex( curAlgo );
1059       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward )
1060         // select the sole compatible algo
1061         algoIndex = myAvailableHypData[dim][Algo].findIndex( soleCompatible );
1062       setCurrentHyp( dim, Algo, algoIndex );
1063
1064       // remember current algo
1065       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1066     }
1067   }
1068
1069   // set hypotheses corresponding to the found algoritms
1070
1071   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1072
1073   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1074   {
1075     if ( !isAccessibleDim( dim ))
1076       continue;
1077     for ( int type = MainHyp; type < NbHypTypes; type++ )
1078     {
1079       myAvailableHypData[ dim ][ type ].clear();
1080       QStringList anAvailable, anExisting;
1081
1082       HypothesisData* curAlgo = algoByDim[ dim ];
1083       int hypIndex = currentHyp( dim, type );
1084
1085       SMESH::SMESH_Hypothesis_var curHyp;
1086       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1087         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ];
1088
1089       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
1090         // try to find algo by selected hypothesis in order to keep it selected
1091         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1092         QString curHypType = curHyp->GetName();
1093         if ( !algoDeselectedByUser &&
1094              myObjHyps[ dim ][ type ].count() > 0 &&
1095              curHypType == myObjHyps[ dim ][ type ][ 0 ]->GetName())
1096         {
1097           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1098           for ( int i = 0 ; i < myAvailableHypData[ dim ][ Algo ].count(); ++i ) {
1099             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1100             if ( curAlgo && hypData && isCompatible( curAlgo, hypData, type ))
1101               break;
1102             else
1103               curAlgo = 0;
1104           }
1105         }
1106       }
1107       // get hyps compatible with curAlgo
1108       if ( curAlgo )
1109       {
1110         // check if a selected hyp is compatible with the curAlgo
1111         if ( !curHyp->_is_nil() ) {
1112           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1113           if ( !isCompatible( curAlgo, hypData, type ))
1114             curHyp = SMESH::SMESH_Hypothesis::_nil();
1115         }
1116         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
1117         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
1118       }
1119       // set list of hypotheses
1120       myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1121       myDlg->tab( dim )->setExistingHyps( type, anExisting );
1122
1123       // set current existing hypothesis
1124       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1125         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1126       else
1127         hypIndex = -1;
1128       if ( !isSubmesh && hypIndex < 0 && anExisting.count() == 1 ) {
1129         // none is yet selected => select the sole existing if it is not optional
1130         QString hypTypeName = myExistingHyps[ dim ][ type ][ 0 ]->GetName();
1131         bool isOptional = true;
1132         if ( algoByDim[ dim ] &&
1133              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName, isOptional ) &&
1134              !isOptional )
1135           hypIndex = 0;
1136       }
1137       setCurrentHyp( dim, type, hypIndex );
1138     }
1139   }
1140 }
1141
1142 //================================================================================
1143 /*!
1144  * \brief Creates and selects hypothesis of hypotheses set
1145  * \param theSetName - The name of hypotheses set
1146  */
1147 //================================================================================
1148 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1149 {
1150   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet(theSetName);
1151   if (!aHypoSet) return;
1152
1153   // clear all hyps
1154   for (int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++) {
1155     setCurrentHyp(dim, Algo, -1);
1156     setCurrentHyp(dim, AddHyp, -1);
1157     setCurrentHyp(dim, MainHyp, -1);
1158   }
1159
1160   for (int aHypType = Algo; aHypType < AddHyp; aHypType++) {
1161     bool isAlgo = (aHypType == Algo);
1162
1163     // set hyps from the set
1164     QStringList* aHypoList = isAlgo ? &aHypoSet->AlgoList : &aHypoSet->HypoList;
1165     for (int i = 0, n = aHypoList->count(); i < n; i++) {
1166       const QString& aHypoTypeName = (*aHypoList)[ i ];
1167       HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
1168       if (!aHypData)
1169         continue;
1170
1171       int aDim = aHypData->Dim[0];
1172       // create or/and set
1173       if (isAlgo) {
1174         int index = myAvailableHypData[aDim][Algo].findIndex( aHypData );
1175         if ( index < 0 ) {
1176           QStringList anAvailable;
1177           availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
1178           myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1179           index = myAvailableHypData[aDim][Algo].findIndex( aHypData );
1180         }
1181         setCurrentHyp( aDim, Algo, index );
1182         onAlgoSelected( index, aDim );
1183       }
1184       else {
1185         bool mainHyp = true;
1186         int index = myAvailableHypData[aDim][MainHyp].findIndex( aHypData );
1187         if ( index < 0 ) {
1188           mainHyp = false;
1189           index = myAvailableHypData[aDim][AddHyp].findIndex( aHypData );
1190         }
1191         if (index >= 0)
1192           createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
1193       }
1194     } // loop on hypos in the set
1195   } // loop on algo/hypo
1196 }
1197
1198 //================================================================================
1199 /*!
1200  * \brief Creates mesh
1201   * \param theMess - Output parameter intended for returning error message
1202   * \retval bool  - TRUE if mesh is created, FALSE otherwise
1203  *
1204  * Creates mesh
1205  */
1206 //================================================================================
1207 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
1208 {
1209   theMess = "";
1210
1211   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1212   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1213
1214   QStringList aList;
1215   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1216   QStringList::Iterator it = aList.begin();
1217   for(; it!=aList.end(); it++) {
1218
1219     QString aGeomEntry = *it;
1220     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1221     GEOM::GEOM_Object_var aGeomVar =
1222       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1223
1224     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1225     if ( aSMESHGen->_is_nil() )
1226       return false;
1227
1228     SUIT_OverrideCursor aWaitCursor;
1229
1230     // create mesh
1231     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
1232     if ( aMeshVar->_is_nil() )
1233       return false;
1234     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1235     if ( aMeshSO )
1236       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1237
1238     for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ ) {
1239       if ( !isAccessibleDim( aDim )) continue;
1240
1241       // assign hypotheses
1242       for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ ) {
1243         int aHypIndex = currentHyp( aDim, aHypType );
1244         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() ) {
1245           SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1246           if ( !aHypVar->_is_nil() )
1247             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
1248         }
1249       }
1250       // find or create algorithm
1251       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1252       if ( !anAlgoVar->_is_nil() )
1253         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1254     }
1255
1256   }
1257   return true;
1258 }
1259
1260 //================================================================================
1261 /*!
1262  * \brief Creates sub-mesh
1263   * \param theMess - Output parameter intended for returning error message
1264   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1265  *
1266  * Creates sub-mesh
1267  */
1268 //================================================================================
1269 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
1270 {
1271   theMess = "";
1272
1273   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1274   if ( aSMESHGen->_is_nil() )
1275     return false;
1276
1277   // get mesh object
1278   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1279   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
1280   SMESH::SMESH_Mesh_var aMeshVar =
1281     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1282   if (aMeshVar->_is_nil())
1283     return false;
1284
1285   // GEOM shape of the main mesh
1286   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
1287
1288   // Name for the new sub-mesh
1289   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
1290
1291   // get geom object
1292   GEOM::GEOM_Object_var aGeomVar;
1293   QStringList aGEOMs;
1294   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
1295   if (aGEOMs.count() == 1)
1296   {
1297     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1298     QString aGeomEntry = aGEOMs.first();
1299     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1300     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1301   }
1302   else if (aGEOMs.count() > 1)
1303   {
1304     // create a GEOM group
1305     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1306     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1307     if (!geomGen->_is_nil() && aStudy) {
1308       GEOM::GEOM_IGroupOperations_var op =
1309         geomGen->GetIGroupOperations(aStudy->StudyId());
1310       if (!op->_is_nil()) {
1311         // check and add all selected GEOM objects: they must be
1312         // a sub-shapes of the main GEOM and must be of one type
1313         int iSubSh = 0;
1314         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1315         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
1316         aSeq->length(aGEOMs.count());
1317         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
1318         for (; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
1319           QString aSubGeomEntry = (*aSubShapesIter);
1320           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.latin1());
1321           GEOM::GEOM_Object_var aSubGeomVar =
1322             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
1323           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
1324           if (iSubSh == 0) {
1325             aGroupType = aSubShapeType;
1326           } else {
1327             if (aSubShapeType != aGroupType)
1328               aGroupType = TopAbs_SHAPE;
1329           }
1330           aSeq[iSubSh] = aSubGeomVar;
1331         }
1332         // create a group
1333         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(mainGeom, aGroupType);
1334         op->UnionList(aGroupVar, aSeq);
1335
1336         if (op->IsDone()) {
1337           aGeomVar = aGroupVar;
1338
1339           // publish the GEOM group in study
1340           QString aNewGeomGroupName ("Auto_group_for_");
1341           aNewGeomGroupName += aName;
1342           SALOMEDS::SObject_var aNewGroupSO =
1343             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGeomVar, aNewGeomGroupName, mainGeom);
1344         }
1345       }
1346     }
1347   }
1348   else {
1349   }
1350   if (aGeomVar->_is_nil())
1351     return false;
1352
1353   SUIT_OverrideCursor aWaitCursor;
1354
1355   // create sub-mesh
1356   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.latin1() );
1357
1358   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
1359   {
1360     if ( !isAccessibleDim( aDim )) continue;
1361
1362     // find or create algorithm
1363     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1364     if ( !anAlgoVar->_is_nil() )
1365       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1366     // assign hypotheses
1367     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1368     {
1369       int aHypIndex = currentHyp( aDim, aHypType );
1370       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1371       {
1372         SMESH::SMESH_Hypothesis_var aHypVar =
1373           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1374         if ( !aHypVar->_is_nil() )
1375           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1376       }
1377     }
1378   }
1379
1380   // deselect geometry: next submesh sould be created on other subshape
1381   myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
1382   selectObject( _PTR(SObject)() );
1383   selectionDone();
1384
1385   return true;
1386 }
1387
1388 //================================================================================
1389 /*!
1390  * \brief Gets current hypothesis or algorithms
1391   * \param theDim - dimension of hypothesis or algorithm
1392   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1393   * \retval int - current hypothesis or algorithms
1394  *
1395  * Gets current hypothesis or algorithms
1396  */
1397 //================================================================================
1398 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1399 {
1400   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1401 }
1402
1403 //================================================================================
1404 /*!
1405  * \brief Returns true if hypotheses of given dim can be assigned
1406   * \param theDim - hypotheses dimension
1407   * \retval bool - result
1408  */
1409 //================================================================================
1410 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
1411 {
1412   return myDlg->tab( theDim )->isEnabled();
1413 }
1414
1415 //================================================================================
1416 /*!
1417  * \brief Sets current hypothesis or algorithms
1418   * \param theDim - dimension of hypothesis or algorithm
1419   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1420   * \param theIndex - Index of hypothesis
1421  *
1422  * Gets current hypothesis or algorithms
1423  */
1424 //================================================================================
1425 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1426                                      const int theHypType,
1427                                      const int theIndex )
1428 {
1429   myIgnoreAlgoSelection = true;
1430   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1431   myIgnoreAlgoSelection = false;
1432 }
1433
1434 //================================================================================
1435 /*!
1436  * \brief Generates default and sets mesh/submesh name
1437  *
1438  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1439  */
1440 //================================================================================
1441 void SMESHGUI_MeshOp::setDefaultName() const
1442 {
1443   QString aResName;
1444
1445   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1446   int i = 1;
1447   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1448   _PTR(SObject) anObj;
1449   do
1450   {
1451     aResName = aPrefix + QString::number( i++ );
1452     anObj = aStudy->FindObject( aResName.latin1() );
1453   }
1454   while ( anObj );
1455
1456   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1457     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1458   aControl->setText( aResName );
1459 }
1460
1461 //================================================================================
1462 /*!
1463  * \brief Gets algorithm or creates it if necessary
1464   * \param theDim - specifies dimension of returned hypotheses/algorifms
1465   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1466  *
1467  * Gets algorithm or creates it if necessary
1468  */
1469 //================================================================================
1470 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1471 {
1472   SMESH::SMESH_Hypothesis_var anAlgoVar;
1473
1474   // get type of the selected algo
1475   int aHypIndex = currentHyp( theDim, Algo );
1476   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
1477   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
1478     return anAlgoVar;
1479   QString aHypName = dataList[ aHypIndex ]->TypeName;
1480
1481   // get existing algoritms
1482   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1483   QStringList tmp;
1484   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
1485
1486   // look for anexisting algo of such a type
1487   QValueList<SMESH::SMESH_Hypothesis_var>& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1488   QValueList<SMESH::SMESH_Hypothesis_var>::iterator anIter;
1489   for ( anIter = aHypVarList.begin(); anIter != aHypVarList.end(); anIter++ )
1490   {
1491     SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1492     if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1493     {
1494       anAlgoVar = aHypVar;
1495       break;
1496     }
1497   }
1498
1499   if (anAlgoVar->_is_nil()) {
1500     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1501     if (aHypData) {
1502       QString aClientLibName = aHypData->ClientLibName;
1503       if (aClientLibName == "") {
1504         // Call hypothesis creation server method (without GUI)
1505         SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1506       } else {
1507         // Get hypotheses creator client (GUI)
1508         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
1509
1510         // Create algorithm
1511         if (aCreator)
1512           aCreator->create(true, myDlg);
1513         else
1514           SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1515       }
1516       QStringList tmpList;
1517       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
1518       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
1519     }
1520
1521     QValueList<SMESH::SMESH_Hypothesis_var>& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
1522     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
1523     {
1524       SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1525       if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1526       {
1527         anAlgoVar = aHypVar;
1528         break;
1529       }
1530     }
1531   }
1532
1533   return anAlgoVar._retn();
1534 }
1535
1536 //================================================================================
1537 /*!
1538  * \brief Reads parameters of edited mesh and assigns them to the dialog
1539  *
1540  * Reads parameters of edited mesh and assigns them to the dialog (called when
1541  * mesh is edited only)
1542  */
1543 //================================================================================
1544 void SMESHGUI_MeshOp::readMesh()
1545 {
1546   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1547   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1548   if ( !pObj )
1549     return;
1550
1551   // Get name of mesh if current object is sub-mesh
1552   SMESH::SMESH_subMesh_var aSubMeshVar =
1553       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1554   if ( !aSubMeshVar->_is_nil() )
1555   {
1556     SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1557     if ( !aMeshVar->_is_nil() )
1558     {
1559       _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1560       QString aMeshName = name( aMeshSO );
1561       myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
1562     }
1563   }
1564
1565   // Get name of geometry object
1566   GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1567   if ( !aGeomVar->_is_nil() )
1568   {
1569     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
1570     QString aShapeName = name( aGeomSO );
1571     myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
1572   }
1573
1574   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
1575   QStringList anExisting;
1576   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1577   {
1578     // get algorithm
1579     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
1580     // find algo index among available ones
1581     int aHypIndex = -1;
1582     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
1583     {
1584       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first();
1585       QString aHypTypeName = aVar->GetName();
1586       HypothesisData* algoData = SMESH::GetHypothesisData( aHypTypeName );
1587       aHypIndex = myAvailableHypData[ dim ][ Algo ].findIndex ( algoData );
1588 //       if ( aHypIndex < 0 && algoData ) {
1589 //         // assigned algo is incompatible with other algorithms
1590 //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
1591 //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
1592 //       }
1593     }
1594     setCurrentHyp( dim, Algo, aHypIndex );
1595     // set existing and available hypothesis according to the selected algo
1596     onAlgoSelected( aHypIndex, dim );
1597   }
1598
1599   // get hypotheses
1600   bool hypWithoutAlgo = false;
1601   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1602   {
1603     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1604     {
1605       // get hypotheses
1606       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1607       // find index of requered hypothesis among existing ones for this dimension and type
1608       int aHypIndex = -1;
1609       if ( myObjHyps[ dim ][ hypType ].count() > 0 ) {
1610         aHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1611                           myExistingHyps[ dim ][ hypType ] );
1612         if ( aHypIndex < 0 ) {
1613           // assigned hypothesis is incompatible with the algorithm
1614           if ( currentHyp( dim, Algo ) < 0 )
1615           { // none algo selected; it is edition for sure, of submesh maybe
1616             hypWithoutAlgo = true;
1617             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ].first() );
1618             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
1619             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
1620           }
1621         }
1622       }
1623       setCurrentHyp( dim, hypType, aHypIndex );
1624     }
1625   }
1626   // make available other hyps of same type as one without algo
1627   if ( hypWithoutAlgo )
1628     onAlgoSelected( currentHyp( 0, Algo ), 0 );
1629 }
1630
1631 //================================================================================
1632 /*!
1633  * \brief Gets name of object
1634   * \param theSO - SObject
1635   * \retval QString - name of object
1636  *
1637  * Gets name of object
1638  */
1639 //================================================================================
1640 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1641 {
1642   QString aResName;
1643   if ( theSO )
1644   {
1645     _PTR(GenericAttribute) anAttr;
1646     _PTR(AttributeName)    aNameAttr;
1647     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1648     {
1649       aNameAttr = anAttr;
1650       aResName = aNameAttr->Value().c_str();
1651     }
1652   }
1653   return aResName;
1654 }
1655
1656 //================================================================================
1657 /*!
1658  * \brief Finds hypothesis in input list
1659   * \param theHyp - hypothesis to be found
1660   * \param theHypList - input list of hypotheses
1661   * \retval int - index of hypothesis or -1 if it is not found
1662  *
1663  * Finds position of hypothesis in input list
1664  */
1665 //================================================================================
1666 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1667                            const QValueList<SMESH::SMESH_Hypothesis_var>& theHypList ) const
1668 {
1669   int aRes = -1;
1670   if ( !theHyp->_is_nil() )
1671   {
1672     int i = 0;
1673     QValueList<SMESH::SMESH_Hypothesis_var>::const_iterator anIter;
1674     for ( anIter = theHypList.begin(); anIter != theHypList.end(); ++ anIter )
1675     {
1676       if ( theHyp->_is_equivalent( *anIter ) )
1677       {
1678         aRes = i;
1679         break;
1680       }
1681       i++;
1682     }
1683   }
1684   return aRes;
1685 }
1686
1687 //================================================================================
1688 /*!
1689  * \brief Edits mesh or sub-mesh
1690   * \param theMess - Output parameter intended for returning error message
1691   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1692  *
1693  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1694  */
1695 //================================================================================
1696 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1697 {
1698   theMess = "";
1699
1700   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1701   if ( aSMESHGen->_is_nil() )
1702     return false;
1703
1704   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1705   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1706   if ( !pObj )
1707     return false;
1708
1709   SUIT_OverrideCursor aWaitCursor;
1710
1711   // Set new name
1712   SMESH::SetName( pObj, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1713
1714   // Assign new hypotheses and algorithms
1715   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1716   {
1717     if ( !isAccessibleDim( dim )) continue;
1718
1719     // find or create algorithm
1720     bool toDelete = false, toAdd = true;
1721     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1722     if ( anAlgoVar->_is_nil() ) {
1723       toAdd = false;
1724     }
1725     if ( myObjHyps[ dim ][ Algo ].count() > 0 ) {
1726       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first();
1727       if ( toAdd ) {
1728         if ( strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) == 0 ) {
1729           toAdd = false;
1730         } else {
1731           toDelete = true;
1732         }
1733       } else {
1734         toDelete = true;
1735       }
1736     }
1737     // remove old algorithm
1738     if ( toDelete ) {
1739       SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first() );
1740       myObjHyps[ dim ][ Algo ].clear();
1741     }
1742
1743     // assign new algorithm
1744     if ( toAdd ) {
1745       SMESH::SMESH_Mesh_var aMeshVar =
1746         SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1747       bool isMesh = !aMeshVar->_is_nil();
1748       if ( isMesh ) {
1749         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1750       } else {
1751         SMESH::SMESH_subMesh_var aVar =
1752           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1753         if ( !aVar->_is_nil() )
1754           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1755       }
1756       myObjHyps[ dim ][ Algo ].append( anAlgoVar );
1757     }
1758
1759     // assign hypotheses
1760     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1761     {
1762       int aNewHypIndex = currentHyp( dim, hypType );
1763       int anOldHypIndex = -1;
1764
1765       // remove old hypotheses
1766       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1767       {
1768         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1769                               myExistingHyps[ dim ][ hypType ] );
1770         if ( aNewHypIndex != anOldHypIndex || // different hyps
1771              anOldHypIndex == -1 )            // hyps of different algos
1772         {
1773           SMESH::RemoveHypothesisOrAlgorithmOnMesh
1774             ( pObj, myObjHyps[ dim ][ hypType ].first() );
1775           myObjHyps[ dim ][ hypType ].clear();
1776         }
1777       }
1778
1779       // assign new hypotheses
1780       if ( aNewHypIndex != anOldHypIndex && aNewHypIndex != -1 )
1781       {
1782         SMESH::SMESH_Mesh_var aMeshVar =
1783           SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1784         bool isMesh = !aMeshVar->_is_nil();
1785         if ( isMesh )
1786         {
1787           SMESH::AddHypothesisOnMesh
1788             (aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1789         }
1790         else
1791         {
1792           SMESH::SMESH_subMesh_var aVar =
1793             SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1794           if ( !aVar->_is_nil() )
1795             SMESH::AddHypothesisOnSubMesh
1796               ( aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1797         }
1798       }
1799       // reread all hypotheses of mesh if necessary
1800       QStringList anExisting;
1801       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1802     }
1803   }
1804
1805   return true;
1806 }
1807
1808 //================================================================================
1809 /*!
1810  * \brief Verifies whether given operator is valid for this one
1811   * \param theOtherOp - other operation
1812   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1813 *
1814 * method redefined from base class verifies whether given operator is valid for
1815 * this one (i.e. can be started "above" this operator). In current implementation method
1816 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1817 * elements.
1818 */
1819 //================================================================================
1820 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1821 {
1822   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1823 }
1824
1825 //================================================================================
1826 /*!
1827  * \brief SLOT. Is called when the user selects a way of geometry selection
1828   * \param theByMesh - true if the user wants to find geometry by mesh element
1829  */
1830 //================================================================================
1831
1832 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
1833 {
1834   if ( theByMesh ) {
1835     if ( !myShapeByMeshOp ) {
1836       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
1837       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
1838               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
1839       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
1840               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
1841     }
1842     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
1843     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1844     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
1845       SMESH::SMESH_Mesh_var aMeshVar =
1846         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1847       if ( !aMeshVar->_is_nil() ) {
1848         myDlg->hide(); // stop processing selection
1849         myShapeByMeshOp->setModule( getSMESHGUI() );
1850         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
1851         myShapeByMeshOp->SetMesh( aMeshVar );
1852         myShapeByMeshOp->start();
1853       }
1854     }
1855   }
1856 }
1857
1858 //================================================================================
1859 /*!
1860  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1861  */
1862 //================================================================================
1863
1864 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
1865 {
1866   if ( myShapeByMeshOp == op ) {
1867     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
1868     myDlg->show();
1869     // Select a found geometry object
1870     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
1871     if ( !aGeomVar->_is_nil() )
1872     {
1873       QString ID = aGeomVar->GetStudyEntry();
1874       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.latin1() )) {
1875         selectObject( aGeomSO );
1876         selectionDone();
1877       }
1878     }
1879   }
1880 }
1881
1882 //================================================================================
1883 /*!
1884  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
1885  */
1886 //================================================================================
1887
1888 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
1889 {
1890   if ( myShapeByMeshOp == op && myDlg ) {
1891     myDlg->show();
1892   }
1893 }
1894
1895 //================================================================================
1896 /*!
1897  * \brief Selects a SObject
1898   * \param theSObj - the SObject to select
1899  */
1900 //================================================================================
1901
1902 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
1903 {
1904   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
1905     SALOME_ListIO anIOList;
1906     if ( theSObj ) {
1907       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1908         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
1909       anIOList.Append( anIO );
1910     }
1911     sm->setSelectedObjects( anIOList, false );
1912   }
1913 }