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