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