Salome HOME
0021014: EDF 1583 SMESH: Improvement of the Python Dump for the creation of groups
[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     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, "ICON_SMESH_TREE_MESH_WARN");
636     if ( aSubMeshSO->_is_nil() )
637       return aSubMeshSO._retn();
638   }
639   SetName( aSubMeshSO, theName, "SubMesh" );
640
641   // Add reference to theShapeObject
642
643   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
644
645   // Publish hypothesis
646
647   SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
648   if ( hypList )
649     for ( int i = 0; i < hypList->length(); i++ ) {
650       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
651       PublishHypothesis( theStudy, aHyp );
652       AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
653     }
654
655   return aSubMeshSO._retn();
656 }
657
658 //=======================================================================
659 //function : PublishGroup
660 //purpose  : 
661 //=======================================================================
662
663 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
664                                                  SMESH::SMESH_Mesh_ptr  theMesh,
665                                                  SMESH::SMESH_GroupBase_ptr theGroup,
666                                                  GEOM::GEOM_Object_ptr  theShapeObject,
667                                                  const char*            theName)
668 {
669   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
670     return SALOMEDS::SObject::_nil();
671
672   SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
673   if ( aGroupSO->_is_nil() )
674   {
675     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
676     if ( aMeshSO->_is_nil() ) {
677       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
678       if ( aMeshSO->_is_nil())
679         return SALOMEDS::SObject::_nil();
680     }
681     int aType = (int)theGroup->GetType();
682     const char* aRootNames[] = {
683       "Compound Groups", "Groups of Nodes", "Groups of Edges",
684       "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements" };
685
686     // Currently, groups with heterogenous content are not supported
687     if ( aType != SMESH::ALL ) {
688       long aRootTag = GetNodeGroupsTag() + aType - 1;
689
690       // Find or create groups root
691       SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
692                                                aMeshSO, aRootTag, 0, false );
693       if ( aType < 6 )
694         SetName( aRootSO, aRootNames[aType] );
695
696       // Add new group to corresponding sub-tree
697       SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
698       const int isEmpty = ( elemTypes->length() == 0 );
699       std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
700       if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
701         pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
702       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
703     }
704     if ( aGroupSO->_is_nil() )
705       return aGroupSO._retn();
706   }
707
708   SetName( aGroupSO, theName, "Group" );
709
710   //Add reference to geometry
711   if ( !theShapeObject->_is_nil() )
712     addReference( theStudy, aGroupSO, theShapeObject, 1 );
713
714   return aGroupSO._retn();
715 }
716
717 //=======================================================================
718 //function : PublishHypothesis
719 //purpose  : 
720 //=======================================================================
721
722 SALOMEDS::SObject_ptr
723   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
724                                   SMESH::SMESH_Hypothesis_ptr theHyp,
725                                   const char*                 theName)
726 {
727   if(MYDEBUG) MESSAGE("PublishHypothesis")
728   if (theStudy->_is_nil() || theHyp->_is_nil())
729     return SALOMEDS::SObject::_nil();
730
731   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
732   if ( aHypSO->_is_nil() )
733   {
734     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
735     if ( father->_is_nil() )
736       return aHypSO._retn();
737
738     //Find or Create Hypothesis root
739     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
740     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
741     SALOMEDS::SObject_var aRootSO =
742       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
743                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
744     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
745
746     // Add New Hypothesis
747     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
748     aPmName += theHyp->GetName();
749     // prepend plugin name to pixmap name
750     string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
751     if ( pluginName != "StdMeshers" )
752       aPmName = pluginName + "::" + aPmName;
753     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
754   }
755
756   if ( !aHypSO->_is_nil() ) {
757     CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
758     SetName( aHypSO, theName, aHypName );
759   }
760
761   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
762   return aHypSO._retn();
763 }
764
765 //=======================================================================
766 //function : GetMeshOrSubmeshByShape
767 //purpose  : 
768 //=======================================================================
769
770 SALOMEDS::SObject_ptr
771   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
772                                         SMESH::SMESH_Mesh_ptr theMesh,
773                                         GEOM::GEOM_Object_ptr theShape)
774 {
775   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
776   SALOMEDS::SObject_var aMeshOrSubMesh;
777   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
778     return aMeshOrSubMesh._retn();
779   
780   TopoDS_Shape aShape;
781   if(theMesh->HasShapeToMesh())
782     aShape = GeomObjectToShape( theShape );
783   else
784     aShape = SMESH_Mesh::PseudoShape();
785
786   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
787
788   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
789     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
790     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
791       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
792     else {
793       int shapeID = meshDS->ShapeToIndex( aShape );
794       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
795       if ( !aSubMesh->_is_nil() )
796         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
797     }
798   }
799   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
800   return aMeshOrSubMesh._retn();
801 }
802
803 //=======================================================================
804 //function : AddHypothesisToShape
805 //purpose  : 
806 //=======================================================================
807
808 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
809                                        SMESH::SMESH_Mesh_ptr       theMesh,
810                                        GEOM::GEOM_Object_ptr       theShape,
811                                        SMESH::SMESH_Hypothesis_ptr theHyp)
812 {
813   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
814   if (theStudy->_is_nil() || theMesh->_is_nil() ||
815       theHyp->_is_nil() || (theShape->_is_nil()
816                             && theMesh->HasShapeToMesh()) )
817     return false;
818
819   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
820   if ( aMeshSO->_is_nil() )
821     aMeshSO = PublishMesh( theStudy, theMesh );
822   SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
823   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
824     return false;
825
826   // Find a mesh or submesh refering to theShape
827   SALOMEDS::SObject_var aMeshOrSubMesh =
828     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
829   if ( aMeshOrSubMesh->_is_nil() )
830   {
831     // publish submesh
832     TopoDS_Shape aShape = GeomObjectToShape( theShape );
833     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
834     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
835       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
836       int shapeID = meshDS->ShapeToIndex( aShape );
837       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
838       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
839     }
840     if ( aMeshOrSubMesh->_is_nil() )
841       return false;
842   }
843
844   //Find or Create Applied Hypothesis root
845   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
846   SALOMEDS::SObject_var AHR =
847     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
848              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
849              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
850   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
851   if ( AHR->_is_nil() )
852     return false;
853
854   addReference( theStudy, AHR, theHyp );
855   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
856   return true;
857 }
858
859 //=======================================================================
860 //function : RemoveHypothesisFromShape
861 //purpose  : 
862 //=======================================================================
863
864 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
865                                             SMESH::SMESH_Mesh_ptr       theMesh,
866                                             GEOM::GEOM_Object_ptr       theShape,
867                                             SMESH::SMESH_Hypothesis_ptr theHyp)
868 {
869   if (theStudy->_is_nil() || theMesh->_is_nil() ||
870       theHyp->_is_nil() || (theShape->_is_nil()
871                             && theMesh->HasShapeToMesh()))
872     return false;
873
874   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
875   if ( aHypSO->_is_nil() )
876     return false;
877
878   // Find a mesh or submesh refering to theShape
879   SALOMEDS::SObject_var aMeshOrSubMesh =
880     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
881   if ( aMeshOrSubMesh->_is_nil() )
882     return false;
883
884   // Find and remove a reference to aHypSO
885   SALOMEDS::SObject_var aRef, anObj;
886   CORBA::String_var     anID = CORBA::string_dup( aHypSO->GetID() );
887   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
888   for ( it->InitEx( true ); it->More(); it->Next() ) {
889     anObj = it->Value();
890     if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
891       theStudy->NewBuilder()->RemoveObject( anObj );
892       break;
893     }
894   }
895   return true;
896 }
897
898 //=======================================================================
899 //function : UpdateParameters
900 //purpose  : 
901 //=======================================================================
902 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
903 {
904
905   if(VARIABLE_DEBUG)
906     cout<<"UpdateParameters : "<<theParameters<<endl;
907   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
908   if(aStudy->_is_nil() || CORBA::is_nil(theObject)) 
909     return;
910
911   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
912   if(aSObj->_is_nil())  
913     return;
914
915   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
916
917   SALOMEDS::GenericAttribute_var aFindAttr;
918   bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
919   if(VARIABLE_DEBUG)
920     cout<<"Find Attribute "<<hasAttr<<endl;
921
922   SALOMEDS::GenericAttribute_var anAttr;
923   anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
924   SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
925
926   CORBA::String_var oldparVar = aStringAttr->Value();
927   CORBA::String_var inpparVar = ParseParameters(theParameters);
928   TCollection_AsciiString aNewParams;
929   TCollection_AsciiString aOldParameters(oldparVar.inout());
930   TCollection_AsciiString anInputParams(inpparVar.inout());
931   if(!hasAttr)
932     aNewParams = anInputParams;
933   else 
934     {
935       int pos = aOldParameters.SearchFromEnd("|");
936       if(pos==-1) pos = 0;
937       TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
938       TCollection_AsciiString previousParam(previousParamFull);
939       TCollection_AsciiString theRepet("1");
940       pos = previousParam.SearchFromEnd(";*=");
941       if(pos >= 0)
942         {
943           theRepet = previousParam.Split(pos+2);
944           pos = pos-1;
945           if(pos==-1) pos = 0;
946           previousParam.Split(pos);
947         }
948       if(previousParam == anInputParams)
949         {
950           theRepet = theRepet.IntegerValue()+1;
951           aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
952         }
953       else
954         {
955           aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
956         }
957     }
958
959   if(VARIABLE_DEBUG)
960   {
961     cout<<"Input Parameters : "<<anInputParams<<endl;
962     cout<<"Old Parameters : "<<aOldParameters<<endl;
963     cout<<"New Parameters : "<<aNewParams<<endl;
964   }
965
966   aStringAttr->SetValue( aNewParams.ToCString() );
967 }
968
969 //=======================================================================
970 //function : ParseParameters
971 //purpose  : 
972 //=======================================================================
973 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
974 {
975   //const char* aParameters = theParameters;
976 //   const char* aParameters = CORBA::string_dup(theParameters);
977   TCollection_AsciiString anInputParams;
978   SALOMEDS::Study_var aStudy = GetCurrentStudy();
979   if( !aStudy->_is_nil() ) {
980 //     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
981 //     for(int j=0;j<aSections->length();j++) {
982 //       SALOMEDS::ListOfStrings aVars= aSections[j];
983 //       for(int i=0;i<aVars.length();i++ ) {
984 //         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
985 //           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
986 //         if(i != aVars.length()-1)
987 //           anInputParams+=":";
988 //       }
989 //       if(j!=aSections->length()-1)
990 //         anInputParams+="|";
991 //     }
992     TCollection_AsciiString paramStr( theParameters );
993     static TCollection_AsciiString separators(":|");
994     int beg = 0, end;
995     char sep, *pParams = (char*)paramStr.ToCString();
996     while ( beg < paramStr.Length() )
997     {
998       end = beg-1;
999       while ( ++end < paramStr.Length() )
1000         if ( pParams[end] == ':' || pParams[end] == '|')
1001           break;
1002       if ( end < paramStr.Length())
1003       {
1004         sep = pParams[end];
1005         pParams[end] = '\0';
1006       }
1007       if ( aStudy->IsVariable( pParams+beg ))
1008         anInputParams += pParams+beg;
1009       if ( end < paramStr.Length() )
1010         anInputParams += sep;
1011       else
1012         break;
1013       beg = end + 1;
1014     }
1015   }
1016   return CORBA::string_dup(anInputParams.ToCString());
1017 }
1018
1019 //=======================================================================
1020 //function : GetParameters
1021 //purpose  : 
1022 //=======================================================================
1023 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1024 {
1025   TCollection_AsciiString aResult;
1026
1027   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
1028   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
1029
1030   if(!aStudy->_is_nil() && 
1031      !CORBA::is_nil(theObject) && 
1032      !aSObj->_is_nil()){
1033     
1034     SALOMEDS::GenericAttribute_var anAttr;
1035     if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
1036       aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
1037     }
1038   }
1039   
1040   return CORBA::string_dup( aResult.ToCString() );
1041 }