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