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