Salome HOME
23586: [EDF] HYDRO: Copy mesh to new geometry
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SMESHGUI_MeshOp.cxx
23 //  Author : Sergey LITONIN, Open CASCADE S.A.S.
24
25 // SMESH includes
26 #include "SMESHGUI_MeshOp.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_GEOMGenUtils.h"
30 #include "SMESHGUI_Hypotheses.h"
31 #include "SMESHGUI_HypothesesUtils.h"
32 #include "SMESHGUI_MeshDlg.h"
33 #include "SMESHGUI_Operations.h"
34 #include "SMESHGUI_ShapeByMeshDlg.h"
35 #include "SMESHGUI_Utils.h"
36 #include "SMESH_NumberFilter.hxx"
37 #include "SMESH_TypeFilter.hxx"
38
39 // SALOME GEOM includes
40 #include <GEOM_SelectionFilter.h>
41 #include <GEOMBase.h>
42 #include <GeometryGUI.h>
43 #include <GEOM_wrap.hxx>
44 #include <GEOMImpl_Types.hxx>
45
46 // SALOME GUI includes
47 #include <SalomeApp_Tools.h>
48 #include <SalomeApp_Application.h>
49 #include <LightApp_Application.h>
50 #include <LightApp_SelectionMgr.h>
51 #include <LightApp_UpdateFlags.h>
52 #include <SUIT_MessageBox.h>
53 #include <SUIT_OverrideCursor.h>
54 #include <SUIT_Session.h>
55 #include <SALOME_InteractiveObject.hxx>
56 #include <SALOME_ListIO.hxx>
57
58 // SALOME KERNEL includes
59 #include <SALOMEDS_SComponent.hxx>
60 #include <SALOMEDS_SObject.hxx>
61 #include <SALOMEDS_Study.hxx>
62 #include <SALOMEDS_wrap.hxx>
63
64 // Qt includes
65 #include <QStringList>
66 #include <QLineEdit>
67 #include <QApplication>
68
69 // OCCT includes
70 #include <TopoDS.hxx>
71 #include <TopoDS_Shape.hxx>
72 #include <TopoDS_Shell.hxx>
73 #include <TopExp_Explorer.hxx>
74 #include <BRep_Tool.hxx>
75
76 // IDL includes
77 #include <SALOMEconfig.h>
78 #include CORBA_CLIENT_HEADER(SMESH_Gen)
79
80 //================================================================================
81 /*!
82  * \brief Constructor
83   * \param theToCreate - if this parameter is true then operation is used for creation,
84   * for editing otherwise
85  *
86  * Initialize operation
87 */
88 //================================================================================
89 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
90 : SMESHGUI_SelectionOp(),
91   myDlg( 0 ),
92   myShapeByMeshOp( 0 ),
93   myToCreate( theToCreate ),
94   myIsMesh( theIsMesh ),
95   myIsInvalidSubMesh( false ),
96   myHypoSet( 0 )
97 {
98   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
99     GeometryGUI::InitGeomGen();
100   myIsOnGeometry = true;
101   myMaxShapeDim = -1;
102 }
103
104 //================================================================================
105 /*!
106  * \brief Destructor
107 */
108 //================================================================================
109 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
110 {
111   if ( myDlg )
112     delete myDlg;
113 }
114
115 //================================================================================
116 /*!
117  * \brief Gets dialog of this operation
118   * \retval LightApp_Dialog* - pointer to dialog of this operation
119 */
120 //================================================================================
121 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
122 {
123   return myDlg;
124 }
125
126 //================================================================================
127 /*!
128  * \brief Creates or edits mesh
129   * \retval bool - TRUE if operation is performed successfully, FALSE otherwise
130  *
131  * Virtual slot redefined from the base class called when "Apply" button is clicked
132  * creates or edits mesh
133  */
134 //================================================================================
135 bool SMESHGUI_MeshOp::onApply()
136 {
137   if (isStudyLocked())
138     return false;
139
140   QString aMess;
141   if ( !isValid( aMess ) )
142   {
143     dlg()->show();
144     if ( aMess != "" )
145       SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), aMess );
146     return false;
147   }
148
149   bool aResult = false;
150   aMess = "";
151   try
152   {
153     QStringList anEntryList;
154     if ( myToCreate && myIsMesh )
155       aResult = createMesh( aMess, anEntryList );
156     if (( myToCreate && !myIsMesh ) || myIsInvalidSubMesh )
157       aResult = createSubMesh( aMess, anEntryList );
158     else if ( !myToCreate )
159       aResult = editMeshOrSubMesh( aMess );
160     if ( aResult )
161     {
162       SMESHGUI::Modified();
163       update( UF_ObjBrowser | UF_Model );
164       if( LightApp_Application* anApp =
165           dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
166         myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
167     }
168   }
169   catch ( const SALOME::SALOME_Exception& S_ex )
170   {
171     SalomeApp_Tools::QtCatchCorbaException( S_ex );
172     aResult = false;
173   }
174   catch ( ... )
175   {
176     aResult = false;
177   }
178
179   if ( aResult )
180   {
181     if ( myToCreate )
182       setDefaultName();
183   }
184   else
185   {
186     if ( aMess == "" )
187       aMess = tr( "SMESH_OPERATION_FAILED" );
188     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), aMess );
189   }
190
191   return aResult;
192 }
193
194 //================================================================================
195 /*!
196  * \brief Creates dialog if necessary and shows it
197  *
198  * Virtual method redefined from base class called when operation is started creates
199  * dialog if necessary and shows it, activates selection
200  */
201 //================================================================================
202 void SMESHGUI_MeshOp::startOperation()
203 {
204   myIgnoreAlgoSelection = false;
205
206   if (!myDlg)
207   {
208     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
209     for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
210     {
211       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
212                this, SLOT( onCreateHyp( const int, const int ) ) );
213       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
214                this, SLOT( onEditHyp( const int, const int ) ) );
215       connect( myDlg->tab( i ), SIGNAL( selectAlgo( const int ) ),
216                this, SLOT( onAlgoSelected( const int ) ) );
217     }
218     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
219     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
220     connect( myDlg, SIGNAL( selectMeshType( const int, const int ) ), SLOT( onAlgoSetByMeshType( const int, const int)));
221     if ( myToCreate ) {
222       if ( myIsMesh ) myHelpFileName = "constructing_meshes.html";
223       else            myHelpFileName = "constructing_submeshes.html";
224     }
225     else {
226       myHelpFileName = "editing_meshes.html";
227     }
228   }
229   SMESHGUI_SelectionOp::startOperation();
230   // iterate through dimensions and get available algorithms, set them to the dialog
231   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
232   for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
233   {
234     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
235     QStringList hypList;
236     // clear available hypotheses
237     aTab->setAvailableHyps( MainHyp, hypList );
238     aTab->setAvailableHyps( AddHyp, hypList );
239     aTab->setExistingHyps( MainHyp, hypList );
240     aTab->setExistingHyps( AddHyp, hypList );
241     myExistingHyps[ i ][ MainHyp ].clear();
242     myExistingHyps[ i ][ AddHyp ].clear();
243     // set algos
244     availableHyps( i, Algo, hypList, myAvailableHypData[i][Algo] );
245     aTab->setAvailableHyps( Algo, hypList );
246   }
247   if ( myToCreate )
248   {
249     setDefaultName();
250     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
251   }
252   else
253   {
254     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
255   }
256   myDlg->setCurrentTab( SMESH::DIM_3D );
257
258   QStringList TypeMeshList;
259   createMeshTypeList( TypeMeshList );
260   setAvailableMeshType( TypeMeshList );
261
262   myDlg->show();
263   myDlg->setGeomPopupEnabled(false);
264   selectionDone();
265
266   myHasConcurrentSubBefore = false;
267
268   myObjectToSelect.clear();
269 }
270
271 //=================================================================================
272 /*!
273  * \brief Selects a recently created mesh or sub-mesh if necessary
274  *
275  * Virtual method redefined from base class called when operation is committed
276  * selects a recently created mesh or sub-mesh if necessary. Allows to perform
277  * selection when the custom selection filters are removed.
278  */
279 //=================================================================================
280 void SMESHGUI_MeshOp::commitOperation()
281 {
282   SMESHGUI_SelectionOp::commitOperation();
283
284   if ( !myObjectToSelect.isEmpty() ) {
285     if ( LightApp_SelectionMgr* aSelectionMgr = selectionMgr() ) {
286       SUIT_DataOwnerPtrList aList;
287       aList.append( new LightApp_DataOwner( myObjectToSelect ) );
288       aSelectionMgr->setSelected( aList );
289     }
290   }
291 }
292
293 //================================================================================
294 /*!
295  * \brief Creates selection filter
296   * \param theId - identifier of current selection widget
297   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
298  *
299  * Creates selection filter in accordance with identifier of current selection widget
300  */
301 //================================================================================
302 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
303 {
304   if ( theId == SMESHGUI_MeshDlg::Geom )
305   {
306 //     TColStd_MapOfInteger allTypesMap;
307 //     for ( int i = 0; i < 10; i++ )
308 //       allTypesMap.Add( i );
309 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
310     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
311   }
312   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
313     return new SMESH_TypeFilter( SMESH::MESHorSUBMESH );
314   else if ( theId == SMESHGUI_MeshDlg::Mesh )
315     return new SMESH_TypeFilter( SMESH::MESH );
316   else
317     return 0;
318 }
319
320 //================================================================================
321 /*!
322  * \brief check if selected shape is a sub-shape of the shape to mesh
323   * \retval bool - check result
324  */
325 //================================================================================
326 bool SMESHGUI_MeshOp::isSubshapeOk() const
327 {
328   if ( !myToCreate || myIsMesh ) // not submesh creation
329     return false;
330
331   // mesh
332   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
333   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
334   if (!pMesh) return false;
335
336   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
337   if (mesh->_is_nil()) return false;
338
339   // main shape of the mesh
340   GEOM::GEOM_Object_var mainGeom = mesh->GetShapeToMesh();
341   if (mainGeom->_is_nil()) return false;
342
343   // geometry
344   QStringList aGEOMs;
345   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
346
347   if (aGEOMs.count() > 0) {
348     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
349     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
350     if (geomGen->_is_nil() || !aStudy) return false;
351
352     GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations(aStudy->StudyId());
353     if (op->_is_nil()) return false;
354
355     // check all selected shapes
356     QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
357     for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++) {
358       QString aSubGeomEntry = (*aSubShapesIter);
359       _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
360       if (!pSubGeom) return false;
361
362       GEOM::GEOM_Object_var aSubGeomVar =
363         GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
364       if (aSubGeomVar->_is_nil()) return false;
365
366       // skl for NPAL14695 - implementation of searching of mainObj
367       GEOM::GEOM_Object_var mainObj = op->GetMainShape(aSubGeomVar); /* _var not _wrap as
368                                                                         mainObj already exists! */
369       while( !mainObj->_is_nil()) {
370         CORBA::String_var entry1 = mainObj->GetEntry();
371         CORBA::String_var entry2 = mainGeom->GetEntry();
372         if (std::string( entry1.in() ) == entry2.in() )
373           return true;
374         mainObj = op->GetMainShape(mainObj);
375       }
376       if ( aSubGeomVar->GetShapeType() == GEOM::COMPOUND )
377       {
378         // is aSubGeomVar a compound of sub-shapes?
379         GEOM::GEOM_IShapesOperations_wrap sop = geomGen->GetIShapesOperations(aStudy->StudyId());
380         if (sop->_is_nil()) return false;
381         GEOM::ListOfLong_var ids = sop->GetAllSubShapesIDs( aSubGeomVar,
382                                                             GEOM::SHAPE,/*sorted=*/false);
383         if ( ids->length() > 0 )
384         {
385           ids->length( 1 );
386           GEOM::GEOM_Object_var compSub = geomGen->AddSubShape( aSubGeomVar, ids );
387           if ( !compSub->_is_nil() )
388           {
389             GEOM::ListOfGO_var shared = sop->GetSharedShapes( mainGeom,
390                                                               compSub,
391                                                               compSub->GetShapeType() );
392             geomGen->RemoveObject( compSub );
393             compSub->UnRegister();
394             if ( shared->length() > 0 ) {
395               geomGen->RemoveObject( shared[0] );
396               shared[0]->UnRegister();
397             }
398             return ( shared->length() > 0 );
399           }
400         }
401       }
402     }
403   }
404
405   return false;
406 }
407
408 //================================================================================
409 /*!
410  * \brief Return name of the algorithm that does not support sub-meshes and makes
411  * sub-mesh creation useless
412  *  \retval char* - string is to be deleted!!!
413  */
414 //================================================================================
415 char* SMESHGUI_MeshOp::isSubmeshIgnored() const
416 {
417   if ( myToCreate && !myIsMesh ) {
418
419     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
420     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
421     _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
422     if ( pMesh ) {
423
424       QStringList algoNames;
425       THypList    algoList;
426       existingHyps(3, Algo, pMesh, algoNames, algoList);
427       if (!algoList.empty()) {
428         HypothesisData* algo =
429           SMESH::GetHypothesisData( SMESH::toQStr( algoList[0].first->GetName() ));
430         if ( algo &&
431              algo->InputTypes.empty() && // builds all dimensions it-self
432              !algo->IsSupportSubmeshes )
433           return CORBA::string_dup( algoNames[0].toLatin1().data() );
434       }
435
436 //       GEOM::GEOM_Object_var geom;
437 //       if (_PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() ))
438 //         geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
439
440 //       if ( !geom->_is_nil() && geom->GetShapeType() >= GEOM::FACE ) { // WIRE, EDGE as well
441         existingHyps(2, Algo, pMesh, algoNames, algoList);
442         if (!algoList.empty()) {
443           HypothesisData* algo =
444             SMESH::GetHypothesisData( SMESH::toQStr( algoList[0].first->GetName() ));
445           if ( algo &&
446                algo->InputTypes.empty() && // builds all dimensions it-self
447                !algo->IsSupportSubmeshes )
448             return CORBA::string_dup( algoNames[0].toLatin1().data() );
449         }
450 //       }
451     }
452   }
453   return 0;
454 }
455
456 //================================================================================
457 /*!
458  * \brief find an existing submesh by the selected shape
459  * \retval _PTR(SObject) - the found submesh SObject
460  */
461 //================================================================================
462 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
463 {
464   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
465   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
466   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
467   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
468   if ( pMesh && pGeom ) {
469     GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
470     if ( !geom->_is_nil() ) {
471       int tag = -1;
472       switch ( geom->GetShapeType() ) {
473       case GEOM::VERTEX:   tag = SMESH::Tag_SubMeshOnVertex;   break;
474       case GEOM::EDGE:     tag = SMESH::Tag_SubMeshOnEdge;     break;
475       case GEOM::WIRE:     tag = SMESH::Tag_SubMeshOnWire;     break;
476       case GEOM::FACE:     tag = SMESH::Tag_SubMeshOnFace;     break;
477       case GEOM::SHELL:    tag = SMESH::Tag_SubMeshOnShell;    break;
478       case GEOM::SOLID:    tag = SMESH::Tag_SubMeshOnSolid;    break;
479       case GEOM::COMPOUND: tag = SMESH::Tag_SubMeshOnCompound; break;
480       default:;
481       }
482       _PTR(GenericAttribute) anAttr;
483       _PTR(SObject) aSubmeshRoot;
484       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
485       if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
486       {
487         _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
488         for ( ; smIter->More(); smIter->Next() )
489         {
490           _PTR(SObject) aSmObj = smIter->Value();
491           if ( ! aSmObj->FindAttribute( anAttr, "AttributeIOR" ))
492             continue;
493           _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
494           for ( ; anIter1->More(); anIter1->Next()) {
495             _PTR(SObject) pGeom2 = anIter1->Value();
496             if ( pGeom2->ReferencedObject( pGeom2 ) &&
497                  pGeom2->GetID() == pGeom->GetID() )
498               return aSmObj;
499           }
500         }
501       }
502     }
503   }
504   return _PTR(SObject)();
505 }
506
507 //================================================================================
508 /*!
509  * \brief Updates dialog's look and feel
510  *
511  * Virtual method redefined from the base class updates dialog's look and feel
512  */
513 //================================================================================
514 void SMESHGUI_MeshOp::selectionDone()
515 {
516   if (!dlg()->isVisible() || !myDlg->isEnabled())
517     return;
518
519   SMESHGUI_SelectionOp::selectionDone();
520
521   try
522   {
523     myIsOnGeometry = true;
524     myIsInvalidSubMesh = false;
525
526     //Check geometry for mesh
527     QString anObjEntry = myDlg->selectedObject(SMESHGUI_MeshDlg::Obj);
528     _PTR(SObject) pObj = studyDS()->FindObjectID(anObjEntry.toLatin1().data());
529     if (pObj)
530     {
531       SMESH::SMESH_Mesh_var aMeshVar =
532         SMESH::SMESH_Mesh::_narrow(_CAST(SObject,pObj)->GetObject());
533       if (!aMeshVar->_is_nil()) {
534         if (!myToCreate && !aMeshVar->HasShapeToMesh())
535           myIsOnGeometry = false;
536       }
537     }
538
539     if (myIsOnGeometry)
540     {
541       // Enable tabs according to shape dimension
542
543       int shapeDim = 3;
544
545       QStringList aGEOMs;
546       myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
547       GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
548
549       if (aGEOMs.count() > 0) {
550         // one or more GEOM shape selected
551         aSeq->length(aGEOMs.count());
552         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
553         int iSubSh = 0;
554         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
555           QString aSubGeomEntry = (*aSubShapesIter);
556           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
557          
558           if( pSubGeom ) { 
559             SALOMEDS_SObject* sobj = _CAST(SObject,pSubGeom);
560             if( sobj ) {
561               GEOM::GEOM_Object_var aSubGeomVar =
562                 GEOM::GEOM_Object::_narrow(sobj->GetObject());
563               if( !aSubGeomVar->_is_nil() ){
564                 aSeq[iSubSh] = aSubGeomVar;
565               }
566             }
567           }
568         }
569       } else {
570         // get geometry by selected sub-mesh
571         QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
572         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
573         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
574         if (!aGeomVar->_is_nil()) {
575           aSeq->length(1);
576           aSeq[0] = aGeomVar;
577         }
578       }
579
580       if (aSeq->length() > 0) {
581         shapeDim = -1;
582         for ( CORBA::ULong iss = 0; iss < aSeq->length() && shapeDim < 3; iss++) {
583           GEOM::GEOM_Object_var aGeomVar = aSeq[iss];
584           switch ( aGeomVar->GetShapeType() ) {
585           case GEOM::SOLID:  shapeDim = 3; break;
586           case GEOM::SHELL:
587             // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
588             // {
589             //   TopoDS_Shape aShape;
590             //   bool isClosed = GEOMBase::GetShape(aGeomVar, aShape) && /*aShape.Closed()*/BRep_Tool::IsClosed(aShape);
591             //   shapeDim = qMax(isClosed ? 3 : 2, shapeDim);
592             // }
593             // break;
594           case GEOM::FACE:   shapeDim = qMax(2, shapeDim); break;
595           case GEOM::WIRE:
596           case GEOM::EDGE:   shapeDim = qMax(1, shapeDim); break;
597           case GEOM::VERTEX: shapeDim = qMax(0, shapeDim); break;
598           default:
599             {
600               TopoDS_Shape aShape;
601               if (GEOMBase::GetShape(aGeomVar, aShape))
602               {
603                 TopExp_Explorer exp (aShape, TopAbs_SOLID);
604                 if (exp.More()) {
605                   shapeDim = 3;
606                 }
607                 // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
608                 // else if ( exp.Init( aShape, TopAbs_SHELL ), exp.More() )
609                 // {
610                 //   shapeDim = 2;
611                 //   for (; exp.More() && shapeDim == 2; exp.Next()) {
612                 //     if (/*exp.Current().Closed()*/BRep_Tool::IsClosed(exp.Current()))
613                 //       shapeDim = 3;
614                 //   }
615                 // }
616                 else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
617                   shapeDim = qMax(2, shapeDim);
618                 else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
619                   shapeDim = qMax(1, shapeDim);
620                 else if ( exp.Init( aShape, TopAbs_VERTEX ), exp.More() )
621                   shapeDim = qMax(0, shapeDim);
622               }
623             }
624           }
625           if ( shapeDim == 3 )
626             break;
627         }
628       }
629       for (int i = SMESH::DIM_3D; i > shapeDim; i--) {
630         // reset algos before disabling tabs (0020138)
631         onAlgoSelected(-1, i);
632       }
633       myDlg->setMaxHypoDim( shapeDim );
634       myMaxShapeDim = shapeDim;
635       myDlg->setHypoSets( SMESH::GetHypothesesSets( shapeDim ));
636
637       if (!myToCreate) // edition: read hypotheses
638       {
639         if (pObj != 0)
640         {
641           SMESH::SMESH_subMesh_var submeshVar =
642             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
643           myIsMesh = submeshVar->_is_nil();
644           myIsInvalidSubMesh = ( !myIsMesh && submeshVar->GetId() < 1 );
645           myDlg->setTitile( myToCreate, myIsMesh );
646           myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !submeshVar->_is_nil() );
647           myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, true );
648           myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
649           if ( myIsInvalidSubMesh )
650           {
651             // it is necessary to select a new geometry
652             myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->show();
653             myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
654           }
655           else
656           {
657             myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->hide();
658           }
659           myDlg->updateGeometry();
660           myDlg->adjustSize();
661           readMesh();
662         }
663         else
664           myDlg->reset();
665       }
666       else if ( !myIsMesh ) // submesh creation
667       {
668         // if a submesh on the selected shape already exist, pass to submesh edition mode
669         if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
670           SMESH::SMESH_subMesh_var sm =
671             SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
672           bool editSubmesh = ( !sm->_is_nil() &&
673                                SUIT_MessageBox::question( myDlg, tr( "SMESH_WARNING" ),
674                                                           tr( "EDIT_SUBMESH_QUESTION"),
675                                                           SUIT_MessageBox::Yes |
676                                                           SUIT_MessageBox::No,
677                                                           SUIT_MessageBox::No )
678                                == SUIT_MessageBox::Yes );
679           if ( editSubmesh )
680           {
681             selectionMgr()->clearFilters();
682             selectObject( pSubmesh );
683             SMESHGUI::GetSMESHGUI()->switchToOperation( SMESHOp::OpEditMeshOrSubMesh );
684             return;
685           }
686           else
687           {
688             myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
689             selectObject( _PTR(SObject)() );
690             selectionDone();
691             return;
692           }
693         }
694         // discard selected mesh if submesh creation not allowed because of
695         // a global algorithm that does not support submeshes
696         if ( char* algoName = isSubmeshIgnored() ) {
697           SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ),
698                                     tr("SUBMESH_NOT_ALLOWED").arg(algoName));
699           CORBA::string_free( algoName );
700           myDlg->selectObject( "", SMESHGUI_MeshDlg::Mesh, "" );
701           selectObject( _PTR(SObject)() );
702           selectionDone();
703           return;
704         }
705
706         // enable/disable popup for choice of geom selection way
707         bool enable = false;
708         QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
709         if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
710           SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
711           if ( !mesh->_is_nil() ) {
712             //rnv: issue 21056: EDF 1608 SMESH: Dialog Box "Create Sub Mesh": focus should automatically switch to geometry
713             QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
714             _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
715             if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
716               myDlg->activateObject(SMESHGUI_MeshDlg::Geom);
717             enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
718           }
719         }
720         myDlg->setGeomPopupEnabled( enable );
721       }
722     }
723     else { // no geometry defined
724
725       QStringList hypList;
726       for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
727       {
728         availableHyps( dim, Algo, hypList, myAvailableHypData[dim][Algo]);
729         myDlg->tab( dim )->setAvailableHyps( Algo, hypList );
730         if ( hypList.empty() ) myDlg->disableTab( dim );
731         else                   myDlg->enableTab( dim );
732       }
733       myMaxShapeDim = -1;
734       //Hide labels and fields (Mesh and Geometry)
735       myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, false );
736       myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, false );
737       myDlg->adjustSize();
738       readMesh();
739     }
740     int curIndex = myDlg->currentMeshType( );
741     QStringList TypeMeshList;
742     createMeshTypeList( TypeMeshList );
743     setAvailableMeshType( TypeMeshList );
744     curIndex =( curIndex >= TypeMeshList.count() ) ? 0 : curIndex;
745     myDlg->setCurrentMeshType( curIndex );
746     setFilteredAlgoData( myMaxShapeDim, curIndex);
747   }
748   catch ( const SALOME::SALOME_Exception& S_ex )
749   {
750     SalomeApp_Tools::QtCatchCorbaException( S_ex );
751   }
752   catch ( ... )
753   {
754   }
755 }
756
757 //================================================================================
758 /*!
759  * \brief Verifies validity of input data
760   * \param theMess - Output parameter intended for returning error message
761   * \retval bool  - TRUE if input data is valid, false otherwise
762  *
763  * Verifies validity of input data. This method is called when "Apply" or "OK" button
764  * is pressed before mesh creation or editing.
765  */
766 //================================================================================
767 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
768 {
769   // Selected object to be  edited
770   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
771   {
772     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
773     return false;
774   }
775
776   // Name
777   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj ).trimmed();
778   if ( aMeshName.isEmpty() )
779   {
780     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
781     return false;
782   }
783
784 /*  // Imported mesh, if create sub-mesh or edit mesh
785   if ( !myToCreate || ( myToCreate && !myIsMesh ))
786   {
787     QString aMeshEntry = myDlg->selectedObject
788       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
789     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
790       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
791       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
792         theMess = tr( "IMPORTED_MESH" );
793         return false;
794       }
795     }
796   }*/
797
798   // Geom
799   if ( myToCreate )
800   {
801     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
802     if ( aGeomEntry.isEmpty() )
803     {
804       theMess = tr( myIsMesh ?
805                     "GEOMETRY_OBJECT_IS_NOT_DEFINED_MESH" :
806                     "GEOMETRY_OBJECT_IS_NOT_DEFINED_SUBMESH");
807       if ( !myIsMesh )
808         return false;
809       dlg()->show();
810       if ( SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), theMess,
811            SUIT_MessageBox::Yes, SUIT_MessageBox::No ) == SUIT_MessageBox::No )
812       {
813         theMess = "";
814         return false;
815       }
816       return true;
817     }
818     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
819     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
820     {
821       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
822       return false;
823     }
824
825     // Mesh
826     if ( !myIsMesh ) // i.e sub-mesh creation,
827     {
828       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
829       if ( aMeshEntry == "" )
830       {
831         theMess = tr( "MESH_IS_NOT_DEFINED" );
832         return false;
833       }
834       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
835       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
836       {
837         theMess = tr( "MESH_IS_NULL" );
838         return false;
839       }
840       if ( !isSubshapeOk() )
841       {
842         theMess = tr( "INVALID_SUBSHAPE" );
843         return false;
844       }
845     }
846   }
847
848   return true;
849 }
850
851 //================================================================================
852 /*!
853  * \brief check compatibility of the algorithm and another algorithm or hypothesis
854   * \param theAlgoData - algorithm data
855   * \param theHypData - hypothesis data
856   * \param theHypType - hypothesis type
857   * \param theHypTypeName - hypothesis type name, must be provided if 2-nd arg is not algo
858   * \retval bool - check result
859  */
860 //================================================================================
861 static bool isCompatible(const HypothesisData* theAlgoData,
862                          const HypothesisData* theHypData,
863                          const int             theHypType)
864 {
865   if ( !theAlgoData )
866     return true;
867
868   if ( theHypType == SMESHGUI_MeshOp::Algo )
869     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
870
871   bool isOptional;
872   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
873 }
874
875 //================================================================================
876 /*!
877  * \brief check compatibility of the geometry
878   * \param theAlgoData - to select hypos able to be used by this algo
879   * \param theCurrentGeomToSelect - the current name of the selected geometry
880   * \param theGeomVar - currently selected geometry
881   * \retval bool - check result
882  */
883 //================================================================================
884 bool SMESHGUI_MeshOp::isCompatibleToGeometry(HypothesisData* theAlgoData,
885                                              QString theCurrentGeomToSelect,
886                                              GEOM::GEOM_Object_var theGeomVar)
887 {
888   if ( theGeomVar->_is_nil() )
889     return true;
890
891   bool isApplicable = false;
892   if ( theCurrentGeomToSelect == myLastGeomToSelect && !theCurrentGeomToSelect.isEmpty() ) {
893     THypLabelIsAppMap::const_iterator iter = myHypMapIsApplicable.find( theAlgoData->Label );
894     if ( iter != myHypMapIsApplicable.end() && iter.key() == theAlgoData->Label ) {
895       isApplicable = iter.value();
896       return isApplicable;
897     }
898   }
899   bool toCheckIsApplicableToAll = !myIsMesh;
900   if ( toCheckIsApplicableToAll )
901     toCheckIsApplicableToAll = ( theGeomVar->GetType() == GEOM_GROUP );
902   isApplicable = SMESH::IsApplicable( theAlgoData->TypeName, theGeomVar, toCheckIsApplicableToAll );
903   myHypMapIsApplicable.insert( theAlgoData->Label, isApplicable );
904   return isApplicable;
905 }
906
907 //================================================================================
908 /*!
909  * \brief check compatibility of the mesh type
910   * \param theAlgoData - to select hypos able to be used by this algo
911   * \param theMeshType - type of mesh for filtering algorithms
912   * \retval bool - check result
913  */
914 //================================================================================
915 bool SMESHGUI_MeshOp::isCompatibleToMeshType(HypothesisData* theAlgoData,
916                                              QString theMeshType)
917 {
918   bool isAvailableAlgo = ( theAlgoData->OutputTypes.count() == 0 );
919   QStringList::const_iterator inElemType = theAlgoData->OutputTypes.begin();
920   for ( ; inElemType != theAlgoData->OutputTypes.end(); inElemType++ ) {
921     if ( *inElemType == theMeshType ) {
922       isAvailableAlgo = true;
923       break;
924     }
925   }
926   return isAvailableAlgo;
927 }
928
929 //================================================================================
930 /*!
931  * \brief Gets available hypotheses or algorithms
932   * \param theDim - specifies dimension of returned hypotheses/algorifms
933   * \param theHypType - specifies whether algorims or hypotheses or additional ones
934   * are retrieved (possible values are in HypType enumeration)
935   * \param theHyps - Output list of hypotheses' names
936   * \param thePrevAlgoData - to select hypos able to be used by previously algo (optional)
937   * \param theNextAlgoData - to select hypos able to be used by next algo (optional)
938   * \param theMeshType - type of mesh for filtering algorithms (optional)
939  *
940  * Gets available hypotheses or algorithm in accordance with input parameters
941  */
942 //================================================================================
943 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
944                                      const int       theHypType,
945                                      QStringList&    theHyps,
946                                      THypDataList&   theDataList,
947                                      HypothesisData* thePrevAlgoData,
948                                      HypothesisData* theNextAlgoData,
949                                      const QString&  theMeshType)
950 {
951   theDataList.clear();
952   theHyps.clear();
953   bool isAlgo = ( theHypType == Algo );
954   bool isAux  = ( theHypType >= AddHyp );
955   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux, myIsOnGeometry, !myIsMesh );
956
957   GEOM::GEOM_Object_var aGeomVar;
958   QString aCurrentGeomToSelect;
959   if ( !theMeshType.isEmpty() ) {
960     aCurrentGeomToSelect = myDlg->selectedObject( myToCreate ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Obj );
961     if ( _PTR(SObject) so = studyDS()->FindObjectID( aCurrentGeomToSelect.toLatin1().data() )) {
962       aGeomVar = SMESH::GetGeom( so );
963     }
964    if ( aCurrentGeomToSelect != myLastGeomToSelect )
965      myHypMapIsApplicable.clear();
966   }
967
968   std::multimap< double, HypothesisData* > sortedHyps;
969   QStringList::const_iterator anIter;
970   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
971   {
972     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
973     if ( ( isCompatible ( thePrevAlgoData, aData, theHypType ) &&
974            isCompatible ( theNextAlgoData, aData, theHypType ) ) ||
975            ( theMeshType == "ANY" && aData->InputTypes.isEmpty()))
976     {
977       if ( ( !theMeshType.isEmpty() )  &&
978            ( theDim >= SMESH::DIM_2D ) &&
979            ( ( theMeshType != "ANY" && !isCompatibleToMeshType( aData, theMeshType )) ||
980              !isCompatibleToGeometry( aData, aCurrentGeomToSelect, aGeomVar )))
981         continue;
982
983       int  groupID = aData->GroupID;
984       int priority = aData->Priority;
985       if ( groupID  < 0 || groupID > 9    ) groupID  = 9;
986       if ( priority < 0 || priority > 999 ) priority = 999;
987
988       sortedHyps.insert( std::make_pair( groupID + priority * 1e-3, aData ));
989     }
990   }
991
992   if ( !sortedHyps.empty() )
993   {
994     HypothesisData* aPrevGroup = SMESH::GetGroupTitle( sortedHyps.rbegin()->second, isAlgo );
995     std::multimap< double, HypothesisData* >::iterator key_hyp = sortedHyps.begin();
996     for ( ; key_hyp != sortedHyps.end(); ++key_hyp )
997     {
998       HypothesisData*  aData = key_hyp->second;
999       HypothesisData* aGroup = SMESH::GetGroupTitle( aData, isAlgo );
1000       if ( aPrevGroup != aGroup )
1001       {
1002         theDataList.append( aGroup );
1003         theHyps.append( aGroup->Label );
1004         aPrevGroup = aGroup;
1005       }
1006       theDataList.append( aData );
1007       theHyps.append( aData->Label );
1008     }
1009   }
1010
1011   if ( !theMeshType.isEmpty() && !aCurrentGeomToSelect.isEmpty() )
1012     myLastGeomToSelect = aCurrentGeomToSelect;
1013 }
1014
1015 //================================================================================
1016 /*!
1017  * \brief Gets existing hypotheses or algorithms
1018  *  \param theDim - specifies dimension of returned hypotheses/algorifms
1019  *  \param theHypType - specifies whether algorims or hypotheses or additional ones
1020  *  are retrieved (possible values are in HypType enumeration)
1021  *  \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
1022  *  \param theHyps - output list of names.
1023  *  \param theHypVars - output list of variables.
1024  *  \param theAlgoData - to select hypos able to be used by this algo (optional)
1025  *
1026  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
1027  * input parameters.
1028  *
1029  * WARNING: when using this method to get hyps existing in Mesh component,
1030  *          call availableHyps() before in order to get only hyps of available types
1031  *          that was filtered by availableHyps()
1032  */
1033 //================================================================================
1034 void SMESHGUI_MeshOp::existingHyps( const int       theDim,
1035                                     const int       theHypType,
1036                                     _PTR(SObject)   theFather,
1037                                     QStringList&    theHyps,
1038                                     THypList&       theHypList,
1039                                     HypothesisData* theAlgoData) const
1040 {
1041   // Clear hypoheses list
1042   theHyps.clear();
1043   theHypList.clear();
1044
1045   if ( !theFather )
1046     return;
1047
1048   _PTR(SObject)          aHypRoot;
1049   _PTR(GenericAttribute) anAttr;
1050   _PTR(AttributeName)    aName;
1051   _PTR(AttributeIOR)     anIOR;
1052
1053   const bool isMesh = !_CAST( SComponent, theFather );
1054   int aPart = -1;
1055   if ( isMesh )
1056     aPart = theHypType == Algo ? SMESH::Tag_RefOnAppliedAlgorithms : SMESH::Tag_RefOnAppliedHypothesis;
1057   else
1058     aPart = theHypType == Algo ? SMESH::Tag_AlgorithmsRoot : SMESH::Tag_HypothesisRoot;
1059
1060   const bool isAux   = ( theHypType >= AddHyp );
1061   const bool allHyps = ( !isMesh && theHypType != Algo && theDim > -1);
1062
1063   if ( theFather->FindSubObject( aPart, aHypRoot ) )
1064   {
1065     _PTR(ChildIterator) anIter =
1066       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
1067     for ( ; anIter->More(); anIter->Next() )
1068     {
1069       _PTR(SObject) anObj = anIter->Value();
1070       if ( isMesh ) // i.e. mesh or submesh
1071       {
1072         _PTR(SObject) aRefObj;
1073         if ( anObj->ReferencedObject( aRefObj ) )
1074           anObj = aRefObj;
1075         else
1076           continue;
1077       }
1078       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
1079       {
1080         aName = anAttr;
1081         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
1082         if ( !CORBA::is_nil( aVar ) )
1083         {
1084           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
1085           if ( !aHypVar->_is_nil() )
1086           {
1087             CORBA::String_var hypType = aHypVar->GetName();
1088             HypothesisData* aData = SMESH::GetHypothesisData( hypType.in() );
1089             if ( !aData) continue;
1090             if (( theDim == -1 || aData->Dim.contains( theDim ) ) &&
1091                 ( isCompatible ( theAlgoData, aData, theHypType )) &&
1092                 ( theHypType == Algo || isAux == aData->IsAuxOrNeedHyp ) &&
1093                 ( !allHyps || myAvailableHypData[theDim][theHypType].count(aData) ))
1094             {
1095               std::string aHypName = aName->Value();
1096               theHyps.append( aHypName.c_str() );
1097               theHypList.append( THypItem( aHypVar, aHypName.c_str() ) );
1098             }
1099           }
1100         }
1101       }
1102     }
1103   }
1104 }
1105
1106 //================================================================================
1107 /*!
1108  * \brief If create or edit a submesh, return a hypothesis holding parameters used
1109  *        to mesh a sub-shape
1110   * \param aHypType - The hypothesis type name
1111   * \param aServerLib - Server library name
1112   * \param hypData - The structure holding the hypothesis type etc.
1113   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
1114  */
1115 //================================================================================
1116 SMESH::SMESH_Hypothesis_var
1117 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
1118                                           const QString& aServerLib ) const
1119 {
1120   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
1121     return SMESH::SMESH_Hypothesis::_nil();
1122
1123   const int nbColonsInMeshEntry = 3;
1124   bool isSubMesh = myToCreate ?
1125     !myIsMesh :
1126     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).count(':') > nbColonsInMeshEntry;
1127
1128   // get mesh and geom object
1129   SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
1130   GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
1131
1132   QString anEntry;
1133   if ( isSubMesh )
1134   {
1135     anEntry = myDlg->selectedObject
1136       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
1137     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
1138     {
1139       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
1140       if ( myToCreate ) // mesh and geom may be selected
1141       {
1142         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
1143         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1144         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
1145           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1146       }
1147       else // edition: sub-mesh may be selected
1148       {
1149         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
1150         if ( !sm->_is_nil() ) {
1151           aMeshVar = sm->GetFather();
1152           aGeomVar = sm->GetSubShape();
1153         }
1154       }
1155     }
1156   }
1157   else // mesh
1158   {
1159     if ( !myToCreate ) // mesh to edit can be selected
1160     {
1161       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1162       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
1163       {
1164         aMeshVar = SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1165         if ( !aMeshVar->_is_nil() )
1166           aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pMesh );
1167       }
1168     }
1169     if ( aGeomVar->_is_nil() ) {
1170       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1171       if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
1172       {
1173         aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1174       }
1175     }
1176   }
1177
1178   SMESH::SMESH_Hypothesis_var hyp =
1179     SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
1180                                                            aServerLib.toLatin1().data(),
1181                                                            aMeshVar,
1182                                                            aGeomVar,
1183                                                            /*byMesh = */isSubMesh);
1184   if ( hyp->_is_nil() && isSubMesh )
1185     hyp = SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
1186                                                                  aServerLib.toLatin1().data(),
1187                                                                  aMeshVar,
1188                                                                  aGeomVar,
1189                                                                  /*byMesh = */false);
1190   return hyp;
1191 }
1192
1193 //================================================================================
1194 /*!
1195  * \brief initialize a hypothesis creator
1196  */
1197 //================================================================================
1198
1199 void SMESHGUI_MeshOp::initHypCreator( SMESHGUI_GenericHypothesisCreator* theCreator )
1200 {
1201   if ( !theCreator ) return;
1202
1203   // Set shapes, of mesh and sub-mesh if any
1204
1205   // get Entry of the Geom object
1206   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1207   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1208   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ); 
1209
1210   if ( myToCreate && myIsMesh )
1211     aMeshEntry = aGeomEntry;
1212
1213   if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
1214     _PTR(SObject) pObj = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
1215     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1216     aMeshEntry = ( aGeomVar->_is_nil() ) ? QString() : SMESH::toQStr( aGeomVar->GetStudyEntry() );
1217   }
1218
1219   if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
1220     _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1221     bool isMesh;
1222     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
1223     if ( !aGeomVar->_is_nil() )
1224     {
1225       aGeomEntry = SMESH::toQStr( aGeomVar->GetStudyEntry() );
1226       if ( isMesh )
1227         aMeshEntry = aGeomEntry;
1228     }
1229   }
1230
1231   if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
1232     // take geometry from submesh being created
1233     _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1234     if ( pObj ) {
1235       // if current object is sub-mesh
1236       SMESH::SMESH_subMesh_var aSubMeshVar =
1237         SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1238       if ( !aSubMeshVar->_is_nil() ) {
1239         SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1240         if ( !aMeshVar->_is_nil() ) {
1241           _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1242           GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
1243           if ( !aGeomVar->_is_nil() )
1244             aMeshEntry = SMESH::toQStr( aGeomVar->GetStudyEntry() );
1245         }
1246       }
1247     }
1248   }
1249
1250   theCreator->setShapeEntry( aGeomEntry );
1251   if ( aMeshEntry != "" )
1252     theCreator->setMainShapeEntry( aMeshEntry );
1253
1254   theCreator->setNoGeomMesh( !myIsOnGeometry && myIsMesh && !myToCreate );
1255 }
1256
1257 //================================================================================
1258 /*!
1259  * \Brief Returns tab dimension
1260   * \param tab - the tab in the dlg
1261   * \param dlg - my dialogue
1262   * \retval int - dimension
1263  */
1264 //================================================================================
1265 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
1266 {
1267   int aDim = -1;
1268   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
1269     if (tab == dlg->tab(i))
1270       aDim = i;
1271   return aDim;
1272 }
1273
1274 //================================================================================
1275 /*!
1276  * \brief Create hypothesis
1277   * \param theHypType - hypothesis category (main or additional)
1278   * \param theIndex - index of type of hypothesis to be cerated
1279  *
1280  * Specifies dimension of hypothesis to be created (using sender() method),
1281  * specifies its type and calls method for hypothesis creation
1282  */
1283 //================================================================================
1284 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
1285 {
1286   // Specifies dimension of hypothesis to be created
1287   int aDim = getTabDim( sender(), myDlg );
1288   if (aDim == -1)
1289     return;
1290
1291   // Specifies type of hypothesis to be created
1292   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
1293   if (theIndex < 0 || theIndex >= dataList.count())
1294     return;
1295   QString aHypTypeName = dataList[ theIndex ]->TypeName;
1296
1297   // Create hypothesis
1298   createHypothesis(aDim, theHypType, aHypTypeName);
1299 }
1300
1301 namespace
1302 {
1303   QString GetUniqueName (const QStringList& theHypNames,
1304                          const QString& theName,
1305                          size_t theIteration = 1)
1306   {
1307     QString aName = theName + "_" + QString::number( theIteration );
1308     if ( theHypNames.contains( aName ) )
1309       return GetUniqueName( theHypNames, theName, ++theIteration );
1310     return aName;
1311   }
1312 }
1313
1314 //================================================================================
1315 /*!
1316  *  Create hypothesis and update dialog.
1317  *  \param theDim - dimension of hypothesis to be created
1318  *  \param theType - hypothesis category (algorithm, hypothesis, additional hypothesis)
1319  *  \param theTypeName - specifies hypothesis to be created
1320  */
1321 //================================================================================
1322 void SMESHGUI_MeshOp::createHypothesis(const int theDim,
1323                                        const int theType,
1324                                        const QString& theTypeName)
1325 {
1326   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName);
1327   if (!aData)
1328     return;
1329
1330   myDim = theDim;
1331   myType = theType;
1332
1333   // get a unique hyp name
1334   QStringList aHypNames;
1335   TDim2Type2HypList::const_iterator aDimIter = myExistingHyps.begin();
1336   for ( ; aDimIter != myExistingHyps.end(); aDimIter++) {
1337     const TType2HypList& aType2HypList = aDimIter.value();
1338     TType2HypList::const_iterator aTypeIter = aType2HypList.begin();
1339     for ( ; aTypeIter != aType2HypList.end(); aTypeIter++) {
1340       const THypList& aHypList = aTypeIter.value();
1341       THypList::const_iterator anIter = aHypList.begin();
1342       for ( ; anIter != aHypList.end(); anIter++) {
1343         const THypItem& aHypItem = *anIter;
1344         const QString& aHypName = aHypItem.second;
1345         aHypNames.append(aHypName);
1346       }
1347     }
1348   }
1349   QString aHypName = GetUniqueName( aHypNames, aData->Label);
1350
1351   // existing hypos
1352   bool dialog = false;
1353
1354   QString aClientLibName = aData->ClientLibName;
1355   if (aClientLibName == "") {
1356     // Call hypothesis creation server method (without GUI)
1357     SMESH::SMESH_Hypothesis_var aHyp =
1358       SMESH::CreateHypothesis(theTypeName, aHypName, false);
1359     aHyp.out();
1360   }
1361   else {
1362     // Get hypotheses creator client (GUI)
1363     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
1364
1365     // Create hypothesis
1366     if (aCreator)
1367     {
1368       // Get parameters appropriate to initialize a new hypothesis
1369       SMESH::SMESH_Hypothesis_var initParamHyp =
1370         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
1371
1372       removeCustomFilters(); // Issue 0020170
1373
1374       // set shapes, of mesh and sub-mesh if any
1375       initHypCreator( aCreator );
1376
1377       myDlg->setEnabled( false );
1378       aCreator->create(initParamHyp, aHypName, myDlg, this, SLOT( onHypoCreated( int ) ) );
1379       dialog = true;
1380     }
1381     else
1382     {
1383      SMESH::SMESH_Hypothesis_var aHyp =
1384        SMESH::CreateHypothesis(theTypeName, aHypName, false);
1385      aHyp.out();
1386     }
1387   }
1388
1389   if( !dialog )
1390     onHypoCreated(2);
1391 }
1392
1393 //================================================================================
1394 /*!
1395  *  Necessary steps after hypothesis creation
1396  *  \param result - creation result:
1397  *   0 = rejected
1398  *   1 = accepted
1399  *   2 = additional value meaning that slot is called not from dialog box
1400  */
1401 //================================================================================
1402 void SMESHGUI_MeshOp::onHypoCreated( int result )
1403 {
1404   if( result != 2 )
1405   {
1406     int obj = myDlg->getActiveObject();
1407     onActivateObject( obj ); // Issue 0020170. Restore filters
1408     myDlg->setEnabled( true );
1409   }
1410
1411   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1412
1413   int nbHyp = myExistingHyps[myDim][myType].count();
1414   HypothesisData* algoData = hypData( myDim, Algo, currentHyp( myDim, Algo ));
1415   QStringList aNewHyps;
1416   existingHyps(myDim, myType, aFather, aNewHyps, myExistingHyps[myDim][myType], algoData);
1417   if (aNewHyps.count() > nbHyp)
1418   {
1419     for (int i = nbHyp; i < aNewHyps.count(); i++)
1420       myDlg->tab(myDim)->addHyp(myType, aNewHyps[i]);
1421   }
1422
1423   if( result!=2 && myHypoSet )
1424     processSet();
1425 }
1426
1427 //================================================================================
1428 /*!
1429  * \brief Calls plugin methods for hypothesis editing
1430   * \param theHypType - specifies whether main hypothesis or additional one
1431   * is edited
1432   * \param theIndex - index of existing hypothesis
1433  *
1434  * Calls plugin methods for hypothesis editing
1435  */
1436 //================================================================================
1437 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
1438 {
1439   // Speicfies dimension of hypothesis to be created
1440   int aDim = getTabDim( sender(), myDlg );
1441   if (aDim == -1)
1442     return;
1443
1444   const THypList& aList = myExistingHyps[ aDim ][ theHypType ];
1445   if ( theIndex < 0 || theIndex >= aList.count() )
1446     return;
1447   const THypItem& aHypItem = aList[ theIndex ];
1448   SMESH::SMESH_Hypothesis_var aHyp = aHypItem.first;
1449   if ( aHyp->_is_nil() )
1450     return;
1451
1452   SMESHGUI_GenericHypothesisCreator* aCreator =
1453     SMESH::GetHypothesisCreator( SMESH::toQStr( aHyp->GetName() ));
1454   if ( aCreator )
1455   {
1456     // set initial parameters
1457     SMESH::SMESH_Hypothesis_var initParamHyp =
1458       getInitParamsHypothesis( SMESH::toQStr( aHyp->GetName() ),
1459                                SMESH::toQStr( aHyp->GetLibName() ));
1460     aCreator->setInitParamsHypothesis( initParamHyp );
1461
1462     // set shapes, of mesh and sub-mesh if any
1463     initHypCreator( aCreator );
1464
1465     removeCustomFilters(); // Issue 0020170
1466     myDlg->setEnabled( false );
1467
1468     aCreator->edit( aHyp.in(), aHypItem.second, dlg(), this, SLOT( onHypoEdited( int ) ) );
1469   }
1470 }
1471
1472 //================================================================================
1473 /*!
1474  *  Necessary steps after hypothesis edition
1475  *  \param result - creation result:
1476  *   0 = rejected
1477  *   1 = accepted
1478  */
1479 //================================================================================
1480 void SMESHGUI_MeshOp::onHypoEdited( int result )
1481 {
1482   int obj = myDlg->getActiveObject();
1483   onActivateObject( obj ); // Issue 0020170. Restore filters
1484   myDlg->setEnabled( true );
1485 }
1486
1487 //================================================================================
1488 /*!
1489  * \brief access to hypothesis data
1490   * \param theDim - hyp dimension
1491   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
1492   * \param theIndex - index in the list
1493   * \retval HypothesisData* - result data, may be 0
1494  */
1495 //================================================================================
1496 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
1497                                           const int theHypType,
1498                                           const int theIndex)
1499 {
1500   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
1501        theHypType > -1 && theHypType < NbHypTypes &&
1502        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
1503     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
1504   return 0;
1505 }
1506
1507 //================================================================================
1508 /*!
1509  * \brief Set available algos and hypos according to the selected algorithm
1510   * \param theIndex - algorithm index
1511  */
1512 //================================================================================
1513 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
1514                                       const int theDim )
1515 {
1516   if ( myIgnoreAlgoSelection )
1517     return;
1518
1519   int curDim = getTabDim( sender(), myDlg );
1520   int aDim = theDim < 0 ? curDim : theDim;
1521   if (aDim == -1)
1522     return;
1523
1524   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1525
1526   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1527   HypothesisData* algoByDim[4];
1528   algoByDim[ aDim ] = algoData;
1529
1530   QStringList anAvailable;
1531
1532   // enable / disable tabs
1533   if ( myIsOnGeometry ) {
1534     for (int i = SMESH::DIM_3D; i >= SMESH::DIM_0D; i--) {
1535       if ( i > aDim ) {
1536         if ( i > myMaxShapeDim ) myDlg->disableTab( i );
1537         else                     myDlg->enableTab( i );
1538       }
1539       else if ( i == aDim ) {
1540         continue;
1541       }
1542       else {//( i < aDim )
1543         if ( algoData && algoData->InputTypes.isEmpty() ) {
1544           myDlg->disableTab( i );
1545           for ( int type = Algo, nbTypes = nbDlgHypTypes(i); type < nbTypes; type++ )
1546             setCurrentHyp(i, type, -1);
1547         }
1548         else {
1549           myDlg->enableTab( i );
1550         }
1551       }
1552     }
1553   }
1554
1555   int algoDim = aDim;
1556   HypothesisData* a3DAlgo = 0;
1557   // 2 loops: backward and forward from algo dimension
1558   for ( int forward = 0; forward <= 1; ++forward )
1559   {
1560     int dim = algoDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
1561     if ( !forward ) {
1562       dim = algoDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
1563     }
1564     HypothesisData* prevAlgo = algoData;
1565     bool noCompatible = false;
1566     for ( ; dim * dir <= lastDim * dir; dim += dir)
1567     {
1568       if ( !isAccessibleDim( dim ))
1569         continue;
1570       if ( noCompatible ) { // the selected algo has no compatible ones
1571         anAvailable.clear();
1572         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1573         myAvailableHypData[dim][Algo].clear();
1574         algoByDim[ dim ] = 0;
1575         continue;
1576       }
1577       HypothesisData* nextAlgo = 0;
1578       if ( myMaxShapeDim == SMESH::DIM_3D && a3DAlgo && dim == SMESH::DIM_2D ) {
1579         nextAlgo = a3DAlgo;
1580       }
1581       // get currently selected algo
1582       int algoIndex = currentHyp( dim, Algo );
1583       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
1584
1585       QString anCompareType = currentMeshTypeName(myDlg->currentMeshType());
1586       QString anCurrentCompareType = "";
1587       if ( dim == SMESH::DIM_3D || anCompareType == "ANY" )
1588         anCurrentCompareType = anCompareType;
1589       else if ( dim == SMESH::DIM_2D ) {
1590         anCurrentCompareType = (anCompareType == "HEXA" || anCompareType == "QUAD") ? "QUAD" : "TRIA";
1591         nextAlgo = 0;
1592       }
1593
1594       // set new available algorithms
1595       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo, nextAlgo, anCurrentCompareType);
1596       HypothesisData* soleCompatible = 0;
1597       if ( anAvailable.count() == 1 )
1598         soleCompatible = myAvailableHypData[dim][Algo][0];
1599       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1600       noCompatible = anAvailable.isEmpty();
1601       algoIndex = myAvailableHypData[dim][Algo].indexOf( curAlgo );
1602       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward && dim == curDim ) {
1603         // select the sole compatible algo
1604         algoIndex = 0;
1605       }
1606       setCurrentHyp( dim, Algo, algoIndex );
1607
1608       // remember current algo
1609       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1610
1611     } // loop on dims
1612
1613     if ( myMaxShapeDim == SMESH::DIM_3D && forward && algoDim == SMESH::DIM_1D )
1614     {
1615       algoDim = SMESH::DIM_3D;
1616       forward = -1;
1617       a3DAlgo = prevAlgo;
1618       continue;
1619     }
1620   } // loops backward and forward
1621
1622
1623   // set hypotheses corresponding to the found algorithms
1624
1625   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1626
1627   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1628   {
1629     if ( !isAccessibleDim( dim ))
1630       continue;
1631
1632     // get indices of selected hyps
1633     const int nbTypes = nbDlgHypTypes(dim);
1634     std::vector<int> hypIndexByType( nbTypes, -1 );
1635     for ( int dlgType = MainHyp; dlgType < nbTypes; dlgType++ )
1636     {
1637       hypIndexByType[ dlgType ] = currentHyp( dim, dlgType );
1638     }
1639
1640     // update hyps
1641     for ( int dlgType = MainHyp; dlgType < nbTypes; dlgType++ )
1642     {
1643       const int type = Min( dlgType, AddHyp );
1644       myAvailableHypData[ dim ][ type ].clear();
1645       QStringList anAvailable, anExisting;
1646
1647       HypothesisData* curAlgo = algoByDim[ dim ];
1648       int hypIndex = hypIndexByType[ dlgType ];
1649
1650       SMESH::SMESH_Hypothesis_var curHyp;
1651       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1652         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ].first;
1653
1654       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
1655         // try to find algo by selected hypothesis in order to keep it selected
1656         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1657         QString curHypType = SMESH::toQStr( curHyp->GetName() );
1658         if ( !algoDeselectedByUser &&
1659              myObjHyps[ dim ][ type ].count() > 0 &&
1660              curHypType == SMESH::toQStr( myObjHyps[ dim ][ type ].first().first->GetName()) )
1661         {
1662           HypothesisData* hypData = SMESH::GetHypothesisData( SMESH::toQStr( curHyp->GetName() ));
1663           for (int i = 0; i < myAvailableHypData[ dim ][ Algo ].count(); ++i) {
1664             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1665             if (curAlgo && hypData && isCompatible(curAlgo, hypData, type))
1666               break;
1667             else
1668               curAlgo = 0;
1669           }
1670         }
1671       }
1672       // get hyps compatible with curAlgo
1673       bool defaulHypAvlbl = false;
1674       if ( curAlgo )
1675       {
1676         // check if a selected hyp is compatible with the curAlgo
1677         if ( !curHyp->_is_nil() ) {
1678           HypothesisData* hypData = SMESH::GetHypothesisData( SMESH::toQStr( curHyp->GetName() ));
1679           if ( !isCompatible( curAlgo, hypData, type ))
1680             curHyp = SMESH::SMESH_Hypothesis::_nil();
1681         }
1682         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
1683         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
1684         defaulHypAvlbl = (type == MainHyp && !curAlgo->IsAuxOrNeedHyp );
1685       }
1686       // set list of hypotheses
1687       if ( dlgType <= AddHyp )
1688       {
1689         myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1690         myDlg->tab( dim )->setExistingHyps( type, anExisting, defaulHypAvlbl );
1691       }
1692       // set current existing hypothesis
1693       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1694         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1695       else
1696         hypIndex = -1;
1697       if ( !isSubmesh && myToCreate && hypIndex < 0 && anExisting.count() == 1 && dim == curDim )
1698       {
1699         // none is yet selected => select the sole existing if it is not optional
1700         CORBA::String_var hypTypeName = myExistingHyps[ dim ][ type ].first().first->GetName();
1701         bool isOptional = true;
1702         if ( algoByDim[ dim ] &&
1703              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName.in(), isOptional ) &&
1704              !isOptional )
1705           hypIndex = 0;
1706       }
1707       setCurrentHyp( dim, dlgType, hypIndex );
1708     }
1709   }
1710 }
1711
1712 //================================================================================
1713 /*!
1714  * \brief Creates and selects hypothesis of hypotheses set
1715  * \param theSetName - The name of hypotheses set
1716  */
1717 //================================================================================
1718 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1719 {
1720   myHypoSet = SMESH::GetHypothesesSet(theSetName);
1721   if (!myHypoSet)
1722     return;
1723
1724   // clear all hyps
1725   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++) {
1726     setCurrentHyp(dim, Algo, -1);
1727     setCurrentHyp(dim, AddHyp, -1);
1728     setCurrentHyp(dim, MainHyp, -1);
1729   }
1730
1731   myHypoSet->init(true); //algorithms
1732   processSet();
1733   myHypoSet->init(false); //hypotheses
1734   processSet();
1735   myHypoSet = 0;
1736 }
1737
1738 //================================================================================
1739 /*!
1740  * \brief One step of hypothesis/algorithm list creation
1741  *
1742  * Creates a hypothesis or an algorithm for current item of internal list of names myHypoSet
1743  */
1744 //================================================================================
1745 void SMESHGUI_MeshOp::processSet()
1746 {
1747   myHypoSet->next();
1748   if( !myHypoSet->more() )
1749     return;
1750
1751   bool isAlgo = myHypoSet->isAlgo();
1752   QString aHypoTypeName = myHypoSet->current();
1753   HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
1754   if (!aHypData)
1755   {
1756     processSet();
1757     return;
1758   }
1759
1760   int aDim = aHypData->Dim[0];
1761   // create or/and set
1762   if (isAlgo)
1763   {
1764     int index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
1765     if ( index < 0 )
1766     {
1767       QStringList anAvailable;
1768       availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
1769       myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1770       index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
1771     }
1772     setCurrentHyp( aDim, Algo, index );
1773     onAlgoSelected( index, aDim );
1774     processSet();
1775   }
1776   else
1777   {
1778     bool mainHyp = true;
1779     QStringList anAvailable;
1780     availableHyps( aDim, MainHyp, anAvailable, myAvailableHypData[aDim][MainHyp] );
1781     myDlg->tab( aDim )->setAvailableHyps( MainHyp, anAvailable );
1782     int index = myAvailableHypData[aDim][MainHyp].indexOf( aHypData );
1783     if ( index < 0 )
1784     {
1785       mainHyp = false;
1786       index = myAvailableHypData[aDim][AddHyp].indexOf( aHypData );
1787     }
1788     if (index >= 0)
1789       createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
1790     else
1791       processSet();
1792   }
1793 }
1794
1795 //================================================================================
1796 /*!
1797  * \brief Creates mesh
1798   * \param theMess - Output parameter intended for returning error message
1799   * \param theEntryList - List of entries of published objects
1800   * \retval bool  - TRUE if mesh is created, FALSE otherwise
1801  *
1802  * Creates mesh
1803  */
1804 //================================================================================
1805 bool SMESHGUI_MeshOp::createMesh( QString& theMess, QStringList& theEntryList )
1806 {
1807   theMess = "";
1808
1809   QStringList aList;
1810   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1811   if ( aList.isEmpty() )
1812   {
1813     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1814     if ( aSMESHGen->_is_nil() )
1815       return false;
1816
1817     SMESH::SMESH_Mesh_var aMeshVar= aSMESHGen->CreateEmptyMesh();
1818     if ( aMeshVar->_is_nil() )
1819       return false;
1820
1821     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1822     if ( aMeshSO ) {
1823       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
1824       theEntryList.append( aMeshSO->GetID().c_str() );
1825     }
1826     return true;
1827   }
1828   QString namePrefix;
1829   if ( aList.count() > 1 )
1830   {
1831     namePrefix = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
1832     int i = namePrefix.length() - 1;
1833     while ( i > 0 && namePrefix[i].isDigit() )
1834       --i;
1835     if ( i < namePrefix.length() - 1 )
1836       namePrefix.chop( namePrefix.length() - 1 - i );
1837     else
1838       namePrefix += "_";
1839   }
1840   QStringList::Iterator it = aList.begin();
1841   for ( int i = 0; it!=aList.end(); it++, ++i )
1842   {
1843     QString aGeomEntry = *it;
1844     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
1845     GEOM::GEOM_Object_var aGeomVar =
1846       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1847
1848     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1849     if ( aSMESHGen->_is_nil() )
1850       return false;
1851
1852     SUIT_OverrideCursor aWaitCursor;
1853
1854     // create mesh
1855     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
1856     if ( aMeshVar->_is_nil() )
1857       return false;
1858     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1859     if ( aMeshSO ) {
1860       theEntryList.append( aMeshSO->GetID().c_str() );
1861       if ( i > 0 ) setDefaultName( namePrefix );
1862       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
1863     }
1864
1865     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
1866     {
1867       if ( !isAccessibleDim( aDim )) continue;
1868
1869       // assign hypotheses
1870       for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(aDim); dlgType++ )
1871       {
1872         const int aHypIndex = currentHyp( aDim, dlgType );
1873         const int  aHypType = Min( dlgType, AddHyp );
1874         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1875         {
1876           SMESH::SMESH_Hypothesis_var aHypVar =
1877             myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
1878           if ( !aHypVar->_is_nil() )
1879             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
1880         }
1881       }
1882       // find or create algorithm
1883       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1884       if ( !anAlgoVar->_is_nil() )
1885         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1886     }
1887   }
1888   return true;
1889 }
1890
1891 //================================================================================
1892 /*!
1893  * \brief Creates sub-mesh
1894  * \param theMess - Output parameter intended for returning error message
1895  * \param theEntryList - List of entries of published objects
1896  * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1897  *
1898  * Creates sub-mesh
1899  */
1900 //================================================================================
1901 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess, QStringList& theEntryList )
1902 {
1903   theMess = "";
1904
1905   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1906   if ( aSMESHGen->_is_nil() )
1907     return false;
1908
1909   // get mesh object
1910   SMESH::SMESH_Mesh_var aMeshVar =
1911     SMESH::EntryToInterface<SMESH::SMESH_Mesh>( myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh ));
1912   if ( aMeshVar->_is_nil() && myIsInvalidSubMesh )
1913   {
1914     SMESH::SMESH_subMesh_var aSMVar =
1915       SMESH::EntryToInterface<SMESH::SMESH_subMesh>( myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ));
1916     if ( !aSMVar->_is_nil() )
1917       aMeshVar = aSMVar->GetMesh();
1918   }
1919   if (aMeshVar->_is_nil())
1920     return false;
1921
1922   // GEOM shape of the main mesh
1923   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
1924
1925   // Name for the new sub-mesh
1926   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
1927
1928   // get geom object
1929   GEOM::GEOM_Object_var aGeomVar;
1930   QStringList aGEOMs;
1931   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
1932   if (aGEOMs.count() == 1)
1933   {
1934     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1935     QString aGeomEntry = aGEOMs.first();
1936     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
1937     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1938   }
1939   else if (aGEOMs.count() > 1)
1940   {
1941     // create a GEOM group
1942     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1943     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1944     if (!geomGen->_is_nil() && aStudy) {
1945       GEOM::GEOM_IGroupOperations_wrap op =
1946         geomGen->GetIGroupOperations(aStudy->StudyId());
1947       if (!op->_is_nil()) {
1948         // check and add all selected GEOM objects: they must be
1949         // a sub-shapes of the main GEOM and must be of one type
1950         int iSubSh = 0;
1951         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1952         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
1953         aSeq->length(aGEOMs.count());
1954         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
1955         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
1956           QString aSubGeomEntry = (*aSubShapesIter);
1957           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
1958           GEOM::GEOM_Object_var aSubGeomVar =
1959             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
1960           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
1961           if (iSubSh == 0) {
1962             aGroupType = aSubShapeType;
1963           } else {
1964             if (aSubShapeType != aGroupType)
1965               aGroupType = TopAbs_SHAPE;
1966           }
1967           aSeq[iSubSh] = aSubGeomVar;
1968         }
1969         // create a group
1970         GEOM::GEOM_Object_wrap aGroupVar = op->CreateGroup(mainGeom, aGroupType);
1971         op->UnionList(aGroupVar, aSeq);
1972
1973         if (op->IsDone())
1974         {
1975           aGeomVar = GEOM::GEOM_Object::_duplicate( aGroupVar.in() );
1976
1977           // publish the GEOM group in study
1978           QString aNewGeomGroupName ("Auto_group_for_");
1979           aNewGeomGroupName += aName;
1980           SALOMEDS::Study_var aStudyVar = _CAST(Study, aStudy)->GetStudy();
1981           SALOMEDS::SObject_wrap aNewGroupSO =
1982             geomGen->AddInStudy( aStudyVar, aGeomVar,
1983                                  aNewGeomGroupName.toLatin1().data(), mainGeom);
1984         }
1985       }
1986     }
1987   }
1988   else {
1989   }
1990   if (aGeomVar->_is_nil())
1991     return false;
1992
1993   SUIT_OverrideCursor aWaitCursor;
1994
1995   QString aNameOrID = aName;
1996   if ( myIsInvalidSubMesh )
1997     // pass a sub-mesh entry to mesh->GetSubMesh() to replace the invalid sub-mesh
1998     // by a valid one in an existing SO
1999     aNameOrID = myDlg->selectedObject(SMESHGUI_MeshDlg::Obj);
2000
2001   // create sub-mesh
2002   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aNameOrID.toLatin1().data() );
2003   _PTR(SObject) aSubMeshSO = SMESH::FindSObject( aSubMeshVar.in() );
2004   if ( aSubMeshSO ) {
2005     SMESH::SetName( aSubMeshSO, aName.toLatin1().data() );
2006     theEntryList.append( aSubMeshSO->GetID().c_str() );
2007   }
2008
2009   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
2010   {
2011     if ( !isAccessibleDim( aDim )) continue;
2012
2013     // find or create algorithm
2014     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
2015     if ( !anAlgoVar->_is_nil() )
2016       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
2017     // assign hypotheses
2018     for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(aDim); dlgType++ )
2019     {
2020       const int aHypIndex = currentHyp( aDim, dlgType );
2021       const int  aHypType = Min( dlgType, AddHyp );
2022       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
2023       {
2024         SMESH::SMESH_Hypothesis_var aHypVar =
2025           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
2026         if ( !aHypVar->_is_nil() )
2027           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
2028       }
2029     }
2030   }
2031
2032   // deselect geometry: next submesh should be created on other sub-shape
2033   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
2034   selectObject( _PTR(SObject)() );
2035   selectionDone();
2036
2037   checkSubMeshConcurrency( aMeshVar, aSubMeshVar, /*askUser=*/true );
2038
2039   return true;
2040 }
2041
2042 //================================================================================
2043 /*!
2044  * \brief Gets current hypothesis or algorithms
2045   * \param theDim - dimension of hypothesis or algorithm
2046   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
2047   * \retval int - current hypothesis or algorithms
2048  *
2049  * Gets current hypothesis or algorithms
2050  */
2051 //================================================================================
2052 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
2053 {
2054   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
2055 }
2056
2057 //================================================================================
2058 /*!
2059  * \brief Checks if a hypothesis is selected
2060  */
2061 //================================================================================
2062
2063 bool SMESHGUI_MeshOp::isSelectedHyp( int theDim, int theHypType, int theIndex) const
2064 {
2065   if ( theIndex < 0 )
2066     return false;
2067
2068   if ( theHypType < AddHyp ) // only one hyp can be selected
2069     return currentHyp( theDim, theHypType ) == theIndex;
2070
2071   for ( int dlgHypType = AddHyp; dlgHypType < nbDlgHypTypes( theDim ); ++dlgHypType )
2072     if ( currentHyp( theDim, dlgHypType ) == theIndex )
2073       return true;
2074
2075   return false;
2076 }
2077
2078 //================================================================================
2079 /*!
2080  * \brief Returns nb of HypType's taking into account possible several
2081  *        selected additional hypotheses which are coded as additional HypType's.
2082  */
2083 //================================================================================
2084
2085 int SMESHGUI_MeshOp::nbDlgHypTypes( const int dim ) const
2086 {
2087   return NbHypTypes + myDlg->tab( dim )->nbAddHypTypes();
2088 }
2089
2090 //================================================================================
2091 /*!
2092  * \brief Returns true if hypotheses of given dim can be assigned
2093   * \param theDim - hypotheses dimension
2094   * \retval bool - result
2095  */
2096 //================================================================================
2097 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
2098 {
2099   return myDlg->isTabEnabled( theDim );
2100 }
2101
2102 //================================================================================
2103 /*!
2104  * \brief Sets current hypothesis or algorithms
2105   * \param theDim - dimension of hypothesis or algorithm
2106   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
2107   * \param theIndex - Index of hypothesis
2108   * \param updateHypsOnAlgoDeselection - to clear and disable hyps if algo deselected
2109  *
2110  * Gets current hypothesis or algorithms
2111  */
2112 //================================================================================
2113 void SMESHGUI_MeshOp::setCurrentHyp( const int  theDim,
2114                                      const int  theHypType,
2115                                      const int  theIndex,
2116                                      const bool updateHypsOnAlgoDeselection)
2117 {
2118   myIgnoreAlgoSelection = true;
2119   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
2120   myIgnoreAlgoSelection = false;
2121
2122   if ( updateHypsOnAlgoDeselection && theHypType == Algo && theIndex < 0 )
2123   {
2124     const QStringList noHyps;
2125     myDlg->tab( theDim )->setAvailableHyps( MainHyp, noHyps );
2126     myDlg->tab( theDim )->setExistingHyps ( MainHyp, noHyps );
2127     myDlg->tab( theDim )->setAvailableHyps( AddHyp,  noHyps );
2128     myDlg->tab( theDim )->setExistingHyps ( AddHyp,  noHyps );
2129   }
2130 }
2131
2132 //================================================================================
2133 /*!
2134  * \brief Generates default and sets mesh/submesh name
2135  *
2136  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
2137  */
2138 //================================================================================
2139 void SMESHGUI_MeshOp::setDefaultName( const QString& thePrefix ) const
2140 {
2141   QString aResName;
2142
2143   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
2144   int i = 1;
2145
2146   QString aPrefix = thePrefix;
2147   if ( aPrefix.isEmpty() )
2148     aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
2149
2150   _PTR(SObject) anObj;
2151   do
2152   {
2153     aResName = aPrefix + QString::number( i++ );
2154     anObj = aStudy->FindObject( aResName.toLatin1().data() );
2155   }
2156   while ( anObj );
2157
2158   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
2159     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
2160   aControl->setText( aResName );
2161 }
2162
2163 //================================================================================
2164 /*!
2165  * \brief Gets algorithm or creates it if necessary
2166   * \param theDim - specifies dimension of returned hypotheses/algorifms
2167   * \retval SMESH::SMESH_Hypothesis_var - algorithm
2168  *
2169  * Gets algorithm or creates it if necessary
2170  */
2171 //================================================================================
2172 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
2173 {
2174   SMESH::SMESH_Hypothesis_var anAlgoVar;
2175
2176   // get type of the selected algo
2177   int aHypIndex = currentHyp( theDim, Algo );
2178   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
2179   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
2180     return anAlgoVar;
2181   QString aHypName = dataList[ aHypIndex ]->TypeName;
2182
2183   // get existing algorithms
2184   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
2185   QStringList tmp;
2186   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
2187
2188   // look for an existing algo of such a type
2189   THypList& aHypVarList = myExistingHyps[ theDim ][ Algo ];
2190   THypList::iterator anIter = aHypVarList.begin();
2191   for ( ; anIter != aHypVarList.end(); anIter++)
2192   {
2193     SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
2194     if ( !aHypVar->_is_nil() && aHypName == SMESH::toQStr( aHypVar->GetName() ))
2195     {
2196       anAlgoVar = aHypVar;
2197       break;
2198     }
2199   }
2200
2201   if (anAlgoVar->_is_nil())
2202   {
2203     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
2204     if (aHypData)
2205     {
2206       QString aClientLibName = aHypData->ClientLibName;
2207       if ( aClientLibName.isEmpty() )
2208       {
2209         // Call hypothesis creation server method (without GUI)
2210         SMESH::SMESH_Hypothesis_var aHyp =
2211           SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
2212         aHyp.out();
2213       }
2214       else
2215       {
2216         // Get hypotheses creator client (GUI)
2217         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
2218
2219         // Create algorithm
2220         if (aCreator)
2221           aCreator->create( true, aHypName, myDlg, 0, QString::null );
2222         else {
2223           SMESH::SMESH_Hypothesis_var aHyp =
2224             SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
2225           aHyp.out();
2226         }
2227         delete aCreator;
2228       }
2229       QStringList tmpList;
2230       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
2231       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
2232     }
2233
2234     THypList& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
2235     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
2236     {
2237       SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
2238       if ( !aHypVar->_is_nil() && aHypName == SMESH::toQStr( aHypVar->GetName() ))
2239       {
2240         anAlgoVar = aHypVar;
2241         break;
2242       }
2243     }
2244   }
2245
2246   return anAlgoVar._retn();
2247 }
2248
2249 //================================================================================
2250 /*!
2251  * \brief Reads parameters of an edited mesh/sub-mesh and assigns them to the dialog
2252  *
2253  * Called when mesh is edited only.
2254  */
2255 //================================================================================
2256 void SMESHGUI_MeshOp::readMesh()
2257 {
2258   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2259   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
2260   if ( !pObj )
2261     return;
2262
2263   if (myIsOnGeometry) {
2264     // Get name of mesh if current object is sub-mesh
2265     SMESH::SMESH_subMesh_var aSubMeshVar =
2266       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
2267     if ( !aSubMeshVar->_is_nil() )
2268     {
2269       SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
2270       if ( !aMeshVar->_is_nil() )
2271       {
2272         _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
2273         QString aMeshName = name( aMeshSO );
2274         myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
2275       }
2276       myHasConcurrentSubBefore = checkSubMeshConcurrency( aMeshVar, aSubMeshVar );
2277     }
2278
2279     if ( !myIsInvalidSubMesh )
2280     {
2281       // Get name of geometry object
2282       CORBA::String_var name = SMESH::GetGeomName( pObj );
2283       if ( name.in() )
2284         myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, name.in() );
2285     }
2286   }
2287
2288   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
2289   QStringList anExisting;
2290   const int lastDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_2D;
2291   bool algoFound = false;
2292   for ( int dim = SMESH::DIM_3D; dim >= lastDim; --dim )
2293   {
2294     // get algorithm
2295     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
2296     // find algo index among available ones
2297     int aHypIndex = -1;
2298     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
2299     {
2300       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first().first;
2301       HypothesisData* algoData = SMESH::GetHypothesisData( SMESH::toQStr( aVar->GetName() ));
2302       aHypIndex = myAvailableHypData[ dim ][ Algo ].indexOf ( algoData );
2303       //       if ( aHypIndex < 0 && algoData ) {
2304       //         // assigned algo is incompatible with other algorithms
2305       //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
2306       //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
2307       //       }
2308       algoFound = ( aHypIndex > -1 );
2309     }
2310     setCurrentHyp( dim, Algo, aHypIndex );
2311     // set existing and available hypothesis according to the selected algo
2312     if ( aHypIndex > -1 || !algoFound )
2313       onAlgoSelected( aHypIndex, dim );
2314   }
2315
2316   // get hypotheses
2317   bool hypWithoutAlgo = false;
2318   for ( int dim = SMESH::DIM_3D; dim >= lastDim; --dim )
2319   {
2320     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2321     {
2322       // get hypotheses
2323       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2324       if ( myObjHyps[ dim ][ hypType ].count() == 0 ) {
2325         setCurrentHyp( dim, hypType, -1 );
2326       }
2327       for ( int i = 0, nb = myObjHyps[ dim ][ hypType ].count(); i < nb; ++i )
2328       {
2329         // find index of required hypothesis among existing ones for this dimension and type
2330         int aHypIndex = find( myObjHyps[ dim ][ hypType ][ i ].first,
2331                               myExistingHyps[ dim ][ hypType ] );
2332         if ( aHypIndex < 0 ) {
2333           // assigned hypothesis is incompatible with the algorithm
2334           if ( currentHyp( dim, Algo ) < 0 )
2335           { // none algo selected; it is edition for sure, of submesh maybe
2336             hypWithoutAlgo = true;
2337             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ][ i ] );
2338             anExisting.push_back( myObjHyps[ dim ][ hypType ][ i ].second );
2339             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
2340             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
2341           }
2342         }
2343         setCurrentHyp( dim, hypType + i, aHypIndex );
2344
2345         if ( hypType == MainHyp ) break; // only one main hyp allowed
2346       }
2347     }
2348   }
2349   // make available other hyps of same type as one without algo
2350   if ( hypWithoutAlgo )
2351     onAlgoSelected( currentHyp( 0, Algo ), 0 );
2352 }
2353
2354 //================================================================================
2355 /*!
2356  * \brief Gets name of object
2357  * \param theSO - SObject
2358  * \retval QString - name of object
2359  *
2360  * Gets name of object
2361  */
2362 //================================================================================
2363 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
2364 {
2365   QString aResName;
2366   if ( theSO )
2367   {
2368     _PTR(GenericAttribute) anAttr;
2369     _PTR(AttributeName)    aNameAttr;
2370     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
2371     {
2372       aNameAttr = anAttr;
2373       aResName = aNameAttr->Value().c_str();
2374     }
2375   }
2376   return aResName;
2377 }
2378
2379 //================================================================================
2380 /*!
2381  * \brief Finds hypothesis in input list
2382   * \param theHyp - hypothesis to be found
2383   * \param theHypList - input list of hypotheses
2384   * \retval int - index of hypothesis or -1 if it is not found
2385  *
2386  * Finds position of hypothesis in input list
2387  */
2388 //================================================================================
2389 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
2390                            const THypList& theHypList ) const
2391 {
2392   int aRes = -1;
2393   if ( !theHyp->_is_nil() )
2394   {
2395     int i = 0;
2396     THypList::const_iterator anIter = theHypList.begin();
2397     for ( ; anIter != theHypList.end(); ++ anIter)
2398     {
2399       if ( theHyp->_is_equivalent( (*anIter).first ) )
2400       {
2401         aRes = i;
2402         break;
2403       }
2404       i++;
2405     }
2406   }
2407   return aRes;
2408 }
2409
2410 //================================================================================
2411 /*!
2412  * \brief Edits mesh or sub-mesh
2413   * \param theMess - Output parameter intended for returning error message
2414   * \retval bool  - TRUE if mesh is edited successfully, FALSE otherwise
2415  *
2416  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
2417  */
2418 //================================================================================
2419 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
2420 {
2421   theMess = "";
2422
2423   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
2424   if ( aSMESHGen->_is_nil() )
2425     return false;
2426
2427   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2428   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
2429   if ( !pObj )
2430     return false;
2431
2432   SUIT_OverrideCursor aWaitCursor;
2433
2434   // Set new name
2435   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
2436   SMESH::SetName( pObj, aName );
2437   int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_2D;
2438
2439   // First, remove old algos in order to avoid messages on algorithm hiding
2440   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2441   {
2442     if ( /*isAccessibleDim( dim ) &&*/ myObjHyps[ dim ][ Algo ].count() > 0 )
2443     {
2444       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first().first;
2445       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2446       if ( anAlgoVar->_is_nil() || // no new algo selected or
2447            SMESH::toQStr(anOldAlgo->GetName()) != SMESH::toQStr(anAlgoVar->GetName())) // algo change
2448       {
2449         // remove old algorithm
2450         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first().first );
2451         myObjHyps[ dim ][ Algo ].clear();
2452       }
2453     }
2454   }
2455
2456   SALOMEDS_SObject* aSObject = _CAST(SObject, pObj);
2457   CORBA::Object_var anObject = aSObject->GetObject();
2458   SMESH::SMESH_Mesh_var       aMeshVar = SMESH::SMESH_Mesh::_narrow( anObject );
2459   SMESH::SMESH_subMesh_var aSubMeshVar = SMESH::SMESH_subMesh::_narrow( anObject );
2460   bool isMesh = !aMeshVar->_is_nil();
2461   if ( !isMesh && !aSubMeshVar->_is_nil() )
2462     aMeshVar = aSubMeshVar->GetFather();
2463
2464   // Assign new algorithms and hypotheses
2465   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2466   {
2467     //if ( !isAccessibleDim( dim )) continue;
2468
2469     // find or create algorithm
2470     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2471
2472     // assign new algorithm
2473     if ( !anAlgoVar->_is_nil() && // some algo selected and
2474          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
2475     {
2476       if ( isMesh )
2477         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
2478       else if ( !aSubMeshVar->_is_nil() )
2479         SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
2480
2481       myObjHyps[ dim ][ Algo ].append( THypItem( anAlgoVar, aName) );
2482     }
2483
2484     // remove deselected hypotheses
2485     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2486     {
2487       for ( int i = 0, nb = myObjHyps[ dim ][ hypType ].count(); i < nb; ++i )
2488       {
2489         SMESH::SMESH_Hypothesis_var hyp = myObjHyps[ dim ][ hypType ][ i ].first;
2490         int hypIndex = this->find( hyp, myExistingHyps[ dim ][ hypType ]);
2491         if ( !isSelectedHyp( dim, hypType, hypIndex ) && !hyp->_is_nil() )
2492         {
2493           SMESH::RemoveHypothesisOrAlgorithmOnMesh( pObj, hyp );
2494         }
2495       }
2496     }
2497     // assign newly selected hypotheses
2498     for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(dim); dlgType++ )
2499     {
2500       const int curIndex = currentHyp( dim, dlgType );
2501       const int  hypType = Min( dlgType, AddHyp );
2502       if ( curIndex >= 0 && curIndex < myExistingHyps[ dim ][ hypType ].count() )
2503       {
2504         SMESH::SMESH_Hypothesis_var hyp = myExistingHyps[ dim ][ hypType ][ curIndex ].first;
2505
2506         bool isAssigned = ( this->find( hyp, myObjHyps[ dim ][ hypType ]) >= 0 );
2507         if ( !isAssigned )
2508         {
2509           if ( isMesh )
2510             SMESH::AddHypothesisOnMesh (aMeshVar, hyp );
2511           else if ( !aSubMeshVar->_is_nil() )
2512             SMESH::AddHypothesisOnSubMesh ( aSubMeshVar, hyp );
2513         }
2514       }
2515       // reread all hypotheses of mesh
2516       QStringList anExisting;
2517       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2518     }
2519   }
2520
2521   myHasConcurrentSubBefore =
2522     checkSubMeshConcurrency( aMeshVar, aSubMeshVar, /*askUser=*/!myHasConcurrentSubBefore );
2523
2524   return true;
2525 }
2526
2527 //================================================================================
2528 /*!
2529  * \brief Checks if a concurrent sub-meshes appear as result of sub-mesh
2530  *        creation/edition and, if (askUser) , proposes the uses to set up a desired
2531  *        order of sub-mesh computation.
2532  *        Returns \c true if a sub-mesh concurrency detected.
2533  */
2534 //================================================================================
2535
2536 bool SMESHGUI_MeshOp::checkSubMeshConcurrency(SMESH::SMESH_Mesh_ptr    mesh,
2537                                               SMESH::SMESH_subMesh_ptr submesh,
2538                                               bool                     askUser)
2539 {
2540   if ( CORBA::is_nil( mesh ) || CORBA::is_nil( submesh ))
2541     return false;
2542
2543   bool isNewConcurrent = mesh->IsUnorderedSubMesh( submesh->GetId() );
2544   if ( isNewConcurrent && askUser )
2545   {
2546     int butID = SUIT_MessageBox::warning( myDlg->parentWidget(), tr( "SMESH_WARNING" ),
2547                                           tr("CONCURRENT_SUBMESH_APPEARS"),
2548                                           tr("SMESH_BUT_YES"), tr("SMESH_BUT_NO"));
2549     if ( butID == 0 )
2550     {
2551       _PTR(SObject) meshSO = SMESH::FindSObject( mesh );
2552       LightApp_SelectionMgr* aSelectionMgr = selectionMgr();
2553       if ( meshSO && aSelectionMgr )
2554       {
2555         myDlg->setEnabled( false ); // disactivate selection
2556         selectionMgr()->clearFilters();
2557         selectObject( meshSO );
2558         SMESHGUI::GetSMESHGUI()->OnGUIEvent( SMESHOp::OpMeshOrder ); // MESH_ORDER
2559         qApp->processEvents();
2560
2561         myDlg->setEnabled( true );
2562         int obj = myDlg->getActiveObject();
2563         onActivateObject( obj ); // restore filter
2564         if ( !myToCreate )
2565         {
2566           selectObject( SMESH::FindSObject( submesh ));
2567           selectionDone();
2568         }
2569       }
2570     }
2571   }
2572
2573   return isNewConcurrent;
2574 }
2575
2576 //================================================================================
2577 /*!
2578  * \brief Verifies whether given operator is valid for this one
2579  * \param theOtherOp - other operation
2580  * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
2581  *
2582  * method redefined from base class verifies whether given operator is valid for
2583  * this one (i.e. can be started "above" this operator). In current implementation method
2584  * returns false if theOtherOp operation is not intended for deleting objects or mesh
2585  * elements.
2586  */
2587 //================================================================================
2588 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
2589 {
2590   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
2591 }
2592
2593 //================================================================================
2594 /*!
2595  * \brief SLOT. Is called when the user selects a way of geometry selection
2596  * \param theByMesh - true if the user wants to find geometry by mesh element
2597  */
2598 //================================================================================
2599 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
2600 {
2601   if ( theByMesh ) {
2602     if ( !myShapeByMeshOp ) {
2603       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
2604       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2605               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2606       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2607               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2608     }
2609     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2610     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
2611     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
2612       SMESH::SMESH_Mesh_var aMeshVar =
2613         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
2614       if ( !aMeshVar->_is_nil() ) {
2615         myDlg->hide(); // stop processing selection
2616         myShapeByMeshOp->setModule( getSMESHGUI() );
2617         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2618         myShapeByMeshOp->SetMesh( aMeshVar );
2619         myShapeByMeshOp->start();
2620       }
2621     }
2622   }
2623 }
2624
2625 //================================================================================
2626 /*!
2627  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2628  */
2629 //================================================================================
2630 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
2631 {
2632   if ( myShapeByMeshOp == op ) {
2633     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
2634     myDlg->show();
2635     // Select a found geometry object
2636     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2637     if ( !aGeomVar->_is_nil() )
2638     {
2639       QString ID = SMESH::toQStr( aGeomVar->GetStudyEntry() );
2640       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.toLatin1().data() )) {
2641         selectObject( aGeomSO );
2642         selectionDone();
2643       }
2644     }
2645   }
2646 }
2647
2648 //================================================================================
2649 /*!
2650  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2651  */
2652 //================================================================================
2653 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
2654 {
2655   if ( myShapeByMeshOp == op && myDlg ) {
2656     myDlg->show();
2657   }
2658 }
2659
2660 //================================================================================
2661 /*!
2662  * \brief Selects a SObject
2663  * \param theSObj - the SObject to select
2664  */
2665 //================================================================================
2666 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
2667 {
2668   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
2669     SALOME_ListIO anIOList;
2670     if ( theSObj ) {
2671       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2672         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
2673       anIOList.Append( anIO );
2674     }
2675     sm->setSelectedObjects( anIOList, false );
2676   }
2677 }
2678 //================================================================================
2679 /*!
2680  * \brief Create available list types of mesh
2681   * \param theTypeMesh - Output list of available types of mesh
2682  */
2683 //================================================================================
2684 void SMESHGUI_MeshOp::createMeshTypeList( QStringList& theTypeMesh)
2685 {
2686   theTypeMesh.clear();
2687   theTypeMesh.append( tr( "MT_ANY" ) );
2688   if ( myMaxShapeDim >= 2 || myMaxShapeDim == -1 )
2689   {
2690     theTypeMesh.append( tr( "MT_TRIANGULAR" ) );
2691     theTypeMesh.append( tr( "MT_QUADRILATERAL" ) );
2692   }
2693   if ( myMaxShapeDim == 3 || myMaxShapeDim == -1 )
2694   {
2695     theTypeMesh.append( tr( "MT_TETRAHEDRAL" ) );
2696     theTypeMesh.append( tr( "MT_HEXAHEDRAL" ) );
2697   }
2698
2699 }
2700 //================================================================================
2701 /*!
2702  * \brief Set available types of mesh
2703   * \param theTypeMesh - List of available types of mesh
2704  */
2705 //================================================================================
2706 void SMESHGUI_MeshOp::setAvailableMeshType( const QStringList& theTypeMesh )
2707 {
2708   myDlg->setAvailableMeshType( theTypeMesh );
2709 }
2710
2711 //================================================================================
2712 /*!
2713  * \brief SLOT. Is called when the user select type of mesh
2714   * \param theTabIndex - Index of current active tab
2715   * \param theIndex - Index of current type of mesh
2716  */
2717 //================================================================================
2718 void SMESHGUI_MeshOp::onAlgoSetByMeshType( const int theTabIndex, const int theIndex )
2719 {
2720   setFilteredAlgoData( theTabIndex, theIndex);
2721 }
2722
2723 //================================================================================
2724 /*!
2725  * \brief Set a filtered list of available algorithms by mesh type
2726   * \param theTabIndex - Index of current active tab
2727   * \param theIndex - Index of current type of mesh
2728  */
2729 //================================================================================
2730 void SMESHGUI_MeshOp::setFilteredAlgoData( const int theTabIndex, const int theIndex )
2731 {
2732   QStringList anAvailableAlgs;
2733   QString anCompareType = currentMeshTypeName( theIndex );
2734   int anCurrentAvailableAlgo = -1;
2735   bool isNone = true;
2736   int aDim = SMESH::DIM_3D;
2737   if ( theIndex == MT_TRIANGULAR || theIndex == MT_QUADRILATERAL)
2738     aDim = SMESH::DIM_2D;
2739   if ( anCompareType == "ANY" )
2740   {
2741     bool isReqDisBound = false;
2742     int aReqDim = SMESH::DIM_3D;
2743     for ( int dim = SMESH::DIM_3D; dim >= SMESH::DIM_2D; dim-- )
2744     {
2745       anCurrentAvailableAlgo = -1;
2746       isNone = currentHyp( dim, Algo ) < 0;
2747       //return current algo in current tab and set new algorithm list
2748       HypothesisData* algoCur = 0;
2749       if ( !isNone && !myAvailableHypData[dim][Algo].empty() ) {
2750         algoCur = myAvailableHypData[dim][Algo].at( currentHyp( dim, Algo ) );
2751       }
2752       HypothesisData* prevAlgo = 0;
2753       HypothesisData* nextAlgo = 0;
2754       if ( dim == SMESH::DIM_2D ) {
2755         prevAlgo = hypData( SMESH::DIM_1D, Algo, currentHyp( SMESH::DIM_1D, Algo ) );
2756         if ( aDim == SMESH::DIM_3D )
2757           nextAlgo = hypData( SMESH::DIM_3D, Algo, currentHyp( SMESH::DIM_3D, Algo ) );
2758       }
2759       // retrieves a list of available algorithms from resources
2760       availableHyps( dim, Algo, anAvailableAlgs, myAvailableHypData[dim][Algo], prevAlgo, nextAlgo, anCompareType);
2761       anCurrentAvailableAlgo = myAvailableHypData[dim][Algo].indexOf( algoCur );
2762       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailableAlgs );
2763       setCurrentHyp( dim, Algo, anCurrentAvailableAlgo );
2764       if ( anCurrentAvailableAlgo > -1 )
2765         isReqDisBound = algoCur->InputTypes.isEmpty();
2766       if ( isReqDisBound ) {
2767         aReqDim = dim;
2768         break;
2769       }
2770     }
2771     if ( !myIsOnGeometry )
2772       for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ ) {
2773         bool disable = myAvailableHypData[i][Algo].isEmpty();
2774         if ( disable ) myDlg->disableTab( i );
2775         else           myDlg->enableTab( i );
2776       }
2777     else
2778       for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ ) {
2779         if ( i > myMaxShapeDim || ( isReqDisBound && i < aReqDim ) ) myDlg->disableTab( i );
2780         else                                                         myDlg->enableTab( i );
2781       }
2782     myDlg->setCurrentTab( theTabIndex );
2783   }
2784   else
2785   {
2786     HypothesisData* anCurrentAlgo = 0;
2787     bool isReqDisBound = true;
2788     QString anCurrentCompareType = anCompareType;
2789     isNone = currentHyp( aDim, Algo ) < 0;
2790     if ( !isNone && !myAvailableHypData[aDim][Algo].empty() )
2791       isReqDisBound = myAvailableHypData[aDim][Algo].at( currentHyp( aDim, Algo ) )->InputTypes.isEmpty();
2792     for ( int dim = aDim; dim >= SMESH::DIM_2D; dim-- )
2793     {
2794       bool isNoneAlg = currentHyp( dim, Algo ) < 0;
2795       anCurrentAvailableAlgo = -1;
2796       HypothesisData* prevAlgo = 0;
2797       HypothesisData* nextAlgo = 0;
2798       if ( dim == SMESH::DIM_2D ) {
2799         prevAlgo = hypData( SMESH::DIM_1D, Algo, currentHyp( SMESH::DIM_1D, Algo ) );
2800         if ( aDim == SMESH::DIM_3D )
2801           nextAlgo = hypData( SMESH::DIM_3D, Algo, currentHyp( SMESH::DIM_3D, Algo ) );
2802       }
2803       // finding algorithm which is selected
2804       if ( !isNoneAlg ) {
2805         anCurrentAlgo = myAvailableHypData[dim][Algo].at( currentHyp( dim, Algo ) );
2806       }
2807       // retrieves a list of available algorithms from resources
2808       availableHyps( dim, Algo, anAvailableAlgs, myAvailableHypData[dim][Algo], prevAlgo, nextAlgo, anCurrentCompareType );
2809       // finding and adding algorithm depending on the type mesh
2810       anCurrentAvailableAlgo = myAvailableHypData[dim][Algo].indexOf( anCurrentAlgo );
2811       //set new algorithm list and select the current algorithm
2812       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailableAlgs );
2813       anCurrentCompareType = ( anCompareType == "HEXA" || anCompareType == "QUAD" ) ? "QUAD" : "TRIA";
2814       setCurrentHyp( dim, Algo, anCurrentAvailableAlgo, /*updateHyps=*/true );
2815     }
2816
2817     for ( int i = myMaxShapeDim; i >= SMESH::DIM_0D; i-- ) {
2818       bool isNoneAlg = currentHyp( i, Algo ) < 0;
2819       if ( !isNoneAlg )
2820         isReqDisBound = myAvailableHypData[i][Algo].at( currentHyp( i, Algo ) )->InputTypes.isEmpty();
2821       else
2822         isReqDisBound = true;
2823       if ( isReqDisBound && !isNoneAlg && i <= aDim) {
2824         for (int j = myMaxShapeDim; j >= SMESH::DIM_0D; j--) {
2825           if ( currentHyp( j, Algo ) < 0 ) {
2826             myDlg->disableTab( j );
2827             setCurrentHyp( j , Algo, -1, /*updateHyps=*/true );
2828           }
2829         }
2830         break;
2831       }
2832       else {
2833         myDlg->enableTab( i );
2834       }
2835     }
2836     if ( aDim == SMESH::DIM_2D) {
2837       setCurrentHyp( SMESH::DIM_3D, Algo, -1, /*updateHyps=*/true );
2838       myDlg->disableTab( SMESH::DIM_3D );
2839     }
2840
2841     int currentTab = ( theTabIndex <= aDim ) ? theTabIndex : aDim;
2842     myDlg->setCurrentTab( currentTab );
2843   }
2844   THypDataList anAvailableAlgsData;
2845   QStringList aHypothesesSetsList = SMESH::GetHypothesesSets( aDim );
2846   QStringList aFilteredHypothesesSetsList;
2847   aFilteredHypothesesSetsList.clear();
2848   QStringList::const_iterator inHypoSetName = aHypothesesSetsList.begin();
2849   for ( ; inHypoSetName != aHypothesesSetsList.end(); ++inHypoSetName ) {
2850     HypothesesSet* currentHypoSet = SMESH::GetHypothesesSet( *inHypoSetName );
2851     bool isAvailable = false;
2852     currentHypoSet->init( true );
2853     while ( currentHypoSet->next(), currentHypoSet->more() ) {
2854       isAvailable = false;
2855       if ( HypothesisData* algoDataIn = SMESH::GetHypothesisData( currentHypoSet->current() )) {
2856         for (int i = SMESH::DIM_0D; i <= myMaxShapeDim; i++) {
2857           int anCurrentAvailableAlgo = myAvailableHypData[i][Algo].indexOf( algoDataIn );
2858           if ( anCurrentAvailableAlgo > -1 ) {
2859             isAvailable = true;
2860             break;
2861           }
2862         }
2863         if ( !isAvailable )
2864           break;
2865       }
2866     }
2867     if ( isAvailable )
2868       aFilteredHypothesesSetsList.append( *inHypoSetName );
2869   }
2870   myDlg->setHypoSets( aFilteredHypothesesSetsList );
2871 }
2872
2873 //================================================================================
2874 /*!
2875  * \brief Get current name types of mesh
2876   * \param theIndex - current index types of mesh
2877   * \retval QString - result
2878  */
2879 //================================================================================
2880 QString SMESHGUI_MeshOp::currentMeshTypeName( const int theIndex ) const
2881 {
2882   QString aMeshType = "";
2883   switch ( theIndex ) {
2884   case MT_ANY:
2885     aMeshType = "ANY";
2886     break;
2887   case MT_TRIANGULAR:
2888     aMeshType = "TRIA";
2889     break;
2890   case MT_QUADRILATERAL:
2891     aMeshType = "QUAD";
2892     break;
2893   case MT_TETRAHEDRAL:
2894     aMeshType = "TETRA";
2895     break;
2896   case MT_HEXAHEDRAL:
2897     aMeshType = "HEXA";
2898     break;
2899   default:;
2900   }
2901   return aMeshType;
2902 }
2903