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