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