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