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