Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // SMESH SMESHGUI : GUI for SMESH component
23 // File   : SMESHGUI_MeshOp.cxx
24 // Author : Sergey LITONIN, Open CASCADE S.A.S.
25 // SMESH includes
26 //
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       for (int i = SMESH::DIM_3D; i > shapeDim; i--) {
547         // reset algos before disabling tabs (0020138)
548         onAlgoSelected(-1, i);
549       }
550       myDlg->setMaxHypoDim( shapeDim );
551
552
553       if (!myToCreate) // edition: read hypotheses
554       {
555         if (pObj != 0)
556         {
557           SMESH::SMESH_subMesh_var aVar =
558             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
559           myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
560           myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, true );
561           myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
562           myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->hide();
563           myDlg->updateGeometry();
564           myDlg->adjustSize();
565           readMesh();
566         }
567         else
568           myDlg->reset();
569       }
570       else if ( !myIsMesh ) // submesh creation
571       {
572         // if a submesh on the selected shape already exist, pass to submesh edition mode
573         if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
574           SMESH::SMESH_subMesh_var sm =
575             SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
576           bool editSubmesh = ( !sm->_is_nil() &&
577                                SUIT_MessageBox::question( myDlg, tr( "SMESH_WARNING" ),
578                                                           tr( "EDIT_SUBMESH_QUESTION"),
579                                                           SUIT_MessageBox::Yes | 
580                                                           SUIT_MessageBox::No,
581                                                           SUIT_MessageBox::No )
582                                == SUIT_MessageBox::Yes );
583           if ( editSubmesh )
584           {
585             selectionMgr()->clearFilters();
586             selectObject( pSubmesh );
587             SMESHGUI::GetSMESHGUI()->switchToOperation(704);
588             return;
589           }
590           else
591           {
592             myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
593             selectObject( _PTR(SObject)() );
594             selectionDone();
595             return;
596           }
597         }
598         // discard selected mesh if submesh creation not allowed because of
599         // a global algorithm that does not support submeshes
600         if ( char* algoName = isSubmeshIgnored() ) {
601           SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ),
602                                     tr("SUBMESH_NOT_ALLOWED").arg(algoName));
603           CORBA::string_free( algoName );
604           myDlg->selectObject( "", SMESHGUI_MeshDlg::Mesh, "" );
605           selectObject( _PTR(SObject)() );
606           selectionDone();
607           return;
608         }
609
610         // enable/disable popup for choice of geom selection way
611         bool enable = false;
612         QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
613         if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
614           SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
615           if ( !mesh->_is_nil() )
616             enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
617         }
618         myDlg->setGeomPopupEnabled( enable );
619       }
620     }
621     else {
622       myDlg->enableTab( SMESH::DIM_3D );
623       QStringList hypList;
624       availableHyps( SMESH::DIM_3D, Algo, hypList,
625                      myAvailableHypData[SMESH::DIM_3D][Algo]);
626
627       SMESHGUI_MeshTab* aTab = myDlg->tab( SMESH::DIM_3D );
628       aTab->setAvailableHyps( Algo, hypList );
629       for (int i = SMESH::DIM_0D;i < SMESH::DIM_3D; ++i) {
630         myDlg->disableTab(i);
631       }
632       //Hide labels and fields (Mesh ang Geometry)
633       myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, false );
634       myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, false );
635       myDlg->adjustSize();
636       readMesh();
637     }
638   }
639   catch ( const SALOME::SALOME_Exception& S_ex )
640   {
641     SalomeApp_Tools::QtCatchCorbaException( S_ex );
642   }
643   catch ( ... )
644   {
645   }
646 }
647
648 //================================================================================
649 /*!
650  * \brief Verifies validity of input data
651   * \param theMess - Output parameter intended for returning error message
652   * \retval bool  - TRUE if input data is valid, false otherwise
653  *
654  * Verifies validity of input data. This method is called when "Apply" or "OK" button
655  * is pressed before mesh creation or editing.
656  */
657 //================================================================================
658 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
659 {
660   // Selected object to be  edited
661   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
662   {
663     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
664     return false;
665   }
666
667   // Name
668   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj ).trimmed();
669   if ( aMeshName.isEmpty() )
670   {
671     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
672     return false;
673   }
674
675 /*  // Imported mesh, if create sub-mesh or edit mesh
676   if ( !myToCreate || ( myToCreate && !myIsMesh ))
677   {
678     QString aMeshEntry = myDlg->selectedObject
679       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
680     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
681       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
682       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
683         theMess = tr( "IMPORTED_MESH" );
684         return false;
685       }
686     }
687   }*/
688
689   // Geom
690   if ( myToCreate )
691   {
692     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
693     if ( aGeomEntry == "" )
694     {
695       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
696       return false;
697     }
698     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
699     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
700     {
701       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
702       return false;
703     }
704
705     // Mesh
706     if ( !myIsMesh ) // i.e sub-mesh creation,
707     {
708       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
709       if ( aMeshEntry == "" )
710       {
711         theMess = tr( "MESH_IS_NOT_DEFINED" );
712         return false;
713       }
714       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
715       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
716       {
717         theMess = tr( "MESH_IS_NULL" );
718         return false;
719       }
720       if ( !isSubshapeOk() )
721       {
722         theMess = tr( "INVALID_SUBSHAPE" );
723         return false;
724       }
725     }
726   }
727
728   return true;
729 }
730
731 //================================================================================
732 /*!
733  * \brief check compatibility of the algorithm and another algorithm or hypothesis
734   * \param theAlgoData - algorithm data
735   * \param theHypData - hypothesis data
736   * \param theHypType - hypothesis type
737   * \param theHypTypeName - hypothesis type name, must be provided if 2-nd arg is not algo
738   * \retval bool - check result
739  */
740 //================================================================================
741
742 static bool isCompatible(const HypothesisData* theAlgoData,
743                          const HypothesisData* theHypData,
744                          const int             theHypType)
745 {
746   if ( !theAlgoData )
747     return true;
748
749   if ( theHypType == SMESHGUI_MeshOp::Algo )
750     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
751
752   bool isOptional;
753   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
754 }
755
756 //================================================================================
757 /*!
758  * \brief Gets available hypotheses or algorithms
759   * \param theDim - specifies dimension of returned hypotheses/algorifms
760   * \param theHypType - specifies whether algorims or hypotheses or additional ones
761   * are retrieved (possible values are in HypType enumeration)
762   * \param theHyps - Output list of hypotheses' names
763   * \param theAlgoData - to select hypos able to be used by this algo (optional)
764  *
765  * Gets available hypotheses or algorithm in accordance with input parameters
766  */
767 //================================================================================
768 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
769                                      const int       theHypType,
770                                      QStringList&    theHyps,
771                                      THypDataList&   theDataList,
772                                      HypothesisData* theAlgoData ) const
773 {
774   theDataList.clear();
775   theHyps.clear();
776   bool isAlgo = ( theHypType == Algo );
777   bool isAux  = ( theHypType == AddHyp );
778   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux, myIsOnGeometry );
779
780   QStringList::const_iterator anIter;
781   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
782   {
783     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
784     if ( isCompatible ( theAlgoData, aData, theHypType )) {
785       theDataList.append( aData );
786       theHyps.append( aData->Label );
787     }
788   }
789 }
790
791 //================================================================================
792 /*!
793  * \brief Gets existing hypotheses or algorithms
794   * \param theDim - specifies dimension of returned hypotheses/algorifms
795   * \param theHypType - specifies whether algorims or hypotheses or additional ones
796   * are retrieved (possible values are in HypType enumeration)
797   * \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
798   * \param theHyps - output list of names.
799   * \param theHypVars - output list of variables.
800   * \param theAlgoData - to select hypos able to be used by this algo (optional)
801  *
802  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
803  * input parameters
804  */
805 //================================================================================
806 void SMESHGUI_MeshOp::existingHyps( const int theDim,
807                                     const int theHypType,
808                                     _PTR(SObject) theFather,
809                                     QStringList& theHyps,
810                                     THypList& theHypList,
811                                     HypothesisData* theAlgoData)
812 {
813   // Clear hypoheses list
814   theHyps.clear();
815   theHypList.clear();
816
817   if ( !theFather )
818     return;
819
820   const bool isAux  = ( theHypType == AddHyp );
821
822   _PTR(SObject)          aHypRoot;
823   _PTR(GenericAttribute) anAttr;
824   _PTR(AttributeName)    aName;
825   _PTR(AttributeIOR)     anIOR;
826
827   bool isMesh = !_CAST( SComponent, theFather );
828   int aPart = -1;
829   if ( isMesh )
830     aPart = theHypType == Algo ? SMESH::Tag_RefOnAppliedAlgorithms : SMESH::Tag_RefOnAppliedHypothesis;
831   else
832     aPart = theHypType == Algo ? SMESH::Tag_AlgorithmsRoot : SMESH::Tag_HypothesisRoot;
833
834   if ( theFather->FindSubObject( aPart, aHypRoot ) )
835   {
836     _PTR(ChildIterator) anIter =
837       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
838     for ( ; anIter->More(); anIter->Next() )
839     {
840       _PTR(SObject) anObj = anIter->Value();
841       if ( isMesh ) // i.e. mesh or submesh
842       {
843         _PTR(SObject) aRefObj;
844         if ( anObj->ReferencedObject( aRefObj ) )
845           anObj = aRefObj;
846         else
847           continue;
848       }
849       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
850       {
851         aName = anAttr;
852         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
853         if ( !CORBA::is_nil( aVar ) )
854         {
855           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
856           if ( !aHypVar->_is_nil() )
857           {
858             HypothesisData* aData = SMESH::GetHypothesisData( aHypVar->GetName() );
859             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
860                  ( isCompatible ( theAlgoData, aData, theHypType )) &&
861                  ( isAux == aData->IsAux ))
862             {
863               std::string aHypName = aName->Value();
864               theHyps.append( aHypName.c_str() );
865               theHypList.append( THypItem( aHypVar, aHypName.c_str() ) );
866             }
867           }
868         }
869       }
870     }
871   }
872 }
873
874 //================================================================================
875 /*!
876  * \brief If create or edit a submesh, return a hypothesis holding parameters used
877  *        to mesh a subshape
878   * \param aHypType - The hypothesis type name
879   * \param aServerLib - Server library name
880   * \param hypData - The structure holding the hypothesis type etc.
881   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
882  */
883 //================================================================================
884
885 SMESH::SMESH_Hypothesis_var
886 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
887                                           const QString& aServerLib ) const
888 {
889   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
890     return SMESH::SMESH_Hypothesis::_nil();
891
892   const int nbColonsInMeshEntry = 3;
893   bool isSubMesh = myToCreate ?
894     !myIsMesh :
895     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).count(':') > nbColonsInMeshEntry;
896
897   // get mesh and geom object
898   SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
899   GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
900
901   QString anEntry;
902   if ( isSubMesh )
903   {
904     anEntry = myDlg->selectedObject
905       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
906     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
907     {
908       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
909       if ( myToCreate ) // mesh and geom may be selected
910       {
911         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
912         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
913         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
914           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
915       }
916       else // edition: sub-mesh may be selected
917       {
918         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
919         if ( !sm->_is_nil() ) {
920           aMeshVar = sm->GetFather();
921           aGeomVar = sm->GetSubShape();
922         }
923       }
924     }
925   }
926   else // mesh
927   {
928     if ( !myToCreate ) // mesh to edit can be selected
929     {
930       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
931       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
932       {
933         aMeshVar = SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
934         if ( !aMeshVar->_is_nil() )
935           aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pMesh );
936       }
937     }
938     if ( aGeomVar->_is_nil() ) {
939       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
940       if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
941       {
942         aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
943       }
944     }
945   }
946
947   return SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
948                                                                 aServerLib.toLatin1().data(),
949                                                                 aMeshVar,
950                                                                 aGeomVar,
951                                                                 /*byMesh = */isSubMesh);
952
953 }
954
955 //================================================================================
956 /*!
957  * \Brief Returns tab dimention
958   * \param tab - the tab in the dlg
959   * \param dlg - my dialogue
960   * \retval int - dimention
961  */
962 //================================================================================
963
964 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
965 {
966   int aDim = -1;
967   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
968     if (tab == dlg->tab(i))
969       aDim = i;
970   return aDim;
971 }
972
973 //================================================================================
974 /*!
975  * \brief Create hypothesis
976   * \param theHypType - hypothesis category (main or additional)
977   * \param theIndex - index of type of hypothesis to be cerated
978  *
979  * Specifies dimension of hypothesis to be created (using sender() method),
980  * specifies its type and calls method for hypothesis creation
981  */
982 //================================================================================
983 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
984 {
985   // Specifies dimension of hypothesis to be created
986   int aDim = getTabDim( sender(), myDlg );
987   if (aDim == -1)
988     return;
989
990   // Specifies type of hypothesis to be created
991   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
992   if (theIndex < 0 || theIndex >= dataList.count())
993     return;
994   QString aHypTypeName = dataList[ theIndex ]->TypeName;
995
996   // Create hypothesis
997   createHypothesis(aDim, theHypType, aHypTypeName);
998 }
999
1000 //================================================================================
1001 /*!
1002  *  Create hypothesis and update dialog.
1003  *  \param theDim - dimension of hypothesis to be created
1004  *  \param theType - hypothesis category (algorithm, hypothesis, additional hypothesis)
1005  *  \param theTypeName - specifies hypothesis to be created
1006  */
1007 //================================================================================
1008 namespace
1009 {
1010   QString GetUniqueName (const QStringList& theHypNames,
1011                          const QString& theName,
1012                          size_t theIteration = 1)
1013   {
1014     QString aName = theName + "_" + QString::number( theIteration );
1015     if ( theHypNames.contains( aName ) )
1016       return GetUniqueName( theHypNames, theName, ++theIteration );
1017     return aName;
1018   }
1019 }
1020
1021 void SMESHGUI_MeshOp::createHypothesis (const int theDim,
1022                                         const int theType,
1023                                         const QString& theTypeName)
1024 {
1025   // During a hypothesis creation we might need to select some objects.
1026   // Main dialog must not update it's own selected objects in this case.
1027   dlg()->deactivateAll();
1028
1029   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName);
1030   if (!aData)
1031     return;
1032
1033   QStringList aHypNames;
1034   TDim2Type2HypList::const_iterator aDimIter = myExistingHyps.begin();
1035   for ( ; aDimIter != myExistingHyps.end(); aDimIter++) {
1036     const TType2HypList& aType2HypList = aDimIter.value();
1037     TType2HypList::const_iterator aTypeIter = aType2HypList.begin();
1038     for ( ; aTypeIter != aType2HypList.end(); aTypeIter++) {
1039       const THypList& aHypList = aTypeIter.value();
1040       THypList::const_iterator anIter = aHypList.begin();
1041       for ( ; anIter != aHypList.end(); anIter++) {
1042         const THypItem& aHypItem = *anIter;
1043         const QString& aHypName = aHypItem.second;
1044         aHypNames.append(aHypName);
1045       }
1046     }
1047   }
1048   QString aHypName = GetUniqueName( aHypNames, aData->Label);
1049
1050   // existing hypos
1051   int nbHyp = myExistingHyps[theDim][theType].count();
1052
1053   QString aClientLibName = aData->ClientLibName;
1054   if (aClientLibName == "") {
1055     // Call hypothesis creation server method (without GUI)
1056     SMESH::CreateHypothesis(theTypeName, aHypName, false);
1057   } else {
1058     // Get hypotheses creator client (GUI)
1059     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
1060
1061     // Create hypothesis
1062     if (aCreator) {
1063       // Get parameters appropriate to initialize a new hypothesis
1064       SMESH::SMESH_Hypothesis_var initParamHyp =
1065         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
1066       myDlg->setEnabled( false );
1067       aCreator->create(initParamHyp, aHypName, myDlg);
1068       myDlg->setEnabled( true );
1069     } else {
1070       SMESH::CreateHypothesis(theTypeName, aHypName, false);
1071     }
1072   }
1073
1074   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1075
1076   HypothesisData* algoData = hypData( theDim, Algo, currentHyp( theDim, Algo ));
1077   QStringList aNewHyps;
1078   existingHyps(theDim, theType, aFather, aNewHyps, myExistingHyps[theDim][theType], algoData);
1079   if (aNewHyps.count() > nbHyp) {
1080     for (int i = nbHyp; i < aNewHyps.count(); i++)
1081       myDlg->tab(theDim)->addHyp(theType, aNewHyps[i]);
1082   }
1083 }
1084
1085 //================================================================================
1086 /*!
1087  * \brief Calls plugin methods for hypothesis editing
1088   * \param theHypType - specifies whether main hypothesis or additional one
1089   * is edited
1090   * \param theIndex - index of existing hypothesis
1091  *
1092  * Calls plugin methods for hypothesis editing
1093  */
1094 //================================================================================
1095 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
1096 {
1097   // Speicfies dimension of hypothesis to be created
1098   int aDim = getTabDim( sender(), myDlg );
1099   if (aDim == -1)
1100     return;
1101
1102   const THypList& aList = myExistingHyps[ aDim ][ theHypType ];
1103   if ( theIndex < 0 || theIndex >= aList.count() )
1104     return;
1105   const THypItem& aHypItem = aList[ theIndex ];
1106   SMESH::SMESH_Hypothesis_var aHyp = aHypItem.first;
1107   if ( aHyp->_is_nil() )
1108     return;
1109
1110   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aHyp->GetName() );
1111   if ( aCreator ) {
1112     // Get initial parameters
1113     SMESH::SMESH_Hypothesis_var initParamHyp =
1114       getInitParamsHypothesis( aHyp->GetName(), aHyp->GetLibName());
1115     myDlg->setEnabled( false );
1116     aCreator->setInitParamsHypothesis( initParamHyp );
1117     aCreator->edit( aHyp.in(), aHypItem.second, dlg() );
1118     myDlg->setEnabled( true );
1119   }
1120 }
1121
1122 //================================================================================
1123 /*!
1124  * \brief access to hypothesis data
1125   * \param theDim - hyp dimension
1126   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
1127   * \param theIndex - index in the list
1128   * \retval HypothesisData* - result data, may be 0
1129  */
1130 //================================================================================
1131
1132 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
1133                                           const int theHypType,
1134                                           const int theIndex)
1135 {
1136   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
1137        theHypType > -1 && theHypType < NbHypTypes &&
1138        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
1139     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
1140   return 0;
1141 }
1142
1143 //================================================================================
1144 /*!
1145  * \brief Set available algos and hypos according to the selected algorithm
1146   * \param theIndex - algorithm index
1147  */
1148 //================================================================================
1149
1150 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
1151                                       const int theDim )
1152 {
1153   if ( myIgnoreAlgoSelection )
1154     return;
1155
1156   int aDim = theDim < 0 ? getTabDim( sender(), myDlg ): theDim;
1157   if (aDim == -1)
1158     return;
1159
1160   // find highest available dimension, all algos of this dimension are available for choice
1161   int aTopDim = -1;
1162   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
1163     if (isAccessibleDim( i ))
1164       aTopDim = i;
1165   if (aTopDim == -1)
1166     return;
1167
1168   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1169
1170   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1171   HypothesisData* algoByDim[4];
1172   algoByDim[ aDim ] = algoData;
1173
1174   QStringList anAvailable;
1175   if ( !algoData ) { // all algos becomes available
1176     availableHyps( aDim, Algo, anAvailable, myAvailableHypData[ aDim ][ Algo ]);
1177     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1178   }
1179
1180   // check that algorithms of other dimentions are compatible with
1181   // the selected one
1182
1183    // 2 loops: backward and forward from algo dimension
1184   for ( int forward = false; forward <= true; ++forward )
1185   {
1186     int dim = aDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
1187     if ( !forward ) {
1188       dim = aDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
1189     }
1190     HypothesisData* prevAlgo = algoData;
1191     bool noCompatible = false;
1192     for ( ; dim * dir <= lastDim * dir; dim += dir)
1193     {
1194       if ( !isAccessibleDim( dim ))
1195         continue;
1196       if ( noCompatible ) { // the selected algo has no compatible ones
1197         anAvailable.clear();
1198         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1199         myAvailableHypData[dim][Algo].clear();
1200         algoByDim[ dim ] = 0;
1201         continue;
1202       }
1203       // get currently selected algo
1204       int algoIndex = currentHyp( dim, Algo );
1205       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
1206       if ( curAlgo ) { // some algo selected
1207         if ( !isCompatible( prevAlgo, curAlgo, Algo ))
1208           curAlgo = 0;
1209       }
1210       // set new available algoritms
1211       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
1212       HypothesisData* soleCompatible = 0;
1213       if ( anAvailable.count() == 1 )
1214         soleCompatible = myAvailableHypData[dim][Algo][0];
1215       if ( dim == aTopDim && prevAlgo ) // all available algoritms should be selectable any way
1216         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], 0 );
1217       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1218       noCompatible = anAvailable.isEmpty();
1219
1220       // restore previously selected algo
1221       algoIndex = myAvailableHypData[dim][Algo].indexOf( curAlgo );
1222       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward && dim != SMESH::DIM_0D)
1223         // select the sole compatible algo
1224         algoIndex = myAvailableHypData[dim][Algo].indexOf( soleCompatible );
1225       setCurrentHyp( dim, Algo, algoIndex );
1226
1227       // remember current algo
1228       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1229     }
1230   }
1231
1232   // set hypotheses corresponding to the found algoritms
1233
1234   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1235
1236   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1237   {
1238     if ( !isAccessibleDim( dim ))
1239       continue;
1240     for ( int type = MainHyp; type < NbHypTypes; type++ )
1241     {
1242       myAvailableHypData[ dim ][ type ].clear();
1243       QStringList anAvailable, anExisting;
1244
1245       HypothesisData* curAlgo = algoByDim[ dim ];
1246       int hypIndex = currentHyp( dim, type );
1247
1248       SMESH::SMESH_Hypothesis_var curHyp;
1249       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1250         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ].first;
1251
1252       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
1253         // try to find algo by selected hypothesis in order to keep it selected
1254         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1255         CORBA::String_var curHypType = curHyp->GetName();
1256         if ( !algoDeselectedByUser &&
1257              myObjHyps[ dim ][ type ].count() > 0 &&
1258              curHypType == myObjHyps[ dim ][ type ].first().first->GetName())
1259         {
1260           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1261           for (int i = 0; i < myAvailableHypData[ dim ][ Algo ].count(); ++i) {
1262             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1263             if (curAlgo && hypData && isCompatible(curAlgo, hypData, type))
1264               break;
1265             else
1266               curAlgo = 0;
1267           }
1268         }
1269       }
1270       // get hyps compatible with curAlgo
1271       if ( curAlgo )
1272       {
1273         // check if a selected hyp is compatible with the curAlgo
1274         if ( !curHyp->_is_nil() ) {
1275           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1276           if ( !isCompatible( curAlgo, hypData, type ))
1277             curHyp = SMESH::SMESH_Hypothesis::_nil();
1278         }
1279         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
1280         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
1281       }
1282       // set list of hypotheses
1283       myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1284       myDlg->tab( dim )->setExistingHyps( type, anExisting );
1285
1286       // set current existing hypothesis
1287       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1288         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1289       else
1290         hypIndex = -1;
1291       if ( !isSubmesh && hypIndex < 0 && anExisting.count() == 1 ) {
1292         // none is yet selected => select the sole existing if it is not optional
1293         CORBA::String_var hypTypeName = myExistingHyps[ dim ][ type ].first().first->GetName();
1294         bool isOptional = true;
1295         if ( algoByDim[ dim ] &&
1296              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName.in(), isOptional ) &&
1297              !isOptional )
1298           hypIndex = 0;
1299       }
1300       setCurrentHyp( dim, type, hypIndex );
1301     }
1302   }
1303 }
1304
1305 //================================================================================
1306 /*!
1307  * \brief Creates and selects hypothesis of hypotheses set
1308  * \param theSetName - The name of hypotheses set
1309  */
1310 //================================================================================
1311 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1312 {
1313   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet(theSetName);
1314   if (!aHypoSet) return;
1315
1316   // clear all hyps
1317   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++) {
1318     setCurrentHyp(dim, Algo, -1);
1319     setCurrentHyp(dim, AddHyp, -1);
1320     setCurrentHyp(dim, MainHyp, -1);
1321   }
1322
1323   for (int aHypType = Algo; aHypType < AddHyp; aHypType++) {
1324     bool isAlgo = (aHypType == Algo);
1325
1326     // set hyps from the set
1327     QStringList* aHypoList = isAlgo ? &aHypoSet->AlgoList : &aHypoSet->HypoList;
1328     for (int i = 0, n = aHypoList->count(); i < n; i++) {
1329       const QString& aHypoTypeName = (*aHypoList)[ i ];
1330       HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
1331       if (!aHypData)
1332         continue;
1333
1334       int aDim = aHypData->Dim[0];
1335       // create or/and set
1336       if (isAlgo) {
1337         int index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
1338         if ( index < 0 ) {
1339           QStringList anAvailable;
1340           availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
1341           myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1342           index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
1343         }
1344         setCurrentHyp( aDim, Algo, index );
1345         onAlgoSelected( index, aDim );
1346       }
1347       else {
1348         bool mainHyp = true;
1349         QStringList anAvailable;
1350         availableHyps( aDim, MainHyp, anAvailable, myAvailableHypData[aDim][MainHyp] );
1351         myDlg->tab( aDim )->setAvailableHyps( MainHyp, anAvailable );
1352         int index = myAvailableHypData[aDim][MainHyp].indexOf( aHypData );
1353         if ( index < 0 ) {
1354           mainHyp = false;
1355           index = myAvailableHypData[aDim][AddHyp].indexOf( aHypData );
1356         }
1357         if (index >= 0)
1358           createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
1359       }
1360     } // loop on hypos in the set
1361   } // loop on algo/hypo
1362 }
1363
1364 //================================================================================
1365 /*!
1366  * \brief Creates mesh
1367   * \param theMess - Output parameter intended for returning error message
1368   * \retval bool  - TRUE if mesh is created, FALSE otherwise
1369  *
1370  * Creates mesh
1371  */
1372 //================================================================================
1373 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
1374 {
1375   theMess = "";
1376
1377   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1378   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1379
1380   QStringList aList;
1381   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1382   QStringList::Iterator it = aList.begin();
1383   for ( ; it!=aList.end(); it++)
1384   {
1385     QString aGeomEntry = *it;
1386     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
1387     GEOM::GEOM_Object_var aGeomVar =
1388       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1389
1390     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1391     if ( aSMESHGen->_is_nil() )
1392       return false;
1393
1394     SUIT_OverrideCursor aWaitCursor;
1395
1396     // create mesh
1397     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
1398     if ( aMeshVar->_is_nil() )
1399       return false;
1400     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1401     if ( aMeshSO )
1402       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
1403
1404     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ ) {
1405       if ( !isAccessibleDim( aDim )) continue;
1406
1407       // assign hypotheses
1408       for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ ) {
1409         int aHypIndex = currentHyp( aDim, aHypType );
1410         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() ) {
1411           SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
1412           if ( !aHypVar->_is_nil() )
1413             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
1414         }
1415       }
1416       // find or create algorithm
1417       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1418       if ( !anAlgoVar->_is_nil() )
1419         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1420     }
1421
1422   }
1423   return true;
1424 }
1425
1426 //================================================================================
1427 /*!
1428  * \brief Creates sub-mesh
1429   * \param theMess - Output parameter intended for returning error message
1430   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1431  *
1432  * Creates sub-mesh
1433  */
1434 //================================================================================
1435 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
1436 {
1437   theMess = "";
1438
1439   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1440   if ( aSMESHGen->_is_nil() )
1441     return false;
1442
1443   // get mesh object
1444   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1445   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
1446   SMESH::SMESH_Mesh_var aMeshVar =
1447     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1448   if (aMeshVar->_is_nil())
1449     return false;
1450
1451   // GEOM shape of the main mesh
1452   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
1453
1454   // Name for the new sub-mesh
1455   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
1456
1457   // get geom object
1458   GEOM::GEOM_Object_var aGeomVar;
1459   QStringList aGEOMs;
1460   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
1461   if (aGEOMs.count() == 1)
1462   {
1463     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1464     QString aGeomEntry = aGEOMs.first();
1465     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
1466     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1467   }
1468   else if (aGEOMs.count() > 1)
1469   {
1470     // create a GEOM group
1471     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1472     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1473     if (!geomGen->_is_nil() && aStudy) {
1474       GEOM::GEOM_IGroupOperations_var op =
1475         geomGen->GetIGroupOperations(aStudy->StudyId());
1476       if (!op->_is_nil()) {
1477         // check and add all selected GEOM objects: they must be
1478         // a sub-shapes of the main GEOM and must be of one type
1479         int iSubSh = 0;
1480         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1481         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
1482         aSeq->length(aGEOMs.count());
1483         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
1484         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
1485           QString aSubGeomEntry = (*aSubShapesIter);
1486           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
1487           GEOM::GEOM_Object_var aSubGeomVar =
1488             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
1489           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
1490           if (iSubSh == 0) {
1491             aGroupType = aSubShapeType;
1492           } else {
1493             if (aSubShapeType != aGroupType)
1494               aGroupType = TopAbs_SHAPE;
1495           }
1496           aSeq[iSubSh] = aSubGeomVar;
1497         }
1498         // create a group
1499         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(mainGeom, aGroupType);
1500         op->UnionList(aGroupVar, aSeq);
1501
1502         if (op->IsDone()) {
1503           aGeomVar = aGroupVar;
1504
1505           // publish the GEOM group in study
1506           QString aNewGeomGroupName ("Auto_group_for_");
1507           aNewGeomGroupName += aName;
1508           SALOMEDS::SObject_var aNewGroupSO =
1509             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGeomVar, 
1510                                 aNewGeomGroupName.toLatin1().data(), mainGeom);
1511         }
1512       }
1513     }
1514   }
1515   else {
1516   }
1517   if (aGeomVar->_is_nil())
1518     return false;
1519
1520   SUIT_OverrideCursor aWaitCursor;
1521
1522   // create sub-mesh
1523   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.toLatin1().data() );
1524   _PTR(SObject) aSubMeshSO = SMESH::FindSObject( aSubMeshVar.in() );
1525   if ( aSubMeshSO )
1526     SMESH::SetName( aSubMeshSO, aName.toLatin1().data() );
1527
1528   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
1529   {
1530     if ( !isAccessibleDim( aDim )) continue;
1531
1532     // find or create algorithm
1533     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1534     if ( !anAlgoVar->_is_nil() )
1535       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1536     // assign hypotheses
1537     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1538     {
1539       int aHypIndex = currentHyp( aDim, aHypType );
1540       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1541       {
1542         SMESH::SMESH_Hypothesis_var aHypVar =
1543           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
1544         if ( !aHypVar->_is_nil() )
1545           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1546       }
1547     }
1548   }
1549
1550   // deselect geometry: next submesh should be created on other subshape
1551   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
1552   selectObject( _PTR(SObject)() );
1553   selectionDone();
1554
1555   return true;
1556 }
1557
1558 //================================================================================
1559 /*!
1560  * \brief Gets current hypothesis or algorithms
1561   * \param theDim - dimension of hypothesis or algorithm
1562   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1563   * \retval int - current hypothesis or algorithms
1564  *
1565  * Gets current hypothesis or algorithms
1566  */
1567 //================================================================================
1568 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1569 {
1570   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1571 }
1572
1573 //================================================================================
1574 /*!
1575  * \brief Returns true if hypotheses of given dim can be assigned
1576   * \param theDim - hypotheses dimension
1577   * \retval bool - result
1578  */
1579 //================================================================================
1580 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
1581 {
1582   return myDlg->tab( theDim )->isEnabled();
1583 }
1584
1585 //================================================================================
1586 /*!
1587  * \brief Sets current hypothesis or algorithms
1588   * \param theDim - dimension of hypothesis or algorithm
1589   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1590   * \param theIndex - Index of hypothesis
1591  *
1592  * Gets current hypothesis or algorithms
1593  */
1594 //================================================================================
1595 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1596                                      const int theHypType,
1597                                      const int theIndex )
1598 {
1599   myIgnoreAlgoSelection = true;
1600   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1601   myIgnoreAlgoSelection = false;
1602 }
1603
1604 //================================================================================
1605 /*!
1606  * \brief Generates default and sets mesh/submesh name
1607  *
1608  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1609  */
1610 //================================================================================
1611 void SMESHGUI_MeshOp::setDefaultName() const
1612 {
1613   QString aResName;
1614
1615   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1616   int i = 1;
1617   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1618   _PTR(SObject) anObj;
1619   do
1620   {
1621     aResName = aPrefix + QString::number( i++ );
1622     anObj = aStudy->FindObject( aResName.toLatin1().data() );
1623   }
1624   while ( anObj );
1625
1626   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1627     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1628   aControl->setText( aResName );
1629 }
1630
1631 //================================================================================
1632 /*!
1633  * \brief Gets algorithm or creates it if necessary
1634   * \param theDim - specifies dimension of returned hypotheses/algorifms
1635   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1636  *
1637  * Gets algorithm or creates it if necessary
1638  */
1639 //================================================================================
1640 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1641 {
1642   SMESH::SMESH_Hypothesis_var anAlgoVar;
1643
1644   // get type of the selected algo
1645   int aHypIndex = currentHyp( theDim, Algo );
1646   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
1647   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
1648     return anAlgoVar;
1649   QString aHypName = dataList[ aHypIndex ]->TypeName;
1650
1651   // get existing algoritms
1652   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1653   QStringList tmp;
1654   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
1655
1656   // look for anexisting algo of such a type
1657   THypList& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1658   THypList::iterator anIter = aHypVarList.begin();
1659   for ( ; anIter != aHypVarList.end(); anIter++)
1660   {
1661     SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
1662     CORBA::String_var aName = aHypVar->GetName();
1663     if ( !aHypVar->_is_nil() && aHypName == aName )
1664     {
1665       anAlgoVar = aHypVar;
1666       break;
1667     }
1668   }
1669
1670   if (anAlgoVar->_is_nil()) {
1671     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1672     if (aHypData) {
1673       QString aClientLibName = aHypData->ClientLibName;
1674       if (aClientLibName == "") {
1675         // Call hypothesis creation server method (without GUI)
1676         SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1677       } else {
1678         // Get hypotheses creator client (GUI)
1679         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
1680
1681         // Create algorithm
1682         if (aCreator)
1683           aCreator->create(true, aHypName, myDlg);
1684         else
1685           SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1686       }
1687       QStringList tmpList;
1688       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
1689       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
1690     }
1691
1692     THypList& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
1693     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
1694     {
1695       SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
1696       CORBA::String_var aName = aHypVar->GetName();
1697       if ( !aHypVar->_is_nil() && aHypName == aName )
1698       {
1699         anAlgoVar = aHypVar;
1700         break;
1701       }
1702     }
1703   }
1704
1705   return anAlgoVar._retn();
1706 }
1707
1708 //================================================================================
1709 /*!
1710  * \brief Reads parameters of edited mesh and assigns them to the dialog
1711  *
1712  * Reads parameters of edited mesh and assigns them to the dialog (called when
1713  * mesh is edited only)
1714  */
1715 //================================================================================
1716 void SMESHGUI_MeshOp::readMesh()
1717 {
1718   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1719   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1720   if ( !pObj )
1721     return;
1722
1723   if (myIsOnGeometry) {
1724     // Get name of mesh if current object is sub-mesh
1725     SMESH::SMESH_subMesh_var aSubMeshVar =
1726       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1727     if ( !aSubMeshVar->_is_nil() )
1728     {
1729       SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1730       if ( !aMeshVar->_is_nil() )
1731       {
1732         _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1733         QString aMeshName = name( aMeshSO );
1734         myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
1735       }
1736     }
1737
1738     // Get name of geometry object
1739     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1740     if ( !aGeomVar->_is_nil() )
1741     {
1742       _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
1743       QString aShapeName = name( aGeomSO );
1744       myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
1745     }
1746   }
1747
1748   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
1749   QStringList anExisting;
1750   const int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
1751   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
1752   {
1753     // get algorithm
1754     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
1755     // find algo index among available ones
1756     int aHypIndex = -1;
1757     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
1758     {
1759       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first().first;
1760       HypothesisData* algoData = SMESH::GetHypothesisData( aVar->GetName() );
1761       aHypIndex = myAvailableHypData[ dim ][ Algo ].indexOf ( algoData );
1762 //       if ( aHypIndex < 0 && algoData ) {
1763 //         // assigned algo is incompatible with other algorithms
1764 //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
1765 //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
1766 //       }
1767     }
1768     setCurrentHyp( dim, Algo, aHypIndex );
1769     // set existing and available hypothesis according to the selected algo
1770     onAlgoSelected( aHypIndex, dim );
1771   }
1772
1773   // get hypotheses
1774   bool hypWithoutAlgo = false;
1775   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
1776   {
1777     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1778     {
1779       // get hypotheses
1780       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1781       // find index of requered hypothesis among existing ones for this dimension and type
1782       int aHypIndex = -1;
1783       if ( myObjHyps[ dim ][ hypType ].count() > 0 ) {
1784         aHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
1785                           myExistingHyps[ dim ][ hypType ] );
1786         if ( aHypIndex < 0 ) {
1787           // assigned hypothesis is incompatible with the algorithm
1788           if ( currentHyp( dim, Algo ) < 0 )
1789           { // none algo selected; it is edition for sure, of submesh maybe
1790             hypWithoutAlgo = true;
1791             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ].first() );
1792             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
1793             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
1794           }
1795         }
1796       }
1797       setCurrentHyp( dim, hypType, aHypIndex );
1798     }
1799   }
1800   // make available other hyps of same type as one without algo
1801   if ( hypWithoutAlgo )
1802     onAlgoSelected( currentHyp( 0, Algo ), 0 );
1803 }
1804
1805 //================================================================================
1806 /*!
1807  * \brief Gets name of object
1808   * \param theSO - SObject
1809   * \retval QString - name of object
1810  *
1811  * Gets name of object
1812  */
1813 //================================================================================
1814 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1815 {
1816   QString aResName;
1817   if ( theSO )
1818   {
1819     _PTR(GenericAttribute) anAttr;
1820     _PTR(AttributeName)    aNameAttr;
1821     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1822     {
1823       aNameAttr = anAttr;
1824       aResName = aNameAttr->Value().c_str();
1825     }
1826   }
1827   return aResName;
1828 }
1829
1830 //================================================================================
1831 /*!
1832  * \brief Finds hypothesis in input list
1833   * \param theHyp - hypothesis to be found
1834   * \param theHypList - input list of hypotheses
1835   * \retval int - index of hypothesis or -1 if it is not found
1836  *
1837  * Finds position of hypothesis in input list
1838  */
1839 //================================================================================
1840 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1841                            const THypList& theHypList ) const
1842 {
1843   int aRes = -1;
1844   if ( !theHyp->_is_nil() )
1845   {
1846     int i = 0;
1847     THypList::const_iterator anIter = theHypList.begin();
1848     for ( ; anIter != theHypList.end(); ++ anIter)
1849     {
1850       if ( theHyp->_is_equivalent( (*anIter).first ) )
1851       {
1852         aRes = i;
1853         break;
1854       }
1855       i++;
1856     }
1857   }
1858   return aRes;
1859 }
1860
1861 //================================================================================
1862 /*!
1863  * \brief Edits mesh or sub-mesh
1864   * \param theMess - Output parameter intended for returning error message
1865   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1866  *
1867  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1868  */
1869 //================================================================================
1870 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1871 {
1872   theMess = "";
1873
1874   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1875   if ( aSMESHGen->_is_nil() )
1876     return false;
1877
1878   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1879   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1880   if ( !pObj )
1881     return false;
1882
1883   SUIT_OverrideCursor aWaitCursor;
1884
1885   // Set new name
1886   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
1887   SMESH::SetName( pObj, aName );
1888   int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
1889
1890   // First, remove old algos in order to avoid messages on algorithm hiding
1891   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
1892   {
1893     if ( isAccessibleDim( dim ) && myObjHyps[ dim ][ Algo ].count() > 0 )
1894     {
1895       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first().first;
1896       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1897       if ( anAlgoVar->_is_nil() || // no new algo selected or
1898            strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) ) // algo change
1899       {
1900         // remove old algorithm
1901         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first().first );
1902         myObjHyps[ dim ][ Algo ].clear();
1903       }
1904     }
1905   }
1906
1907   // Assign new algorithms and hypotheses
1908   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
1909   {
1910     if ( !isAccessibleDim( dim )) continue;
1911
1912     // find or create algorithm
1913     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1914
1915     // assign new algorithm
1916     if ( !anAlgoVar->_is_nil() && // some algo selected and
1917          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
1918     {
1919       SALOMEDS_SObject* aSObject = _CAST(SObject, pObj);
1920       CORBA::Object_var anObject = aSObject->GetObject();
1921       SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_narrow( anObject );
1922       bool isMesh = !aMeshVar->_is_nil();
1923       if ( isMesh ) {
1924         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1925       } else {
1926         SMESH::SMESH_subMesh_var aVar =
1927           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1928         if ( !aVar->_is_nil() )
1929           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1930       }
1931       myObjHyps[ dim ][ Algo ].append( THypItem( anAlgoVar, aName) );
1932     }
1933
1934     // assign hypotheses
1935     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1936     {
1937       int aNewHypIndex = currentHyp( dim, hypType );
1938       int anOldHypIndex = -1;
1939
1940       // remove old hypotheses
1941       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1942       {
1943         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
1944                               myExistingHyps[ dim ][ hypType ] );
1945         if ( aNewHypIndex != anOldHypIndex || // different hyps
1946              anOldHypIndex == -1 )            // hyps of different algos
1947         {
1948           SMESH::RemoveHypothesisOrAlgorithmOnMesh
1949             ( pObj, myObjHyps[ dim ][ hypType ].first().first );
1950           myObjHyps[ dim ][ hypType ].clear();
1951         }
1952       }
1953
1954       // assign new hypotheses
1955       if ( aNewHypIndex != anOldHypIndex && aNewHypIndex != -1 )
1956       {
1957         SMESH::SMESH_Mesh_var aMeshVar =
1958           SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1959         bool isMesh = !aMeshVar->_is_nil();
1960         if ( isMesh )
1961         {
1962           SMESH::AddHypothesisOnMesh
1963             (aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
1964         }
1965         else
1966         {
1967           SMESH::SMESH_subMesh_var aVar =
1968             SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1969           if ( !aVar->_is_nil() )
1970             SMESH::AddHypothesisOnSubMesh
1971               ( aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
1972         }
1973       }
1974       // reread all hypotheses of mesh if necessary
1975       QStringList anExisting;
1976       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1977     }
1978   }
1979
1980   return true;
1981 }
1982
1983 //================================================================================
1984 /*!
1985  * \brief Verifies whether given operator is valid for this one
1986   * \param theOtherOp - other operation
1987   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1988 *
1989 * method redefined from base class verifies whether given operator is valid for
1990 * this one (i.e. can be started "above" this operator). In current implementation method
1991 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1992 * elements.
1993 */
1994 //================================================================================
1995 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1996 {
1997   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1998 }
1999
2000 //================================================================================
2001 /*!
2002  * \brief SLOT. Is called when the user selects a way of geometry selection
2003   * \param theByMesh - true if the user wants to find geometry by mesh element
2004  */
2005 //================================================================================
2006
2007 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
2008 {
2009   if ( theByMesh ) {
2010     if ( !myShapeByMeshOp ) {
2011       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
2012       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2013               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2014       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2015               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2016     }
2017     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2018     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
2019     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
2020       SMESH::SMESH_Mesh_var aMeshVar =
2021         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
2022       if ( !aMeshVar->_is_nil() ) {
2023         myDlg->hide(); // stop processing selection
2024         myShapeByMeshOp->setModule( getSMESHGUI() );
2025         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2026         myShapeByMeshOp->SetMesh( aMeshVar );
2027         myShapeByMeshOp->start();
2028       }
2029     }
2030   }
2031 }
2032
2033 //================================================================================
2034 /*!
2035  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2036  */
2037 //================================================================================
2038
2039 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
2040 {
2041   if ( myShapeByMeshOp == op ) {
2042     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
2043     myDlg->show();
2044     // Select a found geometry object
2045     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2046     if ( !aGeomVar->_is_nil() )
2047     {
2048       QString ID = aGeomVar->GetStudyEntry();
2049       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.toLatin1().data() )) {
2050         selectObject( aGeomSO );
2051         selectionDone();
2052       }
2053     }
2054   }
2055 }
2056
2057 //================================================================================
2058 /*!
2059  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2060  */
2061 //================================================================================
2062
2063 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
2064 {
2065   if ( myShapeByMeshOp == op && myDlg ) {
2066     myDlg->show();
2067   }
2068 }
2069
2070 //================================================================================
2071 /*!
2072  * \brief Selects a SObject
2073   * \param theSObj - the SObject to select
2074  */
2075 //================================================================================
2076
2077 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
2078 {
2079   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
2080     SALOME_ListIO anIOList;
2081     if ( theSObj ) {
2082       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2083         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
2084       anIOList.Append( anIO );
2085     }
2086     sm->setSelectedObjects( anIOList, false );
2087   }
2088 }