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