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