Salome HOME
Replace Descret by Discrete in variable and method names.
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i_1.cxx
1 // Copyright (C) 2007-2011  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.
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 //  $Header   : $
27
28 #include "SMESH_Gen_i.hxx"
29
30 #include "SMESH_Mesh_i.hxx"
31 #include "SMESH_Hypothesis_i.hxx"
32 #include "SMESH_Algo_i.hxx"
33 #include "SMESH_Group_i.hxx"
34 #include "SMESH_subMesh_i.hxx"
35
36 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
37
38 #include "utilities.h"
39 #include "Utils_ExceptHandlers.hxx"
40
41 #include <TCollection_AsciiString.hxx>
42
43 #ifdef _DEBUG_
44 static int MYDEBUG = 0;
45 static int VARIABLE_DEBUG = 0;
46 #else
47 static int MYDEBUG = 0;
48 static int VARIABLE_DEBUG = 0;
49 #endif
50
51 //=============================================================================
52 /*!
53  *  Get...Tag [ static ]
54  *
55  *  Methods which determine SMESH data model structure
56  */
57 //=============================================================================
58
59 long SMESH_Gen_i::GetHypothesisRootTag()
60 {
61   return SMESH::Tag_HypothesisRoot;
62 }
63
64 long SMESH_Gen_i::GetAlgorithmsRootTag()
65 {
66   return SMESH::Tag_AlgorithmsRoot;
67 }
68
69 long SMESH_Gen_i::GetRefOnShapeTag()
70 {
71   return SMESH::Tag_RefOnShape;
72 }
73
74 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
75 {
76   return SMESH::Tag_RefOnAppliedHypothesis;
77 }
78
79 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
80 {
81   return SMESH::Tag_RefOnAppliedAlgorithms;
82 }
83
84 long SMESH_Gen_i::GetSubMeshOnVertexTag()
85 {
86   return SMESH::Tag_SubMeshOnVertex;
87 }
88
89 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
90 {
91   return SMESH::Tag_SubMeshOnEdge;
92 }
93
94 long SMESH_Gen_i::GetSubMeshOnFaceTag()
95 {
96   return SMESH::Tag_SubMeshOnFace;
97 }
98
99 long SMESH_Gen_i::GetSubMeshOnSolidTag()
100 {
101   return SMESH::Tag_SubMeshOnSolid;
102 }
103
104 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
105 {
106   return SMESH::Tag_SubMeshOnCompound;
107 }
108
109 long SMESH_Gen_i::GetSubMeshOnWireTag()
110 {
111   return SMESH::Tag_SubMeshOnWire;
112 }
113
114 long SMESH_Gen_i::GetSubMeshOnShellTag()
115 {
116   return SMESH::Tag_SubMeshOnShell;
117 }
118
119 long SMESH_Gen_i::GetNodeGroupsTag()
120 {
121   return SMESH::Tag_NodeGroups;
122 }
123
124 long SMESH_Gen_i::GetEdgeGroupsTag()
125 {
126   return SMESH::Tag_EdgeGroups;
127 }
128
129 long SMESH_Gen_i::GetFaceGroupsTag()
130 {
131   return SMESH::Tag_FaceGroups;
132 }
133
134 long SMESH_Gen_i::GetVolumeGroupsTag()
135 {
136   return SMESH::Tag_VolumeGroups;
137 }
138
139 long SMESH_Gen_i::Get0DElementsGroupsTag()
140 {
141   return SMESH::Tag_0DElementsGroups;
142 }
143
144 //=============================================================================
145 /*!
146  *  SMESH_Gen_i::CanPublishInStudy
147  *
148  *  Returns true if object can be published in the study
149  */
150 //=============================================================================
151
152 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
153 {
154   if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
155   if(CORBA::is_nil(myCurrentStudy))
156     return false;
157   
158   SMESH::SMESH_Mesh_var aMesh       = SMESH::SMESH_Mesh::_narrow(theIOR);
159   if( !aMesh->_is_nil() )
160     return true;
161
162   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
163   if( !aSubMesh->_is_nil() )
164     return true;
165
166   SMESH::SMESH_Hypothesis_var aHyp  = SMESH::SMESH_Hypothesis::_narrow(theIOR);
167   if( !aHyp->_is_nil() )
168     return true;
169
170   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
171   if( !aGroup->_is_nil() )
172     return true;
173
174   if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
175   return false;
176 }
177
178 //=======================================================================
179 //function : ObjectToSObject
180 //purpose  : 
181 //=======================================================================
182
183 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
184                                                    CORBA::Object_ptr   theObject)
185 {
186   SALOMEDS::SObject_var aSO;
187   if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
188   {
189     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
190     aSO = theStudy->FindObjectIOR( objStr.in() );
191   }
192   return aSO._retn();
193 }
194
195 //=======================================================================
196 //function : objectToServant
197 //purpose  : 
198 //=======================================================================
199
200 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
201 {
202   return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
203 }
204
205 //=======================================================================
206 //function : ShapeToGeomObject
207 //purpose  : 
208 //=======================================================================
209
210 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
211 {
212   GEOM::GEOM_Object_var aShapeObj;
213   if ( !theShape.IsNull() ) {
214     GEOM_Client* aClient = GetShapeReader();
215     TCollection_AsciiString IOR;
216     if ( aClient && aClient->Find( theShape, IOR ))
217     {
218       CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
219       aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
220     }
221   }
222   return aShapeObj._retn();
223 }
224
225 //=======================================================================
226 //function : GeomObjectToShape
227 //purpose  : 
228 //=======================================================================
229
230 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
231 {
232   TopoDS_Shape S;
233   if ( !theGeomObject->_is_nil() ) {
234     GEOM_Client* aClient = GetShapeReader();
235     GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
236     if ( aClient && !aGeomEngine->_is_nil () )
237       S = aClient->GetShape( aGeomEngine, theGeomObject );
238   }
239   return S;
240 }
241
242 //=======================================================================
243 //function : publish
244 //purpose  : 
245 //=======================================================================
246
247 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr   theStudy,
248                                      CORBA::Object_ptr     theIOR,
249                                      SALOMEDS::SObject_ptr theFatherObject,
250                                      const int             theTag = 0,
251                                      const char*           thePixMap = 0,
252                                      const bool            theSelectable = true)
253 {
254   SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
255   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
256   if ( SO->_is_nil() ) {
257     if ( theTag == 0 )
258       SO = aStudyBuilder->NewObject( theFatherObject );
259     else if ( !theFatherObject->FindSubObject( theTag, SO ))
260       SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
261   }
262
263   SALOMEDS::GenericAttribute_var anAttr;
264   if ( !CORBA::is_nil( theIOR )) {
265     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
266     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
267     SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
268   }
269   if ( thePixMap ) {
270     anAttr  = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
271     SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
272     pm->SetPixMap( thePixMap );
273   }
274   if ( !theSelectable ) {
275     anAttr   = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
276     SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
277   }
278   return SO._retn();
279 }
280
281 //=======================================================================
282 //function : setName
283 //purpose  : 
284 //=======================================================================
285
286 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
287                           const char*           theName,
288                           const char*           theDefaultName)
289 {
290   if ( !theSObject->_is_nil() ) {
291     SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
292     SALOMEDS::GenericAttribute_var anAttr =
293       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
294     SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
295     if ( theName && strlen( theName ) != 0 )
296       aNameAttr->SetValue( theName );
297     else {
298       CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
299       if ( strlen( curName ) == 0 ) {
300         TCollection_AsciiString aName( (char*) theDefaultName );
301         aName += TCollection_AsciiString("_") + TCollection_AsciiString( theSObject->Tag() );
302         aNameAttr->SetValue( aName.ToCString() );
303       }
304     }
305   }
306 }
307
308 //=======================================================================
309 //function : SetPixMap
310 //purpose  : 
311 //=======================================================================
312
313 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
314                             const char*           thePixMap)
315 {
316   if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
317   {
318     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
319     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
320     SALOMEDS::GenericAttribute_var anAttr =
321       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
322     SALOMEDS::AttributePixMap_var aPMAttr = SALOMEDS::AttributePixMap::_narrow( anAttr );
323     aPMAttr->SetPixMap( thePixMap );
324   }
325 }
326
327 //=======================================================================
328 //function : addReference
329 //purpose  : 
330 //=======================================================================
331
332 static void addReference (SALOMEDS::Study_ptr   theStudy,
333                           SALOMEDS::SObject_ptr theSObject,
334                           CORBA::Object_ptr     theToObject,
335                           int                   theTag = 0)
336 {
337   SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
338   if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
339     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
340     SALOMEDS::SObject_var aReferenceSO;
341     if ( !theTag ) {
342       // check if the reference to theToObject already exists
343       // and find a free label for the reference object
344       bool isReferred = false;
345       int tag = 1;
346       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
347       for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
348         if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
349           if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
350             isReferred = true;
351         }
352         else if ( !theTag ) {
353           SALOMEDS::GenericAttribute_var anAttr;
354           if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
355             theTag = tag;
356         }
357       }
358       if ( isReferred )
359         return;
360       if ( !theTag )
361         theTag = tag;
362     }
363     if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
364       aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
365     aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
366   }
367 }
368
369 //=============================================================================
370 /*!
371  *  SMESH_Gen_i::PublishInStudy
372  *
373  *  Publish object in the study
374  */
375 //=============================================================================
376
377 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
378                                                   SALOMEDS::SObject_ptr theSObject,
379                                                   CORBA::Object_ptr     theIOR,
380                                                   const char*           theName)
381      throw (SALOME::SALOME_Exception)
382 {
383   Unexpect aCatch(SALOME_SalomeException);
384   SALOMEDS::SObject_var aSO;
385   if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
386     return aSO._retn();
387   if(MYDEBUG) MESSAGE("PublishInStudy");
388
389   // Publishing a mesh
390   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
391   if( !aMesh->_is_nil() )
392     aSO = PublishMesh( theStudy, aMesh, theName );
393
394   // Publishing a sub-mesh
395   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
396   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
397     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
398     aMesh = aSubMesh->GetFather();
399     aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
400   }
401
402   // Publishing a hypothesis or algorithm
403   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
404   if ( aSO->_is_nil() && !aHyp->_is_nil() )
405     aSO = PublishHypothesis( theStudy, aHyp );
406
407   // Publishing a group
408   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
409   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
410     GEOM::GEOM_Object_var aShapeObject;
411     aMesh = aGroup->GetMesh();
412     aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
413   }
414   if(MYDEBUG) MESSAGE("PublishInStudy_END");
415
416   return aSO._retn();
417 }
418
419 //=======================================================================
420 //function : PublishComponent
421 //purpose  : 
422 //=======================================================================
423
424 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
425 {
426   if ( CORBA::is_nil( theStudy ))
427     return SALOMEDS::SComponent::_nil();
428   if(MYDEBUG) MESSAGE("PublishComponent");
429
430   SALOMEDS::SComponent_var father =
431     SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
432   if ( !CORBA::is_nil( father ) )
433     return father._retn();
434
435   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
436     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
437   if ( CORBA::is_nil( aCat ) )
438     return father._retn();
439
440   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
441   if ( CORBA::is_nil( aComp ) )
442     return father._retn();
443
444   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
445   SALOMEDS::GenericAttribute_var anAttr;
446   SALOMEDS::AttributePixMap_var  aPixmap;
447
448   father  = aStudyBuilder->NewComponent( ComponentDataType() );
449   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
450   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
451   aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
452   aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
453   SetName( father, aComp->componentusername(), "MESH" );
454   if(MYDEBUG) MESSAGE("PublishComponent--END");
455
456   return father._retn();
457 }
458
459 //=============================================================================
460 /*!
461  *  findMaxChildTag [ static internal ]
462  *
463  *  Finds maximum child tag for the given object
464  */
465 //=============================================================================
466
467 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
468 {
469   long aTag = 0;
470   if ( !theSObject->_is_nil() ) {
471     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
472     if ( !aStudy->_is_nil() ) {
473       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
474       for ( ; anIter->More(); anIter->Next() ) {
475         long nTag = anIter->Value()->Tag();
476         if ( nTag > aTag )
477           aTag = nTag;
478       }
479     }
480   }
481   return aTag;
482 }
483
484 //=======================================================================
485 //function : PublishMesh
486 //purpose  : 
487 //=======================================================================
488
489 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr   theStudy,
490                                                 SMESH::SMESH_Mesh_ptr theMesh,
491                                                 const char*           theName)
492 {
493   if ( CORBA::is_nil( theStudy ) ||
494        CORBA::is_nil( theMesh ))
495     return SALOMEDS::SComponent::_nil();
496   if(MYDEBUG) MESSAGE("PublishMesh--IN");
497
498   // find or publish a mesh
499
500   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
501   if ( aMeshSO->_is_nil() )
502   {
503     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
504     if ( father->_is_nil() )
505       return aMeshSO._retn();
506
507     // Find correct free tag
508     long aTag = findMaxChildTag( father.in() );
509     if ( aTag <= GetAlgorithmsRootTag() )
510       aTag = GetAlgorithmsRootTag() + 1;
511     else
512       aTag++;
513
514     aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
515     if ( aMeshSO->_is_nil() )
516       return aMeshSO._retn();
517   }
518   SetName( aMeshSO, theName, "Mesh" );
519
520   // Add shape reference
521
522   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
523   if ( !CORBA::is_nil( aShapeObject )) {
524     addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
525
526     // Publish global hypotheses
527
528     SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
529     for ( int i = 0; i < hypList->length(); i++ ) {
530       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
531       PublishHypothesis( theStudy, aHyp );
532       AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
533     }
534   }
535
536   // Publish submeshes
537
538   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
539   if ( !mesh_i )
540     return aMeshSO._retn();
541   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
542   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
543   for ( ; subIt != subMap.end(); subIt++ ) {
544     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
545     if ( !CORBA::is_nil( aSubMesh )) {
546       aShapeObject = aSubMesh->GetSubShape();
547       PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
548     }
549   }
550
551   // Publish groups
552   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
553   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
554   for ( ; it != grMap.end(); it++ )
555   {
556     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
557     if ( !aGroup->_is_nil() ) {
558       GEOM::GEOM_Object_var  aShapeObj;
559       SMESH::SMESH_GroupOnGeom_var aGeomGroup =
560         SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
561       if ( !aGeomGroup->_is_nil() )
562         aShapeObj = aGeomGroup->GetShape();
563       PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
564     }
565   }
566
567   if(MYDEBUG) MESSAGE("PublishMesh_END");
568   return aMeshSO._retn();
569 }
570
571 //=======================================================================
572 //function : PublishSubMesh
573 //purpose  : 
574 //=======================================================================
575
576 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
577                                                    SMESH::SMESH_Mesh_ptr    theMesh,
578                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
579                                                    GEOM::GEOM_Object_ptr    theShapeObject,
580                                                    const char*              theName)
581 {
582   if (theStudy->_is_nil() || theMesh->_is_nil() ||
583       theSubMesh->_is_nil() || theShapeObject->_is_nil() )
584     return SALOMEDS::SObject::_nil();
585
586   SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
587   if ( aSubMeshSO->_is_nil() )
588   {
589     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
590     if ( aMeshSO->_is_nil() ) {
591       aMeshSO = PublishMesh( theStudy, theMesh );
592       if ( aMeshSO->_is_nil())
593         return SALOMEDS::SObject::_nil();
594     }
595     // Find submesh sub-tree tag
596     long aRootTag;
597     const char* aRootName = "";
598     switch ( theShapeObject->GetShapeType() ) {
599     case GEOM::VERTEX:
600       aRootTag  = GetSubMeshOnVertexTag();
601       aRootName = "SubMeshes on Vertex";
602       break;
603     case GEOM::EDGE:
604       aRootTag  = GetSubMeshOnEdgeTag();
605       aRootName = "SubMeshes on Edge";
606       break;
607     case GEOM::WIRE:
608       aRootTag  = GetSubMeshOnWireTag();
609       aRootName = "SubMeshes on Wire";
610       break;
611     case GEOM::FACE:
612       aRootTag  = GetSubMeshOnFaceTag();
613       aRootName = "SubMeshes on Face";    
614       break;
615     case GEOM::SHELL:
616       aRootTag  = GetSubMeshOnShellTag();
617       aRootName = "SubMeshes on Shell";   
618       break;
619     case GEOM::SOLID:
620       aRootTag  = GetSubMeshOnSolidTag();
621       aRootName = "SubMeshes on Solid";
622       break;
623     default:
624       aRootTag  = GetSubMeshOnCompoundTag();
625       aRootName = "SubMeshes on Compound";
626       break;
627     }
628
629     // Find or create submesh root
630     SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
631                                              aMeshSO, aRootTag, 0, false );
632     SetName( aRootSO, aRootName );
633
634     // Add new submesh to corresponding sub-tree
635     SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
636     const int isEmpty = ( elemTypes->length() == 0 );
637     const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
638     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
639     if ( aSubMeshSO->_is_nil() )
640       return aSubMeshSO._retn();
641   }
642   SetName( aSubMeshSO, theName, "SubMesh" );
643
644   // Add reference to theShapeObject
645
646   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
647
648   // Publish hypothesis
649
650   SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
651   if ( hypList )
652     for ( int i = 0; i < hypList->length(); i++ ) {
653       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
654       PublishHypothesis( theStudy, aHyp );
655       AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
656     }
657
658   return aSubMeshSO._retn();
659 }
660
661 //=======================================================================
662 //function : PublishGroup
663 //purpose  : 
664 //=======================================================================
665
666 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
667                                                  SMESH::SMESH_Mesh_ptr  theMesh,
668                                                  SMESH::SMESH_GroupBase_ptr theGroup,
669                                                  GEOM::GEOM_Object_ptr  theShapeObject,
670                                                  const char*            theName)
671 {
672   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
673     return SALOMEDS::SObject::_nil();
674
675   SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
676   if ( aGroupSO->_is_nil() )
677   {
678     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
679     if ( aMeshSO->_is_nil() ) {
680       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
681       if ( aMeshSO->_is_nil())
682         return SALOMEDS::SObject::_nil();
683     }
684     int aType = (int)theGroup->GetType();
685     const char* aRootNames[] = {
686       "Compound Groups", "Groups of Nodes", "Groups of Edges",
687       "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements" };
688
689     // Currently, groups with heterogenous content are not supported
690     if ( aType != SMESH::ALL ) {
691       long aRootTag = GetNodeGroupsTag() + aType - 1;
692
693       // Find or create groups root
694       SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
695                                                aMeshSO, aRootTag, 0, false );
696       if ( aType < 6 )
697         SetName( aRootSO, aRootNames[aType] );
698
699       // Add new group to corresponding sub-tree
700       SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
701       int isEmpty = ( elemTypes->length() == 0 );
702       std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
703       if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
704       {
705         pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
706       }
707       else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
708       {
709         SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
710         for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
711           isEmpty = ( allElemTypes[i] != theGroup->GetType() );
712       }
713       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
714     }
715     if ( aGroupSO->_is_nil() )
716       return aGroupSO._retn();
717   }
718
719   SetName( aGroupSO, theName, "Group" );
720
721   //Add reference to geometry
722   if ( !theShapeObject->_is_nil() )
723     addReference( theStudy, aGroupSO, theShapeObject, 1 );
724
725   return aGroupSO._retn();
726 }
727
728 //=======================================================================
729 //function : PublishHypothesis
730 //purpose  : 
731 //=======================================================================
732
733 SALOMEDS::SObject_ptr
734   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
735                                   SMESH::SMESH_Hypothesis_ptr theHyp,
736                                   const char*                 theName)
737 {
738   if(MYDEBUG) MESSAGE("PublishHypothesis")
739   if (theStudy->_is_nil() || theHyp->_is_nil())
740     return SALOMEDS::SObject::_nil();
741
742   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
743   if ( aHypSO->_is_nil() )
744   {
745     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
746     if ( father->_is_nil() )
747       return aHypSO._retn();
748
749     //Find or Create Hypothesis root
750     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
751     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
752     SALOMEDS::SObject_var aRootSO =
753       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
754                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
755     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
756
757     // Add New Hypothesis
758     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
759     aPmName += theHyp->GetName();
760     // prepend plugin name to pixmap name
761     string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
762     if ( pluginName != "StdMeshers" )
763       aPmName = pluginName + "::" + aPmName;
764     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
765   }
766
767   if ( !aHypSO->_is_nil() ) {
768     CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
769     SetName( aHypSO, theName, aHypName );
770   }
771
772   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
773   return aHypSO._retn();
774 }
775
776 //=======================================================================
777 //function : GetMeshOrSubmeshByShape
778 //purpose  : 
779 //=======================================================================
780
781 SALOMEDS::SObject_ptr
782   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
783                                         SMESH::SMESH_Mesh_ptr theMesh,
784                                         GEOM::GEOM_Object_ptr theShape)
785 {
786   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
787   SALOMEDS::SObject_var aMeshOrSubMesh;
788   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
789     return aMeshOrSubMesh._retn();
790   
791   TopoDS_Shape aShape;
792   if(theMesh->HasShapeToMesh())
793     aShape = GeomObjectToShape( theShape );
794   else
795     aShape = SMESH_Mesh::PseudoShape();
796
797   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
798
799   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
800     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
801     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
802       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
803     else {
804       int shapeID = meshDS->ShapeToIndex( aShape );
805       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
806       if ( !aSubMesh->_is_nil() )
807         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
808     }
809   }
810   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
811   return aMeshOrSubMesh._retn();
812 }
813
814 //=======================================================================
815 //function : AddHypothesisToShape
816 //purpose  : 
817 //=======================================================================
818
819 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
820                                        SMESH::SMESH_Mesh_ptr       theMesh,
821                                        GEOM::GEOM_Object_ptr       theShape,
822                                        SMESH::SMESH_Hypothesis_ptr theHyp)
823 {
824   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
825   if (theStudy->_is_nil() || theMesh->_is_nil() ||
826       theHyp->_is_nil() || (theShape->_is_nil()
827                             && theMesh->HasShapeToMesh()) )
828     return false;
829
830   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
831   if ( aMeshSO->_is_nil() )
832     aMeshSO = PublishMesh( theStudy, theMesh );
833   SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
834   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
835     return false;
836
837   // Find a mesh or submesh refering to theShape
838   SALOMEDS::SObject_var aMeshOrSubMesh =
839     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
840   if ( aMeshOrSubMesh->_is_nil() )
841   {
842     // publish submesh
843     TopoDS_Shape aShape = GeomObjectToShape( theShape );
844     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
845     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
846       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
847       int shapeID = meshDS->ShapeToIndex( aShape );
848       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
849       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
850     }
851     if ( aMeshOrSubMesh->_is_nil() )
852       return false;
853   }
854
855   //Find or Create Applied Hypothesis root
856   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
857   SALOMEDS::SObject_var AHR =
858     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
859              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
860              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
861   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
862   if ( AHR->_is_nil() )
863     return false;
864
865   addReference( theStudy, AHR, theHyp );
866   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
867   return true;
868 }
869
870 //=======================================================================
871 //function : RemoveHypothesisFromShape
872 //purpose  : 
873 //=======================================================================
874
875 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
876                                             SMESH::SMESH_Mesh_ptr       theMesh,
877                                             GEOM::GEOM_Object_ptr       theShape,
878                                             SMESH::SMESH_Hypothesis_ptr theHyp)
879 {
880   if (theStudy->_is_nil() || theMesh->_is_nil() ||
881       theHyp->_is_nil() || (theShape->_is_nil()
882                             && theMesh->HasShapeToMesh()))
883     return false;
884
885   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
886   if ( aHypSO->_is_nil() )
887     return false;
888
889   // Find a mesh or submesh refering to theShape
890   SALOMEDS::SObject_var aMeshOrSubMesh =
891     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
892   if ( aMeshOrSubMesh->_is_nil() )
893     return false;
894
895   // Find and remove a reference to aHypSO
896   SALOMEDS::SObject_var aRef, anObj;
897   CORBA::String_var     anID = CORBA::string_dup( aHypSO->GetID() );
898   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
899   for ( it->InitEx( true ); it->More(); it->Next() ) {
900     anObj = it->Value();
901     if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
902       theStudy->NewBuilder()->RemoveObject( anObj );
903       break;
904     }
905   }
906   return true;
907 }
908
909 //=======================================================================
910 //function : UpdateParameters
911 //purpose  : 
912 //=======================================================================
913 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
914 {
915
916   if(VARIABLE_DEBUG)
917     cout<<"UpdateParameters : "<<theParameters<<endl;
918   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
919   if(aStudy->_is_nil() || CORBA::is_nil(theObject)) 
920     return;
921
922   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
923   if(aSObj->_is_nil())  
924     return;
925
926   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
927
928   SALOMEDS::GenericAttribute_var aFindAttr;
929   bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
930   if(VARIABLE_DEBUG)
931     cout<<"Find Attribute "<<hasAttr<<endl;
932
933   SALOMEDS::GenericAttribute_var anAttr;
934   anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
935   SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
936
937   CORBA::String_var oldparVar = aStringAttr->Value();
938   CORBA::String_var inpparVar = ParseParameters(theParameters);
939   TCollection_AsciiString aNewParams;
940   TCollection_AsciiString aOldParameters(oldparVar.inout());
941   TCollection_AsciiString anInputParams(inpparVar.inout());
942   if(!hasAttr)
943     aNewParams = anInputParams;
944   else 
945     {
946       int pos = aOldParameters.SearchFromEnd("|");
947       if(pos==-1) pos = 0;
948       TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
949       TCollection_AsciiString previousParam(previousParamFull);
950       TCollection_AsciiString theRepet("1");
951       pos = previousParam.SearchFromEnd(";*=");
952       if(pos >= 0)
953         {
954           theRepet = previousParam.Split(pos+2);
955           pos = pos-1;
956           if(pos==-1) pos = 0;
957           previousParam.Split(pos);
958         }
959       if(previousParam == anInputParams)
960         {
961           theRepet = theRepet.IntegerValue()+1;
962           aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
963         }
964       else
965         {
966           aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
967         }
968     }
969
970   if(VARIABLE_DEBUG)
971   {
972     cout<<"Input Parameters : "<<anInputParams<<endl;
973     cout<<"Old Parameters : "<<aOldParameters<<endl;
974     cout<<"New Parameters : "<<aNewParams<<endl;
975   }
976
977   aStringAttr->SetValue( aNewParams.ToCString() );
978 }
979
980 //=======================================================================
981 //function : ParseParameters
982 //purpose  : 
983 //=======================================================================
984 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
985 {
986   //const char* aParameters = theParameters;
987 //   const char* aParameters = CORBA::string_dup(theParameters);
988   TCollection_AsciiString anInputParams;
989   SALOMEDS::Study_var aStudy = GetCurrentStudy();
990   if( !aStudy->_is_nil() ) {
991 //     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
992 //     for(int j=0;j<aSections->length();j++) {
993 //       SALOMEDS::ListOfStrings aVars= aSections[j];
994 //       for(int i=0;i<aVars.length();i++ ) {
995 //         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
996 //           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
997 //         if(i != aVars.length()-1)
998 //           anInputParams+=":";
999 //       }
1000 //       if(j!=aSections->length()-1)
1001 //         anInputParams+="|";
1002 //     }
1003     TCollection_AsciiString paramStr( theParameters );
1004     static TCollection_AsciiString separators(":|");
1005     int beg = 0, end;
1006     char sep, *pParams = (char*)paramStr.ToCString();
1007     while ( beg < paramStr.Length() )
1008     {
1009       end = beg-1;
1010       while ( ++end < paramStr.Length() )
1011         if ( pParams[end] == ':' || pParams[end] == '|')
1012           break;
1013       if ( end < paramStr.Length())
1014       {
1015         sep = pParams[end];
1016         pParams[end] = '\0';
1017       }
1018       if ( aStudy->IsVariable( pParams+beg ))
1019         anInputParams += pParams+beg;
1020       if ( end < paramStr.Length() )
1021         anInputParams += sep;
1022       else
1023         break;
1024       beg = end + 1;
1025     }
1026   }
1027   return CORBA::string_dup(anInputParams.ToCString());
1028 }
1029
1030 //=======================================================================
1031 //function : GetParameters
1032 //purpose  : 
1033 //=======================================================================
1034 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1035 {
1036   TCollection_AsciiString aResult;
1037
1038   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
1039   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
1040
1041   if(!aStudy->_is_nil() && 
1042      !CORBA::is_nil(theObject) && 
1043      !aSObj->_is_nil()){
1044     
1045     SALOMEDS::GenericAttribute_var anAttr;
1046     if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
1047       aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
1048     }
1049   }
1050   
1051   return CORBA::string_dup( aResult.ToCString() );
1052 }