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