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