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