Salome HOME
fc82b6bf3132b0a3b00ae00c2cb32d7d10474f0a
[modules/med.git] / src / MEDCouplingCorba / Client / MEDCouplingMultiFieldsClient.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingMultiFieldsClient.hxx"
22 #include "MEDCouplingFieldTemplate.hxx"
23 #include "MEDCouplingMeshClient.hxx"
24 #include "DataArrayDoubleClient.hxx"
25 #include "MEDCouplingMemArray.hxx"
26 #include "MEDCouplingMesh.hxx"
27
28 #include <vector>
29
30 using namespace MEDCoupling;
31
32 MEDCouplingMultiFields *MEDCouplingMultiFieldsClient::New(SALOME_MED::MEDCouplingMultiFieldsCorbaInterface_ptr fieldPtr)
33 {
34   MEDCouplingMultiFields *ret=MEDCouplingMultiFields::New();
35   fieldPtr->Register();
36   BuildFullMultiFieldsCorbaFetch(ret,fieldPtr);
37   //notify server that the servant is no more used.
38   fieldPtr->UnRegister();
39   return ret;
40 }
41
42 void MEDCouplingMultiFieldsClient::BuildFullMultiFieldsCorbaFetch(MEDCouplingMultiFields *ret,SALOME_MED::MEDCouplingMultiFieldsCorbaInterface_ptr fieldPtr)
43 {
44   //
45   SALOME_TYPES::ListOfLong *tinyL=0;
46   SALOME_TYPES::ListOfDouble *tinyD=0;
47   SALOME_TYPES::ListOfString *tinyS=0;
48   //
49   CORBA::Long nbOfArrays;
50   CORBA::Long nbOfFields;
51   CORBA::Long nbOfMeshes=fieldPtr->getMainTinyInfo(tinyL,tinyD,nbOfArrays,nbOfFields);
52   int tinyLgth=tinyL->length();
53   std::vector<mcIdType> mainI(tinyLgth);
54   for(int i=0;i<tinyLgth;i++)
55     mainI[i]=(*tinyL)[i];
56   delete tinyL; tinyL=0;
57   tinyLgth=tinyD->length();
58   std::vector<double> mainD(tinyLgth);
59   for(int i=0;i<tinyLgth;i++)
60     mainD[i]=(*tinyD)[i];
61   delete tinyD; tinyD=0;
62   //
63   std::vector<MEDCouplingMesh *> cppms(nbOfMeshes);
64   SALOME_MED::MEDCouplingMeshesCorbaInterface *meshes=fieldPtr->getMeshes();
65   for(CORBA::Long i=0;i<nbOfMeshes;i++)
66     {
67       cppms[i]=MEDCouplingMeshClient::New((*meshes)[i]);
68       (*meshes)[i]->UnRegister();
69     }
70   delete meshes;
71   //
72   std::vector<DataArrayDouble *> cppdas(nbOfArrays);
73   for(CORBA::Long i=0;i<nbOfArrays;i++)
74     {
75       SALOME_MED::DataArrayDoubleCorbaInterface_ptr daPtr=fieldPtr->getArray(i);
76       cppdas[i]=DataArrayDoubleClient::New(daPtr);
77       daPtr->UnRegister();
78       CORBA::release(daPtr);
79     }
80   //
81   std::vector<MEDCouplingFieldTemplate *> fts(nbOfFields);
82   for(CORBA::Long i=0;i<nbOfFields;i++)
83     {
84       fieldPtr->getTinyInfo(i,tinyL,tinyD,tinyS);
85       int tinyLgth2=tinyL->length();
86       std::vector<mcIdType> tinyLV(tinyLgth2);
87       for(int j=0;j<tinyLgth2;j++)
88         tinyLV[j]=(*tinyL)[j];
89       delete tinyL; tinyL=0;
90       //
91       tinyLgth2=tinyD->length();
92       std::vector<double> tinyLD(tinyLgth2);
93       for(int j=0;j<tinyLgth2;j++)
94         tinyLD[j]=(*tinyD)[j];
95       delete tinyD; tinyD=0;
96       //
97       tinyLgth2=tinyS->length();
98       std::vector<std::string> tinyLS(tinyLgth2);
99       for(int j=0;j<tinyLgth2;j++)
100         tinyLS[j]=(*tinyS)[j];
101       delete tinyS; tinyS=0;
102       TypeOfField type=(TypeOfField) tinyLV[0];
103       MEDCouplingFieldTemplate *f1=MEDCouplingFieldTemplate::New(type);
104       //
105       DataArrayIdType *array0;
106       f1->resizeForUnserialization(tinyLV,array0);
107       if(array0)
108         {
109           fieldPtr->getSerialisationData(i,tinyL);
110           tinyLgth2=tinyL->length();
111           mcIdType *ptr=array0->getPointer();
112           for(int j=0;j<tinyLgth2;j++)
113             ptr[j]=(*tinyL)[j];
114           delete tinyL; tinyL=0;
115         }
116       f1->finishUnserialization(tinyLV,tinyLD,tinyLS);
117       fts[i]=f1;
118     }
119   //last step
120   ret->finishUnserialization(mainI,mainD,fts,cppms,cppdas);
121   // clean-up
122   for(std::vector<MEDCouplingFieldTemplate *>::iterator it=fts.begin();it!=fts.end();it++)
123     (*it)->decrRef();
124   for(std::vector<MEDCouplingMesh *>::iterator it=cppms.begin();it!=cppms.end();it++)
125     (*it)->decrRef();
126   for(std::vector<DataArrayDouble *>::iterator it=cppdas.begin();it!=cppdas.end();it++)
127     (*it)->decrRef();
128 }
129