Salome HOME
PAL10953. When a submesh is being created, provide possibility to select geometry...
[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/
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 "SALOMEDSClient_Study.hxx"
53 #include "SALOMEDSClient_AttributeIOR.hxx"
54 #include "SALOMEDSClient_AttributeName.hxx"
55 #include "SALOMEDS_SComponent.hxx"
56 #include "SALOMEDS_SObject.hxx"
57
58 #include "LightApp_SelectionMgr.h"
59 #include "LightApp_UpdateFlags.h"
60 #include "SUIT_MessageBox.h"
61 #include "SUIT_Desktop.h"
62 #include "SUIT_OverrideCursor.h"
63 #include "SALOME_InteractiveObject.hxx"
64 #include "SALOME_ListIO.hxx"
65
66 #include "utilities.h"
67
68 #include <qstringlist.h>
69 #include <qlineedit.h>
70
71 #include <TopoDS_Shape.hxx>
72 #include <TopExp_Explorer.hxx>
73
74 enum { GLOBAL_ALGO_TAG        =3,
75        GLOBAL_HYPO_TAG        =2,
76        LOCAL_ALGO_TAG         =2,
77        LOCAL_HYPO_TAG         =1,
78        SUBMESH_ON_EDGE_TAG    =5,
79        SUBMESH_ON_WIRE_TAG    =6,
80        SUBMESH_ON_FACE_TAG    =7,
81        SUBMESH_ON_SHELL_TAG   =8,
82        SUBMESH_ON_SOLID_TAG   =9,
83        SUBMESH_ON_COMPOUND_TAG=10 };
84        
85 //================================================================================
86 /*!
87  * \brief Constructor
88   * \param theToCreate - if this parameter is true then operation is used for creation,
89   * for editing otherwise
90  *
91  * Initialize operation
92 */
93 //================================================================================
94 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
95 : SMESHGUI_SelectionOp(),
96   myToCreate( theToCreate ),
97   myIsMesh( theIsMesh ),
98   myDlg( 0 ),
99   myShapeByMeshDlg( 0 )
100 {
101   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
102     GeometryGUI::InitGeomGen();
103 }
104
105 //================================================================================
106 /*!
107  * \brief Destructor
108 */
109 //================================================================================
110 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
111 {
112   if ( myDlg )
113     delete myDlg;
114 }
115
116 //================================================================================
117 /*!
118  * \brief Gets dialog of this operation
119   * \retval LightApp_Dialog* - pointer to dialog of this operation
120 */
121 //================================================================================
122 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
123 {
124   return myDlg;
125 }
126
127 //================================================================================
128 /*!
129  * \brief Creates or edits mesh
130   * \retval bool - TRUE if operation is performed successfully, FALSE otherwise
131  *
132  * Virtual slot redefined from the base class called when "Apply" button is clicked
133  * creates or edits mesh
134  */
135 //================================================================================
136 bool SMESHGUI_MeshOp::onApply()
137 {
138   if( isStudyLocked() )
139     return false;
140
141   QString aMess;
142   if ( !isValid( aMess ) )
143   {
144     if ( aMess != "" )
145       SUIT_MessageBox::warn1( myDlg,
146         tr( "SMESH_WRN_WARNING" ), aMess, tr( "SMESH_BUT_OK" ) );
147     return false;
148   }
149
150   bool aResult = false;
151   aMess = "";
152   try
153   {
154     if ( myToCreate && myIsMesh )
155       aResult = createMesh( aMess );
156     if ( myToCreate && !myIsMesh )
157       aResult = createSubMesh( aMess );
158     else if ( !myToCreate )
159       aResult = editMeshOrSubMesh( aMess );
160     if ( aResult )
161       update( UF_ObjBrowser | UF_Model );
162   }
163   catch ( const SALOME::SALOME_Exception& S_ex )
164   {
165     SalomeApp_Tools::QtCatchCorbaException( S_ex );
166     aResult = false;
167   }
168   catch ( ... )
169   {
170     aResult = false;
171   }
172
173   if ( aResult )
174   {
175     if ( myToCreate )
176       setDefaultName();
177   }
178   else
179   {
180     if ( aMess == "" )
181       aMess = tr( "SMESH_OPERATION_FAILED" );
182     SUIT_MessageBox::warn1( myDlg,
183       tr( "SMESH_ERROR" ), aMess, tr( "SMESH_BUT_OK" ) );
184   }
185
186   return aResult;
187 }
188
189 //================================================================================
190 /*!
191  * \brief Creates dialog if necessary and shows it
192  *
193  * Virtual method redefined from base class called when operation is started creates
194  * dialog if necessary and shows it, activates selection
195  */
196 //================================================================================
197 void SMESHGUI_MeshOp::startOperation()
198 {
199   if( !myDlg )
200   {
201     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
202     for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
203     {
204       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
205               this, SLOT( onCreateHyp( const int, const int) ) );
206       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
207               this, SLOT( onEditHyp( const int, const int) ) );
208     }
209     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
210     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
211   }
212   SMESHGUI_SelectionOp::startOperation();
213
214   // iterate through dimensions and get available and existing algoritms and hypotheses,
215   // set them to the dialog
216   int i, j;
217   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
218   for ( i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
219   {
220     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
221     QStringList anAvailable, anExisting;
222     for ( j = Algo; j <= AddHyp; j++ )
223     {
224       availableHyps( i, j, anAvailable );
225       existingHyps( i, j, aFather, anExisting, myExistingHyps[ i ][ j ] );
226
227       aTab->setAvailableHyps( j, anAvailable );
228       aTab->setExistingHyps( j, anExisting );
229     }
230   }
231   if ( myToCreate )
232   {
233     setDefaultName();
234     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
235   }
236   else
237     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
238
239   myDlg->setHypoSets( SMESH::GetHypothesesSets() );
240
241   selectionDone();
242
243   myDlg->setCurrentTab( SMESH::DIM_1D );
244   myDlg->show();
245 }
246
247 //================================================================================
248 /*!
249  * \brief Creates selection filter
250   * \param theId - identifier of current selection widget
251   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
252  *
253  * Creates selection filter in accordance with identifier of current selection widget
254  */
255 //================================================================================
256 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
257 {
258   if ( theId == SMESHGUI_MeshDlg::Geom )
259   {
260 //     TColStd_MapOfInteger allTypesMap;
261 //     for ( int i = 0; i < 10; i++ )
262 //       allTypesMap.Add( i );
263 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
264     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
265   }
266   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
267     return new SMESH_TypeFilter( MESHorSUBMESH );
268   else if ( theId == SMESHGUI_MeshDlg::Mesh )
269     return new SMESH_TypeFilter( MESH );
270   else
271     return 0;
272 }
273
274 //================================================================================
275 /*!
276  * \brief check if selected shape is a subshape of the shape to mesh
277   * \retval bool - check result
278  */
279 //================================================================================
280
281 bool SMESHGUI_MeshOp::isSubshapeOk() const
282 {
283   if ( !myToCreate || myIsMesh ) // not submesh creation
284     return false;
285
286   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
287   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
288   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
289   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
290   if ( pMesh && pGeom ) {
291     SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
292     if ( !mesh->_is_nil() ) {
293       GEOM::GEOM_Object_var mainGeom, subGeom;
294       mainGeom = mesh->GetShapeToMesh();
295       subGeom  = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
296       if ( !mainGeom->_is_nil() && !subGeom->_is_nil() ) {
297         TopoDS_Shape mainShape, subShape;
298         if ( GEOMBase::GetShape( mainGeom, mainShape ) &&
299              GEOMBase::GetShape( subGeom, subShape ) )
300           // 1 is index of mainShape itself
301           return GEOMBase::GetIndex( subShape, mainShape, 0 ) > 1;
302       }
303     }
304   }
305   return false;
306 }
307
308 //================================================================================
309 /*!
310  * \brief find an existing submesh by the selected shape
311   * \retval _PTR(SObject) - the found submesh SObject
312  */
313 //================================================================================
314
315 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
316 {
317   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
318   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
319   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
320   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
321   if ( pMesh && pGeom ) {
322     GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
323     if ( !geom->_is_nil() ) {
324       int tag = -1;
325       switch ( geom->GetShapeType() ) {
326       case GEOM::EDGE:     tag = SUBMESH_ON_EDGE_TAG    ; break;
327       case GEOM::WIRE:     tag = SUBMESH_ON_WIRE_TAG    ; break;
328       case GEOM::FACE:     tag = SUBMESH_ON_FACE_TAG    ; break;
329       case GEOM::SHELL:    tag = SUBMESH_ON_SHELL_TAG   ; break;
330       case GEOM::SOLID:    tag = SUBMESH_ON_SOLID_TAG   ; break;
331       case GEOM::COMPOUND: tag = SUBMESH_ON_COMPOUND_TAG; break;
332       default:;
333       }
334       _PTR(SObject) aSubmeshRoot;
335       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
336       if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
337       {
338         _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
339         for (; smIter->More(); smIter->Next() )
340         {
341           _PTR(SObject) aSmObj = smIter->Value();
342           _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
343           for (; anIter1->More(); anIter1->Next()) {
344             _PTR(SObject) pGeom2 = anIter1->Value();
345             if ( pGeom2->ReferencedObject( pGeom2 ) &&
346                  pGeom2->GetID() == pGeom->GetID() )
347               return aSmObj;
348           }
349         }
350       }
351     }     
352   }
353   return _PTR(SObject)();
354 }
355
356 //================================================================================
357 /*!
358  * \brief Updates dialog's look and feel
359  *
360  * Virtual method redefined from the base class updates dialog's look and feel
361  */
362 //================================================================================
363 void SMESHGUI_MeshOp::selectionDone()
364 {
365   SMESHGUI_SelectionOp::selectionDone();
366
367   try
368   {
369     if ( !myToCreate ) // edition
370     {
371       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
372       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
373       if ( pObj != 0 )
374       {
375         SMESH::SMESH_subMesh_var aVar =
376           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
377         myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
378         myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
379         myDlg->updateGeometry();
380         myDlg->adjustSize();
381         readMesh();
382       }
383       else
384         myDlg->reset();
385
386     }
387     else if ( !myIsMesh ) // submesh creation
388     {
389       // if a submesh on the selected shape already exist, pass to submesh edition mode
390       if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
391         SMESH::SMESH_subMesh_var sm = 
392           SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
393         bool editSubmesh = ( !sm->_is_nil() &&
394                              SUIT_MessageBox::question2( myDlg, tr( "SMESH_WARNING" ),
395                                                          tr( "EDIT_SUBMESH_QUESTION"),
396                                                          tr( "SMESH_BUT_YES" ),
397                                                          tr( "SMESH_BUT_NO" ), 1, 0, 0 ));
398         if ( editSubmesh )
399         {
400           selectionMgr()->clearFilters();
401           selectObject( pSubmesh );
402           SMESHGUI::GetSMESHGUI()->switchToOperation(704);
403         }
404         else
405         {
406           selectObject( _PTR(SObject)() );
407           myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
408         }
409       }
410       // enable/disable popup for choice of geom selection way
411       myDlg->setGeomPopupEnabled( !myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh ).isEmpty() );
412
413     }
414   }
415   catch ( const SALOME::SALOME_Exception& S_ex )
416   {
417     SalomeApp_Tools::QtCatchCorbaException( S_ex );
418   }
419   catch ( ... )
420   {
421   }
422
423
424   // Enable tabs according to shape dimension
425
426   int shapeDim = 3;
427   try
428   {
429     GEOM::GEOM_Object_var aGeomVar;
430     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
431     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
432     if ( pGeom ) {
433       aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
434     }
435     else {
436       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
437       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
438       aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
439     }
440     if ( !aGeomVar->_is_nil() ) {
441       shapeDim = 0;
442       switch ( aGeomVar->GetShapeType() ) {
443       case GEOM::SOLID:
444       case GEOM::SHELL:  shapeDim = 3; break;
445       case GEOM::FACE:   shapeDim = 2; break;
446       case GEOM::WIRE:   
447       case GEOM::EDGE:   shapeDim = 1; break;
448       case GEOM::VERTEX: shapeDim = 0; break;
449       default:
450         TopoDS_Shape aShape;
451         if ( GEOMBase::GetShape(aGeomVar, aShape)) {
452           TopExp_Explorer exp( aShape, TopAbs_SHELL );
453           if ( exp.More() )
454             shapeDim = 3;
455           else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
456             shapeDim = 2;
457           else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
458             shapeDim = 1;
459           else
460             shapeDim = 0;
461         }
462       }
463     }
464   }
465   catch ( const SALOME::SALOME_Exception& S_ex )
466   {
467     SalomeApp_Tools::QtCatchCorbaException( S_ex );
468   }
469   catch ( ... )
470   {
471   }
472   myDlg->setMaxHypoDim( shapeDim );
473 }
474
475 //================================================================================
476 /*!
477  * \brief Verifies validity of input data
478   * \param theMess - Output parameter intended for returning error message
479   * \retval bool  - TRUE if input data is valid, false otherwise
480  *
481  * Verifies validity of input data. This method is called when "Apply" or "OK" button
482  * is pressed before mesh creation or editing.
483  */
484 //================================================================================
485 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
486 {
487   // Selected object to be  edited
488   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
489   {
490     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
491     return false;
492   }
493
494   // Name
495   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
496   aMeshName = aMeshName.stripWhiteSpace();
497   if ( aMeshName == "" )
498   {
499     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
500     return false;
501   }
502
503   // Geom
504   if ( myToCreate )
505   {
506     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
507     if ( aGeomEntry == "" )
508     {
509       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
510       return false;
511     }
512     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
513     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
514     {
515       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
516       return false;
517     }
518
519     // Mesh
520     if ( !myIsMesh ) // i.e sub-mesh creation,
521     {
522       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
523       if ( aMeshEntry == "" )
524       {
525         theMess = tr( "MESH_IS_NOT_DEFINED" );
526         return false;
527       }
528       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
529       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
530       {
531         theMess = tr( "MESH_IS_NULL" );
532         return false;
533       }
534       if ( !isSubshapeOk() )
535       {
536         theMess = tr( "INVALID_SUBSHAPE" );
537         return false;
538       }
539     }
540   }
541   return true;
542 }
543
544 //================================================================================
545 /*!
546  * \brief Gets available hypotheses or algorithms
547   * \param theDim - specifies dimension of returned hypotheses/algorifms
548   * \param theHypType - specifies whether algorims or hypotheses or additional ones
549   * are retrieved (possible values are in HypType enumeration)
550   * \param theHyps - Output list of hypotheses' names
551  *
552  * Gets available hypotheses or algorithm in accordance with input parameters
553  */
554 //================================================================================
555 void SMESHGUI_MeshOp::availableHyps( const int theDim,
556                                      const int theHypType,
557                                      QStringList& theHyps ) const
558 {
559   theHyps.clear();
560   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses(
561     theHypType == Algo , theDim, theHypType == AddHyp );
562   QStringList::const_iterator anIter;
563   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
564   {
565     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
566     theHyps.append( aData->Label );
567   }
568 }
569
570 //================================================================================
571 /*!
572  * \brief Gets existing hypotheses or algorithms
573   * \param theDim - specifies dimension of returned hypotheses/algorifms
574   * \param theHypType - specifies whether algorims or hypotheses or additional ones
575   * are retrieved (possible values are in HypType enumeration)
576   * \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
577   * \param theHyps - output list of names.
578   * \param theHypVars - output list of variables.
579  *
580  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
581  * input parameters
582  */
583 //================================================================================
584 void SMESHGUI_MeshOp::existingHyps( const int theDim,
585                                     const int theHypType,
586                                     _PTR(SObject) theFather,
587                                     QStringList& theHyps,
588                                     QValueList<SMESH::SMESH_Hypothesis_var>& theHypVars )
589 {
590   // Clear hypoheses list
591   theHyps.clear();
592   theHypVars.clear();
593
594   if ( !theFather )
595     return;
596
597   _PTR(SObject)          aHypRoot;
598   _PTR(GenericAttribute) anAttr;
599   _PTR(AttributeName)    aName;
600   _PTR(AttributeIOR)     anIOR;
601
602   bool isMesh = !_CAST( SComponent, theFather );
603   int aPart = -1;
604   if ( isMesh )
605     aPart = theHypType == Algo ? GLOBAL_ALGO_TAG : GLOBAL_HYPO_TAG;
606   else
607     aPart = theHypType == Algo ? LOCAL_ALGO_TAG : LOCAL_HYPO_TAG;
608
609   if ( theFather->FindSubObject( aPart, aHypRoot ) )
610   {
611     _PTR(ChildIterator) anIter =
612       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
613     for (; anIter->More(); anIter->Next() )
614     {
615       _PTR(SObject) anObj = anIter->Value();
616       if ( isMesh ) // i.e. mesh or submesh
617       {
618         _PTR(SObject) aRefObj;
619         if ( anObj->ReferencedObject( aRefObj ) )
620           anObj = aRefObj;
621         else
622           continue;
623       }
624       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
625       {
626         aName = anAttr;
627         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
628         if ( !CORBA::is_nil( aVar ) )
629         {
630           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
631           if ( !aHypVar->_is_nil() )
632           {
633             QString aHypType( aHypVar->GetName() );
634             HypothesisData* aData = SMESH::GetHypothesisData( aHypType );
635             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
636                  ( theHypType == AddHyp ) == aData->IsAux )
637             {
638               theHyps.append( aName->Value().c_str() );
639               theHypVars.append( aHypVar );
640             }
641           }
642         }
643       }
644     }
645   }
646 }
647
648 //================================================================================
649 /*!
650  * \brief If create or edit a submesh, return a hypothesis holding parameters used
651  *        to mesh a subshape
652   * \param aHypType - The hypothesis type name
653   * \param aServerLib - Server library name
654   * \param hypData - The structure holding the hypothesis type etc.
655   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
656  */
657 //================================================================================
658
659 SMESH::SMESH_Hypothesis_var
660 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
661                                           const QString& aServerLib ) const
662 {
663   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
664     return SMESH::SMESH_Hypothesis::_nil();
665
666   const int nbColonsInMeshEntry = 3;
667   bool isSubMesh = myToCreate ?
668     !myIsMesh :
669     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).contains(':') > nbColonsInMeshEntry; 
670
671   if ( isSubMesh )
672   {
673     // get mesh and geom object
674     SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
675     GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
676
677     QString anEntry = myDlg->selectedObject
678       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
679     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.latin1() ))
680     {
681       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
682       if ( myToCreate ) // mesh and geom may be selected
683       {
684         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
685         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
686         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.latin1() ))
687           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
688       }
689       else // edition: sub-mesh may be selected
690       {
691         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
692         if ( !sm->_is_nil() ) {
693           aMeshVar = sm->GetFather();
694           aGeomVar = sm->GetSubShape();
695         }
696       }
697     }
698
699     if ( !aMeshVar->_is_nil() && !aGeomVar->_is_nil() )
700       return SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType,
701                                                                     aServerLib,
702                                                                     aMeshVar,
703                                                                     aGeomVar );
704   }
705   return SMESH::SMESH_Hypothesis::_nil();
706 }
707
708 //================================================================================
709 /*!
710  * \brief Calls plugin methods for hypothesis creation
711   * \param theHypType - specifies whether main hypotheses or additional ones
712   * are created
713   * \param theIndex - index of type of hypothesis to be cerated
714  *
715  * Speicfies dimension of hypothesis to be created (using sender() method), specifies
716  * its type and calls plugin methods for hypothesis creation
717  */
718 //================================================================================
719 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
720 {
721   // Speicfies dimension of hypothesis to be created
722   const QObject* aSender = sender();
723   int aDim = -1;
724   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
725     if ( aSender == myDlg->tab( i ) )
726       aDim = i;
727   if ( aDim == -1 )
728     return;
729
730   // Speicfies type of hypothesis to be created
731   QStringList aHypTypeNames = SMESH::GetAvailableHypotheses( false , aDim, theHypType == AddHyp );
732   if ( theIndex < 0 || theIndex >= aHypTypeNames.count() )
733     return;
734
735   QString aHypTypeName = aHypTypeNames[ theIndex ];
736   HypothesisData* aData = SMESH::GetHypothesisData( aHypTypeName.latin1() );
737   if ( aData == 0 )
738     return;
739
740   QString aClientLibName = aData->ClientLibName;
741   QStringList anOldHyps;
742   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
743   existingHyps( aDim, theHypType, aFather, anOldHyps, myExistingHyps[ aDim ][ theHypType ] );
744
745   if ( aClientLibName == "" )
746   {
747     // Call hypothesis creation server method (without GUI)
748     QString aHypName = aData->Label;
749     SMESH::CreateHypothesis( aHypTypeName, aHypName, false );
750   }
751   else
752   {
753     // Get hypotheses creator client (GUI)
754     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aHypTypeName );
755
756     // Create hypothesis
757     if ( aCreator )
758     {
759       // When create or edit a submesh, try to initialize a new hypothesis
760       // with values used to mesh a subshape
761       SMESH::SMESH_Hypothesis_var initParamHyp =
762         getInitParamsHypothesis( aHypTypeName, aData->ServerLibName );
763
764       if ( initParamHyp->_is_nil() )
765         aCreator->create( false, myDlg );
766       else
767         aCreator->create( initParamHyp, myDlg );
768     }
769     else
770     {
771       SMESH::CreateHypothesis( aHypTypeName, aData->Label, false );
772     }
773   }
774
775   QStringList aNewHyps;
776   aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
777   existingHyps( aDim, theHypType, aFather, aNewHyps, myExistingHyps[ aDim ][ theHypType ] );
778   if ( aNewHyps.count() > anOldHyps.count() )
779   {
780     for ( int i = anOldHyps.count(); i < aNewHyps.count(); i++ )
781       myDlg->tab( aDim )->addHyp( theHypType, aNewHyps[ i ] );
782   }
783 }
784
785 //================================================================================
786 /*!
787  * \brief Calls plugin methods for hypothesis editing
788   * \param theHypType - specifies whether main hypothesis or additional one
789   * is edited
790   * \param theIndex - index of existing hypothesis
791  *
792  * Calls plugin methods for hypothesis editing
793  */
794 //================================================================================
795 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
796 {
797   // Speicfies dimension of hypothesis to be created
798   const QObject* aSender = sender();
799   int aDim = -1;
800   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
801     if ( aSender == myDlg->tab( i ) )
802       aDim = i;
803   if ( aDim == -1 )
804     return;
805
806   QValueList<SMESH::SMESH_Hypothesis_var> aList = myExistingHyps[ aDim ][ theHypType ];
807   SMESH::SMESH_Hypothesis_var aHyp = aList[ theIndex - 1 ];
808   if ( aHyp->_is_nil() )
809     return;
810
811   char* aTypeName = aHyp->GetName();
812   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aTypeName );
813   if ( aCreator )
814     aCreator->edit( aHyp.in(), dlg() );
815 }
816
817 //================================================================================
818 /*!
819  * \brief Creates and selects hypothesis of hypotheses set 
820   * \param theSetName - The name of hypotheses set
821  */
822 //================================================================================
823
824 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
825 {
826   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet( theSetName );
827   if ( !aHypoSet ) return;
828
829   for ( int aHypType = Algo; aHypType < AddHyp; aHypType++ )
830   {
831     bool isAlgo = (aHypType == Algo);
832
833     // clear all hyps
834     for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
835       setCurrentHyp( dim, aHypType, -1 );
836
837     // set hyps from the set
838     
839     QStringList* aHypoList = isAlgo ? & aHypoSet->AlgoList : & aHypoSet->HypoList;
840     for ( int i = 0, n = aHypoList->count(); i < n; i++ )
841     {
842       const QString& aHypoTypeName = (*aHypoList)[ i ];
843       HypothesisData* aHypData = SMESH::GetHypothesisData( aHypoTypeName );
844       if ( !aHypData )
845         continue;
846
847       int aDim = aHypData->Dim[0];
848       // create or/and set
849       int index = -1;
850       if ( isAlgo )
851       {
852         QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, aDim );
853         index = aHypTypeNameList.findIndex( aHypoTypeName );
854         if ( index < 0 ) continue;
855         setCurrentHyp ( aDim, aHypType, index );
856       }
857       else
858       {
859         // try to find an existing hypo
860         QValueList<SMESH::SMESH_Hypothesis_var> & aList = myExistingHyps[ aDim ][ aHypType ];
861         int /*iHyp = 0,*/ nbHyp = aList.count();
862 //         for ( ; iHyp < nbHyp; ++iHyp )
863 //         {
864 //           SMESH::SMESH_Hypothesis_var aHyp = aList[ iHyp ];
865 //           if ( !aHyp->_is_nil() && aHypoTypeName == aHyp->GetName() ) {
866 //             index = iHyp;
867 //             break;
868 //           }
869 //         }
870         if ( index >= 0 ) // found
871         {
872           // select the found hypothesis
873           setCurrentHyp ( aDim, aHypType, index );
874         }
875         else
876         {
877           // create a hypothesis
878           QString aClientLibName = aHypData->ClientLibName;
879           if ( aClientLibName == "" ) {
880             // Call hypothesis creation server method (without GUI)
881             SMESH::CreateHypothesis( aHypoTypeName, aHypData->Label, isAlgo );
882           }
883           else {
884             // Get hypotheses creator client (GUI)
885             SMESHGUI_GenericHypothesisCreator* aCreator =
886               SMESH::GetHypothesisCreator( aHypoTypeName );
887             if ( aCreator )
888             {
889               // When create or edit a submesh, try to initialize a new hypothesis
890               // with values used to mesh a subshape
891               SMESH::SMESH_Hypothesis_var initParamHyp =
892                 getInitParamsHypothesis( aHypoTypeName, aHypData->ServerLibName );
893               aCreator->create( initParamHyp, myDlg );
894             }
895             else
896             {
897               SMESH::CreateHypothesis( aHypoTypeName, aHypData->Label, isAlgo );
898             }
899           }
900           QStringList aNewHyps;
901           _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
902           existingHyps( aDim, aHypType, aFather, aNewHyps, aList );
903           if ( aList.count() > nbHyp )
904           {
905             for ( int i = nbHyp; i < aNewHyps.count(); i++ )
906               myDlg->tab( aDim )->addHyp( aHypType, aNewHyps[ i ] );
907           }
908         }
909       }
910     } // loop on hypos in the set
911   } // loop on algo/hypo
912 }
913
914 //================================================================================
915 /*!
916  * \brief Creates mesh
917   * \param theMess - Output parameter intended for returning error message
918   * \retval bool  - TRUE if mesh is created, FALSE otherwise
919  *
920  * Creates mesh
921  */
922 //================================================================================
923 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
924 {
925   theMess = "";
926
927   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
928   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
929   GEOM::GEOM_Object_var aGeomVar =
930     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
931
932   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
933   if ( aSMESHGen->_is_nil() )
934     return false;
935
936   SUIT_OverrideCursor aWaitCursor;
937
938   // create mesh
939   SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
940   if ( aMeshVar->_is_nil() )
941     return false;
942   _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
943   if ( aMeshSO )
944     SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
945
946   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
947   {
948     if ( !isAccessibleDim( aDim )) continue;
949
950     // assign hypotheses
951     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
952     {
953       int aHypIndex = currentHyp( aDim, aHypType );
954       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
955       {
956         SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
957         if ( !aHypVar->_is_nil() )
958           SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
959       }
960     }
961     // find or create algorithm
962     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
963     if ( !anAlgoVar->_is_nil() )
964       SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
965   }
966   return true;
967 }
968
969 //================================================================================
970 /*!
971  * \brief Creates sub-mesh
972   * \param theMess - Output parameter intended for returning error message
973   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
974  *
975  * Creates sub-mesh
976  */
977 //================================================================================
978 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
979 {
980   theMess = "";
981
982   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
983   if ( aSMESHGen->_is_nil() )
984     return false;
985
986   // get mesh object
987   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
988   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
989   SMESH::SMESH_Mesh_var aMeshVar =
990     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
991
992   // get geom object
993   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
994   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
995   GEOM::GEOM_Object_var aGeomVar =
996     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
997
998   SUIT_OverrideCursor aWaitCursor;
999
1000   // create sub-mesh
1001   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
1002   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.latin1() );
1003
1004   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
1005   {
1006     if ( !isAccessibleDim( aDim )) continue;
1007
1008     // find or create algorithm
1009     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1010     if ( !anAlgoVar->_is_nil() )
1011       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1012     // assign hypotheses
1013     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1014     {
1015       int aHypIndex = currentHyp( aDim, aHypType );
1016       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1017       {
1018         SMESH::SMESH_Hypothesis_var aHypVar =
1019           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1020         if ( !aHypVar->_is_nil() )
1021           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1022       }
1023     }
1024   }
1025   return true;
1026 }
1027
1028 //================================================================================
1029 /*!
1030  * \brief Gets current hypothesis or algorithms
1031   * \param theDim - dimension of hypothesis or algorithm
1032   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1033   * \retval int - current hypothesis or algorithms
1034  *
1035  * Gets current hypothesis or algorithms
1036  */
1037 //================================================================================
1038 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1039 {
1040   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1041 }
1042
1043 //================================================================================
1044 /*!
1045  * \brief Returns true if hypotheses of given dim can be assigned
1046   * \param theDim - hypotheses dimension
1047   * \retval bool - result
1048  */
1049 //================================================================================
1050 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim) const
1051 {
1052   return myDlg->tab( theDim )->isEnabled();
1053 }
1054
1055 //================================================================================
1056 /*!
1057  * \brief Sets current hypothesis or algorithms
1058   * \param theDim - dimension of hypothesis or algorithm
1059   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1060   * \param theIndex - Index of hypothesis
1061  *
1062  * Gets current hypothesis or algorithms
1063  */
1064 //================================================================================
1065 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1066                                      const int theHypType,
1067                                      const int theIndex )
1068 {
1069   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1070 }
1071
1072 //================================================================================
1073 /*!
1074  * \brief Generates default and sets mesh/submesh name
1075  *
1076  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1077  */
1078 //================================================================================
1079 void SMESHGUI_MeshOp::setDefaultName() const
1080 {
1081   QString aResName;
1082
1083   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1084   int i = 1;
1085   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1086   _PTR(SObject) anObj;
1087   do
1088   {
1089     aResName = aPrefix + QString::number( i++ );
1090     anObj = aStudy->FindObject( aResName.latin1() );
1091   }
1092   while ( anObj );
1093
1094   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1095     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1096   aControl->setText( aResName );
1097 }
1098
1099 //================================================================================
1100 /*!
1101  * \brief Gets algorithm or creates it if necessary
1102   * \param theDim - specifies dimension of returned hypotheses/algorifms
1103   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1104  *
1105  * Gets algorithm or creates it if necessary
1106  */
1107 //================================================================================
1108 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1109 {
1110   SMESH::SMESH_Hypothesis_var anAlgoVar;
1111   int aHypIndex = currentHyp( theDim, Algo );
1112   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true, theDim, false );
1113   if ( aHypIndex < 0 || aHypIndex >= aHypTypeNameList.count() )
1114     return anAlgoVar;
1115   QString aHypName = aHypTypeNameList[ aHypIndex ];
1116   QValueList<SMESH::SMESH_Hypothesis_var>& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1117   QValueList<SMESH::SMESH_Hypothesis_var>::iterator anIter;
1118   for ( anIter = aHypVarList.begin(); anIter != aHypVarList.end(); anIter++ )
1119   {
1120     SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1121     if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1122     {
1123       anAlgoVar = aHypVar;
1124       break;
1125     }
1126   }
1127   if ( anAlgoVar->_is_nil() )
1128   {
1129     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1130     if ( aHypData )
1131     {
1132       QString aClientLibName = aHypData->ClientLibName;
1133       if ( aClientLibName == "" )
1134         SMESH::CreateHypothesis( aHypName, aHypData->Label, true );
1135       else
1136       {
1137         SMESHGUI_GenericHypothesisCreator* aCreator =
1138           SMESH::GetHypothesisCreator( aHypName );
1139         if ( aCreator )
1140           aCreator->create( true, myDlg );
1141       }
1142       QStringList tmpList;
1143       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
1144       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
1145     }
1146
1147     QValueList<SMESH::SMESH_Hypothesis_var>& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
1148     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
1149     {
1150       SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1151       if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1152       {
1153         anAlgoVar = aHypVar;
1154         break;
1155       }
1156     }
1157   }
1158
1159   return anAlgoVar._retn();
1160 }
1161
1162 //================================================================================
1163 /*!
1164  * \brief Reads parameters of edited mesh and assigns them to the dialog
1165  *
1166  * Reads parameters of edited mesh and assigns them to the dialog (called when
1167  * mesh is edited only)
1168  */
1169 //================================================================================
1170 void SMESHGUI_MeshOp::readMesh()
1171 {
1172   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1173   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1174   if ( !pObj )
1175     return;
1176
1177   // Get name of mesh if current object is sub-mesh
1178   SMESH::SMESH_subMesh_var aSubMeshVar =
1179       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1180   if ( !aSubMeshVar->_is_nil() )
1181   {
1182     SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1183     if ( !aMeshVar->_is_nil() )
1184     {
1185       _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1186       QString aMeshName = name( aMeshSO );
1187       myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
1188     }
1189   }
1190
1191   // Get name of geometry object
1192   GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1193   if ( !aGeomVar->_is_nil() )
1194   {
1195     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
1196     QString aShapeName = name( aGeomSO );
1197     myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
1198   }
1199
1200   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
1201   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1202   {
1203     // get algorithm
1204     QStringList anExisting;
1205     int aHypIndex = -1;
1206     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
1207     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
1208     {
1209       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first();
1210       QString aHypTypeName = aVar->GetName();
1211
1212       QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true , dim, false );
1213       for ( int i = 0, n = aHypTypeNameList.count(); i < n; i++ )
1214         if ( aHypTypeName == aHypTypeNameList[ i ] )
1215         {
1216           aHypIndex = i;
1217           break;
1218         }
1219     }
1220     setCurrentHyp( dim, Algo, aHypIndex );
1221
1222     // get hypotheses
1223     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1224     {
1225       // get hypotheses
1226       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1227       // find index of requered hypothesis among existing ones for this dimension
1228       // and hyp types
1229       int aHypIndex = -1;
1230       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1231         aHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1232                           myExistingHyps[ dim ][ hypType ] );
1233       setCurrentHyp( dim, hypType, aHypIndex );
1234     }
1235   }
1236 }
1237
1238 //================================================================================
1239 /*!
1240  * \brief Gets name of object
1241   * \param theSO - SObject
1242   * \retval QString - name of object
1243  *
1244  * Gets name of object
1245  */
1246 //================================================================================
1247 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1248 {
1249   QString aResName;
1250   if ( theSO )
1251   {
1252     _PTR(GenericAttribute) anAttr;
1253     _PTR(AttributeName)    aNameAttr;
1254     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1255     {
1256       aNameAttr = anAttr;
1257       aResName = aNameAttr->Value().c_str();
1258     }
1259   }
1260   return aResName;
1261 }
1262
1263 //================================================================================
1264 /*!
1265  * \brief Finds hypothesis in input list
1266   * \param theHyp - hypothesis to be found
1267   * \param theHypList - input list of hypotheses
1268   * \retval int - index of hypothesis or -1 if it is not found
1269  *
1270  * Finds position of hypothesis in input list
1271  */
1272 //================================================================================
1273 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1274                            const QValueList<SMESH::SMESH_Hypothesis_var>& theHypList ) const
1275 {
1276   int aRes = -1;
1277   if ( !theHyp->_is_nil() )
1278   {
1279     int i = 0;
1280     QValueList<SMESH::SMESH_Hypothesis_var>::const_iterator anIter;
1281     for ( anIter = theHypList.begin(); anIter != theHypList.end(); ++ anIter )
1282     {
1283       if ( theHyp->_is_equivalent( *anIter ) )
1284       {
1285         aRes = i;
1286         break;
1287       }
1288       i++;
1289     }
1290   }
1291   return aRes;
1292 }
1293
1294 //================================================================================
1295 /*!
1296  * \brief Edits mesh or sub-mesh
1297   * \param theMess - Output parameter intended for returning error message
1298   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1299  *
1300  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1301  */
1302 //================================================================================
1303 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1304 {
1305   theMess = "";
1306
1307   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1308   if ( aSMESHGen->_is_nil() )
1309     return false;
1310
1311   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1312   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1313   if ( !pObj )
1314     return false;
1315
1316   SUIT_OverrideCursor aWaitCursor;
1317
1318   // Set new name
1319   SMESH::SetName( pObj, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1320
1321   // Assign new hypotheses and algorithms
1322   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1323   {
1324     if ( !isAccessibleDim( dim )) continue;
1325
1326     // find or create algorithm
1327     bool toDelete = false, toAdd = true;
1328     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1329     if ( anAlgoVar->_is_nil() ) {
1330       toAdd = false;
1331     }
1332     if ( myObjHyps[ dim ][ Algo ].count() > 0 ) {
1333       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first();
1334       if ( toAdd ) {
1335         if ( strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) == 0 ) {
1336           toAdd = false;
1337         } else {
1338           toDelete = true;
1339         }
1340       } else {
1341         toDelete = true;
1342       }
1343     }
1344     // remove old algorithm
1345     if ( toDelete )
1346       SMESH::RemoveHypothesisOrAlgorithmOnMesh
1347         ( pObj, myObjHyps[ dim ][ Algo ].first() );
1348
1349     // assign new algorithm
1350     if ( toAdd ) {
1351       SMESH::SMESH_Mesh_var aMeshVar =
1352         SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1353       bool isMesh = !aMeshVar->_is_nil();
1354       if ( isMesh ) {
1355         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1356       } else {
1357         SMESH::SMESH_subMesh_var aVar =
1358           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1359         if ( !aVar->_is_nil() )
1360           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1361       }
1362     }
1363
1364     // assign hypotheses
1365     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1366     {
1367       int aNewHypIndex = currentHyp( dim, hypType );
1368       int anOldHypIndex = -1;
1369       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1370         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1371                               myExistingHyps[ dim ][ hypType ] );
1372       if ( aNewHypIndex != anOldHypIndex )
1373       {
1374         // remove old hypotheses
1375         if ( anOldHypIndex >= 0 )
1376           SMESH::RemoveHypothesisOrAlgorithmOnMesh(
1377             pObj, myExistingHyps[ dim ][ hypType ][ anOldHypIndex ] );
1378
1379         // assign new hypotheses
1380         if ( aNewHypIndex != -1 )
1381         {
1382           SMESH::SMESH_Mesh_var aMeshVar =
1383               SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1384           bool isMesh = !aMeshVar->_is_nil();
1385           if ( isMesh )
1386           {
1387             SMESH::AddHypothesisOnMesh(
1388               aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1389           }
1390           else
1391           {
1392             SMESH::SMESH_subMesh_var aVar =
1393               SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1394             if ( !aVar->_is_nil() )
1395               SMESH::AddHypothesisOnSubMesh(
1396                 aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1397           }
1398         }
1399         // reread all hypotheses of mesh if necessary
1400         QStringList anExisting;
1401         existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1402       }
1403     }
1404   }
1405
1406   return true;
1407 }
1408
1409 //================================================================================
1410 /*!
1411  * \brief Verifies whether given operator is valid for this one
1412   * \param theOtherOp - other operation
1413   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1414 *
1415 * method redefined from base class verifies whether given operator is valid for
1416 * this one (i.e. can be started "above" this operator). In current implementation method
1417 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1418 * elements.
1419 */
1420 //================================================================================
1421 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1422 {
1423   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1424 }
1425
1426 //================================================================================
1427 /*!
1428  * \brief SLOT. Is called when the user selects a way of geometry selection
1429   * \param theByMesh - true if the user wants to find geometry by mesh element
1430  */
1431 //================================================================================
1432
1433 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
1434 {
1435   if ( theByMesh ) {
1436     if ( !myShapeByMeshDlg ) {
1437       myShapeByMeshDlg = new SMESHGUI_ShapeByMeshDlg( SMESHGUI::GetSMESHGUI(), "ShapeByMeshDlg");
1438       connect(myShapeByMeshDlg, SIGNAL(PublishShape()), SLOT(onPublishShapeByMeshDlg()));
1439       connect(myShapeByMeshDlg, SIGNAL(Close()), SLOT(onCloseShapeByMeshDlg()));
1440     }
1441     // set mesh object to dlg
1442     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1443     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
1444       SMESH::SMESH_Mesh_var aMeshVar =
1445         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1446       if ( !aMeshVar->_is_nil() ) {
1447         myDlg->hide();
1448         myShapeByMeshDlg->Init();
1449         myShapeByMeshDlg->SetMesh( aMeshVar );
1450         myShapeByMeshDlg->show();
1451       }
1452     }
1453   }
1454 }
1455
1456 //================================================================================
1457 /*!
1458  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1459  */
1460 //================================================================================
1461
1462 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg()
1463 {
1464   onCloseShapeByMeshDlg();
1465
1466   if ( myShapeByMeshDlg ) {
1467     // Select a found geometry object
1468     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshDlg->GetShape();
1469     if ( !aGeomVar->_is_nil() )
1470     {
1471       QString ID = aGeomVar->GetStudyEntry();
1472       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID )) {
1473         selectObject( aGeomSO );
1474         selectionDone();
1475       }
1476     }
1477   }
1478 }
1479
1480 //================================================================================
1481 /*!
1482  * \brief SLOT. Is called Close Ok is pressed in SMESHGUI_ShapeByMeshDlg
1483  */
1484 //================================================================================
1485
1486 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg()
1487 {
1488   if ( myDlg ) {
1489     myDlg->show();
1490     myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
1491     myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
1492   }
1493 }
1494
1495 //================================================================================
1496 /*!
1497  * \brief Selects a SObject
1498   * \param theSObj - the SObject to select
1499  */
1500 //================================================================================
1501
1502 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
1503 {
1504   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
1505     SALOME_ListIO anIOList;
1506     if ( theSObj ) {
1507       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1508         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
1509       anIOList.Append( anIO );
1510     }
1511     sm->setSelectedObjects( anIOList, false );
1512   }
1513 }