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