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