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