Salome HOME
bos#40018 - Mesh hypothesis window is not modal
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SMESHGUI_MeshOp.cxx
23 //  Author : Sergey LITONIN, Open CASCADE S.A.S.
24
25 // SMESH includes
26 #include "SMESHGUI_MeshOp.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_GEOMGenUtils.h"
30 #include "SMESHGUI_GroupOnShapeDlg.h"
31 #include "SMESHGUI_Hypotheses.h"
32 #include "SMESHGUI_HypothesesUtils.h"
33 #include "SMESHGUI_MeshDlg.h"
34 #include "SMESHGUI_Operations.h"
35 #include "SMESHGUI_ShapeByMeshDlg.h"
36 #include "SMESHGUI_Utils.h"
37 #include "SMESH_NumberFilter.hxx"
38 #include "SMESH_TypeDefs.hxx"
39 #include "SMESH_TypeFilter.hxx"
40 #include "SMESH_Gen_i.hxx"
41
42 #include CORBA_SERVER_HEADER(SMESH_BasicHypothesis)
43
44 // SALOME GEOM includes
45 #include <GEOMBase.h>
46 #include <GEOMImpl_Types.hxx>
47 #include <GEOM_Client.hxx>
48 #include <GEOM_SelectionFilter.h>
49 #include <GEOM_wrap.hxx>
50 #include <GeometryGUI.h>
51
52 // SALOME GUI includes
53 #include <LightApp_Application.h>
54 #include <LightApp_SelectionMgr.h>
55 #include <LightApp_UpdateFlags.h>
56 #include <SALOME_InteractiveObject.hxx>
57 #include <SALOME_ListIO.hxx>
58 #include <SUIT_MessageBox.h>
59 #include <SUIT_OverrideCursor.h>
60 #include <SUIT_ResourceMgr.h>
61 #include <SUIT_Session.h>
62 #include <SalomeApp_Application.h>
63 #include <SalomeApp_Tools.h>
64
65 // SALOME KERNEL includes
66 #include <SALOMEDS_SComponent.hxx>
67 #include <SALOMEDS_SObject.hxx>
68 #include <SALOMEDS_Study.hxx>
69 #include <SALOMEDS_wrap.hxx>
70
71 // Qt includes
72 #include <QApplication>
73 #include <QLineEdit>
74 #include <QStringList>
75
76 // OCCT includes
77 #include <BRep_Tool.hxx>
78 #include <TopExp.hxx>
79 #include <TopExp_Explorer.hxx>
80 #include <TopTools_IndexedMapOfShape.hxx>
81 #include <TopoDS.hxx>
82
83 //================================================================================
84 /*!
85  * \brief Constructor
86   * \param theToCreate - if this parameter is true then operation is used for creation,
87   * for editing otherwise
88  *
89  * Initialize operation
90 */
91 //================================================================================
92 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
93 : SMESHGUI_SelectionOp(),
94   myDlg( 0 ),
95   myShapeByMeshOp( 0 ),
96   myToCreate( theToCreate ),
97   myIsMesh( theIsMesh ),
98   myIsInvalidSubMesh( false )
99 {
100   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
101     GeometryGUI::InitGeomGen();
102   myIsOnGeometry = true;
103   myMaxShapeDim = -1;
104 }
105
106 //================================================================================
107 /*!
108  * \brief Destructor
109 */
110 //================================================================================
111 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
112 {
113   if ( myDlg )
114     delete myDlg;
115 }
116
117 //================================================================================
118 /*!
119  * \brief Gets dialog of this operation
120   * \retval LightApp_Dialog* - pointer to dialog of this operation
121 */
122 //================================================================================
123 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
124 {
125   return myDlg;
126 }
127
128 //================================================================================
129 /*!
130  * \brief 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       myDlg->hide();
1296       aCreator->create(initParamHyp, aHypName, myDlg, this, SLOT( onHypoCreated( int ) ) );
1297       dialog = true;
1298     }
1299     else
1300     {
1301      SMESH::SMESH_Hypothesis_var aHyp =
1302        SMESH::CreateHypothesis(theTypeName, aHypName, false);
1303      aHyp.out();
1304     }
1305   }
1306
1307   if( !dialog )
1308     onHypoCreated(2);
1309 }
1310
1311 //================================================================================
1312 /*!
1313  *  Necessary steps after hypothesis creation
1314  *  \param result - creation result:
1315  *   0 = rejected
1316  *   1 = accepted
1317  *   2 = additional value meaning that slot is called not from dialog box
1318  */
1319 //================================================================================
1320 void SMESHGUI_MeshOp::onHypoCreated( int result )
1321 {
1322   if( result != 2 )
1323   {
1324     int obj = myDlg->getActiveObject();
1325     onActivateObject( obj ); // Issue 0020170. Restore filters
1326     myDlg->setEnabled( true );
1327     myDlg->show();
1328   }
1329
1330   _PTR(SComponent) aFather = SMESH::getStudy()->FindComponent("SMESH");
1331
1332   int nbHyp = myExistingHyps[myDim][myType].count();
1333   HypothesisData* algoData = hypData( myDim, Algo, currentHyp( myDim, Algo ));
1334   QStringList aNewHyps;
1335   existingHyps(myDim, myType, aFather, aNewHyps, myExistingHyps[myDim][myType], algoData);
1336   if (aNewHyps.count() > nbHyp)
1337   {
1338     for (int i = nbHyp; i < aNewHyps.count(); i++)
1339       myDlg->tab(myDim)->addHyp(myType, aNewHyps[i]);
1340   }
1341   return;
1342 }
1343
1344 //================================================================================
1345 /*!
1346  * \brief Calls plugin methods for hypothesis editing
1347   * \param theHypType - specifies whether main hypothesis or additional one
1348   * is edited
1349   * \param theIndex - index of existing hypothesis
1350  *
1351  * Calls plugin methods for hypothesis editing
1352  */
1353 //================================================================================
1354 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
1355 {
1356   // Speicfies dimension of hypothesis to be created
1357   int aDim = getTabDim( sender(), myDlg );
1358   if (aDim == -1)
1359     return;
1360
1361   const THypList& aList = myExistingHyps[ aDim ][ theHypType ];
1362   if ( theIndex < 0 || theIndex >= aList.count() )
1363     return;
1364   const THypItem& aHypItem = aList[ theIndex ];
1365   SMESH::SMESH_Hypothesis_var aHyp = aHypItem.first;
1366   if ( aHyp->_is_nil() )
1367     return;
1368
1369   SMESHGUI_GenericHypothesisCreator* aCreator =
1370     SMESH::GetHypothesisCreator( SMESH::toQStr( aHyp->GetName() ));
1371   if ( aCreator )
1372   {
1373     // set initial parameters
1374     SMESH::SMESH_Hypothesis_var initParamHyp =
1375       getInitParamsHypothesis( SMESH::toQStr( aHyp->GetName() ),
1376                                SMESH::toQStr( aHyp->GetLibName() ));
1377     aCreator->setInitParamsHypothesis( initParamHyp );
1378
1379     // set shapes, of mesh and sub-mesh if any
1380     initHypCreator( aCreator );
1381
1382     removeCustomFilters(); // Issue 0020170
1383     myDlg->setEnabled( false );
1384     myDlg->hide();
1385
1386     aCreator->edit( aHyp.in(), aHypItem.second, dlg(), this, SLOT( onHypoEdited( int ) ) );
1387   }
1388 }
1389
1390 //================================================================================
1391 /*!
1392  *  Necessary steps after hypothesis edition
1393  *  \param result - creation result:
1394  *   0 = rejected
1395  *   1 = accepted
1396  */
1397 //================================================================================
1398 void SMESHGUI_MeshOp::onHypoEdited( int /*result*/ )
1399 {
1400   int obj = myDlg->getActiveObject();
1401   onActivateObject( obj ); // Issue 0020170. Restore filters
1402   myDlg->setEnabled( true );
1403   myDlg->show();
1404 }
1405
1406 //================================================================================
1407 /*!
1408  * \brief access to hypothesis data
1409   * \param theDim - hyp dimension
1410   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
1411   * \param theIndex - index in the list
1412   * \retval HypothesisData* - result data, may be 0
1413   */
1414 //================================================================================
1415 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
1416                                           const int theHypType,
1417                                           const int theIndex)
1418 {
1419   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
1420        theHypType > -1 && theHypType < NbHypTypes &&
1421        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
1422     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
1423   return 0;
1424 }
1425
1426 //================================================================================
1427 /*!
1428  * \brief Update list of algorithms and hypotheses according to currently selected
1429  *        algorithm, geometry and mesh type
1430  */
1431 //================================================================================
1432 void SMESHGUI_MeshOp::setFilteredAlgoData()
1433 {
1434   // keep current algorithms
1435   HypothesisData* curAlgos[ 4 ];
1436   for ( int dim = 0; dim <= 3; ++dim )
1437     curAlgos[ dim ] = hypData( dim, Algo, currentHyp( dim, Algo ));
1438
1439   // find out myMaxShapeDim by checking algorithm applicability to geometry
1440
1441   int curMeshType = myDlg->currentMeshType();
1442   myDlg->setCurrentMeshType( MT_ANY );
1443   QStringList algosAvailable;
1444   // get available algorithms taking into account geometry only
1445   if ( myGeom->_is_nil() )
1446   {
1447     myMaxShapeDim = 3;
1448
1449     if ( myToCreate ) // readMesh() has done it if !myToCreate
1450       for ( int dim = 0; dim <= 3; ++dim )
1451         availableHyps( dim, Algo, algosAvailable, myAvailableHypData[ dim ][ Algo ] );
1452   }
1453   else
1454   {
1455     if ( myGeomEntry != myLastGeomEntry )
1456       myHypMapIsApplicable.clear();
1457
1458     for ( int dim = 0; dim <= 3; ++dim )
1459     {
1460       availableHyps( dim, Algo, algosAvailable, myAvailableHypData[ dim ][ Algo ] );
1461       if ( algosAvailable.count() )
1462         myMaxShapeDim = dim;
1463     }
1464     if ( !myGeomEntry.isEmpty() )
1465       myLastGeomEntry = myGeomEntry;
1466   }
1467   myDlg->setMaxHypoDim( myMaxShapeDim );
1468
1469   // set mesh types according to myMaxShapeDim
1470   updateMeshTypeList();
1471
1472   // update available hypo-sets that depend on geometry and mesh type
1473   myDlg->setCurrentMeshType( Max( MT_ANY, curMeshType ));
1474   updateHypoSets(); // it sets myAvailableHypData by curMeshType
1475
1476   // restore current algorithms according to changed myAvailableHypData
1477   algosAvailable.clear();
1478   for ( int dim = 0; dim <= 3; ++dim )
1479   {
1480     int algoIndex = myAvailableHypData[ dim ][ Algo ].indexOf( curAlgos[ dim ]);
1481     while ( algosAvailable.count() <= algoIndex )
1482       algosAvailable << "";
1483     myDlg->tab( dim )->setAvailableHyps( Algo, algosAvailable );
1484     setCurrentHyp( dim, Algo, algoIndex );
1485     if ( algoIndex < 0 )
1486       curAlgos[ dim ] = 0;
1487   }
1488
1489   // find a selected algo, current or of highest dimension
1490   int algoDim = myDlg->currentTab();
1491   if ( !curAlgos[ algoDim ])
1492     for ( algoDim = SMESH::DIM_3D; algoDim >= SMESH::DIM_0D; algoDim-- )
1493       if ( curAlgos[ algoDim ] )
1494         break;
1495   if ( algoDim < SMESH::DIM_0D )
1496     algoDim = myMaxShapeDim;
1497
1498   // set algorithms and hypotheses according to all parameters (mesh type etc)
1499   onAlgoSelected( currentHyp( algoDim, Algo ), algoDim );
1500 }
1501
1502 //================================================================================
1503 /*!
1504  * \brief Set available algos and hypos according to the selected algorithm
1505  * \param theIndex - algorithm index
1506  * \param theDim - algorithm dimension
1507  */
1508 //================================================================================
1509 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
1510                                       const int theDim )
1511 {
1512   if ( myIgnoreAlgoSelection )
1513     return;
1514
1515   int curDim = getTabDim( sender(), myDlg );
1516   int aDim = theDim < 0 ? curDim : theDim;
1517   if (aDim == -1)
1518     return;
1519
1520   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1521
1522   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1523   HypothesisData* algoByDim[4];
1524   algoByDim[ aDim ] = algoData;
1525
1526   QStringList anAvailable;
1527   const int algoDim = aDim;
1528
1529   // 2 loops: backward and forward from algo dimension (forward == to higher dimension)
1530   for ( int forward = 1; forward >= 0; --forward )
1531   {
1532     int dim = algoDim, lastDim = SMESH::DIM_3D, dir = 1;
1533     if ( !forward ) {
1534       dim = algoDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
1535     }
1536     //bool noCompatible = false;
1537     for ( ; dim * dir <= lastDim * dir; dim += dir)
1538     {
1539       // if ( noCompatible ) // the selected algo has no compatible ones (like 1D2D3D algo)
1540       // {
1541       //   anAvailable.clear();
1542       //   myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1543       //   myAvailableHypData[dim][Algo].clear();
1544       //   algoByDim[ dim ] = 0;
1545       // }
1546       // else
1547       {
1548         int           algoIndex = currentHyp( dim, Algo );
1549         HypothesisData *curAlgo = hypData( dim, Algo, algoIndex );
1550
1551         // set new available algorithms
1552         HypothesisData *prevAlgo = 0;
1553         if ( dim != algoDim )
1554           for ( int prevDim = dim + 1; prevDim <=3 && !prevAlgo; ++prevDim )
1555             prevAlgo = hypData( prevDim, Algo, currentHyp( prevDim, Algo ));
1556         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
1557         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1558
1559         //noCompatible = ( prevAlgo && !forward && anAvailable.isEmpty() );
1560
1561         // set current algo
1562         algoIndex = myAvailableHypData[dim][Algo].indexOf( curAlgo );
1563         setCurrentHyp( dim, Algo, algoIndex, /*updateHyps=*/true );
1564
1565         algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1566       }
1567
1568       // activate tabs according to algorithms availability
1569       if ( anAvailable.isEmpty() ) myDlg->disableTab( dim );
1570       else                         myDlg->enableTab( dim );
1571
1572     } // loop on dims
1573   } // loops backward and forward
1574
1575
1576   // set hypotheses corresponding to the found algorithms
1577
1578   _PTR(SObject) pComp = SMESH::getStudy()->FindComponent("SMESH");
1579
1580   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1581   {
1582     if ( !isAccessibleDim( dim ))
1583       continue;
1584
1585     // get indices of selected hyps
1586     const int nbTypes = nbDlgHypTypes(dim);
1587     std::vector<int> hypIndexByType( nbTypes, -1 );
1588     for ( int dlgType = MainHyp; dlgType < nbTypes; dlgType++ )
1589     {
1590       hypIndexByType[ dlgType ] = currentHyp( dim, dlgType );
1591     }
1592
1593     // update hyps
1594     for ( int dlgType = MainHyp; dlgType < nbTypes; dlgType++ )
1595     {
1596       const int type = Min( dlgType, AddHyp );
1597       myAvailableHypData[ dim ][ type ].clear();
1598       QStringList anAvailable, anExisting;
1599
1600       HypothesisData* curAlgo = algoByDim[ dim ];
1601       int hypIndex = hypIndexByType[ dlgType ];
1602
1603       SMESH::SMESH_Hypothesis_var curHyp;
1604       QString                     curHypType;
1605       HypothesisData*             hypData = 0;
1606       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1607       {
1608         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ].first;
1609         if ( !curHyp->_is_nil() )
1610         {
1611           curHypType = SMESH::toQStr( curHyp->GetName() );
1612           hypData = SMESH::GetHypothesisData( curHypType );
1613         }
1614       }
1615       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) // edition, algo not selected
1616       {
1617         // try to find algo by selected hypothesis in order to keep it selected
1618         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1619         if ( !algoDeselectedByUser &&
1620              myObjHyps[ dim ][ type ].count() > 0 &&
1621              curHypType == SMESH::toQStr( myObjHyps[ dim ][ type ].first().first->GetName()) )
1622         {
1623           for ( int i = 0; i < myAvailableHypData[ dim ][ Algo ].count(); ++i )
1624           {
1625             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1626             if ( curAlgo && hypData && isCompatible( curAlgo, hypData, type ))
1627               break;
1628             else
1629               curAlgo = 0;
1630           }
1631         }
1632       }
1633
1634       // get hyps compatible with curAlgo
1635       bool defaulHypAvlbl = false;
1636       if ( curAlgo )
1637       {
1638         if ( hypData && !isCompatible( curAlgo, hypData, type ))
1639           curHyp = SMESH::SMESH_Hypothesis::_nil();
1640         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo );
1641         existingHyps ( dim, type, pComp, anExisting, myExistingHyps[ dim ][ type ], curAlgo );
1642         defaulHypAvlbl = ( type == MainHyp && !curAlgo->IsAuxOrNeedHyp );
1643       }
1644       // set list of hypotheses
1645       if ( dlgType <= AddHyp )
1646       {
1647         myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1648         myDlg->tab( dim )->setExistingHyps( type, anExisting, defaulHypAvlbl );
1649       }
1650       // set current existing hypothesis
1651       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1652         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1653       else
1654         hypIndex = -1;
1655       if ( !isSubmesh && myToCreate && hypIndex < 0 && anExisting.count() == 1 && dim == curDim )
1656       {
1657         // none is yet selected => select the sole existing if it is not optional
1658         CORBA::String_var hypTypeName = myExistingHyps[ dim ][ type ].first().first->GetName();
1659         bool isOptional = true;
1660         if ( algoByDim[ dim ] &&
1661              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName.in(), isOptional ) &&
1662              !isOptional )
1663           hypIndex = 0;
1664       }
1665       setCurrentHyp( dim, dlgType, hypIndex );
1666     }
1667   }
1668   return;
1669 }
1670
1671 //================================================================================
1672 /*!
1673  * \brief Create a sub-mesh on internal edges in the case where the global algorithm
1674  *        is of type 1D-2D[-3D] so that the internal edges would remain not meshed.
1675  */
1676 //================================================================================
1677
1678 void SMESHGUI_MeshOp::createSubMeshOnInternalEdges( SMESH::SMESH_Mesh_ptr theMesh,
1679                                                     GEOM::GEOM_Object_ptr theMainShape )
1680 {
1681   if ( theMesh->_is_nil() || theMainShape->_is_nil() )
1682     return;
1683
1684   if ( isAccessibleDim( 1 ))
1685     return; // global 1D algorithm is/can be assigned
1686
1687   const HypothesesSet::SetType internSet = HypothesesSet::INTERN;
1688   bool toCreate = true;
1689   bool toCreateMandatory = ( myHypoSet &&
1690                              myAverageSize > 0 &&
1691                              ( myHypoSet->init( /*algo=*/ true, internSet ), myHypoSet->more()) &&
1692                              ( myHypoSet->init( /*algo=*/false, internSet ), myHypoSet->more()));
1693   if ( !toCreateMandatory ) // ask the user
1694     toCreate = false; // can't pass both mesh and geometry to Create Submesh operation (so far?)
1695     // toCreate = SUIT_MessageBox::warning( SMESHGUI::desktop(),
1696     //                                      QObject::tr("SMESH_WRN_WARNING"),
1697     //                                      QObject::tr("SMESH_CREATE_SUBMESH_ON_INTERNAL_EDGES"),
1698     //                                      QObject::tr("SMESH_BUT_YES"),
1699     //                                      QObject::tr("SMESH_BUT_NO"), 1, 0);
1700   if ( !toCreate )
1701     return;
1702
1703   TopoDS_Shape shape;
1704   if ( !GEOMBase::GetShape( theMainShape, shape ))
1705     return;
1706
1707   std::vector< TopoDS_Shape > internalEdges;
1708   for ( TopExp_Explorer edge( shape, TopAbs_EDGE, TopAbs_WIRE ); edge.More(); edge.Next() )
1709     internalEdges.push_back( edge.Current() );
1710
1711   if ( internalEdges.empty() )
1712     return;
1713
1714   TopTools_IndexedMapOfShape shapeIDs;
1715   TopExp::MapShapes( shape, shapeIDs );
1716
1717   std::set< int > intIDSet;
1718   for ( size_t i = 0; i < internalEdges.size(); ++i )
1719     intIDSet.insert( shapeIDs.FindIndex( internalEdges[ i ]));
1720
1721   GEOM::GEOM_Gen_var geomGen = theMainShape->GetGen();
1722   if (geomGen->_is_nil()) return;
1723
1724   GEOM::GEOM_Object_var edgeGroup;
1725   GEOM::GEOM_IShapesOperations_wrap sOp = geomGen->GetIShapesOperations();
1726   GEOM::GEOM_IGroupOperations_wrap  gOp = geomGen->GetIGroupOperations();
1727   GEOM::ListOfGO_var         geomGroups = sOp->GetExistingSubObjects( theMainShape,
1728                                                                       /*groupsOnly=*/true );
1729   for ( CORBA::ULong i = 0; i < geomGroups->length(); ++i )
1730   {
1731     GEOM::ListOfLong_var ids = gOp->GetObjects( geomGroups[ i ]);
1732     std::set< int > idSet( & ids[0], & ids[0] + ids->length() );
1733     if ( idSet == intIDSet )
1734     {
1735       edgeGroup = geomGroups[ i ];
1736       break;
1737     }
1738   }
1739
1740   if ( edgeGroup->_is_nil() )
1741   {
1742     GEOM::GEOM_Object_var edgeGroup = gOp->CreateGroup( theMainShape, TopAbs_EDGE );
1743
1744     GEOM::ListOfLong_var edgeIDs = new GEOM::ListOfLong;
1745     edgeIDs->length( internalEdges.size() );
1746     std::set< int >::iterator id = intIDSet.begin();
1747     for ( size_t i = 0; i < intIDSet.size(); ++i, ++id )
1748       edgeIDs[ i ] = *id;
1749     gOp->UnionIDs( edgeGroup, edgeIDs );
1750
1751     SALOMEDS::SObject_wrap so = geomGen->AddInStudy( edgeGroup, "Internal edges", theMainShape );
1752   }
1753
1754   if ( !toCreateMandatory )
1755   {
1756     // show Create Sub-mesh dislog
1757     // _PTR(SObject) aMeshSO = SMESH::FindSObject( theMesh );
1758     // selectionMgr()->clearFilters();
1759     // selectObject( pSubmesh );
1760     // SMESHGUI::GetSMESHGUI()->switchToOperation( SMESHOp::OpEditMeshOrSubMesh );
1761     return;
1762   }
1763
1764   // create a sub-mesh using myAverageSize w/o GUI
1765
1766   SMESH::SMESH_subMesh_var subMesh = theMesh->GetSubMesh( edgeGroup, "" );
1767
1768   for ( int isAlgo = 1; isAlgo >= 0; --isAlgo )
1769     for ( myHypoSet->init( isAlgo, internSet ); myHypoSet->more(); myHypoSet->next() )
1770     {
1771       QString    aHypoTypeName = myHypoSet->current();
1772       HypothesisData* aHypData = SMESH::GetHypothesisData( aHypoTypeName );
1773       if ( !aHypData )
1774         continue;
1775
1776       myDim = aHypData->Dim[0];
1777       if ( myDim != 1 )
1778         continue;
1779
1780       // create or/and set
1781       SMESH::SMESH_Hypothesis_var newHypo;
1782       if ( isAlgo )
1783       {
1784         myAvailableHypData[ myDim ][ Algo ].clear();
1785         myAvailableHypData[ myDim ][ Algo ] << aHypData;
1786         QStringList hypList; hypList << aHypoTypeName;
1787         myDlg->tab( myDim )->setAvailableHyps( Algo, hypList );
1788         setCurrentHyp( myDim, Algo, 0 );
1789         newHypo = getAlgo( myDim );
1790       }
1791       else
1792       {
1793         SMESH::HypInitParams params = { 2, myAverageSize, false };
1794         newHypo = getInitParamsHypothesis( aHypData->TypeName, aHypData->ServerLibName, & params );
1795         QString hypName = GetUniqueName( getHypoNames(), aHypData->Label );
1796         SALOMEDS::SObject_wrap so =
1797           SMESHGUI::GetSMESHGen()->PublishInStudy( SALOMEDS::SObject::_nil(), newHypo,
1798                                                    hypName.toUtf8().data() );
1799       }
1800       SMESH::AddHypothesisOnSubMesh( subMesh, newHypo );
1801     }
1802
1803   return;
1804 }
1805
1806 //================================================================================
1807 /*!
1808  * \brief Ask the user to enter an average size which will be used to create
1809  *        hypotheses of a hypo-set basing on this size
1810  *  \param [out] averageSize - average element size
1811  *  \return bool - false if the user canceled the dialog
1812  */
1813 //================================================================================
1814
1815 bool SMESHGUI_MeshOp::getAverageSize( double & averageSize )
1816 {
1817   HypothesisData* hypData = SMESH::GetHypothesisData( "MaxLength" );
1818   if ( !hypData )
1819     return false;
1820
1821   SMESH::SMESH_Hypothesis_var hyp = getInitParamsHypothesis( hypData->TypeName,
1822                                                              hypData->ServerLibName );
1823   if ( hyp->_is_nil() )
1824     return false;
1825
1826   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( hypData->TypeName );
1827   if ( !aCreator )
1828     return false;
1829   
1830   initHypCreator( aCreator );
1831   myDlg->setEnabled( false );
1832
1833   aCreator->edit( hyp.in(), HypothesesSet::getCommonHypoSetHypoType(),
1834                   dlg(), this, SLOT( onHypoEdited( int )));
1835
1836   StdMeshers::StdMeshers_MaxLength_var lenHyp = StdMeshers::StdMeshers_MaxLength::_narrow( hyp );
1837   if ( lenHyp->_is_nil() )
1838     return false;
1839
1840   averageSize = lenHyp->GetLength();
1841   return true;
1842 }
1843
1844 //================================================================================
1845 /*!
1846  * \brief Creates and selects hypothesis of hypotheses set
1847  * \param theSetName - The name of hypotheses set
1848  */
1849 //================================================================================
1850 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1851 {
1852   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet(theSetName);
1853   myHypoSet = aHypoSet;
1854   if (!aHypoSet)
1855     return;
1856
1857   // clear all hyps
1858   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++)
1859   {
1860     setCurrentHyp(dim, Algo, -1);
1861     setCurrentHyp(dim, AddHyp, -1);
1862     setCurrentHyp(dim, MainHyp, -1);
1863     onAlgoSelected( -1, dim );
1864   }
1865
1866   HypothesesSet::SetType setType = aHypoSet->getPreferredHypType();
1867   if ( !aHypoSet->getAlgoAvailable( setType ))
1868   {
1869     setType = ( setType == HypothesesSet::ALT ) ? HypothesesSet::MAIN : HypothesesSet::ALT;
1870     if ( !aHypoSet->getAlgoAvailable( setType ))
1871       return;
1872   }
1873
1874   myAverageSize = -1;
1875   if ( aHypoSet->toUseCommonSize() && !getAverageSize( myAverageSize ))
1876     return;
1877
1878   int maxDim = -1;
1879   for ( int isAlgo = 1; isAlgo >= 0; --isAlgo )
1880     for ( aHypoSet->init( isAlgo, setType ); aHypoSet->more(); aHypoSet->next() )
1881     {
1882       QString    aHypoTypeName = aHypoSet->current();
1883       HypothesisData* aHypData = SMESH::GetHypothesisData( aHypoTypeName );
1884       if (!aHypData)
1885         continue;
1886
1887       myDim = aHypData->Dim[0];
1888       // create or/and set
1889       if ( isAlgo )
1890       {
1891         int index = myAvailableHypData[myDim][Algo].indexOf( aHypData );
1892         if ( index >= 0 )
1893         {
1894           setCurrentHyp( myDim, Algo, index );
1895           onAlgoSelected( index, myDim );
1896           maxDim = Max( maxDim, myDim );
1897         }
1898       }
1899       else
1900       {
1901         myType = MainHyp;
1902         int index = myAvailableHypData[myDim][MainHyp].indexOf( aHypData );
1903         if ( index < 0 )
1904         {
1905           myType = AddHyp;
1906           index = myAvailableHypData[myDim][AddHyp].indexOf( aHypData );
1907         }
1908         if ( index >= 0 )
1909         {
1910           if ( myAverageSize > 0 )
1911           {
1912             SMESH::HypInitParams params = { 2, myAverageSize, aHypoSet->isQuadDominated() };
1913             SMESH::SMESH_Hypothesis_var hyp =
1914               getInitParamsHypothesis( aHypData->TypeName, aHypData->ServerLibName, & params );
1915
1916             QString hypName = GetUniqueName( getHypoNames(), aHypData->Label );
1917             SALOMEDS::SObject_wrap so =
1918               SMESHGUI::GetSMESHGen()->PublishInStudy( SALOMEDS::SObject::_nil(), hyp,
1919                                                        hypName.toUtf8().data() );
1920             onHypoCreated(2);
1921           }
1922           else
1923           {
1924             createHypothesis( myDim, myType, aHypoTypeName );
1925           }
1926         }
1927       }
1928     }
1929
1930   if ( maxDim > 0 )
1931     myDlg->setCurrentTab( maxDim );
1932
1933   return;
1934 }
1935
1936 //================================================================================
1937 /*!
1938  * \brief Creates mesh
1939  *  \param theMess - Output parameter intended for returning error message
1940  *  \param theEntryList - List of entries of published objects
1941  *  \retval bool  - TRUE if mesh is created, FALSE otherwise
1942  */
1943 //================================================================================
1944 bool SMESHGUI_MeshOp::createMesh( QString& theMess, QStringList& theEntryList )
1945 {
1946   theMess = "";
1947
1948   QStringList aList;
1949   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1950   if ( aList.isEmpty() )
1951   {
1952     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1953     if ( aSMESHGen->_is_nil() )
1954       return false;
1955
1956     SMESH::SMESH_Mesh_var aMesh= aSMESHGen->CreateEmptyMesh();
1957     if ( aMesh->_is_nil() )
1958       return false;
1959
1960     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMesh.in() );
1961     if ( aMeshSO ) {
1962       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
1963       theEntryList.append( aMeshSO->GetID().c_str() );
1964     }
1965     return true;
1966   }
1967   QString namePrefix;
1968   if ( aList.count() > 1 )
1969   {
1970     namePrefix = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
1971     int i = namePrefix.length() - 1;
1972     while ( i > 0 && namePrefix[i].isDigit() )
1973       --i;
1974     if ( i < namePrefix.length() - 1 )
1975       namePrefix.chop( namePrefix.length() - 1 - i );
1976     else
1977       namePrefix += "_";
1978   }
1979   QStringList::Iterator it = aList.begin();
1980   for ( int i = 0; it != aList.end(); it++, ++i )
1981   {
1982     QString aGeomEntry = *it;
1983     _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( aGeomEntry.toUtf8().data() );
1984     GEOM::GEOM_Object_var aGeomVar =
1985       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1986
1987     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1988     if ( aSMESHGen->_is_nil() )
1989       return false;
1990
1991     SUIT_OverrideCursor aWaitCursor;
1992
1993     // create mesh
1994     SMESH::SMESH_Mesh_var aMesh = aSMESHGen->CreateMesh( aGeomVar );
1995     if ( aMesh->_is_nil() )
1996       return false;
1997     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMesh.in() );
1998     if ( aMeshSO ) {
1999       theEntryList.append( aMeshSO->GetID().c_str() );
2000       if ( i > 0 ) setDefaultName( namePrefix );
2001       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
2002     }
2003
2004     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
2005     {
2006       if ( !isAccessibleDim( aDim )) continue;
2007
2008       // assign hypotheses
2009       for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(aDim); dlgType++ )
2010       {
2011         const int aHypIndex = currentHyp( aDim, dlgType );
2012         const int  aHypType = Min( dlgType, AddHyp );
2013         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
2014         {
2015           SMESH::SMESH_Hypothesis_var aHypVar =
2016             myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
2017           if ( !aHypVar->_is_nil() )
2018             SMESH::AddHypothesisOnMesh( aMesh, aHypVar );
2019         }
2020       }
2021       // find or create algorithm
2022       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
2023       if ( !anAlgoVar->_is_nil() )
2024         SMESH::AddHypothesisOnMesh( aMesh, anAlgoVar );
2025     }
2026
2027
2028     if ( myDlg->toCreateAllGroups() && !aGeomVar->_is_nil() )
2029     {
2030       // Create groups on all geom groups
2031
2032       GEOM::GEOM_Gen_var           geomGen = aGeomVar->GetGen();
2033       GEOM::GEOM_IShapesOperations_wrap op = geomGen->GetIShapesOperations();
2034       GEOM::ListOfGO_var        geomGroups = op->GetExistingSubObjects( aGeomVar,
2035                                                                         /*groupsOnly=*/false );
2036       SMESH::SMESH_GroupOnGeom_var meshGroup;
2037       for ( CORBA::ULong iG = 0; iG < geomGroups->length(); ++iG )
2038       {
2039         SMESH::ElementType elemType = SMESHGUI_GroupOnShapeOp::ElementType( geomGroups[ iG ] );
2040         if ( elemType == SMESH::ALL )
2041           continue;
2042         if ( elemType == SMESH::ELEM0D )
2043           elemType = SMESH::NODE;
2044
2045         CORBA::String_var name = geomGroups[ iG ]->GetName();
2046
2047         SALOMEDS::SObject_wrap groupSO = SMESH_Gen_i::GetSMESHGen()->ObjectToSObject( geomGroups[iG] );
2048         if (!groupSO->_is_nil())
2049           meshGroup = aMesh->CreateGroupFromGEOM( elemType, name, geomGroups[ iG ]);
2050         // if ( elemType != SMESH::NODE )
2051         //   meshGroup = aMesh->CreateGroupFromGEOM( SMESH::NODE, name, geomGroups[ iG ]);
2052       }
2053     }
2054
2055     createSubMeshOnInternalEdges( aMesh, aGeomVar );
2056
2057   }
2058
2059   return true;
2060 }
2061
2062 //================================================================================
2063 /*!
2064  * \brief Create sub-mesh
2065  * \param theMess - Output parameter intended for returning error message
2066  * \param theEntryList - List of entries of published objects
2067  * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
2068  */
2069 //================================================================================
2070 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess, QStringList& theEntryList )
2071 {
2072   theMess = "";
2073
2074   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
2075   if ( aSMESHGen->_is_nil() )
2076     return false;
2077
2078   // get mesh object
2079   SMESH::SMESH_Mesh_var aMesh =
2080     SMESH::EntryToInterface<SMESH::SMESH_Mesh>( myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh ));
2081   if ( aMesh->_is_nil() && myIsInvalidSubMesh )
2082   {
2083     SMESH::SMESH_subMesh_var aSMVar =
2084       SMESH::EntryToInterface<SMESH::SMESH_subMesh>( myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ));
2085     if ( !aSMVar->_is_nil() )
2086       aMesh = aSMVar->GetMesh();
2087   }
2088   if ( aMesh->_is_nil() )
2089     return false;
2090
2091   // GEOM shape of the main mesh
2092   GEOM::GEOM_Object_var mainGeom = aMesh->GetShapeToMesh();
2093
2094   // Name for the new sub-mesh
2095   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
2096
2097   // get geom object
2098   GEOM::GEOM_Object_var aGeomVar;
2099   QStringList aGEOMs;
2100   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
2101   if (aGEOMs.count() == 1)
2102   {
2103     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
2104     QString aGeomEntry = aGEOMs.first();
2105     _PTR(SObject) pGeom = SMESH::getStudy()->FindObjectID( aGeomEntry.toUtf8().data() );
2106     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
2107   }
2108   else if (aGEOMs.count() > 1)
2109   {
2110     // create a GEOM group
2111     GEOM::GEOM_Gen_var geomGen = mainGeom->GetGen();
2112     if ( !geomGen->_is_nil() ) {
2113       GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations();
2114       if ( !op->_is_nil() )
2115       {
2116         // check and add all selected GEOM objects: they must be
2117         // a sub-shapes of the main GEOM and must be of one type
2118         int iSubSh = 0;
2119         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
2120         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
2121         aSeq->length(aGEOMs.count());
2122         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
2123         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
2124           QString aSubGeomEntry = (*aSubShapesIter);
2125           _PTR(SObject) pSubGeom = SMESH::getStudy()->FindObjectID(aSubGeomEntry.toUtf8().data());
2126           GEOM::GEOM_Object_var aSubGeomVar =
2127             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
2128           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
2129           if (iSubSh == 0) {
2130             aGroupType = aSubShapeType;
2131           } else {
2132             if (aSubShapeType != aGroupType)
2133               aGroupType = TopAbs_SHAPE;
2134           }
2135           aSeq[iSubSh] = aSubGeomVar;
2136         }
2137         // create a group
2138         GEOM::GEOM_Object_wrap aGroupVar = op->CreateGroup(mainGeom, aGroupType);
2139         op->UnionList(aGroupVar, aSeq);
2140
2141         if (op->IsDone())
2142         {
2143           aGeomVar = GEOM::GEOM_Object::_duplicate( aGroupVar.in() );
2144
2145           // publish the GEOM group in study
2146           QString aNewGeomGroupName ("Auto_group_for_");
2147           aNewGeomGroupName += aName;
2148           SALOMEDS::SObject_wrap aNewGroupSO =
2149             geomGen->AddInStudy( aGeomVar,
2150                                  aNewGeomGroupName.toUtf8().data(), mainGeom);
2151         }
2152       }
2153     }
2154   }
2155   else {
2156   }
2157   if (aGeomVar->_is_nil())
2158     return false;
2159
2160   SUIT_OverrideCursor aWaitCursor;
2161
2162   QString aNameOrID = aName;
2163   if ( myIsInvalidSubMesh )
2164     // pass a sub-mesh entry to mesh->GetSubMesh() to replace the invalid sub-mesh
2165     // by a valid one in an existing SO
2166     aNameOrID = myDlg->selectedObject(SMESHGUI_MeshDlg::Obj);
2167
2168   // create sub-mesh
2169   SMESH::SMESH_subMesh_var aSubMeshVar = aMesh->GetSubMesh( aGeomVar, aNameOrID.toUtf8().data() );
2170   _PTR(SObject) aSubMeshSO = SMESH::FindSObject( aSubMeshVar.in() );
2171   if ( aSubMeshSO ) {
2172     SMESH::SetName( aSubMeshSO, aName.toUtf8().data() );
2173     theEntryList.append( aSubMeshSO->GetID().c_str() );
2174   }
2175
2176   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
2177   {
2178     if ( !isAccessibleDim( aDim )) continue;
2179
2180     // find or create algorithm
2181     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
2182     if ( !anAlgoVar->_is_nil() )
2183       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
2184     // assign hypotheses
2185     for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(aDim); dlgType++ )
2186     {
2187       const int aHypIndex = currentHyp( aDim, dlgType );
2188       const int  aHypType = Min( dlgType, AddHyp );
2189       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
2190       {
2191         SMESH::SMESH_Hypothesis_var aHypVar =
2192           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
2193         if ( !aHypVar->_is_nil() )
2194           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
2195       }
2196     }
2197   }
2198
2199   // deselect geometry: next submesh should be created on other sub-shape
2200   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
2201   selectObject( _PTR(SObject)() );
2202   selectionDone();
2203
2204   checkSubMeshConcurrency( aMesh, aSubMeshVar, /*askUser=*/true );
2205
2206   return true;
2207 }
2208
2209 //================================================================================
2210 /*!
2211  * \brief Return index of current hypothesis or algorithm
2212   * \param theDim - dimension of hypothesis or algorithm
2213   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
2214   * \retval int - index of current hypothesis or algorithm, -1 if None selected
2215  */
2216 //================================================================================
2217 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
2218 {
2219   if ( 0 <= theDim && theDim <= 3 )
2220     return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
2221   else
2222     return -1;
2223 }
2224
2225 //================================================================================
2226 /*!
2227  * \brief Checks if a hypothesis is selected
2228  */
2229 //================================================================================
2230
2231 bool SMESHGUI_MeshOp::isSelectedHyp( int theDim, int theHypType, int theIndex) const
2232 {
2233   if ( theIndex < 0 )
2234     return false;
2235
2236   if ( theHypType < AddHyp ) // only one hyp can be selected
2237     return currentHyp( theDim, theHypType ) == theIndex;
2238
2239   for ( int dlgHypType = AddHyp; dlgHypType < nbDlgHypTypes( theDim ); ++dlgHypType )
2240     if ( currentHyp( theDim, dlgHypType ) == theIndex )
2241       return true;
2242
2243   return false;
2244 }
2245
2246 //================================================================================
2247 /*!
2248  * \brief Returns nb of HypType's taking into account possible several
2249  *        selected additional hypotheses which are coded as additional HypType's.
2250  */
2251 //================================================================================
2252
2253 int SMESHGUI_MeshOp::nbDlgHypTypes( const int dim ) const
2254 {
2255   return NbHypTypes + myDlg->tab( dim )->nbAddHypTypes();
2256 }
2257
2258 //================================================================================
2259 /*!
2260  * \brief Returns true if hypotheses of given dim can be assigned
2261   * \param theDim - hypotheses dimension
2262   * \retval bool - result
2263  */
2264 //================================================================================
2265 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
2266 {
2267   return myDlg->isTabEnabled( theDim );
2268 }
2269
2270 //================================================================================
2271 /*!
2272  * \brief Sets current hypothesis or algorithms
2273   * \param theDim - dimension of hypothesis or algorithm
2274   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
2275   * \param theIndex - Index of hypothesis
2276   * \param updateHypsOnAlgoDeselection - to clear and disable hyps if algo deselected
2277  *
2278  * Gets current hypothesis or algorithms
2279  */
2280 //================================================================================
2281 void SMESHGUI_MeshOp::setCurrentHyp( const int  theDim,
2282                                      const int  theHypType,
2283                                      const int  theIndex,
2284                                      const bool updateHypsOnAlgoDeselection)
2285 {
2286   myIgnoreAlgoSelection = true;
2287   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
2288   myIgnoreAlgoSelection = false;
2289
2290   if ( updateHypsOnAlgoDeselection && theHypType == Algo && theIndex < 0 )
2291   {
2292     const QStringList noHyps;
2293     myDlg->tab( theDim )->setAvailableHyps( MainHyp, noHyps );
2294     myDlg->tab( theDim )->setExistingHyps ( MainHyp, noHyps );
2295     myDlg->tab( theDim )->setAvailableHyps( AddHyp,  noHyps );
2296     myDlg->tab( theDim )->setExistingHyps ( AddHyp,  noHyps );
2297   }
2298 }
2299
2300 //================================================================================
2301 /*!
2302  * \brief Generates default and sets mesh/submesh name
2303  *
2304  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
2305  */
2306 //================================================================================
2307 void SMESHGUI_MeshOp::setDefaultName( const QString& thePrefix ) const
2308 {
2309   QString aResName;
2310
2311   _PTR(Study) aStudy = SMESH::getStudy();
2312   int i = 1;
2313
2314   QString aPrefix = thePrefix;
2315   if ( aPrefix.isEmpty() )
2316     aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
2317
2318   _PTR(SObject) anObj;
2319   do
2320   {
2321     aResName = aPrefix + QString::number( i++ );
2322     anObj = aStudy->FindObject( aResName.toUtf8().data() );
2323   }
2324   while ( anObj );
2325
2326   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
2327     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
2328   aControl->setText( aResName );
2329 }
2330
2331 //================================================================================
2332 /*!
2333  * \brief Gets algorithm or creates it if necessary
2334   * \param theDim - specifies dimension of returned hypotheses/algorifms
2335   * \retval SMESH::SMESH_Hypothesis_var - algorithm
2336  *
2337  * Gets algorithm or creates it if necessary
2338  */
2339 //================================================================================
2340 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
2341 {
2342   SMESH::SMESH_Hypothesis_var anAlgoVar;
2343
2344   // get type of the selected algo
2345   int aHypIndex = currentHyp( theDim, Algo );
2346   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
2347   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
2348     return anAlgoVar;
2349   QString aHypName = dataList[ aHypIndex ]->TypeName;
2350
2351   // get existing algorithms
2352   _PTR(SObject) pComp = SMESH::getStudy()->FindComponent("SMESH");
2353   QStringList tmp;
2354   existingHyps( theDim, Algo, pComp, tmp, myExistingHyps[ theDim ][ Algo ]);
2355
2356   // look for an existing algo of such a type
2357   THypList& aHypVarList = myExistingHyps[ theDim ][ Algo ];
2358   THypList::iterator anIter = aHypVarList.begin();
2359   for ( ; anIter != aHypVarList.end(); anIter++)
2360   {
2361     SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
2362     if ( !aHypVar->_is_nil() && aHypName == SMESH::toQStr( aHypVar->GetName() ))
2363     {
2364       anAlgoVar = aHypVar;
2365       break;
2366     }
2367   }
2368
2369   if (anAlgoVar->_is_nil())
2370   {
2371     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
2372     if (aHypData)
2373     {
2374       QString aClientLibName = aHypData->ClientLibName;
2375       if ( aClientLibName.isEmpty() )
2376       {
2377         // Call hypothesis creation server method (without GUI)
2378         SMESH::SMESH_Hypothesis_var aHyp =
2379           SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
2380         aHyp.out();
2381       }
2382       else
2383       {
2384         // Get hypotheses creator client (GUI)
2385         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
2386
2387         // Create algorithm
2388         if (aCreator)
2389           aCreator->create( true, aHypName, myDlg, 0, QString::null );
2390         else {
2391           SMESH::SMESH_Hypothesis_var aHyp =
2392             SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
2393           aHyp.out();
2394         }
2395         delete aCreator;
2396       }
2397       QStringList tmpList;
2398       _PTR(SComponent) aFather = SMESH::getStudy()->FindComponent( "SMESH" );
2399       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
2400     }
2401
2402     THypList& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
2403     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
2404     {
2405       SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
2406       if ( !aHypVar->_is_nil() && aHypName == SMESH::toQStr( aHypVar->GetName() ))
2407       {
2408         anAlgoVar = aHypVar;
2409         break;
2410       }
2411     }
2412   }
2413
2414   return anAlgoVar._retn();
2415 }
2416
2417 //================================================================================
2418 /*!
2419  * \brief Reads parameters of an edited mesh/sub-mesh and assigns them to the dialog
2420  *
2421  * Called when mesh is edited only.
2422  */
2423 //================================================================================
2424 void SMESHGUI_MeshOp::readMesh()
2425 {
2426   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2427   _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
2428   if ( !pObj )
2429     return;
2430
2431   if ( myIsOnGeometry )
2432   {
2433     // Set name of mesh if current object is sub-mesh
2434     SMESH::SMESH_subMesh_var subMesh = SMESH::SObjectToInterface< SMESH::SMESH_subMesh >( pObj );
2435     if ( !subMesh->_is_nil() )
2436     {
2437       SMESH::SMESH_Mesh_var aMesh =  subMesh->GetFather();
2438       if ( !aMesh->_is_nil() )
2439       {
2440         _PTR(SObject) aMeshSO = SMESH::FindSObject( aMesh );
2441         QString     aMeshName = name( aMeshSO );
2442         myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
2443       }
2444       myHasConcurrentSubBefore = checkSubMeshConcurrency( aMesh, subMesh );
2445     }
2446
2447     if ( !myIsInvalidSubMesh )
2448     {
2449       // Set name of geometry object
2450       CORBA::String_var name = SMESH::GetGeomName( pObj );
2451       if ( name.in() )
2452         myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, name.in() );
2453     }
2454   }
2455
2456   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
2457   QStringList anExisting, anAvailable;
2458   bool algoFound = false;
2459   const int dim1st = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_2D;
2460   for ( int dim = dim1st; dim <= SMESH::DIM_3D; ++dim )
2461   {
2462     // get algorithm
2463     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
2464     // find algo index among available ones
2465     int aHypIndex = -1;
2466     anAvailable.clear();
2467     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
2468     {
2469       SMESH::SMESH_Hypothesis_var hyp = myObjHyps[ dim ][ Algo ].first().first;
2470       HypothesisData* algoData = SMESH::GetHypothesisData( SMESH::toQStr( hyp->GetName() ));
2471       availableHyps( dim, Algo, anAvailable, myAvailableHypData[ dim ][ Algo ] );
2472       aHypIndex = myAvailableHypData[ dim ][ Algo ].indexOf( algoData );
2473       //       if ( aHypIndex < 0 && algoData ) {
2474       //         // assigned algo is incompatible with other algorithms
2475       //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
2476       //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
2477       //       }
2478       algoFound = ( aHypIndex > -1 );
2479     }
2480     myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
2481     setCurrentHyp( dim, Algo, aHypIndex );
2482     // set existing and available hypothesis according to the selected algo
2483     if ( aHypIndex > -1 || !algoFound )
2484       onAlgoSelected( aHypIndex, dim );
2485   }
2486
2487   // get hypotheses
2488   bool hypWithoutAlgo = false;
2489   for ( int dim = dim1st; dim <= SMESH::DIM_3D; ++dim )
2490   {
2491     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2492     {
2493       // get hypotheses
2494       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2495       if ( myObjHyps[ dim ][ hypType ].count() == 0 ) {
2496         setCurrentHyp( dim, hypType, -1 );
2497       }
2498       for ( int i = 0, nb = myObjHyps[ dim ][ hypType ].count(); i < nb; ++i )
2499       {
2500         // find index of required hypothesis among existing ones for this dimension and type
2501         int aHypIndex = find( myObjHyps[ dim ][ hypType ][ i ].first,
2502                               myExistingHyps[ dim ][ hypType ] );
2503         if ( aHypIndex < 0 ) {
2504           // assigned hypothesis is incompatible with the algorithm
2505           if ( currentHyp( dim, Algo ) < 0 )
2506           { // none algo selected; it is edition for sure, of submesh maybe
2507             hypWithoutAlgo = true;
2508             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ][ i ] );
2509             anExisting.push_back( myObjHyps[ dim ][ hypType ][ i ].second );
2510             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
2511             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
2512           }
2513         }
2514         setCurrentHyp( dim, hypType + i, aHypIndex );
2515
2516         if ( hypType == MainHyp ) break; // only one main hyp allowed
2517       }
2518     }
2519   }
2520   // make available other hyps of same type as one without algo
2521   if ( hypWithoutAlgo )
2522     onAlgoSelected( currentHyp( /*dim=*/0, Algo ), /*dim=*/0 );
2523 }
2524
2525 //================================================================================
2526 /*!
2527  * \brief Gets name of object
2528  * \param theSO - SObject
2529  * \retval QString - name of object
2530  *
2531  * Gets name of object
2532  */
2533 //================================================================================
2534 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
2535 {
2536   QString aResName;
2537   if ( theSO )
2538   {
2539     _PTR(GenericAttribute) anAttr;
2540     _PTR(AttributeName)    aNameAttr;
2541     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
2542     {
2543       aNameAttr = anAttr;
2544       aResName = aNameAttr->Value().c_str();
2545     }
2546   }
2547   return aResName;
2548 }
2549
2550 //================================================================================
2551 /*!
2552  * \brief Finds hypothesis in input list
2553   * \param theHyp - hypothesis to be found
2554   * \param theHypList - input list of hypotheses
2555   * \retval int - index of hypothesis or -1 if it is not found
2556  *
2557  * Finds position of hypothesis in input list
2558  */
2559 //================================================================================
2560 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
2561                            const THypList& theHypList ) const
2562 {
2563   int aRes = -1;
2564   if ( !theHyp->_is_nil() )
2565   {
2566     int i = 0;
2567     THypList::const_iterator anIter = theHypList.begin();
2568     for ( ; anIter != theHypList.end(); ++ anIter)
2569     {
2570       if ( theHyp->_is_equivalent( (*anIter).first ) )
2571       {
2572         aRes = i;
2573         break;
2574       }
2575       i++;
2576     }
2577   }
2578   return aRes;
2579 }
2580
2581 //================================================================================
2582 /*!
2583  * \brief Edits mesh or sub-mesh
2584   * \param theMess      - Output parameter intended for returning error message
2585  *  \param theEntryList - List of entries of published objects
2586   * \retval bool        - TRUE if mesh is edited successfully, FALSE otherwise
2587  *
2588  * Assigns new name hypotheses and algorithms to the mesh or sub-mesh
2589  */
2590 //================================================================================
2591 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess, QStringList& theEntryList)
2592 {
2593   theMess = "";
2594
2595   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
2596   if ( aSMESHGen->_is_nil() )
2597     return false;
2598
2599   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2600   _PTR(SObject) pObj = SMESH::getStudy()->FindObjectID( anObjEntry.toUtf8().data() );
2601   if ( !pObj )
2602     return false;
2603
2604   SUIT_OverrideCursor aWaitCursor;
2605
2606   // Set new name
2607   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
2608   SMESH::SetName( pObj, aName );
2609   int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_2D;
2610
2611   // First, remove old algos in order to avoid messages on algorithm hiding
2612   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2613   {
2614     if ( /*isAccessibleDim( dim ) &&*/ myObjHyps[ dim ][ Algo ].count() > 0 )
2615     {
2616       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first().first;
2617       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2618       if ( anAlgoVar->_is_nil() || // no new algo selected or
2619            SMESH::toQStr(anOldAlgo->GetName()) != SMESH::toQStr(anAlgoVar->GetName())) // algo change
2620       {
2621         // remove old algorithm
2622         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first().first );
2623         myObjHyps[ dim ][ Algo ].clear();
2624       }
2625     }
2626   }
2627
2628   SALOMEDS_SObject* aSObject = _CAST(SObject, pObj);
2629   CORBA::Object_var anObject = aSObject->GetObject();
2630   SMESH::SMESH_Mesh_var          aMesh = SMESH::SMESH_Mesh::_narrow( anObject );
2631   SMESH::SMESH_subMesh_var aSubMeshVar = SMESH::SMESH_subMesh::_narrow( anObject );
2632   bool isMesh = !aMesh->_is_nil();
2633   if ( !isMesh && !aSubMeshVar->_is_nil() )
2634     aMesh = aSubMeshVar->GetFather();
2635
2636   _PTR(SObject) aMeshSO = SMESH::FindSObject(aMesh.in());
2637   if (aMeshSO) {
2638     if (aSubMeshVar->_is_nil()) SMESH::SetName(aMeshSO, myDlg->objectText(SMESHGUI_MeshDlg::Obj));
2639     theEntryList.append(aMeshSO->GetID().c_str());
2640   }
2641
2642   // Assign new algorithms and hypotheses
2643   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2644   {
2645     //if ( !isAccessibleDim( dim )) continue;
2646
2647     // find or create algorithm
2648     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2649
2650     // assign new algorithm
2651     if ( !anAlgoVar->_is_nil() && // some algo selected and
2652          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
2653     {
2654       if ( isMesh )
2655         SMESH::AddHypothesisOnMesh( aMesh, anAlgoVar );
2656       else if ( !aSubMeshVar->_is_nil() )
2657         SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
2658
2659       myObjHyps[ dim ][ Algo ].append( THypItem( anAlgoVar, aName) );
2660     }
2661
2662     // remove deselected hypotheses
2663     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2664     {
2665       for ( int i = 0, nb = myObjHyps[ dim ][ hypType ].count(); i < nb; ++i )
2666       {
2667         SMESH::SMESH_Hypothesis_var hyp = myObjHyps[ dim ][ hypType ][ i ].first;
2668         int hypIndex = this->find( hyp, myExistingHyps[ dim ][ hypType ]);
2669         if ( !isSelectedHyp( dim, hypType, hypIndex ) && !hyp->_is_nil() )
2670         {
2671           SMESH::RemoveHypothesisOrAlgorithmOnMesh( pObj, hyp );
2672         }
2673       }
2674     }
2675     // assign newly selected hypotheses
2676     for ( int dlgType = MainHyp; dlgType < nbDlgHypTypes(dim); dlgType++ )
2677     {
2678       const int curIndex = currentHyp( dim, dlgType );
2679       const int  hypType = Min( dlgType, AddHyp );
2680       if ( curIndex >= 0 && curIndex < myExistingHyps[ dim ][ hypType ].count() )
2681       {
2682         SMESH::SMESH_Hypothesis_var hyp = myExistingHyps[ dim ][ hypType ][ curIndex ].first;
2683
2684         bool isAssigned = ( this->find( hyp, myObjHyps[ dim ][ hypType ]) >= 0 );
2685         if ( !isAssigned )
2686         {
2687           if ( isMesh )
2688             SMESH::AddHypothesisOnMesh (aMesh, hyp );
2689           else if ( !aSubMeshVar->_is_nil() )
2690             SMESH::AddHypothesisOnSubMesh ( aSubMeshVar, hyp );
2691         }
2692       }
2693       // reread all hypotheses of mesh
2694       QStringList anExisting;
2695       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2696     }
2697   }
2698
2699   if ( aSubMeshVar->_is_nil() )
2700   {
2701     GEOM::GEOM_Object_var mainGeom = aMesh->GetShapeToMesh();
2702     createSubMeshOnInternalEdges( aMesh, mainGeom );
2703   }
2704
2705   myHasConcurrentSubBefore =
2706     checkSubMeshConcurrency( aMesh, aSubMeshVar, /*askUser=*/!myHasConcurrentSubBefore );
2707
2708   return true;
2709 }
2710
2711 //================================================================================
2712 /*!
2713  * \brief Checks if a concurrent sub-meshes appear as result of sub-mesh
2714  *        creation/edition and, if (askUser) , proposes the uses to set up a desired
2715  *        order of sub-mesh computation.
2716  *        Returns \c true if a sub-mesh concurrency detected.
2717  */
2718 //================================================================================
2719
2720 bool SMESHGUI_MeshOp::checkSubMeshConcurrency(SMESH::SMESH_Mesh_ptr    mesh,
2721                                               SMESH::SMESH_subMesh_ptr submesh,
2722                                               bool                     askUser)
2723 {
2724   if ( CORBA::is_nil( mesh ) || CORBA::is_nil( submesh ))
2725     return false;
2726
2727   bool isNewConcurrent = mesh->IsUnorderedSubMesh( submesh->GetId() );
2728   if ( isNewConcurrent && askUser )
2729   {
2730     int butID = SUIT_MessageBox::warning( myDlg->parentWidget(), tr( "SMESH_WARNING" ),
2731                                           tr("CONCURRENT_SUBMESH_APPEARS"),
2732                                           tr("SMESH_BUT_YES"), tr("SMESH_BUT_NO"));
2733     if ( butID == 0 )
2734     {
2735       _PTR(SObject) meshSO = SMESH::FindSObject( mesh );
2736       LightApp_SelectionMgr* aSelectionMgr = selectionMgr();
2737       if ( meshSO && aSelectionMgr )
2738       {
2739         myDlg->setEnabled( false ); // disactivate selection
2740         selectionMgr()->clearFilters();
2741         selectObject( meshSO );
2742         SMESHGUI::GetSMESHGUI()->OnGUIEvent( SMESHOp::OpMeshOrder ); // MESH_ORDER
2743         qApp->processEvents();
2744
2745         myDlg->setEnabled( true );
2746         int obj = myDlg->getActiveObject();
2747         onActivateObject( obj ); // restore filter
2748         if ( !myToCreate )
2749         {
2750           selectObject( SMESH::FindSObject( submesh ));
2751           selectionDone();
2752         }
2753       }
2754     }
2755   }
2756
2757   return isNewConcurrent;
2758 }
2759
2760 //================================================================================
2761 /*!
2762  * \brief Verifies whether given operator is valid for this one
2763  * \param theOtherOp - other operation
2764  * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
2765  *
2766  * method redefined from base class verifies whether given operator is valid for
2767  * this one (i.e. can be started "above" this operator). In current implementation method
2768  * returns false if theOtherOp operation is not intended for deleting objects or mesh
2769  * elements.
2770  */
2771 //================================================================================
2772 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
2773 {
2774   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
2775 }
2776
2777 //================================================================================
2778 /*!
2779  * \brief SLOT. Is called when the user selects a way of geometry selection
2780  * \param theByMesh - true if the user wants to find geometry by mesh element
2781  */
2782 //================================================================================
2783 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
2784 {
2785   if ( theByMesh ) {
2786     if ( !myShapeByMeshOp ) {
2787       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
2788       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2789               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2790       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2791               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2792     }
2793     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2794     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
2795     if ( _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( aMeshEntry.toUtf8().data() )) {
2796       SMESH::SMESH_Mesh_var aMesh =
2797         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
2798       if ( !aMesh->_is_nil() ) {
2799         myDlg->hide(); // stop processing selection
2800         myShapeByMeshOp->setModule( getSMESHGUI() );
2801         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2802         myShapeByMeshOp->SetMesh( aMesh );
2803         myShapeByMeshOp->start();
2804       }
2805     }
2806   }
2807 }
2808
2809 //================================================================================
2810 /*!
2811  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2812  */
2813 //================================================================================
2814 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
2815 {
2816   if ( myShapeByMeshOp == op ) {
2817     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
2818     myDlg->show();
2819     // Select a found geometry object
2820     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2821     if ( !aGeomVar->_is_nil() )
2822     {
2823       QString ID = SMESH::toQStr( aGeomVar->GetStudyEntry() );
2824       if ( _PTR(SObject) aGeomSO = SMESH::getStudy()->FindObjectID( ID.toUtf8().data() )) {
2825         selectObject( aGeomSO );
2826         selectionDone();
2827       }
2828     }
2829   }
2830 }
2831
2832 //================================================================================
2833 /*!
2834  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2835  */
2836 //================================================================================
2837 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
2838 {
2839   if ( myShapeByMeshOp == op && myDlg ) {
2840     myDlg->show();
2841   }
2842 }
2843
2844 //================================================================================
2845 /*!
2846  * \brief Selects a SObject
2847  * \param theSObj - the SObject to select
2848  */
2849 //================================================================================
2850 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
2851 {
2852   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
2853     SALOME_ListIO anIOList;
2854     if ( theSObj ) {
2855       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2856         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
2857       anIOList.Append( anIO );
2858     }
2859     sm->setSelectedObjects( anIOList, false );
2860   }
2861 }
2862 //================================================================================
2863 /*!
2864  * \brief Update available list of mesh types
2865  */
2866 //================================================================================
2867 void SMESHGUI_MeshOp::updateMeshTypeList()
2868 {
2869   QStringList meshTypes;
2870   meshTypes.append( tr( "MT_ANY" ) );
2871   if ( myMaxShapeDim >= 2 || myMaxShapeDim == -1 )
2872   {
2873     meshTypes.append( tr( "MT_TRIANGULAR" ) );
2874     meshTypes.append( tr( "MT_QUADRILATERAL" ) );
2875   }
2876   if ( myMaxShapeDim == 3 || myMaxShapeDim == -1 )
2877   {
2878     meshTypes.append( tr( "MT_TETRAHEDRAL" ) );
2879     meshTypes.append( tr( "MT_HEXAHEDRAL" ) );
2880   }
2881
2882   myDlg->setAvailableMeshType( meshTypes );
2883 }
2884
2885 //================================================================================
2886 /*!
2887  * \brief Update available list of hypothesis sets
2888  */
2889 //================================================================================
2890 void SMESHGUI_MeshOp::updateHypoSets()
2891 {
2892   // get available algorithms taking into account geometry and mesh type
2893   QStringList algosAvailable;
2894   if ( myDlg->currentMeshType() != MT_ANY )
2895     for ( int dim = 0; dim <= 3; ++dim )
2896       availableHyps( dim, Algo, algosAvailable, myAvailableHypData[ dim ][ Algo ] );
2897
2898   THypDataList anAvailableAlgsData;
2899   QStringList aHypothesesSetsList = SMESH::GetHypothesesSets( myMaxShapeDim );
2900   QStringList aFilteredHypothesesSetsList;
2901   QStringList::const_iterator inHypoSetName = aHypothesesSetsList.begin();
2902   for ( ; inHypoSetName != aHypothesesSetsList.end(); ++inHypoSetName )
2903   {
2904     HypothesesSet* currentHypoSet = SMESH::GetHypothesesSet( *inHypoSetName );
2905     HypothesesSet::SetType sType;
2906     for ( sType = HypothesesSet::MAIN; sType <= HypothesesSet::ALT; SMESHUtils::Increment( sType ))
2907     {
2908       bool isAvailable = false;
2909       for ( currentHypoSet->init( true, sType ); currentHypoSet->more(); currentHypoSet->next() )
2910       {
2911         isAvailable = false;
2912         if ( HypothesisData* algoDataIn = SMESH::GetHypothesisData( currentHypoSet->current() )) {
2913           for ( int dim = SMESH::DIM_0D; dim <= myMaxShapeDim; dim++) {
2914             int aCurrentAvailableAlgo = myAvailableHypData[dim][Algo].indexOf( algoDataIn );
2915             if ( aCurrentAvailableAlgo > -1 ) {
2916               isAvailable = true;
2917               break;
2918             }
2919           }
2920           if ( !isAvailable )
2921             break;
2922         }
2923       }
2924       currentHypoSet->setAlgoAvailable( sType, isAvailable );
2925     }
2926     if ( currentHypoSet->hasAlgo( HypothesesSet::MAIN ) &&
2927          currentHypoSet->hasAlgo( HypothesesSet::ALT ))
2928     {
2929       HypothesesSet::SetType setType = HypothesesSet::getPreferredHypType();
2930       if ( !currentHypoSet->getAlgoAvailable( setType ))
2931         continue; // not add if a preferred type not available currently
2932     }
2933
2934     if ( currentHypoSet->getAlgoAvailable( HypothesesSet::MAIN ) ||
2935          currentHypoSet->getAlgoAvailable( HypothesesSet::ALT ))
2936     {
2937       aFilteredHypothesesSetsList.append( *inHypoSetName );
2938     }
2939   }
2940   myDlg->setHypoSets( aFilteredHypothesesSetsList );
2941 }
2942
2943 //================================================================================
2944 /*!
2945  * \brief SLOT. Is called when the user select type of mesh
2946  * \param theTabIndex - Index of current active tab
2947  * \param theIndex - Index of current type of mesh
2948  */
2949 //================================================================================
2950 void SMESHGUI_MeshOp::onAlgoSetByMeshType( const int /*theTabIndex*/, const int /*theIndex*/ )
2951 {
2952   setFilteredAlgoData();
2953 }