Salome HOME
NPAL16198: EDF462: Submeshes creation duplicate algorithms and hypotheses. Refix.
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i_1.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 // File      : SMESH_Gen_i_1.cxx
24 // Created   : Thu Oct 21 17:24:06 2004
25 // Author    : Edward AGAPOV (eap)
26 // Module    : SMESH
27 // $Header: 
28
29 #include "SMESH_Gen_i.hxx"
30
31 #include "SMESH_Mesh_i.hxx"
32 #include "SMESH_Hypothesis_i.hxx"
33 #include "SMESH_Algo_i.hxx"
34 #include "SMESH_Group_i.hxx"
35 #include "SMESH_subMesh_i.hxx"
36
37 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
38
39 #include "utilities.h"
40 #include "Utils_ExceptHandlers.hxx"
41
42 #include <TCollection_AsciiString.hxx>
43
44 #ifdef _DEBUG_
45 static int MYDEBUG = 0;
46 #else
47 static int MYDEBUG = 0;
48 #endif
49
50 //=============================================================================
51 /*!
52  *  Get...Tag [ static ]
53  *
54  *  Methods which determine SMESH data model structure
55  */
56 //=============================================================================
57
58 long SMESH_Gen_i::GetHypothesisRootTag()
59 {
60   return SMESH::Tag_HypothesisRoot;
61 }
62
63 long SMESH_Gen_i::GetAlgorithmsRootTag()
64 {
65   return SMESH::Tag_AlgorithmsRoot;
66 }
67
68 long SMESH_Gen_i::GetRefOnShapeTag()
69 {
70   return SMESH::Tag_RefOnShape;
71 }
72
73 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
74 {
75   return SMESH::Tag_RefOnAppliedHypothesis;
76 }
77
78 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
79 {
80   return SMESH::Tag_RefOnAppliedAlgorithms;
81 }
82
83 long SMESH_Gen_i::GetSubMeshOnVertexTag()
84 {
85   return SMESH::Tag_SubMeshOnVertex;
86 }
87
88 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
89 {
90   return SMESH::Tag_SubMeshOnEdge;
91 }
92
93 long SMESH_Gen_i::GetSubMeshOnFaceTag()
94 {
95   return SMESH::Tag_SubMeshOnFace;
96 }
97
98 long SMESH_Gen_i::GetSubMeshOnSolidTag()
99 {
100   return SMESH::Tag_SubMeshOnSolid;
101 }
102
103 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
104 {
105   return SMESH::Tag_SubMeshOnCompound;
106 }
107
108 long SMESH_Gen_i::GetSubMeshOnWireTag()
109 {
110   return SMESH::Tag_SubMeshOnWire;
111 }
112
113 long SMESH_Gen_i::GetSubMeshOnShellTag()
114 {
115   return SMESH::Tag_SubMeshOnShell;
116 }
117
118 long SMESH_Gen_i::GetNodeGroupsTag()
119 {
120   return SMESH::Tag_NodeGroups;
121 }
122
123 long SMESH_Gen_i::GetEdgeGroupsTag()
124 {
125   return SMESH::Tag_EdgeGroups;
126 }
127
128 long SMESH_Gen_i::GetFaceGroupsTag()
129 {
130   return SMESH::Tag_FaceGroups;
131 }
132
133 long SMESH_Gen_i::GetVolumeGroupsTag()
134 {
135   return SMESH::Tag_VolumeGroups;
136 }
137
138 //=============================================================================
139 /*!
140  *  SMESH_Gen_i::CanPublishInStudy
141  *
142  *  Returns true if object can be published in the study
143  */
144 //=============================================================================
145
146 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
147 {
148   if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
149   if(CORBA::is_nil(myCurrentStudy))
150     return false;
151   
152   SMESH::SMESH_Mesh_var aMesh       = SMESH::SMESH_Mesh::_narrow(theIOR);
153   if( !aMesh->_is_nil() )
154     return true;
155
156   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
157   if( !aSubMesh->_is_nil() )
158     return true;
159
160   SMESH::SMESH_Hypothesis_var aHyp  = SMESH::SMESH_Hypothesis::_narrow(theIOR);
161   if( !aHyp->_is_nil() )
162     return true;
163
164   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
165   if( !aGroup->_is_nil() )
166     return true;
167
168   if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
169   return false;
170 }
171
172 //=======================================================================
173 //function : ObjectToSObject
174 //purpose  : 
175 //=======================================================================
176
177 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
178                                                    CORBA::Object_ptr   theObject)
179 {
180   SALOMEDS::SObject_var aSO;
181   if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
182   {
183     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
184     aSO = theStudy->FindObjectIOR( objStr.in() );
185   }
186   return aSO._retn();
187 }
188
189 //=======================================================================
190 //function : objectToServant
191 //purpose  : 
192 //=======================================================================
193
194 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
195 {
196   return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
197 }
198
199 //=======================================================================
200 //function : ShapeToGeomObject
201 //purpose  : 
202 //=======================================================================
203
204 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
205 {
206   GEOM::GEOM_Object_var aShapeObj;
207   if ( !theShape.IsNull() ) {
208     GEOM_Client* aClient = GetShapeReader();
209     TCollection_AsciiString IOR;
210     if ( aClient && aClient->Find( theShape, IOR ))
211       aShapeObj = GEOM::GEOM_Object::_narrow
212         ( GetORB()->string_to_object( IOR.ToCString() ) );
213   }
214   return aShapeObj._retn();
215 }
216
217 //=======================================================================
218 //function : GeomObjectToShape
219 //purpose  : 
220 //=======================================================================
221
222 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
223 {
224   TopoDS_Shape S;
225   if ( !theGeomObject->_is_nil() ) {
226     GEOM_Client* aClient = GetShapeReader();
227     GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
228     if ( aClient && !aGeomEngine->_is_nil () )
229       S = aClient->GetShape( aGeomEngine, theGeomObject );
230   }
231   return S;
232 }
233
234 //=======================================================================
235 //function : publish
236 //purpose  : 
237 //=======================================================================
238
239 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr   theStudy,
240                                      CORBA::Object_ptr     theIOR,
241                                      SALOMEDS::SObject_ptr theFatherObject,
242                                      const int             theTag = 0,
243                                      const char*           thePixMap = 0,
244                                      const bool            theSelectable = true)
245 {
246   SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
247   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
248   if ( SO->_is_nil() ) {
249     if ( theTag == 0 )
250       SO = aStudyBuilder->NewObject( theFatherObject );
251     else if ( !theFatherObject->FindSubObject( theTag, SO ))
252       SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
253   }
254
255   SALOMEDS::GenericAttribute_var anAttr;
256   if ( !CORBA::is_nil( theIOR )) {
257     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
258     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
259     SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
260   }
261   if ( thePixMap ) {
262     anAttr  = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
263     SALOMEDS::AttributePixMap::_narrow( anAttr )->SetPixMap( thePixMap );
264   }
265   if ( !theSelectable ) {
266     anAttr   = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
267     SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
268   }
269   return SO._retn();
270 }
271
272 //=======================================================================
273 //function : setName
274 //purpose  : 
275 //=======================================================================
276
277 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
278                           const char*           theName,
279                           const char*           theDefaultName)
280 {
281   if ( !theSObject->_is_nil() ) {
282     SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
283     SALOMEDS::GenericAttribute_var anAttr =
284       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
285     SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
286     if ( theName && strlen( theName ) != 0 )
287       aNameAttr->SetValue( theName );
288     else {
289       CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
290       if ( strlen( curName ) == 0 ) {
291         TCollection_AsciiString aName( (char*) theDefaultName );
292         aName += TCollection_AsciiString("_") + TCollection_AsciiString( theSObject->Tag() );
293         aNameAttr->SetValue( aName.ToCString() );
294       }
295     }
296   }
297 }
298
299 //=======================================================================
300 //function : addReference
301 //purpose  : 
302 //=======================================================================
303
304 static void addReference (SALOMEDS::Study_ptr   theStudy,
305                           SALOMEDS::SObject_ptr theSObject,
306                           CORBA::Object_ptr     theToObject,
307                           int                   theTag = 0)
308 {
309   SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
310   if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
311     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
312     SALOMEDS::SObject_var aReferenceSO;
313     if ( !theTag ) {
314       // check if the reference to theToObject already exists
315       // and find a free label for the reference object
316       bool isReferred = false;
317       int tag = 1;
318       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
319       for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
320         if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
321           if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
322             isReferred = true;
323         }
324         else if ( !theTag ) {
325           SALOMEDS::GenericAttribute_var anAttr;
326           if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
327             theTag = tag;
328         }
329       }
330       if ( isReferred )
331         return;
332       if ( !theTag )
333         theTag = tag;
334     }
335     if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
336       aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
337     aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
338   }
339 }
340
341 //=============================================================================
342 /*!
343  *  SMESH_Gen_i::PublishInStudy
344  *
345  *  Publish object in the study
346  */
347 //=============================================================================
348
349 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
350                                                   SALOMEDS::SObject_ptr theSObject,
351                                                   CORBA::Object_ptr     theIOR,
352                                                   const char*           theName)
353      throw (SALOME::SALOME_Exception)
354 {
355   Unexpect aCatch(SALOME_SalomeException);
356   SALOMEDS::SObject_var aSO;
357   if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
358     return aSO._retn();
359   if(MYDEBUG) MESSAGE("PublishInStudy");
360
361   // Publishing a mesh
362   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
363   if( !aMesh->_is_nil() )
364     aSO = PublishMesh( theStudy, aMesh, theName );
365
366   // Publishing a sub-mesh
367   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
368   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
369     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
370     aMesh = aSubMesh->GetFather();
371     aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
372   }
373
374   // Publishing a hypothesis or algorithm
375   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
376   if ( aSO->_is_nil() && !aHyp->_is_nil() )
377     aSO = PublishHypothesis( theStudy, aHyp );
378
379   // Publishing a group
380   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
381   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
382     GEOM::GEOM_Object_var aShapeObject;
383     aMesh = aGroup->GetMesh();
384     aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
385   }
386   if(MYDEBUG) MESSAGE("PublishInStudy_END");
387
388   return aSO._retn();
389 }
390
391 //=======================================================================
392 //function : PublishComponent
393 //purpose  : 
394 //=======================================================================
395
396 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
397 {
398   if ( CORBA::is_nil( theStudy ))
399     return SALOMEDS::SComponent::_nil();
400   if(MYDEBUG) MESSAGE("PublishComponent");
401
402   SALOMEDS::SComponent_var father =
403     SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
404   if ( !CORBA::is_nil( father ) )
405     return father._retn();
406
407   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
408     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
409   if ( CORBA::is_nil( aCat ) )
410     return father._retn();
411
412   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
413   if ( CORBA::is_nil( aComp ) )
414     return father._retn();
415
416   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
417   SALOMEDS::GenericAttribute_var anAttr;
418   SALOMEDS::AttributePixMap_var  aPixmap;
419
420   father  = aStudyBuilder->NewComponent( ComponentDataType() );
421   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
422   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
423   aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
424   aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
425   SetName( father, aComp->componentusername(), "MESH" );
426   if(MYDEBUG) MESSAGE("PublishComponent--END");
427
428   return father._retn();
429 }
430
431 //=============================================================================
432 /*!
433  *  findMaxChildTag [ static internal ]
434  *
435  *  Finds maximum child tag for the given object
436  */
437 //=============================================================================
438
439 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
440 {
441   long aTag = 0;
442   if ( !theSObject->_is_nil() ) {
443     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
444     if ( !aStudy->_is_nil() ) {
445       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
446       for ( ; anIter->More(); anIter->Next() ) {
447         long nTag = anIter->Value()->Tag();
448         if ( nTag > aTag )
449           aTag = nTag;
450       }
451     }
452   }
453   return aTag;
454 }
455
456 //=======================================================================
457 //function : PublishMesh
458 //purpose  : 
459 //=======================================================================
460
461 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr   theStudy,
462                                                 SMESH::SMESH_Mesh_ptr theMesh,
463                                                 const char*           theName)
464 {
465   if ( CORBA::is_nil( theStudy ) ||
466        CORBA::is_nil( theMesh ))
467     return SALOMEDS::SComponent::_nil();
468   if(MYDEBUG) MESSAGE("PublishMesh--IN");
469
470   // find or publish a mesh
471
472   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
473   if ( aMeshSO->_is_nil() )
474   {
475     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
476     if ( father->_is_nil() )
477       return aMeshSO._retn();
478
479     // Find correct free tag
480     long aTag = findMaxChildTag( father.in() );
481     if ( aTag <= GetAlgorithmsRootTag() )
482       aTag = GetAlgorithmsRootTag() + 1;
483     else
484       aTag++;
485
486     aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
487     if ( aMeshSO->_is_nil() )
488       return aMeshSO._retn();
489   }
490   SetName( aMeshSO, theName, "Mesh" );
491
492   // Add shape reference
493
494   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
495   if ( !CORBA::is_nil( aShapeObject )) {
496     addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
497
498     // Publish global hypotheses
499
500     SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( aShapeObject );
501     if ( hypList )
502       for ( int i = 0; i < hypList->length(); i++ ) {
503         SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
504         PublishHypothesis( theStudy, aHyp );
505         AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
506       }
507   }
508
509   // Publish submeshes
510
511   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
512   if ( !mesh_i )
513     return aMeshSO._retn();
514   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
515   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
516   for ( ; subIt != subMap.end(); subIt++ ) {
517     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
518     if ( !CORBA::is_nil( aSubMesh )) {
519       aShapeObject = aSubMesh->GetSubShape();
520       PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
521     }
522   }
523
524   // Publish groups
525   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
526   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
527   for ( ; it != grMap.end(); it++ )
528   {
529     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
530     if ( !aGroup->_is_nil() ) {
531       GEOM::GEOM_Object_var  aShapeObj;
532       SMESH::SMESH_GroupOnGeom_var aGeomGroup =
533         SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
534       if ( !aGeomGroup->_is_nil() )
535         aShapeObj = aGeomGroup->GetShape();
536       PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
537     }
538   }
539
540   if(MYDEBUG) MESSAGE("PublishMesh_END");
541   return aMeshSO._retn();
542 }
543
544 //=======================================================================
545 //function : PublishSubMesh
546 //purpose  : 
547 //=======================================================================
548
549 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
550                                                    SMESH::SMESH_Mesh_ptr    theMesh,
551                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
552                                                    GEOM::GEOM_Object_ptr    theShapeObject,
553                                                    const char*              theName)
554 {
555   if (theStudy->_is_nil() || theMesh->_is_nil() ||
556       theSubMesh->_is_nil() || theShapeObject->_is_nil() )
557     return SALOMEDS::SObject::_nil();
558
559   SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
560   if ( aSubMeshSO->_is_nil() )
561   {
562     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
563     if ( aMeshSO->_is_nil() ) {
564       aMeshSO = PublishMesh( theStudy, theMesh );
565       if ( aMeshSO->_is_nil())
566         return SALOMEDS::SObject::_nil();
567     }
568     // Find submesh sub-tree tag
569     long aRootTag;
570     char* aRootName = "";
571     switch ( theShapeObject->GetShapeType() ) {
572     case GEOM::VERTEX:
573       aRootTag  = GetSubMeshOnVertexTag();
574       aRootName = "SubMeshes on Vertex";
575       break;
576     case GEOM::EDGE:
577       aRootTag  = GetSubMeshOnEdgeTag();
578       aRootName = "SubMeshes on Edge";
579       break;
580     case GEOM::WIRE:
581       aRootTag  = GetSubMeshOnWireTag();
582       aRootName = "SubMeshes on Wire";
583       break;
584     case GEOM::FACE:
585       aRootTag  = GetSubMeshOnFaceTag();
586       aRootName = "SubMeshes on Face";    
587       break;
588     case GEOM::SHELL:
589       aRootTag  = GetSubMeshOnShellTag();
590       aRootName = "SubMeshes on Shell";   
591       break;
592     case GEOM::SOLID:
593       aRootTag  = GetSubMeshOnSolidTag();
594       aRootName = "SubMeshes on Solid";
595       break;
596     default:
597       aRootTag  = GetSubMeshOnCompoundTag();
598       aRootName = "SubMeshes on Compound";
599       break;
600     }
601
602     // Find or create submesh root
603     SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
604                                              aMeshSO, aRootTag, 0, false );
605     SetName( aRootSO, aRootName );
606
607     // Add new submesh to corresponding sub-tree
608     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, "ICON_SMESH_TREE_MESH_WARN");
609     if ( aSubMeshSO->_is_nil() )
610       return aSubMeshSO._retn();
611   }
612   SetName( aSubMeshSO, theName, "SubMesh" );
613
614   // Add reference to theShapeObject
615
616   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
617
618   // Publish hypothesis
619
620   SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
621   if ( hypList )
622     for ( int i = 0; i < hypList->length(); i++ ) {
623       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
624       PublishHypothesis( theStudy, aHyp );
625       AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
626     }
627
628   return aSubMeshSO._retn();
629 }
630
631 //=======================================================================
632 //function : PublishGroup
633 //purpose  : 
634 //=======================================================================
635
636 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
637                                                  SMESH::SMESH_Mesh_ptr  theMesh,
638                                                  SMESH::SMESH_GroupBase_ptr theGroup,
639                                                  GEOM::GEOM_Object_ptr  theShapeObject,
640                                                  const char*            theName)
641 {
642   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
643     return SALOMEDS::SObject::_nil();
644
645   SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
646   if ( aGroupSO->_is_nil() )
647   {
648     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
649     if ( aMeshSO->_is_nil() ) {
650       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
651       if ( aMeshSO->_is_nil())
652         return SALOMEDS::SObject::_nil();
653     }
654     int aType = (int)theGroup->GetType();
655     const char* aRootNames[] = {
656       "Compound Groups", "Groups of Nodes",
657       "Groups of Edges", "Groups of Faces", "Groups of Volumes" };
658
659     // Currently, groups with heterogenous content are not supported
660     if ( aType != SMESH::ALL ) {
661       long aRootTag = GetNodeGroupsTag() + aType - 1;
662
663       // Find or create groups root
664       SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
665                                                aMeshSO, aRootTag, 0, false );
666       if ( aType < 5 )
667         SetName( aRootSO, aRootNames[aType] );
668
669       // Add new group to corresponding sub-tree
670       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, "ICON_SMESH_TREE_GROUP" );
671     }
672     if ( aGroupSO->_is_nil() )
673       return aGroupSO._retn();
674   }
675
676   SetName( aGroupSO, theName, "Group" );
677
678   //Add reference to geometry
679   if ( !theShapeObject->_is_nil() )
680     addReference( theStudy, aGroupSO, theShapeObject, 1 );
681
682   return aGroupSO._retn();
683 }
684
685 //=======================================================================
686 //function : PublishHypothesis
687 //purpose  : 
688 //=======================================================================
689
690 SALOMEDS::SObject_ptr
691   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
692                                   SMESH::SMESH_Hypothesis_ptr theHyp,
693                                   const char*                 theName)
694 {
695   if(MYDEBUG) MESSAGE("PublishHypothesis")
696   if (theStudy->_is_nil() || theHyp->_is_nil())
697     return SALOMEDS::SObject::_nil();
698
699   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
700   if ( aHypSO->_is_nil() )
701   {
702     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
703     if ( father->_is_nil() )
704       return aHypSO._retn();
705
706     //Find or Create Hypothesis root
707     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
708     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
709     SALOMEDS::SObject_var aRootSO =
710       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
711                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
712     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
713
714     // Add New Hypothesis
715     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
716     aPmName += theHyp->GetName();
717     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
718   }
719
720   if ( !aHypSO->_is_nil() ) {
721     CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
722     SetName( aHypSO, theName, aHypName );
723   }
724
725   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
726   return aHypSO._retn();
727 }
728
729 //=======================================================================
730 //function : GetMeshOrSubmeshByShape
731 //purpose  : 
732 //=======================================================================
733
734 SALOMEDS::SObject_ptr
735   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
736                                         SMESH::SMESH_Mesh_ptr theMesh,
737                                         GEOM::GEOM_Object_ptr theShape)
738 {
739   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
740   SALOMEDS::SObject_var aMeshOrSubMesh;
741   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
742     return aMeshOrSubMesh._retn();
743   
744   TopoDS_Shape aShape;
745   if(theMesh->HasShapeToMesh())
746     aShape = GeomObjectToShape( theShape );
747   else
748     aShape = SMESH_Mesh::PseudoShape();
749
750   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
751
752   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
753     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
754     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
755       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
756     else {
757       int shapeID = meshDS->ShapeToIndex( aShape );
758       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
759       if ( !aSubMesh->_is_nil() )
760         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
761     }
762   }
763   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
764   return aMeshOrSubMesh._retn();
765 }
766
767 //=======================================================================
768 //function : AddHypothesisToShape
769 //purpose  : 
770 //=======================================================================
771
772 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
773                                        SMESH::SMESH_Mesh_ptr       theMesh,
774                                        GEOM::GEOM_Object_ptr       theShape,
775                                        SMESH::SMESH_Hypothesis_ptr theHyp)
776 {
777   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
778   if (theStudy->_is_nil() || theMesh->_is_nil() ||
779       theHyp->_is_nil() || (theShape->_is_nil()
780                             && theMesh->HasShapeToMesh()) )
781     return false;
782
783   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
784   if ( aMeshSO->_is_nil() )
785     aMeshSO = PublishMesh( theStudy, theMesh );
786   SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
787   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
788     return false;
789
790   // Find a mesh or submesh refering to theShape
791   SALOMEDS::SObject_var aMeshOrSubMesh =
792     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
793   if ( aMeshOrSubMesh->_is_nil() )
794   {
795     // publish submesh
796     TopoDS_Shape aShape = GeomObjectToShape( theShape );
797     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
798     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
799       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
800       int shapeID = meshDS->ShapeToIndex( aShape );
801       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
802       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
803     }
804     if ( aMeshOrSubMesh->_is_nil() )
805       return false;
806   }
807
808   //Find or Create Applied Hypothesis root
809   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
810   SALOMEDS::SObject_var AHR =
811     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
812              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
813              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
814   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
815   if ( AHR->_is_nil() )
816     return false;
817
818   addReference( theStudy, AHR, theHyp );
819   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
820   return true;
821 }
822
823 //=======================================================================
824 //function : RemoveHypothesisFromShape
825 //purpose  : 
826 //=======================================================================
827
828 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
829                                             SMESH::SMESH_Mesh_ptr       theMesh,
830                                             GEOM::GEOM_Object_ptr       theShape,
831                                             SMESH::SMESH_Hypothesis_ptr theHyp)
832 {
833   if (theStudy->_is_nil() || theMesh->_is_nil() ||
834       theHyp->_is_nil() || (theShape->_is_nil()
835                             && theMesh->HasShapeToMesh()))
836     return false;
837
838   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
839   if ( aHypSO->_is_nil() )
840     return false;
841
842   // Find a mesh or submesh refering to theShape
843   SALOMEDS::SObject_var aMeshOrSubMesh =
844     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
845   if ( aMeshOrSubMesh->_is_nil() )
846     return false;
847
848   // Find and remove a reference to aHypSO
849   SALOMEDS::SObject_var aRef, anObj;
850   CORBA::String_var     anID = CORBA::string_dup( aHypSO->GetID() );
851   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
852   for ( it->InitEx( true ); it->More(); it->Next() ) {
853     anObj = it->Value();
854     if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
855       theStudy->NewBuilder()->RemoveObject( anObj );
856       break;
857     }
858   }
859   return true;
860 }
861