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