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