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