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