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