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