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