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