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