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