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