Salome HOME
Join modifications from branch BR_3_1_0deb
[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 "SMESH_TypeFilter.hxx"
35 #include "SMESHGUI.h"
36
37 #include "SMESHGUI_HypothesesUtils.h"
38 #include "SMESHGUI_Hypotheses.h"
39 #include "SMESHGUI_Utils.h"
40 #include "SMESHGUI_GEOMGenUtils.h"
41
42 #include "SMESH_TypeFilter.hxx"
43 #include "SMESH_NumberFilter.hxx"
44
45 #include "GEOM_SelectionFilter.h"
46
47 #include "SalomeApp_Tools.h"
48 #include "SALOMEDSClient_Study.hxx"
49 #include "SALOMEDSClient_AttributeIOR.hxx"
50 #include "SALOMEDSClient_AttributeName.hxx"
51 #include "SALOMEDS_SComponent.hxx"
52 #include "SALOMEDS_SObject.hxx"
53
54
55 #include "LightApp_SelectionMgr.h"
56 #include "LightApp_UpdateFlags.h"
57 #include "SUIT_MessageBox.h"
58 #include "SUIT_Desktop.h"
59 #include "SUIT_OverrideCursor.h"
60
61 #include "GEOMBase.h"
62
63 #include "utilities.h"
64
65 #include <qstringlist.h>
66 #include <qlineedit.h>
67
68 #include <TopoDS_Shape.hxx>
69 #include <TopExp_Explorer.hxx>
70
71 //================================================================================
72 /*!
73  * \brief Constructor
74   * \param theToCreate - if this parameter is true then operation is used for creation,
75   * for editing otherwise
76  *
77  * Initialize operation
78 */
79 //================================================================================
80 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
81 : SMESHGUI_SelectionOp(),
82   myToCreate( theToCreate ),
83   myIsMesh( theIsMesh ),
84   myDlg( 0 )
85 {
86 }
87
88 //================================================================================
89 /*!
90  * \brief Destructor
91 */
92 //================================================================================
93 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
94 {
95   if( myDlg )
96     delete myDlg;
97 }
98
99 //================================================================================
100 /*!
101  * \brief Gets dialog of this operation
102   * \retval LightApp_Dialog* - pointer to dialog of this operation
103 */
104 //================================================================================
105 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
106 {
107   return myDlg;
108 }
109
110 //================================================================================
111 /*!
112  * \brief Creates or edits mesh
113   * \retval bool - TRUE if operation is performed successfully, FALSE otherwise
114  *
115  * Virtual slot redefined from the base class called when "Apply" button is clicked
116  * creates or edits mesh
117  */
118 //================================================================================
119 bool SMESHGUI_MeshOp::onApply()
120 {
121   if( isStudyLocked() )
122     return false;
123
124   QString aMess;
125   if ( !isValid( aMess ) )
126   {
127     if ( aMess != "" )
128       SUIT_MessageBox::warn1( myDlg,
129         tr( "SMESH_WRN_WARNING" ), aMess, tr( "SMESH_BUT_OK" ) );
130     return false;
131   }
132
133   bool aResult = false;
134   aMess = "";
135   try
136   {
137     if ( myToCreate && myIsMesh )
138       aResult = createMesh( aMess );
139     if ( myToCreate && !myIsMesh )
140       aResult = createSubMesh( aMess );
141     else if ( !myToCreate )
142       aResult = editMeshOrSubMesh( aMess );
143     if ( aResult )
144       update( UF_ObjBrowser | UF_Model );
145   }
146   catch ( const SALOME::SALOME_Exception& S_ex )
147   {
148     SalomeApp_Tools::QtCatchCorbaException( S_ex );
149     aResult = false;
150   }
151   catch ( ... )
152   {
153     aResult = false;
154   }
155
156   if ( aResult )
157   {
158     if ( myToCreate )
159       setDefaultName();
160   }
161   else
162   {
163     if ( aMess == "" )
164       aMess = tr( "SMESH_OPERATION_FAILED" );
165     SUIT_MessageBox::warn1( myDlg,
166       tr( "SMESH_ERROR" ), aMess, tr( "SMESH_BUT_OK" ) );
167   }
168
169   return aResult;
170 }
171
172 //================================================================================
173 /*!
174  * \brief Creates dialog if necessary and shows it
175  *
176  * Virtual method redefined from base class called when operation is started creates
177  * dialog if necessary and shows it, activates selection
178  */
179 //================================================================================
180 void SMESHGUI_MeshOp::startOperation()
181 {
182   if( !myDlg )
183   {
184     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
185     for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
186     {
187       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
188               this, SLOT( onCreateHyp( const int, const int) ) );
189       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
190               this, SLOT( onEditHyp( const int, const int) ) );
191     }
192     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
193   }
194   SMESHGUI_SelectionOp::startOperation();
195
196   // iterate through dimensions and get available and existing algoritms and hypotheses,
197   // set them to the dialog
198   int i, j;
199   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
200   for ( i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
201   {
202     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
203     QStringList anAvailable, anExisting;
204     for ( j = Algo; j <= AddHyp; j++ )
205     {
206       availableHyps( i, j, anAvailable );
207       existingHyps( i, j, aFather, anExisting, myExistingHyps[ i ][ j ] );
208
209       aTab->setAvailableHyps( j, anAvailable );
210       aTab->setExistingHyps( j, anExisting );
211     }
212   }
213   if ( myToCreate )
214   {
215     setDefaultName();
216     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
217   }
218   else
219     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
220
221   myDlg->setHypoSets( SMESH::GetHypothesesSets() );
222
223   selectionDone();
224
225   myDlg->setCurrentTab( SMESH::DIM_1D );
226   myDlg->show();
227 }
228
229 //================================================================================
230 /*!
231  * \brief Creates selection filter
232   * \param theId - identifier of current selection widget
233   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
234  *
235  * Creates selection filter in accordance with identifier of current selection widget
236  */
237 //================================================================================
238 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
239 {
240   if ( theId == SMESHGUI_MeshDlg::Geom )
241   {
242 //     TColStd_MapOfInteger allTypesMap;
243 //     for ( int i = 0; i < 10; i++ )
244 //       allTypesMap.Add( i );
245 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
246     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
247   }
248   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
249     return new SMESH_TypeFilter( MESHorSUBMESH );
250   else if ( theId == SMESHGUI_MeshDlg::Mesh )
251     return new SMESH_TypeFilter( MESH );
252   else
253     return 0;
254 }
255
256 //================================================================================
257 /*!
258  * \brief Updates dialog's look and feel
259  *
260  * Virtual method redefined from the base class updates dialog's look and feel
261  */
262 //================================================================================
263 void SMESHGUI_MeshOp::selectionDone()
264 {
265   SMESHGUI_SelectionOp::selectionDone();
266
267   if ( !myToCreate )
268   {
269     try
270     {
271       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
272       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
273       if ( pObj != 0 )
274       {
275         SMESH::SMESH_subMesh_var aVar =
276           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
277         myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
278         myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
279         myDlg->updateGeometry();
280         myDlg->adjustSize();
281         readMesh();
282       }
283       else
284         myDlg->reset();
285     }
286     catch ( const SALOME::SALOME_Exception& S_ex )
287     {
288       SalomeApp_Tools::QtCatchCorbaException( S_ex );
289     }
290     catch ( ... )
291     {
292     }
293   }
294
295   // Enable tabs according to shape dimension
296
297   int shapeDim = 3;
298   try
299   {
300     GEOM::GEOM_Object_var aGeomVar;
301     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
302     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
303     if ( pGeom ) {
304       aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
305     }
306     else {
307       QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
308       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
309       aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
310     }
311     if ( !aGeomVar->_is_nil() ) {
312       shapeDim = 0;
313       switch ( aGeomVar->GetShapeType() ) {
314       case GEOM::SOLID:
315       case GEOM::SHELL:  shapeDim = 3; break;
316       case GEOM::FACE:   shapeDim = 2; break;
317       case GEOM::WIRE:   
318       case GEOM::EDGE:   shapeDim = 1; break;
319       case GEOM::VERTEX: shapeDim = 0; break;
320       default:
321         TopoDS_Shape aShape;
322         if ( GEOMBase::GetShape(aGeomVar, aShape)) {
323           TopExp_Explorer exp( aShape, TopAbs_SHELL );
324           if ( exp.More() )
325             shapeDim = 3;
326           else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
327             shapeDim = 2;
328           else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
329             shapeDim = 1;
330           else
331             shapeDim = 0;
332         }
333       }
334     }
335   }
336   catch ( const SALOME::SALOME_Exception& S_ex )
337   {
338     SalomeApp_Tools::QtCatchCorbaException( S_ex );
339   }
340   catch ( ... )
341   {
342   }
343   myDlg->setMaxHypoDim( shapeDim );
344 }
345
346 //================================================================================
347 /*!
348  * \brief Verifies validity of input data
349   * \param theMess - Output parameter intended for returning error message
350   * \retval bool  - TRUE if input data is valid, false otherwise
351  *
352  * Verifies validity of input data. This method is called when "Apply" or "OK" button
353  * is pressed before mesh creation or editing.
354  */
355 //================================================================================
356 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
357 {
358   // Selected object to be  edited
359   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
360   {
361     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
362     return false;
363   }
364
365   // Name
366   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
367   aMeshName = aMeshName.stripWhiteSpace();
368   if ( aMeshName == "" )
369   {
370     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
371     return false;
372   }
373
374   // Geom
375   if ( myToCreate )
376   {
377     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
378     if ( aGeomEntry == "" )
379     {
380       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
381       return false;
382     }
383     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
384     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
385     {
386       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
387       return false;
388     }
389
390     // Mesh
391     if ( !myIsMesh ) // i.e sub-mesh creation,
392     {
393       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
394       if ( aMeshEntry == "" )
395       {
396         theMess = tr( "MESH_IS_NOT_DEFINED" );
397         return false;
398       }
399       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
400       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
401       {
402         theMess = tr( "MESH_IS_NULL" );
403         return false;
404       }
405     }
406   }
407   return true;
408 }
409
410 //================================================================================
411 /*!
412  * \brief Gets available hypotheses or algorithms
413   * \param theDim - specifies dimension of returned hypotheses/algorifms
414   * \param theHypType - specifies whether algorims or hypotheses or additional ones
415   * are retrieved (possible values are in HypType enumeration)
416   * \param theHyps - Output list of hypotheses' names
417  *
418  * Gets available hypotheses or algorithm in accordance with input parameters
419  */
420 //================================================================================
421 void SMESHGUI_MeshOp::availableHyps( const int theDim,
422                                      const int theHypType,
423                                      QStringList& theHyps ) const
424 {
425   theHyps.clear();
426   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses(
427     theHypType == Algo , theDim, theHypType == AddHyp );
428   QStringList::const_iterator anIter;
429   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
430   {
431     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
432     theHyps.append( aData->Label );
433   }
434 }
435
436 //================================================================================
437 /*!
438  * \brief Gets existing hypotheses or algorithms
439   * \param theDim - specifies dimension of returned hypotheses/algorifms
440   * \param theHypType - specifies whether algorims or hypotheses or additional ones
441   * are retrieved (possible values are in HypType enumeration)
442   * \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
443   * \param theHyps - output list of names.
444   * \param theHypVars - output list of variables.
445  *
446  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
447  * input parameters
448  */
449 //================================================================================
450 void SMESHGUI_MeshOp::existingHyps( const int theDim,
451                                     const int theHypType,
452                                     _PTR(SObject) theFather,
453                                     QStringList& theHyps,
454                                     QValueList<SMESH::SMESH_Hypothesis_var>& theHypVars )
455 {
456   // Clear hypoheses list
457   theHyps.clear();
458   theHypVars.clear();
459
460   if ( !theFather )
461     return;
462
463   _PTR(SObject)          aHypRoot;
464   _PTR(GenericAttribute) anAttr;
465   _PTR(AttributeName)    aName;
466   _PTR(AttributeIOR)     anIOR;
467
468   bool isMesh = !_CAST( SComponent, theFather );
469   int aPart = -1;
470   if ( isMesh )
471     aPart = theHypType == Algo ? 3 : 2;
472   else
473     aPart = theHypType == Algo ? 2 : 1;
474
475   if ( theFather->FindSubObject( aPart, aHypRoot ) )
476   {
477     _PTR(ChildIterator) anIter =
478       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
479     for (; anIter->More(); anIter->Next() )
480     {
481       _PTR(SObject) anObj = anIter->Value();
482       if ( isMesh ) // i.e. mesh or submesh
483       {
484         _PTR(SObject) aRefObj;
485         if ( anObj->ReferencedObject( aRefObj ) )
486           anObj = aRefObj;
487         else
488           continue;
489       }
490       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
491       {
492         aName = anAttr;
493         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
494         if ( !CORBA::is_nil( aVar ) )
495         {
496           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
497           if ( !aHypVar->_is_nil() )
498           {
499             QString aHypType( aHypVar->GetName() );
500             HypothesisData* aData = SMESH::GetHypothesisData( aHypType );
501             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
502                  ( theHypType == AddHyp ) == aData->IsAux )
503             {
504               theHyps.append( aName->Value().c_str() );
505               theHypVars.append( aHypVar );
506             }
507           }
508         }
509       }
510     }
511   }
512 }
513
514 //================================================================================
515 /*!
516  * \brief Calls plugin methods for hypothesis creation
517   * \param theHypType - specifies whether main hypotheses or additional ones
518   * are created
519   * \param theIndex - index of type of hypothesis to be cerated
520  *
521  * Speicfies dimension of hypothesis to be created (using sender() method), specifies
522  * its type and calls plugin methods for hypothesis creation
523  */
524 //================================================================================
525 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
526 {
527   // Speicfies dimension of hypothesis to be created
528   const QObject* aSender = sender();
529   int aDim = -1;
530   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
531     if ( aSender == myDlg->tab( i ) )
532       aDim = i;
533   if ( aDim == -1 )
534     return;
535
536   // Speicfies type of hypothesis to be created
537   QStringList aHypTypeNames = SMESH::GetAvailableHypotheses( false , aDim, theHypType == AddHyp );
538   if ( theIndex < 0 || theIndex >= aHypTypeNames.count() )
539     return;
540
541   QString aHypTypeName = aHypTypeNames[ theIndex ];
542   HypothesisData* aData = SMESH::GetHypothesisData( aHypTypeName.latin1() );
543   if ( aData == 0 )
544     return;
545
546   QString aClientLibName = aData->ClientLibName;
547   QStringList anOldHyps;
548   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
549   existingHyps( aDim, theHypType, aFather, anOldHyps, myExistingHyps[ aDim ][ theHypType ] );
550
551   if ( aClientLibName == "" )
552   {
553     // Call hypothesis creation server method (without GUI)
554     QString aHypName = aData->Label;
555     SMESH::CreateHypothesis( aHypTypeName, aHypName, false );
556   }
557   else
558   {
559     // Get hypotheses creator client (GUI)
560     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aHypTypeName );
561
562     // Create hypothesis
563     if( aCreator )
564       aCreator->create( false, myDlg );
565   }
566
567   QStringList aNewHyps;
568   aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
569   existingHyps( aDim, theHypType, aFather, aNewHyps, myExistingHyps[ aDim ][ theHypType ] );
570   if ( aNewHyps.count() > anOldHyps.count() )
571   {
572     for ( int i = anOldHyps.count(); i < aNewHyps.count(); i++ )
573       myDlg->tab( aDim )->addHyp( theHypType, aNewHyps[ i ] );
574   }
575 }
576
577 //================================================================================
578 /*!
579  * \brief Calls plugin methods for hypothesis editing
580   * \param theHypType - specifies whether main hypothesis or additional one
581   * is edited
582   * \param theIndex - index of existing hypothesis
583  *
584  * Calls plugin methods for hypothesis editing
585  */
586 //================================================================================
587 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
588 {
589   // Speicfies dimension of hypothesis to be created
590   const QObject* aSender = sender();
591   int aDim = -1;
592   for ( int i = SMESH::DIM_1D; i <= SMESH::DIM_3D; i++ )
593     if ( aSender == myDlg->tab( i ) )
594       aDim = i;
595   if ( aDim == -1 )
596     return;
597
598   QValueList<SMESH::SMESH_Hypothesis_var> aList = myExistingHyps[ aDim ][ theHypType ];
599   SMESH::SMESH_Hypothesis_var aHyp = aList[ theIndex - 1 ];
600   if ( aHyp->_is_nil() )
601     return;
602
603   char* aTypeName = aHyp->GetName();
604   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator( aTypeName );
605   if ( aCreator )
606     aCreator->edit( aHyp.in(), dlg() );
607 }
608
609 //================================================================================
610 /*!
611  * \brief Creates and selects hypothesis of hypotheses set 
612   * \param theSetName - The name of hypotheses set
613  */
614 //================================================================================
615
616 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
617 {
618   HypothesesSet* aHypoSet = SMESH::GetHypothesesSet( theSetName );
619   if ( !aHypoSet ) return;
620
621   for ( int aHypType = Algo; aHypType < AddHyp; aHypType++ )
622   {
623     bool isAlgo = (aHypType == Algo);
624
625     // clear all hyps
626     for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
627       setCurrentHyp( dim, aHypType, -1 );
628
629     // set hyps from the set
630     
631     QStringList* aHypoList = isAlgo ? & aHypoSet->AlgoList : & aHypoSet->HypoList;
632     for ( int i = 0, n = aHypoList->count(); i < n; i++ )
633     {
634       const QString& aHypoTypeName = (*aHypoList)[ i ];
635       HypothesisData* aHypData = SMESH::GetHypothesisData( aHypoTypeName );
636       if ( !aHypData )
637         continue;
638
639       int aDim = aHypData->Dim[0];
640       // create or/and set
641       int index = -1;
642       if ( isAlgo )
643       {
644         QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, aDim );
645         index = aHypTypeNameList.findIndex( aHypoTypeName );
646         if ( index < 0 ) continue;
647         setCurrentHyp ( aDim, aHypType, index );
648       }
649       else
650       {
651         // try to find an existing hypo
652         QValueList<SMESH::SMESH_Hypothesis_var> & aList = myExistingHyps[ aDim ][ aHypType ];
653         int iHyp = 0, nbHyp = aList.count();
654         for ( ; iHyp < nbHyp; ++iHyp )
655         {
656           SMESH::SMESH_Hypothesis_var aHyp = aList[ iHyp ];
657           if ( !aHyp->_is_nil() && aHypoTypeName == aHyp->GetName() ) {
658             index = iHyp;
659             break;
660           }
661         }
662         if ( index >= 0 ) // found
663         {
664           // select an algorithm
665           setCurrentHyp ( aDim, aHypType, index );
666         }
667         else
668         {
669           // create a hypothesis
670           QString aClientLibName = aHypData->ClientLibName;
671           if ( aClientLibName == "" ) {
672             // Call hypothesis creation server method (without GUI)
673             SMESH::CreateHypothesis( aHypoTypeName, aHypData->Label, isAlgo );
674           }
675           else {
676             // Get hypotheses creator client (GUI)
677             SMESHGUI_GenericHypothesisCreator* aCreator =
678               SMESH::GetHypothesisCreator( aHypoTypeName );
679             aCreator->create( false, myDlg );
680           }
681           QStringList aNewHyps;
682           _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
683           existingHyps( aDim, aHypType, aFather, aNewHyps, aList );
684           if ( aList.count() > nbHyp )
685           {
686             for ( int i = nbHyp; i < aNewHyps.count(); i++ )
687               myDlg->tab( aDim )->addHyp( aHypType, aNewHyps[ i ] );
688           }
689         }
690       }
691     } // loop on hypos in the set
692   } // loop on algo/hypo
693 }
694
695 //================================================================================
696 /*!
697  * \brief Creates mesh
698   * \param theMess - Output parameter intended for returning error message
699   * \retval bool  - TRUE if mesh is created, FALSE otherwise
700  *
701  * Creates mesh
702  */
703 //================================================================================
704 bool SMESHGUI_MeshOp::createMesh( QString& theMess )
705 {
706   theMess = "";
707
708   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
709   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
710   GEOM::GEOM_Object_var aGeomVar =
711     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
712
713   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
714   if ( aSMESHGen->_is_nil() )
715     return false;
716
717   SUIT_OverrideCursor aWaitCursor;
718
719   // create mesh
720   SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
721   if ( aMeshVar->_is_nil() )
722     return false;
723   _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
724   if ( aMeshSO )
725     SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
726
727   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
728   {
729     if ( !isAccessibleDim( aDim )) continue;
730
731     // assign hypotheses
732     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
733     {
734       int aHypIndex = currentHyp( aDim, aHypType );
735       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
736       {
737         SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
738         if ( !aHypVar->_is_nil() )
739           SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
740       }
741     }
742     // find or create algorithm
743     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
744     if ( !anAlgoVar->_is_nil() )
745       SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
746   }
747   return true;
748 }
749
750 //================================================================================
751 /*!
752  * \brief Creates sub-mesh
753   * \param theMess - Output parameter intended for returning error message
754   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
755  *
756  * Creates sub-mesh
757  */
758 //================================================================================
759 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess )
760 {
761   theMess = "";
762
763   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
764   if ( aSMESHGen->_is_nil() )
765     return false;
766
767   // get mesh object
768   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
769   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.latin1() );
770   SMESH::SMESH_Mesh_var aMeshVar =
771     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
772
773   // get geom object
774   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
775   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.latin1() );
776   GEOM::GEOM_Object_var aGeomVar =
777     GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
778
779   SUIT_OverrideCursor aWaitCursor;
780
781   // create sub-mesh
782   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
783   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.latin1() );
784
785   for ( int aDim = SMESH::DIM_1D; aDim <= SMESH::DIM_3D; aDim++ )
786   {
787     if ( !isAccessibleDim( aDim )) continue;
788
789     // find or create algorithm
790     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
791     if ( !anAlgoVar->_is_nil() )
792       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
793     // assign hypotheses
794     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
795     {
796       int aHypIndex = currentHyp( aDim, aHypType );
797       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
798       {
799         SMESH::SMESH_Hypothesis_var aHypVar =
800           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ];
801         if ( !aHypVar->_is_nil() )
802           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
803       }
804     }
805   }
806   return true;
807 }
808
809 //================================================================================
810 /*!
811  * \brief Gets current hypothesis or algorithms
812   * \param theDim - dimension of hypothesis or algorithm
813   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
814   * \retval int - current hypothesis or algorithms
815  *
816  * Gets current hypothesis or algorithms
817  */
818 //================================================================================
819 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
820 {
821   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
822 }
823
824 //================================================================================
825 /*!
826  * \brief Returns true if hypotheses of given dim can be assigned
827   * \param theDim - hypotheses dimension
828   * \retval bool - result
829  */
830 //================================================================================
831 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim) const
832 {
833   return myDlg->tab( theDim )->isEnabled();
834 }
835
836 //================================================================================
837 /*!
838  * \brief Sets current hypothesis or algorithms
839   * \param theDim - dimension of hypothesis or algorithm
840   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
841   * \param theIndex - Index of hypothesis
842  *
843  * Gets current hypothesis or algorithms
844  */
845 //================================================================================
846 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
847                                      const int theHypType,
848                                      const int theIndex )
849 {
850   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
851 }
852
853 //================================================================================
854 /*!
855  * \brief Generates default and sets mesh/submesh name
856  *
857  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
858  */
859 //================================================================================
860 void SMESHGUI_MeshOp::setDefaultName() const
861 {
862   QString aResName;
863
864   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
865   int i = 1;
866   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
867   _PTR(SObject) anObj;
868   do
869   {
870     aResName = aPrefix + QString::number( i++ );
871     anObj = aStudy->FindObject( aResName.latin1() );
872   }
873   while ( anObj );
874
875   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
876     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
877   aControl->setText( aResName );
878 }
879
880 //================================================================================
881 /*!
882  * \brief Gets algorithm or creates it if necessary
883   * \param theDim - specifies dimension of returned hypotheses/algorifms
884   * \retval SMESH::SMESH_Hypothesis_var - algorithm
885  *
886  * Gets algorithm or creates it if necessary
887  */
888 //================================================================================
889 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
890 {
891   SMESH::SMESH_Hypothesis_var anAlgoVar;
892   int aHypIndex = currentHyp( theDim, Algo );
893   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true, theDim, false );
894   if ( aHypIndex < 0 || aHypIndex >= aHypTypeNameList.count() )
895     return anAlgoVar;
896   QString aHypName = aHypTypeNameList[ aHypIndex ];
897   QValueList<SMESH::SMESH_Hypothesis_var>& aHypVarList = myExistingHyps[ theDim ][ Algo ];
898   QValueList<SMESH::SMESH_Hypothesis_var>::iterator anIter;
899   for ( anIter = aHypVarList.begin(); anIter != aHypVarList.end(); anIter++ )
900   {
901     SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
902     if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
903     {
904       anAlgoVar = aHypVar;
905       break;
906     }
907   }
908   if ( anAlgoVar->_is_nil() )
909   {
910     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
911     if ( aHypData )
912     {
913       QString aClientLibName = aHypData->ClientLibName;
914       if ( aClientLibName == "" )
915         SMESH::CreateHypothesis( aHypName, aHypData->Label, true );
916       else
917       {
918         SMESHGUI_GenericHypothesisCreator* aCreator =
919           SMESH::GetHypothesisCreator( aHypName );
920         if ( aCreator )
921           aCreator->create( true, myDlg );
922       }
923       QStringList tmpList;
924       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
925       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
926     }
927
928     QValueList<SMESH::SMESH_Hypothesis_var>& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
929     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
930     {
931       SMESH::SMESH_Hypothesis_var aHypVar = *anIter;
932       if ( !aHypVar->_is_nil() && aHypName == aHypVar->GetName() )
933       {
934         anAlgoVar = aHypVar;
935         break;
936       }
937     }
938   }
939
940   return anAlgoVar._retn();
941 }
942
943 //================================================================================
944 /*!
945  * \brief Reads parameters of edited mesh and assigns them to the dialog
946  *
947  * Reads parameters of edited mesh and assigns them to the dialog (called when
948  * mesh is edited only)
949  */
950 //================================================================================
951 void SMESHGUI_MeshOp::readMesh()
952 {
953   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
954   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
955   if ( !pObj )
956     return;
957
958   // Get name of mesh if current object is sub-mesh
959   SMESH::SMESH_subMesh_var aSubMeshVar =
960       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
961   if ( !aSubMeshVar->_is_nil() )
962   {
963     SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
964     if ( !aMeshVar->_is_nil() )
965     {
966       _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
967       QString aMeshName = name( aMeshSO );
968       myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
969     }
970   }
971
972   // Get name of geometry object
973   GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
974   if ( !aGeomVar->_is_nil() )
975   {
976     _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
977     QString aShapeName = name( aGeomSO );
978     myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
979   }
980
981   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
982   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
983   {
984     // get algorithm
985     QStringList anExisting;
986     int aHypIndex = -1;
987     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
988     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
989     {
990       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first();
991       QString aHypTypeName = aVar->GetName();
992
993       QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( true , dim, false );
994       for ( int i = 0, n = aHypTypeNameList.count(); i < n; i++ )
995         if ( aHypTypeName == aHypTypeNameList[ i ] )
996         {
997           aHypIndex = i;
998           break;
999         }
1000     }
1001     setCurrentHyp( dim, Algo, aHypIndex );
1002
1003     // get hypotheses
1004     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1005     {
1006       // get hypotheses
1007       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1008       // find index of requered hypothesis among existing ones for this dimension
1009       // and hyp types
1010       int aHypIndex = -1;
1011       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1012         aHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1013                           myExistingHyps[ dim ][ hypType ] );
1014       setCurrentHyp( dim, hypType, aHypIndex );
1015     }
1016   }
1017 }
1018
1019 //================================================================================
1020 /*!
1021  * \brief Gets name of object
1022   * \param theSO - SObject
1023   * \retval QString - name of object
1024  *
1025  * Gets name of object
1026  */
1027 //================================================================================
1028 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
1029 {
1030   QString aResName;
1031   if ( theSO )
1032   {
1033     _PTR(GenericAttribute) anAttr;
1034     _PTR(AttributeName)    aNameAttr;
1035     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
1036     {
1037       aNameAttr = anAttr;
1038       aResName = aNameAttr->Value().c_str();
1039     }
1040   }
1041   return aResName;
1042 }
1043
1044 //================================================================================
1045 /*!
1046  * \brief Finds hypothesis in input list
1047   * \param theHyp - hypothesis to be found
1048   * \param theHypList - input list of hypotheses
1049   * \retval int - index of hypothesis or -1 if it is not found
1050  *
1051  * Finds position of hypothesis in input list
1052  */
1053 //================================================================================
1054 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
1055                            const QValueList<SMESH::SMESH_Hypothesis_var>& theHypList ) const
1056 {
1057   int aRes = -1;
1058   if ( !theHyp->_is_nil() )
1059   {
1060     int i = 0;
1061     QValueList<SMESH::SMESH_Hypothesis_var>::const_iterator anIter;
1062     for ( anIter = theHypList.begin(); anIter != theHypList.end(); ++ anIter )
1063     {
1064       if ( theHyp->_is_equivalent( *anIter ) )
1065       {
1066         aRes = i;
1067         break;
1068       }
1069       i++;
1070     }
1071   }
1072   return aRes;
1073 }
1074
1075 //================================================================================
1076 /*!
1077  * \brief Edits mesh or sub-mesh
1078   * \param theMess - Output parameter intended for returning error message
1079   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
1080  *
1081  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
1082  */
1083 //================================================================================
1084 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
1085 {
1086   theMess = "";
1087
1088   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1089   if ( aSMESHGen->_is_nil() )
1090     return false;
1091
1092   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1093   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.latin1() );
1094   if ( !pObj )
1095     return false;
1096
1097   SUIT_OverrideCursor aWaitCursor;
1098
1099   // Set new name
1100   SMESH::SetName( pObj, myDlg->objectText( SMESHGUI_MeshDlg::Obj ).latin1() );
1101
1102   // Assign new hypotheses and algorithms
1103   for ( int dim = SMESH::DIM_1D; dim <= SMESH::DIM_3D; dim++ )
1104   {
1105     if ( !isAccessibleDim( dim )) continue;
1106
1107     // find or create algorithm
1108     bool toDelete = false, toAdd = true;
1109     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
1110     if ( anAlgoVar->_is_nil() ) {
1111       toAdd = false;
1112     }
1113     if ( myObjHyps[ dim ][ Algo ].count() > 0 ) {
1114       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first();
1115       if ( toAdd ) {
1116         if ( strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) == 0 ) {
1117           toAdd = false;
1118         } else {
1119           toDelete = true;
1120         }
1121       } else {
1122         toDelete = true;
1123       }
1124     }
1125     // remove old algorithm
1126     if ( toDelete )
1127       SMESH::RemoveHypothesisOrAlgorithmOnMesh
1128         ( pObj, myObjHyps[ dim ][ Algo ].first() );
1129
1130     // assign new algorithm
1131     if ( toAdd ) {
1132       SMESH::SMESH_Mesh_var aMeshVar =
1133         SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1134       bool isMesh = !aMeshVar->_is_nil();
1135       if ( isMesh ) {
1136         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1137       } else {
1138         SMESH::SMESH_subMesh_var aVar =
1139           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1140         if ( !aVar->_is_nil() )
1141           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
1142       }
1143     }
1144
1145     // assign hypotheses
1146     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
1147     {
1148       int aNewHypIndex = currentHyp( dim, hypType );
1149       int anOldHypIndex = -1;
1150       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
1151         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first(),
1152                               myExistingHyps[ dim ][ hypType ] );
1153       if ( aNewHypIndex != anOldHypIndex )
1154       {
1155         // remove old hypotheses
1156         if ( anOldHypIndex >= 0 )
1157           SMESH::RemoveHypothesisOrAlgorithmOnMesh(
1158             pObj, myExistingHyps[ dim ][ hypType ][ anOldHypIndex ] );
1159
1160         // assign new hypotheses
1161         if ( aNewHypIndex != -1 )
1162         {
1163           SMESH::SMESH_Mesh_var aMeshVar =
1164               SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1165           bool isMesh = !aMeshVar->_is_nil();
1166           if ( isMesh )
1167           {
1168             SMESH::AddHypothesisOnMesh(
1169               aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1170           }
1171           else
1172           {
1173             SMESH::SMESH_subMesh_var aVar =
1174               SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
1175             if ( !aVar->_is_nil() )
1176               SMESH::AddHypothesisOnSubMesh(
1177                 aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ] );
1178           }
1179         }
1180         // reread all hypotheses of mesh if necessary
1181         QStringList anExisting;
1182         existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
1183       }
1184     }
1185   }
1186
1187   return true;
1188 }
1189
1190 //================================================================================
1191 /*!
1192  * \brief Verifies whether given operator is valid for this one
1193   * \param theOtherOp - other operation
1194   * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
1195 *
1196 * method redefined from base class verifies whether given operator is valid for
1197 * this one (i.e. can be started "above" this operator). In current implementation method
1198 * retuns false if theOtherOp operation is not intended for deleting objects or mesh
1199 * elements.
1200 */
1201 //================================================================================
1202 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
1203 {
1204   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
1205 }