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