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