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