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