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