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