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