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