Salome HOME
taking into account of the xml file for the component test of ther Corba
[modules/med.git] / src / MedMem / Support_i.cxx
1 //=============================================================================
2 // File      : Support_i.cxx
3 // Project   : SALOME
4 // Author    : EDF
5 // Copyright : EDF 2002
6 // $Header: /export/home/CVS/SALOME_ROOT/MED/src/MedMem/Support_i.cxx
7 //=============================================================================
8
9 #include "utilities.h"
10 #include "Utils_CorbaException.hxx"
11 #include "Utils_ORB_INIT.hxx"
12 #include "Utils_SINGLETON.hxx"
13
14 #include CORBA_SERVER_HEADER(MED)
15 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
16
17
18 #include "MEDMEM_define.hxx"
19 #include "MEDMEM_Support.hxx"
20
21 #include "Support_i.hxx"
22 #include "Mesh_i.hxx"
23 #include "convert.hxx"
24
25 // Initialisation des variables statiques
26 map < int, ::SUPPORT *> SUPPORT_i::supportMap ;
27 int SUPPORT_i::supportIndex = 0 ;
28
29
30 //=============================================================================
31 /*!
32  * Default constructor
33  */
34 //=============================================================================
35 SUPPORT_i::SUPPORT_i() :_support((::SUPPORT *)NULL)
36 {
37         BEGIN_OF("Default Constructor SUPPORT_i");
38         END_OF("Default Constructor SUPPORT_i");
39 }
40
41 //=============================================================================
42 /*!
43  * Constructor
44  */
45 //=============================================================================
46 SUPPORT_i::SUPPORT_i(const ::SUPPORT * const s) :_support(s),
47                    _corbaIndex(SUPPORT_i::supportIndex++)
48 {
49         BEGIN_OF("Constructor SUPPORT_i");
50         SUPPORT_i::supportMap[_corbaIndex]=(::SUPPORT *)_support;
51         END_OF("Constructor SUPPORT_i");
52 }
53 //=============================================================================
54 /*!
55  * Constructor
56  */
57 //=============================================================================
58 SUPPORT_i::SUPPORT_i(const SUPPORT_i &s) :_support(s._support),
59                             _corbaIndex(SUPPORT_i::supportIndex++)
60 {
61         BEGIN_OF("Constructor SUPPORT_i");
62         SUPPORT_i::supportMap[_corbaIndex]=(::SUPPORT *)_support;
63         END_OF("Constructor SUPPORT_i");
64 }
65 //=============================================================================
66 /*!
67  * Destructor
68  */
69 //=============================================================================
70
71 SUPPORT_i::~SUPPORT_i()
72 {
73 }
74 //=============================================================================
75 /*!
76  * CORBA: Accessor for Corba Index 
77  */
78 //=============================================================================
79
80 CORBA::Long SUPPORT_i::getCorbaIndex()
81 throw (SALOME::SALOME_Exception)
82 {
83         if (_support==NULL)
84                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
85                                              SALOME::INTERNAL_ERROR);
86         return _corbaIndex;
87 }
88
89 //=============================================================================
90 /*!
91  * CORBA: Accessor for Name 
92  */
93 //=============================================================================
94
95 char * SUPPORT_i::getName()     
96 throw (SALOME::SALOME_Exception)
97 {
98         if (_support==NULL)
99                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
100                                              SALOME::INTERNAL_ERROR);
101         try
102         {
103                 return CORBA::string_dup(_support->getName().c_str());
104         }
105         catch(...)
106         {
107                 MESSAGE("Unable to access the name of the support ");
108                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
109                                                 ,SALOME::INTERNAL_ERROR);
110         }
111
112 }
113
114 //=============================================================================
115 /*!
116  * CORBA: Accessor for Description 
117  */
118 //=============================================================================
119
120 char*  SUPPORT_i::getDescription()
121 throw (SALOME::SALOME_Exception)
122 {
123         if (_support==NULL)
124                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
125                                              SALOME::INTERNAL_ERROR);
126         try
127         {
128                 return CORBA::string_dup(_support->getDescription().c_str());
129         }
130         catch(...)
131         {
132                 MESSAGE("Unable to access the description of the support ");
133                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
134                                                 ,SALOME::INTERNAL_ERROR);
135         }
136 }
137
138 //=============================================================================
139 /*!
140  * CORBA: Accessor for Mesh 
141  */
142 //=============================================================================
143
144 SALOME_MED::MESH_ptr SUPPORT_i::getMesh()
145 throw (SALOME::SALOME_Exception)
146 {
147   BEGIN_OF("SALOME_MED::MESH_ptr SUPPORT_i::getMesh()");
148
149         if (_support==NULL)
150                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
151                                              SALOME::INTERNAL_ERROR);
152         try
153         {
154                 MESH * mesh = _support->getMesh();
155
156                 SCRUTE(mesh) ;
157
158                 MESH_i * m1 = new MESH_i(mesh);
159                 SALOME_MED::MESH_ptr m2 = m1->POA_SALOME_MED::MESH::_this();
160
161
162
163                 MESSAGE("SALOME_MED::MESH_ptr SUPPORT_i::getMesh() checking des pointeurs CORBA");
164
165                 SCRUTE(m1);
166                 SCRUTE(m2);
167
168                 m1->_remove_ref();
169
170                 END_OF("SALOME_MED::MESH_ptr SUPPORT_i::getMesh()");
171
172                 return (SALOME_MED::MESH::_duplicate(m2));
173         }
174         catch(...)
175         {
176                 MESSAGE("Unable to access the assoicated mesh");
177                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
178                                                 ,SALOME::INTERNAL_ERROR);
179         }
180 }
181
182 //=============================================================================
183 /*!
184  * CORBA: boolean indicating if support concerns all elements 
185  */
186 //=============================================================================
187
188 CORBA::Boolean SUPPORT_i::isOnAllElements()
189 throw (SALOME::SALOME_Exception)
190 {
191         if (_support==NULL)
192                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
193                                              SALOME::INTERNAL_ERROR);
194         try
195         {
196                 return _support->isOnAllElements();
197         }
198         catch(...)
199         {
200                 MESSAGE("Unable to access the type of the support");
201                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
202                                                 ,SALOME::INTERNAL_ERROR);
203         }
204 }
205
206 //=============================================================================
207 /*!
208  * CORBA: Accessor for type of support's entity 
209  */
210 //=============================================================================
211
212 SALOME_MED::medEntityMesh SUPPORT_i::getEntity() 
213 throw (SALOME::SALOME_Exception)
214 {
215   BEGIN_OF("SALOME_MED::medEntityMesh SUPPORT_i::getEntity()");
216
217   if (_support==NULL)
218     THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
219                                              SALOME::INTERNAL_ERROR);
220   try
221     {
222       END_OF("SALOME_MED::medEntityMesh SUPPORT_i::getEntity()");
223       return convertMedEntToIdlEnt(_support->getEntity());
224     }
225   catch(...)
226     {
227       MESSAGE("Unable to access support s entity");
228       THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object", \
229                                    SALOME::INTERNAL_ERROR);
230     }
231 }
232
233 //=============================================================================
234 /*!
235  * CORBA: Accessor for types of geometry elements 
236  */
237 //=============================================================================
238
239 SALOME_MED::medGeometryElement_array * SUPPORT_i::getTypes() 
240 throw (SALOME::SALOME_Exception)
241 {
242         if (_support==NULL)
243                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
244                                              SALOME::INTERNAL_ERROR);
245         SALOME_MED::medGeometryElement_array_var myseq = new SALOME_MED::medGeometryElement_array;
246         try
247         {
248                 int mySeqLength=_support->getNumberOfTypes();
249                 myseq->length(mySeqLength);
250                 const medGeometryElement * elemts = _support->getTypes();
251                 for (int i=0;i<mySeqLength;i++)
252                 {
253                         myseq[i]=convertMedEltToIdlElt(elemts[i]);
254                 }
255         }
256         catch(...)
257         {
258                 MESSAGE("Unable to access support different types");
259                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object", \
260                                               SALOME::INTERNAL_ERROR);
261         }
262         return myseq._retn();
263 }
264
265 //=============================================================================
266 /*!
267  * CORBA: Number of different types of geometry elements
268  *        existing in the support
269  */
270 //=============================================================================
271 CORBA::Long SUPPORT_i::getNumberOfElements(SALOME_MED::medGeometryElement geomElement) 
272 throw (SALOME::SALOME_Exception)
273 {
274
275   SCRUTE(geomElement);
276   SCRUTE(SALOME_MED::MED_ALL_ELEMENTS);
277
278         if (_support==NULL)
279                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
280                                              SALOME::INTERNAL_ERROR);
281         try
282         {
283                 return _support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
284         }
285         catch(...)
286         {
287                 MESSAGE("Unable to access the number of support different types");
288                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
289                                                 ,SALOME::INTERNAL_ERROR);
290         }
291
292 }
293
294 //=============================================================================
295 /*!
296  * CORBA: get Nodes 
297  */
298 //=============================================================================
299
300 Engines::long_array *  SUPPORT_i::getNumber(SALOME_MED::medGeometryElement geomElement) 
301 throw (SALOME::SALOME_Exception)
302 {
303   SCRUTE(_support);
304   SCRUTE(geomElement);
305   SCRUTE(convertIdlEltToMedElt(geomElement));
306
307         if (_support==NULL)
308                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
309                                              SALOME::INTERNAL_ERROR);
310         Engines::long_array_var myseq= new Engines::long_array;
311         try
312         {
313                 int nbelements=_support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
314                 myseq->length(nbelements);
315 SCRUTE(_support->getName());
316 SCRUTE(nbelements);
317 SCRUTE(convertIdlEltToMedElt(geomElement));
318                 const int * numbers=_support->getNumber(convertIdlEltToMedElt(geomElement));
319                 for (int i=0;i<nbelements;i++)
320                 {
321                         myseq[i]=numbers[i];
322 SCRUTE(numbers[i]);
323                 }
324         }
325         catch(...)
326         {
327                 MESSAGE("Unable to access the support optionnal index");
328                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
329                                                 ,SALOME::INTERNAL_ERROR);
330         }
331         return myseq._retn();
332         
333 }
334 //=============================================================================
335 /*!
336  * CORBA: Global Nodes Index (optionnaly designed by the user)
337  * CORBA:  ??????????????????????????????
338  */
339 //=============================================================================
340
341 Engines::long_array *  SUPPORT_i::getNumberIndex()
342 throw (SALOME::SALOME_Exception)
343 {
344         if (_support==NULL)
345                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
346                                              SALOME::INTERNAL_ERROR);
347         Engines::long_array_var myseq= new Engines::long_array;
348         try
349         {
350                 MESSAGE ("Nombre d'elements  mis de façon stupide a MED_ALL_ELEMENTS");
351                 int nbelements=_support->getNumberOfElements(::MED_ALL_ELEMENTS);
352                 myseq->length(nbelements);
353                 const int * numbers=_support->getNumberIndex();
354                 for (int i=0;i<nbelements;i++)
355                 {
356                         myseq[i]=numbers[i];
357                 }
358         }
359         catch(...)
360         {
361                 MESSAGE("Unable to access the support index");
362                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
363                                                 ,SALOME::INTERNAL_ERROR);
364         }
365         return myseq._retn();
366         
367 }
368
369 //=============================================================================
370 /*!
371  * CORBA: Array containing indexes for elements included in the support  
372  */
373 //=============================================================================
374
375 CORBA::Long SUPPORT_i::getNumberOfGaussPoints(SALOME_MED::medGeometryElement geomElement)
376 throw (SALOME::SALOME_Exception)
377 {
378         if (_support==NULL)
379                 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
380                                              SALOME::INTERNAL_ERROR);
381         try
382         {
383                 return _support->getNumberOfGaussPoint(convertIdlEltToMedElt(geomElement));
384         }
385         catch(...)
386         {
387                 MESSAGE("Unable to access number of Gauss points");
388                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
389                                                 ,SALOME::INTERNAL_ERROR);
390         }
391 }
392
393 //=============================================================================
394 /*!
395  * CORBA: add the Support in the StudyManager 
396  */
397 //=============================================================================
398 void SUPPORT_i::addInStudy (SALOMEDS::Study_ptr myStudy, SALOME_MED::SUPPORT_ptr myIor)
399   throw (SALOME::SALOME_Exception)
400 {
401   BEGIN_OF("SUPPORT_i::addInStudy");
402
403   if ( _supportId != "" )
404   {
405       MESSAGE("Support already in Study");
406       THROW_SALOME_CORBA_EXCEPTION("Support already in Study", \
407                                    SALOME::BAD_PARAM);
408   };
409   
410   SALOMEDS::StudyBuilder_var     myBuilder = myStudy->NewBuilder();
411   SALOMEDS::GenericAttribute_var anAttr;
412   SALOMEDS::AttributeName_var    aName;
413   SALOMEDS::AttributeIOR_var     aIOR;
414   
415   // Find SComponent labelled 'Med'
416   MESSAGE("Find SComponent labelled 'Med'");
417   //  SALOMEDS::SComponent_var medfather = myStudy->FindComponent("Med");
418   SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
419   if ( CORBA::is_nil(medfather) ) 
420     THROW_SALOME_CORBA_EXCEPTION("SComponent labelled 'Med' not Found",SALOME::INTERNAL_ERROR);
421   
422   if ( CORBA::is_nil(myStudy) ) 
423     THROW_SALOME_CORBA_EXCEPTION("Study deleted !!!",SALOME::INTERNAL_ERROR);
424
425   // Find SObject MESH (represent mesh in support)
426   SALOMEDS::SObject_var medmeshfather = myStudy->FindObject("MEDMESH");
427   if ( CORBA::is_nil(medmeshfather) )
428   { 
429     cout << "On a leve l exception" << flush;
430     THROW_SALOME_CORBA_EXCEPTION("SObject labelled 'MEDMESH' not Found",SALOME::INTERNAL_ERROR);
431   }
432   cout << "Find SObject MESH (represent mesh in support)"<< flush;
433
434   string meshName = getMesh()->getName() ;
435
436                 // seulement sous Med : il peut y avoir le meme sous SMESH !!!
437   SALOMEDS::SObject_var medsupportfather = myStudy->FindObject(meshName.c_str()); 
438   if ( CORBA::is_nil(medsupportfather) ) 
439     THROW_SALOME_CORBA_EXCEPTION("SObject Mesh in Support not Found",SALOME::INTERNAL_ERROR);
440   // perhaps add MESH automatically ?
441   
442   MESSAGE("Add a support Object under MED/MESH/MESHNAME");
443   SALOMEDS::SObject_var medsupfather = myStudy->FindObject("MEDSUPPORT");
444   if ( CORBA::is_nil(medsupfather) )
445   {
446                 MESSAGE("Add Object MEDSUPPORT");
447                 medsupfather = myBuilder->NewObject(medmeshfather);
448                 anAttr = myBuilder->FindOrCreateAttribute(medsupfather, "AttributeName");
449                 aName = SALOMEDS::AttributeName::_narrow(anAttr);
450                 aName->SetValue("MEDSUPPORT");
451
452   } ;
453
454
455   //myBuilder->NewCommand();
456   SALOMEDS::SObject_var supportEntry = myStudy->FindObject(_support->getName().c_str());
457                          // c'est pas bon, car il faut rechercher uniquement sous le bon MESH !!!
458   if ( CORBA::is_nil(supportEntry) ) 
459   { 
460     // not already in study : we create it !
461     SALOMEDS::SObject_var newObj = myBuilder->NewObject(medsupfather);
462     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
463     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
464     CORBA::ORB_var &orb = init(0,0);
465     string iorStr = orb->object_to_string(myIor);
466     anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
467     aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
468     aIOR->SetValue(iorStr.c_str());
469     anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeName");
470     aName = SALOMEDS::AttributeName::_narrow(anAttr);
471     aName->SetValue(_support->getName().c_str());
472     _supportId = newObj->GetID();
473   } 
474   else 
475   {
476     // already in study : put new AttributeIOR !
477     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
478     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
479     CORBA::ORB_var &orb = init(0,0);
480     string iorStr = orb->object_to_string(myIor);
481     anAttr = myBuilder->FindOrCreateAttribute(supportEntry, "AttributeIOR");
482     aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
483     aIOR->SetValue(iorStr.c_str());
484   }
485     myBuilder->CommitCommand();
486   
487   END_OF("SUPPORT_i::addInStudy");
488 }