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