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