Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDCouplingCorba / Client / MEDCouplingMultiFieldsClient.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MEDCouplingMultiFieldsClient.hxx"
21 #include "MEDCouplingFieldTemplate.hxx"
22 #include "MEDCouplingMeshClient.hxx"
23 #include "DataArrayDoubleClient.hxx"
24 #include "MEDCouplingMemArray.hxx"
25 #include "MEDCouplingMesh.hxx"
26
27 #include <vector>
28
29 using namespace ParaMEDMEM;
30
31 MEDCouplingMultiFields *MEDCouplingMultiFieldsClient::New(SALOME_MED::MEDCouplingMultiFieldsCorbaInterface_ptr fieldPtr)
32 {
33   MEDCouplingMultiFields *ret=MEDCouplingMultiFields::New();
34   fieldPtr->Register();
35   BuildFullMultiFieldsCorbaFetch(ret,fieldPtr);
36   //notify server that the servant is no more used.
37   fieldPtr->UnRegister();
38   return ret;
39 }
40
41 void MEDCouplingMultiFieldsClient::BuildFullMultiFieldsCorbaFetch(MEDCouplingMultiFields *ret,SALOME_MED::MEDCouplingMultiFieldsCorbaInterface_ptr fieldPtr)
42 {
43   //
44   SALOME_TYPES::ListOfLong *tinyL=0;
45   SALOME_TYPES::ListOfDouble *tinyD=0;
46   SALOME_TYPES::ListOfString *tinyS=0;
47   //
48   CORBA::Long nbOfArrays;
49   CORBA::Long nbOfFields;
50   CORBA::Long nbOfMeshes=fieldPtr->getMainTinyInfo(tinyL,tinyD,nbOfArrays,nbOfFields);
51   int tinyLgth=tinyL->length();
52   std::vector<int> mainI(tinyLgth);
53   for(int i=0;i<tinyLgth;i++)
54     mainI[i]=(*tinyL)[i];
55   delete tinyL; tinyL=0;
56   tinyLgth=tinyD->length();
57   std::vector<double> mainD(tinyLgth);
58   for(int i=0;i<tinyLgth;i++)
59     mainD[i]=(*tinyD)[i];
60   delete tinyD; tinyD=0;
61   //
62   std::vector<MEDCouplingMesh *> cppms(nbOfMeshes);
63   SALOME_MED::MEDCouplingMeshesCorbaInterface *meshes=fieldPtr->getMeshes();
64   for(CORBA::Long i=0;i<nbOfMeshes;i++)
65     {
66       cppms[i]=MEDCouplingMeshClient::New((*meshes)[i]);
67       (*meshes)[i]->UnRegister();
68     }
69   delete meshes;
70   //
71   std::vector<DataArrayDouble *> cppdas(nbOfArrays);
72   for(CORBA::Long i=0;i<nbOfArrays;i++)
73     {
74       SALOME_MED::DataArrayDoubleCorbaInterface_ptr daPtr=fieldPtr->getArray(i);
75       cppdas[i]=DataArrayDoubleClient::New(daPtr);
76       daPtr->UnRegister();
77       CORBA::release(daPtr);
78     }
79   //
80   std::vector<MEDCouplingFieldTemplate *> fts(nbOfFields);
81   for(CORBA::Long i=0;i<nbOfFields;i++)
82     {
83       fieldPtr->getTinyInfo(i,tinyL,tinyD,tinyS);
84       int tinyLgth=tinyL->length();
85       std::vector<int> tinyLV(tinyLgth);
86       for(int j=0;j<tinyLgth;j++)
87         tinyLV[j]=(*tinyL)[j];
88       delete tinyL; tinyL=0;
89       //
90       tinyLgth=tinyD->length();
91       std::vector<double> tinyLD(tinyLgth);
92       for(int j=0;j<tinyLgth;j++)
93         tinyLD[j]=(*tinyD)[j];
94       delete tinyD; tinyD=0;
95       //
96       tinyLgth=tinyS->length();
97       std::vector<std::string> tinyLS(tinyLgth);
98       for(int j=0;j<tinyLgth;j++)
99         tinyLS[j]=(*tinyS)[j];
100       delete tinyS; tinyS=0;
101       TypeOfField type=(TypeOfField) tinyLV[0];
102       MEDCouplingFieldTemplate *f1=MEDCouplingFieldTemplate::New(type);
103       //
104       DataArrayInt *array0;
105       f1->resizeForUnserialization(tinyLV,array0);
106       if(array0)
107         {
108           fieldPtr->getSerialisationData(i,tinyL);
109           tinyLgth=tinyL->length();
110           int *ptr=array0->getPointer();
111           for(int j=0;j<tinyLgth;j++)
112             ptr[j]=(*tinyL)[j];
113           delete tinyL; tinyL=0;
114         }
115       f1->finishUnserialization(tinyLV,tinyLD,tinyLS);
116       fts[i]=f1;
117     }
118   //last step
119   ret->finishUnserialization(mainI,mainD,fts,cppms,cppdas);
120   // clean-up
121   for(std::vector<MEDCouplingFieldTemplate *>::iterator it=fts.begin();it!=fts.end();it++)
122     (*it)->decrRef();
123   for(std::vector<MEDCouplingMesh *>::iterator it=cppms.begin();it!=cppms.end();it++)
124     (*it)->decrRef();
125   for(std::vector<DataArrayDouble *>::iterator it=cppdas.begin();it!=cppdas.end();it++)
126     (*it)->decrRef();
127 }
128