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