Salome HOME
0021274: EDF 1872 SMESH: Save in hdf files loses 0D elements in a group
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i_1.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 //
24 //  File      : SMESH_Gen_i_1.cxx
25 //  Created   : Thu Oct 21 17:24:06 2004
26 //  Author    : Edward AGAPOV (eap)
27 //  Module    : SMESH
28 //  $Header   : $
29
30 #include "SMESH_Gen_i.hxx"
31
32 #include "SMESH_Mesh_i.hxx"
33 #include "SMESH_Hypothesis_i.hxx"
34 #include "SMESH_Algo_i.hxx"
35 #include "SMESH_Group_i.hxx"
36 #include "SMESH_subMesh_i.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 static int VARIABLE_DEBUG = 0;
48 #else
49 static int MYDEBUG = 0;
50 static int VARIABLE_DEBUG = 0;
51 #endif
52
53 //=============================================================================
54 /*!
55  *  Get...Tag [ static ]
56  *
57  *  Methods which determine SMESH data model structure
58  */
59 //=============================================================================
60
61 long SMESH_Gen_i::GetHypothesisRootTag()
62 {
63   return SMESH::Tag_HypothesisRoot;
64 }
65
66 long SMESH_Gen_i::GetAlgorithmsRootTag()
67 {
68   return SMESH::Tag_AlgorithmsRoot;
69 }
70
71 long SMESH_Gen_i::GetRefOnShapeTag()
72 {
73   return SMESH::Tag_RefOnShape;
74 }
75
76 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
77 {
78   return SMESH::Tag_RefOnAppliedHypothesis;
79 }
80
81 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
82 {
83   return SMESH::Tag_RefOnAppliedAlgorithms;
84 }
85
86 long SMESH_Gen_i::GetSubMeshOnVertexTag()
87 {
88   return SMESH::Tag_SubMeshOnVertex;
89 }
90
91 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
92 {
93   return SMESH::Tag_SubMeshOnEdge;
94 }
95
96 long SMESH_Gen_i::GetSubMeshOnFaceTag()
97 {
98   return SMESH::Tag_SubMeshOnFace;
99 }
100
101 long SMESH_Gen_i::GetSubMeshOnSolidTag()
102 {
103   return SMESH::Tag_SubMeshOnSolid;
104 }
105
106 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
107 {
108   return SMESH::Tag_SubMeshOnCompound;
109 }
110
111 long SMESH_Gen_i::GetSubMeshOnWireTag()
112 {
113   return SMESH::Tag_SubMeshOnWire;
114 }
115
116 long SMESH_Gen_i::GetSubMeshOnShellTag()
117 {
118   return SMESH::Tag_SubMeshOnShell;
119 }
120
121 long SMESH_Gen_i::GetNodeGroupsTag()
122 {
123   return SMESH::Tag_NodeGroups;
124 }
125
126 long SMESH_Gen_i::GetEdgeGroupsTag()
127 {
128   return SMESH::Tag_EdgeGroups;
129 }
130
131 long SMESH_Gen_i::GetFaceGroupsTag()
132 {
133   return SMESH::Tag_FaceGroups;
134 }
135
136 long SMESH_Gen_i::GetVolumeGroupsTag()
137 {
138   return SMESH::Tag_VolumeGroups;
139 }
140
141 long SMESH_Gen_i::Get0DElementsGroupsTag()
142 {
143   return SMESH::Tag_0DElementsGroups;
144 }
145
146 //=============================================================================
147 /*!
148  *  SMESH_Gen_i::CanPublishInStudy
149  *
150  *  Returns true if object can be published in the study
151  */
152 //=============================================================================
153
154 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
155 {
156   if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
157   if(CORBA::is_nil(myCurrentStudy))
158     return false;
159   
160   SMESH::SMESH_Mesh_var aMesh       = SMESH::SMESH_Mesh::_narrow(theIOR);
161   if( !aMesh->_is_nil() )
162     return true;
163
164   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
165   if( !aSubMesh->_is_nil() )
166     return true;
167
168   SMESH::SMESH_Hypothesis_var aHyp  = SMESH::SMESH_Hypothesis::_narrow(theIOR);
169   if( !aHyp->_is_nil() )
170     return true;
171
172   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
173   if( !aGroup->_is_nil() )
174     return true;
175
176   if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
177   return false;
178 }
179
180 //=======================================================================
181 //function : ObjectToSObject
182 //purpose  : 
183 //=======================================================================
184
185 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
186                                                    CORBA::Object_ptr   theObject)
187 {
188   SALOMEDS::SObject_var aSO;
189   if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
190   {
191     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
192     aSO = theStudy->FindObjectIOR( objStr.in() );
193   }
194   return aSO._retn();
195 }
196
197 //=======================================================================
198 //function : objectToServant
199 //purpose  : 
200 //=======================================================================
201
202 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
203 {
204   return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
205 }
206
207 //=======================================================================
208 //function : ShapeToGeomObject
209 //purpose  : 
210 //=======================================================================
211
212 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
213 {
214   GEOM::GEOM_Object_var aShapeObj;
215   if ( !theShape.IsNull() ) {
216     GEOM_Client* aClient = GetShapeReader();
217     TCollection_AsciiString IOR;
218     if ( aClient && aClient->Find( theShape, IOR ))
219     {
220       CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
221       aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
222     }
223   }
224   return aShapeObj._retn();
225 }
226
227 //=======================================================================
228 //function : GeomObjectToShape
229 //purpose  : 
230 //=======================================================================
231
232 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
233 {
234   TopoDS_Shape S;
235   if ( !theGeomObject->_is_nil() ) {
236     GEOM_Client* aClient = GetShapeReader();
237     GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
238     if ( aClient && !aGeomEngine->_is_nil () )
239       S = aClient->GetShape( aGeomEngine, theGeomObject );
240   }
241   return S;
242 }
243
244 //=======================================================================
245 //function : publish
246 //purpose  : 
247 //=======================================================================
248
249 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr   theStudy,
250                                      CORBA::Object_ptr     theIOR,
251                                      SALOMEDS::SObject_ptr theFatherObject,
252                                      const int             theTag = 0,
253                                      const char*           thePixMap = 0,
254                                      const bool            theSelectable = true)
255 {
256   SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
257   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
258   if ( SO->_is_nil() ) {
259     if ( theTag == 0 )
260       SO = aStudyBuilder->NewObject( theFatherObject );
261     else if ( !theFatherObject->FindSubObject( theTag, SO ))
262       SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
263   }
264
265   SALOMEDS::GenericAttribute_var anAttr;
266   if ( !CORBA::is_nil( theIOR )) {
267     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
268     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
269     SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
270   }
271   if ( thePixMap ) {
272     anAttr  = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
273     SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
274     pm->SetPixMap( thePixMap );
275   }
276   if ( !theSelectable ) {
277     anAttr   = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
278     SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
279   }
280   return SO._retn();
281 }
282
283 //=======================================================================
284 //function : setName
285 //purpose  : 
286 //=======================================================================
287
288 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
289                           const char*           theName,
290                           const char*           theDefaultName)
291 {
292   if ( !theSObject->_is_nil() ) {
293     SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
294     SALOMEDS::GenericAttribute_var anAttr =
295       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
296     SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
297     if ( theName && strlen( theName ) != 0 )
298       aNameAttr->SetValue( theName );
299     else {
300       CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
301       if ( strlen( curName ) == 0 ) {
302         TCollection_AsciiString aName( (char*) theDefaultName );
303         aName += TCollection_AsciiString("_") + TCollection_AsciiString( theSObject->Tag() );
304         aNameAttr->SetValue( aName.ToCString() );
305       }
306     }
307   }
308 }
309
310 //=======================================================================
311 //function : SetPixMap
312 //purpose  : 
313 //=======================================================================
314
315 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
316                             const char*           thePixMap)
317 {
318   if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
319   {
320     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
321     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
322     SALOMEDS::GenericAttribute_var anAttr =
323       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
324     SALOMEDS::AttributePixMap_var aPMAttr = SALOMEDS::AttributePixMap::_narrow( anAttr );
325     aPMAttr->SetPixMap( thePixMap );
326   }
327 }
328
329 //=======================================================================
330 //function : addReference
331 //purpose  : 
332 //=======================================================================
333
334 static void addReference (SALOMEDS::Study_ptr   theStudy,
335                           SALOMEDS::SObject_ptr theSObject,
336                           CORBA::Object_ptr     theToObject,
337                           int                   theTag = 0)
338 {
339   SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
340   if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
341     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
342     SALOMEDS::SObject_var aReferenceSO;
343     if ( !theTag ) {
344       // check if the reference to theToObject already exists
345       // and find a free label for the reference object
346       bool isReferred = false;
347       int tag = 1;
348       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
349       for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
350         if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
351           if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
352             isReferred = true;
353         }
354         else if ( !theTag ) {
355           SALOMEDS::GenericAttribute_var anAttr;
356           if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
357             theTag = tag;
358         }
359       }
360       if ( isReferred )
361         return;
362       if ( !theTag )
363         theTag = tag;
364     }
365     if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
366       aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
367     aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
368   }
369 }
370
371 //=============================================================================
372 /*!
373  *  SMESH_Gen_i::PublishInStudy
374  *
375  *  Publish object in the study
376  */
377 //=============================================================================
378
379 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
380                                                   SALOMEDS::SObject_ptr theSObject,
381                                                   CORBA::Object_ptr     theIOR,
382                                                   const char*           theName)
383      throw (SALOME::SALOME_Exception)
384 {
385   Unexpect aCatch(SALOME_SalomeException);
386   SALOMEDS::SObject_var aSO;
387   if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
388     return aSO._retn();
389   if(MYDEBUG) MESSAGE("PublishInStudy");
390
391   // Publishing a mesh
392   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
393   if( !aMesh->_is_nil() )
394     aSO = PublishMesh( theStudy, aMesh, theName );
395
396   // Publishing a sub-mesh
397   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
398   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
399     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
400     aMesh = aSubMesh->GetFather();
401     aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
402   }
403
404   // Publishing a hypothesis or algorithm
405   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
406   if ( aSO->_is_nil() && !aHyp->_is_nil() )
407     aSO = PublishHypothesis( theStudy, aHyp );
408
409   // Publishing a group
410   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
411   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
412     GEOM::GEOM_Object_var aShapeObject;
413     aMesh = aGroup->GetMesh();
414     aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
415   }
416   if(MYDEBUG) MESSAGE("PublishInStudy_END");
417
418   return aSO._retn();
419 }
420
421 //=======================================================================
422 //function : PublishComponent
423 //purpose  : 
424 //=======================================================================
425
426 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
427 {
428   if ( CORBA::is_nil( theStudy ))
429     return SALOMEDS::SComponent::_nil();
430   if(MYDEBUG) MESSAGE("PublishComponent");
431
432   SALOMEDS::SComponent_var father =
433     SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
434   if ( !CORBA::is_nil( father ) )
435     return father._retn();
436
437   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
438     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
439   if ( CORBA::is_nil( aCat ) )
440     return father._retn();
441
442   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
443   if ( CORBA::is_nil( aComp ) )
444     return father._retn();
445
446   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
447   SALOMEDS::GenericAttribute_var anAttr;
448   SALOMEDS::AttributePixMap_var  aPixmap;
449
450   father  = aStudyBuilder->NewComponent( ComponentDataType() );
451   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
452   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
453   aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
454   aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
455   SetName( father, aComp->componentusername(), "MESH" );
456   if(MYDEBUG) MESSAGE("PublishComponent--END");
457
458   return father._retn();
459 }
460
461 //=============================================================================
462 /*!
463  *  findMaxChildTag [ static internal ]
464  *
465  *  Finds maximum child tag for the given object
466  */
467 //=============================================================================
468
469 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
470 {
471   long aTag = 0;
472   if ( !theSObject->_is_nil() ) {
473     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
474     if ( !aStudy->_is_nil() ) {
475       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
476       for ( ; anIter->More(); anIter->Next() ) {
477         long nTag = anIter->Value()->Tag();
478         if ( nTag > aTag )
479           aTag = nTag;
480       }
481     }
482   }
483   return aTag;
484 }
485
486 //=======================================================================
487 //function : PublishMesh
488 //purpose  : 
489 //=======================================================================
490
491 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr   theStudy,
492                                                 SMESH::SMESH_Mesh_ptr theMesh,
493                                                 const char*           theName)
494 {
495   if ( CORBA::is_nil( theStudy ) ||
496        CORBA::is_nil( theMesh ))
497     return SALOMEDS::SComponent::_nil();
498   if(MYDEBUG) MESSAGE("PublishMesh--IN");
499
500   // find or publish a mesh
501
502   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
503   if ( aMeshSO->_is_nil() )
504   {
505     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
506     if ( father->_is_nil() )
507       return aMeshSO._retn();
508
509     // Find correct free tag
510     long aTag = findMaxChildTag( father.in() );
511     if ( aTag <= GetAlgorithmsRootTag() )
512       aTag = GetAlgorithmsRootTag() + 1;
513     else
514       aTag++;
515
516     aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
517     if ( aMeshSO->_is_nil() )
518       return aMeshSO._retn();
519   }
520   SetName( aMeshSO, theName, "Mesh" );
521
522   // Add shape reference
523
524   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
525   if ( !CORBA::is_nil( aShapeObject )) {
526     addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
527
528     // Publish global hypotheses
529
530     SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
531     for ( int i = 0; i < hypList->length(); i++ ) {
532       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
533       PublishHypothesis( theStudy, aHyp );
534       AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
535     }
536   }
537
538   // Publish submeshes
539
540   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
541   if ( !mesh_i )
542     return aMeshSO._retn();
543   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
544   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
545   for ( ; subIt != subMap.end(); subIt++ ) {
546     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
547     if ( !CORBA::is_nil( aSubMesh )) {
548       aShapeObject = aSubMesh->GetSubShape();
549       PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
550     }
551   }
552
553   // Publish groups
554   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
555   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
556   for ( ; it != grMap.end(); it++ )
557   {
558     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
559     if ( !aGroup->_is_nil() ) {
560       GEOM::GEOM_Object_var  aShapeObj;
561       SMESH::SMESH_GroupOnGeom_var aGeomGroup =
562         SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
563       if ( !aGeomGroup->_is_nil() )
564         aShapeObj = aGeomGroup->GetShape();
565       PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
566     }
567   }
568
569   if(MYDEBUG) MESSAGE("PublishMesh_END");
570   return aMeshSO._retn();
571 }
572
573 //=======================================================================
574 //function : PublishSubMesh
575 //purpose  : 
576 //=======================================================================
577
578 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
579                                                    SMESH::SMESH_Mesh_ptr    theMesh,
580                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
581                                                    GEOM::GEOM_Object_ptr    theShapeObject,
582                                                    const char*              theName)
583 {
584   if (theStudy->_is_nil() || theMesh->_is_nil() ||
585       theSubMesh->_is_nil() || theShapeObject->_is_nil() )
586     return SALOMEDS::SObject::_nil();
587
588   SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
589   if ( aSubMeshSO->_is_nil() )
590   {
591     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
592     if ( aMeshSO->_is_nil() ) {
593       aMeshSO = PublishMesh( theStudy, theMesh );
594       if ( aMeshSO->_is_nil())
595         return SALOMEDS::SObject::_nil();
596     }
597     // Find submesh sub-tree tag
598     long aRootTag;
599     const char* aRootName = "";
600     switch ( theShapeObject->GetShapeType() ) {
601     case GEOM::VERTEX:
602       aRootTag  = GetSubMeshOnVertexTag();
603       aRootName = "SubMeshes on Vertex";
604       break;
605     case GEOM::EDGE:
606       aRootTag  = GetSubMeshOnEdgeTag();
607       aRootName = "SubMeshes on Edge";
608       break;
609     case GEOM::WIRE:
610       aRootTag  = GetSubMeshOnWireTag();
611       aRootName = "SubMeshes on Wire";
612       break;
613     case GEOM::FACE:
614       aRootTag  = GetSubMeshOnFaceTag();
615       aRootName = "SubMeshes on Face";    
616       break;
617     case GEOM::SHELL:
618       aRootTag  = GetSubMeshOnShellTag();
619       aRootName = "SubMeshes on Shell";   
620       break;
621     case GEOM::SOLID:
622       aRootTag  = GetSubMeshOnSolidTag();
623       aRootName = "SubMeshes on Solid";
624       break;
625     default:
626       aRootTag  = GetSubMeshOnCompoundTag();
627       aRootName = "SubMeshes on Compound";
628       break;
629     }
630
631     // Find or create submesh root
632     SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
633                                              aMeshSO, aRootTag, 0, false );
634     SetName( aRootSO, aRootName );
635
636     // Add new submesh to corresponding sub-tree
637     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, "ICON_SMESH_TREE_MESH_WARN");
638     if ( aSubMeshSO->_is_nil() )
639       return aSubMeshSO._retn();
640   }
641   SetName( aSubMeshSO, theName, "SubMesh" );
642
643   // Add reference to theShapeObject
644
645   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
646
647   // Publish hypothesis
648
649   SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
650   if ( hypList )
651     for ( int i = 0; i < hypList->length(); i++ ) {
652       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
653       PublishHypothesis( theStudy, aHyp );
654       AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
655     }
656
657   return aSubMeshSO._retn();
658 }
659
660 //=======================================================================
661 //function : PublishGroup
662 //purpose  : 
663 //=======================================================================
664
665 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
666                                                  SMESH::SMESH_Mesh_ptr  theMesh,
667                                                  SMESH::SMESH_GroupBase_ptr theGroup,
668                                                  GEOM::GEOM_Object_ptr  theShapeObject,
669                                                  const char*            theName)
670 {
671   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
672     return SALOMEDS::SObject::_nil();
673
674   SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
675   if ( aGroupSO->_is_nil() )
676   {
677     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
678     if ( aMeshSO->_is_nil() ) {
679       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
680       if ( aMeshSO->_is_nil())
681         return SALOMEDS::SObject::_nil();
682     }
683     int aType = (int)theGroup->GetType();
684     const char* aRootNames[] = {
685       "Compound Groups", "Groups of Nodes", "Groups of Edges",
686       "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements" };
687
688     // Currently, groups with heterogenous content are not supported
689     if ( aType != SMESH::ALL ) {
690       long aRootTag = GetNodeGroupsTag() + aType - 1;
691
692       // Find or create groups root
693       SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
694                                                aMeshSO, aRootTag, 0, false );
695       if ( aType < 6 )
696         SetName( aRootSO, aRootNames[aType] );
697
698       // Add new group to corresponding sub-tree
699       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, "ICON_SMESH_TREE_GROUP" );
700     }
701     if ( aGroupSO->_is_nil() )
702       return aGroupSO._retn();
703   }
704
705   SetName( aGroupSO, theName, "Group" );
706
707   //Add reference to geometry
708   if ( !theShapeObject->_is_nil() )
709     addReference( theStudy, aGroupSO, theShapeObject, 1 );
710
711   return aGroupSO._retn();
712 }
713
714 //=======================================================================
715 //function : PublishHypothesis
716 //purpose  : 
717 //=======================================================================
718
719 SALOMEDS::SObject_ptr
720   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
721                                   SMESH::SMESH_Hypothesis_ptr theHyp,
722                                   const char*                 theName)
723 {
724   if(MYDEBUG) MESSAGE("PublishHypothesis")
725   if (theStudy->_is_nil() || theHyp->_is_nil())
726     return SALOMEDS::SObject::_nil();
727
728   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
729   if ( aHypSO->_is_nil() )
730   {
731     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
732     if ( father->_is_nil() )
733       return aHypSO._retn();
734
735     //Find or Create Hypothesis root
736     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
737     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
738     SALOMEDS::SObject_var aRootSO =
739       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
740                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
741     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
742
743     // Add New Hypothesis
744     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
745     aPmName += theHyp->GetName();
746     // prepend plugin name to pixmap name
747     string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
748     if ( pluginName != "StdMeshers" )
749       aPmName = pluginName + "::" + aPmName;
750     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
751   }
752
753   if ( !aHypSO->_is_nil() ) {
754     CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
755     SetName( aHypSO, theName, aHypName );
756   }
757
758   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
759   return aHypSO._retn();
760 }
761
762 //=======================================================================
763 //function : GetMeshOrSubmeshByShape
764 //purpose  : 
765 //=======================================================================
766
767 SALOMEDS::SObject_ptr
768   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
769                                         SMESH::SMESH_Mesh_ptr theMesh,
770                                         GEOM::GEOM_Object_ptr theShape)
771 {
772   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
773   SALOMEDS::SObject_var aMeshOrSubMesh;
774   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
775     return aMeshOrSubMesh._retn();
776   
777   TopoDS_Shape aShape;
778   if(theMesh->HasShapeToMesh())
779     aShape = GeomObjectToShape( theShape );
780   else
781     aShape = SMESH_Mesh::PseudoShape();
782
783   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
784
785   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
786     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
787     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
788       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
789     else {
790       int shapeID = meshDS->ShapeToIndex( aShape );
791       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
792       if ( !aSubMesh->_is_nil() )
793         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
794     }
795   }
796   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
797   return aMeshOrSubMesh._retn();
798 }
799
800 //=======================================================================
801 //function : AddHypothesisToShape
802 //purpose  : 
803 //=======================================================================
804
805 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
806                                        SMESH::SMESH_Mesh_ptr       theMesh,
807                                        GEOM::GEOM_Object_ptr       theShape,
808                                        SMESH::SMESH_Hypothesis_ptr theHyp)
809 {
810   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
811   if (theStudy->_is_nil() || theMesh->_is_nil() ||
812       theHyp->_is_nil() || (theShape->_is_nil()
813                             && theMesh->HasShapeToMesh()) )
814     return false;
815
816   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
817   if ( aMeshSO->_is_nil() )
818     aMeshSO = PublishMesh( theStudy, theMesh );
819   SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
820   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
821     return false;
822
823   // Find a mesh or submesh refering to theShape
824   SALOMEDS::SObject_var aMeshOrSubMesh =
825     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
826   if ( aMeshOrSubMesh->_is_nil() )
827   {
828     // publish submesh
829     TopoDS_Shape aShape = GeomObjectToShape( theShape );
830     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
831     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
832       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
833       int shapeID = meshDS->ShapeToIndex( aShape );
834       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
835       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
836     }
837     if ( aMeshOrSubMesh->_is_nil() )
838       return false;
839   }
840
841   //Find or Create Applied Hypothesis root
842   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
843   SALOMEDS::SObject_var AHR =
844     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
845              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
846              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
847   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
848   if ( AHR->_is_nil() )
849     return false;
850
851   addReference( theStudy, AHR, theHyp );
852   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
853   return true;
854 }
855
856 //=======================================================================
857 //function : RemoveHypothesisFromShape
858 //purpose  : 
859 //=======================================================================
860
861 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
862                                             SMESH::SMESH_Mesh_ptr       theMesh,
863                                             GEOM::GEOM_Object_ptr       theShape,
864                                             SMESH::SMESH_Hypothesis_ptr theHyp)
865 {
866   if (theStudy->_is_nil() || theMesh->_is_nil() ||
867       theHyp->_is_nil() || (theShape->_is_nil()
868                             && theMesh->HasShapeToMesh()))
869     return false;
870
871   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
872   if ( aHypSO->_is_nil() )
873     return false;
874
875   // Find a mesh or submesh refering to theShape
876   SALOMEDS::SObject_var aMeshOrSubMesh =
877     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
878   if ( aMeshOrSubMesh->_is_nil() )
879     return false;
880
881   // Find and remove a reference to aHypSO
882   SALOMEDS::SObject_var aRef, anObj;
883   CORBA::String_var     anID = CORBA::string_dup( aHypSO->GetID() );
884   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
885   for ( it->InitEx( true ); it->More(); it->Next() ) {
886     anObj = it->Value();
887     if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
888       theStudy->NewBuilder()->RemoveObject( anObj );
889       break;
890     }
891   }
892   return true;
893 }
894
895 //=======================================================================
896 //function : UpdateParameters
897 //purpose  : 
898 //=======================================================================
899 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
900 {
901
902   if(VARIABLE_DEBUG)
903     cout<<"UpdateParameters : "<<theParameters<<endl;
904   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
905   if(aStudy->_is_nil() || CORBA::is_nil(theObject)) 
906     return;
907
908   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
909   if(aSObj->_is_nil())  
910     return;
911
912   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
913
914   SALOMEDS::GenericAttribute_var aFindAttr;
915   bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
916   if(VARIABLE_DEBUG)
917     cout<<"Find Attribute "<<hasAttr<<endl;
918
919   SALOMEDS::GenericAttribute_var anAttr;
920   anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
921   SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
922
923   CORBA::String_var oldparVar = aStringAttr->Value();
924   CORBA::String_var inpparVar = ParseParameters(theParameters);
925   TCollection_AsciiString aNewParams;
926   TCollection_AsciiString aOldParameters(oldparVar.inout());
927   TCollection_AsciiString anInputParams(inpparVar.inout());
928   if(!hasAttr)
929     aNewParams = anInputParams;
930   else 
931     {
932       int pos = aOldParameters.SearchFromEnd("|");
933       if(pos==-1) pos = 0;
934       TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
935       TCollection_AsciiString previousParam(previousParamFull);
936       TCollection_AsciiString theRepet("1");
937       pos = previousParam.SearchFromEnd(";*=");
938       if(pos >= 0)
939         {
940           theRepet = previousParam.Split(pos+2);
941           pos = pos-1;
942           if(pos==-1) pos = 0;
943           previousParam.Split(pos);
944         }
945       if(previousParam == anInputParams)
946         {
947           theRepet = theRepet.IntegerValue()+1;
948           aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
949         }
950       else
951         {
952           aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
953         }
954     }
955
956   if(VARIABLE_DEBUG)
957   {
958     cout<<"Input Parameters : "<<anInputParams<<endl;
959     cout<<"Old Parameters : "<<aOldParameters<<endl;
960     cout<<"New Parameters : "<<aNewParams<<endl;
961   }
962
963   aStringAttr->SetValue( aNewParams.ToCString() );
964 }
965
966 //=======================================================================
967 //function : ParseParameters
968 //purpose  : 
969 //=======================================================================
970 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
971 {
972   //const char* aParameters = theParameters;
973 //   const char* aParameters = CORBA::string_dup(theParameters);
974   TCollection_AsciiString anInputParams;
975   SALOMEDS::Study_var aStudy = GetCurrentStudy();
976   if( !aStudy->_is_nil() ) {
977 //     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
978 //     for(int j=0;j<aSections->length();j++) {
979 //       SALOMEDS::ListOfStrings aVars= aSections[j];
980 //       for(int i=0;i<aVars.length();i++ ) {
981 //         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
982 //           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
983 //         if(i != aVars.length()-1)
984 //           anInputParams+=":";
985 //       }
986 //       if(j!=aSections->length()-1)
987 //         anInputParams+="|";
988 //     }
989     TCollection_AsciiString paramStr( theParameters );
990     static TCollection_AsciiString separators(":|");
991     int beg = 0, end;
992     char sep, *pParams = (char*)paramStr.ToCString();
993     while ( beg < paramStr.Length() )
994     {
995       end = beg-1;
996       while ( ++end < paramStr.Length() )
997         if ( pParams[end] == ':' || pParams[end] == '|')
998           break;
999       if ( end < paramStr.Length())
1000       {
1001         sep = pParams[end];
1002         pParams[end] = '\0';
1003       }
1004       if ( aStudy->IsVariable( pParams+beg ))
1005         anInputParams += pParams+beg;
1006       if ( end < paramStr.Length() )
1007         anInputParams += sep;
1008       else
1009         break;
1010       beg = end + 1;
1011     }
1012   }
1013   return CORBA::string_dup(anInputParams.ToCString());
1014 }
1015
1016 //=======================================================================
1017 //function : GetParameters
1018 //purpose  : 
1019 //=======================================================================
1020 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1021 {
1022   TCollection_AsciiString aResult;
1023
1024   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
1025   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
1026
1027   if(!aStudy->_is_nil() && 
1028      !CORBA::is_nil(theObject) && 
1029      !aSObj->_is_nil()){
1030     
1031     SALOMEDS::GenericAttribute_var anAttr;
1032     if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
1033       aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
1034     }
1035   }
1036   
1037   return CORBA::string_dup( aResult.ToCString() );
1038 }