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