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