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