Salome HOME
Update copyrights 2014.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 //  File   : SMESHGUI_MeshOp.cxx
23 //  Author : Sergey LITONIN, Open CASCADE S.A.S.
24
25 // SMESH includes
26 #include "SMESHGUI_MeshOp.h"
27
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_MeshDlg.h"
30 #include "SMESHGUI_ShapeByMeshDlg.h"
31 #include "SMESHGUI_HypothesesUtils.h"
32 #include "SMESHGUI_Hypotheses.h"
33 #include "SMESHGUI_Utils.h"
34 #include "SMESHGUI_GEOMGenUtils.h"
35
36 #include <SMESH_TypeFilter.hxx>
37 #include <SMESH_NumberFilter.hxx>
38
39 // SALOME GEOM includes
40 #include <GEOM_SelectionFilter.h>
41 #include <GEOMBase.h>
42 #include <GeometryGUI.h>
43 #include <GEOM_wrap.hxx>
44
45 // SALOME GUI includes
46 #include <SalomeApp_Tools.h>
47 #include <SalomeApp_Application.h>
48 #include <LightApp_Application.h>
49 #include <LightApp_SelectionMgr.h>
50 #include <LightApp_UpdateFlags.h>
51 #include <SUIT_MessageBox.h>
52 #include <SUIT_OverrideCursor.h>
53 #include <SUIT_Session.h>
54 #include <SALOME_InteractiveObject.hxx>
55 #include <SALOME_ListIO.hxx>
56
57 // SALOME KERNEL includes
58 #include <SALOMEDS_SComponent.hxx>
59 #include <SALOMEDS_SObject.hxx>
60 #include <SALOMEDS_Study.hxx>
61 #include <SALOMEDS_wrap.hxx>
62
63 // Qt includes
64 #include <QStringList>
65 #include <QLineEdit>
66 #include <QApplication>
67
68 // OCCT includes
69 #include <TopoDS.hxx>
70 #include <TopoDS_Shape.hxx>
71 #include <TopoDS_Shell.hxx>
72 #include <TopExp_Explorer.hxx>
73 #include <BRep_Tool.hxx>
74
75 // IDL includes
76 #include <SALOMEconfig.h>
77 #include CORBA_CLIENT_HEADER(SMESH_Gen)
78
79 //================================================================================
80 /*!
81  * \brief Constructor
82   * \param theToCreate - if this parameter is true then operation is used for creation,
83   * for editing otherwise
84  *
85  * Initialize operation
86 */
87 //================================================================================
88 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
89 : SMESHGUI_SelectionOp(),
90   myToCreate( theToCreate ),
91   myIsMesh( theIsMesh ),
92   myDlg( 0 ),
93   myShapeByMeshOp( 0 ),
94   myHypoSet( 0 )
95 {
96   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
97     GeometryGUI::InitGeomGen();
98   myIsOnGeometry = true;
99   myMaxShapeDim = -1;
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::warning( myDlg, tr( "SMESH_WRN_WARNING" ), aMess );
144     return false;
145   }
146
147   bool aResult = false;
148   aMess = "";
149   try
150   {
151     QStringList anEntryList;
152     if ( myToCreate && myIsMesh )
153       aResult = createMesh( aMess, anEntryList );
154     if ( myToCreate && !myIsMesh )
155       aResult = createSubMesh( aMess, anEntryList );
156     else if ( !myToCreate )
157       aResult = editMeshOrSubMesh( aMess );
158     if ( aResult )
159     {
160       SMESHGUI::Modified();
161       update( UF_ObjBrowser | UF_Model );
162       if( LightApp_Application* anApp =
163           dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
164         myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
165     }
166   }
167   catch ( const SALOME::SALOME_Exception& S_ex )
168   {
169     SalomeApp_Tools::QtCatchCorbaException( S_ex );
170     aResult = false;
171   }
172   catch ( ... )
173   {
174     aResult = false;
175   }
176
177   if ( aResult )
178   {
179     if ( myToCreate )
180       setDefaultName();
181   }
182   else
183   {
184     if ( aMess == "" )
185       aMess = tr( "SMESH_OPERATION_FAILED" );
186     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), aMess );
187   }
188
189   return aResult;
190 }
191
192 //================================================================================
193 /*!
194  * \brief Creates dialog if necessary and shows it
195  *
196  * Virtual method redefined from base class called when operation is started creates
197  * dialog if necessary and shows it, activates selection
198  */
199 //================================================================================
200 void SMESHGUI_MeshOp::startOperation()
201 {
202   if (!myDlg)
203   {
204     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
205     for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
206     {
207       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
208               this, SLOT( onCreateHyp( const int, const int ) ) );
209       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
210               this, SLOT( onEditHyp( const int, const int ) ) );
211       connect( myDlg->tab( i ), SIGNAL( selectAlgo( const int ) ),
212               this, SLOT( onAlgoSelected( const int ) ) );
213     }
214     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
215     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
216     connect( myDlg, SIGNAL( selectMeshType( const int, const int ) ), SLOT( onAlgoSetByMeshType( const int, const int)));
217     if ( myToCreate ) {
218       if ( myIsMesh ) myHelpFileName = "constructing_meshes_page.html";
219       else            myHelpFileName = "constructing_submeshes_page.html";
220     }
221     else {
222       myHelpFileName = "editing_meshes_page.html";
223     }
224   }
225   SMESHGUI_SelectionOp::startOperation();
226   // iterate through dimensions and get available algoritms, set them to the dialog
227   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
228   for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
229   {
230     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
231     QStringList hypList;
232     // clear available hypotheses
233     aTab->setAvailableHyps( MainHyp, hypList );
234     aTab->setAvailableHyps( AddHyp, hypList );
235     aTab->setExistingHyps( MainHyp, hypList );
236     aTab->setExistingHyps( AddHyp, hypList );
237     myExistingHyps[ i ][ MainHyp ].clear();
238     myExistingHyps[ i ][ AddHyp ].clear();
239     // set algos
240     availableHyps( i, Algo, hypList, myAvailableHypData[i][Algo] );
241     aTab->setAvailableHyps( Algo, hypList );
242   }
243   if ( myToCreate )
244   {
245     setDefaultName();
246     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
247   }
248   else
249     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
250
251   myDlg->setCurrentTab( SMESH::DIM_3D );
252
253   QStringList TypeMeshList;
254   createMeshTypeList( TypeMeshList );
255   setAvailableMeshType( TypeMeshList );
256
257   myDlg->show();
258   myDlg->setGeomPopupEnabled(false);
259   selectionDone();
260
261   myIgnoreAlgoSelection = false;
262   myHasConcurrentSubBefore = false;
263
264   myObjectToSelect.clear();
265 }
266
267 //=================================================================================
268 /*!
269  * \brief Selects a recently created mesh or sub-mesh if necessary
270  *
271  * Virtual method redefined from base class called when operation is commited
272  * selects a recently created mesh or sub-mesh if necessary. Allows to perform
273  * selection when the custom selection filters are removed.
274  */
275 //=================================================================================
276 void SMESHGUI_MeshOp::commitOperation()
277 {
278   SMESHGUI_SelectionOp::commitOperation();
279
280   if ( !myObjectToSelect.isEmpty() ) {
281     if ( LightApp_SelectionMgr* aSelectionMgr = selectionMgr() ) {
282       SUIT_DataOwnerPtrList aList;
283       aList.append( new LightApp_DataOwner( myObjectToSelect ) );
284       aSelectionMgr->setSelected( aList );
285     }
286   }
287 }
288
289 //================================================================================
290 /*!
291  * \brief Creates selection filter
292   * \param theId - identifier of current selection widget
293   * \retval SUIT_SelectionFilter* - pointer to the created filter or null
294  *
295  * Creates selection filter in accordance with identifier of current selection widget
296  */
297 //================================================================================
298 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
299 {
300   if ( theId == SMESHGUI_MeshDlg::Geom )
301   {
302 //     TColStd_MapOfInteger allTypesMap;
303 //     for ( int i = 0; i < 10; i++ )
304 //       allTypesMap.Add( i );
305 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
306     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
307   }
308   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
309     return new SMESH_TypeFilter( SMESH::MESHorSUBMESH );
310   else if ( theId == SMESHGUI_MeshDlg::Mesh )
311     return new SMESH_TypeFilter( SMESH::MESH );
312   else
313     return 0;
314 }
315
316 //================================================================================
317 /*!
318  * \brief check if selected shape is a sub-shape of the shape to mesh
319   * \retval bool - check result
320  */
321 //================================================================================
322 bool SMESHGUI_MeshOp::isSubshapeOk() const
323 {
324   if ( !myToCreate || myIsMesh ) // not submesh creation
325     return false;
326
327   // mesh
328   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
329   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
330   if (!pMesh) return false;
331
332   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
333   if (mesh->_is_nil()) return false;
334
335   // main shape of the mesh
336   GEOM::GEOM_Object_var mainGeom = mesh->GetShapeToMesh();
337   if (mainGeom->_is_nil()) return false;
338
339   // geometry
340   QStringList aGEOMs;
341   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
342
343   if (aGEOMs.count() > 0) {
344     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
345     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
346     if (geomGen->_is_nil() || !aStudy) return false;
347
348     GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations(aStudy->StudyId());
349     if (op->_is_nil()) return false;
350
351     // check all selected shapes
352     QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
353     for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++) {
354       QString aSubGeomEntry = (*aSubShapesIter);
355       _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
356       if (!pSubGeom) return false;
357
358       GEOM::GEOM_Object_var aSubGeomVar =
359         GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
360       if (aSubGeomVar->_is_nil()) return false;
361
362       // skl for NPAL14695 - implementation of searching of mainObj
363       GEOM::GEOM_Object_var mainObj = op->GetMainShape(aSubGeomVar); /* _var not _wrap as
364                                                                         mainObj already exists! */
365       while(1) {
366         if (mainObj->_is_nil())
367           return false;
368         CORBA::String_var entry1 = mainObj->GetEntry();
369         CORBA::String_var entry2 = mainGeom->GetEntry();
370         if (std::string( entry1.in() ) == entry2.in() )
371           return true;
372         mainObj = op->GetMainShape(mainObj);
373       }
374     }
375   }
376
377   return false;
378 }
379
380 //================================================================================
381 /*!
382  * \brief Return name of the algorithm that does not support submeshes and makes
383  * submesh creation useless
384  *  \retval char* - string is to be deleted!!!
385  */
386 //================================================================================
387 char* SMESHGUI_MeshOp::isSubmeshIgnored() const
388 {
389   if ( myToCreate && !myIsMesh ) {
390
391     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
392     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
393     _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
394     if ( pMesh ) {
395
396       QStringList algoNames;
397       THypList    algoList;
398       existingHyps(3, Algo, pMesh, algoNames, algoList);
399       if (!algoList.empty()) {
400         HypothesisData* algo = SMESH::GetHypothesisData( algoList[0].first->GetName() );
401         if ( algo &&
402              algo->InputTypes.empty() && // builds all dimensions it-self
403              !algo->IsSupportSubmeshes )
404           return CORBA::string_dup( algoNames[0].toLatin1().data() );
405       }
406
407 //       GEOM::GEOM_Object_var geom;
408 //       if (_PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() ))
409 //         geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
410
411 //       if ( !geom->_is_nil() && geom->GetShapeType() >= GEOM::FACE ) { // WIRE, EDGE as well
412         existingHyps(2, Algo, pMesh, algoNames, algoList);
413         if (!algoList.empty()) {
414           HypothesisData* algo = SMESH::GetHypothesisData( algoList[0].first->GetName() );
415           if ( algo &&
416                algo->InputTypes.empty() && // builds all dimensions it-self
417                !algo->IsSupportSubmeshes )
418             return CORBA::string_dup( algoNames[0].toLatin1().data() );
419         }
420 //       }
421     }
422   }
423   return 0;
424 }
425
426 //================================================================================
427 /*!
428  * \brief find an existing submesh by the selected shape
429  * \retval _PTR(SObject) - the found submesh SObject
430  */
431 //================================================================================
432 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
433 {
434   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
435   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
436   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
437   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
438   if ( pMesh && pGeom ) {
439     GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
440     if ( !geom->_is_nil() ) {
441       int tag = -1;
442       switch ( geom->GetShapeType() ) {
443       case GEOM::VERTEX:   tag = SMESH::Tag_SubMeshOnVertex;   break;
444       case GEOM::EDGE:     tag = SMESH::Tag_SubMeshOnEdge;     break;
445       case GEOM::WIRE:     tag = SMESH::Tag_SubMeshOnWire;     break;
446       case GEOM::FACE:     tag = SMESH::Tag_SubMeshOnFace;     break;
447       case GEOM::SHELL:    tag = SMESH::Tag_SubMeshOnShell;    break;
448       case GEOM::SOLID:    tag = SMESH::Tag_SubMeshOnSolid;    break;
449       case GEOM::COMPOUND: tag = SMESH::Tag_SubMeshOnCompound; break;
450       default:;
451       }
452       _PTR(GenericAttribute) anAttr;
453       _PTR(SObject) aSubmeshRoot;
454       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
455       if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
456       {
457         _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
458         for ( ; smIter->More(); smIter->Next() )
459         {
460           _PTR(SObject) aSmObj = smIter->Value();
461           if ( ! aSmObj->FindAttribute( anAttr, "AttributeIOR" ))
462             continue;
463           _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
464           for ( ; anIter1->More(); anIter1->Next()) {
465             _PTR(SObject) pGeom2 = anIter1->Value();
466             if ( pGeom2->ReferencedObject( pGeom2 ) &&
467                  pGeom2->GetID() == pGeom->GetID() )
468               return aSmObj;
469           }
470         }
471       }
472     }
473   }
474   return _PTR(SObject)();
475 }
476
477 //================================================================================
478 /*!
479  * \brief Updates dialog's look and feel
480  *
481  * Virtual method redefined from the base class updates dialog's look and feel
482  */
483 //================================================================================
484 void SMESHGUI_MeshOp::selectionDone()
485 {
486   if (!dlg()->isVisible() || !myDlg->isEnabled())
487     return;
488
489   SMESHGUI_SelectionOp::selectionDone();
490
491   try
492   {
493     myIsOnGeometry = true;
494
495     //Check geometry for mesh
496     QString anObjEntry = myDlg->selectedObject(SMESHGUI_MeshDlg::Obj);
497     _PTR(SObject) pObj = studyDS()->FindObjectID(anObjEntry.toLatin1().data());
498     if (pObj)
499     {
500       SMESH::SMESH_Mesh_var aMeshVar =
501         SMESH::SMESH_Mesh::_narrow(_CAST(SObject,pObj)->GetObject());
502       if (!aMeshVar->_is_nil()) {
503         if (!myToCreate && !aMeshVar->HasShapeToMesh())
504           myIsOnGeometry = false;
505       }
506     }
507
508     if (myIsOnGeometry)
509     {
510       // Enable tabs according to shape dimension
511
512       int shapeDim = 3;
513
514       QStringList aGEOMs;
515       myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
516       GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
517
518       if (aGEOMs.count() > 0) {
519         // one or more GEOM shape selected
520         aSeq->length(aGEOMs.count());
521         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
522         int iSubSh = 0;
523         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
524           QString aSubGeomEntry = (*aSubShapesIter);
525           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
526           GEOM::GEOM_Object_var aSubGeomVar =
527             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
528           aSeq[iSubSh] = aSubGeomVar;
529         }
530       } else {
531         // get geometry by selected sub-mesh
532         QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
533         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
534         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
535         if (!aGeomVar->_is_nil()) {
536           aSeq->length(1);
537           aSeq[0] = aGeomVar;
538         }
539       }
540
541       if (aSeq->length() > 0) {
542         shapeDim = -1;
543         for (int iss = 0; iss < aSeq->length() && shapeDim < 3; iss++) {
544           GEOM::GEOM_Object_var aGeomVar = aSeq[iss];
545           switch ( aGeomVar->GetShapeType() ) {
546           case GEOM::SOLID:  shapeDim = 3; break;
547           case GEOM::SHELL:
548             // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
549             // {
550             //   TopoDS_Shape aShape;
551             //   bool isClosed = GEOMBase::GetShape(aGeomVar, aShape) && /*aShape.Closed()*/BRep_Tool::IsClosed(aShape);
552             //   shapeDim = qMax(isClosed ? 3 : 2, shapeDim);
553             // }
554             // break;
555           case GEOM::FACE:   shapeDim = qMax(2, shapeDim); break;
556           case GEOM::WIRE:
557           case GEOM::EDGE:   shapeDim = qMax(1, shapeDim); break;
558           case GEOM::VERTEX: shapeDim = qMax(0, shapeDim); break;
559           default:
560             {
561               TopoDS_Shape aShape;
562               if (GEOMBase::GetShape(aGeomVar, aShape))
563               {
564                 TopExp_Explorer exp (aShape, TopAbs_SOLID);
565                 if (exp.More()) {
566                   shapeDim = 3;
567                 }
568                 // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
569                 // else if ( exp.Init( aShape, TopAbs_SHELL ), exp.More() )
570                 // {
571                 //   shapeDim = 2;
572                 //   for (; exp.More() && shapeDim == 2; exp.Next()) {
573                 //     if (/*exp.Current().Closed()*/BRep_Tool::IsClosed(exp.Current()))
574                 //       shapeDim = 3;
575                 //   }
576                 // }
577                 else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
578                   shapeDim = qMax(2, shapeDim);
579                 else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
580                   shapeDim = qMax(1, shapeDim);
581                 else if ( exp.Init( aShape, TopAbs_VERTEX ), exp.More() )
582                   shapeDim = qMax(0, shapeDim);
583               }
584             }
585           }
586           if ( shapeDim == 3 )
587             break;
588         }
589       }
590       for (int i = SMESH::DIM_3D; i > shapeDim; i--) {
591         // reset algos before disabling tabs (0020138)
592         onAlgoSelected(-1, i);
593       }
594       myDlg->setMaxHypoDim( shapeDim );
595       myMaxShapeDim = shapeDim;
596       myDlg->setHypoSets( SMESH::GetHypothesesSets( shapeDim, "" ));
597
598       if (!myToCreate) // edition: read hypotheses
599       {
600         if (pObj != 0)
601         {
602           SMESH::SMESH_subMesh_var submeshVar =
603             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
604           myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !submeshVar->_is_nil() );
605           myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, true );
606           myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
607           myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->hide();
608           myDlg->updateGeometry();
609           myDlg->adjustSize();
610           readMesh();
611           myIsMesh = submeshVar->_is_nil();
612         }
613         else
614           myDlg->reset();
615       }
616       else if ( !myIsMesh ) // submesh creation
617       {
618         // if a submesh on the selected shape already exist, pass to submesh edition mode
619         if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
620           SMESH::SMESH_subMesh_var sm =
621             SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
622           bool editSubmesh = ( !sm->_is_nil() &&
623                                SUIT_MessageBox::question( myDlg, tr( "SMESH_WARNING" ),
624                                                           tr( "EDIT_SUBMESH_QUESTION"),
625                                                           SUIT_MessageBox::Yes |
626                                                           SUIT_MessageBox::No,
627                                                           SUIT_MessageBox::No )
628                                == SUIT_MessageBox::Yes );
629           if ( editSubmesh )
630           {
631             selectionMgr()->clearFilters();
632             selectObject( pSubmesh );
633             SMESHGUI::GetSMESHGUI()->switchToOperation(704);
634             return;
635           }
636           else
637           {
638             myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
639             selectObject( _PTR(SObject)() );
640             selectionDone();
641             return;
642           }
643         }
644         // discard selected mesh if submesh creation not allowed because of
645         // a global algorithm that does not support submeshes
646         if ( char* algoName = isSubmeshIgnored() ) {
647           SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ),
648                                     tr("SUBMESH_NOT_ALLOWED").arg(algoName));
649           CORBA::string_free( algoName );
650           myDlg->selectObject( "", SMESHGUI_MeshDlg::Mesh, "" );
651           selectObject( _PTR(SObject)() );
652           selectionDone();
653           return;
654         }
655
656         // enable/disable popup for choice of geom selection way
657         bool enable = false;
658         QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
659         if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
660           SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
661           if ( !mesh->_is_nil() ) {
662             //rnv: issue 21056: EDF 1608 SMESH: Dialog Box "Create Sub Mesh": focus should automatically switch to geometry
663             QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
664             _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
665             if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
666               myDlg->activateObject(SMESHGUI_MeshDlg::Geom);
667             enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
668           }
669         }
670         myDlg->setGeomPopupEnabled( enable );
671       }
672     }
673     else { // no geometry defined
674       myDlg->enableTab( SMESH::DIM_3D );
675       QStringList hypList;
676       availableHyps( SMESH::DIM_3D, Algo, hypList,
677                      myAvailableHypData[SMESH::DIM_3D][Algo]);
678
679       SMESHGUI_MeshTab* aTab = myDlg->tab( SMESH::DIM_3D );
680       aTab->setAvailableHyps( Algo, hypList );
681       for (int i = SMESH::DIM_0D;i < SMESH::DIM_3D; ++i) {
682         myDlg->disableTab(i);
683       }
684       myMaxShapeDim = -1;
685       //Hide labels and fields (Mesh and Geometry)
686       myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, false );
687       myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, false );
688       myDlg->adjustSize();
689       readMesh();
690     }
691     QStringList TypeMeshList;
692     createMeshTypeList( TypeMeshList );
693     setAvailableMeshType( TypeMeshList );
694   }
695   catch ( const SALOME::SALOME_Exception& S_ex )
696   {
697     SalomeApp_Tools::QtCatchCorbaException( S_ex );
698   }
699   catch ( ... )
700   {
701   }
702 }
703
704 //================================================================================
705 /*!
706  * \brief Verifies validity of input data
707   * \param theMess - Output parameter intended for returning error message
708   * \retval bool  - TRUE if input data is valid, false otherwise
709  *
710  * Verifies validity of input data. This method is called when "Apply" or "OK" button
711  * is pressed before mesh creation or editing.
712  */
713 //================================================================================
714 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
715 {
716   // Selected object to be  edited
717   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
718   {
719     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
720     return false;
721   }
722
723   // Name
724   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj ).trimmed();
725   if ( aMeshName.isEmpty() )
726   {
727     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
728     return false;
729   }
730
731 /*  // Imported mesh, if create sub-mesh or edit mesh
732   if ( !myToCreate || ( myToCreate && !myIsMesh ))
733   {
734     QString aMeshEntry = myDlg->selectedObject
735       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
736     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
737       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
738       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
739         theMess = tr( "IMPORTED_MESH" );
740         return false;
741       }
742     }
743   }*/
744
745   // Geom
746   if ( myToCreate )
747   {
748     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
749     if ( aGeomEntry.isEmpty() )
750     {
751       theMess = tr( myIsMesh ?
752                     "GEOMETRY_OBJECT_IS_NOT_DEFINED_MESH" :
753                     "GEOMETRY_OBJECT_IS_NOT_DEFINED_SUBMESH");
754       if ( !myIsMesh )
755         return false;
756       dlg()->show();
757       if ( SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), theMess,
758            SUIT_MessageBox::Yes, SUIT_MessageBox::No ) == SUIT_MessageBox::No )
759       {
760         theMess = "";
761         return false;
762       }
763       return true;
764     }
765     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
766     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
767     {
768       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
769       return false;
770     }
771
772     // Mesh
773     if ( !myIsMesh ) // i.e sub-mesh creation,
774     {
775       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
776       if ( aMeshEntry == "" )
777       {
778         theMess = tr( "MESH_IS_NOT_DEFINED" );
779         return false;
780       }
781       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
782       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
783       {
784         theMess = tr( "MESH_IS_NULL" );
785         return false;
786       }
787       if ( !isSubshapeOk() )
788       {
789         theMess = tr( "INVALID_SUBSHAPE" );
790         return false;
791       }
792     }
793   }
794
795   return true;
796 }
797
798 //================================================================================
799 /*!
800  * \brief check compatibility of the algorithm and another algorithm or hypothesis
801   * \param theAlgoData - algorithm data
802   * \param theHypData - hypothesis data
803   * \param theHypType - hypothesis type
804   * \param theHypTypeName - hypothesis type name, must be provided if 2-nd arg is not algo
805   * \retval bool - check result
806  */
807 //================================================================================
808 static bool isCompatible(const HypothesisData* theAlgoData,
809                          const HypothesisData* theHypData,
810                          const int             theHypType)
811 {
812   if ( !theAlgoData )
813     return true;
814
815   if ( theHypType == SMESHGUI_MeshOp::Algo )
816     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
817
818   bool isOptional;
819   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
820 }
821
822 //================================================================================
823 /*!
824  * \brief Gets available hypotheses or algorithms
825   * \param theDim - specifies dimension of returned hypotheses/algorifms
826   * \param theHypType - specifies whether algorims or hypotheses or additional ones
827   * are retrieved (possible values are in HypType enumeration)
828   * \param theHyps - Output list of hypotheses' names
829   * \param theAlgoData - to select hypos able to be used by this algo (optional)
830  *
831  * Gets available hypotheses or algorithm in accordance with input parameters
832  */
833 //================================================================================
834 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
835                                      const int       theHypType,
836                                      QStringList&    theHyps,
837                                      THypDataList&   theDataList,
838                                      HypothesisData* theAlgoData ) const
839 {
840   theDataList.clear();
841   theHyps.clear();
842   bool isAlgo = ( theHypType == Algo );
843   bool isAux  = ( theHypType == AddHyp );
844   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux, myIsOnGeometry, !myIsMesh );
845
846   QStringList::const_iterator anIter;
847   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
848   {
849     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
850     if ( isCompatible ( theAlgoData, aData, theHypType )) {
851       theDataList.append( aData );
852       theHyps.append( aData->Label );
853     }
854   }
855 }
856
857 //================================================================================
858 /*!
859  * \brief Gets existing hypotheses or algorithms
860  *  \param theDim - specifies dimension of returned hypotheses/algorifms
861  *  \param theHypType - specifies whether algorims or hypotheses or additional ones
862  *  are retrieved (possible values are in HypType enumeration)
863  *  \param theFather - start object for finding ( may be component, mesh, or sub-mesh )
864  *  \param theHyps - output list of names.
865  *  \param theHypVars - output list of variables.
866  *  \param theAlgoData - to select hypos able to be used by this algo (optional)
867  *
868  * Gets existing (i.e. already created) hypotheses or algorithm in accordance with
869  * input parameters
870  */
871 //================================================================================
872 void SMESHGUI_MeshOp::existingHyps( const int       theDim,
873                                     const int       theHypType,
874                                     _PTR(SObject)   theFather,
875                                     QStringList&    theHyps,
876                                     THypList&       theHypList,
877                                     HypothesisData* theAlgoData)
878 {
879   // Clear hypoheses list
880   theHyps.clear();
881   theHypList.clear();
882
883   if ( !theFather )
884     return;
885
886   const bool isAux  = ( theHypType == AddHyp );
887
888   _PTR(SObject)          aHypRoot;
889   _PTR(GenericAttribute) anAttr;
890   _PTR(AttributeName)    aName;
891   _PTR(AttributeIOR)     anIOR;
892
893   bool isMesh = !_CAST( SComponent, theFather );
894   int aPart = -1;
895   if ( isMesh )
896     aPart = theHypType == Algo ? SMESH::Tag_RefOnAppliedAlgorithms : SMESH::Tag_RefOnAppliedHypothesis;
897   else
898     aPart = theHypType == Algo ? SMESH::Tag_AlgorithmsRoot : SMESH::Tag_HypothesisRoot;
899
900   if ( theFather->FindSubObject( aPart, aHypRoot ) )
901   {
902     _PTR(ChildIterator) anIter =
903       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
904     for ( ; anIter->More(); anIter->Next() )
905     {
906       _PTR(SObject) anObj = anIter->Value();
907       if ( isMesh ) // i.e. mesh or submesh
908       {
909         _PTR(SObject) aRefObj;
910         if ( anObj->ReferencedObject( aRefObj ) )
911           anObj = aRefObj;
912         else
913           continue;
914       }
915       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
916       {
917         aName = anAttr;
918         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
919         if ( !CORBA::is_nil( aVar ) )
920         {
921           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
922           if ( !aHypVar->_is_nil() )
923           {
924             CORBA::String_var hypType = aHypVar->GetName();
925             HypothesisData* aData = SMESH::GetHypothesisData( hypType.in() );
926             if ( !aData) continue;
927             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
928                  ( isCompatible ( theAlgoData, aData, theHypType )) &&
929                  ( theHypType == Algo || isAux == aData->IsAuxOrNeedHyp ))
930             {
931               std::string aHypName = aName->Value();
932               theHyps.append( aHypName.c_str() );
933               theHypList.append( THypItem( aHypVar, aHypName.c_str() ) );
934             }
935           }
936         }
937       }
938     }
939   }
940 }
941
942 //================================================================================
943 /*!
944  * \brief If create or edit a submesh, return a hypothesis holding parameters used
945  *        to mesh a sub-shape
946   * \param aHypType - The hypothesis type name
947   * \param aServerLib - Server library name
948   * \param hypData - The structure holding the hypothesis type etc.
949   * \retval SMESH::SMESH_Hypothesis_var - the hypothesis holding parameter values
950  */
951 //================================================================================
952 SMESH::SMESH_Hypothesis_var
953 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
954                                           const QString& aServerLib ) const
955 {
956   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
957     return SMESH::SMESH_Hypothesis::_nil();
958
959   const int nbColonsInMeshEntry = 3;
960   bool isSubMesh = myToCreate ?
961     !myIsMesh :
962     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).count(':') > nbColonsInMeshEntry;
963
964   // get mesh and geom object
965   SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
966   GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
967
968   QString anEntry;
969   if ( isSubMesh )
970   {
971     anEntry = myDlg->selectedObject
972       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
973     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
974     {
975       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
976       if ( myToCreate ) // mesh and geom may be selected
977       {
978         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
979         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
980         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
981           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
982       }
983       else // edition: sub-mesh may be selected
984       {
985         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
986         if ( !sm->_is_nil() ) {
987           aMeshVar = sm->GetFather();
988           aGeomVar = sm->GetSubShape();
989         }
990       }
991     }
992   }
993   else // mesh
994   {
995     if ( !myToCreate ) // mesh to edit can be selected
996     {
997       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
998       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
999       {
1000         aMeshVar = SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1001         if ( !aMeshVar->_is_nil() )
1002           aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pMesh );
1003       }
1004     }
1005     if ( aGeomVar->_is_nil() ) {
1006       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1007       if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
1008       {
1009         aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1010       }
1011     }
1012   }
1013
1014   SMESH::SMESH_Hypothesis_var hyp =
1015     SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
1016                                                            aServerLib.toLatin1().data(),
1017                                                            aMeshVar,
1018                                                            aGeomVar,
1019                                                            /*byMesh = */isSubMesh);
1020   if ( hyp->_is_nil() && isSubMesh )
1021     hyp = SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
1022                                                                  aServerLib.toLatin1().data(),
1023                                                                  aMeshVar,
1024                                                                  aGeomVar,
1025                                                                  /*byMesh = */false);
1026   return hyp;
1027 }
1028
1029 //================================================================================
1030 /*!
1031  * \Brief Returns tab dimention
1032   * \param tab - the tab in the dlg
1033   * \param dlg - my dialogue
1034   * \retval int - dimention
1035  */
1036 //================================================================================
1037 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
1038 {
1039   int aDim = -1;
1040   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
1041     if (tab == dlg->tab(i))
1042       aDim = i;
1043   return aDim;
1044 }
1045
1046 //================================================================================
1047 /*!
1048  * \brief Create hypothesis
1049   * \param theHypType - hypothesis category (main or additional)
1050   * \param theIndex - index of type of hypothesis to be cerated
1051  *
1052  * Specifies dimension of hypothesis to be created (using sender() method),
1053  * specifies its type and calls method for hypothesis creation
1054  */
1055 //================================================================================
1056 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
1057 {
1058   // Specifies dimension of hypothesis to be created
1059   int aDim = getTabDim( sender(), myDlg );
1060   if (aDim == -1)
1061     return;
1062
1063   // Specifies type of hypothesis to be created
1064   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
1065   if (theIndex < 0 || theIndex >= dataList.count())
1066     return;
1067   QString aHypTypeName = dataList[ theIndex ]->TypeName;
1068
1069   // Create hypothesis
1070   createHypothesis(aDim, theHypType, aHypTypeName);
1071 }
1072
1073 namespace
1074 {
1075   QString GetUniqueName (const QStringList& theHypNames,
1076                          const QString& theName,
1077                          size_t theIteration = 1)
1078   {
1079     QString aName = theName + "_" + QString::number( theIteration );
1080     if ( theHypNames.contains( aName ) )
1081       return GetUniqueName( theHypNames, theName, ++theIteration );
1082     return aName;
1083   }
1084 }
1085
1086 //================================================================================
1087 /*!
1088  *  Create hypothesis and update dialog.
1089  *  \param theDim - dimension of hypothesis to be created
1090  *  \param theType - hypothesis category (algorithm, hypothesis, additional hypothesis)
1091  *  \param theTypeName - specifies hypothesis to be created
1092  */
1093 //================================================================================
1094 void SMESHGUI_MeshOp::createHypothesis(const int theDim,
1095                                        const int theType,
1096                                        const QString& theTypeName)
1097 {
1098   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName);
1099   if (!aData)
1100     return;
1101
1102   myDim = theDim;
1103   myType = theType;
1104   QStringList aHypNames;
1105   TDim2Type2HypList::const_iterator aDimIter = myExistingHyps.begin();
1106   for ( ; aDimIter != myExistingHyps.end(); aDimIter++) {
1107     const TType2HypList& aType2HypList = aDimIter.value();
1108     TType2HypList::const_iterator aTypeIter = aType2HypList.begin();
1109     for ( ; aTypeIter != aType2HypList.end(); aTypeIter++) {
1110       const THypList& aHypList = aTypeIter.value();
1111       THypList::const_iterator anIter = aHypList.begin();
1112       for ( ; anIter != aHypList.end(); anIter++) {
1113         const THypItem& aHypItem = *anIter;
1114         const QString& aHypName = aHypItem.second;
1115         aHypNames.append(aHypName);
1116       }
1117     }
1118   }
1119   QString aHypName = GetUniqueName( aHypNames, aData->Label);
1120
1121   // existing hypos
1122   bool dialog = false;
1123
1124   QString aClientLibName = aData->ClientLibName;
1125   if (aClientLibName == "") {
1126     // Call hypothesis creation server method (without GUI)
1127     SMESH::SMESH_Hypothesis_var aHyp =
1128       SMESH::CreateHypothesis(theTypeName, aHypName, false);
1129     aHyp.out();
1130   }
1131   else {
1132     // Get hypotheses creator client (GUI)
1133     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
1134
1135     // Create hypothesis
1136     if (aCreator) {
1137       // Get parameters appropriate to initialize a new hypothesis
1138       SMESH::SMESH_Hypothesis_var initParamHyp =
1139         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
1140
1141       removeCustomFilters(); // Issue 0020170
1142
1143       // Get Entry of the Geom object
1144       QString aGeomEntry = "";
1145       QString aMeshEntry = "";
1146       QString anObjEntry = "";
1147       aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1148       aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1149       anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1150
1151       if ( myToCreate && myIsMesh )
1152         aMeshEntry = aGeomEntry;
1153
1154       if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
1155         _PTR(SObject) pObj = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
1156         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1157         aMeshEntry = ( aGeomVar->_is_nil() ) ? "" : aMeshEntry = aGeomVar->GetStudyEntry();
1158       }
1159
1160       if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
1161         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1162         bool isMesh;
1163         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
1164         if ( !aGeomVar->_is_nil() )
1165         {
1166           aGeomEntry = aGeomVar->GetStudyEntry();
1167           if ( isMesh )
1168             aMeshEntry = aGeomEntry;
1169         }
1170       }
1171
1172       if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
1173         // take geometry from submesh being created
1174         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1175         if ( pObj ) {
1176           // if current object is sub-mesh
1177           SMESH::SMESH_subMesh_var aSubMeshVar =
1178             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1179           if ( !aSubMeshVar->_is_nil() ) {
1180             SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1181             if ( !aMeshVar->_is_nil() ) {
1182               _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1183               GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
1184               if ( !aGeomVar->_is_nil() )
1185                 aMeshEntry = aGeomVar->GetStudyEntry();
1186             }
1187           }
1188         }
1189       }
1190
1191       aCreator->setShapeEntry( aGeomEntry );
1192       if ( aMeshEntry != "" )
1193         aCreator->setMainShapeEntry( aMeshEntry );
1194       myDlg->setEnabled( false );
1195       aCreator->create(initParamHyp, aHypName, myDlg, this, SLOT( onHypoCreated( int ) ) );
1196       dialog = true;
1197     }
1198     else {
1199      SMESH::SMESH_Hypothesis_var aHyp =
1200        SMESH::CreateHypothesis(theTypeName, aHypName, false);
1201      aHyp.out();
1202     }
1203   }
1204
1205   if( !dialog )
1206     onHypoCreated(2);
1207 }
1208
1209 //================================================================================
1210 /*!
1211  *  Necessary steps after hypothesis creation
1212  *  \param result - creation result:
1213  *   0 = rejected
1214  *   1 = accepted
1215  *   2 = additional value meaning that slot is called not from dialog box
1216  */
1217 //================================================================================
1218 void SMESHGUI_MeshOp::onHypoCreated( int result )
1219 {
1220   if( result != 2 )
1221   {
1222     int obj = myDlg->getActiveObject();
1223     onActivateObject( obj ); // Issue 0020170. Restore filters
1224     myDlg->setEnabled( true );
1225   }
1226
1227   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1228
1229   int nbHyp = myExistingHyps[myDim][myType].count();
1230   HypothesisData* algoData = hypData( myDim, Algo, currentHyp( myDim, Algo ));
1231   QStringList aNewHyps;
1232   existingHyps(myDim, myType, aFather, aNewHyps, myExistingHyps[myDim][myType], algoData);
1233   if (aNewHyps.count() > nbHyp)
1234   {
1235     for (int i = nbHyp; i < aNewHyps.count(); i++)
1236       myDlg->tab(myDim)->addHyp(myType, aNewHyps[i]);
1237   }
1238
1239   if( result!=2 && myHypoSet )
1240     processSet();
1241 }
1242
1243 //================================================================================
1244 /*!
1245  * \brief Calls plugin methods for hypothesis editing
1246   * \param theHypType - specifies whether main hypothesis or additional one
1247   * is edited
1248   * \param theIndex - index of existing hypothesis
1249  *
1250  * Calls plugin methods for hypothesis editing
1251  */
1252 //================================================================================
1253 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
1254 {
1255   // Speicfies dimension of hypothesis to be created
1256   int aDim = getTabDim( sender(), myDlg );
1257   if (aDim == -1)
1258     return;
1259
1260   const THypList& aList = myExistingHyps[ aDim ][ theHypType ];
1261   if ( theIndex < 0 || theIndex >= aList.count() )
1262     return;
1263   const THypItem& aHypItem = aList[ theIndex ];
1264   SMESH::SMESH_Hypothesis_var aHyp = aHypItem.first;
1265   if ( aHyp->_is_nil() )
1266     return;
1267
1268   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHyp->GetName());
1269   if ( aCreator )
1270   {
1271     // Get initial parameters
1272     SMESH::SMESH_Hypothesis_var initParamHyp =
1273       getInitParamsHypothesis( aHyp->GetName(), aHyp->GetLibName());
1274     aCreator->setInitParamsHypothesis( initParamHyp );
1275
1276     // Get Entry of the Geom object
1277     QString aGeomEntry = "";
1278     QString aMeshEntry = "";
1279     QString anObjEntry = "";
1280     aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1281     aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1282     anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
1283
1284     if ( myToCreate && myIsMesh )
1285       aMeshEntry = aGeomEntry;
1286
1287     if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
1288       _PTR(SObject) pObj = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
1289       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
1290       aMeshEntry = ( aGeomVar->_is_nil() ) ? "" : aMeshEntry = aGeomVar->GetStudyEntry();
1291     }
1292
1293     if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
1294       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1295       bool isMesh;
1296       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
1297       if ( !aGeomVar->_is_nil() )
1298       {
1299         aGeomEntry = aGeomVar->GetStudyEntry();
1300         if ( isMesh )
1301           aMeshEntry = aGeomEntry;
1302       }
1303     }
1304
1305     if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
1306       // take geometry from submesh being created
1307       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
1308       if ( pObj ) {
1309         // if current object is sub-mesh
1310         SMESH::SMESH_subMesh_var aSubMeshVar =
1311           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
1312         if ( !aSubMeshVar->_is_nil() ) {
1313           SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
1314           if ( !aMeshVar->_is_nil() ) {
1315             _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
1316             GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
1317             if ( !aGeomVar->_is_nil() )
1318               aMeshEntry = aGeomVar->GetStudyEntry();
1319           }
1320         }
1321       }
1322     }
1323
1324     aCreator->setShapeEntry( aGeomEntry );
1325     if ( aMeshEntry != "" )
1326       aCreator->setMainShapeEntry( aMeshEntry );
1327     removeCustomFilters(); // Issue 0020170
1328     myDlg->setEnabled( false );
1329     aCreator->edit( aHyp.in(), aHypItem.second, dlg(), this, SLOT( onHypoEdited( int ) ) );
1330   }
1331 }
1332
1333 //================================================================================
1334 /*!
1335  *  Necessary steps after hypothesis edition
1336  *  \param result - creation result:
1337  *   0 = rejected
1338  *   1 = accepted
1339  */
1340 //================================================================================
1341 void SMESHGUI_MeshOp::onHypoEdited( int result )
1342 {
1343   int obj = myDlg->getActiveObject();
1344   onActivateObject( obj ); // Issue 0020170. Restore filters
1345   myDlg->setEnabled( true );
1346 }
1347
1348 //================================================================================
1349 /*!
1350  * \brief access to hypothesis data
1351   * \param theDim - hyp dimension
1352   * \param theHypType - hyp type (Algo,MainHyp or AddHyp)
1353   * \param theIndex - index in the list
1354   * \retval HypothesisData* - result data, may be 0
1355  */
1356 //================================================================================
1357 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
1358                                           const int theHypType,
1359                                           const int theIndex)
1360 {
1361   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
1362        theHypType > -1 && theHypType < NbHypTypes &&
1363        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
1364     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
1365   return 0;
1366 }
1367
1368 //================================================================================
1369 /*!
1370  * \brief Set available algos and hypos according to the selected algorithm
1371   * \param theIndex - algorithm index
1372  */
1373 //================================================================================
1374 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
1375                                       const int theDim )
1376 {
1377   if ( myIgnoreAlgoSelection )
1378     return;
1379
1380   int aDim = theDim < 0 ? getTabDim( sender(), myDlg ): theDim;
1381   if (aDim == -1)
1382     return;
1383
1384   // find highest available dimension, all algos of this dimension are available for choice
1385   int aTopDim = -1;
1386   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
1387     if (isAccessibleDim( i ))
1388       aTopDim = i;
1389   if (aTopDim == -1)
1390     return;
1391
1392   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
1393
1394   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
1395   HypothesisData* algoByDim[4];
1396   algoByDim[ aDim ] = algoData;
1397
1398   QStringList anAvailable;
1399   if ( !algoData ) { // all algos becomes available
1400     availableHyps( aDim, Algo, anAvailable, myAvailableHypData[ aDim ][ Algo ]);
1401     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1402   }
1403   // check that tab enable, if algorithm building needed algo is one less than dimension
1404   if ( algoData && myIsOnGeometry && !algoData->InputTypes.isEmpty() &&
1405        ( aDim > SMESH::DIM_0D ) && !isAccessibleDim( aDim - 1 ) ){
1406     myDlg->enableTab( aDim - 1 );
1407   }
1408   if ( (myDlg->currentMeshType() != MT_ANY) &&
1409        (( !algoData && ( aDim > SMESH::DIM_0D ) && isAccessibleDim( aDim - 1 )) ||
1410         ( algoData && myIsOnGeometry && algoData->InputTypes.isEmpty() &&
1411           ( aDim > SMESH::DIM_0D ) && isAccessibleDim( aDim - 1 ) ) ) ){
1412     for (int i = aDim - 1; i >= SMESH::DIM_0D; i--){
1413       if ( isAccessibleDim( i ) ) myDlg->disableTab( i );
1414     }
1415   }
1416   // check that algorithms of other dimentions are compatible with
1417   // the selected one
1418
1419   // 2 loops: backward and forward from algo dimension
1420   for ( int forward = false; forward <= true; ++forward )
1421   {
1422     int dim = aDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
1423     if ( !forward ) {
1424       dim = aDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
1425     }
1426     HypothesisData* prevAlgo = algoData;
1427     bool noCompatible = false;
1428     for ( ; dim * dir <= lastDim * dir; dim += dir)
1429     {
1430       if ( !isAccessibleDim( dim ))
1431         continue;
1432       if ( noCompatible ) { // the selected algo has no compatible ones
1433         anAvailable.clear();
1434         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1435         myAvailableHypData[dim][Algo].clear();
1436         algoByDim[ dim ] = 0;
1437         continue;
1438       }
1439       // get currently selected algo
1440       int algoIndex = currentHyp( dim, Algo );
1441       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
1442       if ( curAlgo ) { // some algo selected
1443         if ( !isCompatible( prevAlgo, curAlgo, Algo ))
1444           curAlgo = 0;
1445       }
1446       // set new available algoritms
1447       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
1448       HypothesisData* soleCompatible = 0;
1449       if ( anAvailable.count() == 1 )
1450         soleCompatible = myAvailableHypData[dim][Algo][0];
1451       if ( dim == aTopDim && prevAlgo ) // all available algoritms should be selectable any way
1452         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], 0 );
1453       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
1454       noCompatible = anAvailable.isEmpty();
1455
1456       // restore previously selected algo
1457       algoIndex = myAvailableHypData[dim][Algo].indexOf( curAlgo );
1458       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward && dim != SMESH::DIM_0D)
1459         // select the sole compatible algo
1460         algoIndex = myAvailableHypData[dim][Algo].indexOf( soleCompatible );
1461       setCurrentHyp( dim, Algo, algoIndex );
1462
1463       // remember current algo
1464       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
1465     }
1466   }
1467
1468   // set hypotheses corresponding to the found algoritms
1469
1470   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1471
1472   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
1473   {
1474     if ( !isAccessibleDim( dim ))
1475       continue;
1476     for ( int type = MainHyp; type < NbHypTypes; type++ )
1477     {
1478       myAvailableHypData[ dim ][ type ].clear();
1479       QStringList anAvailable, anExisting;
1480
1481       HypothesisData* curAlgo = algoByDim[ dim ];
1482       int hypIndex = currentHyp( dim, type );
1483
1484       SMESH::SMESH_Hypothesis_var curHyp;
1485       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
1486         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ].first;
1487
1488       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
1489         // try to find algo by selected hypothesis in order to keep it selected
1490         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
1491         CORBA::String_var curHypType = curHyp->GetName();
1492         if ( !algoDeselectedByUser &&
1493              myObjHyps[ dim ][ type ].count() > 0 &&
1494              !strcmp( curHypType, myObjHyps[ dim ][ type ].first().first->GetName()) )
1495         {
1496           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1497           for (int i = 0; i < myAvailableHypData[ dim ][ Algo ].count(); ++i) {
1498             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
1499             if (curAlgo && hypData && isCompatible(curAlgo, hypData, type))
1500               break;
1501             else
1502               curAlgo = 0;
1503           }
1504         }
1505       }
1506       // get hyps compatible with curAlgo
1507       bool defaulHypAvlbl = false;
1508       if ( curAlgo )
1509       {
1510         // check if a selected hyp is compatible with the curAlgo
1511         if ( !curHyp->_is_nil() ) {
1512           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
1513           if ( !isCompatible( curAlgo, hypData, type ))
1514             curHyp = SMESH::SMESH_Hypothesis::_nil();
1515         }
1516         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
1517         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
1518         defaulHypAvlbl = (type == MainHyp && !curAlgo->IsAuxOrNeedHyp );
1519       }
1520       // set list of hypotheses
1521       myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
1522       myDlg->tab( dim )->setExistingHyps( type, anExisting, defaulHypAvlbl );
1523
1524       // set current existing hypothesis
1525       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
1526         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
1527       else
1528         hypIndex = -1;
1529       if ( !isSubmesh && hypIndex < 0 && anExisting.count() == 1 ) {
1530         // none is yet selected => select the sole existing if it is not optional
1531         CORBA::String_var hypTypeName = myExistingHyps[ dim ][ type ].first().first->GetName();
1532         bool isOptional = true;
1533         if ( algoByDim[ dim ] &&
1534              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName.in(), isOptional ) &&
1535              !isOptional )
1536           hypIndex = 0;
1537       }
1538       setCurrentHyp( dim, type, hypIndex );
1539     }
1540   }
1541 }
1542
1543 //================================================================================
1544 /*!
1545  * \brief Creates and selects hypothesis of hypotheses set
1546  * \param theSetName - The name of hypotheses set
1547  */
1548 //================================================================================
1549 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
1550 {
1551   myHypoSet = SMESH::GetHypothesesSet(theSetName);
1552   if (!myHypoSet)
1553     return;
1554
1555   // clear all hyps
1556   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++) {
1557     setCurrentHyp(dim, Algo, -1);
1558     setCurrentHyp(dim, AddHyp, -1);
1559     setCurrentHyp(dim, MainHyp, -1);
1560   }
1561
1562   myHypoSet->init(true); //algorithms
1563   processSet();
1564   myHypoSet->init(false); //hypotheses
1565   processSet();
1566   myHypoSet = 0;
1567 }
1568
1569 //================================================================================
1570 /*!
1571  * \brief One step of hypothesis/algorithm list creation
1572  *
1573  * Creates a hypothesis or an algorithm for current item of internal list of names myHypoSet
1574  */
1575 //================================================================================
1576 void SMESHGUI_MeshOp::processSet()
1577 {
1578   myHypoSet->next();
1579   if( !myHypoSet->more() )
1580     return;
1581
1582   bool isAlgo = myHypoSet->isAlgo();
1583   QString aHypoTypeName = myHypoSet->current();
1584   HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
1585   if (!aHypData)
1586   {
1587     processSet();
1588     return;
1589   }
1590
1591   int aDim = aHypData->Dim[0];
1592   // create or/and set
1593   if (isAlgo)
1594   {
1595     int index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
1596     if ( index < 0 )
1597     {
1598       QStringList anAvailable;
1599       availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
1600       myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
1601       index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
1602     }
1603     setCurrentHyp( aDim, Algo, index );
1604     onAlgoSelected( index, aDim );
1605     processSet();
1606   }
1607   else
1608   {
1609     bool mainHyp = true;
1610     QStringList anAvailable;
1611     availableHyps( aDim, MainHyp, anAvailable, myAvailableHypData[aDim][MainHyp] );
1612     myDlg->tab( aDim )->setAvailableHyps( MainHyp, anAvailable );
1613     int index = myAvailableHypData[aDim][MainHyp].indexOf( aHypData );
1614     if ( index < 0 )
1615     {
1616       mainHyp = false;
1617       index = myAvailableHypData[aDim][AddHyp].indexOf( aHypData );
1618     }
1619     if (index >= 0)
1620       createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
1621     else
1622       processSet();
1623   }
1624 }
1625
1626 //================================================================================
1627 /*!
1628  * \brief Creates mesh
1629   * \param theMess - Output parameter intended for returning error message
1630   * \param theEntryList - List of entries of published objects
1631   * \retval bool  - TRUE if mesh is created, FALSE otherwise
1632  *
1633  * Creates mesh
1634  */
1635 //================================================================================
1636 bool SMESHGUI_MeshOp::createMesh( QString& theMess, QStringList& theEntryList )
1637 {
1638   theMess = "";
1639
1640   QStringList aList;
1641   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
1642   if ( aList.isEmpty() )
1643   {
1644     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1645     if ( aSMESHGen->_is_nil() )
1646       return false;
1647
1648     SMESH::SMESH_Mesh_var aMeshVar= aSMESHGen->CreateEmptyMesh();
1649     if ( aMeshVar->_is_nil() )
1650       return false;
1651
1652     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1653     if ( aMeshSO ) {
1654       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
1655       theEntryList.append( aMeshSO->GetID().c_str() );
1656     }
1657     return true;
1658   }
1659   QString namePrefix;
1660   if ( aList.count() > 1 )
1661   {
1662     namePrefix = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
1663     int i = namePrefix.length() - 1;
1664     while ( i > 0 && namePrefix[i].isDigit() )
1665       --i;
1666     if ( i < namePrefix.length() - 1 )
1667       namePrefix.chop( namePrefix.length() - 1 - i );
1668     else
1669       namePrefix += "_";
1670   }
1671   QStringList::Iterator it = aList.begin();
1672   for ( int i = 0; it!=aList.end(); it++, ++i )
1673   {
1674     QString aGeomEntry = *it;
1675     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
1676     GEOM::GEOM_Object_var aGeomVar =
1677       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1678
1679     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1680     if ( aSMESHGen->_is_nil() )
1681       return false;
1682
1683     SUIT_OverrideCursor aWaitCursor;
1684
1685     // create mesh
1686     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
1687     if ( aMeshVar->_is_nil() )
1688       return false;
1689     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
1690     if ( aMeshSO ) {
1691       theEntryList.append( aMeshSO->GetID().c_str() );
1692       if ( i > 0 ) setDefaultName( namePrefix );
1693       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
1694     }
1695
1696     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ ) {
1697       if ( !isAccessibleDim( aDim )) continue;
1698
1699       // assign hypotheses
1700       for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ ) {
1701         int aHypIndex = currentHyp( aDim, aHypType );
1702         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() ) {
1703           SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
1704           if ( !aHypVar->_is_nil() )
1705             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
1706         }
1707       }
1708       // find or create algorithm
1709       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1710       if ( !anAlgoVar->_is_nil() )
1711         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
1712     }
1713   }
1714   return true;
1715 }
1716
1717 //================================================================================
1718 /*!
1719  * \brief Creates sub-mesh
1720   * \param theMess - Output parameter intended for returning error message
1721   * \param theEntryList - List of entries of published objects
1722   * \retval bool  - TRUE if sub-mesh is created, FALSE otherwise
1723  *
1724  * Creates sub-mesh
1725  */
1726 //================================================================================
1727 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess, QStringList& theEntryList )
1728 {
1729   theMess = "";
1730
1731   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
1732   if ( aSMESHGen->_is_nil() )
1733     return false;
1734
1735   // get mesh object
1736   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
1737   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
1738   SMESH::SMESH_Mesh_var aMeshVar =
1739     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
1740   if (aMeshVar->_is_nil())
1741     return false;
1742
1743   // GEOM shape of the main mesh
1744   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
1745
1746   // Name for the new sub-mesh
1747   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
1748
1749   // get geom object
1750   GEOM::GEOM_Object_var aGeomVar;
1751   QStringList aGEOMs;
1752   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
1753   if (aGEOMs.count() == 1)
1754   {
1755     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
1756     QString aGeomEntry = aGEOMs.first();
1757     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
1758     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
1759   }
1760   else if (aGEOMs.count() > 1)
1761   {
1762     // create a GEOM group
1763     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
1764     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1765     if (!geomGen->_is_nil() && aStudy) {
1766       GEOM::GEOM_IGroupOperations_wrap op =
1767         geomGen->GetIGroupOperations(aStudy->StudyId());
1768       if (!op->_is_nil()) {
1769         // check and add all selected GEOM objects: they must be
1770         // a sub-shapes of the main GEOM and must be of one type
1771         int iSubSh = 0;
1772         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
1773         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
1774         aSeq->length(aGEOMs.count());
1775         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
1776         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
1777           QString aSubGeomEntry = (*aSubShapesIter);
1778           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
1779           GEOM::GEOM_Object_var aSubGeomVar =
1780             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
1781           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
1782           if (iSubSh == 0) {
1783             aGroupType = aSubShapeType;
1784           } else {
1785             if (aSubShapeType != aGroupType)
1786               aGroupType = TopAbs_SHAPE;
1787           }
1788           aSeq[iSubSh] = aSubGeomVar;
1789         }
1790         // create a group
1791         GEOM::GEOM_Object_wrap aGroupVar = op->CreateGroup(mainGeom, aGroupType);
1792         op->UnionList(aGroupVar, aSeq);
1793
1794         if (op->IsDone())
1795         {
1796           aGeomVar = GEOM::GEOM_Object::_duplicate( aGroupVar.in() );
1797
1798           // publish the GEOM group in study
1799           QString aNewGeomGroupName ("Auto_group_for_");
1800           aNewGeomGroupName += aName;
1801           SALOMEDS::Study_var aStudyVar = _CAST(Study, aStudy)->GetStudy();
1802           SALOMEDS::SObject_wrap aNewGroupSO =
1803             geomGen->AddInStudy( aStudyVar, aGeomVar,
1804                                  aNewGeomGroupName.toLatin1().data(), mainGeom);
1805         }
1806       }
1807     }
1808   }
1809   else {
1810   }
1811   if (aGeomVar->_is_nil())
1812     return false;
1813
1814   SUIT_OverrideCursor aWaitCursor;
1815
1816   // create sub-mesh
1817   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.toLatin1().data() );
1818   _PTR(SObject) aSubMeshSO = SMESH::FindSObject( aSubMeshVar.in() );
1819   if ( aSubMeshSO ) {
1820     SMESH::SetName( aSubMeshSO, aName.toLatin1().data() );
1821     theEntryList.append( aSubMeshSO->GetID().c_str() );
1822   }
1823
1824   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
1825   {
1826     if ( !isAccessibleDim( aDim )) continue;
1827
1828     // find or create algorithm
1829     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
1830     if ( !anAlgoVar->_is_nil() )
1831       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
1832     // assign hypotheses
1833     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
1834     {
1835       int aHypIndex = currentHyp( aDim, aHypType );
1836       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
1837       {
1838         SMESH::SMESH_Hypothesis_var aHypVar =
1839           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
1840         if ( !aHypVar->_is_nil() )
1841           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
1842       }
1843     }
1844   }
1845
1846   // deselect geometry: next submesh should be created on other sub-shape
1847   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
1848   selectObject( _PTR(SObject)() );
1849   selectionDone();
1850
1851   checkSubMeshConcurrency( aMeshVar, aSubMeshVar, /*askUser=*/true );
1852
1853   return true;
1854 }
1855
1856 //================================================================================
1857 /*!
1858  * \brief Gets current hypothesis or algorithms
1859   * \param theDim - dimension of hypothesis or algorithm
1860   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1861   * \retval int - current hypothesis or algorithms
1862  *
1863  * Gets current hypothesis or algorithms
1864  */
1865 //================================================================================
1866 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
1867 {
1868   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
1869 }
1870
1871 //================================================================================
1872 /*!
1873  * \brief Returns true if hypotheses of given dim can be assigned
1874   * \param theDim - hypotheses dimension
1875   * \retval bool - result
1876  */
1877 //================================================================================
1878 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
1879 {
1880   return myDlg->isTabEnabled( theDim );
1881 }
1882
1883 //================================================================================
1884 /*!
1885  * \brief Sets current hypothesis or algorithms
1886   * \param theDim - dimension of hypothesis or algorithm
1887   * \param theHypType - Type of hypothesis (Algo, MainHyp, AddHyp)
1888   * \param theIndex - Index of hypothesis
1889  *
1890  * Gets current hypothesis or algorithms
1891  */
1892 //================================================================================
1893 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
1894                                      const int theHypType,
1895                                      const int theIndex )
1896 {
1897   myIgnoreAlgoSelection = true;
1898   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
1899   myIgnoreAlgoSelection = false;
1900 }
1901
1902 //================================================================================
1903 /*!
1904  * \brief Generates default and sets mesh/submesh name
1905  *
1906  * Generates and sets default mesh/submesh name(Mesh_1, Mesh_2, etc.)
1907  */
1908 //================================================================================
1909 void SMESHGUI_MeshOp::setDefaultName( const QString& thePrefix ) const
1910 {
1911   QString aResName;
1912
1913   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
1914   int i = 1;
1915
1916   QString aPrefix = thePrefix;
1917   if ( aPrefix.isEmpty() )
1918     aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
1919
1920   _PTR(SObject) anObj;
1921   do
1922   {
1923     aResName = aPrefix + QString::number( i++ );
1924     anObj = aStudy->FindObject( aResName.toLatin1().data() );
1925   }
1926   while ( anObj );
1927
1928   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
1929     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
1930   aControl->setText( aResName );
1931 }
1932
1933 //================================================================================
1934 /*!
1935  * \brief Gets algorithm or creates it if necessary
1936   * \param theDim - specifies dimension of returned hypotheses/algorifms
1937   * \retval SMESH::SMESH_Hypothesis_var - algorithm
1938  *
1939  * Gets algorithm or creates it if necessary
1940  */
1941 //================================================================================
1942 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
1943 {
1944   SMESH::SMESH_Hypothesis_var anAlgoVar;
1945
1946   // get type of the selected algo
1947   int aHypIndex = currentHyp( theDim, Algo );
1948   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
1949   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
1950     return anAlgoVar;
1951   QString aHypName = dataList[ aHypIndex ]->TypeName;
1952
1953   // get existing algoritms
1954   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
1955   QStringList tmp;
1956   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
1957
1958   // look for an existing algo of such a type
1959   THypList& aHypVarList = myExistingHyps[ theDim ][ Algo ];
1960   THypList::iterator anIter = aHypVarList.begin();
1961   for ( ; anIter != aHypVarList.end(); anIter++)
1962   {
1963     SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
1964     CORBA::String_var aName = aHypVar->GetName();
1965     if ( !aHypVar->_is_nil() && !strcmp(aHypName.toLatin1().data(), aName) )
1966     {
1967       anAlgoVar = aHypVar;
1968       break;
1969     }
1970   }
1971
1972   if (anAlgoVar->_is_nil())
1973   {
1974     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
1975     if (aHypData)
1976     {
1977       QString aClientLibName = aHypData->ClientLibName;
1978       if ( aClientLibName.isEmpty() )
1979       {
1980         // Call hypothesis creation server method (without GUI)
1981         SMESH::SMESH_Hypothesis_var aHyp =
1982           SMESH::CreateHypothesis(aHypName, aHypName, true);
1983         aHyp.out();
1984       }
1985       else
1986       {
1987         // Get hypotheses creator client (GUI)
1988         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
1989
1990         // Create algorithm
1991         if (aCreator)
1992           aCreator->create( true, aHypName, myDlg, 0, QString::null );
1993         else {
1994           SMESH::SMESH_Hypothesis_var aHyp =
1995             SMESH::CreateHypothesis(aHypName, aHypName, true);
1996           aHyp.out();
1997         }
1998       }
1999       QStringList tmpList;
2000       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
2001       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
2002     }
2003
2004     THypList& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
2005     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
2006     {
2007       SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
2008       CORBA::String_var aName = aHypVar->GetName();
2009       if ( !aHypVar->_is_nil() && !strcmp(aHypName.toLatin1().data(), aName) )
2010       {
2011         anAlgoVar = aHypVar;
2012         break;
2013       }
2014     }
2015   }
2016
2017   return anAlgoVar._retn();
2018 }
2019
2020 //================================================================================
2021 /*!
2022  * \brief Reads parameters of an edited mesh/sub-mesh and assigns them to the dialog
2023  *
2024  * Called when mesh is edited only.
2025  */
2026 //================================================================================
2027 void SMESHGUI_MeshOp::readMesh()
2028 {
2029   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2030   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
2031   if ( !pObj )
2032     return;
2033
2034   if (myIsOnGeometry) {
2035     // Get name of mesh if current object is sub-mesh
2036     SMESH::SMESH_subMesh_var aSubMeshVar =
2037       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
2038     if ( !aSubMeshVar->_is_nil() )
2039     {
2040       SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
2041       if ( !aMeshVar->_is_nil() )
2042       {
2043         _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
2044         QString aMeshName = name( aMeshSO );
2045         myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
2046       }
2047       myHasConcurrentSubBefore = checkSubMeshConcurrency( aMeshVar, aSubMeshVar );
2048     }
2049
2050     // Get name of geometry object
2051     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
2052     if ( !aGeomVar->_is_nil() )
2053     {
2054       _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
2055       QString aShapeName = name( aGeomSO );
2056       myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
2057     }
2058   }
2059
2060   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
2061   QStringList anExisting;
2062   const int lastDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
2063   bool algoFound = false;
2064   for ( int dim = SMESH::DIM_3D; dim >= lastDim; --dim )
2065   {
2066     // get algorithm
2067     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
2068     // find algo index among available ones
2069     int aHypIndex = -1;
2070     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
2071     {
2072       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first().first;
2073       HypothesisData* algoData = SMESH::GetHypothesisData( aVar->GetName() );
2074       aHypIndex = myAvailableHypData[ dim ][ Algo ].indexOf ( algoData );
2075 //       if ( aHypIndex < 0 && algoData ) {
2076 //         // assigned algo is incompatible with other algorithms
2077 //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
2078 //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
2079 //       }
2080       algoFound = ( aHypIndex > -1 );
2081     }
2082     setCurrentHyp( dim, Algo, aHypIndex );
2083     // set existing and available hypothesis according to the selected algo
2084     if ( aHypIndex > -1 || !algoFound )
2085       onAlgoSelected( aHypIndex, dim );
2086   }
2087
2088   // get hypotheses
2089   bool hypWithoutAlgo = false;
2090   for ( int dim = SMESH::DIM_3D; dim >= lastDim; --dim )
2091   {
2092     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2093     {
2094       // get hypotheses
2095       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2096       // find index of requered hypothesis among existing ones for this dimension and type
2097       int aHypIndex = -1;
2098       if ( myObjHyps[ dim ][ hypType ].count() > 0 ) {
2099         aHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
2100                           myExistingHyps[ dim ][ hypType ] );
2101         if ( aHypIndex < 0 ) {
2102           // assigned hypothesis is incompatible with the algorithm
2103           if ( currentHyp( dim, Algo ) < 0 )
2104           { // none algo selected; it is edition for sure, of submesh maybe
2105             hypWithoutAlgo = true;
2106             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ].first() );
2107             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
2108             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
2109           }
2110         }
2111       }
2112       setCurrentHyp( dim, hypType, aHypIndex );
2113     }
2114   }
2115   // make available other hyps of same type as one without algo
2116   if ( hypWithoutAlgo )
2117     onAlgoSelected( currentHyp( 0, Algo ), 0 );
2118 }
2119
2120 //================================================================================
2121 /*!
2122  * \brief Gets name of object
2123   * \param theSO - SObject
2124   * \retval QString - name of object
2125  *
2126  * Gets name of object
2127  */
2128 //================================================================================
2129 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
2130 {
2131   QString aResName;
2132   if ( theSO )
2133   {
2134     _PTR(GenericAttribute) anAttr;
2135     _PTR(AttributeName)    aNameAttr;
2136     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
2137     {
2138       aNameAttr = anAttr;
2139       aResName = aNameAttr->Value().c_str();
2140     }
2141   }
2142   return aResName;
2143 }
2144
2145 //================================================================================
2146 /*!
2147  * \brief Finds hypothesis in input list
2148   * \param theHyp - hypothesis to be found
2149   * \param theHypList - input list of hypotheses
2150   * \retval int - index of hypothesis or -1 if it is not found
2151  *
2152  * Finds position of hypothesis in input list
2153  */
2154 //================================================================================
2155 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
2156                            const THypList& theHypList ) const
2157 {
2158   int aRes = -1;
2159   if ( !theHyp->_is_nil() )
2160   {
2161     int i = 0;
2162     THypList::const_iterator anIter = theHypList.begin();
2163     for ( ; anIter != theHypList.end(); ++ anIter)
2164     {
2165       if ( theHyp->_is_equivalent( (*anIter).first ) )
2166       {
2167         aRes = i;
2168         break;
2169       }
2170       i++;
2171     }
2172   }
2173   return aRes;
2174 }
2175
2176 //================================================================================
2177 /*!
2178  * \brief Edits mesh or sub-mesh
2179   * \param theMess - Output parameter intended for returning error message
2180   * \retval bool  - TRUE if mesh is edited succesfully, FALSE otherwise
2181  *
2182  * Assigns new name hypotheses and algoriths to the mesh or sub-mesh
2183  */
2184 //================================================================================
2185 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
2186 {
2187   theMess = "";
2188
2189   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
2190   if ( aSMESHGen->_is_nil() )
2191     return false;
2192
2193   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
2194   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
2195   if ( !pObj )
2196     return false;
2197
2198   SUIT_OverrideCursor aWaitCursor;
2199
2200   // Set new name
2201   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
2202   SMESH::SetName( pObj, aName );
2203   int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
2204
2205   // First, remove old algos in order to avoid messages on algorithm hiding
2206   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2207   {
2208     if ( isAccessibleDim( dim ) && myObjHyps[ dim ][ Algo ].count() > 0 )
2209     {
2210       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first().first;
2211       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2212       if ( anAlgoVar->_is_nil() || // no new algo selected or
2213            strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) ) // algo change
2214       {
2215         // remove old algorithm
2216         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first().first );
2217         myObjHyps[ dim ][ Algo ].clear();
2218       }
2219     }
2220   }
2221
2222   SALOMEDS_SObject* aSObject = _CAST(SObject, pObj);
2223   CORBA::Object_var anObject = aSObject->GetObject();
2224   SMESH::SMESH_Mesh_var       aMeshVar = SMESH::SMESH_Mesh::_narrow( anObject );
2225   SMESH::SMESH_subMesh_var aSubMeshVar = SMESH::SMESH_subMesh::_narrow( anObject );
2226   bool isMesh = !aMeshVar->_is_nil();
2227   if ( !isMesh && !aSubMeshVar->_is_nil() )
2228     aMeshVar = aSubMeshVar->GetFather();
2229
2230   // Assign new algorithms and hypotheses
2231   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
2232   {
2233     if ( !isAccessibleDim( dim )) continue;
2234
2235     // find or create algorithm
2236     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
2237
2238     // assign new algorithm
2239     if ( !anAlgoVar->_is_nil() && // some algo selected and
2240          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
2241     {
2242       if ( isMesh )
2243         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
2244       else if ( !aSubMeshVar->_is_nil() )
2245         SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
2246
2247       myObjHyps[ dim ][ Algo ].append( THypItem( anAlgoVar, aName) );
2248     }
2249
2250     // assign hypotheses
2251     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
2252     {
2253       int aNewHypIndex = currentHyp( dim, hypType );
2254       int anOldHypIndex = -1;
2255
2256       // remove old hypotheses
2257       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
2258       {
2259         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
2260                               myExistingHyps[ dim ][ hypType ] );
2261         if ( aNewHypIndex != anOldHypIndex || // different hyps
2262              anOldHypIndex == -1 )            // hyps of different algos
2263         {
2264           SMESH::RemoveHypothesisOrAlgorithmOnMesh
2265             ( pObj, myObjHyps[ dim ][ hypType ].first().first );
2266           myObjHyps[ dim ][ hypType ].clear();
2267         }
2268       }
2269
2270       // assign new hypotheses
2271       if ( aNewHypIndex != anOldHypIndex && aNewHypIndex > -1 )
2272       {
2273         if ( isMesh )
2274           SMESH::AddHypothesisOnMesh
2275             (aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
2276         else if ( !aSubMeshVar->_is_nil() )
2277           SMESH::AddHypothesisOnSubMesh
2278             ( aSubMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
2279       }
2280       // reread all hypotheses of mesh if necessary
2281       QStringList anExisting;
2282       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
2283     }
2284   }
2285
2286   myHasConcurrentSubBefore =
2287     checkSubMeshConcurrency( aMeshVar, aSubMeshVar, /*askUser=*/!myHasConcurrentSubBefore );
2288
2289   return true;
2290 }
2291
2292 //================================================================================
2293 /*!
2294  * \brief Checks if a concurrent sub-meshes appear as result of sub-mesh
2295  *        creation/edition and, if (askUser) , proposes the uses to set up a desired
2296  *        order of sub-mesh computation.
2297  *        Returns \c true if a sub-mesh concurrency detected.
2298  */
2299 //================================================================================
2300
2301 bool SMESHGUI_MeshOp::checkSubMeshConcurrency(SMESH::SMESH_Mesh_ptr    mesh,
2302                                               SMESH::SMESH_subMesh_ptr submesh,
2303                                               bool                     askUser)
2304 {
2305   if ( CORBA::is_nil( mesh ) || CORBA::is_nil( submesh ))
2306     return false;
2307
2308   bool isNewConcurrent = mesh->IsUnorderedSubMesh( submesh->GetId() );
2309   if ( isNewConcurrent && askUser )
2310   {
2311     int butID = SUIT_MessageBox::warning( myDlg->parentWidget(), tr( "SMESH_WARNING" ),
2312                                           tr("CONCURRENT_SUBMESH_APPEARS"),
2313                                           tr("SMESH_BUT_YES"), tr("SMESH_BUT_NO"));
2314     if ( butID == 0 )
2315     {
2316       _PTR(SObject) meshSO = SMESH::FindSObject( mesh );
2317       LightApp_SelectionMgr* aSelectionMgr = selectionMgr();
2318       if ( meshSO && aSelectionMgr )
2319       {
2320         myDlg->setEnabled( false ); // disactivate selection
2321         selectionMgr()->clearFilters();
2322         selectObject( meshSO );
2323         SMESHGUI::GetSMESHGUI()->OnGUIEvent( 713 ); // MESH_ORDER
2324         qApp->processEvents();
2325
2326         myDlg->setEnabled( true );
2327         int obj = myDlg->getActiveObject();
2328         onActivateObject( obj ); // restore filter
2329         if ( !myToCreate )
2330         {
2331           selectObject( SMESH::FindSObject( submesh ));
2332           selectionDone();
2333         }
2334       }
2335     }
2336   }
2337
2338   return isNewConcurrent;
2339 }
2340
2341 //================================================================================
2342 /*!
2343  * \brief Verifies whether given operator is valid for this one
2344  * \param theOtherOp - other operation
2345  * \return Returns TRUE if the given operator is valid for this one, FALSE otherwise
2346  *
2347  * method redefined from base class verifies whether given operator is valid for
2348  * this one (i.e. can be started "above" this operator). In current implementation method
2349  * retuns false if theOtherOp operation is not intended for deleting objects or mesh
2350  * elements.
2351  */
2352 //================================================================================
2353 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
2354 {
2355   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
2356 }
2357
2358 //================================================================================
2359 /*!
2360  * \brief SLOT. Is called when the user selects a way of geometry selection
2361  * \param theByMesh - true if the user wants to find geometry by mesh element
2362  */
2363 //================================================================================
2364 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
2365 {
2366   if ( theByMesh ) {
2367     if ( !myShapeByMeshOp ) {
2368       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
2369       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
2370               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
2371       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
2372               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
2373     }
2374     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
2375     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
2376     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
2377       SMESH::SMESH_Mesh_var aMeshVar =
2378         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
2379       if ( !aMeshVar->_is_nil() ) {
2380         myDlg->hide(); // stop processing selection
2381         myShapeByMeshOp->setModule( getSMESHGUI() );
2382         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
2383         myShapeByMeshOp->SetMesh( aMeshVar );
2384         myShapeByMeshOp->start();
2385       }
2386     }
2387   }
2388 }
2389
2390 //================================================================================
2391 /*!
2392  * \brief SLOT. Is called when Ok is pressed in SMESHGUI_ShapeByMeshDlg
2393  */
2394 //================================================================================
2395 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
2396 {
2397   if ( myShapeByMeshOp == op ) {
2398     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
2399     myDlg->show();
2400     // Select a found geometry object
2401     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
2402     if ( !aGeomVar->_is_nil() )
2403     {
2404       QString ID = aGeomVar->GetStudyEntry();
2405       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.toLatin1().data() )) {
2406         selectObject( aGeomSO );
2407         selectionDone();
2408       }
2409     }
2410   }
2411 }
2412
2413 //================================================================================
2414 /*!
2415  * \brief SLOT. Is called when Close is pressed in SMESHGUI_ShapeByMeshDlg
2416  */
2417 //================================================================================
2418 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
2419 {
2420   if ( myShapeByMeshOp == op && myDlg ) {
2421     myDlg->show();
2422   }
2423 }
2424
2425 //================================================================================
2426 /*!
2427  * \brief Selects a SObject
2428  * \param theSObj - the SObject to select
2429  */
2430 //================================================================================
2431 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
2432 {
2433   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
2434     SALOME_ListIO anIOList;
2435     if ( theSObj ) {
2436       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
2437         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
2438       anIOList.Append( anIO );
2439     }
2440     sm->setSelectedObjects( anIOList, false );
2441   }
2442 }
2443 //================================================================================
2444 /*!
2445  * \brief Create available list types of mesh
2446   * \param theTypeMesh - Output list of available types of mesh
2447  */
2448 //================================================================================
2449 void SMESHGUI_MeshOp::createMeshTypeList( QStringList& theTypeMesh)
2450 {
2451   theTypeMesh.clear();
2452   theTypeMesh.append( tr( "MT_ANY" ) );
2453   if ( myIsOnGeometry &&  ( myMaxShapeDim >= 2 || myMaxShapeDim == -1 ) )
2454   {
2455     theTypeMesh.append( tr( "MT_TRIANGULAR" ) );
2456     theTypeMesh.append( tr( "MT_QUADRILATERAL" ) );
2457   }
2458   if ( myIsOnGeometry && ( myMaxShapeDim == 3 || myMaxShapeDim == -1 ) )
2459   {
2460     theTypeMesh.append( tr( "MT_TETRAHEDRAL" ) );
2461     theTypeMesh.append( tr( "MT_HEXAHEDRAL" ) );
2462   }
2463
2464 }
2465 //================================================================================
2466 /*!
2467  * \brief Set available types of mesh
2468   * \param theTypeMesh - List of available types of mesh
2469  */
2470 //================================================================================
2471 void SMESHGUI_MeshOp::setAvailableMeshType( const QStringList& theTypeMesh )
2472 {
2473   myDlg->setAvailableMeshType( theTypeMesh );
2474 }
2475
2476 //================================================================================
2477 /*!
2478  * \brief SLOT. Is called when the user select type of mesh
2479   * \param theTabIndex - Index of current active tab
2480   * \param theIndex - Index of current type of mesh
2481  */
2482 //================================================================================
2483 void SMESHGUI_MeshOp::onAlgoSetByMeshType( const int theTabIndex, const int theIndex)
2484 {
2485   int aDim;
2486   if ( !myIsOnGeometry ) return;
2487   THypDataList anAvailableAlgsData;
2488   QStringList anAvailableAlgs;
2489   QString anCompareType = "ANY";
2490   bool isAvailableChoiceAlgo = false;
2491   int anCurrentAvailableAlgo = 0;
2492   bool isNone = true;
2493   switch ( theIndex ) {
2494     case MT_ANY:
2495     {
2496       for ( int dim = SMESH::DIM_2D; dim <= SMESH::DIM_3D; dim++ )
2497       {
2498         isNone = currentHyp( dim, Algo ) < 0;
2499         isAvailableChoiceAlgo = false;
2500         // retrieves a list of available algorithms from resources
2501         availableHyps( dim, Algo, anAvailableAlgs, anAvailableAlgsData );
2502         //return current algo in current tab
2503         if ( !isNone && !myAvailableHypData[dim][Algo].empty() ){
2504           for (int i = 0 ; i < anAvailableAlgsData.count(); i++)
2505           {
2506             HypothesisData* algoAny = anAvailableAlgsData.at(i);
2507             HypothesisData* algoCur = myAvailableHypData[dim][Algo].at( currentHyp( dim, Algo ) );
2508             QString tem = algoAny->Label;
2509             if ( algoAny->Label == algoCur->Label ){
2510               isAvailableChoiceAlgo = true;
2511               anCurrentAvailableAlgo = i;
2512               break;
2513             }
2514           }
2515         }
2516         else if ( !isNone ){
2517           isAvailableChoiceAlgo = true;
2518           anCurrentAvailableAlgo = currentHyp( dim, Algo );
2519         }
2520         //set new algorithm list and select the current algorithm
2521         myAvailableHypData[dim][Algo] = anAvailableAlgsData;
2522         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailableAlgs );
2523         if ( isAvailableChoiceAlgo )
2524           setCurrentHyp( dim, Algo, anCurrentAvailableAlgo );
2525       }
2526       int aMaxShapeDim = ( myMaxShapeDim == -1 ) ? SMESH::DIM_3D : myMaxShapeDim;
2527       for ( int i = SMESH::DIM_0D; i <= aMaxShapeDim; i++ ) {
2528         myDlg->enableTab( i );
2529       }
2530       myDlg->setCurrentTab( theTabIndex );
2531       myDlg->setHypoSets( SMESH::GetHypothesesSets( aMaxShapeDim, "" ) );
2532     }
2533     break;
2534     case MT_TRIANGULAR:{
2535       aDim = SMESH::DIM_2D;
2536       anCompareType = "TRIA";
2537     }
2538     break;
2539     case MT_QUADRILATERAL:{
2540       aDim = SMESH::DIM_2D;
2541       anCompareType = "QUAD";
2542     }
2543     break;
2544     case MT_TETRAHEDRAL:{
2545       aDim = SMESH::DIM_3D;
2546       anCompareType = "TETRA";
2547     }
2548     break;
2549     case MT_HEXAHEDRAL:{
2550       aDim = SMESH::DIM_3D;
2551       anCompareType = "HEXA";
2552     }
2553     break;
2554     default:;
2555   }
2556   if ( anCompareType != "ANY" )
2557   {
2558     QString anCurrentAlgo;
2559     bool isReqDisBound = true;
2560     isNone = currentHyp( aDim, Algo ) < 0;
2561     // retrieves a list of available algorithms from resources
2562     availableHyps( aDim, Algo, anAvailableAlgs, anAvailableAlgsData );
2563     // finding algorithm which is selected
2564     if ( !isNone && !myAvailableHypData[aDim][Algo].empty() &&
2565          myAvailableHypData[aDim][Algo].count() != anAvailableAlgsData.count() ){
2566       anCurrentAlgo = myAvailableHypData[aDim][Algo].at( currentHyp( aDim, Algo ) )->Label;
2567       isReqDisBound = myAvailableHypData[aDim][Algo].at( currentHyp( aDim, Algo ) )->InputTypes.isEmpty();
2568     }
2569     else if ( !isNone ){
2570       anCurrentAlgo = anAvailableAlgsData.at( currentHyp( aDim, Algo ) )->Label;
2571       isReqDisBound = anAvailableAlgsData.at( currentHyp( aDim, Algo ) )->InputTypes.isEmpty();
2572     }
2573     anAvailableAlgs.clear();
2574     myAvailableHypData[aDim][Algo].clear();
2575     // finding and adding algorithm depending on the type mesh
2576     for ( int i = 0 ; i < anAvailableAlgsData.count(); i++ )
2577     {
2578       HypothesisData* algoIn = anAvailableAlgsData.at( i );
2579       bool isAvailableAlgo = ( algoIn->OutputTypes.count() == 0 );
2580       QStringList::const_iterator inElemType = algoIn->OutputTypes.begin();
2581       for ( ; inElemType != algoIn->OutputTypes.end(); inElemType++ )
2582       {
2583         if ( *inElemType == anCompareType ){
2584           isAvailableAlgo = true;
2585           break;
2586         }
2587       }
2588       if ( isAvailableAlgo || algoIn->OutputTypes.count()==0 ){
2589         anAvailableAlgs.append( algoIn->Label );
2590         myAvailableHypData[aDim][Algo].append( algoIn );
2591       }
2592       //algorithm will be active, if the chosen algorithm available in the current mesh type
2593       if ( !isNone &&  isAvailableAlgo && algoIn->Label == anCurrentAlgo ){
2594         isAvailableChoiceAlgo = true;
2595         anCurrentAvailableAlgo = anAvailableAlgs.count() - 1 ;
2596       }
2597     }
2598     //set new algorithm list and select the current algorithm
2599     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailableAlgs );
2600     if ( isAvailableChoiceAlgo )
2601       setCurrentHyp( aDim, Algo, anCurrentAvailableAlgo );
2602     int aMaxShapeDim = ( myMaxShapeDim == -1 ) ? SMESH::DIM_3D : myMaxShapeDim;
2603     if ( isNone || isReqDisBound || !isAvailableChoiceAlgo ) {
2604       for ( int i = SMESH::DIM_0D; i <= aMaxShapeDim; i++ ) {
2605         if ( aDim != i ) {
2606           myDlg->disableTab( i );
2607           setCurrentHyp(i, Algo, -1);
2608         }
2609       }
2610     }
2611     else if ( !isNone ){
2612       if ( aDim == SMESH::DIM_2D){
2613         myDlg->disableTab( SMESH::DIM_3D );
2614         setCurrentHyp( SMESH::DIM_3D, Algo, -1);
2615       }
2616       for ( int i = aMaxShapeDim; i > SMESH::DIM_0D; i-- )
2617       {
2618         isReqDisBound = ( currentHyp( i, Algo ) < 0 ) ? true : myAvailableHypData[i][Algo].at( currentHyp( i, Algo ) )->InputTypes.isEmpty();
2619         if ( aMaxShapeDim != i && isReqDisBound) {
2620           for (int j = i - 1; j >= SMESH::DIM_0D; j--){
2621             myDlg->disableTab( j );
2622             setCurrentHyp( j , Algo, -1 );
2623           }
2624           break;
2625         }
2626       }
2627     }
2628     myDlg->setHypoSets( SMESH::GetHypothesesSets( aDim, anCompareType ) );
2629     myDlg->enableTab( aDim );
2630     myDlg->setCurrentTab( aDim );
2631   }
2632 }