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