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