1 //=============================================================================
2 // File : MEDMEM_Support_i.cxx
5 // Copyright : EDF 2002
6 // $Header: /export/home/PAL/MED_SRC/src/MEDMEM_I/MEDMEM_Support_i.cxx
7 //=============================================================================
10 #include "Utils_CorbaException.hxx"
11 #include "Utils_ORB_INIT.hxx"
12 #include "Utils_SINGLETON.hxx"
14 #include CORBA_SERVER_HEADER(MED)
15 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
18 #include "MEDMEM_define.hxx"
19 #include "MEDMEM_Support.hxx"
21 #include "MEDMEM_Support_i.hxx"
22 #include "MEDMEM_Mesh_i.hxx"
23 #include "MEDMEM_convert.hxx"
25 #include "SenderFactory.hxx"
26 #include "MultiCommException.hxx"
27 using namespace MEDMEM;
29 // Initialisation des variables statiques
30 map < int, ::SUPPORT *> SUPPORT_i::supportMap ;
31 int SUPPORT_i::supportIndex = 0 ;
34 //=============================================================================
38 //=============================================================================
39 SUPPORT_i::SUPPORT_i() :_support((::SUPPORT *)NULL),_corbaIndex(SUPPORT_i::supportIndex++)
41 BEGIN_OF("Default Constructor SUPPORT_i");
42 END_OF("Default Constructor SUPPORT_i");
45 //=============================================================================
49 //=============================================================================
50 SUPPORT_i::SUPPORT_i(const ::SUPPORT * const s) :_support(s),
51 _corbaIndex(SUPPORT_i::supportIndex++)
53 BEGIN_OF("Constructor SUPPORT_i");
54 SUPPORT_i::supportMap[_corbaIndex]=(::SUPPORT *)_support;
55 END_OF("Constructor SUPPORT_i");
57 //=============================================================================
61 //=============================================================================
62 SUPPORT_i::SUPPORT_i(const SUPPORT_i &s) :_support(s._support),
63 _corbaIndex(SUPPORT_i::supportIndex++)
65 BEGIN_OF("Constructor SUPPORT_i");
66 SUPPORT_i::supportMap[_corbaIndex]=(::SUPPORT *)_support;
67 END_OF("Constructor SUPPORT_i");
69 //=============================================================================
73 //=============================================================================
75 SUPPORT_i::~SUPPORT_i()
78 //=============================================================================
80 * CORBA: Accessor for Corba Index
82 //=============================================================================
84 CORBA::Long SUPPORT_i::getCorbaIndex()
85 throw (SALOME::SALOME_Exception)
88 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
89 SALOME::INTERNAL_ERROR);
93 //=============================================================================
95 * CORBA: Accessor for Name
97 //=============================================================================
99 char * SUPPORT_i::getName()
100 throw (SALOME::SALOME_Exception)
103 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
104 SALOME::INTERNAL_ERROR);
107 return CORBA::string_dup(_support->getName().c_str());
109 catch (MEDEXCEPTION &ex)
111 MESSAGE("Unable to access the name of the support ");
112 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
117 //=============================================================================
119 * CORBA: Accessor for Description
121 //=============================================================================
123 char* SUPPORT_i::getDescription()
124 throw (SALOME::SALOME_Exception)
127 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
128 SALOME::INTERNAL_ERROR);
131 return CORBA::string_dup(_support->getDescription().c_str());
133 catch (MEDEXCEPTION &ex)
135 MESSAGE("Unable to access the description of the support ");
136 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
140 //=============================================================================
142 * CORBA: Accessor for Description and Name
144 //=============================================================================
146 SALOME_MED::SUPPORT::supportInfos * SUPPORT_i::getSupportGlobal()
147 throw (SALOME::SALOME_Exception)
150 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
151 SALOME::INTERNAL_ERROR);
152 SALOME_MED::SUPPORT::supportInfos_var all = new SALOME_MED::SUPPORT::supportInfos;
155 all->name = CORBA::string_dup(_support->getName().c_str());
156 all->description = CORBA::string_dup(_support->getDescription().c_str());
157 const int numberOfTypes = _support->getNumberOfTypes();
158 all->numberOfGeometricType = numberOfTypes;
159 all->entity = _support->getEntity();
161 all->types.length(numberOfTypes);
162 all->nbEltTypes.length(numberOfTypes);
163 const medGeometryElement * elemts = _support->getTypes();
164 for (int i=0;i<numberOfTypes;i++)
166 all->types[i] = convertMedEltToIdlElt(elemts[i]);
167 all->nbEltTypes[i] = _support->getNumberOfElements(elemts[i]);
170 catch (MEDEXCEPTION &ex)
172 MESSAGE("Unable to access the description of the support ");
173 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
180 //=============================================================================
182 * CORBA: Accessor for Mesh
184 //=============================================================================
186 SALOME_MED::MESH_ptr SUPPORT_i::getMesh()
187 throw (SALOME::SALOME_Exception)
189 BEGIN_OF("SALOME_MED::MESH_ptr SUPPORT_i::getMesh()");
192 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
193 SALOME::INTERNAL_ERROR);
196 MESH * mesh = _support->getMesh();
200 MESH_i * m1 = new MESH_i(mesh);
201 SALOME_MED::MESH_ptr m2 = m1->POA_SALOME_MED::MESH::_this();
202 MESSAGE("SALOME_MED::MESH_ptr SUPPORT_i::getMesh() checking des pointeurs CORBA");
207 END_OF("SALOME_MED::MESH_ptr SUPPORT_i::getMesh()");
211 catch (MEDEXCEPTION &ex)
213 MESSAGE("Unable to access the assoicated mesh");
214 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
218 //=============================================================================
220 * CORBA: boolean indicating if support concerns all elements
222 //=============================================================================
224 CORBA::Boolean SUPPORT_i::isOnAllElements()
225 throw (SALOME::SALOME_Exception)
228 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
229 SALOME::INTERNAL_ERROR);
232 return _support->isOnAllElements();
234 catch (MEDEXCEPTION &ex)
236 MESSAGE("Unable to access the type of the support");
237 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
241 //=============================================================================
245 //=============================================================================
246 CORBA::Long SUPPORT_i::getNumberOfTypes()
247 throw (SALOME::SALOME_Exception)
250 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
251 SALOME::INTERNAL_ERROR);
254 return _support->getNumberOfTypes();
256 catch (MEDEXCEPTION &ex)
258 MESSAGE("Unable to access number of support different types");
259 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
265 //=============================================================================
267 * CORBA: Accessor for type of support's entity
269 //=============================================================================
271 SALOME_MED::medEntityMesh SUPPORT_i::getEntity()
272 throw (SALOME::SALOME_Exception)
274 BEGIN_OF("SALOME_MED::medEntityMesh SUPPORT_i::getEntity()");
277 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
278 SALOME::INTERNAL_ERROR);
281 END_OF("SALOME_MED::medEntityMesh SUPPORT_i::getEntity()");
282 return convertMedEntToIdlEnt(_support->getEntity());
284 catch (MEDEXCEPTION &ex)
286 MESSAGE("Unable to access support s entity");
287 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
291 //=============================================================================
293 * CORBA: Accessor for types of geometry elements
295 //=============================================================================
297 SALOME_MED::medGeometryElement_array * SUPPORT_i::getTypes()
298 throw (SALOME::SALOME_Exception)
301 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
302 SALOME::INTERNAL_ERROR);
303 SALOME_MED::medGeometryElement_array_var myseq = new SALOME_MED::medGeometryElement_array;
306 int mySeqLength=_support->getNumberOfTypes();
307 myseq->length(mySeqLength);
308 const medGeometryElement * elemts = _support->getTypes();
309 for (int i=0;i<mySeqLength;i++)
311 myseq[i]=convertMedEltToIdlElt(elemts[i]);
314 catch (MEDEXCEPTION &ex)
316 MESSAGE("Unable to access support different types");
317 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
319 return myseq._retn();
322 //=============================================================================
324 * CORBA: Number of different types of geometry elements
325 * existing in the support
327 //=============================================================================
328 CORBA::Long SUPPORT_i::getNumberOfElements(SALOME_MED::medGeometryElement geomElement)
329 throw (SALOME::SALOME_Exception)
333 SCRUTE(SALOME_MED::MED_ALL_ELEMENTS);
336 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
337 SALOME::INTERNAL_ERROR);
340 return _support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
342 catch (MEDEXCEPTION &ex)
344 MESSAGE("Unable to access the number of support different types");
345 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
350 //=============================================================================
354 //=============================================================================
356 SALOME_MED::long_array * SUPPORT_i::getNumber(SALOME_MED::medGeometryElement geomElement)
357 throw (SALOME::SALOME_Exception)
361 SCRUTE(convertIdlEltToMedElt(geomElement));
364 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
365 SALOME::INTERNAL_ERROR);
366 SALOME_MED::long_array_var myseq= new SALOME_MED::long_array;
369 int nbelements=_support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
370 myseq->length(nbelements);
371 SCRUTE(_support->getName());
373 SCRUTE(convertIdlEltToMedElt(geomElement));
374 const int * numbers=_support->getNumber(convertIdlEltToMedElt(geomElement));
375 for (int i=0;i<nbelements;i++)
381 catch (MEDEXCEPTION &ex)
383 MESSAGE("Unable to access the support optionnal index");
384 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
386 return myseq._retn();
390 //=============================================================================
392 * CORBA: 2nd get Nodes
394 //=============================================================================
395 SALOME::SenderInt_ptr SUPPORT_i::getSenderForNumber(SALOME_MED::medGeometryElement geomElement)
396 throw (SALOME::SALOME_Exception)
400 SCRUTE(convertIdlEltToMedElt(geomElement));
402 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
403 SALOME::INTERNAL_ERROR);
404 SALOME::SenderInt_ptr ret;
407 int nbelements=_support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
408 const int * numbers=_support->getNumber(convertIdlEltToMedElt(geomElement));
409 ret=SenderFactory::buildSender(*this,numbers,nbelements);
411 catch (MEDEXCEPTION &ex)
413 MESSAGE("Unable to access the support optionnal index");
414 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
416 catch(MultiCommException &ex2)
417 THROW_SALOME_CORBA_EXCEPTION(ex2.what(),SALOME::INTERNAL_ERROR);
420 //=============================================================================
422 * CORBA: Global Nodes Index (optionnaly designed by the user)
423 * CORBA: ??????????????????????????????
425 //=============================================================================
427 SALOME_MED::long_array * SUPPORT_i::getNumberIndex()
428 throw (SALOME::SALOME_Exception)
431 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
432 SALOME::INTERNAL_ERROR);
433 SALOME_MED::long_array_var myseq= new SALOME_MED::long_array;
436 MESSAGE ("Nombre d'elements mis de façon stupide a MED_ALL_ELEMENTS");
437 int nbelements=_support->getNumberOfElements(::MED_ALL_ELEMENTS);
438 myseq->length(nbelements);
439 const int * numbers=_support->getNumberIndex();
440 for (int i=0;i<nbelements;i++)
445 catch (MEDEXCEPTION &ex)
447 MESSAGE("Unable to access the support index");
448 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
450 return myseq._retn();
453 //=============================================================================
455 * CORBA: 2nd Global Nodes Index (optionnaly designed by the user)
457 //=============================================================================
459 SALOME::SenderInt_ptr SUPPORT_i::getSenderForNumberIndex()
460 throw (SALOME::SALOME_Exception)
463 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
464 SALOME::INTERNAL_ERROR);
465 SALOME::SenderInt_ptr ret;
468 MESSAGE ("Nombre d'elements mis de façon stupide a MED_ALL_ELEMENTS");
469 int nbelements=_support->getNumberOfElements(::MED_ALL_ELEMENTS);
470 const int * numbers=_support->getNumberIndex();
471 ret=SenderFactory::buildSender(*this,numbers,nbelements);
473 catch (MEDEXCEPTION &ex)
475 MESSAGE("Unable to access the support index");
476 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
478 catch(MultiCommException &ex2)
479 THROW_SALOME_CORBA_EXCEPTION(ex2.what(),SALOME::INTERNAL_ERROR);
482 //=============================================================================
486 //=============================================================================
488 void SUPPORT_i::getBoundaryElements()
489 throw (SALOME::SALOME_Exception)
492 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
493 SALOME::INTERNAL_ERROR);
496 (const_cast< ::SUPPORT *>(_support))->getBoundaryElements();
498 catch (MEDEXCEPTION &ex)
500 MESSAGE("Unable to access elements");
501 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object"\
502 ,SALOME::INTERNAL_ERROR);
507 //=============================================================================
509 * CORBA: Array containing indexes for elements included in the support
511 //=============================================================================
513 CORBA::Long SUPPORT_i::getNumberOfGaussPoint(SALOME_MED::medGeometryElement geomElement)
514 throw (SALOME::SALOME_Exception)
517 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
518 SALOME::INTERNAL_ERROR);
521 return _support->getNumberOfGaussPoint(convertIdlEltToMedElt(geomElement));
523 catch (MEDEXCEPTION &ex)
525 MESSAGE("Unable to access number of Gauss points");
526 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
529 //=============================================================================
531 * CORBA: Global Nodes Index (optionnaly designed by the user)
533 //=============================================================================
534 SALOME_MED::long_array * SUPPORT_i::getNumbersOfGaussPoint()
535 throw (SALOME::SALOME_Exception)
538 THROW_SALOME_CORBA_EXCEPTION("No associated Support", \
539 SALOME::INTERNAL_ERROR);
540 SALOME_MED::long_array_var myseq= new SALOME_MED::long_array;
543 int mySeqLength=_support->getNumberOfTypes();
544 myseq->length(mySeqLength);
545 const medGeometryElement * elemts = _support->getTypes();
546 for (int i=0;i<mySeqLength;i++)
548 myseq[i]= _support->getNumberOfGaussPoint(elemts[i]);
551 catch (MEDEXCEPTION &ex)
553 MESSAGE("Unable to access number of Gauss points");
554 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
557 //=============================================================================
559 * CORBA: add the Support in the StudyManager
561 //=============================================================================
562 void SUPPORT_i::addInStudy (SALOMEDS::Study_ptr myStudy, SALOME_MED::SUPPORT_ptr myIor)
563 throw (SALOME::SALOME_Exception,SALOMEDS::StudyBuilder::LockProtection)
565 const char * LOC = "SUPPORT_i::addInStudy";
568 if ( _supportId != "" )
570 MESSAGE(LOC << "Support already in Study");
571 THROW_SALOME_CORBA_EXCEPTION("Support already in Study", \
575 if ( CORBA::is_nil(myStudy) )
577 MESSAGE(LOC << "Study not found");
578 THROW_SALOME_CORBA_EXCEPTION("Study deleted !!!",
579 SALOME::INTERNAL_ERROR);
583 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
584 SALOMEDS::GenericAttribute_var anAttr;
585 SALOMEDS::AttributeName_var aName;
586 SALOMEDS::AttributeIOR_var aIOR;
588 // Find SComponent labelled 'Med'
589 MESSAGE(LOC << " Find SComponent labelled 'MED'");
590 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
591 if ( CORBA::is_nil(medfather) )
593 MESSAGE(LOC << "MED not found");
594 THROW_SALOME_CORBA_EXCEPTION("SComponent labelled 'Med' not Found",SALOME::INTERNAL_ERROR);
597 // Find SObject MESH (represent mesh in support)
598 SALOMEDS::SObject_var medmeshfather = myStudy->FindObjectByPath("/Med/MEDMESH");
599 if ( CORBA::is_nil(medmeshfather) )
601 MESSAGE(LOC << " No /Med/MEDMESH Found in study")
602 THROW_SALOME_CORBA_EXCEPTION("SObject labelled 'MEDMESH' not Found",SALOME::INTERNAL_ERROR);
604 MESSAGE(LOC << " Find SObject MESH (represent mesh in support)");
606 string meshName = getMesh()->getName() ;
607 string meshNameStudy = meshName;
609 for (string::size_type pos=0; pos<meshNameStudy.size();++pos)
611 if (isspace(meshNameStudy[pos])) meshNameStudy[pos] = '_';
614 // seulement sous Med : il peut y avoir le meme sous SMESH !!!
615 SALOMEDS::SObject_var medsupportfather = myStudy->FindObject(meshName.c_str());
616 if ( CORBA::is_nil(medsupportfather) )
617 THROW_SALOME_CORBA_EXCEPTION("SObject Mesh in Support not Found",SALOME::INTERNAL_ERROR);
618 // perhaps add MESH automatically ?
620 MESSAGE("Add a support Object under /MED/MESH/MESHNAME");
622 char * medsupfatherName;
623 int lenName = 15 + strlen(meshName.c_str()) + 1;
624 medsupfatherName = new char[lenName];
625 medsupfatherName = strcpy(medsupfatherName,"MEDSUPPORTS_OF_");
626 medsupfatherName = strcat(medsupfatherName,meshNameStudy.c_str());
628 SCRUTE(medsupfatherName);
630 SALOMEDS::SObject_var medsupfather = myStudy->FindObject(medsupfatherName);
631 if ( CORBA::is_nil(medsupfather) )
633 MESSAGE("Add Object MEDSUPPORT");
634 medsupfather = myBuilder->NewObject(medmeshfather);
635 anAttr = myBuilder->FindOrCreateAttribute(medsupfather, "AttributeName");
636 aName = SALOMEDS::AttributeName::_narrow(anAttr);
637 aName->SetValue(medsupfatherName);
641 //myBuilder->NewCommand();
643 string supportName = _support->getName();
647 SCRUTE(meshNameStudy);
649 char * supportEntryPath;
650 lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str())+1;
651 supportEntryPath = new char[lenName];
652 supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
653 supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
654 supportEntryPath = strcat(supportEntryPath,meshNameStudy.c_str());
655 supportEntryPath = strcat(supportEntryPath,"/");
656 supportEntryPath = strcat(supportEntryPath,supportName.c_str());
658 //SCRUTE(supportEntryPath);
660 MESSAGE("supportEntryPath in support " << supportEntryPath << " length " << lenName);
662 // SALOMEDS::SObject_var supportEntry = myStudy->FindObject(_support->getName().c_str());
663 // c'est pas bon, car il faut rechercher uniquement sous le bon MESH !!!
664 SALOMEDS::SObject_var supportEntry = myStudy->FindObjectByPath(supportEntryPath);
666 if ( CORBA::is_nil(supportEntry) )
668 MESSAGE(" supportEntry is a nil corba object and is going to be created");
669 // not already in study : we create it !
670 SALOMEDS::SObject_var newObj = myBuilder->NewObject(medsupfather);
671 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
672 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
673 CORBA::ORB_var &orb = init(0,0);
674 string iorStr = orb->object_to_string(myIor);
675 anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
676 aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
677 aIOR->SetValue(iorStr.c_str());
678 anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeName");
679 aName = SALOMEDS::AttributeName::_narrow(anAttr);
680 aName->SetValue(_support->getName().c_str());
681 _supportId = newObj->GetID();
685 // already in study : put new AttributeIOR !
686 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
687 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
688 CORBA::ORB_var &orb = init(0,0);
689 string iorStr = orb->object_to_string(myIor);
690 anAttr = myBuilder->FindOrCreateAttribute(supportEntry, "AttributeIOR");
691 aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
692 aIOR->SetValue(iorStr.c_str());
694 myBuilder->CommitCommand();
696 SALOMEDS::SObject_var supportEntryBis = myStudy->FindObjectByPath(supportEntryPath);
698 MESSAGE("Just for checking, reuse of the corba pointer");
700 if ( CORBA::is_nil(supportEntry) )
702 MESSAGE("The reuse is OK");
706 MESSAGE("well !! the reuse is not OK and there was a problem in the storage in the study");
709 delete [] medsupfatherName;
710 delete [] supportEntryPath;
712 // register the Corba pointer: increase the referrence count
713 MESSAGE("Registering of the Corba Support pointer");