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