Salome HOME
Dump Python extension
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i_1.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 // File      : SMESH_Gen_i_1.cxx
24 // Created   : Thu Oct 21 17:24:06 2004
25 // Author    : Edward AGAPOV (eap)
26 // Module    : SMESH
27 // $Header: 
28
29 #include "SMESH_Gen_i.hxx"
30
31 #include "SMESH_Mesh_i.hxx"
32 #include "SMESH_Hypothesis_i.hxx"
33 #include "SMESH_Algo_i.hxx"
34 #include "SMESH_Group_i.hxx"
35 #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 : addReference
303 //purpose  : 
304 //=======================================================================
305
306 static void addReference (SALOMEDS::Study_ptr   theStudy,
307                           SALOMEDS::SObject_ptr theSObject,
308                           CORBA::Object_ptr     theToObject,
309                           int                   theTag = 0)
310 {
311   SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
312   if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
313     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
314     SALOMEDS::SObject_var aReferenceSO;
315     if ( !theTag ) {
316       // check if the reference to theToObject already exists
317       // and find a free label for the reference object
318       bool isReferred = false;
319       int tag = 1;
320       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
321       for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
322         if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
323           if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
324             isReferred = true;
325         }
326         else if ( !theTag ) {
327           SALOMEDS::GenericAttribute_var anAttr;
328           if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
329             theTag = tag;
330         }
331       }
332       if ( isReferred )
333         return;
334       if ( !theTag )
335         theTag = tag;
336     }
337     if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
338       aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
339     aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
340   }
341 }
342
343 //=============================================================================
344 /*!
345  *  SMESH_Gen_i::PublishInStudy
346  *
347  *  Publish object in the study
348  */
349 //=============================================================================
350
351 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
352                                                   SALOMEDS::SObject_ptr theSObject,
353                                                   CORBA::Object_ptr     theIOR,
354                                                   const char*           theName)
355      throw (SALOME::SALOME_Exception)
356 {
357   Unexpect aCatch(SALOME_SalomeException);
358   SALOMEDS::SObject_var aSO;
359   if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
360     return aSO._retn();
361   if(MYDEBUG) MESSAGE("PublishInStudy");
362
363   // Publishing a mesh
364   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
365   if( !aMesh->_is_nil() )
366     aSO = PublishMesh( theStudy, aMesh, theName );
367
368   // Publishing a sub-mesh
369   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
370   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
371     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
372     aMesh = aSubMesh->GetFather();
373     aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
374   }
375
376   // Publishing a hypothesis or algorithm
377   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
378   if ( aSO->_is_nil() && !aHyp->_is_nil() )
379     aSO = PublishHypothesis( theStudy, aHyp );
380
381   // Publishing a group
382   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
383   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
384     GEOM::GEOM_Object_var aShapeObject;
385     aMesh = aGroup->GetMesh();
386     aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
387   }
388   if(MYDEBUG) MESSAGE("PublishInStudy_END");
389
390   return aSO._retn();
391 }
392
393 //=======================================================================
394 //function : PublishComponent
395 //purpose  : 
396 //=======================================================================
397
398 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
399 {
400   if ( CORBA::is_nil( theStudy ))
401     return SALOMEDS::SComponent::_nil();
402   if(MYDEBUG) MESSAGE("PublishComponent");
403
404   SALOMEDS::SComponent_var father =
405     SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
406   if ( !CORBA::is_nil( father ) )
407     return father._retn();
408
409   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
410     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
411   if ( CORBA::is_nil( aCat ) )
412     return father._retn();
413
414   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
415   if ( CORBA::is_nil( aComp ) )
416     return father._retn();
417
418   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
419   SALOMEDS::GenericAttribute_var anAttr;
420   SALOMEDS::AttributePixMap_var  aPixmap;
421
422   father  = aStudyBuilder->NewComponent( ComponentDataType() );
423   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
424   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
425   aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
426   aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
427   SetName( father, aComp->componentusername(), "MESH" );
428   if(MYDEBUG) MESSAGE("PublishComponent--END");
429
430   return father._retn();
431 }
432
433 //=============================================================================
434 /*!
435  *  findMaxChildTag [ static internal ]
436  *
437  *  Finds maximum child tag for the given object
438  */
439 //=============================================================================
440
441 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
442 {
443   long aTag = 0;
444   if ( !theSObject->_is_nil() ) {
445     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
446     if ( !aStudy->_is_nil() ) {
447       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
448       for ( ; anIter->More(); anIter->Next() ) {
449         long nTag = anIter->Value()->Tag();
450         if ( nTag > aTag )
451           aTag = nTag;
452       }
453     }
454   }
455   return aTag;
456 }
457
458 //=======================================================================
459 //function : PublishMesh
460 //purpose  : 
461 //=======================================================================
462
463 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr   theStudy,
464                                                 SMESH::SMESH_Mesh_ptr theMesh,
465                                                 const char*           theName)
466 {
467   if ( CORBA::is_nil( theStudy ) ||
468        CORBA::is_nil( theMesh ))
469     return SALOMEDS::SComponent::_nil();
470   if(MYDEBUG) MESSAGE("PublishMesh--IN");
471
472   // find or publish a mesh
473
474   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
475   if ( aMeshSO->_is_nil() )
476   {
477     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
478     if ( father->_is_nil() )
479       return aMeshSO._retn();
480
481     // Find correct free tag
482     long aTag = findMaxChildTag( father.in() );
483     if ( aTag <= GetAlgorithmsRootTag() )
484       aTag = GetAlgorithmsRootTag() + 1;
485     else
486       aTag++;
487
488     aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
489     if ( aMeshSO->_is_nil() )
490       return aMeshSO._retn();
491   }
492   SetName( aMeshSO, theName, "Mesh" );
493
494   // Add shape reference
495
496   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
497   if ( !CORBA::is_nil( aShapeObject )) {
498     addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
499
500     // Publish global hypotheses
501
502     SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( aShapeObject );
503     if ( hypList )
504       for ( int i = 0; i < hypList->length(); i++ ) {
505         SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
506         PublishHypothesis( theStudy, aHyp );
507         AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
508       }
509   }
510
511   // Publish submeshes
512
513   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
514   if ( !mesh_i )
515     return aMeshSO._retn();
516   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
517   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
518   for ( ; subIt != subMap.end(); subIt++ ) {
519     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
520     if ( !CORBA::is_nil( aSubMesh )) {
521       aShapeObject = aSubMesh->GetSubShape();
522       PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
523     }
524   }
525
526   // Publish groups
527   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
528   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
529   for ( ; it != grMap.end(); it++ )
530   {
531     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
532     if ( !aGroup->_is_nil() ) {
533       GEOM::GEOM_Object_var  aShapeObj;
534       SMESH::SMESH_GroupOnGeom_var aGeomGroup =
535         SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
536       if ( !aGeomGroup->_is_nil() )
537         aShapeObj = aGeomGroup->GetShape();
538       PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
539     }
540   }
541
542   if(MYDEBUG) MESSAGE("PublishMesh_END");
543   return aMeshSO._retn();
544 }
545
546 //=======================================================================
547 //function : PublishSubMesh
548 //purpose  : 
549 //=======================================================================
550
551 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
552                                                    SMESH::SMESH_Mesh_ptr    theMesh,
553                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
554                                                    GEOM::GEOM_Object_ptr    theShapeObject,
555                                                    const char*              theName)
556 {
557   if (theStudy->_is_nil() || theMesh->_is_nil() ||
558       theSubMesh->_is_nil() || theShapeObject->_is_nil() )
559     return SALOMEDS::SObject::_nil();
560
561   SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
562   if ( aSubMeshSO->_is_nil() )
563   {
564     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
565     if ( aMeshSO->_is_nil() ) {
566       aMeshSO = PublishMesh( theStudy, theMesh );
567       if ( aMeshSO->_is_nil())
568         return SALOMEDS::SObject::_nil();
569     }
570     // Find submesh sub-tree tag
571     long aRootTag;
572     char* aRootName = "";
573     switch ( theShapeObject->GetShapeType() ) {
574     case GEOM::VERTEX:
575       aRootTag  = GetSubMeshOnVertexTag();
576       aRootName = "SubMeshes on Vertex";
577       break;
578     case GEOM::EDGE:
579       aRootTag  = GetSubMeshOnEdgeTag();
580       aRootName = "SubMeshes on Edge";
581       break;
582     case GEOM::WIRE:
583       aRootTag  = GetSubMeshOnWireTag();
584       aRootName = "SubMeshes on Wire";
585       break;
586     case GEOM::FACE:
587       aRootTag  = GetSubMeshOnFaceTag();
588       aRootName = "SubMeshes on Face";    
589       break;
590     case GEOM::SHELL:
591       aRootTag  = GetSubMeshOnShellTag();
592       aRootName = "SubMeshes on Shell";   
593       break;
594     case GEOM::SOLID:
595       aRootTag  = GetSubMeshOnSolidTag();
596       aRootName = "SubMeshes on Solid";
597       break;
598     default:
599       aRootTag  = GetSubMeshOnCompoundTag();
600       aRootName = "SubMeshes on Compound";
601       break;
602     }
603
604     // Find or create submesh root
605     SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
606                                              aMeshSO, aRootTag, 0, false );
607     SetName( aRootSO, aRootName );
608
609     // Add new submesh to corresponding sub-tree
610     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, "ICON_SMESH_TREE_MESH_WARN");
611     if ( aSubMeshSO->_is_nil() )
612       return aSubMeshSO._retn();
613   }
614   SetName( aSubMeshSO, theName, "SubMesh" );
615
616   // Add reference to theShapeObject
617
618   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
619
620   // Publish hypothesis
621
622   SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
623   if ( hypList )
624     for ( int i = 0; i < hypList->length(); i++ ) {
625       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
626       PublishHypothesis( theStudy, aHyp );
627       AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
628     }
629
630   return aSubMeshSO._retn();
631 }
632
633 //=======================================================================
634 //function : PublishGroup
635 //purpose  : 
636 //=======================================================================
637
638 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
639                                                  SMESH::SMESH_Mesh_ptr  theMesh,
640                                                  SMESH::SMESH_GroupBase_ptr theGroup,
641                                                  GEOM::GEOM_Object_ptr  theShapeObject,
642                                                  const char*            theName)
643 {
644   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
645     return SALOMEDS::SObject::_nil();
646
647   SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
648   if ( aGroupSO->_is_nil() )
649   {
650     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
651     if ( aMeshSO->_is_nil() ) {
652       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
653       if ( aMeshSO->_is_nil())
654         return SALOMEDS::SObject::_nil();
655     }
656     int aType = (int)theGroup->GetType();
657     const char* aRootNames[] = {
658       "Compound Groups", "Groups of Nodes",
659       "Groups of Edges", "Groups of Faces", "Groups of Volumes" };
660
661     // Currently, groups with heterogenous content are not supported
662     if ( aType != SMESH::ALL ) {
663       long aRootTag = GetNodeGroupsTag() + aType - 1;
664
665       // Find or create groups root
666       SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
667                                                aMeshSO, aRootTag, 0, false );
668       if ( aType < 5 )
669         SetName( aRootSO, aRootNames[aType] );
670
671       // Add new group to corresponding sub-tree
672       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, "ICON_SMESH_TREE_GROUP" );
673     }
674     if ( aGroupSO->_is_nil() )
675       return aGroupSO._retn();
676   }
677
678   SetName( aGroupSO, theName, "Group" );
679
680   //Add reference to geometry
681   if ( !theShapeObject->_is_nil() )
682     addReference( theStudy, aGroupSO, theShapeObject, 1 );
683
684   return aGroupSO._retn();
685 }
686
687 //=======================================================================
688 //function : PublishHypothesis
689 //purpose  : 
690 //=======================================================================
691
692 SALOMEDS::SObject_ptr
693   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
694                                   SMESH::SMESH_Hypothesis_ptr theHyp,
695                                   const char*                 theName)
696 {
697   if(MYDEBUG) MESSAGE("PublishHypothesis")
698   if (theStudy->_is_nil() || theHyp->_is_nil())
699     return SALOMEDS::SObject::_nil();
700
701   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
702   if ( aHypSO->_is_nil() )
703   {
704     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
705     if ( father->_is_nil() )
706       return aHypSO._retn();
707
708     //Find or Create Hypothesis root
709     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
710     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
711     SALOMEDS::SObject_var aRootSO =
712       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
713                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
714     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
715
716     // Add New Hypothesis
717     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
718     aPmName += theHyp->GetName();
719     // prepend plugin name to pixmap name
720     string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
721     if ( pluginName != "StdMeshers" )
722       aPmName = pluginName + "::" + aPmName;
723     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
724   }
725
726   if ( !aHypSO->_is_nil() ) {
727     CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
728     SetName( aHypSO, theName, aHypName );
729   }
730
731   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
732   return aHypSO._retn();
733 }
734
735 //=======================================================================
736 //function : GetMeshOrSubmeshByShape
737 //purpose  : 
738 //=======================================================================
739
740 SALOMEDS::SObject_ptr
741   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
742                                         SMESH::SMESH_Mesh_ptr theMesh,
743                                         GEOM::GEOM_Object_ptr theShape)
744 {
745   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
746   SALOMEDS::SObject_var aMeshOrSubMesh;
747   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
748     return aMeshOrSubMesh._retn();
749   
750   TopoDS_Shape aShape;
751   if(theMesh->HasShapeToMesh())
752     aShape = GeomObjectToShape( theShape );
753   else
754     aShape = SMESH_Mesh::PseudoShape();
755
756   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
757
758   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
759     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
760     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
761       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
762     else {
763       int shapeID = meshDS->ShapeToIndex( aShape );
764       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
765       if ( !aSubMesh->_is_nil() )
766         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
767     }
768   }
769   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
770   return aMeshOrSubMesh._retn();
771 }
772
773 //=======================================================================
774 //function : AddHypothesisToShape
775 //purpose  : 
776 //=======================================================================
777
778 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
779                                        SMESH::SMESH_Mesh_ptr       theMesh,
780                                        GEOM::GEOM_Object_ptr       theShape,
781                                        SMESH::SMESH_Hypothesis_ptr theHyp)
782 {
783   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
784   if (theStudy->_is_nil() || theMesh->_is_nil() ||
785       theHyp->_is_nil() || (theShape->_is_nil()
786                             && theMesh->HasShapeToMesh()) )
787     return false;
788
789   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
790   if ( aMeshSO->_is_nil() )
791     aMeshSO = PublishMesh( theStudy, theMesh );
792   SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
793   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
794     return false;
795
796   // Find a mesh or submesh refering to theShape
797   SALOMEDS::SObject_var aMeshOrSubMesh =
798     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
799   if ( aMeshOrSubMesh->_is_nil() )
800   {
801     // publish submesh
802     TopoDS_Shape aShape = GeomObjectToShape( theShape );
803     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
804     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
805       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
806       int shapeID = meshDS->ShapeToIndex( aShape );
807       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
808       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
809     }
810     if ( aMeshOrSubMesh->_is_nil() )
811       return false;
812   }
813
814   //Find or Create Applied Hypothesis root
815   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
816   SALOMEDS::SObject_var AHR =
817     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
818              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
819              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
820   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
821   if ( AHR->_is_nil() )
822     return false;
823
824   addReference( theStudy, AHR, theHyp );
825   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
826   return true;
827 }
828
829 //=======================================================================
830 //function : RemoveHypothesisFromShape
831 //purpose  : 
832 //=======================================================================
833
834 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
835                                             SMESH::SMESH_Mesh_ptr       theMesh,
836                                             GEOM::GEOM_Object_ptr       theShape,
837                                             SMESH::SMESH_Hypothesis_ptr theHyp)
838 {
839   if (theStudy->_is_nil() || theMesh->_is_nil() ||
840       theHyp->_is_nil() || (theShape->_is_nil()
841                             && theMesh->HasShapeToMesh()))
842     return false;
843
844   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
845   if ( aHypSO->_is_nil() )
846     return false;
847
848   // Find a mesh or submesh refering to theShape
849   SALOMEDS::SObject_var aMeshOrSubMesh =
850     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
851   if ( aMeshOrSubMesh->_is_nil() )
852     return false;
853
854   // Find and remove a reference to aHypSO
855   SALOMEDS::SObject_var aRef, anObj;
856   CORBA::String_var     anID = CORBA::string_dup( aHypSO->GetID() );
857   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
858   for ( it->InitEx( true ); it->More(); it->Next() ) {
859     anObj = it->Value();
860     if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
861       theStudy->NewBuilder()->RemoveObject( anObj );
862       break;
863     }
864   }
865   return true;
866 }
867
868 //=======================================================================
869 //function : UpdateSObject
870 //purpose  : 
871 //=======================================================================
872 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
873 {
874
875   if(VARIABLE_DEBUG)
876     cout<<"UpdateParameters : "<<theParameters<<endl;
877   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
878   if(aStudy->_is_nil() || CORBA::is_nil(theObject)) 
879     return;
880
881   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
882   if(aSObj->_is_nil())  
883     return;
884   
885   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
886   
887   SALOMEDS::GenericAttribute_var aFindAttr;
888   bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
889   if(VARIABLE_DEBUG)
890     cout<<"Find Attribute "<<hasAttr<<endl;
891
892   SALOMEDS::GenericAttribute_var anAttr;
893   anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
894   SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
895
896   TCollection_AsciiString aNewParams;
897   TCollection_AsciiString aOldParameters(aStringAttr->Value());
898   TCollection_AsciiString anInputParams(ParseParameters(theParameters));
899   
900   if(!hasAttr)
901     aNewParams = anInputParams;
902   else 
903     aNewParams = aOldParameters+"|"+anInputParams;
904
905   if(VARIABLE_DEBUG)
906     {
907       cout<<"Input Parameters : "<<anInputParams<<endl;
908       cout<<"Old Parameters : "<<aOldParameters<<endl;
909       cout<<"New Parameters : "<<aNewParams<<endl;
910     }
911   
912   
913   aStringAttr->SetValue( aNewParams.ToCString() );
914 }
915
916 //=======================================================================
917 //function : GetParameters
918 //purpose  : 
919 //=======================================================================
920 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
921 {
922   const char* aParameters = CORBA::string_dup(theParameters);
923   TCollection_AsciiString anInputParams;
924   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
925   if( !aStudy->_is_nil() ) {
926     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
927     if(aSections->length() > 0) {
928       SALOMEDS::ListOfStrings aVars= aSections[0];
929       for(int i=0;i<aVars.length();i++ ) {
930         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
931           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
932         if(i != aVars.length()-1)
933           anInputParams+=":";
934       }
935     }
936   }
937   return CORBA::string_dup(anInputParams.ToCString());
938 }
939
940 //=======================================================================
941 //function : GetParameters
942 //purpose  : 
943 //=======================================================================
944 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
945 {
946   TCollection_AsciiString aResult;
947
948   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
949   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
950
951   if(!aStudy->_is_nil() && 
952      !CORBA::is_nil(theObject) && 
953      !aSObj->_is_nil()){
954     
955     SALOMEDS::GenericAttribute_var anAttr;
956     if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
957       aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
958     }
959   }
960   
961   return CORBA::string_dup( aResult.ToCString() );
962 }