Salome HOME
Update of CheckDone
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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_GEOMGenUtils.h"
30 #include "SMESHGUI_GroupOnShapeDlg.h"
31 #include "SMESHGUI_Hypotheses.h"
32 #include "SMESHGUI_HypothesesUtils.h"
33 #include "SMESHGUI_MeshDlg.h"
34 #include "SMESHGUI_Operations.h"
35 #include "SMESHGUI_ShapeByMeshDlg.h"
36 #include "SMESHGUI_Utils.h"
37 #include "SMESH_NumberFilter.hxx"
38 #include "SMESH_TypeDefs.hxx"
39 #include "SMESH_TypeFilter.hxx"
40
41 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
42
43 // SALOME GEOM includes
44 #include <GEOMBase.h>
45 #include <GEOMImpl_Types.hxx>
46 #include <GEOM_SelectionFilter.h>
47 #include <GEOM_wrap.hxx>
48 #include <GeometryGUI.h>
49
50 // SALOME GUI includes
51 #include <LightApp_Application.h>
52 #include <LightApp_SelectionMgr.h>
53 #include <LightApp_UpdateFlags.h>
54 #include <SALOME_InteractiveObject.hxx>
55 #include <SALOME_ListIO.hxx>
56 #include <SUIT_MessageBox.h>
57 #include <SUIT_OverrideCursor.h>
58 #include <SUIT_ResourceMgr.h>
59 #include <SUIT_Session.h>
60 #include <SalomeApp_Application.h>
61 #include <SalomeApp_Tools.h>
62
63 // SALOME KERNEL includes
64 #include <SALOMEDS_SComponent.hxx>
65 #include <SALOMEDS_SObject.hxx>
66 #include <SALOMEDS_Study.hxx>
67 #include <SALOMEDS_wrap.hxx>
68
69 // Qt includes
70 #include <QApplication>
71 #include <QLineEdit>
72 #include <QStringList>
73
74 // OCCT includes
75 #include <BRep_Tool.hxx>
76 #include <TopExp.hxx>
77 #include <TopExp_Explorer.hxx>
78 #include <TopTools_IndexedMapOfShape.hxx>
79 #include <TopoDS.hxx>
80
81 //================================================================================
82 /*!
83  * \brief Constructor
84   * \param theToCreate - if this parameter is true then operation is used for creation,
85   * for editing otherwise
86  *
87  * Initialize operation
88 */
89 //================================================================================
90 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
91 : SMESHGUI_SelectionOp(),
92   myDlg( 0 ),
93   myShapeByMeshOp( 0 ),
94   myToCreate( theToCreate ),
95   myIsMesh( theIsMesh ),
96   myIsInvalidSubMesh( false )
97 {
98   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
99     GeometryGUI::InitGeomGen();
100   myIsOnGeometry = true;
101   myMaxShapeDim = -1;
102 }
103
104 //================================================================================
105 /*!
106  * \brief Destructor
107 */
108 //================================================================================
109 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
110 {
111   if ( myDlg )
112     delete myDlg;
113 }
114
115 //================================================================================
116 /*!
117  * \brief Gets dialog of this operation
118   * \retval LightApp_Dialog* - pointer to dialog of this operation
119 */
120 //================================================================================
121 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
122 {
123   return myDlg;
124 }
125
126 //================================================================================
127 /*!
128  * \brief Creates or edits mesh
129   * \retval bool - TRUE if operation is performed successfully, FALSE otherwise
130  *
131  * Virtual slot redefined from the base class called when "Apply" button is clicked
132  * creates or edits mesh
133  */
134 //================================================================================
135 bool SMESHGUI_MeshOp::onApply()
136 {
137   if (SMESHGUI::isStudyLocked())
138     return false;
139
140   QString aMess;
141   if ( !isValid( aMess ) )
142   {
143     dlg()->show();
144     if ( aMess != "" )
145       SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), aMess );
146     return false;
147   }
148
149   bool aResult = false;
150   aMess = "";
151   try
152   {
153     QStringList anEntryList;
154     if ( myToCreate && myIsMesh )
155       aResult = createMesh( aMess, anEntryList );
156     if (( myToCreate && !myIsMesh ) || myIsInvalidSubMesh )
157       aResult = createSubMesh( aMess, anEntryList );
158     else if ( !myToCreate )
159       aResult = editMeshOrSubMesh( aMess );
160     if ( aResult )
161     {
162       SMESHGUI::Modified();
163       update( UF_ObjBrowser | UF_Model );
164       if( LightApp_Application* anApp =
165           dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
166         myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
167     }
168   }
169   catch ( const SALOME::SALOME_Exception& S_ex )
170   {
171     SalomeApp_Tools::QtCatchCorbaException( S_ex );
172     aResult = false;
173   }
174   catch ( ... )
175   {
176     aResult = false;
177   }
178
179   if ( aResult )
180   {
181     if ( myToCreate )
182       setDefaultName();
183   }
184   else
185   {
186     if ( aMess == "" )
187       aMess = tr( "SMESH_OPERATION_FAILED" );
188     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), aMess );
189   }
190
191   myHypoSet = 0;
192
193   return aResult;
194 }
195
196 //================================================================================
197 /*!
198  * \brief Creates dialog if necessary and shows it
199  *
200  * Virtual method redefined from base class called when operation is started creates
201  * dialog if necessary and shows it, activates selection
202  */
203 //================================================================================
204 void SMESHGUI_MeshOp::startOperation()
205 {
206   myIgnoreAlgoSelection = false;
207
208   if (!myDlg)
209   {
210     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
211     for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
212     {
213       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
214                this, SLOT( onCreateHyp( const int, const int ) ) );
215       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
216                this, SLOT( onEditHyp( const int, const int ) ) );
217       connect( myDlg->tab( i ), SIGNAL( selectAlgo( const int ) ),
218                this, SLOT( onAlgoSelected( const int ) ) );
219     }
220     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
221     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
222     connect( myDlg, SIGNAL( selectMeshType( const int, const int ) ), SLOT( onAlgoSetByMeshType( const int, const int)));
223
224     if ( myToCreate ) {
225       if ( myIsMesh ) myHelpFileName = "constructing_meshes.html";
226       else            myHelpFileName = "constructing_submeshes.html";
227     }
228     else {
229       myHelpFileName = "editing_meshes.html";
230     }
231   }
232   SMESHGUI_SelectionOp::startOperation();
233
234   // clear available hypotheses
235   QStringList hypList;
236   for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
237   {
238     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
239     // set algos
240     aTab->setAvailableHyps( Algo, hypList );
241     aTab->setAvailableHyps( MainHyp, hypList );
242     aTab->setAvailableHyps( AddHyp, hypList );
243     aTab->setExistingHyps( MainHyp, hypList );
244     aTab->setExistingHyps( AddHyp, hypList );
245     myExistingHyps[ i ][ MainHyp ].clear();
246     myExistingHyps[ i ][ AddHyp ].clear();
247   }
248   if ( myToCreate )
249   {
250     setDefaultName();
251     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
252   }
253   else
254   {
255     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
256   }
257
258   updateMeshTypeList();
259   myDlg->setCurrentMeshType( MT_ANY );
260
261   myDlg->setCurrentTab( SMESH::DIM_3D );
262   myDlg->setGeomPopupEnabled(false);
263   myDlg->show();
264
265   selectionDone();
266
267   myHypoSet = 0;
268   myHasConcurrentSubBefore = false;
269   myObjectToSelect.clear();
270 }
271
272 //=================================================================================
273 /*!
274  * \brief Selects a recently created mesh or sub-mesh if necessary
275  *
276  * Virtual method redefined from base class called when operation is committed
277  * selects a recently created mesh or sub-mesh if necessary. Allows to perform
278  * selection when the custom selection filters are removed.
279  */
280 //=================================================================================
281 void SMESHGUI_MeshOp::commitOperation()
282 {
283   SMESHGUI_SelectionOp::commitOperation();
284
285   if ( !myObjectToSelect.isEmpty() ) {
286     if ( LightApp_SelectionMgr* aSelectionMgr = selectionMgr() ) {
287       SUIT_DataOwnerPtrList aList;
288       aList.append( new LightApp_DataOwner( myObjectToSelect ) );
289       aSelectionMgr->setSelected( aList );
290     }
291   }
292 }
293
294 //================================================================================
295 /*!
296  * \brief Creates selection filter
297  *  \param theId - identifier of current selection widget
298  *  \retval SUIT_SelectionFilter* - pointer to the created filter or null
299  *
300  * Creates selection filter in accordance with identifier of current selection widget
301  */
302 //================================================================================
303 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
304 {
305   if ( theId == SMESHGUI_MeshDlg::Geom )
306   {
307 //     TColStd_MapOfInteger allTypesMap;
308 //     for ( int i = 0; i < 10; i++ )
309 //       allTypesMap.Add( i );
310 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
311     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
312   }
313   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
314     return new SMESH_TypeFilter( SMESH::MESHorSUBMESH );
315   else if ( theId == SMESHGUI_MeshDlg::Mesh )
316     return new SMESH_TypeFilter( SMESH::MESH );
317   else
318     return 0;
319 }
320
321 //================================================================================
322 /*!
323  * \brief check if selected shape is a sub-shape of the shape to mesh
324   * \retval bool - check result
325  */
326 //================================================================================
327 bool SMESHGUI_MeshOp::isSubshapeOk() const
328 {
329   if ( !myToCreate || myIsMesh ) // not submesh creation
330     return false;
331
332   // mesh
333   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
334   _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() );
335   if (!pMesh) return false;
336
337   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
338   if (mesh->_is_nil()) return false;
339
340   // main shape of the mesh
341   GEOM::GEOM_Object_var mainGeom = mesh->GetShapeToMesh();
342   if (mainGeom->_is_nil()) return false;
343
344   // geometry
345   QStringList aGEOMs;
346   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
347
348   if (aGEOMs.count() > 0) {
349     GEOM::GEOM_Gen_var geomGen = mainGeom->GetGen();
350     if (geomGen->_is_nil()) return false;
351
352     GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations();
353     if (op->_is_nil()) return false;
354
355     // check all selected shapes
356     QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
357     for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++) {
358       QString aSubGeomEntry = (*aSubShapesIter);
359       _PTR(SObject) pSubGeom = SMESH::getStudy()->FindObjectID(aSubGeomEntry.toUtf8().data());
360       if (!pSubGeom) return false;
361
362       GEOM::GEOM_Object_var aSubGeomVar =
363         GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
364       if (aSubGeomVar->_is_nil()) return false;
365
366       // skl for NPAL14695 - implementation of searching of mainObj
367       GEOM::GEOM_Object_var mainObj = op->GetMainShape(aSubGeomVar); /* _var not _wrap as
368                                                                         mainObj already exists! */
369       while( !mainObj->_is_nil()) {
370         CORBA::String_var entry1 = mainObj->GetEntry();
371         CORBA::String_var entry2 = mainGeom->GetEntry();
372         if (std::string( entry1.in() ) == entry2.in() )
373           return true;
374         mainObj = op->GetMainShape(mainObj);
375       }
376       if ( aSubGeomVar->GetShapeType() == GEOM::COMPOUND )
377       {
378         // is aSubGeomVar a compound of sub-shapes?
379         GEOM::GEOM_IShapesOperations_wrap sop = geomGen->GetIShapesOperations();
380         if (sop->_is_nil()) return false;
381         GEOM::ListOfLong_var ids = sop->GetAllSubShapesIDs( aSubGeomVar,
382                                                             GEOM::SHAPE,/*sorted=*/false);
383         if ( ids->length() > 0 )
384         {
385           ids->length( 1 );
386           GEOM::GEOM_Object_var compSub = geomGen->AddSubShape( aSubGeomVar, ids );
387           if ( !compSub->_is_nil() )
388           {
389             GEOM::ListOfGO_var shared = sop->GetSharedShapes( mainGeom,
390                                                               compSub,
391                                                               compSub->GetShapeType() );
392             geomGen->RemoveObject( compSub );
393             compSub->UnRegister();
394             if ( shared->length() > 0 ) {
395               geomGen->RemoveObject( shared[0] );
396               shared[0]->UnRegister();
397             }
398             return ( shared->length() > 0 );
399           }
400         }
401       }
402     }
403   }
404
405   return false;
406 }
407
408 //================================================================================
409 /*!
410  * \brief Return name of the algorithm that does not support sub-meshes and makes
411  * sub-mesh creation useless
412  *  \retval char* - string is to be deleted!!!
413  */
414 //================================================================================
415 char* SMESHGUI_MeshOp::isSubmeshIgnored() const
416 {
417   if ( myToCreate && !myIsMesh ) {
418
419     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
420     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
421     _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() );
422     if ( pMesh ) {
423
424       QStringList algoNames;
425       THypList    algoList;
426       existingHyps(3, Algo, pMesh, algoNames, algoList);
427       if (!algoList.empty()) {
428         HypothesisData* algo =
429           SMESH::GetHypothesisData( SMESH::toQStr( algoList[0].first->GetName() ));
430         if ( algo &&
431              algo->InputTypes.empty() && // builds all dimensions it-self
432              !algo->IsSupportSubmeshes )
433           return CORBA::string_dup( algoNames[0].toUtf8().data() );
434       }
435
436 //       GEOM::GEOM_Object_var geom;
437 //       if (_PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( aGeomEntry.toUtf8().data() ))
438 //         geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
439
440 //       if ( !geom->_is_nil() && geom->GetShapeType() >= GEOM::FACE ) { // WIRE, EDGE as well
441         existingHyps(2, Algo, pMesh, algoNames, algoList);
442         if (!algoList.empty()) {
443           HypothesisData* algo =
444             SMESH::GetHypothesisData( SMESH::toQStr( algoList[0].first->GetName() ));
445           if ( algo &&
446                algo->InputTypes.empty() && // builds all dimensions it-self
447                !algo->IsSupportSubmeshes )
448             return CORBA::string_dup( algoNames[0].toUtf8().data() );
449         }
450         //       }
451     }
452   }
453   return 0;
454 }
455
456 //================================================================================
457 /*!
458  * \brief find an existing submesh by the selected shape
459  * \retval _PTR(SObject) - the found submesh SObject
460  */
461 //================================================================================
462 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
463 {
464   QString  aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
465   _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() );
466   if ( pMesh && !myGeom->_is_nil() ) {
467     int tag = -1;
468     switch ( myGeom->GetShapeType() ) {
469     case GEOM::VERTEX:   tag = SMESH::Tag_SubMeshOnVertex;   break;
470     case GEOM::EDGE:     tag = SMESH::Tag_SubMeshOnEdge;     break;
471     case GEOM::WIRE:     tag = SMESH::Tag_SubMeshOnWire;     break;
472     case GEOM::FACE:     tag = SMESH::Tag_SubMeshOnFace;     break;
473     case GEOM::SHELL:    tag = SMESH::Tag_SubMeshOnShell;    break;
474     case GEOM::SOLID:    tag = SMESH::Tag_SubMeshOnSolid;    break;
475     case GEOM::COMPOUND: tag = SMESH::Tag_SubMeshOnCompound; break;
476     default:;
477     }
478     _PTR(GenericAttribute) anAttr;
479     _PTR(SObject) aSubmeshRoot;
480     _PTR(Study) aStudy = SMESH::getStudy();
481     if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
482     {
483       _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
484       for ( ; smIter->More(); smIter->Next() )
485       {
486         _PTR(SObject) aSmObj = smIter->Value();
487         if ( ! aSmObj->FindAttribute( anAttr, "AttributeIOR" ))
488           continue;
489         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
490         for ( ; anIter1->More(); anIter1->Next()) {
491           _PTR(SObject) pGeom2 = anIter1->Value();
492           if ( pGeom2->ReferencedObject( pGeom2 ) &&
493                myGeomEntry == pGeom2->GetID().c_str() )
494             return aSmObj;
495         }
496       }
497     }
498   }
499   return _PTR(SObject)();
500 }
501
502 //================================================================================
503 /*!
504  * \brief Updates dialog's look and feel
505  *
506  * Virtual method redefined from the base class updates dialog's look and feel
507  */
508 //================================================================================
509 void SMESHGUI_MeshOp::selectionDone()
510 {
511   if (!dlg()->isVisible() || !myDlg->isEnabled())
512     return;
513
514   SMESHGUI_SelectionOp::selectionDone();
515
516   myGeomEntry.clear();
517   myGeom = GEOM::GEOM_Object::_nil();
518   myIsInvalidSubMesh = false;
519   myIsOnGeometry = true;
520
521   try
522   {
523     // allow selecting multiple geom objects only for sub-mesh creation
524     QStringList aGEOMs;
525     myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aGEOMs );
526     int nbGeoms = aGEOMs.count();
527     if ( nbGeoms > 0 )
528     {
529       myGeomEntry = aGEOMs[0];
530       _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( myGeomEntry.toUtf8().data() );
531       if ( nbGeoms > 1 && myToCreate && myIsMesh )
532       {
533         selectObject( pGeom );
534         selectionDone();
535         return;
536       }
537       myGeom = SMESH::SObjectToInterface< GEOM::GEOM_Object >( pGeom ); // creation case
538     }
539
540     QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
541     _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
542
543     SMESH::SMESH_Mesh_var    mesh = SMESH::SObjectToInterface< SMESH::SMESH_Mesh >( pObj );
544     SMESH::SMESH_subMesh_var subMesh;
545
546     if ( !myToCreate ) // edition: read hypotheses
547     {
548       if ( pObj )
549       {
550         subMesh  = SMESH::SObjectToInterface< SMESH::SMESH_subMesh >( pObj );
551         myIsMesh = subMesh->_is_nil();
552         myGeom   = SMESH::GetShapeOnMeshOrSubMesh( pObj );
553         myIsInvalidSubMesh = ( !myIsMesh && subMesh->GetId() < 1 );
554         if ( !mesh->_is_nil() && !mesh->HasShapeToMesh() )
555         {
556           myIsOnGeometry = false;
557           myGeom = GEOM::GEOM_Object::_nil();
558         }
559         if ( !myGeom->_is_nil() )
560           myGeomEntry = myGeom->GetStudyEntry();
561
562         myDlg->setTitile( myToCreate, myIsMesh );
563         myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !subMesh->_is_nil() );
564         myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, !myGeom->_is_nil() );
565         myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
566         if ( myIsInvalidSubMesh )
567         {
568           // it is necessary to select a new geometry
569           myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->show();
570           myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
571         }
572         else
573         {
574           myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->hide();
575         }
576         myDlg->updateGeometry();
577         myDlg->adjustSize();
578         readMesh();
579       }
580       else
581         myDlg->reset();
582     }
583     else if ( !myIsMesh ) // sub-mesh creation
584     {
585       // if a submesh on the selected shape already exist, pass to submesh edition mode
586       if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
587         subMesh = SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
588         bool editSubmesh = ( !subMesh->_is_nil() &&
589                              SUIT_MessageBox::question( myDlg, tr( "SMESH_WARNING" ),
590                                                         tr( "EDIT_SUBMESH_QUESTION"),
591                                                         SUIT_MessageBox::Yes |
592                                                         SUIT_MessageBox::No,
593                                                         SUIT_MessageBox::No )
594                              == SUIT_MessageBox::Yes );
595         if ( editSubmesh )
596         {
597           selectionMgr()->clearFilters();
598           selectObject( pSubmesh );
599           SMESHGUI::GetSMESHGUI()->switchToOperation( SMESHOp::OpEditMeshOrSubMesh );
600           return;
601         }
602         else
603         {
604           myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
605           selectObject( _PTR(SObject)() );
606           selectionDone();
607           return;
608         }
609       }
610       // discard selected mesh if submesh creation not allowed because of
611       // a global algorithm that does not support submeshes
612       if ( char* algoName = isSubmeshIgnored() ) {
613         SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ),
614                                   tr("SUBMESH_NOT_ALLOWED").arg(algoName));
615         CORBA::string_free( algoName );
616         myDlg->selectObject( "", SMESHGUI_MeshDlg::Mesh, "" );
617         selectObject( _PTR(SObject)() );
618         selectionDone();
619         return;
620       }
621
622       // enable/disable popup for choice of geom selection way
623       bool enable = false;
624       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
625       if ( _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toLatin1().data() ))
626       {
627         mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
628         if ( !mesh->_is_nil() )
629           enable = /*( shapeDim > 1 ) && */( mesh->NbEdges() > 0 );
630       }
631       myDlg->setGeomPopupEnabled( enable );
632
633       //rnv: issue 21056: "Create Sub Mesh": focus should automatically switch to geometry
634       if ( pObj && myGeomEntry.isEmpty() )
635         myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
636     }
637
638     setFilteredAlgoData();
639
640   }
641   catch ( const SALOME::SALOME_Exception& S_ex )
642   {
643     SalomeApp_Tools::QtCatchCorbaException( S_ex );
644   }
645   catch ( ... )
646   {
647   }
648 }
649
650 //================================================================================
651 /*!
652  * \brief Verifies validity of input data
653   * \param theMess - Output parameter intended for returning error message
654   * \retval bool  - TRUE if input data is valid, false otherwise
655  *
656  * Verifies validity of input data. This method is called when "Apply" or "OK" button
657  * is pressed before mesh creation or editing.
658  */
659 //================================================================================
660 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
661 {
662   // Selected object to be  edited
663   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
664   {
665     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
666     return false;
667   }
668
669   // Name
670   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj ).trimmed();
671   if ( aMeshName.isEmpty() )
672   {
673     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
674     return false;
675   }
676
677 /*  // Imported mesh, if create sub-mesh or edit mesh
678   if ( !myToCreate || ( myToCreate && !myIsMesh ))
679   {
680     QString aMeshEntry = myDlg->selectedObject
681       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
682     if ( _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() )) {
683       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
684       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
685         theMess = tr( "IMPORTED_MESH" );
686         return false;
687       }
688     }
689   }*/
690
691   // Geom
692   if ( myToCreate || myIsInvalidSubMesh )
693   {
694     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
695     if ( aGeomEntry.isEmpty() )
696     {
697       theMess = tr( myIsMesh ?
698                     "GEOMETRY_OBJECT_IS_NOT_DEFINED_MESH" :
699                     "GEOMETRY_OBJECT_IS_NOT_DEFINED_SUBMESH");
700       if ( !myIsMesh )
701         return false;
702       dlg()->show();
703       if ( SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), theMess,
704            SUIT_MessageBox::Yes, SUIT_MessageBox::No ) == SUIT_MessageBox::No )
705       {
706         theMess = "";
707         return false;
708       }
709       return true;
710     }
711     _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( aGeomEntry.toUtf8().data() );
712     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
713     {
714       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
715       return false;
716     }
717
718     // Mesh
719     if ( !myIsMesh ) // i.e sub-mesh creation,
720     {
721       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
722       if ( aMeshEntry == "" )
723       {
724         theMess = tr( "MESH_IS_NOT_DEFINED" );
725         return false;
726       }
727       _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() );
728       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
729       {
730         theMess = tr( "MESH_IS_NULL" );
731         return false;
732       }
733       if ( !isSubshapeOk() )
734       {
735         theMess = tr( "INVALID_SUBSHAPE" );
736         return false;
737       }
738     }
739   }
740
741   return true;
742 }
743
744 //================================================================================
745 /*!
746  * \brief check compatibility of the algorithm and another algorithm or hypothesis
747  *  \param theAlgoData - algorithm data
748  *  \param theHypData - hypothesis data
749  *  \param theHypType - hypothesis type
750  *  \retval bool - check result
751  */
752 //================================================================================
753 static bool isCompatible(const HypothesisData* theAlgoData,
754                          const HypothesisData* theHypData,
755                          const int             theHypType)
756 {
757   if ( !theAlgoData )
758     return true;
759
760   if ( theHypType == SMESHGUI_MeshOp::Algo ) // compatibility of 2 algos
761     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
762
763   bool isOptional; // compatibility of algo and hyp
764   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
765 }
766
767 //================================================================================
768 /*!
769  * \brief check compatibility of the geometry
770  * \param theAlgoData - to select hypos able to be used by this algo
771  * \param theDim - algo dimension
772  * \retval bool - check result
773  */
774 //================================================================================
775 bool SMESHGUI_MeshOp::isCompatibleToGeometry(HypothesisData* theAlgoData,
776                                              const int       theDim)
777 {
778   if ( theDim < 1 || myGeom->_is_nil() )
779     return true;
780
781   bool isApplicable = false;
782   if ( myGeomEntry == myLastGeomEntry && !myGeomEntry.isEmpty() ) {
783     THypLabelIsAppMap::const_iterator lab2isApp = myHypMapIsApplicable.find( theAlgoData->TypeName );
784     if ( lab2isApp != myHypMapIsApplicable.end() ) {
785       isApplicable = lab2isApp.value();
786       return isApplicable;
787     }
788   }
789   bool toCheckIsApplicableToAll = !myIsMesh;
790   if ( toCheckIsApplicableToAll )
791     toCheckIsApplicableToAll = ( myGeom->GetType() == GEOM_GROUP );
792
793   isApplicable = SMESH::IsApplicable( theAlgoData->TypeName, myGeom, toCheckIsApplicableToAll );
794   myHypMapIsApplicable.insert( theAlgoData->TypeName, isApplicable );
795   return isApplicable;
796 }
797
798 //================================================================================
799 /*!
800  * \brief check compatibility of the mesh type
801  *  \param theAlgoData - to select hypos able to be used by this algo
802  *  \param theDim - algo dimension
803  *  \retval bool - check result
804  */
805 //================================================================================
806 bool SMESHGUI_MeshOp::isCompatibleToMeshType(HypothesisData* theAlgoData,
807                                              const int       theDim)
808 {
809   if ( theDim < 2 )
810     return true;
811
812   QString elemType;
813   int curMeshType = myDlg->currentMeshType();
814   switch ( curMeshType ) {
815   case MT_ANY:
816     return true;
817
818   case MT_TRIANGULAR:
819     if ( theDim > 2 )
820       return false;
821     elemType = "TRIA";
822     break;
823
824   case MT_QUADRILATERAL:
825     if ( theDim > 2 )
826       return false;
827     elemType = "QUAD";
828     break;
829
830   case MT_TETRAHEDRAL:
831     if ( theDim < 3 )
832       elemType = "TRIA";
833     else
834       elemType = "TETRA";
835     break;
836
837   case MT_HEXAHEDRAL:
838     if ( theDim < 3 )
839       elemType = "QUAD";
840     else
841       elemType = "HEXA";
842     break;
843
844   default:
845     return true;
846   }
847
848   bool isAvailableAlgo = theAlgoData->OutputTypes.isEmpty();
849   QStringList::const_iterator inElemType = theAlgoData->OutputTypes.begin();
850   for ( ; inElemType != theAlgoData->OutputTypes.end(); inElemType++ )
851   {
852     if ( *inElemType == elemType )
853     {
854       isAvailableAlgo = true;
855       break;
856     }
857   }
858   return isAvailableAlgo;
859 }
860
861 //================================================================================
862 /*!
863  * \brief Get available hypotheses or algorithms
864  *  \param theDim - specifies dimension of returned hypotheses/algorifms
865  *  \param theHypType - specifies whether algorims or hypotheses or additional ones
866  *         are retrieved (possible values are in HypType enumeration)
867  *  \param theHyps - output list of hypotheses' names
868  *  \param thePrevAlgoData - to select hypos able to be used by previously algo (optional)
869  *  \param theNextAlgoData - to select hypos able to be used by next algo (optional)
870  *  \param theMeshType - type of mesh for filtering algorithms (optional)
871  *
872  * Get available hypotheses or algorithm in accordance with input parameters.
873  * Result (\a theHyps) optionally depends on the following:
874  * - selected algorithm(s)
875  * - selected geometry
876  * - selected mesh type
877  */
878 //================================================================================
879 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
880                                      const int       theHypType,
881                                      QStringList&    theHyps,
882                                      THypDataList&   theDataList,
883                                      HypothesisData* thePrevAlgoData,
884                                      HypothesisData* theNextAlgoData,
885                                      const QString&  /*theMeshType*/)
886 {
887   theDataList.clear();
888   theHyps.clear();
889   bool isAlgo = ( theHypType == Algo );
890   bool isAux  = ( theHypType >= AddHyp );
891   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux, myIsOnGeometry, !myIsMesh );
892
893   std::multimap< double, HypothesisData* > sortedHyps;
894   QStringList::const_iterator anIter;
895   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
896   {
897     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
898     if ( isCompatible( thePrevAlgoData, aData, theHypType ) &&
899          isCompatible( theNextAlgoData, aData, theHypType ) &&
900          isCompatibleToGeometry( aData, theDim )            &&
901          isCompatibleToMeshType( aData, theDim ))
902     {
903       int  groupID = aData->GroupID;
904       int priority = aData->Priority;
905       if ( groupID  < 0 || groupID > 9    ) groupID  = 9;
906       if ( priority < 0 || priority > 999 ) priority = 999;
907
908       sortedHyps.insert( std::make_pair( groupID + priority * 1e-3, aData ));
909     }
910   }
911
912   if ( !sortedHyps.empty() )
913   {
914     HypothesisData* aPrevGroup = SMESH::GetGroupTitle( sortedHyps.rbegin()->second, isAlgo );
915     std::multimap< double, HypothesisData* >::iterator key_hyp = sortedHyps.begin();
916     for ( ; key_hyp != sortedHyps.end(); ++key_hyp )
917     {
918       HypothesisData*  aData = key_hyp->second;
919       HypothesisData* aGroup = SMESH::GetGroupTitle( aData, isAlgo );
920       if ( aPrevGroup != aGroup )
921       {
922         theDataList.append( aGroup );
923         theHyps.append( aGroup->Label );
924         aPrevGroup = aGroup;
925       }
926       theDataList.append( aData );
927       theHyps.append( aData->Label );
928     }
929   }
930 }
931
932 //================================================================================
933 /*!
934  * \brief Gets existing hypotheses or algorithms
935  *  \param theDim - specifies dimension of returned hypotheses/algorifms
936  *  \param theHypType - specifies whether algorims or hypotheses or additional ones
937  *         are retrieved (possible values are in HypType enumeration)
938  *  \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
939  *  \param theHyps - output list of names.
940  *  \param theHypVars - output list of variables.
941  *  \param theAlgoData - to select hypos able to be used by this algo (optional)
942  *
943  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
944  * input parameters.
945  *
946  * WARNING: when using this method to get hyps existing in Mesh component,
947  *          call availableHyps() before in order to get only hyps of available types
948  *          that was filtered by availableHyps()
949  */
950 //================================================================================
951 void SMESHGUI_MeshOp::existingHyps( const int       theDim,
952                                     const int       theHypType,
953                                     _PTR(SObject)   theFather,
954                                     QStringList&    theHyps,
955                                     THypList&       theHypList,
956                                     HypothesisData* theAlgoData) const
957 {
958   // Clear hypoheses list
959   theHyps.clear();
960   theHypList.clear();
961
962   if ( !theFather )
963     return;
964
965   _PTR(SObject)          aHypRoot;
966   _PTR(GenericAttribute) anAttr;
967   _PTR(AttributeName)    aName;
968   _PTR(AttributeIOR)     anIOR;
969
970   const bool isMesh = !_CAST( SComponent, theFather );
971   int aPart = -1;
972   if ( isMesh )
973     aPart = theHypType == Algo ? SMESH::Tag_RefOnAppliedAlgorithms : SMESH::Tag_RefOnAppliedHypothesis;
974   else
975     aPart = theHypType == Algo ? SMESH::Tag_AlgorithmsRoot : SMESH::Tag_HypothesisRoot;
976
977   const bool isAux   = ( theHypType >= AddHyp );
978   const bool allHyps = ( !isMesh && theHypType != Algo && theDim > -1);
979
980   if ( theFather->FindSubObject( aPart, aHypRoot ) )
981   {
982     _PTR(ChildIterator) anIter = SMESH::getStudy()->NewChildIterator( aHypRoot );
983     for ( ; anIter->More(); anIter->Next() )
984     {
985       _PTR(SObject) anObj = anIter->Value();
986       if ( isMesh ) // i.e. mesh or submesh
987       {
988         _PTR(SObject) aRefObj;
989         if ( anObj->ReferencedObject( aRefObj ) )
990           anObj = aRefObj;
991         else
992           continue;
993       }
994       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
995       {
996         aName = anAttr;
997         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
998         if ( !CORBA::is_nil( aVar ) )
999         {
1000           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
1001           if ( !aHypVar->_is_nil() )
1002           {
1003             CORBA::String_var hypType = aHypVar->GetName();
1004             HypothesisData* aData = SMESH::GetHypothesisData( hypType.in() );
1005             if ( !aData) continue;
1006             if (( theDim == -1 || aData->Dim.contains( theDim ) ) &&
1007                 ( isCompatible ( theAlgoData, aData, theHypType )) &&
1008                 ( theHypType == Algo || isAux == aData->IsAuxOrNeedHyp ) &&
1009                 ( !allHyps || myAvailableHypData[theDim][theHypType].count(aData) ))
1010             {
1011               std::string aHypName = aName->Value();
1012               theHyps.append( aHypName.c_str() );
1013               theHypList.append( THypItem( aHypVar, aHypName.c_str() ) );
1014             }
1015           }
1016         }
1017       }
1018     }
1019   }
1020 }
1021
1022 //================================================================================
1023 /*!
1024  * \brief If create or edit a submesh, return a hypothesis holding parameters used
1025  *        to mesh a sub-shape
1026   * \param aHypType - The hypothesis type name
1027   * \param aServerLib - Server library name
1028   * \param hypData - The structure holding the hypothesis type etc.
1029   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
1030  */
1031 //================================================================================
1032 SMESH::SMESH_Hypothesis_var
1033 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString&              aHypType,
1034                                           const QString&              aServerLib,
1035                                           const SMESH::HypInitParams* aParams ) const
1036 {
1037   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
1038     return SMESH::SMESH_Hypothesis::_nil();
1039
1040   const int nbColonsInMeshEntry = 3;
1041   bool isSubMesh = myToCreate ?
1042     !myIsMesh :
1043     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).count(':') > nbColonsInMeshEntry;
1044
1045   // get mesh and geom object
1046   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_nil();
1047   GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
1048
1049   QString anEntry;
1050   if ( isSubMesh )
1051   {
1052     anEntry = myDlg->selectedObject
1053       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
1054     if ( _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anEntry.toUtf8().data() ))
1055     {
1056       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
1057       if ( myToCreate ) // mesh and geom may be selected
1058       {
1059         aMesh = SMESH::SMESH_Mesh::_narrow( Obj );
1060         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1061         if ( _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( anEntry.toUtf8().data() ))
1062           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1063       }
1064       else // edition: sub-mesh may be selected
1065       {
1066         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
1067         if ( !sm->_is_nil() ) {
1068           aMesh = sm->GetFather();
1069           aGeomVar = sm->GetSubShape();
1070         }
1071       }
1072     }
1073   }
1074   else // mesh
1075   {
1076     if ( !myToCreate ) // mesh to edit can be selected
1077     {
1078       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1079       if ( _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( anEntry.toUtf8().data() ))
1080       {
1081         aMesh = SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1082         if ( !aMesh->_is_nil() )
1083           aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pMesh );
1084       }
1085     }
1086     if ( aGeomVar->_is_nil() ) {
1087       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1088       if ( _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( anEntry.toUtf8().data() ))
1089       {
1090         aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1091       }
1092     }
1093   }
1094
1095   SMESH::HypInitParams initParams;
1096   enum { BY_MESH, BY_GEOM, BY_AVERAGE_LENGTH }; // same as ::SMESH_Hypothesis::InitWay
1097   if ( aParams )
1098   {
1099     initParams = *aParams;
1100   }
1101   else
1102   {
1103     initParams.way = isSubMesh ? BY_MESH : BY_GEOM;
1104   }
1105
1106   SMESH::SMESH_Hypothesis_var hyp;
1107   if ( initParams.way == BY_AVERAGE_LENGTH )
1108     hyp = SMESHGUI::GetSMESHGen()->CreateHypothesisByAverageLength( aHypType.toUtf8().data(),
1109                                                                     aServerLib.toUtf8().data(),
1110                                                                     initParams.averageLength,
1111                                                                     initParams.quadDominated );
1112   else
1113     hyp = SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toUtf8().data(),
1114                                                                  aServerLib.toUtf8().data(),
1115                                                                  aMesh,
1116                                                                  aGeomVar,
1117                                                                  initParams );
1118   if ( hyp->_is_nil() && initParams.way == BY_MESH )
1119   {
1120     initParams.way = BY_GEOM;
1121     hyp = SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
1122                                                                  aServerLib.toUtf8().data(),
1123                                                                  aMesh,
1124                                                                  aGeomVar,
1125                                                                  initParams );
1126   }
1127   if ( hyp->_is_nil() && initParams.way == BY_GEOM )
1128   {
1129     initParams.way = BY_AVERAGE_LENGTH;
1130     initParams.averageLength = 1.;
1131     hyp = SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
1132                                                                  aServerLib.toUtf8().data(),
1133                                                                  aMesh,
1134                                                                  aGeomVar,
1135                                                                  initParams );
1136   }
1137   return hyp;
1138 }
1139
1140 //================================================================================
1141 /*!
1142  * \brief initialize a hypothesis creator
1143  */
1144 //================================================================================
1145
1146 void SMESHGUI_MeshOp::initHypCreator( SMESHGUI_GenericHypothesisCreator* theCreator )
1147 {
1148   if ( !theCreator ) return;
1149
1150   // Set shapes, of mesh and sub-mesh if any
1151
1152   // get Entry of the Geom object
1153   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1154   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1155   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ); 
1156
1157   if ( myToCreate && myIsMesh )
1158     aMeshEntry = aGeomEntry;
1159
1160   if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
1161     _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() );
1162     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1163     aMeshEntry = ( aGeomVar->_is_nil() ) ? QString() : SMESH::toQStr( aGeomVar->GetStudyEntry() );
1164   }
1165
1166   if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
1167     _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
1168     bool isMesh;
1169     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
1170     if ( !aGeomVar->_is_nil() )
1171     {
1172       aGeomEntry = SMESH::toQStr( aGeomVar->GetStudyEntry() );
1173       if ( isMesh )
1174         aMeshEntry = aGeomEntry;
1175     }
1176   }
1177
1178   if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
1179     // take geometry from submesh being created
1180     _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
1181     if ( pObj ) {
1182       // if current object is sub-mesh
1183       SMESH::SMESH_subMesh_var aSubMeshVar =
1184         SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1185       if ( !aSubMeshVar->_is_nil() ) {
1186         SMESH::SMESH_Mesh_var aMesh =  aSubMeshVar->GetFather();
1187         if ( !aMesh->_is_nil() ) {
1188           _PTR(SObject) aMeshSO = SMESH::FindSObject( aMesh );
1189           GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
1190           if ( !aGeomVar->_is_nil() )
1191             aMeshEntry = SMESH::toQStr( aGeomVar->GetStudyEntry() );
1192         }
1193       }
1194     }
1195   }
1196
1197   theCreator->setShapeEntry( aGeomEntry );
1198   if ( aMeshEntry != "" )
1199     theCreator->setMainShapeEntry( aMeshEntry );
1200
1201   theCreator->setNoGeomMesh( !myIsOnGeometry && myIsMesh && !myToCreate );
1202 }
1203
1204 //================================================================================
1205 /*!
1206  * \Brief Returns tab dimension
1207   * \param tab - the tab in the dlg
1208   * \param dlg - my dialogue
1209   * \retval int - dimension
1210  */
1211 //================================================================================
1212 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
1213 {
1214   int aDim = -1;
1215   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
1216     if (tab == dlg->tab(i))
1217       aDim = i;
1218   return aDim;
1219 }
1220
1221 //================================================================================
1222 /*!
1223  * \brief Create hypothesis
1224   * \param theHypType - hypothesis category (main or additional)
1225   * \param theIndex - index of type of hypothesis to be created
1226  *
1227  * Specifies dimension of hypothesis to be created (using sender() method),
1228  * specifies its type and calls method for hypothesis creation
1229  */
1230 //================================================================================
1231 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
1232 {
1233   // Specifies dimension of hypothesis to be created
1234   int aDim = getTabDim( sender(), myDlg );
1235   if (aDim == -1)
1236     return;
1237
1238   // Specifies type of hypothesis to be created
1239   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
1240   if (theIndex < 0 || theIndex >= dataList.count())
1241     return;
1242   QString aHypTypeName = dataList[ theIndex ]->TypeName;
1243
1244   // Create hypothesis
1245   createHypothesis(aDim, theHypType, aHypTypeName);
1246 }
1247
1248 namespace
1249 {
1250   QString GetUniqueName (const QStringList& theHypNames,
1251                          const QString& theName,
1252                          size_t theIteration = 1)
1253   {
1254     QString aName = theName + "_" + QString::number( theIteration );
1255     if ( theHypNames.contains( aName ) )
1256       return GetUniqueName( theHypNames, theName, ++theIteration );
1257     return aName;
1258   }
1259 }
1260
1261 //================================================================================
1262 /*!
1263  * \brief Return names of all existing hypotheses
1264  */
1265 //================================================================================
1266
1267 QStringList SMESHGUI_MeshOp::getHypoNames()
1268 {
1269   QStringList aHypNames;
1270
1271   TDim2Type2HypList::const_iterator aDimIter = myExistingHyps.begin();
1272   for ( ; aDimIter != myExistingHyps.end(); aDimIter++) {
1273     const TType2HypList& aType2HypList = aDimIter.value();
1274     TType2HypList::const_iterator aTypeIter = aType2HypList.begin();
1275     for ( ; aTypeIter != aType2HypList.end(); aTypeIter++) {
1276       const THypList& aHypList = aTypeIter.value();
1277       THypList::const_iterator anIter = aHypList.begin();
1278       for ( ; anIter != aHypList.end(); anIter++) {
1279         const THypItem& aHypItem = *anIter;
1280         const QString& aHypName = aHypItem.second;
1281         aHypNames.append(aHypName);
1282       }
1283     }
1284   }
1285   return aHypNames;
1286 }
1287
1288 //================================================================================
1289 /*!
1290  *  Create hypothesis and update dialog.
1291  *  \param theDim - dimension of hypothesis to be created
1292  *  \param theType - hypothesis category (algorithm, hypothesis, additional hypothesis)
1293  *  \param theTypeName - specifies hypothesis to be created
1294  */
1295 //================================================================================
1296 void SMESHGUI_MeshOp::createHypothesis(const int      theDim,
1297                                        const int      theType,
1298                                        const QString& theTypeName)
1299 {
1300   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName);
1301   if (!aData)
1302     return;
1303
1304   myDim = theDim;
1305   myType = theType;
1306
1307   // get a unique hyp name
1308   QString aHypName = GetUniqueName( getHypoNames(), aData->Label);
1309
1310   // existing hypos
1311   bool dialog = false;
1312
1313   QString aClientLibName = aData->ClientLibName;
1314   if (aClientLibName == "") {
1315     // Call hypothesis creation server method (without GUI)
1316     SMESH::SMESH_Hypothesis_var aHyp =
1317       SMESH::CreateHypothesis(theTypeName, aHypName, false);
1318     aHyp.out();
1319   }
1320   else {
1321     // Get hypotheses creator client (GUI)
1322     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
1323
1324     // Create hypothesis
1325     if (aCreator)
1326     {
1327       // Get parameters appropriate to initialize a new hypothesis
1328       SMESH::SMESH_Hypothesis_var initParamHyp =
1329         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
1330
1331       removeCustomFilters(); // Issue 0020170
1332
1333       // set shapes, of mesh and sub-mesh if any
1334       initHypCreator( aCreator );
1335
1336       myDlg->setEnabled( false );
1337       aCreator->create(initParamHyp, aHypName, myDlg, this, SLOT( onHypoCreated( int ) ) );
1338       dialog = true;
1339     }
1340     else
1341     {
1342      SMESH::SMESH_Hypothesis_var aHyp =
1343        SMESH::CreateHypothesis(theTypeName, aHypName, false);
1344      aHyp.out();
1345     }
1346   }
1347
1348   if( !dialog )
1349     onHypoCreated(2);
1350 }
1351
1352 //================================================================================
1353 /*!
1354  *  Necessary steps after hypothesis creation
1355  *  \param result - creation result:
1356  *   0 = rejected
1357  *   1 = accepted
1358  *   2 = additional value meaning that slot is called not from dialog box
1359  */
1360 //================================================================================
1361 void SMESHGUI_MeshOp::onHypoCreated( int result )
1362 {
1363   if( result != 2 )
1364   {
1365     int obj = myDlg->getActiveObject();
1366     onActivateObject( obj ); // Issue 0020170. Restore filters
1367     myDlg->setEnabled( true );
1368   }
1369
1370   _PTR(SComponent) aFather = SMESH::getStudy()->FindComponent("SMESH");
1371
1372   int nbHyp = myExistingHyps[myDim][myType].count();
1373   HypothesisData* algoData = hypData( myDim, Algo, currentHyp( myDim, Algo ));
1374   QStringList aNewHyps;
1375   existingHyps(myDim, myType, aFather, aNewHyps, myExistingHyps[myDim][myType], algoData);
1376   if (aNewHyps.count() > nbHyp)
1377   {
1378     for (int i = nbHyp; i < aNewHyps.count(); i++)
1379       myDlg->tab(myDim)->addHyp(myType, aNewHyps[i]);
1380   }
1381   return;
1382 }
1383
1384 //================================================================================
1385 /*!
1386  * \brief Calls plugin methods for hypothesis editing
1387   * \param theHypType - specifies whether main hypothesis or additional one
1388   * is edited
1389   * \param theIndex - index of existing hypothesis
1390  *
1391  * Calls plugin methods for hypothesis editing
1392  */
1393 //================================================================================
1394 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
1395 {
1396   // Speicfies dimension of hypothesis to be created
1397   int aDim = getTabDim( sender(), myDlg );
1398   if (aDim == -1)
1399     return;
1400
1401   const THypList& aList = myExistingHyps[ aDim ][ theHypType ];
1402   if ( theIndex < 0 || theIndex >= aList.count() )
1403     return;
1404   const THypItem& aHypItem = aList[ theIndex ];
1405   SMESH::SMESH_Hypothesis_var aHyp = aHypItem.first;
1406   if ( aHyp->_is_nil() )
1407     return;
1408
1409   SMESHGUI_GenericHypothesisCreator* aCreator =
1410     SMESH::GetHypothesisCreator( SMESH::toQStr( aHyp->GetName() ));
1411   if ( aCreator )
1412   {
1413     // set initial parameters
1414     SMESH::SMESH_Hypothesis_var initParamHyp =
1415       getInitParamsHypothesis( SMESH::toQStr( aHyp->GetName() ),
1416                                SMESH::toQStr( aHyp->GetLibName() ));
1417     aCreator->setInitParamsHypothesis( initParamHyp );
1418
1419     // set shapes, of mesh and sub-mesh if any
1420     initHypCreator( aCreator );
1421
1422     removeCustomFilters(); // Issue 0020170
1423     myDlg->setEnabled( false );
1424
1425     aCreator->edit( aHyp.in(), aHypItem.second, dlg(), this, SLOT( onHypoEdited( int ) ) );
1426   }
1427 }
1428
1429 //================================================================================
1430 /*!
1431  *  Necessary steps after hypothesis edition
1432  *  \param result - creation result:
1433  *   0 = rejected
1434  *   1 = accepted
1435  */
1436 //================================================================================
1437 void SMESHGUI_MeshOp::onHypoEdited( int /*result*/ )
1438 {
1439   int obj = myDlg->getActiveObject();
1440   onActivateObject( obj ); // Issue 0020170. Restore filters
1441   myDlg->setEnabled( true );
1442 }
1443
1444 //================================================================================
1445 /*!
1446  * \brief access to hypothesis data
1447   * \param theDim - hyp dimension
1448   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
1449   * \param theIndex - index in the list
1450   * \retval HypothesisData* - result data, may be 0
1451   */
1452 //================================================================================
1453 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
1454                                           const int theHypType,
1455                                           const int theIndex)
1456 {
1457   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
1458        theHypType > -1 && theHypType < NbHypTypes &&
1459        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
1460     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
1461   return 0;
1462 }
1463
1464 //================================================================================
1465 /*!
1466  * \brief Update list of algorithms and hypotheses according to currently selected
1467  *        algorithm, geometry and mesh type
1468  */
1469 //================================================================================
1470 void SMESHGUI_MeshOp::setFilteredAlgoData()
1471 {
1472   // keep current algorithms
1473   HypothesisData* curAlgos[ 4 ];
1474   for ( int dim = 0; dim <= 3; ++dim )
1475     curAlgos[ dim ] = hypData( dim, Algo, currentHyp( dim, Algo ));
1476
1477   // find out myMaxShapeDim by checking algorithm applicability to geometry
1478
1479   int curMeshType = myDlg->currentMeshType();
1480   myDlg->setCurrentMeshType( MT_ANY );
1481   QStringList algosAvailable;
1482   // get available algorithms taking into account geometry only
1483   if ( myGeom->_is_nil() )
1484   {
1485     myMaxShapeDim = 3;
1486
1487     if ( myToCreate ) // readMesh() has done it if !myToCreate
1488       for ( int dim = 0; dim <= 3; ++dim )
1489         availableHyps( dim, Algo, algosAvailable, myAvailableHypData[ dim ][ Algo ] );
1490   }
1491   else
1492   {
1493     if ( myGeomEntry != myLastGeomEntry )
1494       myHypMapIsApplicable.clear();
1495
1496     for ( int dim = 0; dim <= 3; ++dim )
1497     {
1498       availableHyps( dim, Algo, algosAvailable, myAvailableHypData[ dim ][ Algo ] );
1499       if ( algosAvailable.count() )
1500         myMaxShapeDim = dim;
1501     }
1502     if ( !myGeomEntry.isEmpty() )
1503       myLastGeomEntry = myGeomEntry;
1504   }
1505   myDlg->setMaxHypoDim( myMaxShapeDim );
1506
1507   // set mesh types according to myMaxShapeDim
1508   updateMeshTypeList();
1509
1510   // update available hypo-sets that depend on geometry and mesh type
1511   myDlg->setCurrentMeshType( Max( MT_ANY, curMeshType ));
1512   updateHypoSets(); // it sets myAvailableHypData by curMeshType
1513
1514   // restore current algorithms according to changed myAvailableHypData
1515   algosAvailable.clear();
1516   for ( int dim = 0; dim <= 3; ++dim )
1517   {
1518     int algoIndex = myAvailableHypData[ dim ][ Algo ].indexOf( curAlgos[ dim ]);
1519     while ( algosAvailable.count() <= algoIndex )
1520       algosAvailable << "";
1521     myDlg->tab( dim )->setAvailableHyps( Algo, algosAvailable );
1522     setCurrentHyp( dim, Algo, algoIndex );
1523     if ( algoIndex < 0 )
1524       curAlgos[ dim ] = 0;
1525   }
1526
1527   // find a selected algo, current or of highest dimension
1528   int algoDim = myDlg->currentTab();
1529   if ( !curAlgos[ algoDim ])
1530     for ( algoDim = SMESH::DIM_3D; algoDim >= SMESH::DIM_0D; algoDim-- )
1531       if ( curAlgos[ algoDim ] )
1532         break;
1533   if ( algoDim < SMESH::DIM_0D )
1534     algoDim = myMaxShapeDim;
1535
1536   // set algorithms and hypotheses according to all parameters (mesh type etc)
1537   onAlgoSelected( currentHyp( algoDim, Algo ), algoDim );
1538 }
1539
1540 //================================================================================
1541 /*!
1542  * \brief Set available algos and hypos according to the selected algorithm
1543  * \param theIndex - algorithm index
1544  * \param theDim - algorithm dimension
1545  */
1546 //================================================================================
1547 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
1548                                       const int theDim )
1549 {
1550   if ( myIgnoreAlgoSelection )
1551     return;
1552
1553   int curDim = getTabDim( sender(), myDlg );
1554   int aDim = theDim < 0 ? curDim : theDim;
1555   if (aDim == -1)
1556     return;
1557
1558   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1559
1560   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1561   HypothesisData* algoByDim[4];
1562   algoByDim[ aDim ] = algoData;
1563
1564   QStringList anAvailable;
1565   const int algoDim = aDim;
1566
1567   // 2 loops: backward and forward from algo dimension (forward == to higher dimension)
1568   for ( int forward = 1; forward >= 0; --forward )
1569   {
1570     int dim = algoDim, lastDim = SMESH::DIM_3D, dir = 1;
1571     if ( !forward ) {
1572       dim = algoDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
1573     }
1574     //bool noCompatible = false;
1575     for ( ; dim * dir <= lastDim * dir; dim += dir)
1576     {
1577       // if ( noCompatible ) // the selected algo has no compatible ones (like 1D2D3D algo)
1578       // {
1579       //   anAvailable.clear();
1580       //   myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1581       //   myAvailableHypData[dim][Algo].clear();
1582       //   algoByDim[ dim ] = 0;
1583       // }
1584       // else
1585       {
1586         int           algoIndex = currentHyp( dim, Algo );
1587         HypothesisData *curAlgo = hypData( dim, Algo, algoIndex );
1588
1589         // set new available algorithms
1590         HypothesisData *prevAlgo = 0;
1591         if ( dim != algoDim )
1592           for ( int prevDim = dim + 1; prevDim <=3 && !prevAlgo; ++prevDim )
1593             prevAlgo = hypData( prevDim, Algo, currentHyp( prevDim, Algo ));
1594         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
1595         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1596
1597         //noCompatible = ( prevAlgo && !forward && anAvailable.isEmpty() );
1598
1599         // set current algo
1600         algoIndex = myAvailableHypData[dim][Algo].indexOf( curAlgo );
1601         setCurrentHyp( dim, Algo, algoIndex, /*updateHyps=*/true );
1602
1603         algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1604       }
1605
1606       // activate tabs according to algorithms availability
1607       if ( anAvailable.isEmpty() ) myDlg->disableTab( dim );
1608       else                         myDlg->enableTab( dim );
1609
1610     } // loop on dims
1611   } // loops backward and forward
1612
1613
1614   // set hypotheses corresponding to the found algorithms
1615
1616   _PTR(SObject) pComp = SMESH::getStudy()->FindComponent("SMESH");
1617
1618   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1619   {
1620     if ( !isAccessibleDim( dim ))
1621       continue;
1622
1623     // get indices of selected hyps
1624     const int nbTypes = nbDlgHypTypes(dim);
1625     std::vector<int> hypIndexByType( nbTypes, -1 );
1626     for ( int dlgType = MainHyp; dlgType < nbTypes; dlgType++ )
1627     {
1628       hypIndexByType[ dlgType ] = currentHyp( dim, dlgType );
1629     }
1630
1631     // update hyps
1632     for ( int dlgType = MainHyp; dlgType < nbTypes; dlgType++ )
1633     {
1634       const int type = Min( dlgType, AddHyp );
1635       myAvailableHypData[ dim ][ type ].clear();
1636       QStringList anAvailable, anExisting;
1637
1638       HypothesisData* curAlgo = algoByDim[ dim ];
1639       int hypIndex = hypIndexByType[ dlgType ];
1640
1641       SMESH::SMESH_Hypothesis_var curHyp;
1642       QString                     curHypType;
1643       HypothesisData*             hypData = 0;
1644       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1645       {
1646         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ].first;
1647         if ( !curHyp->_is_nil() )
1648         {
1649           curHypType = SMESH::toQStr( curHyp->GetName() );
1650           hypData = SMESH::GetHypothesisData( curHypType );
1651         }
1652       }
1653       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) // edition, algo not selected
1654       {
1655         // try to find algo by selected hypothesis in order to keep it selected
1656         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1657         if ( !algoDeselectedByUser &&
1658              myObjHyps[ dim ][ type ].count() > 0 &&
1659              curHypType == SMESH::toQStr( myObjHyps[ dim ][ type ].first().first->GetName()) )
1660         {
1661           for ( int i = 0; i < myAvailableHypData[ dim ][ Algo ].count(); ++i )
1662           {
1663             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1664             if ( curAlgo && hypData && isCompatible( curAlgo, hypData, type ))
1665               break;
1666             else
1667               curAlgo = 0;
1668           }
1669         }
1670       }
1671
1672       // get hyps compatible with curAlgo
1673       bool defaulHypAvlbl = false;
1674       if ( curAlgo )
1675       {
1676         if ( hypData && !isCompatible( curAlgo, hypData, type ))
1677           curHyp = SMESH::SMESH_Hypothesis::_nil();
1678         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo );
1679         existingHyps ( dim, type, pComp, anExisting, myExistingHyps[ dim ][ type ], curAlgo );
1680         defaulHypAvlbl = ( type == MainHyp && !curAlgo->IsAuxOrNeedHyp );
1681       }
1682       // set list of hypotheses
1683       if ( dlgType <= AddHyp )
1684       {
1685         myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1686         myDlg->tab( dim )->setExistingHyps( type, anExisting, defaulHypAvlbl );
1687       }
1688       // set current existing hypothesis
1689       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1690         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1691       else
1692         hypIndex = -1;
1693       if ( !isSubmesh && myToCreate && hypIndex < 0 && anExisting.count() == 1 && dim == curDim )
1694       {
1695         // none is yet selected => select the sole existing if it is not optional
1696         CORBA::String_var hypTypeName = myExistingHyps[ dim ][ type ].first().first->GetName();
1697         bool isOptional = true;
1698         if ( algoByDim[ dim ] &&
1699              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName.in(), isOptional ) &&
1700              !isOptional )
1701           hypIndex = 0;
1702       }
1703       setCurrentHyp( dim, dlgType, hypIndex );
1704     }
1705   }
1706   return;
1707 }
1708
1709 //================================================================================
1710 /*!
1711  * \brief Create a sub-mesh on internal edges in the case where the global algorithm
1712  *        is of type 1D-2D[-3D] so that the internal edges would remain not meshed.
1713  */
1714 //================================================================================
1715
1716 void SMESHGUI_MeshOp::createSubMeshOnInternalEdges( SMESH::SMESH_Mesh_ptr theMesh,
1717                                                     GEOM::GEOM_Object_ptr theMainShape )
1718 {
1719   if ( theMesh->_is_nil() || theMainShape->_is_nil() )
1720     return;
1721
1722   if ( isAccessibleDim( 1 ))
1723     return; // global 1D algorithm is/can be assigned
1724
1725   const HypothesesSet::SetType internSet = HypothesesSet::INTERN;
1726   bool toCreate = true;
1727   bool toCreateMandatory = ( myHypoSet &&
1728                              myAverageSize > 0 &&
1729                              ( myHypoSet->init( /*algo=*/ true, internSet ), myHypoSet->more()) &&
1730                              ( myHypoSet->init( /*algo=*/false, internSet ), myHypoSet->more()));
1731   if ( !toCreateMandatory ) // ask the user
1732     toCreate = false; // can't pass both mesh and geometry to Create Submesh operation (so far?)
1733     // toCreate = SUIT_MessageBox::warning( SMESHGUI::desktop(),
1734     //                                      QObject::tr("SMESH_WRN_WARNING"),
1735     //                                      QObject::tr("SMESH_CREATE_SUBMESH_ON_INTERNAL_EDGES"),
1736     //                                      QObject::tr("SMESH_BUT_YES"),
1737     //                                      QObject::tr("SMESH_BUT_NO"), 1, 0);
1738   if ( !toCreate )
1739     return;
1740
1741   TopoDS_Shape shape;
1742   if ( !GEOMBase::GetShape( theMainShape, shape ))
1743     return;
1744
1745   std::vector< TopoDS_Shape > internalEdges;
1746   for ( TopExp_Explorer edge( shape, TopAbs_EDGE, TopAbs_WIRE ); edge.More(); edge.Next() )
1747     internalEdges.push_back( edge.Current() );
1748
1749   if ( internalEdges.empty() )
1750     return;
1751
1752   TopTools_IndexedMapOfShape shapeIDs;
1753   TopExp::MapShapes( shape, shapeIDs );
1754
1755   std::set< int > intIDSet;
1756   for ( size_t i = 0; i < internalEdges.size(); ++i )
1757     intIDSet.insert( shapeIDs.FindIndex( internalEdges[ i ]));
1758
1759   GEOM::GEOM_Gen_var geomGen = theMainShape->GetGen();
1760   if (geomGen->_is_nil()) return;
1761
1762   GEOM::GEOM_Object_var edgeGroup;
1763   GEOM::GEOM_IShapesOperations_wrap sOp = geomGen->GetIShapesOperations();
1764   GEOM::GEOM_IGroupOperations_wrap  gOp = geomGen->GetIGroupOperations();
1765   GEOM::ListOfGO_var         geomGroups = sOp->GetExistingSubObjects( theMainShape,
1766                                                                       /*groupsOnly=*/true );
1767   for ( CORBA::ULong i = 0; i < geomGroups->length(); ++i )
1768   {
1769     GEOM::ListOfLong_var ids = gOp->GetObjects( geomGroups[ i ]);
1770     std::set< int > idSet( & ids[0], & ids[0] + ids->length() );
1771     if ( idSet == intIDSet )
1772     {
1773       edgeGroup = geomGroups[ i ];
1774       break;
1775     }
1776   }
1777
1778   if ( edgeGroup->_is_nil() )
1779   {
1780     GEOM::GEOM_Object_var edgeGroup = gOp->CreateGroup( theMainShape, TopAbs_EDGE );
1781
1782     GEOM::ListOfLong_var edgeIDs = new GEOM::ListOfLong;
1783     edgeIDs->length( internalEdges.size() );
1784     std::set< int >::iterator id = intIDSet.begin();
1785     for ( size_t i = 0; i < intIDSet.size(); ++i, ++id )
1786       edgeIDs[ i ] = *id;
1787     gOp->UnionIDs( edgeGroup, edgeIDs );
1788
1789     SALOMEDS::SObject_wrap so = geomGen->AddInStudy( edgeGroup, "Internal edges", theMainShape );
1790   }
1791
1792   if ( !toCreateMandatory )
1793   {
1794     // show Create Sub-mesh dislog
1795     // _PTR(SObject) aMeshSO = SMESH::FindSObject( theMesh );
1796     // selectionMgr()->clearFilters();
1797     // selectObject( pSubmesh );
1798     // SMESHGUI::GetSMESHGUI()->switchToOperation( SMESHOp::OpEditMeshOrSubMesh );
1799     return;
1800   }
1801
1802   // create a sub-mesh using myAverageSize w/o GUI
1803
1804   SMESH::SMESH_subMesh_var subMesh = theMesh->GetSubMesh( edgeGroup, "" );
1805
1806   for ( int isAlgo = 1; isAlgo >= 0; --isAlgo )
1807     for ( myHypoSet->init( isAlgo, internSet ); myHypoSet->more(); myHypoSet->next() )
1808     {
1809       QString    aHypoTypeName = myHypoSet->current();
1810       HypothesisData* aHypData = SMESH::GetHypothesisData( aHypoTypeName );
1811       if ( !aHypData )
1812         continue;
1813
1814       myDim = aHypData->Dim[0];
1815       if ( myDim != 1 )
1816         continue;
1817
1818       // create or/and set
1819       SMESH::SMESH_Hypothesis_var newHypo;
1820       if ( isAlgo )
1821       {
1822         myAvailableHypData[ myDim ][ Algo ].clear();
1823         myAvailableHypData[ myDim ][ Algo ] << aHypData;
1824         QStringList hypList; hypList << aHypoTypeName;
1825         myDlg->tab( myDim )->setAvailableHyps( Algo, hypList );
1826         setCurrentHyp( myDim, Algo, 0 );
1827         newHypo = getAlgo( myDim );
1828       }
1829       else
1830       {
1831         SMESH::HypInitParams params = { 2, myAverageSize, false };
1832         newHypo = getInitParamsHypothesis( aHypData->TypeName, aHypData->ServerLibName, & params );
1833         QString hypName = GetUniqueName( getHypoNames(), aHypData->Label );
1834         SALOMEDS::SObject_wrap so =
1835           SMESHGUI::GetSMESHGen()->PublishInStudy( SALOMEDS::SObject::_nil(), newHypo,
1836                                                    hypName.toUtf8().data() );
1837       }
1838       SMESH::AddHypothesisOnSubMesh( subMesh, newHypo );
1839     }
1840
1841   return;
1842 }
1843
1844 //================================================================================
1845 /*!
1846  * \brief Ask the user to enter an average size which will be used to create
1847  *        hypotheses of a hypo-set basing on this size
1848  *  \param [out] averageSize - average element size
1849  *  \return bool - false if the user canceled the dialog
1850  */
1851 //================================================================================
1852
1853 bool SMESHGUI_MeshOp::getAverageSize( double & averageSize )
1854 {
1855   HypothesisData* hypData = SMESH::GetHypothesisData( "MaxLength" );
1856   if ( !hypData )
1857     return false;
1858
1859   SMESH::SMESH_Hypothesis_var hyp = getInitParamsHypothesis( hypData->TypeName,
1860                                                              hypData->ServerLibName );
1861   if ( hyp->_is_nil() )
1862     return false;
1863
1864   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( hypData->TypeName );
1865   if ( !aCreator )
1866     return false;
1867   
1868   initHypCreator( aCreator );
1869   myDlg->setEnabled( false );
1870
1871   aCreator->edit( hyp.in(), HypothesesSet::getCommonHypoSetHypoType(),
1872                   dlg(), this, SLOT( onHypoEdited( int )));
1873
1874   StdMeshers::StdMeshers_MaxLength_var lenHyp = StdMeshers::StdMeshers_MaxLength::_narrow( hyp );
1875   if ( lenHyp->_is_nil() )
1876     return false;
1877
1878   averageSize = lenHyp->GetLength();
1879   return true;
1880 }
1881
1882 //================================================================================
1883 /*!
1884  * \brief Creates and selects hypothesis of hypotheses set
1885  * \param theSetName - The name of hypotheses set
1886  */
1887 //================================================================================
1888 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1889 {
1890   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet(theSetName);
1891   myHypoSet = aHypoSet;
1892   if (!aHypoSet)
1893     return;
1894
1895   // clear all hyps
1896   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++)
1897   {
1898     setCurrentHyp(dim, Algo, -1);
1899     setCurrentHyp(dim, AddHyp, -1);
1900     setCurrentHyp(dim, MainHyp, -1);
1901     onAlgoSelected( -1, dim );
1902   }
1903
1904   HypothesesSet::SetType setType = aHypoSet->getPreferredHypType();
1905   if ( !aHypoSet->getAlgoAvailable( setType ))
1906   {
1907     setType = ( setType == HypothesesSet::ALT ) ? HypothesesSet::MAIN : HypothesesSet::ALT;
1908     if ( !aHypoSet->getAlgoAvailable( setType ))
1909       return;
1910   }
1911
1912   myAverageSize = -1;
1913   if ( aHypoSet->toUseCommonSize() && !getAverageSize( myAverageSize ))
1914     return;
1915
1916   int maxDim = -1;
1917   for ( int isAlgo = 1; isAlgo >= 0; --isAlgo )
1918     for ( aHypoSet->init( isAlgo, setType ); aHypoSet->more(); aHypoSet->next() )
1919     {
1920       QString    aHypoTypeName = aHypoSet->current();
1921       HypothesisData* aHypData = SMESH::GetHypothesisData( aHypoTypeName );
1922       if (!aHypData)
1923         continue;
1924
1925       myDim = aHypData->Dim[0];
1926       // create or/and set
1927       if ( isAlgo )
1928       {
1929         int index = myAvailableHypData[myDim][Algo].indexOf( aHypData );
1930         if ( index >= 0 )
1931         {
1932           setCurrentHyp( myDim, Algo, index );
1933           onAlgoSelected( index, myDim );
1934           maxDim = Max( maxDim, myDim );
1935         }
1936       }
1937       else
1938       {
1939         myType = MainHyp;
1940         int index = myAvailableHypData[myDim][MainHyp].indexOf( aHypData );
1941         if ( index < 0 )
1942         {
1943           myType = AddHyp;
1944           index = myAvailableHypData[myDim][AddHyp].indexOf( aHypData );
1945         }
1946         if ( index >= 0 )
1947         {
1948           if ( myAverageSize > 0 )
1949           {
1950             SMESH::HypInitParams params = { 2, myAverageSize, aHypoSet->isQuadDominated() };
1951             SMESH::SMESH_Hypothesis_var hyp =
1952               getInitParamsHypothesis( aHypData->TypeName, aHypData->ServerLibName, & params );
1953
1954             QString hypName = GetUniqueName( getHypoNames(), aHypData->Label );
1955             SALOMEDS::SObject_wrap so =
1956               SMESHGUI::GetSMESHGen()->PublishInStudy( SALOMEDS::SObject::_nil(), hyp,
1957                                                        hypName.toUtf8().data() );
1958             onHypoCreated(2);
1959           }
1960           else
1961           {
1962             createHypothesis( myDim, myType, aHypoTypeName );
1963           }
1964         }
1965       }
1966     }
1967
1968   if ( maxDim > 0 )
1969     myDlg->setCurrentTab( maxDim );
1970
1971   return;
1972 }
1973
1974 //================================================================================
1975 /*!
1976  * \brief Creates mesh
1977  *  \param theMess - Output parameter intended for returning error message
1978  *  \param theEntryList - List of entries of published objects
1979  *  \retval bool  - TRUE if mesh is created, FALSE otherwise
1980  */
1981 //================================================================================
1982 bool SMESHGUI_MeshOp::createMesh( QString& theMess, QStringList& theEntryList )
1983 {
1984   theMess = "";
1985
1986   QStringList aList;
1987   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1988   if ( aList.isEmpty() )
1989   {
1990     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1991     if ( aSMESHGen->_is_nil() )
1992       return false;
1993
1994     SMESH::SMESH_Mesh_var aMesh= aSMESHGen->CreateEmptyMesh();
1995     if ( aMesh->_is_nil() )
1996       return false;
1997
1998     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMesh.in() );
1999     if ( aMeshSO ) {
2000       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
2001       theEntryList.append( aMeshSO->GetID().c_str() );
2002     }
2003     return true;
2004   }
2005   QString namePrefix;
2006   if ( aList.count() > 1 )
2007   {
2008     namePrefix = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
2009     int i = namePrefix.length() - 1;
2010     while ( i > 0 && namePrefix[i].isDigit() )
2011       --i;
2012     if ( i < namePrefix.length() - 1 )
2013       namePrefix.chop( namePrefix.length() - 1 - i );
2014     else
2015       namePrefix += "_";
2016   }
2017   QStringList::Iterator it = aList.begin();
2018   for ( int i = 0; it != aList.end(); it++, ++i )
2019   {
2020     QString aGeomEntry = *it;
2021     _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( aGeomEntry.toUtf8().data() );
2022     GEOM::GEOM_Object_var aGeomVar =
2023       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
2024
2025     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
2026     if ( aSMESHGen->_is_nil() )
2027       return false;
2028
2029     SUIT_OverrideCursor aWaitCursor;
2030
2031     // create mesh
2032     SMESH::SMESH_Mesh_var aMesh = aSMESHGen->CreateMesh( aGeomVar );
2033     if ( aMesh->_is_nil() )
2034       return false;
2035     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMesh.in() );
2036     if ( aMeshSO ) {
2037       theEntryList.append( aMeshSO->GetID().c_str() );
2038       if ( i > 0 ) setDefaultName( namePrefix );
2039       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
2040     }
2041
2042     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
2043     {
2044       if ( !isAccessibleDim( aDim )) continue;
2045
2046       // assign hypotheses
2047       for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(aDim); dlgType++ )
2048       {
2049         const int aHypIndex = currentHyp( aDim, dlgType );
2050         const int  aHypType = Min( dlgType, AddHyp );
2051         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
2052         {
2053           SMESH::SMESH_Hypothesis_var aHypVar =
2054             myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
2055           if ( !aHypVar->_is_nil() )
2056             SMESH::AddHypothesisOnMesh( aMesh, aHypVar );
2057         }
2058       }
2059       // find or create algorithm
2060       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
2061       if ( !anAlgoVar->_is_nil() )
2062         SMESH::AddHypothesisOnMesh( aMesh, anAlgoVar );
2063     }
2064
2065
2066     if ( myDlg->toCreateAllGroups() && !aGeomVar->_is_nil() )
2067     {
2068       // Create groups on all geom groups
2069
2070       GEOM::GEOM_Gen_var           geomGen = aGeomVar->GetGen();
2071       GEOM::GEOM_IShapesOperations_wrap op = geomGen->GetIShapesOperations();
2072       GEOM::ListOfGO_var        geomGroups = op->GetExistingSubObjects( aGeomVar,
2073                                                                         /*groupsOnly=*/false );
2074       SMESH::SMESH_GroupOnGeom_var meshGroup;
2075       for ( CORBA::ULong iG = 0; iG < geomGroups->length(); ++iG )
2076       {
2077         SMESH::ElementType elemType = SMESHGUI_GroupOnShapeOp::ElementType( geomGroups[ iG ] );
2078         if ( elemType == SMESH::ALL )
2079           continue;
2080         if ( elemType == SMESH::ELEM0D )
2081           elemType = SMESH::NODE;
2082
2083         CORBA::String_var name = geomGroups[ iG ]->GetName();
2084         meshGroup = aMesh->CreateGroupFromGEOM( elemType, name, geomGroups[ iG ]);
2085         // if ( elemType != SMESH::NODE )
2086         //   meshGroup = aMesh->CreateGroupFromGEOM( SMESH::NODE, name, geomGroups[ iG ]);
2087       }
2088     }
2089
2090     createSubMeshOnInternalEdges( aMesh, aGeomVar );
2091
2092   }
2093
2094   return true;
2095 }
2096
2097 //================================================================================
2098 /*!
2099  * \brief Create sub-mesh
2100  * \param theMess - Output parameter intended for returning error message
2101  * \param theEntryList - List of entries of published objects
2102  * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
2103  */
2104 //================================================================================
2105 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess, QStringList& theEntryList )
2106 {
2107   theMess = "";
2108
2109   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
2110   if ( aSMESHGen->_is_nil() )
2111     return false;
2112
2113   // get mesh object
2114   SMESH::SMESH_Mesh_var aMesh =
2115     SMESH::EntryToInterface<SMESH::SMESH_Mesh>( myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh ));
2116   if ( aMesh->_is_nil() && myIsInvalidSubMesh )
2117   {
2118     SMESH::SMESH_subMesh_var aSMVar =
2119       SMESH::EntryToInterface<SMESH::SMESH_subMesh>( myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ));
2120     if ( !aSMVar->_is_nil() )
2121       aMesh = aSMVar->GetMesh();
2122   }
2123   if ( aMesh->_is_nil() )
2124     return false;
2125
2126   // GEOM shape of the main mesh
2127   GEOM::GEOM_Object_var mainGeom = aMesh->GetShapeToMesh();
2128
2129   // Name for the new sub-mesh
2130   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
2131
2132   // get geom object
2133   GEOM::GEOM_Object_var aGeomVar;
2134   QStringList aGEOMs;
2135   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
2136   if (aGEOMs.count() == 1)
2137   {
2138     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
2139     QString aGeomEntry = aGEOMs.first();
2140     _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( aGeomEntry.toUtf8().data() );
2141     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
2142   }
2143   else if (aGEOMs.count() > 1)
2144   {
2145     // create a GEOM group
2146     GEOM::GEOM_Gen_var geomGen = mainGeom->GetGen();
2147     if ( !geomGen->_is_nil() ) {
2148       GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations();
2149       if ( !op->_is_nil() )
2150       {
2151         // check and add all selected GEOM objects: they must be
2152         // a sub-shapes of the main GEOM and must be of one type
2153         int iSubSh = 0;
2154         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
2155         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
2156         aSeq->length(aGEOMs.count());
2157         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
2158         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
2159           QString aSubGeomEntry = (*aSubShapesIter);
2160           _PTR(SObject) pSubGeom = SMESH::getStudy()->FindObjectID(aSubGeomEntry.toUtf8().data());
2161           GEOM::GEOM_Object_var aSubGeomVar =
2162             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
2163           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
2164           if (iSubSh == 0) {
2165             aGroupType = aSubShapeType;
2166           } else {
2167             if (aSubShapeType != aGroupType)
2168               aGroupType = TopAbs_SHAPE;
2169           }
2170           aSeq[iSubSh] = aSubGeomVar;
2171         }
2172         // create a group
2173         GEOM::GEOM_Object_wrap aGroupVar = op->CreateGroup(mainGeom, aGroupType);
2174         op->UnionList(aGroupVar, aSeq);
2175
2176         if (op->IsDone())
2177         {
2178           aGeomVar = GEOM::GEOM_Object::_duplicate( aGroupVar.in() );
2179
2180           // publish the GEOM group in study
2181           QString aNewGeomGroupName ("Auto_group_for_");
2182           aNewGeomGroupName += aName;
2183           SALOMEDS::SObject_wrap aNewGroupSO =
2184             geomGen->AddInStudy( aGeomVar,
2185                                  aNewGeomGroupName.toUtf8().data(), mainGeom);
2186         }
2187       }
2188     }
2189   }
2190   else {
2191   }
2192   if (aGeomVar->_is_nil())
2193     return false;
2194
2195   SUIT_OverrideCursor aWaitCursor;
2196
2197   QString aNameOrID = aName;
2198   if ( myIsInvalidSubMesh )
2199     // pass a sub-mesh entry to mesh->GetSubMesh() to replace the invalid sub-mesh
2200     // by a valid one in an existing SO
2201     aNameOrID = myDlg->selectedObject(SMESHGUI_MeshDlg::Obj);
2202
2203   // create sub-mesh
2204   SMESH::SMESH_subMesh_var aSubMeshVar = aMesh->GetSubMesh( aGeomVar, aNameOrID.toUtf8().data() );
2205   _PTR(SObject) aSubMeshSO = SMESH::FindSObject( aSubMeshVar.in() );
2206   if ( aSubMeshSO ) {
2207     SMESH::SetName( aSubMeshSO, aName.toUtf8().data() );
2208     theEntryList.append( aSubMeshSO->GetID().c_str() );
2209   }
2210
2211   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
2212   {
2213     if ( !isAccessibleDim( aDim )) continue;
2214
2215     // find or create algorithm
2216     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
2217     if ( !anAlgoVar->_is_nil() )
2218       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
2219     // assign hypotheses
2220     for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(aDim); dlgType++ )
2221     {
2222       const int aHypIndex = currentHyp( aDim, dlgType );
2223       const int  aHypType = Min( dlgType, AddHyp );
2224       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
2225       {
2226         SMESH::SMESH_Hypothesis_var aHypVar =
2227           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
2228         if ( !aHypVar->_is_nil() )
2229           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
2230       }
2231     }
2232   }
2233
2234   // deselect geometry: next submesh should be created on other sub-shape
2235   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
2236   selectObject( _PTR(SObject)() );
2237   selectionDone();
2238
2239   checkSubMeshConcurrency( aMesh, aSubMeshVar, /*askUser=*/true );
2240
2241   return true;
2242 }
2243
2244 //================================================================================
2245 /*!
2246  * \brief Return index of current hypothesis or algorithm
2247   * \param theDim - dimension of hypothesis or algorithm
2248   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
2249   * \retval int - index of current hypothesis or algorithm, -1 if None selected
2250  */
2251 //================================================================================
2252 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
2253 {
2254   if ( 0 <= theDim && theDim <= 3 )
2255     return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
2256   else
2257     return -1;
2258 }
2259
2260 //================================================================================
2261 /*!
2262  * \brief Checks if a hypothesis is selected
2263  */
2264 //================================================================================
2265
2266 bool SMESHGUI_MeshOp::isSelectedHyp( int theDim, int theHypType, int theIndex) const
2267 {
2268   if ( theIndex < 0 )
2269     return false;
2270
2271   if ( theHypType < AddHyp ) // only one hyp can be selected
2272     return currentHyp( theDim, theHypType ) == theIndex;
2273
2274   for ( int dlgHypType = AddHyp; dlgHypType < nbDlgHypTypes( theDim ); ++dlgHypType )
2275     if ( currentHyp( theDim, dlgHypType ) == theIndex )
2276       return true;
2277
2278   return false;
2279 }
2280
2281 //================================================================================
2282 /*!
2283  * \brief Returns nb of HypType's taking into account possible several
2284  *        selected additional hypotheses which are coded as additional HypType's.
2285  */
2286 //================================================================================
2287
2288 int SMESHGUI_MeshOp::nbDlgHypTypes( const int dim ) const
2289 {
2290   return NbHypTypes + myDlg->tab( dim )->nbAddHypTypes();
2291 }
2292
2293 //================================================================================
2294 /*!
2295  * \brief Returns true if hypotheses of given dim can be assigned
2296   * \param theDim - hypotheses dimension
2297   * \retval bool - result
2298  */
2299 //================================================================================
2300 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
2301 {
2302   return myDlg->isTabEnabled( theDim );
2303 }
2304
2305 //================================================================================
2306 /*!
2307  * \brief Sets current hypothesis or algorithms
2308   * \param theDim - dimension of hypothesis or algorithm
2309   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
2310   * \param theIndex - Index of hypothesis
2311   * \param updateHypsOnAlgoDeselection - to clear and disable hyps if algo deselected
2312  *
2313  * Gets current hypothesis or algorithms
2314  */
2315 //================================================================================
2316 void SMESHGUI_MeshOp::setCurrentHyp( const int  theDim,
2317                                      const int  theHypType,
2318                                      const int  theIndex,
2319                                      const bool updateHypsOnAlgoDeselection)
2320 {
2321   myIgnoreAlgoSelection = true;
2322   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
2323   myIgnoreAlgoSelection = false;
2324
2325   if ( updateHypsOnAlgoDeselection && theHypType == Algo && theIndex < 0 )
2326   {
2327     const QStringList noHyps;
2328     myDlg->tab( theDim )->setAvailableHyps( MainHyp, noHyps );
2329     myDlg->tab( theDim )->setExistingHyps ( MainHyp, noHyps );
2330     myDlg->tab( theDim )->setAvailableHyps( AddHyp,  noHyps );
2331     myDlg->tab( theDim )->setExistingHyps ( AddHyp,  noHyps );
2332   }
2333 }
2334
2335 //================================================================================
2336 /*!
2337  * \brief Generates default and sets mesh/submesh name
2338  *
2339  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
2340  */
2341 //================================================================================
2342 void SMESHGUI_MeshOp::setDefaultName( const QString& thePrefix ) const
2343 {
2344   QString aResName;
2345
2346   _PTR(Study) aStudy = SMESH::getStudy();
2347   int i = 1;
2348
2349   QString aPrefix = thePrefix;
2350   if ( aPrefix.isEmpty() )
2351     aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
2352
2353   _PTR(SObject) anObj;
2354   do
2355   {
2356     aResName = aPrefix + QString::number( i++ );
2357     anObj = aStudy->FindObject( aResName.toUtf8().data() );
2358   }
2359   while ( anObj );
2360
2361   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
2362     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
2363   aControl->setText( aResName );
2364 }
2365
2366 //================================================================================
2367 /*!
2368  * \brief Gets algorithm or creates it if necessary
2369   * \param theDim - specifies dimension of returned hypotheses/algorifms
2370   * \retval SMESH::SMESH_Hypothesis_var - algorithm
2371  *
2372  * Gets algorithm or creates it if necessary
2373  */
2374 //================================================================================
2375 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
2376 {
2377   SMESH::SMESH_Hypothesis_var anAlgoVar;
2378
2379   // get type of the selected algo
2380   int aHypIndex = currentHyp( theDim, Algo );
2381   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
2382   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
2383     return anAlgoVar;
2384   QString aHypName = dataList[ aHypIndex ]->TypeName;
2385
2386   // get existing algorithms
2387   _PTR(SObject) pComp = SMESH::getStudy()->FindComponent("SMESH");
2388   QStringList tmp;
2389   existingHyps( theDim, Algo, pComp, tmp, myExistingHyps[ theDim ][ Algo ]);
2390
2391   // look for an existing algo of such a type
2392   THypList& aHypVarList = myExistingHyps[ theDim ][ Algo ];
2393   THypList::iterator anIter = aHypVarList.begin();
2394   for ( ; anIter != aHypVarList.end(); anIter++)
2395   {
2396     SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
2397     if ( !aHypVar->_is_nil() && aHypName == SMESH::toQStr( aHypVar->GetName() ))
2398     {
2399       anAlgoVar = aHypVar;
2400       break;
2401     }
2402   }
2403
2404   if (anAlgoVar->_is_nil())
2405   {
2406     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
2407     if (aHypData)
2408     {
2409       QString aClientLibName = aHypData->ClientLibName;
2410       if ( aClientLibName.isEmpty() )
2411       {
2412         // Call hypothesis creation server method (without GUI)
2413         SMESH::SMESH_Hypothesis_var aHyp =
2414           SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
2415         aHyp.out();
2416       }
2417       else
2418       {
2419         // Get hypotheses creator client (GUI)
2420         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
2421
2422         // Create algorithm
2423         if (aCreator)
2424           aCreator->create( true, aHypName, myDlg, 0, QString::null );
2425         else {
2426           SMESH::SMESH_Hypothesis_var aHyp =
2427             SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
2428           aHyp.out();
2429         }
2430         delete aCreator;
2431       }
2432       QStringList tmpList;
2433       _PTR(SComponent) aFather = SMESH::getStudy()->FindComponent( "SMESH" );
2434       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
2435     }
2436
2437     THypList& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
2438     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
2439     {
2440       SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
2441       if ( !aHypVar->_is_nil() && aHypName == SMESH::toQStr( aHypVar->GetName() ))
2442       {
2443         anAlgoVar = aHypVar;
2444         break;
2445       }
2446     }
2447   }
2448
2449   return anAlgoVar._retn();
2450 }
2451
2452 //================================================================================
2453 /*!
2454  * \brief Reads parameters of an edited mesh/sub-mesh and assigns them to the dialog
2455  *
2456  * Called when mesh is edited only.
2457  */
2458 //================================================================================
2459 void SMESHGUI_MeshOp::readMesh()
2460 {
2461   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2462   _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
2463   if ( !pObj )
2464     return;
2465
2466   if ( myIsOnGeometry )
2467   {
2468     // Set name of mesh if current object is sub-mesh
2469     SMESH::SMESH_subMesh_var subMesh = SMESH::SObjectToInterface< SMESH::SMESH_subMesh >( pObj );
2470     if ( !subMesh->_is_nil() )
2471     {
2472       SMESH::SMESH_Mesh_var aMesh =  subMesh->GetFather();
2473       if ( !aMesh->_is_nil() )
2474       {
2475         _PTR(SObject) aMeshSO = SMESH::FindSObject( aMesh );
2476         QString     aMeshName = name( aMeshSO );
2477         myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
2478       }
2479       myHasConcurrentSubBefore = checkSubMeshConcurrency( aMesh, subMesh );
2480     }
2481
2482     if ( !myIsInvalidSubMesh )
2483     {
2484       // Set name of geometry object
2485       CORBA::String_var name = SMESH::GetGeomName( pObj );
2486       if ( name.in() )
2487         myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, name.in() );
2488     }
2489   }
2490
2491   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
2492   QStringList anExisting, anAvailable;
2493   bool algoFound = false;
2494   const int dim1st = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_2D;
2495   for ( int dim = dim1st; dim <= SMESH::DIM_3D; ++dim )
2496   {
2497     // get algorithm
2498     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
2499     // find algo index among available ones
2500     int aHypIndex = -1;
2501     anAvailable.clear();
2502     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
2503     {
2504       SMESH::SMESH_Hypothesis_var hyp = myObjHyps[ dim ][ Algo ].first().first;
2505       HypothesisData* algoData = SMESH::GetHypothesisData( SMESH::toQStr( hyp->GetName() ));
2506       availableHyps( dim, Algo, anAvailable, myAvailableHypData[ dim ][ Algo ] );
2507       aHypIndex = myAvailableHypData[ dim ][ Algo ].indexOf( algoData );
2508       //       if ( aHypIndex < 0 && algoData ) {
2509       //         // assigned algo is incompatible with other algorithms
2510       //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
2511       //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
2512       //       }
2513       algoFound = ( aHypIndex > -1 );
2514     }
2515     myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
2516     setCurrentHyp( dim, Algo, aHypIndex );
2517     // set existing and available hypothesis according to the selected algo
2518     if ( aHypIndex > -1 || !algoFound )
2519       onAlgoSelected( aHypIndex, dim );
2520   }
2521
2522   // get hypotheses
2523   bool hypWithoutAlgo = false;
2524   for ( int dim = dim1st; dim <= SMESH::DIM_3D; ++dim )
2525   {
2526     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2527     {
2528       // get hypotheses
2529       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2530       if ( myObjHyps[ dim ][ hypType ].count() == 0 ) {
2531         setCurrentHyp( dim, hypType, -1 );
2532       }
2533       for ( int i = 0, nb = myObjHyps[ dim ][ hypType ].count(); i < nb; ++i )
2534       {
2535         // find index of required hypothesis among existing ones for this dimension and type
2536         int aHypIndex = find( myObjHyps[ dim ][ hypType ][ i ].first,
2537                               myExistingHyps[ dim ][ hypType ] );
2538         if ( aHypIndex < 0 ) {
2539           // assigned hypothesis is incompatible with the algorithm
2540           if ( currentHyp( dim, Algo ) < 0 )
2541           { // none algo selected; it is edition for sure, of submesh maybe
2542             hypWithoutAlgo = true;
2543             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ][ i ] );
2544             anExisting.push_back( myObjHyps[ dim ][ hypType ][ i ].second );
2545             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
2546             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
2547           }
2548         }
2549         setCurrentHyp( dim, hypType + i, aHypIndex );
2550
2551         if ( hypType == MainHyp ) break; // only one main hyp allowed
2552       }
2553     }
2554   }
2555   // make available other hyps of same type as one without algo
2556   if ( hypWithoutAlgo )
2557     onAlgoSelected( currentHyp( /*dim=*/0, Algo ), /*dim=*/0 );
2558 }
2559
2560 //================================================================================
2561 /*!
2562  * \brief Gets name of object
2563  * \param theSO - SObject
2564  * \retval QString - name of object
2565  *
2566  * Gets name of object
2567  */
2568 //================================================================================
2569 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
2570 {
2571   QString aResName;
2572   if ( theSO )
2573   {
2574     _PTR(GenericAttribute) anAttr;
2575     _PTR(AttributeName)    aNameAttr;
2576     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
2577     {
2578       aNameAttr = anAttr;
2579       aResName = aNameAttr->Value().c_str();
2580     }
2581   }
2582   return aResName;
2583 }
2584
2585 //================================================================================
2586 /*!
2587  * \brief Finds hypothesis in input list
2588   * \param theHyp - hypothesis to be found
2589   * \param theHypList - input list of hypotheses
2590   * \retval int - index of hypothesis or -1 if it is not found
2591  *
2592  * Finds position of hypothesis in input list
2593  */
2594 //================================================================================
2595 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
2596                            const THypList& theHypList ) const
2597 {
2598   int aRes = -1;
2599   if ( !theHyp->_is_nil() )
2600   {
2601     int i = 0;
2602     THypList::const_iterator anIter = theHypList.begin();
2603     for ( ; anIter != theHypList.end(); ++ anIter)
2604     {
2605       if ( theHyp->_is_equivalent( (*anIter).first ) )
2606       {
2607         aRes = i;
2608         break;
2609       }
2610       i++;
2611     }
2612   }
2613   return aRes;
2614 }
2615
2616 //================================================================================
2617 /*!
2618  * \brief Edits mesh or sub-mesh
2619   * \param theMess - Output parameter intended for returning error message
2620   * \retval bool  - TRUE if mesh is edited successfully, FALSE otherwise
2621  *
2622  * Assigns new name hypotheses and algorithms to the mesh or sub-mesh
2623  */
2624 //================================================================================
2625 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
2626 {
2627   theMess = "";
2628
2629   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
2630   if ( aSMESHGen->_is_nil() )
2631     return false;
2632
2633   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2634   _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
2635   if ( !pObj )
2636     return false;
2637
2638   SUIT_OverrideCursor aWaitCursor;
2639
2640   // Set new name
2641   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
2642   SMESH::SetName( pObj, aName );
2643   int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_2D;
2644
2645   // First, remove old algos in order to avoid messages on algorithm hiding
2646   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2647   {
2648     if ( /*isAccessibleDim( dim ) &&*/ myObjHyps[ dim ][ Algo ].count() > 0 )
2649     {
2650       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first().first;
2651       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2652       if ( anAlgoVar->_is_nil() || // no new algo selected or
2653            SMESH::toQStr(anOldAlgo->GetName()) != SMESH::toQStr(anAlgoVar->GetName())) // algo change
2654       {
2655         // remove old algorithm
2656         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first().first );
2657         myObjHyps[ dim ][ Algo ].clear();
2658       }
2659     }
2660   }
2661
2662   SALOMEDS_SObject* aSObject = _CAST(SObject, pObj);
2663   CORBA::Object_var anObject = aSObject->GetObject();
2664   SMESH::SMESH_Mesh_var          aMesh = SMESH::SMESH_Mesh::_narrow( anObject );
2665   SMESH::SMESH_subMesh_var aSubMeshVar = SMESH::SMESH_subMesh::_narrow( anObject );
2666   bool isMesh = !aMesh->_is_nil();
2667   if ( !isMesh && !aSubMeshVar->_is_nil() )
2668     aMesh = aSubMeshVar->GetFather();
2669
2670   // Assign new algorithms and hypotheses
2671   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2672   {
2673     //if ( !isAccessibleDim( dim )) continue;
2674
2675     // find or create algorithm
2676     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2677
2678     // assign new algorithm
2679     if ( !anAlgoVar->_is_nil() && // some algo selected and
2680          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
2681     {
2682       if ( isMesh )
2683         SMESH::AddHypothesisOnMesh( aMesh, anAlgoVar );
2684       else if ( !aSubMeshVar->_is_nil() )
2685         SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
2686
2687       myObjHyps[ dim ][ Algo ].append( THypItem( anAlgoVar, aName) );
2688     }
2689
2690     // remove deselected hypotheses
2691     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2692     {
2693       for ( int i = 0, nb = myObjHyps[ dim ][ hypType ].count(); i < nb; ++i )
2694       {
2695         SMESH::SMESH_Hypothesis_var hyp = myObjHyps[ dim ][ hypType ][ i ].first;
2696         int hypIndex = this->find( hyp, myExistingHyps[ dim ][ hypType ]);
2697         if ( !isSelectedHyp( dim, hypType, hypIndex ) && !hyp->_is_nil() )
2698         {
2699           SMESH::RemoveHypothesisOrAlgorithmOnMesh( pObj, hyp );
2700         }
2701       }
2702     }
2703     // assign newly selected hypotheses
2704     for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(dim); dlgType++ )
2705     {
2706       const int curIndex = currentHyp( dim, dlgType );
2707       const int  hypType = Min( dlgType, AddHyp );
2708       if ( curIndex >= 0 && curIndex < myExistingHyps[ dim ][ hypType ].count() )
2709       {
2710         SMESH::SMESH_Hypothesis_var hyp = myExistingHyps[ dim ][ hypType ][ curIndex ].first;
2711
2712         bool isAssigned = ( this->find( hyp, myObjHyps[ dim ][ hypType ]) >= 0 );
2713         if ( !isAssigned )
2714         {
2715           if ( isMesh )
2716             SMESH::AddHypothesisOnMesh (aMesh, hyp );
2717           else if ( !aSubMeshVar->_is_nil() )
2718             SMESH::AddHypothesisOnSubMesh ( aSubMeshVar, hyp );
2719         }
2720       }
2721       // reread all hypotheses of mesh
2722       QStringList anExisting;
2723       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2724     }
2725   }
2726
2727   if ( aSubMeshVar->_is_nil() )
2728   {
2729     GEOM::GEOM_Object_var mainGeom = aMesh->GetShapeToMesh();
2730     createSubMeshOnInternalEdges( aMesh, mainGeom );
2731   }
2732
2733   myHasConcurrentSubBefore =
2734     checkSubMeshConcurrency( aMesh, aSubMeshVar, /*askUser=*/!myHasConcurrentSubBefore );
2735
2736   return true;
2737 }
2738
2739 //================================================================================
2740 /*!
2741  * \brief Checks if a concurrent sub-meshes appear as result of sub-mesh
2742  *        creation/edition and, if (askUser) , proposes the uses to set up a desired
2743  *        order of sub-mesh computation.
2744  *        Returns \c true if a sub-mesh concurrency detected.
2745  */
2746 //================================================================================
2747
2748 bool SMESHGUI_MeshOp::checkSubMeshConcurrency(SMESH::SMESH_Mesh_ptr    mesh,
2749                                               SMESH::SMESH_subMesh_ptr submesh,
2750                                               bool                     askUser)
2751 {
2752   if ( CORBA::is_nil( mesh ) || CORBA::is_nil( submesh ))
2753     return false;
2754
2755   bool isNewConcurrent = mesh->IsUnorderedSubMesh( submesh->GetId() );
2756   if ( isNewConcurrent && askUser )
2757   {
2758     int butID = SUIT_MessageBox::warning( myDlg->parentWidget(), tr( "SMESH_WARNING" ),
2759                                           tr("CONCURRENT_SUBMESH_APPEARS"),
2760                                           tr("SMESH_BUT_YES"), tr("SMESH_BUT_NO"));
2761     if ( butID == 0 )
2762     {
2763       _PTR(SObject) meshSO = SMESH::FindSObject( mesh );
2764       LightApp_SelectionMgr* aSelectionMgr = selectionMgr();
2765       if ( meshSO && aSelectionMgr )
2766       {
2767         myDlg->setEnabled( false ); // disactivate selection
2768         selectionMgr()->clearFilters();
2769         selectObject( meshSO );
2770         SMESHGUI::GetSMESHGUI()->OnGUIEvent( SMESHOp::OpMeshOrder ); // MESH_ORDER
2771         qApp->processEvents();
2772
2773         myDlg->setEnabled( true );
2774         int obj = myDlg->getActiveObject();
2775         onActivateObject( obj ); // restore filter
2776         if ( !myToCreate )
2777         {
2778           selectObject( SMESH::FindSObject( submesh ));
2779           selectionDone();
2780         }
2781       }
2782     }
2783   }
2784
2785   return isNewConcurrent;
2786 }
2787
2788 //================================================================================
2789 /*!
2790  * \brief Verifies whether given operator is valid for this one
2791  * \param theOtherOp - other operation
2792  * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
2793  *
2794  * method redefined from base class verifies whether given operator is valid for
2795  * this one (i.e. can be started "above" this operator). In current implementation method
2796  * returns false if theOtherOp operation is not intended for deleting objects or mesh
2797  * elements.
2798  */
2799 //================================================================================
2800 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
2801 {
2802   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
2803 }
2804
2805 //================================================================================
2806 /*!
2807  * \brief SLOT. Is called when the user selects a way of geometry selection
2808  * \param theByMesh - true if the user wants to find geometry by mesh element
2809  */
2810 //================================================================================
2811 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
2812 {
2813   if ( theByMesh ) {
2814     if ( !myShapeByMeshOp ) {
2815       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
2816       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2817               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2818       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2819               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2820     }
2821     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2822     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
2823     if ( _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() )) {
2824       SMESH::SMESH_Mesh_var aMesh =
2825         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
2826       if ( !aMesh->_is_nil() ) {
2827         myDlg->hide(); // stop processing selection
2828         myShapeByMeshOp->setModule( getSMESHGUI() );
2829         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2830         myShapeByMeshOp->SetMesh( aMesh );
2831         myShapeByMeshOp->start();
2832       }
2833     }
2834   }
2835 }
2836
2837 //================================================================================
2838 /*!
2839  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2840  */
2841 //================================================================================
2842 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
2843 {
2844   if ( myShapeByMeshOp == op ) {
2845     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
2846     myDlg->show();
2847     // Select a found geometry object
2848     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2849     if ( !aGeomVar->_is_nil() )
2850     {
2851       QString ID = SMESH::toQStr( aGeomVar->GetStudyEntry() );
2852       if ( _PTR(SObject) aGeomSO = SMESH::getStudy()->FindObjectID( ID.toUtf8().data() )) {
2853         selectObject( aGeomSO );
2854         selectionDone();
2855       }
2856     }
2857   }
2858 }
2859
2860 //================================================================================
2861 /*!
2862  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2863  */
2864 //================================================================================
2865 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
2866 {
2867   if ( myShapeByMeshOp == op && myDlg ) {
2868     myDlg->show();
2869   }
2870 }
2871
2872 //================================================================================
2873 /*!
2874  * \brief Selects a SObject
2875  * \param theSObj - the SObject to select
2876  */
2877 //================================================================================
2878 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
2879 {
2880   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
2881     SALOME_ListIO anIOList;
2882     if ( theSObj ) {
2883       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2884         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
2885       anIOList.Append( anIO );
2886     }
2887     sm->setSelectedObjects( anIOList, false );
2888   }
2889 }
2890 //================================================================================
2891 /*!
2892  * \brief Update available list of mesh types
2893  */
2894 //================================================================================
2895 void SMESHGUI_MeshOp::updateMeshTypeList()
2896 {
2897   QStringList meshTypes;
2898   meshTypes.append( tr( "MT_ANY" ) );
2899   if ( myMaxShapeDim >= 2 || myMaxShapeDim == -1 )
2900   {
2901     meshTypes.append( tr( "MT_TRIANGULAR" ) );
2902     meshTypes.append( tr( "MT_QUADRILATERAL" ) );
2903   }
2904   if ( myMaxShapeDim == 3 || myMaxShapeDim == -1 )
2905   {
2906     meshTypes.append( tr( "MT_TETRAHEDRAL" ) );
2907     meshTypes.append( tr( "MT_HEXAHEDRAL" ) );
2908   }
2909
2910   myDlg->setAvailableMeshType( meshTypes );
2911 }
2912
2913 //================================================================================
2914 /*!
2915  * \brief Update available list of hypothesis sets
2916  */
2917 //================================================================================
2918 void SMESHGUI_MeshOp::updateHypoSets()
2919 {
2920   // get available algorithms taking into account geometry and mesh type
2921   QStringList algosAvailable;
2922   if ( myDlg->currentMeshType() != MT_ANY )
2923     for ( int dim = 0; dim <= 3; ++dim )
2924       availableHyps( dim, Algo, algosAvailable, myAvailableHypData[ dim ][ Algo ] );
2925
2926   THypDataList anAvailableAlgsData;
2927   QStringList aHypothesesSetsList = SMESH::GetHypothesesSets( myMaxShapeDim );
2928   QStringList aFilteredHypothesesSetsList;
2929   QStringList::const_iterator inHypoSetName = aHypothesesSetsList.begin();
2930   for ( ; inHypoSetName != aHypothesesSetsList.end(); ++inHypoSetName )
2931   {
2932     HypothesesSet* currentHypoSet = SMESH::GetHypothesesSet( *inHypoSetName );
2933     HypothesesSet::SetType sType;
2934     for ( sType = HypothesesSet::MAIN; sType <= HypothesesSet::ALT; SMESHUtils::Increment( sType ))
2935     {
2936       bool isAvailable = false;
2937       for ( currentHypoSet->init( true, sType ); currentHypoSet->more(); currentHypoSet->next() )
2938       {
2939         isAvailable = false;
2940         if ( HypothesisData* algoDataIn = SMESH::GetHypothesisData( currentHypoSet->current() )) {
2941           for ( int dim = SMESH::DIM_0D; dim <= myMaxShapeDim; dim++) {
2942             int aCurrentAvailableAlgo = myAvailableHypData[dim][Algo].indexOf( algoDataIn );
2943             if ( aCurrentAvailableAlgo > -1 ) {
2944               isAvailable = true;
2945               break;
2946             }
2947           }
2948           if ( !isAvailable )
2949             break;
2950         }
2951       }
2952       currentHypoSet->setAlgoAvailable( sType, isAvailable );
2953     }
2954     if ( currentHypoSet->hasAlgo( HypothesesSet::MAIN ) &&
2955          currentHypoSet->hasAlgo( HypothesesSet::ALT ))
2956     {
2957       HypothesesSet::SetType setType = HypothesesSet::getPreferredHypType();
2958       if ( !currentHypoSet->getAlgoAvailable( setType ))
2959         continue; // not add if a preferred type not available currently
2960     }
2961
2962     if ( currentHypoSet->getAlgoAvailable( HypothesesSet::MAIN ) ||
2963          currentHypoSet->getAlgoAvailable( HypothesesSet::ALT ))
2964     {
2965       aFilteredHypothesesSetsList.append( *inHypoSetName );
2966     }
2967   }
2968   myDlg->setHypoSets( aFilteredHypothesesSetsList );
2969 }
2970
2971 //================================================================================
2972 /*!
2973  * \brief SLOT. Is called when the user select type of mesh
2974  * \param theTabIndex - Index of current active tab
2975  * \param theIndex - Index of current type of mesh
2976  */
2977 //================================================================================
2978 void SMESHGUI_MeshOp::onAlgoSetByMeshType( const int /*theTabIndex*/, const int /*theIndex*/ )
2979 {
2980   setFilteredAlgoData();
2981 }