Salome HOME
Distributed Geom, Smesh. Modifications by Anthony GEAY.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /**
21 *  SMESH SMESHGUI
22 *
23 *  Copyright (C) 2005  CEA/DEN, EDF R&D
24 *
25 *
26 *
27 *  File   : SMESHGUI_MeshOp.h
28 *  Author : Sergey LITONIN
29 *  Module : SMESHGUI
30 */
31
32 #include "SMESHGUI_MeshOp.h"
33 #include "SMESHGUI_MeshDlg.h"
34 #include "SMESHGUI_ShapeByMeshDlg.h"
35 #include "SMESH_TypeFilter.hxx"
36 #include "SMESHGUI.h"
37
38 #include "SMESHGUI_HypothesesUtils.h"
39 #include "SMESHGUI_Hypotheses.h"
40 #include "SMESHGUI_Utils.h"
41 #include "SMESHGUI_GEOMGenUtils.h"
42 #include "SMESHGUI_VTKUtils.h"
43
44 #include "SMESH_TypeFilter.hxx"
45 #include "SMESH_NumberFilter.hxx"
46
47 #include "GEOM_SelectionFilter.h"
48 #include "GEOMBase.h"
49 #include "GeometryGUI.h"
50
51 #include "SalomeApp_Tools.h"
52 #include "SalomeApp_Application.h"
53 #include "SALOMEDSClient_Study.hxx"
54 #include "SALOMEDSClient_AttributeIOR.hxx"
55 #include "SALOMEDSClient_AttributeName.hxx"
56 #include "SALOMEDS_SComponent.hxx"
57 #include "SALOMEDS_SObject.hxx"
58
59 #include "LightApp_SelectionMgr.h"
60 #include "LightApp_UpdateFlags.h"
61 #include "SUIT_MessageBox.h"
62 #include "SUIT_Desktop.h"
63 #include "SUIT_OverrideCursor.h"
64 #include "SALOME_InteractiveObject.hxx"
65 #include "SALOME_ListIO.hxx"
66
67 #include "utilities.h"
68
69 #include <qstringlist.h>
70 #include <qlineedit.h>
71
72 #include <TopoDS_Shape.hxx>
73 #include <TopExp_Explorer.hxx>
74
75 enum { GLOBAL_ALGO_TAG        =3,
76        GLOBAL_HYPO_TAG        =2,
77        LOCAL_ALGO_TAG         =2,
78        LOCAL_HYPO_TAG         =1,
79        SUBMESH_ON_VERTEX_TAG  =4,
80        SUBMESH_ON_EDGE_TAG    =5,
81        SUBMESH_ON_WIRE_TAG    =6,
82        SUBMESH_ON_FACE_TAG    =7,
83        SUBMESH_ON_SHELL_TAG   =8,
84        SUBMESH_ON_SOLID_TAG   =9,
85        SUBMESH_ON_COMPOUND_TAG=10 };
86
87 //================================================================================
88 /*!
89  * \brief Constructor
90   * \param theToCreate - if this parameter is true then operation is used for creation,
91   * for editing otherwise
92  *
93  * Initialize operation
94 */
95 //================================================================================
96 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
97 : SMESHGUI_SelectionOp(),
98   myToCreate( theToCreate ),
99   myIsMesh( theIsMesh ),
100   myDlg( 0 ),
101   myShapeByMeshOp( 0 )
102 {
103   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
104     GeometryGUI::InitGeomGen();
105 }
106
107 //================================================================================
108 /*!
109  * \brief Destructor
110 */
111 //================================================================================
112 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
113 {
114   if ( myDlg )
115     delete myDlg;
116 }
117
118 //================================================================================
119 /*!
120  * \brief Gets dialog of this operation
121   * \retval LightApp_Dialog* - pointer to dialog of this operation
122 */
123 //================================================================================
124 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
125 {
126   return myDlg;
127 }
128
129 //================================================================================
130 /*!
131  * \brief Creates or edits mesh
132   * \retval bool - TRUE if operation is performed successfully, FALSE otherwise
133  *
134  * Virtual slot redefined from the base class called when "Apply" button is clicked
135  * creates or edits mesh
136  */
137 //================================================================================
138 bool SMESHGUI_MeshOp::onApply()
139 {
140   if( isStudyLocked() )
141     return false;
142
143   QString aMess;
144   if ( !isValid( aMess ) )
145   {
146     dlg()->show();
147     if ( aMess != "" )
148       SUIT_MessageBox::warn1( myDlg,
149         tr( "SMESH_WRN_WARNING" ), aMess, tr( "SMESH_BUT_OK" ) );
150     return false;
151   }
152
153   bool aResult = false;
154   aMess = "";
155   try
156   {
157     if ( myToCreate && myIsMesh )
158       aResult = createMesh( aMess );
159     if ( myToCreate && !myIsMesh )
160       aResult = createSubMesh( aMess );
161     else if ( !myToCreate )
162       aResult = editMeshOrSubMesh( aMess );
163     if ( aResult )
164       update( UF_ObjBrowser | UF_Model );
165   }
166   catch ( const SALOME::SALOME_Exception& S_ex )
167   {
168     SalomeApp_Tools::QtCatchCorbaException( S_ex );
169     aResult = false;
170   }
171   catch ( ... )
172   {
173     aResult = false;
174   }
175
176   if ( aResult )
177   {
178     if ( myToCreate )
179       setDefaultName();
180   }
181   else
182   {
183     if ( aMess == "" )
184       aMess = tr( "SMESH_OPERATION_FAILED" );
185     SUIT_MessageBox::warn1( myDlg,
186       tr( "SMESH_ERROR" ), aMess, tr( "SMESH_BUT_OK" ) );
187   }
188
189   return aResult;
190 }
191
192 //================================================================================
193 /*!
194  * \brief Creates dialog if necessary and shows it
195  *
196  * Virtual method redefined from base class called when operation is started creates
197  * dialog if necessary and shows it, activates selection
198  */
199 //================================================================================
200 void SMESHGUI_MeshOp::startOperation()
201 {
202   if( !myDlg )
203   {
204     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
205     for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
206     {
207       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
208               this, SLOT( onCreateHyp( const int, const int ) ) );
209       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
210               this, SLOT( onEditHyp( const int, const int ) ) );
211       connect( myDlg->tab( i ), SIGNAL( selectAlgo( const int ) ),
212               this, SLOT( onAlgoSelected( const int ) ) );
213     }
214     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
215     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
216
217     if ( myToCreate )
218       if ( myIsMesh ) myHelpFileName = "/files/constructing_meshes.htm";
219       else myHelpFileName = "/files/constructing_submeshes.htm";
220     else myHelpFileName = "files/reassigning_hypotheses_and_algorithms.htm";
221   }
222   SMESHGUI_SelectionOp::startOperation();
223
224   // iterate through dimensions and get available algoritms, set them to the dialog
225   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
226   for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
227   {
228     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
229     QStringList hypList;
230     // clear available hypotheses
231     aTab->setAvailableHyps( MainHyp, hypList );
232     aTab->setAvailableHyps( AddHyp, hypList );
233     aTab->setExistingHyps( MainHyp, hypList );
234     aTab->setExistingHyps( AddHyp, hypList );
235     myExistingHyps[ i ][ MainHyp ].clear();
236     myExistingHyps[ i ][ AddHyp ].clear();
237     // set algos
238     availableHyps( i, Algo, hypList, myAvailableHypData[i][Algo] );
239     aTab->setAvailableHyps( Algo, hypList );
240   }
241   if ( myToCreate )
242   {
243     setDefaultName();
244     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
245   }
246   else
247     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
248
249   myDlg->setHypoSets( SMESH::GetHypothesesSets() );
250
251   myDlg->setCurrentTab( SMESH::DIM_3D );
252   myDlg->show();
253
254   selectionDone();
255
256   myIgnoreAlgoSelection = false;
257 }
258
259 //================================================================================
260 /*!
261  * \brief Creates selection filter
262   * \param theId - identifier of current selection widget
263   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
264  *
265  * Creates selection filter in accordance with identifier of current selection widget
266  */
267 //================================================================================
268 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
269 {
270   if ( theId == SMESHGUI_MeshDlg::Geom )
271   {
272 //     TColStd_MapOfInteger allTypesMap;
273 //     for ( int i = 0; i < 10; i++ )
274 //       allTypesMap.Add( i );
275 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
276     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
277   }
278   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
279     return new SMESH_TypeFilter( MESHorSUBMESH );
280   else if ( theId == SMESHGUI_MeshDlg::Mesh )
281     return new SMESH_TypeFilter( MESH );
282   else
283     return 0;
284 }
285
286 //================================================================================
287 /*!
288  * \brief check if selected shape is a subshape of the shape to mesh
289   * \retval bool - check result
290  */
291 //================================================================================
292
293 bool SMESHGUI_MeshOp::isSubshapeOk() const
294 {
295   if ( !myToCreate || myIsMesh ) // not submesh creation
296     return false;
297
298   // mesh
299   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
300   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
301   if (!pMesh) return false;
302
303   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
304   if (mesh->_is_nil()) return false;
305
306   // main shape of the mesh
307   GEOM::GEOM_Object_var mainGeom = mesh->GetShapeToMesh();
308   if (mainGeom->_is_nil()) return false;
309
310   // geometry
311   QStringList aGEOMs;
312   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
313
314   if (aGEOMs.count() > 0) {
315     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
316     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
317     if (geomGen->_is_nil() || !aStudy) return false;
318
319     GEOM::GEOM_IGroupOperations_var op =
320         geomGen->GetIGroupOperations(aStudy->StudyId());
321     if (op->_is_nil()) return false;
322
323     // check all selected shapes
324     QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
325     for (; aSubShapesIter != aGEOMs.end(); aSubShapesIter++) {
326       QString aSubGeomEntry = (*aSubShapesIter);
327       _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.latin1());
328       if (!pSubGeom) return false;
329
330       GEOM::GEOM_Object_var aSubGeomVar =
331         GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
332       if (aSubGeomVar->_is_nil()) return false;
333
334       // skl for NPAL14695 - implementation of searching of mainObj
335       GEOM::GEOM_Object_var mainObj = op->GetMainShape(aSubGeomVar);
336       //if (mainObj->_is_nil() ||
337       //    string(mainObj->GetEntry()) != string(mainGeom->GetEntry())) return false;
338       while(1) {
339         if(mainObj->_is_nil())
340           return false;
341         if( string(mainObj->GetEntry()) == string(mainGeom->GetEntry()) )
342           return true;
343         mainObj = op->GetMainShape(mainObj);
344       }
345     }
346     //return true;
347   }
348
349   return false;
350 }
351
352 //================================================================================
353 /*!
354  * \brief find an existing submesh by the selected shape
355   * \retval _PTR(SObject) - the found submesh SObject
356  */
357 //================================================================================
358
359 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
360 {
361   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
362   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
363   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
364   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
365   if ( pMesh && pGeom ) {
366     GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
367     if ( !geom->_is_nil() ) {
368       int tag = -1;
369       switch ( geom->GetShapeType() ) {
370       case GEOM::VERTEX:   tag = SUBMESH_ON_VERTEX_TAG  ; break;
371       case GEOM::EDGE:     tag = SUBMESH_ON_EDGE_TAG    ; break;
372       case GEOM::WIRE:     tag = SUBMESH_ON_WIRE_TAG    ; break;
373       case GEOM::FACE:     tag = SUBMESH_ON_FACE_TAG    ; break;
374       case GEOM::SHELL:    tag = SUBMESH_ON_SHELL_TAG   ; break;
375       case GEOM::SOLID:    tag = SUBMESH_ON_SOLID_TAG   ; break;
376       case GEOM::COMPOUND: tag = SUBMESH_ON_COMPOUND_TAG; break;
377       default:;
378       }
379       _PTR(GenericAttribute) anAttr;
380       _PTR(SObject) aSubmeshRoot;
381       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
382       if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
383       {
384         _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
385         for (; smIter->More(); smIter->Next() )
386         {
387           _PTR(SObject) aSmObj = smIter->Value();
388           if ( ! aSmObj->FindAttribute( anAttr, "AttributeIOR" ))
389             continue;
390           _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
391           for (; anIter1->More(); anIter1->Next()) {
392             _PTR(SObject) pGeom2 = anIter1->Value();
393             if ( pGeom2->ReferencedObject( pGeom2 ) &&
394                  pGeom2->GetID() == pGeom->GetID() )
395               return aSmObj;
396           }
397         }
398       }
399     }
400   }
401   return _PTR(SObject)();
402 }
403
404 //================================================================================
405 /*!
406  * \brief Updates dialog's look and feel
407  *
408  * Virtual method redefined from the base class updates dialog's look and feel
409  */
410 //================================================================================
411 void SMESHGUI_MeshOp::selectionDone()
412 {
413   if ( !dlg()->isShown() || !myDlg->isEnabled() )
414     return;
415
416   SMESHGUI_SelectionOp::selectionDone();
417
418   try
419   {
420     // Enable tabs according to shape dimension
421
422     int shapeDim = 3;
423
424     QStringList aGEOMs;
425     myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
426     GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
427
428     if (aGEOMs.count() > 0) {
429       // one or more GEOM shape selected
430       aSeq->length(aGEOMs.count());
431       QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
432       int iSubSh = 0;
433       for (; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
434         QString aSubGeomEntry = (*aSubShapesIter);
435         _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.latin1());
436         GEOM::GEOM_Object_var aSubGeomVar =
437           GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
438         aSeq[iSubSh] = aSubGeomVar;
439       }
440     } else {
441       // get geometry by selected sub-mesh
442       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
443       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
444       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
445       if (!aGeomVar->_is_nil()) {
446         aSeq->length(1);
447         aSeq[0] = aGeomVar;
448       }
449     }
450
451     if (aSeq->length() > 0) {
452       shapeDim = 0;
453       for (int iss = 0; iss < aSeq->length() && shapeDim < 3; iss++) {
454         GEOM::GEOM_Object_var aGeomVar = aSeq[iss];
455         switch ( aGeomVar->GetShapeType() ) {
456         case GEOM::SOLID:
457         case GEOM::SHELL:  shapeDim = 3; break;
458         case GEOM::FACE:   shapeDim = (shapeDim < 2) ? 2 : shapeDim; break;
459         case GEOM::WIRE:
460         case GEOM::EDGE:   shapeDim = (shapeDim < 1) ? 1 : shapeDim; break;
461         case GEOM::VERTEX: break;
462         default:
463           TopoDS_Shape aShape;
464           if ( GEOMBase::GetShape(aGeomVar, aShape)) {
465             TopExp_Explorer exp( aShape, TopAbs_SHELL );
466             if ( exp.More() )
467               shapeDim = 3;
468             else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
469               shapeDim = (shapeDim < 2) ? 2 : shapeDim;
470             else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
471               shapeDim = (shapeDim < 1) ? 1 : shapeDim;
472             else
473               ;//shapeDim = 0;
474           }
475         }
476       }
477     }
478     myDlg->setMaxHypoDim( shapeDim );
479
480
481     if ( !myToCreate ) // edition: read hypotheses
482     {
483       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
484       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
485       if ( pObj != 0 )
486       {
487         SMESH::SMESH_subMesh_var aVar =
488           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
489         myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
490         myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
491         myDlg->updateGeometry();
492         myDlg->adjustSize();
493         readMesh();
494       }
495       else
496         myDlg->reset();
497
498     }
499     else if ( !myIsMesh ) // submesh creation
500     {
501       // if a submesh on the selected shape already exist, pass to submesh edition mode
502       if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
503         SMESH::SMESH_subMesh_var sm =
504           SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
505         bool editSubmesh = ( !sm->_is_nil() &&
506                              SUIT_MessageBox::question2( myDlg, tr( "SMESH_WARNING" ),
507                                                          tr( "EDIT_SUBMESH_QUESTION"),
508                                                          tr( "SMESH_BUT_YES" ),
509                                                          tr( "SMESH_BUT_NO" ), 1, 0, 0 ));
510         if ( editSubmesh )
511         {
512           selectionMgr()->clearFilters();
513           selectObject( pSubmesh );
514           SMESHGUI::GetSMESHGUI()->switchToOperation(704);
515           return;
516         }
517         else
518         {
519           myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
520           selectObject( _PTR(SObject)() );
521           selectionDone();
522         }
523       }
524
525       // enable/disable popup for choice of geom selection way
526       bool enable = false;
527       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
528       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
529         SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
530         if ( !mesh->_is_nil() )
531           enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
532       }
533       myDlg->setGeomPopupEnabled( enable );
534     }
535   }
536   catch ( const SALOME::SALOME_Exception& S_ex )
537   {
538     SalomeApp_Tools::QtCatchCorbaException( S_ex );
539   }
540   catch ( ... )
541   {
542   }
543 }
544
545 //================================================================================
546 /*!
547  * \brief Verifies validity of input data
548   * \param theMess - Output parameter intended for returning error message
549   * \retval bool  - TRUE if input data is valid, false otherwise
550  *
551  * Verifies validity of input data. This method is called when "Apply" or "OK" button
552  * is pressed before mesh creation or editing.
553  */
554 //================================================================================
555 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
556 {
557   // Selected object to be  edited
558   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
559   {
560     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
561     return false;
562   }
563
564   // Name
565   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
566   aMeshName = aMeshName.stripWhiteSpace();
567   if ( aMeshName == "" )
568   {
569     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
570     return false;
571   }
572
573   // Imported mesh, if create sub-mesh or edit mesh
574   if ( !myToCreate || ( myToCreate && !myIsMesh ))
575   {
576     QString aMeshEntry = myDlg->selectedObject
577       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
578     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
579       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
580       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
581         theMess = tr( "IMPORTED_MESH" );
582         return false;
583       }
584     }
585   }
586
587   // Geom
588   if ( myToCreate )
589   {
590     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
591     if ( aGeomEntry == "" )
592     {
593       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
594       return false;
595     }
596     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
597     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
598     {
599       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
600       return false;
601     }
602
603     // Mesh
604     if ( !myIsMesh ) // i.e sub-mesh creation,
605     {
606       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
607       if ( aMeshEntry == "" )
608       {
609         theMess = tr( "MESH_IS_NOT_DEFINED" );
610         return false;
611       }
612       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
613       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
614       {
615         theMess = tr( "MESH_IS_NULL" );
616         return false;
617       }
618       if ( !isSubshapeOk() )
619       {
620         theMess = tr( "INVALID_SUBSHAPE" );
621         return false;
622       }
623     }
624   }
625
626   return true;
627 }
628
629 //================================================================================
630 /*!
631  * \brief check compatibility of the algorithm and another algorithm or hypothesis
632   * \param theAlgoData - algorithm data
633   * \param theHypData - hypothesis data
634   * \param theHypType - hypothesis type
635   * \param theHypTypeName - hypothesis type name, must be provided if 2-nd arg is not algo
636   * \retval bool - check result
637  */
638 //================================================================================
639
640 static bool isCompatible(const HypothesisData* theAlgoData,
641                          const HypothesisData* theHypData,
642                          const int             theHypType)
643 {
644   if ( !theAlgoData )
645     return true;
646
647   if ( theHypType == SMESHGUI_MeshOp::Algo )
648     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
649
650   bool isOptional;
651   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
652 }
653
654 //================================================================================
655 /*!
656  * \brief Gets available hypotheses or algorithms
657   * \param theDim - specifies dimension of returned hypotheses/algorifms
658   * \param theHypType - specifies whether algorims or hypotheses or additional ones
659   * are retrieved (possible values are in HypType enumeration)
660   * \param theHyps - Output list of hypotheses' names
661   * \param theAlgoData - to select hypos able to be used by this algo (optional)
662  *
663  * Gets available hypotheses or algorithm in accordance with input parameters
664  */
665 //================================================================================
666 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
667                                      const int       theHypType,
668                                      QStringList&    theHyps,
669                                      THypDataList&   theDataList,
670                                      HypothesisData* theAlgoData ) const
671 {
672   theDataList.clear();
673   theHyps.clear();
674   bool isAlgo = ( theHypType == Algo );
675   bool isAux  = ( theHypType == AddHyp );
676   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux );
677
678   QStringList::const_iterator anIter;
679   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
680   {
681     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
682     if ( isCompatible ( theAlgoData, aData, theHypType )) {
683       theDataList.append( aData );
684       theHyps.append( aData->Label );
685     }
686   }
687 }
688
689 //================================================================================
690 /*!
691  * \brief Gets existing hypotheses or algorithms
692   * \param theDim - specifies dimension of returned hypotheses/algorifms
693   * \param theHypType - specifies whether algorims or hypotheses or additional ones
694   * are retrieved (possible values are in HypType enumeration)
695   * \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
696   * \param theDataList - output list of hypotheses data
697   * \param theHyps - output list of names.
698   * \param theHypVars - output list of variables.
699   * \param theAlgoData - to select hypos able to be used by this algo (optional)
700  *
701  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
702  * input parameters
703  */
704 //================================================================================
705 void SMESHGUI_MeshOp::existingHyps( const int theDim,
706                                     const int theHypType,
707                                     _PTR(SObject) theFather,
708                                     QStringList& theHyps,
709                                     QValueList<SMESH::SMESH_Hypothesis_var>& theHypVars,
710                                     HypothesisData* theAlgoData)
711 {
712   // Clear hypoheses list
713   theHyps.clear();
714   theHypVars.clear();
715
716   if ( !theFather )
717     return;
718
719   const bool isAux  = ( theHypType == AddHyp );
720
721   _PTR(SObject)          aHypRoot;
722   _PTR(GenericAttribute) anAttr;
723   _PTR(AttributeName)    aName;
724   _PTR(AttributeIOR)     anIOR;
725
726   bool isMesh = !_CAST( SComponent, theFather );
727   int aPart = -1;
728   if ( isMesh )
729     aPart = theHypType == Algo ? GLOBAL_ALGO_TAG : GLOBAL_HYPO_TAG;
730   else
731     aPart = theHypType == Algo ? LOCAL_ALGO_TAG : LOCAL_HYPO_TAG;
732
733   if ( theFather->FindSubObject( aPart, aHypRoot ) )
734   {
735     _PTR(ChildIterator) anIter =
736       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
737     for (; anIter->More(); anIter->Next() )
738     {
739       _PTR(SObject) anObj = anIter->Value();
740       if ( isMesh ) // i.e. mesh or submesh
741       {
742         _PTR(SObject) aRefObj;
743         if ( anObj->ReferencedObject( aRefObj ) )
744           anObj = aRefObj;
745         else
746           continue;
747       }
748       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
749       {
750         aName = anAttr;
751         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
752         if ( !CORBA::is_nil( aVar ) )
753         {
754           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
755           if ( !aHypVar->_is_nil() )
756           {
757             QString aHypType( aHypVar->GetName() );
758             HypothesisData* aData = SMESH::GetHypothesisData( aHypType );
759             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
760                  ( isCompatible ( theAlgoData, aData, theHypType )) &&
761                  ( isAux == aData->IsAux ))
762             {
763               //theDataList.append( aData );
764               theHyps.append( aName->Value().c_str() );
765               theHypVars.append( aHypVar );
766             }
767           }
768         }
769       }
770     }
771   }
772 }
773
774 //================================================================================
775 /*!
776  * \brief If create or edit a submesh, return a hypothesis holding parameters used
777  *        to mesh a subshape
778   * \param aHypType - The hypothesis type name
779   * \param aServerLib - Server library name
780   * \param hypData - The structure holding the hypothesis type etc.
781   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
782  */
783 //================================================================================
784
785 SMESH::SMESH_Hypothesis_var
786 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
787                                           const QString& aServerLib ) const
788 {
789   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
790     return SMESH::SMESH_Hypothesis::_nil();
791
792   const int nbColonsInMeshEntry = 3;
793   bool isSubMesh = myToCreate ?
794     !myIsMesh :
795     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).contains(':') > nbColonsInMeshEntry;
796
797   if ( isSubMesh )
798   {
799     // get mesh and geom object
800     SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
801     GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
802
803     QString anEntry = myDlg->selectedObject
804       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
805     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.latin1() ))
806     {
807       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
808       if ( myToCreate ) // mesh and geom may be selected
809       {
810         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
811         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
812         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.latin1() ))
813           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
814       }
815       else // edition: sub-mesh may be selected
816       {
817         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
818         if ( !sm->_is_nil() ) {
819           aMeshVar = sm->GetFather();
820           aGeomVar = sm->GetSubShape();
821         }
822       }
823     }
824
825     if ( !aMeshVar->_is_nil() && !aGeomVar->_is_nil() )
826       return SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType,
827                                                                     aServerLib,
828                                                                     aMeshVar,
829                                                                     aGeomVar );
830   }
831   return SMESH::SMESH_Hypothesis::_nil();
832 }
833
834 //================================================================================
835 /*!
836  * \Brief Returns tab dimention
837   * \param tab - the tab in the dlg
838   * \param dlg - my dialogue
839   * \retval int - dimention
840  */
841 //================================================================================
842
843 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
844 {
845   int aDim = -1;
846   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
847     if (tab == dlg->tab(i))
848       aDim = i;
849   return aDim;
850 }
851
852 //================================================================================
853 /*!
854  * \brief Create hypothesis
855   * \param theHypType - hypothesis category (main or additional)
856   * \param theIndex - index of type of hypothesis to be cerated
857  *
858  * Specifies dimension of hypothesis to be created (using sender() method),
859  * specifies its type and calls method for hypothesis creation
860  */
861 //================================================================================
862 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
863 {
864   // Specifies dimension of hypothesis to be created
865   int aDim = getTabDim( sender(), myDlg );
866   if (aDim == -1)
867     return;
868
869   // Specifies type of hypothesis to be created
870   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
871   if (theIndex < 0 || theIndex >= dataList.count())
872     return;
873   QString aHypTypeName = dataList[ theIndex ]->TypeName;
874
875   // Create hypothesis
876   createHypothesis(aDim, theHypType, aHypTypeName);
877 }
878
879 //================================================================================
880 /*!
881  *  Create hypothesis and update dialog.
882  *  \param theDim - dimension of hypothesis to be created
883  *  \param theType - hypothesis category (algorithm, hypothesis, additional hypothesis)
884  *  \param theTypeName - specifies hypothesis to be created
885  */
886 //================================================================================
887 void SMESHGUI_MeshOp::createHypothesis (const int theDim,
888                                         const int theType,
889                                         const QString& theTypeName)
890 {
891   // During a hypothesis creation we might need to select some objects.
892   // Main dialog must not update it's own selected objects in this case.
893   dlg()->deactivateAll();
894
895   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName.latin1());
896   if (!aData)
897     return;
898
899   // existing hypos
900   int nbHyp = myExistingHyps[theDim][theType].count();
901
902   QString aClientLibName = aData->ClientLibName;
903   if (aClientLibName == "") {
904     // Call hypothesis creation server method (without GUI)
905     SMESH::CreateHypothesis(theTypeName, aData->Label, false);
906   } else {
907     // Get hypotheses creator client (GUI)
908     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
909
910     // Create hypothesis
911     if (aCreator) {
912       // When create or edit a submesh, try to initialize a new hypothesis
913       // with values used to mesh a subshape
914       SMESH::SMESH_Hypothesis_var initParamHyp =
915         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
916       myDlg->setEnabled( false );
917       aCreator->create(initParamHyp, myDlg);
918       myDlg->setEnabled( true );
919     } else {
920       SMESH::CreateHypothesis(theTypeName, aData->Label, false);
921     }
922   }
923
924   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
925
926   HypothesisData* algoData = hypData( theDim, Algo, currentHyp( theDim, Algo ));
927   QStringList aNewHyps;
928   existingHyps(theDim, theType, aFather, aNewHyps, myExistingHyps[theDim][theType], algoData);
929   if (aNewHyps.count() > nbHyp) {
930     for (int i = nbHyp; i < aNewHyps.count(); i++)
931       myDlg->tab(theDim)->addHyp(theType, aNewHyps[i]);
932   }
933 }
934
935 //================================================================================
936 /*!
937  * \brief Calls plugin methods for hypothesis editing
938   * \param theHypType - specifies whether main hypothesis or additional one
939   * is edited
940   * \param theIndex - index of existing hypothesis
941  *
942  * Calls plugin methods for hypothesis editing
943  */
944 //================================================================================
945 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
946 {
947   // Speicfies dimension of hypothesis to be created
948   int aDim = getTabDim( sender(), myDlg );
949   if (aDim == -1)
950     return;
951
952   QValueList<SMESH::SMESH_Hypothesis_var> aList = myExistingHyps[ aDim ][ theHypType ];
953   if ( theIndex < 0 || theIndex >= aList.count() )
954     return;
955   SMESH::SMESH_Hypothesis_var aHyp = aList[ theIndex ];
956   if ( aHyp->_is_nil() )
957     return;
958
959   char* aTypeName = aHyp->GetName();
960   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aTypeName );
961   if ( aCreator ) {
962     myDlg->setEnabled( false );
963     aCreator->edit( aHyp.in(), dlg() );
964     myDlg->setEnabled( true );
965   }
966 }
967
968 //================================================================================
969 /*!
970  * \brief access to hypothesis data
971   * \param theDim - hyp dimension
972   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
973   * \param theIndex - index in the list
974   * \retval HypothesisData* - result data, may be 0
975  */
976 //================================================================================
977
978 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
979                                           const int theHypType,
980                                           const int theIndex)
981 {
982   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
983        theHypType > -1 && theHypType < NbHypTypes &&
984        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
985     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
986   return 0;
987 }
988
989 //================================================================================
990 /*!
991  * \brief Set available algos and hypos according to the selected algorithm
992   * \param theIndex - algorithm index
993  */
994 //================================================================================
995
996 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
997                                       const int theDim )
998 {
999   if ( myIgnoreAlgoSelection )
1000     return;
1001
1002   int aDim = theDim < 0 ? getTabDim( sender(), myDlg ): theDim;
1003   if (aDim == -1)
1004     return;
1005
1006   // find highest available dimension, all algos of this dimension are available for choice
1007   int aTopDim = -1;
1008   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
1009     if (isAccessibleDim( i ))
1010       aTopDim = i;
1011   if (aTopDim == -1)
1012     return;
1013
1014   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1015
1016   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1017   HypothesisData* algoByDim[4];
1018   algoByDim[ aDim ] = algoData;
1019
1020   QStringList anAvailable;
1021   if ( !algoData ) { // all algos becomes available
1022     availableHyps( aDim, Algo, anAvailable, myAvailableHypData[ aDim ][ Algo ]);
1023     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1024   }
1025
1026   // check that algorithms of other dimentions are compatible with
1027   // the selected one
1028
1029    // 2 loops: backward and forward from algo dimension
1030   for ( int forward = false; forward <= true; ++forward )
1031   {
1032     int dim = aDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
1033     if ( !forward ) {
1034       dim = aDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
1035     }
1036     HypothesisData* prevAlgo = algoData;
1037     bool noCompatible = false;
1038     for ( ; dim * dir <= lastDim * dir ; dim += dir )
1039     {
1040       if ( !isAccessibleDim( dim ))
1041         continue;
1042       if ( noCompatible ) { // the selected algo has no compatible ones
1043         anAvailable.clear();
1044         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1045         myAvailableHypData[dim][Algo].clear();
1046         algoByDim[ dim ] = 0;
1047         continue;
1048       }
1049       // get currently selected algo
1050       int algoIndex = currentHyp( dim, Algo );
1051       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
1052       if ( curAlgo ) { // some algo selected
1053         if ( !isCompatible( prevAlgo, curAlgo, Algo ))
1054           curAlgo = 0;
1055       }
1056       // set new available algoritms
1057       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
1058       HypothesisData* soleCompatible = 0;
1059       if ( anAvailable.count() == 1 )
1060         soleCompatible = myAvailableHypData[dim][Algo][0];
1061       if ( dim == aTopDim && prevAlgo ) // all available algoritms should be selectable any way
1062         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], 0 );
1063       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1064       noCompatible = anAvailable.isEmpty();
1065
1066       // restore previously selected algo
1067       algoIndex = myAvailableHypData[dim][Algo].findIndex( curAlgo );
1068       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward && dim != SMESH::DIM_0D)
1069         // select the sole compatible algo
1070         algoIndex = myAvailableHypData[dim][Algo].findIndex( soleCompatible );
1071       setCurrentHyp( dim, Algo, algoIndex );
1072
1073       // remember current algo
1074       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1075     }
1076   }
1077
1078   // set hypotheses corresponding to the found algoritms
1079
1080   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1081
1082   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1083   {
1084     if ( !isAccessibleDim( dim ))
1085       continue;
1086     for ( int type = MainHyp; type < NbHypTypes; type++ )
1087     {
1088       myAvailableHypData[ dim ][ type ].clear();
1089       QStringList anAvailable, anExisting;
1090
1091       HypothesisData* curAlgo = algoByDim[ dim ];
1092       int hypIndex = currentHyp( dim, type );
1093
1094       SMESH::SMESH_Hypothesis_var curHyp;
1095       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1096         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ];
1097
1098       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
1099         // try to find algo by selected hypothesis in order to keep it selected
1100         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1101         QString curHypType = curHyp->GetName();
1102         if ( !algoDeselectedByUser &&
1103              myObjHyps[ dim ][ type ].count() > 0 &&
1104              curHypType == myObjHyps[ dim ][ type ][ 0 ]->GetName())
1105         {
1106           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1107           for ( int i = 0 ; i < myAvailableHypData[ dim ][ Algo ].count(); ++i ) {
1108             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1109             if ( curAlgo && hypData && isCompatible( curAlgo, hypData, type ))
1110               break;
1111             else
1112               curAlgo = 0;
1113           }
1114         }
1115       }
1116       // get hyps compatible with curAlgo
1117       if ( curAlgo )
1118       {
1119         // check if a selected hyp is compatible with the curAlgo
1120         if ( !curHyp->_is_nil() ) {
1121           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1122           if ( !isCompatible( curAlgo, hypData, type ))
1123             curHyp = SMESH::SMESH_Hypothesis::_nil();
1124         }
1125         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
1126         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
1127       }
1128       // set list of hypotheses
1129       myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1130       myDlg->tab( dim )->setExistingHyps( type, anExisting );
1131
1132       // set current existing hypothesis
1133       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1134         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1135       else
1136         hypIndex = -1;
1137       if ( !isSubmesh && hypIndex < 0 && anExisting.count() == 1 ) {
1138         // none is yet selected => select the sole existing if it is not optional
1139         QString hypTypeName = myExistingHyps[ dim ][ type ][ 0 ]->GetName();
1140         bool isOptional = true;
1141         if ( algoByDim[ dim ] &&
1142              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName, isOptional ) &&
1143              !isOptional )
1144           hypIndex = 0;
1145       }
1146       setCurrentHyp( dim, type, hypIndex );
1147     }
1148   }
1149 }
1150
1151 //================================================================================
1152 /*!
1153  * \brief Creates and selects hypothesis of hypotheses set
1154  * \param theSetName - The name of hypotheses set
1155  */
1156 //================================================================================
1157 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1158 {
1159   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet(theSetName);
1160   if (!aHypoSet) return;
1161
1162   // clear all hyps
1163   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++) {
1164     setCurrentHyp(dim, Algo, -1);
1165     setCurrentHyp(dim, AddHyp, -1);
1166     setCurrentHyp(dim, MainHyp, -1);
1167   }
1168
1169   for (int aHypType = Algo; aHypType < AddHyp; aHypType++) {
1170     bool isAlgo = (aHypType == Algo);
1171
1172     // set hyps from the set
1173     QStringList* aHypoList = isAlgo ? &aHypoSet->AlgoList : &aHypoSet->HypoList;
1174     for (int i = 0, n = aHypoList->count(); i < n; i++) {
1175       const QString& aHypoTypeName = (*aHypoList)[ i ];
1176       HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
1177       if (!aHypData)
1178         continue;
1179
1180       int aDim = aHypData->Dim[0];
1181       // create or/and set
1182       if (isAlgo) {
1183         int index = myAvailableHypData[aDim][Algo].findIndex( aHypData );
1184         if ( index < 0 ) {
1185           QStringList anAvailable;
1186           availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
1187           myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1188           index = myAvailableHypData[aDim][Algo].findIndex( aHypData );
1189         }
1190         setCurrentHyp( aDim, Algo, index );
1191         onAlgoSelected( index, aDim );
1192       }
1193       else {
1194         bool mainHyp = true;
1195         int index = myAvailableHypData[aDim][MainHyp].findIndex( aHypData );
1196         if ( index < 0 ) {
1197           mainHyp = false;
1198           index = myAvailableHypData[aDim][AddHyp].findIndex( aHypData );
1199         }
1200         if (index >= 0)
1201           createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
1202       }
1203     } // loop on hypos in the set
1204   } // loop on algo/hypo
1205 }
1206
1207 //================================================================================
1208 /*!
1209  * \brief Creates mesh
1210   * \param theMess - Output parameter intended for returning error message
1211   * \retval bool  - TRUE if mesh is created, FALSE otherwise
1212  *
1213  * Creates mesh
1214  */
1215 //================================================================================
1216 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
1217 {
1218   theMess = "";
1219
1220   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1221   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1222
1223   QStringList aList;
1224   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1225   QStringList::Iterator it = aList.begin();
1226   for(; it!=aList.end(); it++) {
1227
1228     QString aGeomEntry = *it;
1229     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1230     GEOM::GEOM_Object_var aGeomVar =
1231       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1232
1233     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1234     if ( aSMESHGen->_is_nil() )
1235       return false;
1236
1237     SUIT_OverrideCursor aWaitCursor;
1238
1239     // create mesh
1240     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
1241     if ( aMeshVar->_is_nil() )
1242       return false;
1243     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1244     if ( aMeshSO )
1245       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1246
1247     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ ) {
1248       if ( !isAccessibleDim( aDim )) continue;
1249
1250       // assign hypotheses
1251       for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ ) {
1252         int aHypIndex = currentHyp( aDim, aHypType );
1253         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() ) {
1254           SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1255           if ( !aHypVar->_is_nil() )
1256             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
1257         }
1258       }
1259       // find or create algorithm
1260       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1261       if ( !anAlgoVar->_is_nil() )
1262         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1263     }
1264
1265   }
1266   return true;
1267 }
1268
1269 //================================================================================
1270 /*!
1271  * \brief Creates sub-mesh
1272   * \param theMess - Output parameter intended for returning error message
1273   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1274  *
1275  * Creates sub-mesh
1276  */
1277 //================================================================================
1278 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
1279 {
1280   theMess = "";
1281
1282   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1283   if ( aSMESHGen->_is_nil() )
1284     return false;
1285
1286   // get mesh object
1287   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1288   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
1289   SMESH::SMESH_Mesh_var aMeshVar =
1290     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1291   if (aMeshVar->_is_nil())
1292     return false;
1293
1294   // GEOM shape of the main mesh
1295   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
1296
1297   // Name for the new sub-mesh
1298   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
1299
1300   // get geom object
1301   GEOM::GEOM_Object_var aGeomVar;
1302   QStringList aGEOMs;
1303   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
1304   if (aGEOMs.count() == 1)
1305   {
1306     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1307     QString aGeomEntry = aGEOMs.first();
1308     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1309     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1310   }
1311   else if (aGEOMs.count() > 1)
1312   {
1313     // create a GEOM group
1314     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1315     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1316     if (!geomGen->_is_nil() && aStudy) {
1317       GEOM::GEOM_IGroupOperations_var op =
1318         geomGen->GetIGroupOperations(aStudy->StudyId());
1319       if (!op->_is_nil()) {
1320         // check and add all selected GEOM objects: they must be
1321         // a sub-shapes of the main GEOM and must be of one type
1322         int iSubSh = 0;
1323         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1324         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
1325         aSeq->length(aGEOMs.count());
1326         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
1327         for (; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
1328           QString aSubGeomEntry = (*aSubShapesIter);
1329           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.latin1());
1330           GEOM::GEOM_Object_var aSubGeomVar =
1331             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
1332           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
1333           if (iSubSh == 0) {
1334             aGroupType = aSubShapeType;
1335           } else {
1336             if (aSubShapeType != aGroupType)
1337               aGroupType = TopAbs_SHAPE;
1338           }
1339           aSeq[iSubSh] = aSubGeomVar;
1340         }
1341         // create a group
1342         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(mainGeom, aGroupType);
1343         op->UnionList(aGroupVar, aSeq);
1344
1345         if (op->IsDone()) {
1346           aGeomVar = aGroupVar;
1347
1348           // publish the GEOM group in study
1349           QString aNewGeomGroupName ("Auto_group_for_");
1350           aNewGeomGroupName += aName;
1351           SALOMEDS::SObject_var aNewGroupSO =
1352             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGeomVar, aNewGeomGroupName, mainGeom);
1353         }
1354       }
1355     }
1356   }
1357   else {
1358   }
1359   if (aGeomVar->_is_nil())
1360     return false;
1361
1362   SUIT_OverrideCursor aWaitCursor;
1363
1364   // create sub-mesh
1365   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.latin1() );
1366
1367   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
1368   {
1369     if ( !isAccessibleDim( aDim )) continue;
1370
1371     // find or create algorithm
1372     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1373     if ( !anAlgoVar->_is_nil() )
1374       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1375     // assign hypotheses
1376     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1377     {
1378       int aHypIndex = currentHyp( aDim, aHypType );
1379       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1380       {
1381         SMESH::SMESH_Hypothesis_var aHypVar =
1382           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1383         if ( !aHypVar->_is_nil() )
1384           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1385       }
1386     }
1387   }
1388
1389   // deselect geometry: next submesh should be created on other subshape
1390   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
1391   selectObject( _PTR(SObject)() );
1392   selectionDone();
1393
1394   return true;
1395 }
1396
1397 //================================================================================
1398 /*!
1399  * \brief Gets current hypothesis or algorithms
1400   * \param theDim - dimension of hypothesis or algorithm
1401   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1402   * \retval int - current hypothesis or algorithms
1403  *
1404  * Gets current hypothesis or algorithms
1405  */
1406 //================================================================================
1407 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1408 {
1409   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1410 }
1411
1412 //================================================================================
1413 /*!
1414  * \brief Returns true if hypotheses of given dim can be assigned
1415   * \param theDim - hypotheses dimension
1416   * \retval bool - result
1417  */
1418 //================================================================================
1419 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
1420 {
1421   return myDlg->tab( theDim )->isEnabled();
1422 }
1423
1424 //================================================================================
1425 /*!
1426  * \brief Sets current hypothesis or algorithms
1427   * \param theDim - dimension of hypothesis or algorithm
1428   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1429   * \param theIndex - Index of hypothesis
1430  *
1431  * Gets current hypothesis or algorithms
1432  */
1433 //================================================================================
1434 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1435                                      const int theHypType,
1436                                      const int theIndex )
1437 {
1438   myIgnoreAlgoSelection = true;
1439   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1440   myIgnoreAlgoSelection = false;
1441 }
1442
1443 //================================================================================
1444 /*!
1445  * \brief Generates default and sets mesh/submesh name
1446  *
1447  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1448  */
1449 //================================================================================
1450 void SMESHGUI_MeshOp::setDefaultName() const
1451 {
1452   QString aResName;
1453
1454   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1455   int i = 1;
1456   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1457   _PTR(SObject) anObj;
1458   do
1459   {
1460     aResName = aPrefix + QString::number( i++ );
1461     anObj = aStudy->FindObject( aResName.latin1() );
1462   }
1463   while ( anObj );
1464
1465   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1466     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1467   aControl->setText( aResName );
1468 }
1469
1470 //================================================================================
1471 /*!
1472  * \brief Gets algorithm or creates it if necessary
1473   * \param theDim - specifies dimension of returned hypotheses/algorifms
1474   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1475  *
1476  * Gets algorithm or creates it if necessary
1477  */
1478 //================================================================================
1479 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1480 {
1481   SMESH::SMESH_Hypothesis_var anAlgoVar;
1482
1483   // get type of the selected algo
1484   int aHypIndex = currentHyp( theDim, Algo );
1485   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
1486   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
1487     return anAlgoVar;
1488   QString aHypName = dataList[ aHypIndex ]->TypeName;
1489
1490   // get existing algoritms
1491   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1492   QStringList tmp;
1493   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
1494
1495   // look for anexisting algo of such a type
1496   QValueList<SMESH::SMESH_Hypothesis_var>& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1497   QValueList<SMESH::SMESH_Hypothesis_var>::iterator anIter;
1498   for ( anIter = aHypVarList.begin(); anIter != aHypVarList.end(); anIter++ )
1499   {
1500     SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1501     if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1502     {
1503       anAlgoVar = aHypVar;
1504       break;
1505     }
1506   }
1507
1508   if (anAlgoVar->_is_nil()) {
1509     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1510     if (aHypData) {
1511       QString aClientLibName = aHypData->ClientLibName;
1512       if (aClientLibName == "") {
1513         // Call hypothesis creation server method (without GUI)
1514         SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1515       } else {
1516         // Get hypotheses creator client (GUI)
1517         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
1518
1519         // Create algorithm
1520         if (aCreator)
1521           aCreator->create(true, myDlg);
1522         else
1523           SMESH::CreateHypothesis(aHypName, aHypData->Label, true);
1524       }
1525       QStringList tmpList;
1526       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
1527       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
1528     }
1529
1530     QValueList<SMESH::SMESH_Hypothesis_var>& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
1531     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
1532     {
1533       SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1534       if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1535       {
1536         anAlgoVar = aHypVar;
1537         break;
1538       }
1539     }
1540   }
1541
1542   return anAlgoVar._retn();
1543 }
1544
1545 //================================================================================
1546 /*!
1547  * \brief Reads parameters of edited mesh and assigns them to the dialog
1548  *
1549  * Reads parameters of edited mesh and assigns them to the dialog (called when
1550  * mesh is edited only)
1551  */
1552 //================================================================================
1553 void SMESHGUI_MeshOp::readMesh()
1554 {
1555   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1556   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1557   if ( !pObj )
1558     return;
1559
1560   // Get name of mesh if current object is sub-mesh
1561   SMESH::SMESH_subMesh_var aSubMeshVar =
1562       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1563   if ( !aSubMeshVar->_is_nil() )
1564   {
1565     SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1566     if ( !aMeshVar->_is_nil() )
1567     {
1568       _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1569       QString aMeshName = name( aMeshSO );
1570       myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
1571     }
1572   }
1573
1574   // Get name of geometry object
1575   GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1576   if ( !aGeomVar->_is_nil() )
1577   {
1578     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
1579     QString aShapeName = name( aGeomSO );
1580     myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
1581   }
1582
1583   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
1584   QStringList anExisting;
1585   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1586   {
1587     // get algorithm
1588     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
1589     // find algo index among available ones
1590     int aHypIndex = -1;
1591     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
1592     {
1593       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first();
1594       QString aHypTypeName = aVar->GetName();
1595       HypothesisData* algoData = SMESH::GetHypothesisData( aHypTypeName );
1596       aHypIndex = myAvailableHypData[ dim ][ Algo ].findIndex ( algoData );
1597 //       if ( aHypIndex < 0 && algoData ) {
1598 //         // assigned algo is incompatible with other algorithms
1599 //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
1600 //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
1601 //       }
1602     }
1603     setCurrentHyp( dim, Algo, aHypIndex );
1604     // set existing and available hypothesis according to the selected algo
1605     onAlgoSelected( aHypIndex, dim );
1606   }
1607
1608   // get hypotheses
1609   bool hypWithoutAlgo = false;
1610   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1611   {
1612     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1613     {
1614       // get hypotheses
1615       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1616       // find index of requered hypothesis among existing ones for this dimension and type
1617       int aHypIndex = -1;
1618       if ( myObjHyps[ dim ][ hypType ].count() > 0 ) {
1619         aHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1620                           myExistingHyps[ dim ][ hypType ] );
1621         if ( aHypIndex < 0 ) {
1622           // assigned hypothesis is incompatible with the algorithm
1623           if ( currentHyp( dim, Algo ) < 0 )
1624           { // none algo selected; it is edition for sure, of submesh maybe
1625             hypWithoutAlgo = true;
1626             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ].first() );
1627             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
1628             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
1629           }
1630         }
1631       }
1632       setCurrentHyp( dim, hypType, aHypIndex );
1633     }
1634   }
1635   // make available other hyps of same type as one without algo
1636   if ( hypWithoutAlgo )
1637     onAlgoSelected( currentHyp( 0, Algo ), 0 );
1638 }
1639
1640 //================================================================================
1641 /*!
1642  * \brief Gets name of object
1643   * \param theSO - SObject
1644   * \retval QString - name of object
1645  *
1646  * Gets name of object
1647  */
1648 //================================================================================
1649 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1650 {
1651   QString aResName;
1652   if ( theSO )
1653   {
1654     _PTR(GenericAttribute) anAttr;
1655     _PTR(AttributeName)    aNameAttr;
1656     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1657     {
1658       aNameAttr = anAttr;
1659       aResName = aNameAttr->Value().c_str();
1660     }
1661   }
1662   return aResName;
1663 }
1664
1665 //================================================================================
1666 /*!
1667  * \brief Finds hypothesis in input list
1668   * \param theHyp - hypothesis to be found
1669   * \param theHypList - input list of hypotheses
1670   * \retval int - index of hypothesis or -1 if it is not found
1671  *
1672  * Finds position of hypothesis in input list
1673  */
1674 //================================================================================
1675 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1676                            const QValueList<SMESH::SMESH_Hypothesis_var>& theHypList ) const
1677 {
1678   int aRes = -1;
1679   if ( !theHyp->_is_nil() )
1680   {
1681     int i = 0;
1682     QValueList<SMESH::SMESH_Hypothesis_var>::const_iterator anIter;
1683     for ( anIter = theHypList.begin(); anIter != theHypList.end(); ++ anIter )
1684     {
1685       if ( theHyp->_is_equivalent( *anIter ) )
1686       {
1687         aRes = i;
1688         break;
1689       }
1690       i++;
1691     }
1692   }
1693   return aRes;
1694 }
1695
1696 //================================================================================
1697 /*!
1698  * \brief Edits mesh or sub-mesh
1699   * \param theMess - Output parameter intended for returning error message
1700   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1701  *
1702  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1703  */
1704 //================================================================================
1705 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1706 {
1707   theMess = "";
1708
1709   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1710   if ( aSMESHGen->_is_nil() )
1711     return false;
1712
1713   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1714   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1715   if ( !pObj )
1716     return false;
1717
1718   SUIT_OverrideCursor aWaitCursor;
1719
1720   // Set new name
1721   SMESH::SetName( pObj, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1722
1723   // First, remove old algos in order to avoid messages on algorithm hiding
1724   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1725   {
1726     if ( isAccessibleDim( dim ) && myObjHyps[ dim ][ Algo ].count() > 0 )
1727     {
1728       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first();
1729       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1730       if ( anAlgoVar->_is_nil() || // no new algo selected or
1731            strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) ) // algo change
1732       {
1733         // remove old algorithm
1734         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first() );
1735         myObjHyps[ dim ][ Algo ].clear();
1736       }
1737     }
1738   }
1739
1740   // Assign new algorithms and hypotheses
1741   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1742   {
1743     if ( !isAccessibleDim( dim )) continue;
1744
1745     // find or create algorithm
1746     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1747
1748     // assign new algorithm
1749     if ( !anAlgoVar->_is_nil() && // some algo selected and
1750          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
1751     {
1752       SMESH::SMESH_Mesh_var aMeshVar =
1753         SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1754       bool isMesh = !aMeshVar->_is_nil();
1755       if ( isMesh ) {
1756         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1757       } else {
1758         SMESH::SMESH_subMesh_var aVar =
1759           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1760         if ( !aVar->_is_nil() )
1761           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1762       }
1763       myObjHyps[ dim ][ Algo ].append( anAlgoVar );
1764     }
1765
1766     // assign hypotheses
1767     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1768     {
1769       int aNewHypIndex = currentHyp( dim, hypType );
1770       int anOldHypIndex = -1;
1771
1772       // remove old hypotheses
1773       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1774       {
1775         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1776                               myExistingHyps[ dim ][ hypType ] );
1777         if ( aNewHypIndex != anOldHypIndex || // different hyps
1778              anOldHypIndex == -1 )            // hyps of different algos
1779         {
1780           SMESH::RemoveHypothesisOrAlgorithmOnMesh
1781             ( pObj, myObjHyps[ dim ][ hypType ].first() );
1782           myObjHyps[ dim ][ hypType ].clear();
1783         }
1784       }
1785
1786       // assign new hypotheses
1787       if ( aNewHypIndex != anOldHypIndex && aNewHypIndex != -1 )
1788       {
1789         SMESH::SMESH_Mesh_var aMeshVar =
1790           SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1791         bool isMesh = !aMeshVar->_is_nil();
1792         if ( isMesh )
1793         {
1794           SMESH::AddHypothesisOnMesh
1795             (aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1796         }
1797         else
1798         {
1799           SMESH::SMESH_subMesh_var aVar =
1800             SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1801           if ( !aVar->_is_nil() )
1802             SMESH::AddHypothesisOnSubMesh
1803               ( aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1804         }
1805       }
1806       // reread all hypotheses of mesh if necessary
1807       QStringList anExisting;
1808       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1809     }
1810   }
1811
1812   return true;
1813 }
1814
1815 //================================================================================
1816 /*!
1817  * \brief Verifies whether given operator is valid for this one
1818   * \param theOtherOp - other operation
1819   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1820 *
1821 * method redefined from base class verifies whether given operator is valid for
1822 * this one (i.e. can be started "above" this operator). In current implementation method
1823 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1824 * elements.
1825 */
1826 //================================================================================
1827 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1828 {
1829   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1830 }
1831
1832 //================================================================================
1833 /*!
1834  * \brief SLOT. Is called when the user selects a way of geometry selection
1835   * \param theByMesh - true if the user wants to find geometry by mesh element
1836  */
1837 //================================================================================
1838
1839 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
1840 {
1841   if ( theByMesh ) {
1842     if ( !myShapeByMeshOp ) {
1843       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
1844       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
1845               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
1846       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
1847               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
1848     }
1849     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
1850     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1851     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
1852       SMESH::SMESH_Mesh_var aMeshVar =
1853         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1854       if ( !aMeshVar->_is_nil() ) {
1855         myDlg->hide(); // stop processing selection
1856         myShapeByMeshOp->setModule( getSMESHGUI() );
1857         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
1858         myShapeByMeshOp->SetMesh( aMeshVar );
1859         myShapeByMeshOp->start();
1860       }
1861     }
1862   }
1863 }
1864
1865 //================================================================================
1866 /*!
1867  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1868  */
1869 //================================================================================
1870
1871 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
1872 {
1873   if ( myShapeByMeshOp == op ) {
1874     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
1875     myDlg->show();
1876     // Select a found geometry object
1877     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
1878     if ( !aGeomVar->_is_nil() )
1879     {
1880       QString ID = aGeomVar->GetStudyEntry();
1881       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.latin1() )) {
1882         selectObject( aGeomSO );
1883         selectionDone();
1884       }
1885     }
1886   }
1887 }
1888
1889 //================================================================================
1890 /*!
1891  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
1892  */
1893 //================================================================================
1894
1895 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
1896 {
1897   if ( myShapeByMeshOp == op && myDlg ) {
1898     myDlg->show();
1899   }
1900 }
1901
1902 //================================================================================
1903 /*!
1904  * \brief Selects a SObject
1905   * \param theSObj - the SObject to select
1906  */
1907 //================================================================================
1908
1909 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
1910 {
1911   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
1912     SALOME_ListIO anIOList;
1913     if ( theSObj ) {
1914       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1915         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
1916       anIOList.Append( anIO );
1917     }
1918     sm->setSelectedObjects( anIOList, false );
1919   }
1920 }