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