Salome HOME
ac9183c07b7ecf2f37c724148e4ebbb34abb74b9
[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     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   selectionDone();
247
248   myDlg->setCurrentTab( SMESH::DIM_1D );
249   myDlg->show();
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 ( myShapeByMeshDlg && myShapeByMeshDlg->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 Calls plugin methods for hypothesis creation
749   * \param theHypType - specifies whether main hypotheses or additional ones
750   * are created
751   * \param theIndex - index of type of hypothesis to be cerated
752  *
753  * Speicfies dimension of hypothesis to be created (using sender() method), specifies
754  * its type and calls plugin methods for hypothesis creation
755  */
756 //================================================================================
757 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
758 {
759   // Speicfies dimension of hypothesis to be created
760   const QObject* aSender = sender();
761   int aDim = -1;
762   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
763     if ( aSender == myDlg->tab( i ) )
764       aDim = i;
765   if ( aDim == -1 )
766     return;
767
768   // Speicfies type of hypothesis to be created
769   QStringList aHypTypeNames = SMESH::GetAvailableHypotheses( false , aDim, theHypType == AddHyp );
770   if ( theIndex < 0 || theIndex >= aHypTypeNames.count() )
771     return;
772
773   QString aHypTypeName = aHypTypeNames[ theIndex ];
774   HypothesisData* aData = SMESH::GetHypothesisData( aHypTypeName.latin1() );
775   if ( aData == 0 )
776     return;
777
778   QString aClientLibName = aData->ClientLibName;
779   QStringList anOldHyps;
780   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
781   existingHyps( aDim, theHypType, aFather, anOldHyps, myExistingHyps[ aDim ][ theHypType ] );
782
783   if ( aClientLibName == "" )
784   {
785     // Call hypothesis creation server method (without GUI)
786     QString aHypName = aData->Label;
787     SMESH::CreateHypothesis( aHypTypeName, aHypName, false );
788   }
789   else
790   {
791     // Get hypotheses creator client (GUI)
792     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aHypTypeName );
793
794     // Create hypothesis
795     if ( aCreator )
796     {
797       // When create or edit a submesh, try to initialize a new hypothesis
798       // with values used to mesh a subshape
799       SMESH::SMESH_Hypothesis_var initParamHyp =
800         getInitParamsHypothesis( aHypTypeName, aData->ServerLibName );
801
802       if ( initParamHyp->_is_nil() )
803         aCreator->create( false, myDlg );
804       else
805         aCreator->create( initParamHyp, myDlg );
806     }
807     else
808     {
809       SMESH::CreateHypothesis( aHypTypeName, aData->Label, false );
810     }
811   }
812
813   QStringList aNewHyps;
814   aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
815   existingHyps( aDim, theHypType, aFather, aNewHyps, myExistingHyps[ aDim ][ theHypType ] );
816   if ( aNewHyps.count() > anOldHyps.count() )
817   {
818     for ( int i = anOldHyps.count(); i < aNewHyps.count(); i++ )
819       myDlg->tab( aDim )->addHyp( theHypType, aNewHyps[ i ] );
820   }
821 }
822
823 //================================================================================
824 /*!
825  * \brief Calls plugin methods for hypothesis editing
826   * \param theHypType - specifies whether main hypothesis or additional one
827   * is edited
828   * \param theIndex - index of existing hypothesis
829  *
830  * Calls plugin methods for hypothesis editing
831  */
832 //================================================================================
833 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
834 {
835   // Speicfies dimension of hypothesis to be created
836   const QObject* aSender = sender();
837   int aDim = -1;
838   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
839     if ( aSender == myDlg->tab( i ) )
840       aDim = i;
841   if ( aDim == -1 )
842     return;
843
844   QValueList<SMESH::SMESH_Hypothesis_var> aList = myExistingHyps[ aDim ][ theHypType ];
845   SMESH::SMESH_Hypothesis_var aHyp = aList[ theIndex - 1 ];
846   if ( aHyp->_is_nil() )
847     return;
848
849   char* aTypeName = aHyp->GetName();
850   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aTypeName );
851   if ( aCreator )
852     aCreator->edit( aHyp.in(), dlg() );
853 }
854
855 //================================================================================
856 /*!
857  * \brief Creates and selects hypothesis of hypotheses set 
858   * \param theSetName - The name of hypotheses set
859  */
860 //================================================================================
861
862 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
863 {
864   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet( theSetName );
865   if ( !aHypoSet ) return;
866
867   for ( int aHypType = Algo; aHypType < AddHyp; aHypType++ )
868   {
869     bool isAlgo = (aHypType == Algo);
870
871     // clear all hyps
872     for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
873       setCurrentHyp( dim, aHypType, -1 );
874
875     // set hyps from the set
876     
877     QStringList* aHypoList = isAlgo ? & aHypoSet->AlgoList : & aHypoSet->HypoList;
878     for ( int i = 0, n = aHypoList->count(); i < n; i++ )
879     {
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       int index = -1;
888       if ( isAlgo )
889       {
890         QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, aDim );
891         index = aHypTypeNameList.findIndex( aHypoTypeName );
892         if ( index < 0 ) continue;
893         setCurrentHyp ( aDim, aHypType, index );
894       }
895       else
896       {
897         // try to find an existing hypo
898         QValueList<SMESH::SMESH_Hypothesis_var> & aList = myExistingHyps[ aDim ][ aHypType ];
899         int /*iHyp = 0,*/ nbHyp = aList.count();
900 //         for ( ; iHyp < nbHyp; ++iHyp )
901 //         {
902 //           SMESH::SMESH_Hypothesis_var aHyp = aList[ iHyp ];
903 //           if ( !aHyp->_is_nil() && aHypoTypeName == aHyp->GetName() ) {
904 //             index = iHyp;
905 //             break;
906 //           }
907 //         }
908         if ( index >= 0 ) // found
909         {
910           // select the found hypothesis
911           setCurrentHyp ( aDim, aHypType, index );
912         }
913         else
914         {
915           // create a hypothesis
916           QString aClientLibName = aHypData->ClientLibName;
917           if ( aClientLibName == "" ) {
918             // Call hypothesis creation server method (without GUI)
919             SMESH::CreateHypothesis( aHypoTypeName, aHypData->Label, isAlgo );
920           }
921           else {
922             // Get hypotheses creator client (GUI)
923             SMESHGUI_GenericHypothesisCreator* aCreator =
924               SMESH::GetHypothesisCreator( aHypoTypeName );
925             if ( aCreator )
926             {
927               // When create or edit a submesh, try to initialize a new hypothesis
928               // with values used to mesh a subshape
929               SMESH::SMESH_Hypothesis_var initParamHyp =
930                 getInitParamsHypothesis( aHypoTypeName, aHypData->ServerLibName );
931               aCreator->create( initParamHyp, myDlg );
932             }
933             else
934             {
935               SMESH::CreateHypothesis( aHypoTypeName, aHypData->Label, isAlgo );
936             }
937           }
938           QStringList aNewHyps;
939           _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
940           existingHyps( aDim, aHypType, aFather, aNewHyps, aList );
941           if ( aList.count() > nbHyp )
942           {
943             for ( int i = nbHyp; i < aNewHyps.count(); i++ )
944               myDlg->tab( aDim )->addHyp( aHypType, aNewHyps[ i ] );
945           }
946         }
947       }
948     } // loop on hypos in the set
949   } // loop on algo/hypo
950 }
951
952 //================================================================================
953 /*!
954  * \brief Creates mesh
955   * \param theMess - Output parameter intended for returning error message
956   * \retval bool  - TRUE if mesh is created, FALSE otherwise
957  *
958  * Creates mesh
959  */
960 //================================================================================
961 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
962 {
963   theMess = "";
964
965   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
966   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
967   GEOM::GEOM_Object_var aGeomVar =
968     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
969
970   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
971   if ( aSMESHGen->_is_nil() )
972     return false;
973
974   SUIT_OverrideCursor aWaitCursor;
975
976   // create mesh
977   SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
978   if ( aMeshVar->_is_nil() )
979     return false;
980   _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
981   if ( aMeshSO )
982     SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
983
984   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
985   {
986     if ( !isAccessibleDim( aDim )) continue;
987
988     // assign hypotheses
989     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
990     {
991       int aHypIndex = currentHyp( aDim, aHypType );
992       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
993       {
994         SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
995         if ( !aHypVar->_is_nil() )
996           SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
997       }
998     }
999     // find or create algorithm
1000     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1001     if ( !anAlgoVar->_is_nil() )
1002       SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1003   }
1004   return true;
1005 }
1006
1007 //================================================================================
1008 /*!
1009  * \brief Creates sub-mesh
1010   * \param theMess - Output parameter intended for returning error message
1011   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1012  *
1013  * Creates sub-mesh
1014  */
1015 //================================================================================
1016 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
1017 {
1018   theMess = "";
1019
1020   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1021   if ( aSMESHGen->_is_nil() )
1022     return false;
1023
1024   // get mesh object
1025   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1026   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
1027   SMESH::SMESH_Mesh_var aMeshVar =
1028     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1029
1030   // get geom object
1031   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1032   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
1033   GEOM::GEOM_Object_var aGeomVar =
1034     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1035
1036   SUIT_OverrideCursor aWaitCursor;
1037
1038   // create sub-mesh
1039   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
1040   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.latin1() );
1041
1042   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
1043   {
1044     if ( !isAccessibleDim( aDim )) continue;
1045
1046     // find or create algorithm
1047     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1048     if ( !anAlgoVar->_is_nil() )
1049       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1050     // assign hypotheses
1051     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1052     {
1053       int aHypIndex = currentHyp( aDim, aHypType );
1054       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1055       {
1056         SMESH::SMESH_Hypothesis_var aHypVar =
1057           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
1058         if ( !aHypVar->_is_nil() )
1059           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1060       }
1061     }
1062   }
1063   return true;
1064 }
1065
1066 //================================================================================
1067 /*!
1068  * \brief Gets current hypothesis or algorithms
1069   * \param theDim - dimension of hypothesis or algorithm
1070   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1071   * \retval int - current hypothesis or algorithms
1072  *
1073  * Gets current hypothesis or algorithms
1074  */
1075 //================================================================================
1076 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1077 {
1078   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1079 }
1080
1081 //================================================================================
1082 /*!
1083  * \brief Returns true if hypotheses of given dim can be assigned
1084   * \param theDim - hypotheses dimension
1085   * \retval bool - result
1086  */
1087 //================================================================================
1088 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim) const
1089 {
1090   return myDlg->tab( theDim )->isEnabled();
1091 }
1092
1093 //================================================================================
1094 /*!
1095  * \brief Sets current hypothesis or algorithms
1096   * \param theDim - dimension of hypothesis or algorithm
1097   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1098   * \param theIndex - Index of hypothesis
1099  *
1100  * Gets current hypothesis or algorithms
1101  */
1102 //================================================================================
1103 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1104                                      const int theHypType,
1105                                      const int theIndex )
1106 {
1107   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1108 }
1109
1110 //================================================================================
1111 /*!
1112  * \brief Generates default and sets mesh/submesh name
1113  *
1114  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1115  */
1116 //================================================================================
1117 void SMESHGUI_MeshOp::setDefaultName() const
1118 {
1119   QString aResName;
1120
1121   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1122   int i = 1;
1123   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1124   _PTR(SObject) anObj;
1125   do
1126   {
1127     aResName = aPrefix + QString::number( i++ );
1128     anObj = aStudy->FindObject( aResName.latin1() );
1129   }
1130   while ( anObj );
1131
1132   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1133     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1134   aControl->setText( aResName );
1135 }
1136
1137 //================================================================================
1138 /*!
1139  * \brief Gets algorithm or creates it if necessary
1140   * \param theDim - specifies dimension of returned hypotheses/algorifms
1141   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1142  *
1143  * Gets algorithm or creates it if necessary
1144  */
1145 //================================================================================
1146 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1147 {
1148   SMESH::SMESH_Hypothesis_var anAlgoVar;
1149   int aHypIndex = currentHyp( theDim, Algo );
1150   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true, theDim, false );
1151   if ( aHypIndex < 0 || aHypIndex >= aHypTypeNameList.count() )
1152     return anAlgoVar;
1153   QString aHypName = aHypTypeNameList[ aHypIndex ];
1154   QValueList<SMESH::SMESH_Hypothesis_var>& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1155   QValueList<SMESH::SMESH_Hypothesis_var>::iterator anIter;
1156   for ( anIter = aHypVarList.begin(); anIter != aHypVarList.end(); anIter++ )
1157   {
1158     SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1159     if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1160     {
1161       anAlgoVar = aHypVar;
1162       break;
1163     }
1164   }
1165   if ( anAlgoVar->_is_nil() )
1166   {
1167     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1168     if ( aHypData )
1169     {
1170       QString aClientLibName = aHypData->ClientLibName;
1171       if ( aClientLibName == "" )
1172         SMESH::CreateHypothesis( aHypName, aHypData->Label, true );
1173       else
1174       {
1175         SMESHGUI_GenericHypothesisCreator* aCreator =
1176           SMESH::GetHypothesisCreator( aHypName );
1177         if ( aCreator )
1178           aCreator->create( true, myDlg );
1179       }
1180       QStringList tmpList;
1181       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
1182       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
1183     }
1184
1185     QValueList<SMESH::SMESH_Hypothesis_var>& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
1186     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
1187     {
1188       SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
1189       if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
1190       {
1191         anAlgoVar = aHypVar;
1192         break;
1193       }
1194     }
1195   }
1196
1197   return anAlgoVar._retn();
1198 }
1199
1200 //================================================================================
1201 /*!
1202  * \brief Reads parameters of edited mesh and assigns them to the dialog
1203  *
1204  * Reads parameters of edited mesh and assigns them to the dialog (called when
1205  * mesh is edited only)
1206  */
1207 //================================================================================
1208 void SMESHGUI_MeshOp::readMesh()
1209 {
1210   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1211   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1212   if ( !pObj )
1213     return;
1214
1215   // Get name of mesh if current object is sub-mesh
1216   SMESH::SMESH_subMesh_var aSubMeshVar =
1217       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1218   if ( !aSubMeshVar->_is_nil() )
1219   {
1220     SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1221     if ( !aMeshVar->_is_nil() )
1222     {
1223       _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1224       QString aMeshName = name( aMeshSO );
1225       myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
1226     }
1227   }
1228
1229   // Get name of geometry object
1230   GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1231   if ( !aGeomVar->_is_nil() )
1232   {
1233     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
1234     QString aShapeName = name( aGeomSO );
1235     myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
1236   }
1237
1238   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
1239   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1240   {
1241     // get algorithm
1242     QStringList anExisting;
1243     int aHypIndex = -1;
1244     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
1245     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
1246     {
1247       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first();
1248       QString aHypTypeName = aVar->GetName();
1249
1250       QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true , dim, false );
1251       for ( int i = 0, n = aHypTypeNameList.count(); i < n; i++ )
1252         if ( aHypTypeName == aHypTypeNameList[ i ] )
1253         {
1254           aHypIndex = i;
1255           break;
1256         }
1257     }
1258     setCurrentHyp( dim, Algo, aHypIndex );
1259
1260     // get hypotheses
1261     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1262     {
1263       // get hypotheses
1264       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1265       // find index of requered hypothesis among existing ones for this dimension
1266       // and hyp types
1267       int aHypIndex = -1;
1268       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1269         aHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1270                           myExistingHyps[ dim ][ hypType ] );
1271       setCurrentHyp( dim, hypType, aHypIndex );
1272     }
1273   }
1274 }
1275
1276 //================================================================================
1277 /*!
1278  * \brief Gets name of object
1279   * \param theSO - SObject
1280   * \retval QString - name of object
1281  *
1282  * Gets name of object
1283  */
1284 //================================================================================
1285 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1286 {
1287   QString aResName;
1288   if ( theSO )
1289   {
1290     _PTR(GenericAttribute) anAttr;
1291     _PTR(AttributeName)    aNameAttr;
1292     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1293     {
1294       aNameAttr = anAttr;
1295       aResName = aNameAttr->Value().c_str();
1296     }
1297   }
1298   return aResName;
1299 }
1300
1301 //================================================================================
1302 /*!
1303  * \brief Finds hypothesis in input list
1304   * \param theHyp - hypothesis to be found
1305   * \param theHypList - input list of hypotheses
1306   * \retval int - index of hypothesis or -1 if it is not found
1307  *
1308  * Finds position of hypothesis in input list
1309  */
1310 //================================================================================
1311 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1312                            const QValueList<SMESH::SMESH_Hypothesis_var>& theHypList ) const
1313 {
1314   int aRes = -1;
1315   if ( !theHyp->_is_nil() )
1316   {
1317     int i = 0;
1318     QValueList<SMESH::SMESH_Hypothesis_var>::const_iterator anIter;
1319     for ( anIter = theHypList.begin(); anIter != theHypList.end(); ++ anIter )
1320     {
1321       if ( theHyp->_is_equivalent( *anIter ) )
1322       {
1323         aRes = i;
1324         break;
1325       }
1326       i++;
1327     }
1328   }
1329   return aRes;
1330 }
1331
1332 //================================================================================
1333 /*!
1334  * \brief Edits mesh or sub-mesh
1335   * \param theMess - Output parameter intended for returning error message
1336   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1337  *
1338  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1339  */
1340 //================================================================================
1341 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1342 {
1343   theMess = "";
1344
1345   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1346   if ( aSMESHGen->_is_nil() )
1347     return false;
1348
1349   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1350   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1351   if ( !pObj )
1352     return false;
1353
1354   SUIT_OverrideCursor aWaitCursor;
1355
1356   // Set new name
1357   SMESH::SetName( pObj, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1358
1359   // Assign new hypotheses and algorithms
1360   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1361   {
1362     if ( !isAccessibleDim( dim )) continue;
1363
1364     // find or create algorithm
1365     bool toDelete = false, toAdd = true;
1366     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1367     if ( anAlgoVar->_is_nil() ) {
1368       toAdd = false;
1369     }
1370     if ( myObjHyps[ dim ][ Algo ].count() > 0 ) {
1371       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first();
1372       if ( toAdd ) {
1373         if ( strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) == 0 ) {
1374           toAdd = false;
1375         } else {
1376           toDelete = true;
1377         }
1378       } else {
1379         toDelete = true;
1380       }
1381     }
1382     // remove old algorithm
1383     if ( toDelete )
1384       SMESH::RemoveHypothesisOrAlgorithmOnMesh
1385         ( pObj, myObjHyps[ dim ][ Algo ].first() );
1386
1387     // assign new algorithm
1388     if ( toAdd ) {
1389       SMESH::SMESH_Mesh_var aMeshVar =
1390         SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1391       bool isMesh = !aMeshVar->_is_nil();
1392       if ( isMesh ) {
1393         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1394       } else {
1395         SMESH::SMESH_subMesh_var aVar =
1396           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1397         if ( !aVar->_is_nil() )
1398           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1399       }
1400     }
1401
1402     // assign hypotheses
1403     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1404     {
1405       int aNewHypIndex = currentHyp( dim, hypType );
1406       int anOldHypIndex = -1;
1407       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1408         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1409                               myExistingHyps[ dim ][ hypType ] );
1410       if ( aNewHypIndex != anOldHypIndex )
1411       {
1412         // remove old hypotheses
1413         if ( anOldHypIndex >= 0 )
1414           SMESH::RemoveHypothesisOrAlgorithmOnMesh(
1415             pObj, myExistingHyps[ dim ][ hypType ][ anOldHypIndex ] );
1416
1417         // assign new hypotheses
1418         if ( aNewHypIndex != -1 )
1419         {
1420           SMESH::SMESH_Mesh_var aMeshVar =
1421               SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1422           bool isMesh = !aMeshVar->_is_nil();
1423           if ( isMesh )
1424           {
1425             SMESH::AddHypothesisOnMesh(
1426               aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1427           }
1428           else
1429           {
1430             SMESH::SMESH_subMesh_var aVar =
1431               SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1432             if ( !aVar->_is_nil() )
1433               SMESH::AddHypothesisOnSubMesh(
1434                 aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1435           }
1436         }
1437         // reread all hypotheses of mesh if necessary
1438         QStringList anExisting;
1439         existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1440       }
1441     }
1442   }
1443
1444   return true;
1445 }
1446
1447 //================================================================================
1448 /*!
1449  * \brief Verifies whether given operator is valid for this one
1450   * \param theOtherOp - other operation
1451   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1452 *
1453 * method redefined from base class verifies whether given operator is valid for
1454 * this one (i.e. can be started "above" this operator). In current implementation method
1455 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1456 * elements.
1457 */
1458 //================================================================================
1459 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1460 {
1461   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1462 }
1463
1464 //================================================================================
1465 /*!
1466  * \brief SLOT. Is called when the user selects a way of geometry selection
1467   * \param theByMesh - true if the user wants to find geometry by mesh element
1468  */
1469 //================================================================================
1470
1471 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
1472 {
1473   if ( theByMesh ) {
1474     if ( !myShapeByMeshDlg ) {
1475       myShapeByMeshDlg = new SMESHGUI_ShapeByMeshDlg( SMESHGUI::GetSMESHGUI(), "ShapeByMeshDlg");
1476       connect(myShapeByMeshDlg, SIGNAL(PublishShape()), SLOT(onPublishShapeByMeshDlg()));
1477       connect(myShapeByMeshDlg, SIGNAL(Close()), SLOT(onCloseShapeByMeshDlg()));
1478     }
1479     // set mesh object to dlg
1480     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1481     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() )) {
1482       SMESH::SMESH_Mesh_var aMeshVar =
1483         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1484       if ( !aMeshVar->_is_nil() ) {
1485         myDlg->hide();
1486         myDlg->activateObject( SMESHGUI_MeshDlg::Mesh );
1487         myShapeByMeshDlg->Init();
1488         myShapeByMeshDlg->SetMesh( aMeshVar );
1489         myShapeByMeshDlg->show();
1490       }
1491     }
1492   }
1493 }
1494
1495 //================================================================================
1496 /*!
1497  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
1498  */
1499 //================================================================================
1500
1501 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg()
1502 {
1503   if ( myShapeByMeshDlg ) {
1504     // Select a found geometry object
1505     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshDlg->GetShape();
1506     if ( !aGeomVar->_is_nil() )
1507     {
1508       QString ID = aGeomVar->GetStudyEntry();
1509       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.latin1() )) {
1510         SMESH::SMESH_Mesh_ptr aMeshPtr = myShapeByMeshDlg->GetMesh();
1511         if ( !CORBA::is_nil( aMeshPtr )) {
1512           if (_PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshPtr )) {
1513             myDlg->activateObject( SMESHGUI_MeshDlg::Mesh );
1514             myDlg->selectObject( aMeshSO->GetName().c_str(), SMESHGUI_MeshDlg::Mesh, aMeshSO->GetID().c_str() );
1515           }
1516         }
1517         myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
1518         selectObject( aGeomSO );
1519         //selectionDone();
1520       }
1521     }
1522     else {
1523       onCloseShapeByMeshDlg();
1524     }
1525   }
1526   myDlg->show();
1527 }
1528
1529 //================================================================================
1530 /*!
1531  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
1532  */
1533 //================================================================================
1534
1535 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg()
1536 {
1537   if ( myDlg ) {
1538     myDlg->show();
1539     myDlg->activateObject( SMESHGUI_MeshDlg::Geom );
1540     myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
1541   }
1542 }
1543
1544 //================================================================================
1545 /*!
1546  * \brief Selects a SObject
1547   * \param theSObj - the SObject to select
1548  */
1549 //================================================================================
1550
1551 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
1552 {
1553   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
1554     SALOME_ListIO anIOList;
1555     if ( theSObj ) {
1556       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
1557         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
1558       anIOList.Append( anIO );
1559     }
1560     sm->setSelectedObjects( anIOList, false );
1561   }
1562 }