Salome HOME
Do not unregister in Publish: random problems when meshing in parallel (yacs foreach...
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i_1.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 "SMESHDS_Mesh.hxx"
30 #include "SMESH_Algo_i.hxx"
31 #include "SMESH_Comment.hxx"
32 #include "SMESH_Group_i.hxx"
33 #include "SMESH_Hypothesis_i.hxx"
34 #include "SMESH_Mesh_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 #include <SALOMEDS_wrap.hxx>
42 #include <SALOMEDS_Attributes_wrap.hxx>
43
44 #include <TCollection_AsciiString.hxx>
45 #include <TopoDS_Solid.hxx>
46
47 #include <cctype>
48
49 #ifdef _DEBUG_
50 static int MYDEBUG = 0;
51 #else
52 static int MYDEBUG = 0;
53 #endif
54
55 using namespace std;
56
57 //=============================================================================
58 /*!
59  *  Get...Tag [ static ]
60  *
61  *  Methods which determine SMESH data model structure
62  */
63 //=============================================================================
64
65 long SMESH_Gen_i::GetHypothesisRootTag()
66 {
67   return SMESH::Tag_HypothesisRoot;
68 }
69
70 long SMESH_Gen_i::GetAlgorithmsRootTag()
71 {
72   return SMESH::Tag_AlgorithmsRoot;
73 }
74
75 long SMESH_Gen_i::GetRefOnShapeTag()
76 {
77   return SMESH::Tag_RefOnShape;
78 }
79
80 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
81 {
82   return SMESH::Tag_RefOnAppliedHypothesis;
83 }
84
85 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
86 {
87   return SMESH::Tag_RefOnAppliedAlgorithms;
88 }
89
90 long SMESH_Gen_i::GetSubMeshOnVertexTag()
91 {
92   return SMESH::Tag_SubMeshOnVertex;
93 }
94
95 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
96 {
97   return SMESH::Tag_SubMeshOnEdge;
98 }
99
100 long SMESH_Gen_i::GetSubMeshOnFaceTag()
101 {
102   return SMESH::Tag_SubMeshOnFace;
103 }
104
105 long SMESH_Gen_i::GetSubMeshOnSolidTag()
106 {
107   return SMESH::Tag_SubMeshOnSolid;
108 }
109
110 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
111 {
112   return SMESH::Tag_SubMeshOnCompound;
113 }
114
115 long SMESH_Gen_i::GetSubMeshOnWireTag()
116 {
117   return SMESH::Tag_SubMeshOnWire;
118 }
119
120 long SMESH_Gen_i::GetSubMeshOnShellTag()
121 {
122   return SMESH::Tag_SubMeshOnShell;
123 }
124
125 long SMESH_Gen_i::GetNodeGroupsTag()
126 {
127   return SMESH::Tag_NodeGroups;
128 }
129
130 long SMESH_Gen_i::GetEdgeGroupsTag()
131 {
132   return SMESH::Tag_EdgeGroups;
133 }
134
135 long SMESH_Gen_i::GetFaceGroupsTag()
136 {
137   return SMESH::Tag_FaceGroups;
138 }
139
140 long SMESH_Gen_i::GetVolumeGroupsTag()
141 {
142   return SMESH::Tag_VolumeGroups;
143 }
144
145 long SMESH_Gen_i::Get0DElementsGroupsTag()
146 {
147   return SMESH::Tag_0DElementsGroups;
148 }
149
150 long SMESH_Gen_i::GetBallElementsGroupsTag()
151 {
152   return SMESH::Tag_BallElementsGroups;
153 }
154
155 //=============================================================================
156 /*!
157  *  SMESH_Gen_i::CanPublishInStudy
158  *
159  *  Returns true if object can be published in the study
160  */
161 //=============================================================================
162
163 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
164 {
165   if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
166   if( GetCurrentStudyID() < 0 )
167     return false;
168   
169   SMESH::SMESH_Mesh_var aMesh       = SMESH::SMESH_Mesh::_narrow(theIOR);
170   if( !aMesh->_is_nil() )
171     return true;
172
173   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
174   if( !aSubMesh->_is_nil() )
175     return true;
176
177   SMESH::SMESH_Hypothesis_var aHyp  = SMESH::SMESH_Hypothesis::_narrow(theIOR);
178   if( !aHyp->_is_nil() )
179     return true;
180
181   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
182   if( !aGroup->_is_nil() )
183     return true;
184
185   if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
186   return false;
187 }
188
189 //=======================================================================
190 //function : ObjectToSObject
191 //purpose  : Put a result into a SALOMEDS::SObject_wrap or call UnRegister()!
192 //=======================================================================
193
194 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
195                                                    CORBA::Object_ptr   theObject)
196 {
197   SALOMEDS::SObject_wrap aSO;
198   if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
199   {
200     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
201     aSO = theStudy->FindObjectIOR( objStr.in() );
202   }
203   return aSO._retn();
204 }
205
206 //=======================================================================
207 //function : objectToServant
208 //purpose  : 
209 //=======================================================================
210
211 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
212 {
213   return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
214 }
215
216 //=======================================================================
217 //function : ShapeToGeomObject
218 //purpose  : 
219 //=======================================================================
220
221 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
222 {
223   GEOM::GEOM_Object_var aShapeObj;
224   if ( !theShape.IsNull() ) {
225     GEOM_Client* aClient = GetShapeReader();
226     TCollection_AsciiString IOR;
227     if ( aClient && aClient->Find( theShape, IOR ))
228     {
229       CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
230       aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
231     }
232   }
233   return aShapeObj._retn();
234 }
235
236 //=======================================================================
237 //function : GeomObjectToShape
238 //purpose  : 
239 //=======================================================================
240
241 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
242 {
243   TopoDS_Shape S;
244   if ( !theGeomObject->_is_nil() ) {
245     GEOM_Client* aClient = GetShapeReader();
246     GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
247     if ( aClient && !aGeomEngine->_is_nil () )
248       S = aClient->GetShape( aGeomEngine, theGeomObject );
249   }
250   return S;
251 }
252
253 //=======================================================================
254 //function : publish
255 //purpose  : 
256 //=======================================================================
257
258 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr   theStudy,
259                                      CORBA::Object_ptr     theIOR,
260                                      SALOMEDS::SObject_ptr theFatherObject,
261                                      const int             theTag = 0,
262                                      const char*           thePixMap = 0,
263                                      const bool            theSelectable = true)
264 {
265   SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
266   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder();
267   SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
268   bool isNewSO = false, isInUseCaseTree = false;
269   if ( SO->_is_nil() )
270   {
271     if ( theTag == 0 ) {
272       SO = aStudyBuilder->NewObject( theFatherObject );
273       isNewSO = true;
274     }
275     else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
276     {
277       SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
278       isNewSO = true;
279     }
280     else
281     {
282       isInUseCaseTree = useCaseBuilder->IsUseCaseNode( SO );
283     }
284   }
285   else
286   {
287     isInUseCaseTree = useCaseBuilder->IsUseCaseNode( SO );
288   }
289
290   SALOMEDS::GenericAttribute_wrap anAttr;
291   if ( !CORBA::is_nil( theIOR ))
292   {
293     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
294     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
295     SALOMEDS::AttributeIOR_wrap iorAttr = anAttr;
296     CORBA::String_var objStrCur = iorAttr->Value();
297     bool sameIOR = ( objStrCur.in() && strcmp( objStr.in(), objStrCur.in() ) == 0 );
298     if ( !sameIOR )
299     {
300       iorAttr->SetValue( objStr.in() );
301       // UnRegister() !!! --> No: random problems when meshing in parallel (yacs foreach) in distributed python scripts
302 //      SALOME::GenericObj_var genObj = SALOME::GenericObj::_narrow( theIOR );
303 //      if ( !genObj->_is_nil() )
304 //        genObj->UnRegister();
305     }
306   }
307
308   if ( thePixMap ) {
309     anAttr  = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
310     SALOMEDS::AttributePixMap_wrap pm = anAttr;
311     pm->SetPixMap( thePixMap );
312   }
313
314   if ( !theSelectable ) {
315     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
316     SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
317     selAttr->SetSelectable( false );
318   }
319
320   if ( !isNewSO )
321     aStudyBuilder->RemoveReference( SO );// remove garbage reference (23336)
322
323   // add object to the use case tree
324   // (to support tree representation customization and drag-n-drop)
325   if ( isNewSO || !isInUseCaseTree )
326   {
327     // define the next tag after given one in the data tree to insert SObject
328     SALOMEDS::SObject_wrap curObj, objAfter;
329     if ( theFatherObject->GetLastChildTag() > theTag && theTag > 0 )
330     {
331       SALOMEDS::UseCaseIterator_wrap
332         anUseCaseIter = useCaseBuilder->GetUseCaseIterator(theFatherObject);
333       for ( ; anUseCaseIter->More(); anUseCaseIter->Next() ) {
334         curObj = anUseCaseIter->Value();
335         if ( curObj->Tag() > theTag  ) {
336           objAfter = curObj;
337           break;
338         }
339       }
340     }
341     if ( !CORBA::is_nil( objAfter ))
342       useCaseBuilder->InsertBefore( SO, objAfter );    // insert at given tag
343     else if ( !isInUseCaseTree )
344       useCaseBuilder->AppendTo( theFatherObject, SO ); // append to the end of list
345   }
346   return SO._retn();
347 }
348
349 //=======================================================================
350 //function : setName
351 //purpose  :
352 //=======================================================================
353
354 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
355                           const char*           theName,
356                           const char*           theDefaultName)
357 {
358   if ( !theSObject->_is_nil() ) {
359     SALOMEDS::Study_var               aStudy = theSObject->GetStudy();
360     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
361     SALOMEDS::GenericAttribute_wrap   anAttr =
362       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
363     SALOMEDS::AttributeName_wrap aNameAttr = anAttr;
364     if ( theName && theName[0] ) {
365       std::string name( theName ); // trim trailing white spaces
366       for ( size_t i = name.size()-1; i > 0; --i )
367         if ( isspace( name[i] )) name[i] = '\0';
368         else                     break;
369       aNameAttr->SetValue( name.c_str() );
370     }
371     else
372     {
373       CORBA::String_var curName = aNameAttr->Value();
374       if ( strlen( curName.in() ) == 0 ) {
375         SMESH_Comment aName(theDefaultName);
376         aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
377       }
378     }
379   }
380 }
381
382 //=======================================================================
383 //function : SetPixMap
384 //purpose  :
385 //=======================================================================
386
387 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
388                             const char*           thePixMap)
389 {
390   if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
391   {
392     SALOMEDS::Study_var               aStudy = theSObject->GetStudy();
393     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
394     SALOMEDS::GenericAttribute_wrap anAttr =
395       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
396     SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
397     aPMAttr->SetPixMap( thePixMap );
398   }
399 }
400
401 //=======================================================================
402 //function : addReference
403 //purpose  : 
404 //=======================================================================
405
406 static void addReference (SALOMEDS::Study_ptr   theStudy,
407                           SALOMEDS::SObject_ptr theSObject,
408                           CORBA::Object_ptr     theToObject,
409                           int                   theTag = 0)
410 {
411   SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
412   if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
413     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
414     SALOMEDS::SObject_wrap aReferenceSO;
415     if ( !theTag ) {
416       // check if the reference to theToObject already exists
417       // and find a free label for the reference object
418       bool isReferred = false;
419       int tag = 1;
420       SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
421       for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
422         SALOMEDS::SObject_wrap curSO = anIter->Value();
423         if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
424           CORBA::String_var refEntry = aReferenceSO->GetID();
425           CORBA::String_var  toEntry = aToObjSO->GetID();
426           if ( strcmp( refEntry, toEntry ) == 0 )
427             isReferred = true;
428         }
429         else if ( !theTag ) {
430           SALOMEDS::GenericAttribute_wrap anAttr;
431           if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
432             theTag = tag;
433         }
434       }
435       if ( isReferred )
436         return;
437       if ( !theTag )
438         theTag = tag;
439     }
440     if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
441       aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
442
443     aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
444
445     // add reference to the use case tree
446     // (to support tree representation customization and drag-n-drop)
447     SALOMEDS::UseCaseBuilder_wrap  useCaseBuilder = theStudy->GetUseCaseBuilder();
448     SALOMEDS::UseCaseIterator_wrap    useCaseIter = useCaseBuilder->GetUseCaseIterator(theSObject);
449     for ( ; useCaseIter->More(); useCaseIter->Next() )
450     {
451       SALOMEDS::SObject_wrap curSO = useCaseIter->Value();
452       if ( curSO->Tag() == theTag )
453         return;
454     }
455     useCaseBuilder->AppendTo( theSObject, aReferenceSO );
456   }
457 }
458
459 //=============================================================================
460 /*!
461  *  SMESH_Gen_i::PublishInStudy
462  *
463  *  Publish object in the study
464  */
465 //=============================================================================
466
467 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
468                                                   SALOMEDS::SObject_ptr theSObject,
469                                                   CORBA::Object_ptr     theIOR,
470                                                   const char*           theName)
471      throw (SALOME::SALOME_Exception)
472 {
473   Unexpect aCatch(SALOME_SalomeException);
474   SALOMEDS::SObject_wrap aSO;
475   if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
476     return aSO._retn();
477   if(MYDEBUG) MESSAGE("PublishInStudy");
478
479   // Publishing a mesh
480   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
481   if( !aMesh->_is_nil() )
482     aSO = PublishMesh( theStudy, aMesh, theName );
483
484   // Publishing a sub-mesh
485   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
486   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
487     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
488     aMesh = aSubMesh->GetFather();
489     aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
490   }
491
492   // Publishing a hypothesis or algorithm
493   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
494   if ( aSO->_is_nil() && !aHyp->_is_nil() )
495     aSO = PublishHypothesis( theStudy, aHyp );
496
497   // Publishing a group
498   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
499   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
500     GEOM::GEOM_Object_var aShapeObject;
501     aMesh = aGroup->GetMesh();
502     aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
503   }
504   if(MYDEBUG) MESSAGE("PublishInStudy_END");
505
506   return aSO._retn();
507 }
508
509 //=======================================================================
510 //function : PublishComponent
511 //purpose  : 
512 //=======================================================================
513
514 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
515 {
516   if ( CORBA::is_nil( theStudy ))
517     return SALOMEDS::SComponent::_nil();
518   if(MYDEBUG) MESSAGE("PublishComponent");
519
520   SALOMEDS::StudyBuilder_var    aStudyBuilder  = theStudy->NewBuilder(); 
521   SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
522
523   CORBA::String_var   compDataType = ComponentDataType();
524   SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
525   if ( !CORBA::is_nil( father ) ) {
526     // check that the component is added to the use case browser
527     if ( !useCaseBuilder->IsUseCaseNode( father ) ) {
528       useCaseBuilder->SetRootCurrent();
529       useCaseBuilder->Append( father ); // component object is added as the top level item
530     }
531     return father._retn();
532   }
533
534   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
535     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
536   if ( CORBA::is_nil( aCat ) )
537     return father._retn();
538
539   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
540   if ( CORBA::is_nil( aComp ) )
541     return father._retn();
542
543   SALOMEDS::GenericAttribute_wrap anAttr;
544   SALOMEDS::AttributePixMap_wrap  aPixmap;
545
546   father  = aStudyBuilder->NewComponent( compDataType.in() );
547   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
548   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
549   aPixmap = anAttr;
550   aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
551   CORBA::String_var userName = aComp->componentusername();
552   SetName( father, userName.in(), "MESH" );
553   // add component to the use case tree
554   // (to support tree representation customization and drag-n-drop)
555   useCaseBuilder->SetRootCurrent();
556   useCaseBuilder->Append( father ); // component object is added as the top level item
557   if(MYDEBUG) MESSAGE("PublishComponent--END");
558
559   return father._retn();
560 }
561
562 //=======================================================================
563 //function : PublishMesh
564 //purpose  : 
565 //=======================================================================
566
567 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr   theStudy,
568                                                 SMESH::SMESH_Mesh_ptr theMesh,
569                                                 const char*           theName)
570 {
571   if ( CORBA::is_nil( theStudy ) ||
572        CORBA::is_nil( theMesh ))
573     return SALOMEDS::SComponent::_nil();
574   if(MYDEBUG) MESSAGE("PublishMesh--IN");
575
576   // find or publish a mesh
577
578   SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
579   if ( aMeshSO->_is_nil() )
580   {
581     SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
582     if ( father->_is_nil() )
583       return aMeshSO._retn();
584
585     // Find correct free tag
586     long aTag = father->GetLastChildTag();
587     if ( aTag <= GetAlgorithmsRootTag() )
588       aTag = GetAlgorithmsRootTag() + 1;
589     else
590       aTag++;
591
592     aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
593     if ( aMeshSO->_is_nil() )
594       return aMeshSO._retn();
595   }
596   SetName( aMeshSO, theName, "Mesh" );
597
598   // Add shape reference
599
600   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
601   if ( !CORBA::is_nil( aShapeObject )) {
602     addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
603
604     // Publish global hypotheses
605
606     SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
607     for ( CORBA::ULong i = 0; i < hypList->length(); i++ )
608     {
609       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
610       SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
611       AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
612     }
613   }
614
615   // Publish submeshes
616
617   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
618   if ( !mesh_i )
619     return aMeshSO._retn();
620   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
621   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
622   for ( ; subIt != subMap.end(); subIt++ ) {
623     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
624     if ( !CORBA::is_nil( aSubMesh )) {
625       aShapeObject = aSubMesh->GetSubShape();
626       SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
627     }
628   }
629
630   // Publish groups
631   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
632   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
633   for ( ; it != grMap.end(); it++ )
634   {
635     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
636     if ( !aGroup->_is_nil() ) {
637       GEOM::GEOM_Object_var aShapeObj;
638       SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
639       if ( !aGeomGroup->_is_nil() )
640         aShapeObj = aGeomGroup->GetShape();
641       SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
642     }
643   }
644
645   if(MYDEBUG) MESSAGE("PublishMesh_END");
646   return aMeshSO._retn();
647 }
648
649 //=======================================================================
650 //function : PublishSubMesh
651 //purpose  : 
652 //=======================================================================
653
654 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
655                                                    SMESH::SMESH_Mesh_ptr    theMesh,
656                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
657                                                    GEOM::GEOM_Object_ptr    theShapeObject,
658                                                    const char*              theName)
659 {
660   if (theStudy->_is_nil() || theMesh->_is_nil() ||
661       theSubMesh->_is_nil() || theShapeObject->_is_nil() )
662     return SALOMEDS::SObject::_nil();
663
664   SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
665   if ( aSubMeshSO->_is_nil() )
666   {
667     SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
668     if ( aMeshSO->_is_nil() ) {
669       aMeshSO = PublishMesh( theStudy, theMesh );
670       if ( aMeshSO->_is_nil())
671         return SALOMEDS::SObject::_nil();
672     }
673     // Find submesh sub-tree tag
674     long aRootTag;
675     const char* aRootName = "";
676     switch ( theShapeObject->GetShapeType() ) {
677     case GEOM::VERTEX:
678       aRootTag  = GetSubMeshOnVertexTag();
679       aRootName = "SubMeshes on Vertex";
680       break;
681     case GEOM::EDGE:
682       aRootTag  = GetSubMeshOnEdgeTag();
683       aRootName = "SubMeshes on Edge";
684       break;
685     case GEOM::WIRE:
686       aRootTag  = GetSubMeshOnWireTag();
687       aRootName = "SubMeshes on Wire";
688       break;
689     case GEOM::FACE:
690       aRootTag  = GetSubMeshOnFaceTag();
691       aRootName = "SubMeshes on Face";    
692       break;
693     case GEOM::SHELL:
694       aRootTag  = GetSubMeshOnShellTag();
695       aRootName = "SubMeshes on Shell";   
696       break;
697     case GEOM::SOLID:
698       aRootTag  = GetSubMeshOnSolidTag();
699       aRootName = "SubMeshes on Solid";
700       break;
701     default:
702       aRootTag  = GetSubMeshOnCompoundTag();
703       aRootName = "SubMeshes on Compound";
704       break;
705     }
706
707     // Find or create submesh root
708     SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
709                                              aMeshSO, aRootTag, 0, false );
710     if ( aRootSO->_is_nil() )
711       return aSubMeshSO._retn();
712
713     SetName( aRootSO, aRootName );
714
715     // Add new submesh to corresponding sub-tree
716     SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
717     const int isEmpty = ( elemTypes->length() == 0 );
718     const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
719     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
720     if ( aSubMeshSO->_is_nil() )
721       return aSubMeshSO._retn();
722   }
723   SetName( aSubMeshSO, theName, "SubMesh" );
724
725   // Add reference to theShapeObject
726
727   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
728
729   // Publish hypothesis
730
731   SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
732   for ( CORBA::ULong i = 0; i < hypList->length(); i++ ) {
733     SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
734     SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
735     AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
736   }
737
738   return aSubMeshSO._retn();
739 }
740
741 //=======================================================================
742 //function : PublishGroup
743 //purpose  : 
744 //=======================================================================
745
746 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
747                                                  SMESH::SMESH_Mesh_ptr  theMesh,
748                                                  SMESH::SMESH_GroupBase_ptr theGroup,
749                                                  GEOM::GEOM_Object_ptr  theShapeObject,
750                                                  const char*            theName)
751 {
752   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
753     return SALOMEDS::SObject::_nil();
754
755   SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
756   if ( aGroupSO->_is_nil() )
757   {
758     SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
759     if ( aMeshSO->_is_nil() ) {
760       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
761       if ( aMeshSO->_is_nil())
762         return SALOMEDS::SObject::_nil();
763     }
764     size_t aType = (int)theGroup->GetType();
765     const char* aRootNames[] = {
766       "Compound Groups", "Groups of Nodes", "Groups of Edges",
767       "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
768       "Groups of Balls" };
769
770     // Currently, groups with heterogeneous content are not supported
771     if ( aType != SMESH::ALL )
772     {
773       long aRootTag = GetNodeGroupsTag() + aType - 1;
774
775       // Find or create groups root
776       SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
777                                                aMeshSO, aRootTag, 0, false );
778       if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
779
780       if ( aType < sizeof(aRootNames)/sizeof(char*) )
781         SetName( aRootSO, aRootNames[aType] );
782
783       // Add new group to corresponding sub-tree
784       int isEmpty = false;
785       std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
786       if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
787       {
788         pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
789       }
790       else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
791       {
792         if ( theGroup->GetType() == SMESH::NODE )
793           isEmpty = ( theMesh->NbNodes() == 0 );
794         else
795         {
796           SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
797           for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
798             isEmpty = ( allElemTypes[i] != theGroup->GetType() );
799         }
800       }
801       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
802     }
803     if ( aGroupSO->_is_nil() )
804       return aGroupSO._retn();
805   }
806
807   SetName( aGroupSO, theName, "Group" );
808
809   //Add reference to geometry
810   if ( !theShapeObject->_is_nil() )
811     addReference( theStudy, aGroupSO, theShapeObject, 1 );
812
813   return aGroupSO._retn();
814 }
815
816 //=======================================================================
817 //function : PublishHypothesis
818 //purpose  : 
819 //=======================================================================
820
821 SALOMEDS::SObject_ptr
822   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
823                                   SMESH::SMESH_Hypothesis_ptr theHyp,
824                                   const char*                 theName)
825 {
826   if(MYDEBUG) MESSAGE("PublishHypothesis")
827   if (theStudy->_is_nil() || theHyp->_is_nil())
828     return SALOMEDS::SObject::_nil();
829
830   CORBA::String_var hypType = theHyp->GetName();
831
832   SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
833   if ( aHypSO->_is_nil() )
834   {
835     SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
836     if ( father->_is_nil() )
837       return aHypSO._retn();
838
839     //Find or Create Hypothesis root
840     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
841     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
842     SALOMEDS::SObject_wrap aRootSO =
843       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
844                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
845     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
846
847     // Add New Hypothesis
848     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
849     aPmName += hypType.in();
850     // prepend plugin name to pixmap name
851     string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
852     if ( pluginName != "StdMeshers" )
853       aPmName = pluginName + "::" + aPmName;
854     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
855   }
856
857   SetName( aHypSO, theName, hypType.in() );
858
859   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
860   return aHypSO._retn();
861 }
862
863 //=======================================================================
864 //function : GetMeshOrSubmeshByShape
865 //purpose  : 
866 //=======================================================================
867
868 SALOMEDS::SObject_ptr
869   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
870                                         SMESH::SMESH_Mesh_ptr theMesh,
871                                         GEOM::GEOM_Object_ptr theShape)
872 {
873   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
874   SALOMEDS::SObject_wrap aMeshOrSubMesh;
875   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
876     return aMeshOrSubMesh._retn();
877   
878   TopoDS_Shape aShape;
879   if(theMesh->HasShapeToMesh())
880     aShape = GeomObjectToShape( theShape );
881   else
882     aShape = SMESH_Mesh::PseudoShape();
883
884   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
885
886   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
887     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
888     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
889       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
890     else {
891       int shapeID = meshDS->ShapeToIndex( aShape );
892       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
893       if ( !aSubMesh->_is_nil() )
894         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
895     }
896   }
897   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
898   return aMeshOrSubMesh._retn();
899 }
900
901 //=======================================================================
902 //function : AddHypothesisToShape
903 //purpose  : 
904 //=======================================================================
905
906 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
907                                        SMESH::SMESH_Mesh_ptr       theMesh,
908                                        GEOM::GEOM_Object_ptr       theShape,
909                                        SMESH::SMESH_Hypothesis_ptr theHyp)
910 {
911   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
912   if (theStudy->_is_nil() || theMesh->_is_nil() ||
913       theHyp->_is_nil() || (theShape->_is_nil()
914                             && theMesh->HasShapeToMesh()) )
915     return false;
916
917   SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
918   if ( aMeshSO->_is_nil() )
919     aMeshSO = PublishMesh( theStudy, theMesh );
920   SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
921   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
922     return false;
923
924   // Find a mesh or submesh referring to theShape
925   SALOMEDS::SObject_wrap aMeshOrSubMesh =
926     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
927   if ( aMeshOrSubMesh->_is_nil() )
928   {
929     // publish submesh
930     TopoDS_Shape aShape = GeomObjectToShape( theShape );
931     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
932     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
933       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
934       int shapeID = meshDS->ShapeToIndex( aShape );
935       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
936       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
937     }
938     if ( aMeshOrSubMesh->_is_nil() )
939       return false;
940   }
941
942   //Find or Create Applied Hypothesis root
943   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
944   SALOMEDS::SObject_wrap AHR =
945     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
946              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
947              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
948   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
949
950   addReference( theStudy, AHR, theHyp );
951
952   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
953   return true;
954 }
955
956 //=======================================================================
957 //function : RemoveHypothesisFromShape
958 //purpose  : 
959 //=======================================================================
960
961 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
962                                             SMESH::SMESH_Mesh_ptr       theMesh,
963                                             GEOM::GEOM_Object_ptr       theShape,
964                                             SMESH::SMESH_Hypothesis_ptr theHyp)
965 {
966   if (theStudy->_is_nil() || theMesh->_is_nil() ||
967       theHyp->_is_nil() || (theShape->_is_nil()
968                             && theMesh->HasShapeToMesh()))
969     return false;
970
971   SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
972   if ( aHypSO->_is_nil() )
973     return false;
974
975   CORBA::String_var hypEntry = aHypSO->GetID();
976
977   // Find a mesh or sub-mesh referring to theShape
978   SALOMEDS::SObject_wrap aMeshOrSubMesh =
979     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
980   if ( aMeshOrSubMesh->_is_nil() )
981     return false;
982
983   // Find and remove a reference to aHypSO
984   SALOMEDS::SObject_wrap aRef, anObj;
985   SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
986   bool found = false;
987   for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
988     anObj = it->Value();
989     if (anObj->ReferencedObject( aRef.inout() ))
990     {
991       CORBA::String_var refEntry = aRef->GetID();
992       found = ( strcmp( refEntry, hypEntry ) == 0 );
993     }
994     if ( found )
995     {
996       SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
997       builder->RemoveObject( anObj );
998     }
999   }
1000
1001   return true;
1002 }
1003
1004 //================================================================================
1005 /*!
1006  * \brief Stores names of variables that WILL be passes as parameters when calling
1007  *        some method of a given object.
1008  *  \param [in] theObject - the object whose a method WILL be called with \a theParameters.
1009  *  \param [in] theParameters - a string containing parameters separated by ':'.
1010  */
1011 //================================================================================
1012
1013 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
1014 {
1015   SALOMEDS::Study_var aStudy = GetCurrentStudy();
1016   if ( aStudy->_is_nil() )
1017     return;
1018
1019   // find variable names within theParameters
1020
1021   myLastObj.clear();
1022   myLastParameters.clear();
1023   myLastParamIndex.clear(); /* vector holding indices of virables within the string
1024                                of all variables used for theObject */ 
1025   int nbVars = 0;
1026   int pos = 0, prevPos = 0, len = strlen( theParameters );
1027   if ( len == 0 ) return;
1028   while ( pos <= len )
1029   {
1030     if ( pos == len || theParameters[pos] == ':' )
1031     {
1032       if ( prevPos < pos )
1033       {
1034         string val( theParameters + prevPos, theParameters + pos );
1035         if ( !aStudy->IsVariable( val.c_str() ))
1036           val.clear();
1037         myLastParameters.push_back( val );
1038         nbVars += (! myLastParameters.back().empty() );
1039       }
1040       else
1041       {
1042         myLastParameters.push_back("");
1043       }
1044       prevPos = pos+1;
1045     }
1046     ++pos;
1047   }
1048
1049   if ( nbVars < 1 )
1050     return;
1051
1052   // store
1053   // (1) variable names in the string of all variables used for theObject and
1054   // (2) indices of found variables in myLastParamIndex.
1055
1056   // remember theObject
1057   SALOMEDS::SObject_wrap aSObj =  ObjectToSObject(aStudy,theObject);
1058   if ( aSObj->_is_nil() )
1059     return;
1060   CORBA::String_var anObjEntry = aSObj->GetID();
1061   myLastObj = anObjEntry.in();
1062
1063   // get a string of variable names
1064   SALOMEDS::StudyBuilder_var   aStudyBuilder = aStudy->NewBuilder();
1065   SALOMEDS::GenericAttribute_wrap     anAttr =
1066     aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1067   SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1068   CORBA::String_var                  oldVars = aStringAttr->Value();
1069   std::string                         varStr = oldVars.in();
1070
1071   // add new variables and find indices of variables
1072   for ( size_t i = 0; i < myLastParameters.size(); ++i )
1073   {
1074     int varIndex = -1;
1075     if ( !myLastParameters[i].empty() )
1076     {
1077       // find index of myLastParameters[i] in varStr
1078       int curIndex  = 0;
1079       bool varFound = false;
1080       size_t pos    = 0;
1081       // varStr can be "A|B::C;*=2|D"
1082       const std::string separators(":|;*=");
1083       while ( pos < varStr.size() && !varFound )
1084       {
1085         // skip separators
1086         while ( separators.find( varStr[ pos ]) != std::string::npos )
1087           if ( ++pos >= varStr.size() )
1088             break;
1089         // skip repetition number following '='
1090         if ( varStr[ pos-1 ] == '=' )
1091         {
1092           while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1093             ++pos;
1094           continue; // to skip next separator
1095         }
1096         // compare variable name
1097         if ( pos < varStr.size() )
1098         {
1099           varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1100                        // same string beginning but is length same?
1101                        ( pos + myLastParameters[i].size() >= varStr.size() ||
1102                          separators.find( varStr[ pos+1 ]) != std::string::npos ));
1103           if ( varFound )
1104             varIndex = curIndex;
1105           else
1106             pos = varStr.find_first_of( separators, pos ); // goto the next separator
1107           ++curIndex;
1108         }
1109       }
1110       // add new variable
1111       if ( !varFound )
1112       {
1113         varStr += ":" + myLastParameters[i];
1114         varIndex = curIndex;
1115       }
1116     }
1117     myLastParamIndex.push_back( varIndex );
1118   }
1119   aStringAttr->SetValue( varStr.c_str() );
1120 }
1121
1122 //================================================================================
1123 /*!
1124  * \brief Return all variables used to create an object
1125  *  \param [in] theObjectEntry - an object entry in the current study
1126  *  \return std::vector< std::string > - all variable names (or values of removed variables)
1127  */
1128 //================================================================================
1129
1130 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1131 {
1132   std::vector< std::string > varNames;
1133   if ( myCurrentStudy->_is_nil() )
1134     return varNames;
1135
1136   SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1137   if ( myCurrentStudy->_is_nil() )
1138     return varNames;
1139
1140   // get a string of variable names
1141   SALOMEDS::StudyBuilder_var   aStudyBuilder = myCurrentStudy->NewBuilder();
1142   SALOMEDS::GenericAttribute_wrap     anAttr =
1143     aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1144   SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1145   CORBA::String_var                  oldVars = aStringAttr->Value();
1146   std::string                         varStr = oldVars.in();
1147
1148   // separate variables within varStr;
1149   // varStr can be "A|B::C;*=2|D"
1150   size_t pos = 0;
1151   const std::string separators(":|;*=");
1152   while ( pos < varStr.size() )
1153   {
1154     // skip separators
1155     pos = varStr.find_first_not_of( separators, pos );
1156     // while ( separators.find( varStr[ pos ]) != std::string::npos )
1157     //   if ( ++pos >= varStr.size() )
1158     //     break;
1159     // skip repetition number following '='
1160     if ( varStr[ pos-1 ] == '=' )
1161     {
1162       while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1163         ++pos;
1164       continue; // to skip next separator
1165     }
1166     // store variable name
1167     if ( pos < varStr.size() )
1168     {
1169       size_t pos2 = varStr.find_first_of( separators, pos );
1170       varNames.push_back( varStr.substr( pos, pos2 - pos));
1171       pos = pos2;
1172     }
1173   }
1174   return varNames;
1175 }
1176
1177 //=======================================================================
1178 //function : ParseParameters
1179 //purpose  : Replace variables by their values
1180 //=======================================================================
1181 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1182 // {
1183 //   //const char* aParameters = theParameters;
1184 // //   const char* aParameters = CORBA::string_dup(theParameters);
1185 //   TCollection_AsciiString anInputParams;
1186 //   SALOMEDS::Study_var aStudy = GetCurrentStudy();
1187 //   if( !aStudy->_is_nil() ) {
1188 // //     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1189 // //     for(int j=0;j<aSections->length();j++) {
1190 // //       SALOMEDS::ListOfStrings aVars= aSections[j];
1191 // //       for(int i=0;i<aVars.length();i++ ) {
1192 // //         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
1193 // //           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1194 // //         if(i != aVars.length()-1)
1195 // //           anInputParams+=":";
1196 // //       }
1197 // //       if(j!=aSections->length()-1)
1198 // //         anInputParams+="|";
1199 // //     }
1200 //     TCollection_AsciiString paramStr( theParameters );
1201 //     int beg = 0, end;
1202 //     char sep, *pParams = (char*)paramStr.ToCString();
1203 //     while ( beg < paramStr.Length() )
1204 //     {
1205 //       end = beg-1;
1206 //       while ( ++end < paramStr.Length() )
1207 //         if ( pParams[end] == ':' || pParams[end] == '|')
1208 //           break;
1209 //       if ( end < paramStr.Length())
1210 //       {
1211 //         sep = pParams[end];
1212 //         pParams[end] = '\0';
1213 //       }
1214 //       if ( aStudy->IsVariable( pParams+beg ))
1215 //         anInputParams += pParams+beg;
1216 //       if ( end < paramStr.Length() )
1217 //         anInputParams += sep;
1218 //       else
1219 //         break;
1220 //       beg = end + 1;
1221 //     }
1222 //   }
1223 //   return CORBA::string_dup(anInputParams.ToCString());
1224 // }
1225
1226 //=======================================================================
1227 //function : GetParameters
1228 //purpose  : 
1229 //=======================================================================
1230
1231 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1232 {
1233   CORBA::String_var aResult("");
1234
1235   SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1236   if ( !aSObj->_is_nil() )
1237   {
1238     SALOMEDS::GenericAttribute_wrap attr;
1239     if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1240     {
1241       SALOMEDS::AttributeString_wrap strAttr = attr;
1242       aResult = strAttr->Value();
1243     }
1244   }
1245   return aResult._retn();
1246 }