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