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