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