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