1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 //=============================================================================
24 // File : MEDMEM_Support_i.cxx
27 // $Header: /export/home/PAL/MED_SRC/src/MEDMEM_I/MEDMEM_Support_i.cxx
28 //=============================================================================
30 #include "utilities.h"
31 #include "Utils_CorbaException.hxx"
32 #include "Utils_ORB_INIT.hxx"
33 #include "Utils_SINGLETON.hxx"
35 #include CORBA_SERVER_HEADER(MED)
36 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
39 #include "MEDMEM_define.hxx"
40 #include "MEDMEM_Support.hxx"
41 #include "MEDMEM_Mesh.hxx"
43 #include "MEDMEM_Support_i.hxx"
44 #include "MEDMEM_Mesh_i.hxx"
45 #include "MEDMEM_convert.hxx"
47 #include "SenderFactory.hxx"
48 #include "MultiCommException.hxx"
49 using namespace MEDMEM;
51 // Initialisation des variables statiques
52 map < int, ::SUPPORT *> SUPPORT_i::supportMap ;
53 int SUPPORT_i::supportIndex = 0 ;
56 //=============================================================================
60 //=============================================================================
61 SUPPORT_i::SUPPORT_i() :_support((::SUPPORT *)NULL),_corbaIndex(SUPPORT_i::supportIndex++)
65 //=============================================================================
69 //=============================================================================
70 SUPPORT_i::SUPPORT_i(const ::SUPPORT * const s) :_support(s),
71 _corbaIndex(SUPPORT_i::supportIndex++)
73 if ( _support ) _support->addReference();
74 SUPPORT_i::supportMap[_corbaIndex]=(::SUPPORT *)_support;
76 //=============================================================================
80 //=============================================================================
81 SUPPORT_i::SUPPORT_i(const SUPPORT_i &s) :_support(s._support),
82 _corbaIndex(SUPPORT_i::supportIndex++)
84 if ( _support ) _support->addReference();
85 SUPPORT_i::supportMap[_corbaIndex]=(::SUPPORT *)_support;
87 //=============================================================================
91 //=============================================================================
93 SUPPORT_i::~SUPPORT_i()
95 if ( SUPPORT_i::supportMap[_corbaIndex] )
96 SUPPORT_i::supportMap[_corbaIndex]->removeReference();
98 //=============================================================================
100 * CORBA: Accessor for Corba Index
102 //=============================================================================
104 CORBA::Long SUPPORT_i::getCorbaIndex()
105 throw (SALOME::SALOME_Exception)
108 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
112 //=============================================================================
114 * CORBA: Accessor for Name
116 //=============================================================================
118 char * SUPPORT_i::getName()
119 throw (SALOME::SALOME_Exception)
122 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
125 return CORBA::string_dup(_support->getName().c_str());
127 catch (MEDEXCEPTION &ex)
129 MESSAGE("Unable to access the name of the support ");
130 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
134 //=============================================================================
136 * CORBA: Accessor for Description
138 //=============================================================================
140 char* SUPPORT_i::getDescription()
141 throw (SALOME::SALOME_Exception)
144 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
147 return CORBA::string_dup(_support->getDescription().c_str());
149 catch (MEDEXCEPTION &ex)
151 MESSAGE("Unable to access the description of the support ");
152 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
156 //=============================================================================
158 * CORBA: Accessor for Description and Name
160 //=============================================================================
162 SALOME_MED::SUPPORT::supportInfos * SUPPORT_i::getSupportGlobal()
163 throw (SALOME::SALOME_Exception)
166 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
167 SALOME_MED::SUPPORT::supportInfos_var all = new SALOME_MED::SUPPORT::supportInfos;
170 all->name = CORBA::string_dup(_support->getName().c_str());
171 all->description = CORBA::string_dup(_support->getDescription().c_str());
172 const int numberOfTypes = _support->getNumberOfTypes();
173 all->numberOfGeometricType = numberOfTypes;
174 all->entity = _support->getEntity();
175 all->isOnAllElements = _support->isOnAllElements();
176 all->types.length(numberOfTypes);
177 all->nbEltTypes.length(numberOfTypes);
178 all->nodalConnectivityLength.length(numberOfTypes);
179 const medGeometryElement * types = _support->getTypes();
180 const MESH* mesh = _support->getMesh()->convertInMESH();
181 for (int i=0;i<numberOfTypes;i++)
183 int nbelements = _support->getNumberOfElements(types[i]);
187 case MED_EN::MED_POLYGON:
188 case MED_EN::MED_POLYHEDRA:
190 if (_support->isOnAllElements() )
192 connLength = mesh->getConnectivityLength(MED_EN::MED_NODAL,
193 _support->getEntity(),MED_EN::MED_ALL_ELEMENTS);
197 const int * index = mesh->getConnectivityIndex(MED_EN::MED_NODAL,_support->getEntity());
198 const int * numbers=_support->getNumber(types[i]);
199 for (int j=0;j<nbelements;j++)
201 int elem = numbers[j] ;
202 connLength += index[elem] - index[elem-1];
208 connLength = nbelements * ( types[i] % 100 );
210 all->types[i] = convertMedEltToIdlElt(types[i]);
211 all->nbEltTypes[i] = nbelements;
212 all->nodalConnectivityLength[i] = connLength;
214 mesh->removeReference();
216 catch (MEDEXCEPTION &ex)
218 MESSAGE("Unable to access the description of the support ");
219 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
225 //=============================================================================
227 * CORBA: Accessor for Mesh
229 //=============================================================================
231 SALOME_MED::GMESH_ptr SUPPORT_i::getMesh()
232 throw (SALOME::SALOME_Exception)
235 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
238 const GMESH * mesh = _support->getMesh();
242 GMESH_i * m1 = new GMESH_i(const_cast<GMESH*>( mesh ));
243 SALOME_MED::GMESH_ptr m2 = m1->_this();
244 MESSAGE("SALOME_MED::GMESH_ptr SUPPORT_i::getMesh() checking des pointeurs CORBA");
251 catch (MEDEXCEPTION &ex)
253 MESSAGE("Unable to access the assoicated mesh");
254 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
258 //=============================================================================
260 * CORBA: boolean indicating if support concerns all elements
262 //=============================================================================
264 CORBA::Boolean SUPPORT_i::isOnAllElements()
265 throw (SALOME::SALOME_Exception)
268 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
271 return _support->isOnAllElements();
273 catch (MEDEXCEPTION &ex)
275 MESSAGE("Unable to access the type of the support");
276 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
280 //=============================================================================
284 //=============================================================================
285 CORBA::Long SUPPORT_i::getNumberOfTypes()
286 throw (SALOME::SALOME_Exception)
289 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
292 return _support->getNumberOfTypes();
294 catch (MEDEXCEPTION &ex)
296 MESSAGE("Unable to access number of support different types");
297 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
302 //=============================================================================
304 * CORBA: Accessor for type of support's entity
306 //=============================================================================
308 SALOME_MED::medEntityMesh SUPPORT_i::getEntity()
309 throw (SALOME::SALOME_Exception)
312 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
315 return convertMedEntToIdlEnt(_support->getEntity());
317 catch (MEDEXCEPTION &ex)
319 MESSAGE("Unable to access support s entity");
320 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
324 //=============================================================================
326 * CORBA: Accessor for types of geometry elements
328 //=============================================================================
330 SALOME_MED::medGeometryElement_array * SUPPORT_i::getTypes()
331 throw (SALOME::SALOME_Exception)
334 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
335 SALOME_MED::medGeometryElement_array_var myseq = new SALOME_MED::medGeometryElement_array;
338 int mySeqLength=_support->getNumberOfTypes();
339 myseq->length(mySeqLength);
340 const medGeometryElement * elemts = _support->getTypes();
341 for (int i=0;i<mySeqLength;i++)
343 myseq[i]=convertMedEltToIdlElt(elemts[i]);
346 catch (MEDEXCEPTION &ex)
348 MESSAGE("Unable to access support different types");
349 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
351 return myseq._retn();
354 //=============================================================================
356 * CORBA: Number of different types of geometry elements
357 * existing in the support
359 //=============================================================================
360 CORBA::Long SUPPORT_i::getNumberOfElements(SALOME_MED::medGeometryElement geomElement)
361 throw (SALOME::SALOME_Exception)
364 SCRUTE(SALOME_MED::MED_ALL_ELEMENTS);
367 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
370 return _support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
372 catch (MEDEXCEPTION &ex)
374 MESSAGE("Unable to access the number of support different types");
375 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
379 //=============================================================================
383 //=============================================================================
385 SALOME_TYPES::ListOfLong * SUPPORT_i::getNumber(SALOME_MED::medGeometryElement geomElement)
386 throw (SALOME::SALOME_Exception)
390 SCRUTE(convertIdlEltToMedElt(geomElement));
393 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
394 SALOME_TYPES::ListOfLong_var myseq= new SALOME_TYPES::ListOfLong;
397 int nbelements=_support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
398 myseq->length(nbelements);
399 SCRUTE(_support->getName());
401 SCRUTE(convertIdlEltToMedElt(geomElement));
402 const int * numbers=_support->getNumber(convertIdlEltToMedElt(geomElement));
403 for (int i=0;i<nbelements;i++)
409 catch (MEDEXCEPTION &ex)
411 MESSAGE("Unable to access the support optionnal index");
412 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
414 return myseq._retn();
417 //=============================================================================
419 * CORBA: get Nodes from file
421 //=============================================================================
422 SALOME_TYPES::ListOfLong * SUPPORT_i::getNumberFromFile(SALOME_MED::medGeometryElement geomElement)
423 throw (SALOME::SALOME_Exception)
427 SCRUTE(convertIdlEltToMedElt(geomElement));
430 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
431 SALOME_TYPES::ListOfLong_var myseq= new SALOME_TYPES::ListOfLong;
434 int nbelements=_support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
435 myseq->length(nbelements);
436 SCRUTE(_support->getName());
438 SCRUTE(convertIdlEltToMedElt(geomElement));
439 const int * numbers=_support->getNumberFromFile(convertIdlEltToMedElt(geomElement));
440 for (int i=0;i<nbelements;i++)
446 catch (MEDEXCEPTION &ex)
448 MESSAGE("Unable to access the support optionnal index");
449 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
451 return myseq._retn();
454 //=============================================================================
456 * CORBA: 2nd get Nodes
458 //=============================================================================
459 SALOME::SenderInt_ptr SUPPORT_i::getSenderForNumber(SALOME_MED::medGeometryElement geomElement)
460 throw (SALOME::SALOME_Exception)
464 SCRUTE(convertIdlEltToMedElt(geomElement));
466 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
467 SALOME::SenderInt_ptr ret;
470 int nbelements=_support->getNumberOfElements(convertIdlEltToMedElt(geomElement));
471 const int * numbers=_support->getNumber(convertIdlEltToMedElt(geomElement));
472 ret=SenderFactory::buildSender(*this,numbers,nbelements);
474 catch (MEDEXCEPTION &ex)
476 MESSAGE("Unable to access the support optionnal index");
477 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
479 catch(MultiCommException &ex2)
480 THROW_SALOME_CORBA_EXCEPTION(ex2.what(),SALOME::INTERNAL_ERROR);
483 //=============================================================================
485 * CORBA: Global Nodes Index (optionnaly designed by the user)
486 * CORBA: ??????????????????????????????
488 //=============================================================================
490 SALOME_TYPES::ListOfLong * SUPPORT_i::getNumberIndex()
491 throw (SALOME::SALOME_Exception)
494 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
495 SALOME_TYPES::ListOfLong_var myseq= new SALOME_TYPES::ListOfLong;
498 MESSAGE ("Nombre d'elements mis de façon stupide a MED_ALL_ELEMENTS");
499 int nbelements=_support->getNumberOfElements(::MED_ALL_ELEMENTS);
500 myseq->length(nbelements);
501 const int * numbers=_support->getNumberIndex();
502 for (int i=0;i<nbelements;i++)
507 catch (MEDEXCEPTION &ex)
509 MESSAGE("Unable to access the support index");
510 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
512 return myseq._retn();
514 //=============================================================================
516 * CORBA: 2nd Global Nodes Index (optionnaly designed by the user)
518 //=============================================================================
520 SALOME::SenderInt_ptr SUPPORT_i::getSenderForNumberIndex()
521 throw (SALOME::SALOME_Exception)
524 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
525 SALOME::SenderInt_ptr ret;
528 MESSAGE ("Nombre d'elements mis de façon stupide a MED_ALL_ELEMENTS");
529 int nbelements=_support->getNumberOfElements(::MED_ALL_ELEMENTS);
530 const int * numbers=_support->getNumberIndex();
531 ret=SenderFactory::buildSender(*this,numbers,nbelements);
533 catch (MEDEXCEPTION &ex)
535 MESSAGE("Unable to access the support index");
536 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
538 catch(MultiCommException &ex2)
539 THROW_SALOME_CORBA_EXCEPTION(ex2.what(),SALOME::INTERNAL_ERROR);
542 //=============================================================================
546 //=============================================================================
548 void SUPPORT_i::getBoundaryElements()
549 throw (SALOME::SALOME_Exception)
552 THROW_SALOME_CORBA_EXCEPTION("No associated Support", SALOME::INTERNAL_ERROR);
555 (const_cast< ::SUPPORT *>(_support))->getBoundaryElements();
557 catch (MEDEXCEPTION &)
559 MESSAGE("Unable to access elements");
560 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Support C++ Object",
561 SALOME::INTERNAL_ERROR);
564 //=============================================================================
566 * CORBA: add the Support in the StudyManager
568 //=============================================================================
569 void SUPPORT_i::addInStudy (SALOMEDS::Study_ptr myStudy, SALOME_MED::SUPPORT_ptr myIor)
570 throw (SALOME::SALOME_Exception,SALOMEDS::StudyBuilder::LockProtection)
572 if ( _supportId != "" )
574 MESSAGE("Support already in Study");
575 THROW_SALOME_CORBA_EXCEPTION("Support already in Study", SALOME::BAD_PARAM);
578 if ( CORBA::is_nil(myStudy) )
580 MESSAGE("Study not found");
581 THROW_SALOME_CORBA_EXCEPTION("Study deleted !!!",
582 SALOME::INTERNAL_ERROR);
586 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
587 SALOMEDS::GenericAttribute_var anAttr;
588 SALOMEDS::AttributeName_var aName;
589 SALOMEDS::AttributeIOR_var aIOR;
591 // Find SComponent labelled 'Med'
592 MESSAGE(" Find SComponent labelled 'MED'");
593 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
594 if ( CORBA::is_nil(medfather) )
596 MESSAGE("MED not found");
597 THROW_SALOME_CORBA_EXCEPTION("SComponent labelled 'Med' not Found",SALOME::INTERNAL_ERROR);
600 // Find SObject MESH (represent mesh in support)
601 SALOMEDS::SObject_var medmeshfather = myStudy->FindObjectByPath("/Med/MEDMESH");
602 if ( CORBA::is_nil(medmeshfather) )
604 MESSAGE(" No /Med/MEDMESH Found in study")
605 THROW_SALOME_CORBA_EXCEPTION("SObject labelled 'MEDMESH' not Found",SALOME::INTERNAL_ERROR);
607 MESSAGE(" Find SObject MESH (represent mesh in support)");
609 string meshName = _support->getMeshName() ;
610 string meshNameStudy = meshName;
612 for (string::size_type pos=0; pos<meshNameStudy.size();++pos)
614 if (isspace(meshNameStudy[pos])) meshNameStudy[pos] = '_';
617 // seulement sous Med : il peut y avoir le meme sous SMESH !!!
618 SALOMEDS::SObject_var medsupportfather = myStudy->FindObject(meshName.c_str());
619 if ( CORBA::is_nil(medsupportfather) )
620 THROW_SALOME_CORBA_EXCEPTION("SObject Mesh in Support not Found",SALOME::INTERNAL_ERROR);
621 // perhaps add MESH automatically ?
623 MESSAGE("Add a support Object under /Med/MESH/MESHNAME");
625 char * medsupfatherName;
626 int lenName = 15 + strlen(meshName.c_str()) + 1;
627 medsupfatherName = new char[lenName];
628 medsupfatherName = strcpy(medsupfatherName,"MEDSUPPORTS_OF_");
629 medsupfatherName = strcat(medsupfatherName,meshNameStudy.c_str());
631 SCRUTE(medsupfatherName);
633 SALOMEDS::SObject_var medsupfather = myStudy->FindObject(medsupfatherName);
634 if ( CORBA::is_nil(medsupfather) )
636 MESSAGE("Add Object MEDSUPPORT");
637 medsupfather = myBuilder->NewObject(medmeshfather);
638 anAttr = myBuilder->FindOrCreateAttribute(medsupfather, "AttributeName");
639 aName = SALOMEDS::AttributeName::_narrow(anAttr);
640 aName->SetValue(medsupfatherName);
643 //myBuilder->NewCommand();
645 string supportEntryPath = getEntryPath( _support );
647 SALOMEDS::SObject_var supportEntry = myStudy->FindObjectByPath(supportEntryPath.c_str());
651 if ( CORBA::is_nil(supportEntry) )
653 MESSAGE(" supportEntry is a nil corba object and is going to be created");
654 // not already in study : we create it !
655 SALOMEDS::SObject_var newObj = myBuilder->NewObject(medsupfather);
656 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
657 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
658 CORBA::ORB_var &orb = init(0,0);
659 CORBA::String_var iorStr = orb->object_to_string(myIor);
660 anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
661 aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
662 aIOR->SetValue(iorStr.in());
663 anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeName");
664 aName = SALOMEDS::AttributeName::_narrow(anAttr);
665 aName->SetValue(_support->getName().c_str());
666 _supportId = newObj->GetID();
670 // already in study : put new AttributeIOR !
671 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
672 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
673 CORBA::ORB_var &orb = init(0,0);
674 CORBA::String_var iorStr = orb->object_to_string(myIor);
675 anAttr = myBuilder->FindOrCreateAttribute(supportEntry, "AttributeIOR");
676 aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
677 aIOR->SetValue(iorStr.in());
679 myBuilder->CommitCommand();
681 SALOMEDS::SObject_var supportEntryBis = myStudy->FindObjectByPath(supportEntryPath.c_str());
683 MESSAGE("Just for checking, reuse of the corba pointer");
685 if ( CORBA::is_nil(supportEntry) )
687 MESSAGE("The reuse is OK");
691 MESSAGE("well !! the reuse is not OK and there was a problem in the storage in the study");
694 delete [] medsupfatherName;
697 // register the Corba pointer: increase the referrence count
698 MESSAGE("Registering of the Corba Support pointer");
702 //=======================================================================
703 //function : getEntryPath
705 //=======================================================================
707 string SUPPORT_i::getEntryPath(const ::MEDMEM::SUPPORT * aSupport)
709 string meshNameStudy( aSupport->getMeshName() );
710 for (string::size_type pos=0; pos<meshNameStudy.size();++pos)
711 if (isspace(meshNameStudy[pos])) meshNameStudy[pos] = '_';
713 string supportName = aSupport->getName();
714 string supportNameStudy( supportName.c_str(), strlen( supportName.c_str() ));
715 string supportEntryPath =
716 "/Med/MEDMESH/MEDSUPPORTS_OF_" + meshNameStudy + "/" + supportNameStudy;
717 SCRUTE( supportEntryPath );
719 return supportEntryPath;