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