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