Salome HOME
a73f93c3c0417fcd49ad3dd738dc02fb01b7f0aa
[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 ( !myIsEnablePublish )
483     return aSO._retn();
484   if ( CORBA::is_nil( theIOR ))
485     return aSO._retn();
486   if(MYDEBUG) MESSAGE("PublishInStudy");
487
488   // Publishing a mesh
489   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
490   if( !aMesh->_is_nil() )
491     aSO = PublishMesh( aMesh, theName );
492
493   // Publishing a sub-mesh
494   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
495   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
496     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
497     aMesh = aSubMesh->GetFather();
498     aSO = PublishSubMesh( aMesh, aSubMesh, aShapeObject, theName );
499   }
500
501   // Publishing a hypothesis or algorithm
502   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
503   if ( aSO->_is_nil() && !aHyp->_is_nil() )
504     aSO = PublishHypothesis( aHyp, theName );
505
506   // Publishing a group
507   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
508   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
509     GEOM::GEOM_Object_var aShapeObject;
510     aMesh = aGroup->GetMesh();
511     aSO = PublishGroup( aMesh, aGroup, aShapeObject, theName );
512   }
513   if(MYDEBUG) MESSAGE("PublishInStudy_END");
514
515   return aSO._retn();
516 }
517
518 //=======================================================================
519 //function : PublishComponent
520 //purpose  : 
521 //=======================================================================
522
523 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent()
524 {
525   if(MYDEBUG) MESSAGE("PublishComponent");
526   if ( !myIsEnablePublish )
527     return SALOMEDS::SComponent::_nil();
528
529   SALOMEDS::StudyBuilder_var    aStudyBuilder  = getStudyServant()->NewBuilder();
530   SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = getStudyServant()->GetUseCaseBuilder();
531
532   CORBA::String_var   compDataType = ComponentDataType();
533   SALOMEDS::SComponent_wrap father = getStudyServant()->FindComponent( compDataType.in() );
534   if ( !CORBA::is_nil( father ) ) {
535     // check that the component is added to the use case browser
536     if ( !useCaseBuilder->IsUseCaseNode( father ) ) {
537       useCaseBuilder->SetRootCurrent();
538       useCaseBuilder->Append( father ); // component object is added as the top level item
539     }
540     return father._retn();
541   }
542
543   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
544     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
545   if ( CORBA::is_nil( aCat ) )
546     return father._retn();
547
548   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
549   if ( CORBA::is_nil( aComp ) )
550     return father._retn();
551
552   SALOMEDS::GenericAttribute_wrap anAttr;
553   SALOMEDS::AttributePixMap_wrap  aPixmap;
554
555   father  = aStudyBuilder->NewComponent( compDataType.in() );
556   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
557   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
558   aPixmap = anAttr;
559   aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
560   CORBA::String_var userName = aComp->componentusername();
561   SetName( father, userName.in(), "MESH" );
562   // add component to the use case tree
563   // (to support tree representation customization and drag-n-drop)
564   useCaseBuilder->SetRootCurrent();
565   useCaseBuilder->Append( father ); // component object is added as the top level item
566   if(MYDEBUG) MESSAGE("PublishComponent--END");
567
568   return father._retn();
569 }
570
571 //=======================================================================
572 //function : PublishMesh
573 //purpose  : 
574 //=======================================================================
575
576 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SMESH::SMESH_Mesh_ptr theMesh,
577                                                 const char*           theName)
578 {
579   if ( !myIsEnablePublish )
580     return SALOMEDS::SObject::_nil();
581   if ( CORBA::is_nil( theMesh ))
582     return SALOMEDS::SObject::_nil();
583   if(MYDEBUG) MESSAGE("PublishMesh--IN");
584
585   // find or publish a mesh
586
587   SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theMesh );
588   if ( aMeshSO->_is_nil() )
589   {
590     SALOMEDS::SComponent_wrap father = PublishComponent();
591     if ( father->_is_nil() )
592       return aMeshSO._retn();
593
594     // Find correct free tag
595     long aTag = father->GetLastChildTag();
596     if ( aTag <= GetAlgorithmsRootTag() )
597       aTag = GetAlgorithmsRootTag() + 1;
598     else
599       aTag++;
600
601     aMeshSO = publish ( theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
602     if ( aMeshSO->_is_nil() )
603       return aMeshSO._retn();
604   }
605   SetName( aMeshSO, theName, "Mesh" );
606
607   // Add shape reference
608
609   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
610   if ( !CORBA::is_nil( aShapeObject )) {
611     addReference( aMeshSO, aShapeObject, GetRefOnShapeTag() );
612
613     // Publish global hypotheses
614
615     SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
616     for ( CORBA::ULong i = 0; i < hypList->length(); i++ )
617     {
618       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
619       SALOMEDS::SObject_wrap so = PublishHypothesis( aHyp );
620       AddHypothesisToShape( theMesh, aShapeObject, aHyp );
621     }
622   }
623
624   // Publish submeshes
625
626   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
627   if ( !mesh_i )
628     return aMeshSO._retn();
629   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
630   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
631   for ( ; subIt != subMap.end(); subIt++ ) {
632     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
633     if ( !CORBA::is_nil( aSubMesh )) {
634       aShapeObject = aSubMesh->GetSubShape();
635       SALOMEDS::SObject_wrap( PublishSubMesh( theMesh, aSubMesh, aShapeObject ));
636     }
637   }
638
639   // Publish groups
640   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
641   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
642   for ( ; it != grMap.end(); it++ )
643   {
644     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
645     if ( !aGroup->_is_nil() ) {
646       GEOM::GEOM_Object_var aShapeObj;
647       SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
648       if ( !aGeomGroup->_is_nil() )
649         aShapeObj = aGeomGroup->GetShape();
650       SALOMEDS::SObject_wrap( PublishGroup( theMesh, aGroup, aShapeObj ));
651     }
652   }
653
654   if(MYDEBUG) MESSAGE("PublishMesh_END");
655   return aMeshSO._retn();
656 }
657
658 //=======================================================================
659 //function : PublishSubMesh
660 //purpose  : 
661 //=======================================================================
662
663 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SMESH::SMESH_Mesh_ptr    theMesh,
664                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
665                                                    GEOM::GEOM_Object_ptr    theShapeObject,
666                                                    const char*              theName)
667 {
668   if ( !myIsEnablePublish )
669     return SALOMEDS::SObject::_nil();
670   if ( theMesh->_is_nil() || theSubMesh->_is_nil() || theShapeObject->_is_nil() )
671     return SALOMEDS::SObject::_nil();
672
673   SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theSubMesh );
674   if ( aSubMeshSO->_is_nil() )
675   {
676     SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theMesh );
677     if ( aMeshSO->_is_nil() ) {
678       aMeshSO = PublishMesh( theMesh );
679       if ( aMeshSO->_is_nil())
680         return SALOMEDS::SObject::_nil();
681     }
682     // Find submesh sub-tree tag
683     long aRootTag;
684     const char* aRootName = "";
685     switch ( theShapeObject->GetShapeType() ) {
686     case GEOM::VERTEX:
687       aRootTag  = GetSubMeshOnVertexTag();
688       aRootName = "SubMeshes on Vertex";
689       break;
690     case GEOM::EDGE:
691       aRootTag  = GetSubMeshOnEdgeTag();
692       aRootName = "SubMeshes on Edge";
693       break;
694     case GEOM::WIRE:
695       aRootTag  = GetSubMeshOnWireTag();
696       aRootName = "SubMeshes on Wire";
697       break;
698     case GEOM::FACE:
699       aRootTag  = GetSubMeshOnFaceTag();
700       aRootName = "SubMeshes on Face";    
701       break;
702     case GEOM::SHELL:
703       aRootTag  = GetSubMeshOnShellTag();
704       aRootName = "SubMeshes on Shell";   
705       break;
706     case GEOM::SOLID:
707       aRootTag  = GetSubMeshOnSolidTag();
708       aRootName = "SubMeshes on Solid";
709       break;
710     default:
711       aRootTag  = GetSubMeshOnCompoundTag();
712       aRootName = "SubMeshes on Compound";
713       break;
714     }
715
716     // Find or create submesh root
717     SALOMEDS::SObject_wrap aRootSO = publish ( CORBA::Object::_nil(),
718                                                aMeshSO, aRootTag, 0, false );
719     if ( aRootSO->_is_nil() )
720       return aSubMeshSO._retn();
721
722     SetName( aRootSO, aRootName );
723
724     // Add new submesh to corresponding sub-tree
725     SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
726     const int isEmpty = ( elemTypes->length() == 0 );
727     const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
728     aSubMeshSO = publish ( theSubMesh, aRootSO, 0, pm[isEmpty] );
729     if ( aSubMeshSO->_is_nil() )
730       return aSubMeshSO._retn();
731   }
732   SetName( aSubMeshSO, theName, "SubMesh" );
733
734   // Add reference to theShapeObject
735
736   addReference( aSubMeshSO, theShapeObject, 1 );
737
738   // Publish hypothesis
739
740   SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
741   for ( CORBA::ULong i = 0; i < hypList->length(); i++ ) {
742     SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
743     SALOMEDS::SObject_wrap so = PublishHypothesis( aHyp );
744     AddHypothesisToShape( theMesh, theShapeObject, aHyp );
745   }
746
747   return aSubMeshSO._retn();
748 }
749
750 //=======================================================================
751 //function : PublishGroup
752 //purpose  : 
753 //=======================================================================
754
755 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SMESH::SMESH_Mesh_ptr  theMesh,
756                                                  SMESH::SMESH_GroupBase_ptr theGroup,
757                                                  GEOM::GEOM_Object_ptr  theShapeObject,
758                                                  const char*            theName)
759 {
760   if ( !myIsEnablePublish )
761     return SALOMEDS::SObject::_nil();
762   if (theMesh->_is_nil() || theGroup->_is_nil() )
763     return SALOMEDS::SObject::_nil();
764
765   SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theGroup );
766   if ( aGroupSO->_is_nil() )
767   {
768     SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theMesh );
769     if ( aMeshSO->_is_nil() ) {
770       aMeshSO = PublishInStudy( SALOMEDS::SObject::_nil(), theMesh, "");
771       if ( aMeshSO->_is_nil())
772         return SALOMEDS::SObject::_nil();
773     }
774     size_t aType = (int)theGroup->GetType();
775     const char* aRootNames[] = {
776       "Compound Groups", "Groups of Nodes", "Groups of Edges",
777       "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
778       "Groups of Balls" };
779
780     // Currently, groups with heterogeneous content are not supported
781     if ( aType != SMESH::ALL )
782     {
783       long aRootTag = GetNodeGroupsTag() + aType - 1;
784
785       // Find or create groups root
786       SALOMEDS::SObject_wrap aRootSO = publish ( CORBA::Object::_nil(),
787                                                  aMeshSO, aRootTag, 0, false );
788       if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
789
790       if ( aType < sizeof(aRootNames)/sizeof(char*) )
791         SetName( aRootSO, aRootNames[aType] );
792
793       // Add new group to corresponding sub-tree
794       int isEmpty = false;
795       std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
796       if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
797       {
798         pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
799       }
800       else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
801       {
802         if ( theGroup->GetType() == SMESH::NODE )
803           isEmpty = ( theMesh->NbNodes() == 0 );
804         else
805         {
806           SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
807           for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
808             isEmpty = ( allElemTypes[i] != theGroup->GetType() );
809         }
810       }
811       aGroupSO = publish ( theGroup, aRootSO, 0, pm[isEmpty].c_str() );
812     }
813     if ( aGroupSO->_is_nil() )
814       return aGroupSO._retn();
815   }
816
817   SetName( aGroupSO, theName, "Group" );
818
819   //Add reference to geometry
820   if ( !theShapeObject->_is_nil() )
821     addReference( aGroupSO, theShapeObject, 1 );
822
823   return aGroupSO._retn();
824 }
825
826 //=======================================================================
827 //function : PublishHypothesis
828 //purpose  : 
829 //=======================================================================
830
831 SALOMEDS::SObject_ptr
832   SMESH_Gen_i::PublishHypothesis (SMESH::SMESH_Hypothesis_ptr theHyp,
833                                   const char*                 theName)
834 {
835   if(MYDEBUG) MESSAGE("PublishHypothesis")
836   if ( !myIsEnablePublish )
837     return SALOMEDS::SObject::_nil();
838   if (theHyp->_is_nil())
839     return SALOMEDS::SObject::_nil();
840
841   CORBA::String_var hypType = theHyp->GetName();
842
843   SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theHyp );
844   if ( aHypSO->_is_nil() )
845   {
846     SALOMEDS::SComponent_wrap father = PublishComponent();
847     if ( father->_is_nil() )
848       return aHypSO._retn();
849
850     //Find or Create Hypothesis root
851     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
852     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
853     SALOMEDS::SObject_wrap aRootSO =
854       publish (CORBA::Object::_nil(),father, aRootTag,
855                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
856     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
857
858     // Add New Hypothesis
859     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
860     aPmName += hypType.in();
861     // prepend plugin name to pixmap name
862     string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
863     if ( pluginName != "StdMeshers" )
864       aPmName = pluginName + "::" + aPmName;
865     aHypSO = publish( theHyp, aRootSO, 0, aPmName.c_str() );
866   }
867
868   SetName( aHypSO, theName, hypType.in() );
869
870   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
871   return aHypSO._retn();
872 }
873
874 //=======================================================================
875 //function : UpdateIcons
876 //purpose  : update icons of a mesh and its children upon mesh modification
877 //=======================================================================
878
879 void SMESH_Gen_i::UpdateIcons( SMESH::SMESH_Mesh_ptr theMesh )
880 {
881   SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( theMesh );
882   if ( ! mesh_i )
883     return;
884
885   SALOMEDS::SObject_wrap so = ObjectToSObject( theMesh );
886   if ( so->_is_nil() )
887     return;
888
889   // set icon of the mesh
890   if ( mesh_i->NbNodes() == 0 )
891     SetPixMap( so, "ICON_SMESH_TREE_MESH_WARN" );
892   else if ( mesh_i->IsComputedOK() )
893     SetPixMap( so, "ICON_SMESH_TREE_MESH" );
894   else
895     SetPixMap( so, "ICON_SMESH_TREE_MESH_PARTIAL" );
896
897   // set icons of sub-objects
898   SALOMEDS::Study_var         study = getStudyServant();
899   SALOMEDS::ChildIterator_wrap iter = study->NewChildIterator( so );
900   for ( ; iter->More(); iter->Next() )
901   {
902     so = iter->Value(); // 1st level child - root of algos, hyps, sub-meshes or groups
903     if ( so->Tag() < SMESH::Tag_FirstSubMesh )
904       continue;
905
906     SALOMEDS::ChildIterator_wrap subIter = study->NewChildIterator( so );
907     for ( ; subIter->More(); subIter->Next() )
908     {
909       so = subIter->Value(); // 2nd level child - a sub-mesh or group
910
911       CORBA::Object_var           obj = SObjectToObject( so );
912       SMESH::SMESH_IDSource_var idSrc = SMESH::SMESH_IDSource::_narrow( obj );
913       if ( idSrc->_is_nil() )
914         continue;
915
916       SMESH::SMESH_GroupBase_var     grp = SMESH::SMESH_GroupBase::_narrow( obj );
917       SMESH::SMESH_GroupOnFilter_var gof = SMESH::SMESH_GroupOnFilter::_narrow( obj );
918       const bool         isGroup = !grp->_is_nil();
919       const bool isGroupOnFilter = !gof->_is_nil();
920
921       bool isEmpty = ( mesh_i->NbNodes() == 0 );
922       if ( !isEmpty )
923       {
924         if ( isGroupOnFilter ) // GetTypes() can be very long on GroupOnFilter!
925         {
926           SMESH::long_array_var nbByType = mesh_i->GetNbElementsByType();
927           isEmpty = ( nbByType[ grp->GetType() ] == 0 );
928         }
929         else
930         {
931           SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
932           isEmpty = ( elemTypes->length() == 0 );
933         }
934       }
935       if ( isEmpty )
936         SetPixMap( so, "ICON_SMESH_TREE_MESH_WARN");
937       else if ( !isGroup )
938         SetPixMap( so, "ICON_SMESH_TREE_MESH" );
939       else if ( isGroupOnFilter )
940         SetPixMap( so, "ICON_SMESH_TREE_GROUP_ON_FILTER" );
941       else
942         SetPixMap( so, "ICON_SMESH_TREE_GROUP" );
943     }
944   }
945 }
946
947 //=======================================================================
948 //function : GetMeshOrSubmeshByShape
949 //purpose  : 
950 //=======================================================================
951
952 SALOMEDS::SObject_ptr
953 SMESH_Gen_i::GetMeshOrSubmeshByShape (SMESH::SMESH_Mesh_ptr theMesh,
954                                       GEOM::GEOM_Object_ptr theShape)
955 {
956   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
957   SALOMEDS::SObject_wrap aMeshOrSubMesh;
958   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
959     return aMeshOrSubMesh._retn();
960   
961   TopoDS_Shape aShape;
962   if(theMesh->HasShapeToMesh())
963     aShape = GeomObjectToShape( theShape );
964   else
965     aShape = SMESH_Mesh::PseudoShape();
966
967   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
968
969   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
970     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
971     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
972       aMeshOrSubMesh = ObjectToSObject( theMesh );
973     else {
974       int shapeID = meshDS->ShapeToIndex( aShape );
975       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
976       if ( !aSubMesh->_is_nil() )
977         aMeshOrSubMesh = ObjectToSObject( aSubMesh );
978     }
979   }
980   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
981   return aMeshOrSubMesh._retn();
982 }
983
984 //=======================================================================
985 //function : AddHypothesisToShape
986 //purpose  : 
987 //=======================================================================
988
989 bool SMESH_Gen_i::AddHypothesisToShape(SMESH::SMESH_Mesh_ptr       theMesh,
990                                        GEOM::GEOM_Object_ptr       theShape,
991                                        SMESH::SMESH_Hypothesis_ptr theHyp)
992 {
993   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
994   if (theMesh->_is_nil() ||
995       theHyp->_is_nil() || (theShape->_is_nil()
996                             && theMesh->HasShapeToMesh()) )
997     return false;
998
999   SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theMesh );
1000   if ( aMeshSO->_is_nil() )
1001     aMeshSO = PublishMesh( theMesh );
1002   SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theHyp );
1003   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
1004     return false;
1005
1006   // Find a mesh or submesh referring to theShape
1007   SALOMEDS::SObject_wrap aMeshOrSubMesh =
1008     GetMeshOrSubmeshByShape( theMesh, theShape );
1009   if ( aMeshOrSubMesh->_is_nil() )
1010   {
1011     // publish submesh
1012     TopoDS_Shape aShape = GeomObjectToShape( theShape );
1013     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
1014     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
1015       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
1016       int shapeID = meshDS->ShapeToIndex( aShape );
1017       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
1018       aMeshOrSubMesh = PublishSubMesh( theMesh, aSubMesh, theShape );
1019     }
1020     if ( aMeshOrSubMesh->_is_nil() )
1021       return false;
1022   }
1023
1024   //Find or Create Applied Hypothesis root
1025   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
1026   SALOMEDS::SObject_wrap AHR =
1027     publish (CORBA::Object::_nil(), aMeshOrSubMesh,
1028              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
1029              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
1030   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
1031
1032   addReference( AHR, theHyp );
1033
1034   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
1035   return true;
1036 }
1037
1038 //=======================================================================
1039 //function : RemoveHypothesisFromShape
1040 //purpose  : 
1041 //=======================================================================
1042
1043 bool SMESH_Gen_i::RemoveHypothesisFromShape(SMESH::SMESH_Mesh_ptr       theMesh,
1044                                             GEOM::GEOM_Object_ptr       theShape,
1045                                             SMESH::SMESH_Hypothesis_ptr theHyp)
1046 {
1047   if (theMesh->_is_nil() ||
1048       theHyp->_is_nil() || (theShape->_is_nil()
1049                             && theMesh->HasShapeToMesh()))
1050     return false;
1051
1052   SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theHyp );
1053   if ( aHypSO->_is_nil() )
1054     return false;
1055
1056   CORBA::String_var hypEntry = aHypSO->GetID();
1057
1058   // Find a mesh or sub-mesh referring to theShape
1059   SALOMEDS::SObject_wrap aMeshOrSubMesh =
1060     GetMeshOrSubmeshByShape( theMesh, theShape );
1061   if ( aMeshOrSubMesh->_is_nil() )
1062     return false;
1063
1064   // Find and remove a reference to aHypSO
1065   SALOMEDS::SObject_wrap aRef, anObj;
1066   SALOMEDS::ChildIterator_wrap it = getStudyServant()->NewChildIterator( aMeshOrSubMesh );
1067   bool found = false;
1068   for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
1069     anObj = it->Value();
1070     if (anObj->ReferencedObject( aRef.inout() ))
1071     {
1072       CORBA::String_var refEntry = aRef->GetID();
1073       found = ( strcmp( refEntry, hypEntry ) == 0 );
1074     }
1075     if ( found )
1076     {
1077       SALOMEDS::StudyBuilder_var builder = getStudyServant()->NewBuilder();
1078       builder->RemoveObject( anObj );
1079     }
1080   }
1081
1082   return true;
1083 }
1084
1085 //================================================================================
1086 /*!
1087  * \brief Stores names of variables that WILL be passes as parameters when calling
1088  *        some method of a given object.
1089  *  \param [in] theObject - the object whose a method WILL be called with \a theParameters.
1090  *  \param [in] theParameters - a string containing parameters separated by ':'.
1091  */
1092 //================================================================================
1093
1094 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
1095 {
1096   // find variable names within theParameters
1097
1098   myLastObj.clear();
1099   myLastParameters.clear();
1100   myLastParamIndex.clear(); /* vector holding indices of virables within the string
1101                                of all variables used for theObject */ 
1102   int nbVars = 0;
1103   int pos = 0, prevPos = 0, len = strlen( theParameters );
1104   if ( len == 0 ) return;
1105   while ( pos <= len )
1106   {
1107     if ( pos == len || theParameters[pos] == ':' )
1108     {
1109       if ( prevPos < pos )
1110       {
1111         string val( theParameters + prevPos, theParameters + pos );
1112         if ( !getStudyServant()->IsVariable( val.c_str() ))
1113           val.clear();
1114         myLastParameters.push_back( val );
1115         nbVars += (! myLastParameters.back().empty() );
1116       }
1117       else
1118       {
1119         myLastParameters.push_back("");
1120       }
1121       prevPos = pos+1;
1122     }
1123     ++pos;
1124   }
1125
1126   if ( nbVars < 1 )
1127     return;
1128
1129   // store
1130   // (1) variable names in the string of all variables used for theObject and
1131   // (2) indices of found variables in myLastParamIndex.
1132
1133   // remember theObject
1134   SALOMEDS::SObject_wrap aSObj =  ObjectToSObject(theObject);
1135   if ( aSObj->_is_nil() )
1136     return;
1137   CORBA::String_var anObjEntry = aSObj->GetID();
1138   myLastObj = anObjEntry.in();
1139
1140   // get a string of variable names
1141   SALOMEDS::StudyBuilder_var   aStudyBuilder = getStudyServant()->NewBuilder();
1142   SALOMEDS::GenericAttribute_wrap     anAttr =
1143     aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1144   SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1145   CORBA::String_var                  oldVars = aStringAttr->Value();
1146   std::string                         varStr = oldVars.in();
1147
1148   // add new variables and find indices of variables
1149   for ( size_t i = 0; i < myLastParameters.size(); ++i )
1150   {
1151     int varIndex = -1;
1152     if ( !myLastParameters[i].empty() )
1153     {
1154       // find index of myLastParameters[i] in varStr
1155       int curIndex  = 0;
1156       bool varFound = false;
1157       size_t pos    = 0;
1158       // varStr can be "A|B::C;*=2|D"
1159       const std::string separators(":|;*=");
1160       while ( pos < varStr.size() && !varFound )
1161       {
1162         // skip separators
1163         while ( separators.find( varStr[ pos ]) != std::string::npos )
1164           if ( ++pos >= varStr.size() )
1165             break;
1166         // skip repetition number following '='
1167         if ( varStr[ pos-1 ] == '=' )
1168         {
1169           while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1170             ++pos;
1171           continue; // to skip next separator
1172         }
1173         // compare variable name
1174         if ( pos < varStr.size() )
1175         {
1176           varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1177                        // same string beginning but is length same?
1178                        ( pos + myLastParameters[i].size() >= varStr.size() ||
1179                          separators.find( varStr[ pos+1 ]) != std::string::npos ));
1180           if ( varFound )
1181             varIndex = curIndex;
1182           else
1183             pos = varStr.find_first_of( separators, pos ); // goto the next separator
1184           ++curIndex;
1185         }
1186       }
1187       // add new variable
1188       if ( !varFound )
1189       {
1190         varStr += ":" + myLastParameters[i];
1191         varIndex = curIndex;
1192       }
1193     }
1194     myLastParamIndex.push_back( varIndex );
1195   }
1196   aStringAttr->SetValue( varStr.c_str() );
1197 }
1198
1199 //================================================================================
1200 /*!
1201  * \brief Return all variables used to create an object
1202  *  \param [in] theObjectEntry - an object entry in the current study
1203  *  \return std::vector< std::string > - all variable names (or values of removed variables)
1204  */
1205 //================================================================================
1206
1207 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1208 {
1209   std::vector< std::string > varNames;
1210
1211   SALOMEDS::SObject_wrap aSObj = getStudyServant()->FindObjectID( theObjectEntry.c_str() );
1212
1213   // get a string of variable names
1214   SALOMEDS::StudyBuilder_var   aStudyBuilder = getStudyServant()->NewBuilder();
1215   SALOMEDS::GenericAttribute_wrap     anAttr =
1216     aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1217   SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1218   CORBA::String_var                  oldVars = aStringAttr->Value();
1219   std::string                         varStr = oldVars.in();
1220
1221   // separate variables within varStr;
1222   // varStr can be "A|B::C;*=2|D"
1223   size_t pos = 0;
1224   const std::string separators(":|;*=");
1225   while ( pos < varStr.size() )
1226   {
1227     // skip separators
1228     pos = varStr.find_first_not_of( separators, pos );
1229     // while ( separators.find( varStr[ pos ]) != std::string::npos )
1230     //   if ( ++pos >= varStr.size() )
1231     //     break;
1232     // skip repetition number following '='
1233     if ( varStr[ pos-1 ] == '=' )
1234     {
1235       while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1236         ++pos;
1237       continue; // to skip next separator
1238     }
1239     // store variable name
1240     if ( pos < varStr.size() )
1241     {
1242       size_t pos2 = varStr.find_first_of( separators, pos );
1243       varNames.push_back( varStr.substr( pos, pos2 - pos));
1244       pos = pos2;
1245     }
1246   }
1247   return varNames;
1248 }
1249
1250 //=======================================================================
1251 //function : ParseParameters
1252 //purpose  : Replace variables by their values
1253 //=======================================================================
1254 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1255 // {
1256 //   //const char* aParameters = theParameters;
1257 // //   const char* aParameters = CORBA::string_dup(theParameters);
1258 //   TCollection_AsciiString anInputParams;
1259 //   SALOMEDS::Study_var aStudy = getStudyServant();
1260 //   if( !aStudy->_is_nil() ) {
1261 // //     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1262 // //     for(int j=0;j<aSections->length();j++) {
1263 // //       SALOMEDS::ListOfStrings aVars= aSections[j];
1264 // //       for(int i=0;i<aVars.length();i++ ) {
1265 // //         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
1266 // //           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1267 // //         if(i != aVars.length()-1)
1268 // //           anInputParams+=":";
1269 // //       }
1270 // //       if(j!=aSections->length()-1)
1271 // //         anInputParams+="|";
1272 // //     }
1273 //     TCollection_AsciiString paramStr( theParameters );
1274 //     int beg = 0, end;
1275 //     char sep, *pParams = (char*)paramStr.ToCString();
1276 //     while ( beg < paramStr.Length() )
1277 //     {
1278 //       end = beg-1;
1279 //       while ( ++end < paramStr.Length() )
1280 //         if ( pParams[end] == ':' || pParams[end] == '|')
1281 //           break;
1282 //       if ( end < paramStr.Length())
1283 //       {
1284 //         sep = pParams[end];
1285 //         pParams[end] = '\0';
1286 //       }
1287 //       if ( aStudy->IsVariable( pParams+beg ))
1288 //         anInputParams += pParams+beg;
1289 //       if ( end < paramStr.Length() )
1290 //         anInputParams += sep;
1291 //       else
1292 //         break;
1293 //       beg = end + 1;
1294 //     }
1295 //   }
1296 //   return CORBA::string_dup(anInputParams.ToCString());
1297 // }
1298
1299 //=======================================================================
1300 //function : GetParameters
1301 //purpose  : 
1302 //=======================================================================
1303
1304 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1305 {
1306   CORBA::String_var aResult("");
1307
1308   SALOMEDS::SObject_wrap aSObj = ObjectToSObject( theObject );
1309   if ( !aSObj->_is_nil() )
1310   {
1311     SALOMEDS::GenericAttribute_wrap attr;
1312     if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1313     {
1314       SALOMEDS::AttributeString_wrap strAttr = attr;
1315       aResult = strAttr->Value();
1316     }
1317   }
1318   return aResult._retn();
1319 }
1320
1321
1322
1323 // ==============
1324 // Study context
1325 // ==============
1326
1327 //=======================================================================
1328 //function : addObject
1329 //purpose  : register object in the internal map and return its id
1330 //=======================================================================
1331
1332 int StudyContext::addObject( const std::string& theIOR )
1333 {
1334   int nextId = getNextId();
1335   mapIdToIOR.Bind( nextId, theIOR );
1336   return nextId;
1337 }
1338
1339 //=======================================================================
1340 //function : findId
1341 //purpose  : find the object id in the internal map by the IOR
1342 //=======================================================================
1343
1344 int StudyContext::findId( const std::string& theIOR )
1345 {
1346   TInt2StringMap::iterator imap;
1347   for ( imap = mapIdToIOR.begin(); imap != mapIdToIOR.end(); ++imap ) {
1348     if ( *imap == theIOR )
1349       return imap.Iterator().Key();
1350   }
1351   return 0;
1352 }
1353
1354 //=======================================================================
1355 //function : getIORbyId
1356 //purpose  : get object's IOR by id
1357 //=======================================================================
1358
1359 std::string StudyContext::getIORbyId( const int theId )
1360 {
1361   if ( mapIdToIOR.IsBound( theId ) )
1362     return mapIdToIOR( theId );
1363   return std::string();
1364 }
1365
1366 //=======================================================================
1367 //function : getIORbyOldId
1368 //purpose  : get object's IOR by old id
1369 //=======================================================================
1370
1371 std::string StudyContext::getIORbyOldId( const int theOldId )
1372 {
1373   if ( mapIdToId.IsBound( theOldId ) )
1374     return getIORbyId( mapIdToId( theOldId ));
1375   return std::string();
1376 }
1377
1378 //=======================================================================
1379 //function : mapOldToNew
1380 //purpose  : maps old object id to the new one (used when restoring data)
1381 //=======================================================================
1382
1383 void StudyContext::mapOldToNew( const int oldId, const int newId )
1384 {
1385   mapIdToId.Bind( oldId, newId );
1386 }
1387
1388 //=======================================================================
1389 //function : getOldId
1390 //purpose  : get old id by a new one
1391 //=======================================================================
1392
1393 int StudyContext::getOldId( const int newId )
1394 {
1395   TInt2IntMap::iterator imap;
1396   for ( imap = mapIdToId.begin(); imap != mapIdToId.end(); ++imap ) {
1397     if ( *imap == newId )
1398       return imap.Iterator().Key();
1399   }
1400   return 0;
1401 }