Salome HOME
DCQ : Merge with Ecole Ete a6.
[modules/visu.git] / src / VISU_I / VISU_CorbaMedConvertor.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VISU_CorbaMedConvertor.cxx
25 //  Author : Alexey PETROV
26 //  Module : VISU
27 //  $Header$
28 //  Copyright (C) 2003  CEA/DEN, EDF R&D
29
30 #include "VISU_CorbaMedConvertor.hxx"
31 #include "VISU_ConvertorUtils.hxx"
32
33 #include <valarray>     
34 #include <vtkCellType.h>
35
36 using namespace VISU;
37 using namespace std;
38
39 #define USER_INTERLACE MED_FULL_INTERLACE
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46 static med_err ret = 0;
47
48 extern "C" {
49   VISU_Convertor* CreateMEDConvertor(SALOMEDS::SObject_ptr theMedSObject) {
50     return new VISU_MEDConvertor(theMedSObject);
51   }
52   VISU_Convertor* CreateMEDFieldConvertor(SALOME_MED::FIELD_ptr theField) {
53     return new VISU_MEDFieldConvertor(theField);
54   }
55 }
56
57 typedef map<VISU::TEntity,SALOME_MED::medEntityMesh> TVisu2MedEntity;
58 static TVisu2MedEntity aVisu2MedEntity;
59 typedef map<SALOME_MED::medEntityMesh,VISU::TEntity> TMed2VisuEntity;
60 static TMed2VisuEntity aMed2VisuEntity;
61 static int INIT = (
62                    aVisu2MedEntity[VISU::CELL_ENTITY] = SALOME_MED::MED_CELL,
63                    aVisu2MedEntity[VISU::FACE_ENTITY] = SALOME_MED::MED_FACE,
64                    aVisu2MedEntity[VISU::EDGE_ENTITY] = SALOME_MED::MED_EDGE,
65                    aVisu2MedEntity[VISU::NODE_ENTITY] = SALOME_MED::MED_NODE,
66
67                    aMed2VisuEntity[SALOME_MED::MED_CELL] = VISU::CELL_ENTITY,
68                    aMed2VisuEntity[SALOME_MED::MED_FACE] = VISU::FACE_ENTITY,
69                    aMed2VisuEntity[SALOME_MED::MED_EDGE] = VISU::EDGE_ENTITY,
70                    aMed2VisuEntity[SALOME_MED::MED_NODE] = VISU::NODE_ENTITY,
71
72                    1);
73
74 static int CELLGEOM[MED_NBR_GEOMETRIE_MAILLE] = {
75   SALOME_MED::MED_POINT1,
76   SALOME_MED::MED_SEG2,
77   SALOME_MED::MED_SEG3,
78   SALOME_MED::MED_TRIA3,
79   SALOME_MED::MED_QUAD4,
80   SALOME_MED::MED_TRIA6,
81   SALOME_MED::MED_QUAD8,
82   SALOME_MED::MED_TETRA4,
83   SALOME_MED::MED_PYRA5,
84   SALOME_MED::MED_PENTA6,
85   SALOME_MED::MED_HEXA8,
86   SALOME_MED::MED_TETRA10,
87   SALOME_MED::MED_PYRA13,
88   SALOME_MED::MED_PENTA15,
89   SALOME_MED::MED_HEXA20
90 };
91
92 static const int VTKCELLGEOMEND = 8;
93 static int VTKCELLGEOM[VTKCELLGEOMEND] = {
94   SALOME_MED::MED_POINT1,
95   SALOME_MED::MED_SEG2,
96   SALOME_MED::MED_TRIA3,
97   SALOME_MED::MED_QUAD4,
98   SALOME_MED::MED_TETRA4,
99   SALOME_MED::MED_PYRA5,
100   SALOME_MED::MED_PENTA6,
101   SALOME_MED::MED_HEXA8
102 };
103
104 static int FACEGEOM[MED_NBR_GEOMETRIE_FACE] = {
105   SALOME_MED::MED_TRIA3,
106   SALOME_MED::MED_QUAD4,
107   SALOME_MED::MED_TRIA6,
108   SALOME_MED::MED_QUAD8
109 };
110
111 static int EDGEGEOM[MED_NBR_GEOMETRIE_ARETE] = {
112   SALOME_MED::MED_SEG2,
113   SALOME_MED::MED_SEG3
114 };
115
116 static int NODEGEOM[1] = {
117   SALOME_MED::MED_POINT1,
118 };
119
120 void GetEntity2Geom(const VISU::TEntity& theEntity, int*& theVector, int* theEnd)
121 {
122   switch(theEntity){
123   case VISU::CELL_ENTITY: theVector = CELLGEOM; *theEnd = MED_NBR_GEOMETRIE_MAILLE; break;
124   case VISU::FACE_ENTITY: theVector = FACEGEOM; *theEnd = MED_NBR_GEOMETRIE_FACE; break;
125   case VISU::EDGE_ENTITY: theVector = EDGEGEOM; *theEnd = MED_NBR_GEOMETRIE_ARETE; break;
126   case VISU::NODE_ENTITY: theVector = NODEGEOM; *theEnd = 1; break;
127   default:
128     throw std::runtime_error("GetEntity2Geom >> theEntity is uncorrect !!!");
129   }
130 }
131
132 struct SalomeMed2vtk {
133   SALOME_MED::medGeometryElement medType;
134   char *medName;
135   int medNbNodes;
136   int vtkType;
137   char *vtkName;
138   int vtkNbNodes;
139 };
140
141 #define CORBAMED2VTK(MEDTYPE,VTKTYPE,VTKNBNODES) \
142  {SALOME_MED::MEDTYPE,#MEDTYPE,getNbMedNodes(MEDTYPE),VTKTYPE,#VTKTYPE,VTKNBNODES}
143 static SalomeMed2vtk salome_med2vtk[SALOME_MED::MED_ALL_ELEMENTS] = {
144   {SALOME_MED::MED_NONE,"MED_NONE",0,VTK_EMPTY_CELL,"VTK_EMPTY_CELL",0},
145   CORBAMED2VTK(MED_POINT1,VTK_VERTEX,1),
146   CORBAMED2VTK(MED_SEG2,VTK_LINE,2),
147   CORBAMED2VTK(MED_SEG3,VTK_LINE,2),
148   CORBAMED2VTK(MED_TRIA3,VTK_TRIANGLE,3),
149   CORBAMED2VTK(MED_QUAD4,VTK_QUAD,4),
150   CORBAMED2VTK(MED_TRIA6,VTK_TRIANGLE,3),
151   CORBAMED2VTK(MED_QUAD8,VTK_QUAD,4),
152   CORBAMED2VTK(MED_TETRA4,VTK_TETRA,4),
153   CORBAMED2VTK(MED_PYRA5,VTK_PYRAMID,5),
154   CORBAMED2VTK(MED_PENTA6,VTK_WEDGE,6),
155   CORBAMED2VTK(MED_HEXA8,VTK_HEXAHEDRON,8),
156   CORBAMED2VTK(MED_TETRA10,VTK_TETRA,4),
157   CORBAMED2VTK(MED_PYRA13,VTK_PYRAMID,5),
158   CORBAMED2VTK(MED_PENTA15,VTK_WEDGE,6),
159   CORBAMED2VTK(MED_HEXA20,VTK_HEXAHEDRON,8)
160 };
161 #undef CORBAMED2VTK
162
163 int GetIdMEDType(int medType){
164   for(int i = 0; i < SALOME_MED::MED_ALL_ELEMENTS; i++)
165     if(salome_med2vtk[i].medType == medType) return i;
166   return -1;
167 }
168
169 string GetName(SALOMEDS::SObject_ptr aSObject){
170   SALOMEDS::GenericAttribute_var anAttr;
171   if (aSObject->FindAttribute(anAttr,"AttributeName")) {
172     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
173     CORBA::String_var aString = aName->Value();
174     return aString.in();
175   }
176   return "";
177 }
178
179
180 static void GetCellsSize(vtkIdType& theNbCells, vtkIdType& theCellsSize,
181                          SALOME_MED::MESH_ptr theMEDMesh,
182                          const VISU::TEntity& theEntity)
183 {
184   int iGeomElemEnd;
185   int* aGeomElemVector;
186   GetEntity2Geom(theEntity,aGeomElemVector,&iGeomElemEnd);
187   theNbCells = theCellsSize = 0;
188   const SALOME_MED::medEntityMesh& aMedEntity = aVisu2MedEntity[theEntity];
189   if(MYDEBUG) MESSAGE("GetCellsSize - theEntity = "<<theEntity);
190   for (int iGeomElem = 0, aCounter = 0; iGeomElem < iGeomElemEnd; iGeomElem++) {
191     int medId = GetIdMEDType(aGeomElemVector[iGeomElem]);
192     SALOME_MED::medGeometryElement aMedType = salome_med2vtk[medId].medType;
193     med_int iNumElemEnd = theMEDMesh->getNumberOfElements(aMedEntity,aMedType);
194     if(iNumElemEnd > 0){
195       if(MYDEBUG) MESSAGE("GetCellsSize - iNumElemEnd = "<<iNumElemEnd);
196       theNbCells += iNumElemEnd;
197       theCellsSize += iNumElemEnd*(salome_med2vtk[medId].vtkNbNodes + 1);
198     }
199   }
200 }
201
202
203 static void GetCellsSize(vtkIdType& theNbCells, vtkIdType& theCellsSize,
204                          SALOME_MED::FAMILY_ptr theMEDFamily)
205 {
206   SALOME_MED::medGeometryElement_array_var aGeom = theMEDFamily->getTypes();
207   med_int iGeomElemEnd = aGeom->length();
208   theNbCells = theCellsSize = 0;
209   if(MYDEBUG) MESSAGE("GetCellsSize - iGeomElemEnd = "<<iGeomElemEnd);
210   for (int iGeomElem = 0; iGeomElem < iGeomElemEnd; iGeomElem++) {
211     SALOME_MED::medGeometryElement aGeomType = aGeom[iGeomElem];
212     SALOME_MED::long_array_var aCellNumForType = theMEDFamily->getNumber(aGeomType);
213     int medId = GetIdMEDType(aGeomType);
214     med_int iNumElemEnd = aCellNumForType->length();
215     if(iNumElemEnd > 0){
216       if(MYDEBUG) MESSAGE("GetCellsSize - iNumElemEnd = "<<iNumElemEnd);
217       theNbCells += iNumElemEnd;
218       theCellsSize += iNumElemEnd*(salome_med2vtk[medId].vtkNbNodes + 1);
219     }
220   }
221 }
222
223
224 static void GetCellsSize(VISU::TMesh& theMesh, 
225                          SALOME_MED::MESH_ptr theMEDMesh, 
226                          const VISU::TEntity& theEntity)
227 {
228   VISU::TMeshOnEntity& aMeshOnEntity = theMesh.myMeshOnEntityMap[theEntity];
229   if(theEntity == VISU::NODE_ENTITY){
230     theMesh.myNbPoints = theMEDMesh->getNumberOfNodes();
231     aMeshOnEntity.myNbCells = theMesh.myNbPoints;
232     aMeshOnEntity.myCellsSize = 2*theMesh.myNbPoints;
233     vtkIdType aNbCells, aCellsSize;
234     GetCellsSize(aNbCells,aCellsSize,theMEDMesh,VISU::CELL_ENTITY);
235     if(aNbCells > 0){
236       VISU::TMeshOnEntity& aMeshOnCells = theMesh.myMeshOnEntityMap[VISU::CELL_ENTITY];
237       aMeshOnCells.myEntity = VISU::CELL_ENTITY;
238       aMeshOnCells.myMeshName = theMesh.myName;
239       aMeshOnCells.myNbCells = aNbCells;
240       aMeshOnCells.myCellsSize = aCellsSize;
241     }
242   }else
243     GetCellsSize(aMeshOnEntity.myNbCells,aMeshOnEntity.myCellsSize,theMEDMesh,theEntity);
244 }
245
246
247 VISU_Convertor* VISU_MEDFieldConvertor::Build() {
248   if(myField->_is_nil()) 
249     throw std::runtime_error("VISU_MEDFieldConvertor::Build >> myField->_is_nil() !!!");
250   
251   SALOME_MED::SUPPORT_var aMEDSupport = myField->getSupport();
252   if(aMEDSupport->_is_nil()) 
253     throw std::runtime_error("VISU_MEDFieldConvertor::Build >> aMEDSupport->_is_nil() !!!");
254   SALOME_MED::medEntityMesh aMEDEntity = aMEDSupport->getEntity();
255   VISU::TEntity anEntity = aMed2VisuEntity[aMEDEntity];
256   SALOME_MED::MESH_var aMEDMesh = aMEDSupport->getMesh();
257   if(aMEDMesh->_is_nil()) 
258     throw std::runtime_error("VISU_MEDFieldConvertor::Build >> aMEDMesh->_is_nil() !!!");
259   CORBA::String_var aMeshName = aMEDMesh->getName();
260   CORBA::String_var aFieldName = myField->getName();
261
262   VISU::TMesh &aMesh = myMeshMap[aMeshName.in()];
263   aMesh.myDim = aMEDMesh->getSpaceDimension();
264   aMesh.myName = aMeshName.in();
265   VISUMED::TMesh &aMesh2 = myMeshMap2[aMeshName.in()];
266   aMesh2.myMesh = aMEDMesh;
267   if(MYDEBUG) MESSAGE("VISU_MEDFieldConvertor::Build - aMeshName = "<<aMeshName<<"; myDim = "<<aMesh.myDim);
268
269   VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[anEntity];
270   aMeshOnEntity.myEntity = anEntity;
271   aMeshOnEntity.myMeshName = aMeshName.in();
272   VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[anEntity];
273   aMeshOnEntity2.mySupport = aMEDSupport;
274   if(anEntity == VISU::NODE_ENTITY)
275     aMesh2.myMeshOnEntityMap[VISU::CELL_ENTITY].mySupport = aMEDSupport;
276   GetCellsSize(aMesh,aMEDMesh,anEntity);
277
278   VISU::TField& aField = aMeshOnEntity.myFieldMap[aFieldName.in()];
279   aField.myId = myField->getOrderNumber();
280   aField.myName = aFieldName.in();
281   aField.myEntity = anEntity;
282   aField.myMeshName = aMeshName.in();
283   aField.myNbComp = myField->getNumberOfComponents();
284   aField.myNbValField = 1;
285   aField.myDataSize = aMeshOnEntity.myNbCells * aField.myNbComp;
286   aField.myCompNames.resize(aField.myNbComp);
287   aField.myUnitNames.resize(aField.myNbComp);
288   if(MYDEBUG) MESSAGE("VISU_MEDFieldConvertor::Build - aMeshName = "<<aMeshName<<"; myDim = "<<aMesh.myDim);
289   int iTimeStamp = myField->getIterationNumber();
290   VISU::TField::TValForTime& aValForTime = aField.myValField[iTimeStamp];
291   aValForTime.myId = iTimeStamp;
292   double dt = myField->getTime();
293   aValForTime.myTime = VISU::TField::TTime(dt,"");
294   
295   VISUMED::TField& aField2 = aMeshOnEntity2.myFieldMap[aFieldName.in()];
296   VISUMED::TField::TValForTime& aValForTime2 = aField2.myValField[iTimeStamp];
297   aValForTime2.myField = myField;
298   if(MYDEBUG) 
299     MESSAGE("VISU_MEDConvertor::Build - aFieldName = '"<<aFieldName<<"'; myId = "<<aField.myId<<"; myTime = "<<dt);
300   return this;
301 }
302
303 VISU_Convertor* VISU_MEDConvertor::Build() {
304   if(mySObject->_is_nil()) 
305     throw std::runtime_error("VISU_MEDConvertor::Build >> mySObject->_is_nil() !!!");
306   SALOMEDS::Study_var aStudy = mySObject->GetStudy();
307
308   CORBA::Object_var aMedObject = SObjectToObject(mySObject);
309   if(!CORBA::is_nil(aMedObject)){
310     SALOME_MED::MED_var aMED = SALOME_MED::MED::_narrow(aMedObject);
311     if(!aMED->_is_nil()){
312       CORBA::Short aTag = mySObject->Tag();
313       SALOMEDS::SObject_var aMedCompSObj = mySObject->GetFather();
314       SALOMEDS::SObject_var aMeshSObj;
315       if(!aMedCompSObj->FindSubObject(aTag+1,aMeshSObj)) 
316         throw std::runtime_error("VISU_MEDConvertor::Build >> Cann't find MEDMESH label !!!");
317       if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - MEDMESH found.");
318       SALOMEDS::ChildIterator_var aMeshIterator = aStudy->NewChildIterator(aMeshSObj);
319       for(; aMeshIterator->More(); aMeshIterator->Next()){
320         aMeshSObj = aMeshIterator->Value();
321         if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aMeshSObj = '"<<::GetName(aMeshSObj)<<"'");
322         CORBA::Object_var aMedMesh = SObjectToObject(aMeshSObj);
323         if(CORBA::is_nil(aMedMesh)) continue;
324         SALOME_MED::MESH_var aMEDMesh = SALOME_MED::MESH::_narrow(aMedMesh);
325         if(aMEDMesh->_is_nil()) continue;
326         CORBA::String_var aMeshName = aMEDMesh->getName();
327         VISU::TMesh &aMesh = myMeshMap[aMeshName.in()];
328         aMesh.myDim = aMEDMesh->getSpaceDimension();
329         aMesh.myName = aMeshName.in();
330         VISUMED::TMesh &aMesh2 = myMeshMap2[aMeshName.in()];
331         aMesh2.myMesh = aMEDMesh;
332         if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aMeshName = "<<aMeshName<<"; myDim = "<<aMesh.myDim);
333         SALOMEDS::ChildIterator_var aSupportIterator = aStudy->NewChildIterator(aMeshSObj);
334         for(; aSupportIterator->More(); aSupportIterator->Next()){
335           SALOMEDS::SObject_var aSupportSObj = aSupportIterator->Value();
336           CORBA::Object_var aMedSupport = SObjectToObject(aSupportSObj);
337           if(CORBA::is_nil(aMedSupport)) continue;
338           SALOME_MED::SUPPORT_var aMEDSupport = SALOME_MED::SUPPORT::_narrow(aMedSupport); 
339           if(aMEDSupport->_is_nil()) continue;
340           SALOME_MED::MESH_var aMeshOnSupport = aMEDSupport->getMesh();
341           SALOME_MED::medEntityMesh aMEDEntity = aMEDSupport->getEntity();
342           VISU::TEntity anEntity = aMed2VisuEntity[aMEDEntity];
343           CORBA::String_var aSupportName = aMEDSupport->getName();
344           if(aMEDSupport->isOnAllElements()){
345             if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - Support isOnAllElements = '"<<aSupportName<<"' anEntity = "<<anEntity);
346             int aNbCells, aCellsSize;
347             //Check, if there is any data on the support?
348             if(anEntity == VISU::NODE_ENTITY){
349               aMesh.myNbPoints = aMeshOnSupport->getNumberOfNodes();
350               aNbCells = aMesh.myNbPoints;
351               aCellsSize = 2*aMesh.myNbPoints;
352             }else{
353               GetCellsSize(aNbCells,aCellsSize,aMeshOnSupport,anEntity);
354             }
355             if(aNbCells == 0) continue;
356             VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[anEntity];
357             aMeshOnEntity.myMeshName = aMeshName.in();
358             aMeshOnEntity.myEntity = anEntity;
359             aMeshOnEntity.myNbCells = aNbCells;
360             aMeshOnEntity.myCellsSize = aCellsSize;
361
362             VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[anEntity];
363             aMeshOnEntity2.mySupport = aMEDSupport;
364           }else{
365             SALOME_MED::FAMILY_var aMEDFamily = SALOME_MED::FAMILY::_narrow(aMedSupport);
366             if(!aMEDFamily->_is_nil()) {
367               int aNbCells, aCellsSize;
368               GetCellsSize(aNbCells,aCellsSize,aMEDFamily);
369               if(aNbCells > 0){
370                 if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aFamily = '"<<aSupportName<<"' anEntity = "<<anEntity);
371                 VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[anEntity];
372                 VISU::TFamily& aFamily = aMeshOnEntity.myFamilyMap[aSupportName.in()];
373                 aFamily.myName = aSupportName.in();
374                 aFamily.myEntity = anEntity;
375                 aFamily.myNbCells = aNbCells;
376                 aFamily.myCellsSize = aCellsSize;
377                 aFamily.myId = aMEDFamily->getIdentifier();
378
379                 VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[anEntity];
380                 VISUMED::TFamily& aFamily2 = aMeshOnEntity2.myFamilyMap[aSupportName.in()];
381                 aFamily2.myFamily = aMEDFamily;
382               }
383             }
384             SALOME_MED::GROUP_var aMEDGroup = SALOME_MED::GROUP::_narrow(aMedSupport);
385             if(!aMEDGroup->_is_nil()) {
386               if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aGroup = '"<<aSupportName<<"' anEntity = "<<anEntity);
387               VISUMED::TGroupMap& aGroupMap2 = aMesh2.myGroupMap;
388               VISUMED::TGroup& aGroup2 = aGroupMap2[aSupportName.in()];
389               aGroup2.myGroup = aMEDGroup;
390               SALOME_MED::Family_array_var aFamilies = aMEDGroup->getFamilies();
391               int iFamilyEnd = aFamilies->length();
392               for(int iFamaily = 0; iFamaily < iFamilyEnd; iFamaily++){
393                 aMEDFamily = aFamilies[iFamaily];
394                 CORBA::String_var aFamilyName = aMEDFamily->getName();
395                 VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[anEntity];
396                 if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aGroup - aFamilyName = '"<<aFamilyName.in()<<"'");
397                 VISU::TFamily& aFamily = aMeshOnEntity.myFamilyMap[aFamilyName.in()];
398                 VISU::TBindGroups& aBindGroups = aFamily.myGroups;
399                 aBindGroups.insert(aSupportName.in());
400               }
401             }
402           }
403         }
404         //Correction of TMesh.TGroupMap
405         const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh.myMeshOnEntityMap;
406         if(aMeshOnEntityMap.empty()) continue;
407         VISU::TGroupMap& aGroupMap = aMesh.myGroupMap;
408         VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
409         for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
410           const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
411           const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity.myFamilyMap;
412           if(aFamilyMap.empty()) continue;
413           VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
414           for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
415             const VISU::TFamily& aFamily = aFamilyMapIter->second;
416             const VISU::TBindGroups& aBindGroups = aFamily.myGroups;
417             if(aBindGroups.empty()) continue;
418             VISU::TBindGroups::const_iterator aBindGroupsIter = aBindGroups.begin();
419             for(; aBindGroupsIter != aBindGroups.end(); aBindGroupsIter++){
420               const string& aGroupName = *aBindGroupsIter;
421               VISU::TGroup& aGroup = aGroupMap[aGroupName];
422               aGroup.myName = aGroupName;
423               aGroup.myMeshName = aMesh.myName;
424               aGroup.myNbCells += aFamily.myNbCells;
425               aGroup.myCellsSize += aFamily.myCellsSize;
426               VISU::TFamilyAndEntity aFamilyAndEntity(aFamily.myName,aFamily.myEntity);
427               aGroup.myFamilyAndEntitySet.insert(aFamilyAndEntity);
428             }
429           }
430         }
431       }
432       SALOMEDS::SObject_var aFieldSObj;
433       if(aMedCompSObj->FindSubObject(aTag+2,aFieldSObj)){
434         if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - MEDFIELD found.");
435         SALOMEDS::ChildIterator_var aFieldIterator = aStudy->NewChildIterator(aFieldSObj);
436         for(int iField = 0; aFieldIterator->More(); aFieldIterator->Next(), iField++){
437           aFieldSObj = aFieldIterator->Value();
438           if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aFieldName = '"<<::GetName(aFieldSObj)<<"'");
439           SALOMEDS::ChildIterator_var aTimeStampIterator = aStudy->NewChildIterator(aFieldSObj);
440           for(int iTimeStamp = 1; aTimeStampIterator->More(); aTimeStampIterator->Next(), iTimeStamp++){
441             SALOMEDS::SObject_var aTimeStampSObj = aTimeStampIterator->Value();
442             if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aTimeStampSObj = '"<<::GetName(aTimeStampSObj)<<"'");
443             CORBA::Object_var aMedField = SObjectToObject(aTimeStampSObj);
444             if(CORBA::is_nil(aMedField)) continue;
445             SALOME_MED::FIELD_var aMEDField = SALOME_MED::FIELD::_narrow(aMedField);
446             if(aMEDField->_is_nil()) continue;
447             SALOME_MED::SUPPORT_var aMEDSupport = aMEDField->getSupport();
448             if(aMEDSupport->_is_nil()) continue;
449             SALOME_MED::medEntityMesh aMEDEntity = aMEDSupport->getEntity();
450             VISU::TEntity anEntity = aMed2VisuEntity[aMEDEntity];
451             SALOME_MED::MESH_var aMEDMesh = aMEDSupport->getMesh();
452             if(aMEDMesh->_is_nil()) continue;
453             CORBA::String_var aMeshName = aMEDMesh->getName();
454             CORBA::String_var aFieldName = aMEDField->getName();
455
456             VISU::TMesh &aMesh = myMeshMap[aMeshName.in()];
457             aMesh.myDim = aMEDMesh->getSpaceDimension();
458             aMesh.myName = aMeshName.in();
459             VISUMED::TMesh &aMesh2 = myMeshMap2[aMeshName.in()];
460             aMesh2.myMesh = aMEDMesh;
461
462             VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[anEntity];
463             aMeshOnEntity.myEntity = anEntity;
464             aMeshOnEntity.myMeshName = aMeshName.in();
465             VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[anEntity];
466             aMeshOnEntity2.mySupport = aMEDSupport;
467             if(anEntity == VISU::NODE_ENTITY)
468               aMesh2.myMeshOnEntityMap[VISU::CELL_ENTITY].mySupport = aMEDSupport;
469             GetCellsSize(aMesh,aMEDMesh,anEntity);
470
471             VISU::TField& aField = aMeshOnEntity.myFieldMap[aFieldName.in()];
472             aField.myId = iField;
473             aField.myName = aFieldName.in();
474             aField.myEntity = anEntity;
475             aField.myMeshName = aMeshName.in();
476             aField.myNbComp = aMEDField->getNumberOfComponents();
477             aField.myNbValField = iTimeStamp;
478             aField.myDataSize = aMeshOnEntity.myNbCells * aField.myNbComp;
479             if(MYDEBUG && !iTimeStamp) 
480               MESSAGE("VISU_MEDConvertor::Build - aMeshOnEntity.myNbCells = "<<aMeshOnEntity.myNbCells);
481             aField.myCompNames.resize(aField.myNbComp);
482             aField.myUnitNames.resize(aField.myNbComp);
483
484             int anId = aMEDField->getIterationNumber();
485             VISU::TField::TValForTime& aValForTime = aField.myValField[anId];
486             aValForTime.myId = anId;
487             aValForTime.myTime = VISU::TField::TTime(aMEDField->getTime(),"");
488             VISUMED::TField& aField2 = aMeshOnEntity2.myFieldMap[aFieldName.in()];
489             VISUMED::TField::TValForTime& aValForTime2 = aField2.myValField[anId];
490             aValForTime2.myField = aMEDField;
491             if(MYDEBUG) 
492               MESSAGE("VISU_MEDConvertor::Build - aMeshName = '"<<aMeshName<<
493                       "'; myEntity = "<<anEntity<<"; myTime = "<<aValForTime.myTime.first);
494           }      
495         }
496       }
497       return this; 
498     }
499     return NULL; 
500   }
501   SALOMEDS::ChildIterator_var aTimeStampIterator = aStudy->NewChildIterator(mySObject);
502   for(int iTimeStamp = 1; aTimeStampIterator->More(); aTimeStampIterator->Next(), iTimeStamp++){
503     SALOMEDS::SObject_var aTimeStampSObj = aTimeStampIterator->Value();
504     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aTimeStampSObj = '"<<::GetName(aTimeStampSObj)<<"'");
505     CORBA::Object_var aMedField = SObjectToObject(aTimeStampSObj);
506     if(CORBA::is_nil(aMedField)) continue;
507     SALOME_MED::FIELD_var aMEDField = SALOME_MED::FIELD::_narrow(aMedField);
508     if(aMEDField->_is_nil()) continue;
509     SALOME_MED::SUPPORT_var aMEDSupport = aMEDField->getSupport();
510     if(aMEDSupport->_is_nil()) continue;
511     SALOME_MED::medEntityMesh aMEDEntity = aMEDSupport->getEntity();
512     VISU::TEntity anEntity = aMed2VisuEntity[aMEDEntity];
513     SALOME_MED::MESH_var aMEDMesh = aMEDSupport->getMesh();
514     if(aMEDMesh->_is_nil()) continue;
515     CORBA::String_var aMeshName = aMEDMesh->getName();
516     CORBA::String_var aFieldName = aMEDField->getName();
517
518     VISU::TMesh &aMesh = myMeshMap[aMeshName.in()];
519     aMesh.myDim = aMEDMesh->getSpaceDimension();
520     aMesh.myName = aMeshName.in();
521     aMesh.myNbPoints = aMEDMesh->getNumberOfNodes();
522     VISUMED::TMesh &aMesh2 = myMeshMap2[aMeshName.in()];
523     aMesh2.myMesh = aMEDMesh;
524     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aMeshName = "<<aMeshName<<"; myDim = "<<aMesh.myDim);
525
526     VISU::TMeshOnEntity& aMeshOnEntity = aMesh.myMeshOnEntityMap[anEntity];
527     aMeshOnEntity.myEntity = anEntity;
528     aMeshOnEntity.myMeshName = aMeshName.in();
529     VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[anEntity];
530     aMeshOnEntity2.mySupport = aMEDSupport;
531     if(anEntity == VISU::NODE_ENTITY)
532       aMesh2.myMeshOnEntityMap[VISU::CELL_ENTITY].mySupport = aMEDSupport;
533     GetCellsSize(aMesh,aMEDMesh,anEntity);
534
535     VISU::TField& aField = aMeshOnEntity.myFieldMap[aFieldName.in()];
536     CORBA::Short iField = mySObject->Tag();
537     aField.myId = iField;
538     aField.myName = aFieldName.in();
539     aField.myEntity = anEntity;
540     aField.myMeshName = aMeshName.in();
541     aField.myNbComp = aMEDField->getNumberOfComponents();
542     aField.myNbValField = iTimeStamp;
543     aField.myDataSize = aMeshOnEntity.myNbCells * aField.myNbComp;
544     if(MYDEBUG && !iTimeStamp) 
545       MESSAGE("VISU_MEDConvertor::Build - aMeshOnEntity.myNbCells = "<<aMeshOnEntity.myNbCells);
546     aField.myCompNames.resize(aField.myNbComp);
547     aField.myUnitNames.resize(aField.myNbComp);
548
549     int anId = aMEDField->getIterationNumber();
550     VISU::TField::TValForTime& aValForTime = aField.myValField[anId];
551     aValForTime.myId = anId;
552     aValForTime.myTime = VISU::TField::TTime(aMEDField->getTime(),"");
553     VISUMED::TField& aField2 = aMeshOnEntity2.myFieldMap[aFieldName.in()];
554     VISUMED::TField::TValForTime& aValForTime2 = aField2.myValField[anId];
555     aValForTime2.myField = aMEDField;
556     if(MYDEBUG) 
557       MESSAGE("VISU_MEDConvertor::Build - aMeshName = '"<<aMeshName<<
558               "'; myEntity = "<<anEntity<<"; myTime = "<<aValForTime.myTime.first);
559   }
560   return this; 
561 }
562
563 int VISU_MEDConvertor::LoadMeshOnEntity(VISU::TMeshOnEntity& theMeshOnEntity, 
564                                         const string& theFamilyName)
565 {
566   //Main part of code
567   const string& aMeshName = theMeshOnEntity.myMeshName;
568   const VISU::TEntity& anEntity = theMeshOnEntity.myEntity;
569   VISU::TMesh& aMesh = myMeshMap[aMeshName];
570   int isPointsUpdated;
571   if(anEntity == VISU::NODE_ENTITY) 
572     isPointsUpdated = LoadPoints(aMesh,theFamilyName);
573   else 
574     isPointsUpdated = LoadPoints(aMesh);
575   int isCellsOnEntityUpdated = LoadCellsOnEntity(theMeshOnEntity,theFamilyName);
576
577   return (isPointsUpdated || isCellsOnEntityUpdated);
578 }
579   
580 int VISU_MEDConvertor::LoadMeshOnGroup(VISU::TMesh& theMesh, 
581                                        const VISU::TFamilyAndEntitySet& theFamilyAndEntitySet)
582 {
583   //Main part of code
584   int isPointsUpdated = 0;
585   int isCellsOnEntityUpdated = 0;
586   VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = theFamilyAndEntitySet.begin();
587   for(; aFamilyAndEntitySetIter != theFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
588     const string& aFamilyName = aFamilyAndEntitySetIter->first;
589     const VISU::TEntity& anEntity = aFamilyAndEntitySetIter->second;
590     VISU::TMeshOnEntity& aMeshOnEntity = theMesh.myMeshOnEntityMap[anEntity];
591     if(anEntity == VISU::NODE_ENTITY){
592       isPointsUpdated += LoadPoints(theMesh,aFamilyName);
593       isCellsOnEntityUpdated += LoadCellsOnEntity(aMeshOnEntity);
594     }else{
595       isPointsUpdated += LoadPoints(theMesh);
596       isCellsOnEntityUpdated += LoadCellsOnEntity(aMeshOnEntity,aFamilyName);
597     }
598   }
599
600   return (isPointsUpdated || isCellsOnEntityUpdated);
601 }
602
603 int VISU_MEDConvertor::LoadFieldOnMesh(VISU::TMesh& theMesh, 
604                                        VISU::TMeshOnEntity& theMeshOnEntity, 
605                                        VISU::TField& theField, 
606                                        VISU::TField::TValForTime& theValForTime)
607 {
608   //Main part of code
609   int isPointsUpdated = LoadPoints(theMesh);
610   int isCellsOnEntityUpdated = LoadCellsOnEntity(theMeshOnEntity);
611   int isFieldUpdated = LoadField(theMeshOnEntity,theField,theValForTime);
612   
613   return (isPointsUpdated || isCellsOnEntityUpdated || isFieldUpdated);
614 }
615
616 int VISU_MEDConvertor::LoadPoints(VISU::TMesh& theMesh, const string& theFamilyName)
617 {
618   //Check on existing family
619   VISU::TMeshOnEntity& aMeshOnEntity = theMesh.myMeshOnEntityMap[VISU::NODE_ENTITY];
620   aMeshOnEntity.myEntity = VISU::NODE_ENTITY;
621   aMeshOnEntity.myMeshName = theMesh.myName;
622   VISU::TFamily* pFamily = VISU::GetFamily(aMeshOnEntity,theFamilyName);
623   bool isFamilyPresent = (pFamily != NULL);
624   VISU::TFamily& aFamily = *pFamily;
625   //Check on loading already done
626   bool isPointsLoaded = !theMesh.myPointsCoord.empty();
627   if(isPointsLoaded) 
628     if(!isFamilyPresent) return 0;
629     else if(!aFamily.mySubMesh.empty()) return 0;
630   VISUMED::TMesh& aMesh2 = myMeshMap2[theMesh.myName];
631   SALOME_MED::MESH_var& aMedMesh = aMesh2.myMesh;
632   VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[VISU::NODE_ENTITY];
633   if(MYDEBUG) 
634     MESSAGE("LoadPoints - isPointsLoaded = "<<isPointsLoaded<<"; theFamilyName = '"<<theFamilyName<<"'");
635   theMesh.myDim = aMedMesh->getSpaceDimension();
636   int iNumElemEnd = aMedMesh->getNumberOfNodes();
637   VISU::TMesh::TPointsCoord& aPointsCoord = theMesh.myPointsCoord;
638   if(MYDEBUG) MESSAGE("LoadPoints - iNumElemEnd = "<<iNumElemEnd);
639   if (iNumElemEnd <= 0) throw std::runtime_error("LoadPoints >> There is no points in the mesh !!!");
640   aPointsCoord.resize(theMesh.myDim*iNumElemEnd,0.0);
641    SALOME_MED::double_array_var coord = aMedMesh->getCoordinates(SALOME_MED::MED_FULL_INTERLACE);
642   if(!isPointsLoaded){
643     for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) 
644       for(int iDim = 0, iNumElem2Dim = iNumElem*theMesh.myDim; iDim < theMesh.myDim; iDim++, iNumElem2Dim++)
645         aPointsCoord[iNumElem2Dim] = coord[iNumElem2Dim];
646     if(MYDEBUG) MESSAGE("LoadPoints - Filling aMeshOnEntity with type NODE_ENTITY");
647     VISU::TMeshOnEntity::TConnForCellType& aConnForCellType = aMeshOnEntity.myCellsConn[VTK_VERTEX];
648     aConnForCellType.resize(iNumElemEnd);
649     for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++)
650       aConnForCellType[iNumElem] = VISU::TMeshOnEntity::TConnect(1,iNumElem);
651   }
652   if(isFamilyPresent){
653     if(MYDEBUG) MESSAGE("LoadPoints - Filling aFamily SubMesh");
654     VISUMED::TFamily aFamily2 = aMeshOnEntity2.myFamilyMap[aFamily.myName];
655     SALOME_MED::FAMILY_var aMedFamily = aFamily2.myFamily;
656     VISU::TFamily::TSubMeshOnCellType& aSubMeshOnCellType = aFamily.mySubMesh[VTK_VERTEX];
657     SALOME_MED::medGeometryElement_array_var aGeom = aMedFamily->getTypes();
658     SALOME_MED::long_array_var aCellNumForType = aMedFamily->getNumber(aGeom[0]);
659     int iNumElemEndTmp = iNumElemEnd;
660     iNumElemEnd = aCellNumForType->length();
661     for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) {
662       int tmp = aCellNumForType[iNumElem]-1;
663       if(0 > tmp || tmp >= iNumElemEndTmp) {
664         static QString aString;
665         aString.sprintf("LoadPoints >> iNumElemEndTmp(%d) <= aCellNumForType[%d]=%d < 0 !!!",iNumElemEnd,iNumElem,tmp);
666         throw std::runtime_error(aString.latin1());
667       }
668       aSubMeshOnCellType.insert(tmp);
669     }
670   }
671   return 1;
672 }
673
674 int VISU_MEDConvertor::LoadCellsOnEntity(VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
675 {
676   //Check on existing family
677   VISU::TFamily* pFamily = VISU::GetFamily(theMeshOnEntity,theFamilyName);
678   bool isFamilyPresent = (pFamily != NULL);
679   VISU::TFamily& aFamily = *pFamily;
680   //Check on loading already done
681   bool isCellsLoaded = !theMeshOnEntity.myCellsConn.empty();
682   if(isCellsLoaded) 
683     if(!isFamilyPresent) return 0;
684     else if(!aFamily.mySubMesh.empty()) return 0;
685   VISUMED::TMesh& aMesh2 = myMeshMap2[theMeshOnEntity.myMeshName];
686   VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[theMeshOnEntity.myEntity];
687   SALOME_MED::SUPPORT_var& aMedSupport = aMeshOnEntity2.mySupport;
688   SALOME_MED::MESH_var aMedMesh = aMedSupport->getMesh();
689   if(MYDEBUG) {
690     MESSAGE("LoadCellsOnEntity - theFamilyName = '"<<theFamilyName<<"'");
691     MESSAGE("LoadCellsOnEntity - isCellsLoaded = "<<isCellsLoaded<<"; isFamilyPresent = "<<isFamilyPresent);
692   }
693   //Main part of code
694   int iGeomElemEnd;
695   int* aGeomElemVector;
696   const VISU::TEntity& anEntity = theMeshOnEntity.myEntity;
697   GetEntity2Geom(anEntity,aGeomElemVector,&iGeomElemEnd);
698   const SALOME_MED::medEntityMesh& aMedEntity = aVisu2MedEntity[anEntity];
699   VISU::TMesh &aMesh = myMeshMap[theMeshOnEntity.myMeshName];
700   int aNbPoints = aMesh.myPointsCoord.size()/aMesh.myDim;
701   if(!isCellsLoaded){
702     for (int iGeomElem = 0, aCounter = 0; iGeomElem < iGeomElemEnd; iGeomElem++) {
703       int medId = GetIdMEDType(aGeomElemVector[iGeomElem]);
704       int nbMedNodes = salome_med2vtk[medId].medNbNodes;
705       int nbVtkNodes = salome_med2vtk[medId].vtkNbNodes;
706       int aVtkType = salome_med2vtk[medId].vtkType;
707       SALOME_MED::medGeometryElement aMedType = salome_med2vtk[medId].medType;
708       med_int iNumElemEnd = aMedMesh->getNumberOfElements(aMedEntity,aMedType);
709       if (iNumElemEnd > 0) {
710         SALOME_MED::long_array_var conn = 
711           aMedMesh->getConnectivity(SALOME_MED::MED_FULL_INTERLACE,SALOME_MED::MED_NODAL,aMedEntity,aMedType);
712         VISU::TMeshOnEntity::TConnForCellType& aConnForCellType = theMeshOnEntity.myCellsConn[aVtkType];
713         //APO - aConnForCellType.resize(iNumElemEnd);
714         valarray<med_int> aConnect(nbMedNodes);
715         int aNbConnForElem = conn->length()/iNumElemEnd;
716         if(MYDEBUG) MESSAGE("LoadCellsOnEntity - medName = "<<salome_med2vtk[medId].medName<<
717                             "; iNumElemEnd = "<<iNumElemEnd<<"; aNbConnForElem = "<<aNbConnForElem);
718         for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) {
719           //APO - VISU::TMeshOnEntity::TConnect& anArray = aConnForCellType[iNumElem];
720           //APO - anArray.resize(nbVtkNodes);
721           VISU::TMeshOnEntity::TConnect anArray(nbVtkNodes);
722           for (int k = 0, kj = iNumElem*aNbConnForElem; k < nbMedNodes; k++) {
723             aConnect[k] = conn[kj+k] - 1;
724           }
725           switch(aMedType){
726           case SALOME_MED::MED_TETRA4 :
727           case SALOME_MED::MED_TETRA10 :
728             anArray[0] = aConnect[0];
729             anArray[1] = aConnect[1];
730             anArray[2] = aConnect[3];  
731             anArray[3] = aConnect[2];  
732             break;
733           case SALOME_MED::MED_PYRA5 :
734           case SALOME_MED::MED_PYRA13 :
735             anArray[0] = aConnect[0];
736             anArray[1] = aConnect[3];  
737             anArray[2] = aConnect[2];
738             anArray[3] = aConnect[1];  
739             anArray[4] = aConnect[4];
740             break;
741           default:
742             for (int k = 0; k < nbVtkNodes; k++) 
743               anArray[k] = aConnect[k];
744           }
745           for (int k = 0; k < nbVtkNodes; k++) 
746             if(anArray[k] < 0 || aNbPoints <= anArray[k]){
747               static QString aString;
748               aString.sprintf("ImportCells >> aNbPoints(%d) <= anArray[%d][%d]=%d < 0 !!!",aNbPoints,iNumElem,k,anArray[k]);
749               throw std::runtime_error(aString.latin1());
750             }
751           aConnForCellType.push_back(anArray);
752         }
753         //Workaround for MED Component data structure
754         int aSize = aConnForCellType.size();
755         aMeshOnEntity2.myCellsFirstIndex[aMedType] = VISUMED::TMeshOnEntity::TIndexAndSize(aCounter,aSize);
756         aCounter += aSize;
757       }
758     }
759   }
760   //Filling aFamily SubMesh
761   if(isFamilyPresent){
762     VISUMED::TFamily aFamily2 = aMeshOnEntity2.myFamilyMap[aFamily.myName];
763     SALOME_MED::FAMILY_var aMedFamily = aFamily2.myFamily;
764     SALOME_MED::medGeometryElement_array_var aGeom = aMedFamily->getTypes();
765     iGeomElemEnd = aGeom->length();
766     if(MYDEBUG) MESSAGE("LoadCellsOnEntity - iGeomElemEnd = "<<iGeomElemEnd);
767     for (int iGeomElem = 0; iGeomElem < iGeomElemEnd; iGeomElem++) {
768       SALOME_MED::medGeometryElement aGeomType = aGeom[iGeomElem];
769       SALOME_MED::long_array_var aCellNumForType = aMedFamily->getNumber(aGeomType);
770       int medId = GetIdMEDType(aGeomType);
771       int aVtkType = salome_med2vtk[medId].vtkType;
772       SALOME_MED::medGeometryElement aMedType = salome_med2vtk[medId].medType;
773       VISU::TFamily::TSubMeshOnCellType& aSubMeshOnCellType = aFamily.mySubMesh[aVtkType]; 
774       med_int iNumElemEndTmp = theMeshOnEntity.myCellsConn[aVtkType].size();
775       med_int iNumElemEnd = aCellNumForType->length();
776       int aCounter = aMeshOnEntity2.myCellsFirstIndex[aMedType].first;
777       if(MYDEBUG) 
778         MESSAGE("LoadCellsOnEntity - medName = "<<salome_med2vtk[medId].medName<<
779                 "; iNumElemEnd = "<<iNumElemEnd<<"; aCounter = "<<aCounter);
780       for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) {
781         int tmp = aCellNumForType[iNumElem]-aCounter-1;
782         if(0 > tmp || tmp >= iNumElemEndTmp) {
783           static QString aString;
784           aString.sprintf("LoadCellsOnEntity >> iNumElemEndTmp(%d) <= aCellNumForType[%d]=%d < 0 !!!",iNumElemEndTmp,iNumElem,tmp);
785           throw std::runtime_error(aString.latin1());
786         }
787         aSubMeshOnCellType.insert(tmp);
788       }
789     }
790   }
791   return 1;
792 }
793
794 template<class TArray> int ImportField(TArray& theArray, 
795                                        const VISU::TMesh& theMesh,
796                                        const VISU::TField& theField,
797                                        VISU::TField::TValForTime& theValForTime,
798                                        const VISU::TMeshOnEntity& theMeshOnEntity,
799                                        const VISUMED::TMeshOnEntity& theMeshOnEntity2)
800 {
801   if(theField.myEntity == VISU::NODE_ENTITY){
802     VISU::TField::TValForCellsWithType& aValForCellsWithType = theValForTime.myValForCells[VTK_VERTEX];
803     int iNumElemEnd = theMesh.myPointsCoord.size()/theMesh.myDim*theField.myNbComp;
804     if(MYDEBUG) MESSAGE("ImportField - iNumElemEnd = "<<iNumElemEnd);
805     aValForCellsWithType.resize(iNumElemEnd);
806     for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) 
807       aValForCellsWithType[iNumElem] = theArray[iNumElem];
808   }else{
809     int iGeomElemEnd;
810     int* aGeomElemVector;
811     const VISU::TEntity& anEntity = theField.myEntity;
812     GetEntity2Geom(anEntity,aGeomElemVector,&iGeomElemEnd);
813     for (int iGeomElem = 0, aCounter = 0; iGeomElem < iGeomElemEnd; iGeomElem++) {
814       int medId = GetIdMEDType(aGeomElemVector[iGeomElem]);
815       int aVtkType = salome_med2vtk[medId].vtkType;
816       SALOME_MED::medGeometryElement aMedType = salome_med2vtk[medId].medType;
817       const VISUMED::TMeshOnEntity::TCellsFirstIndex& aCellsFirstIndex = theMeshOnEntity2.myCellsFirstIndex;
818       VISUMED::TMeshOnEntity::TCellsFirstIndex::const_iterator aCellsFirstIndexIter = aCellsFirstIndex.find(aMedType);
819       if(aCellsFirstIndexIter != aCellsFirstIndex.end()){
820         const VISU::TMeshOnEntity::TCellsConn& aCellsConn = theMeshOnEntity.myCellsConn;
821         VISU::TMeshOnEntity::TCellsConn::const_iterator aCellsConnIter = aCellsConn.find(aVtkType);
822         const VISU::TMeshOnEntity::TConnForCellType& aConnForCellType = aCellsConnIter->second;
823         const VISUMED::TMeshOnEntity::TIndexAndSize& aIndexAndSize = aCellsFirstIndexIter->second;
824         int iNumElemEnd = aIndexAndSize.second;
825         if(MYDEBUG) 
826           MESSAGE("ImportField - medName = "<<salome_med2vtk[medId].medName<<
827                   "; aIndexAndSize = {"<<aIndexAndSize.first<<","<<aIndexAndSize.second<<"}");
828         VISU::TField::TValForCellsWithType& aValForCellsWithType = theValForTime.myValForCells[aVtkType];
829         aValForCellsWithType.resize(iNumElemEnd*theField.myNbComp);
830         for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++)
831           for (int k = 0, kj = iNumElem*theField.myNbComp; k < theField.myNbComp; k++)
832             aValForCellsWithType[kj+k] = theArray[aIndexAndSize.first*theField.myNbComp+kj+k];
833       }
834     }
835   }
836   return 1;
837 }
838
839 int VISU_MEDConvertor::LoadField(const VISU::TMeshOnEntity& theMeshOnEntity,
840                                  const VISU::TField& theField, VISU::TField::TValForTime& theValForTime)
841 {
842   //Check on loading already done
843   if(!theValForTime.myValForCells.empty()) return 0;
844   VISU::TMesh& aMesh = myMeshMap[theMeshOnEntity.myMeshName];
845   VISUMED::TMesh& aMesh2 = myMeshMap2[theMeshOnEntity.myMeshName];
846   VISUMED::TMeshOnEntity& aMeshOnEntity2 = aMesh2.myMeshOnEntityMap[theMeshOnEntity.myEntity];
847   VISUMED::TField& aField2 = aMeshOnEntity2.myFieldMap[theField.myName];
848   VISUMED::TField::TValForTime& aValForTime2 = aField2.myValField[theValForTime.myId];
849   SALOME_MED::FIELD_var aMEDField = aValForTime2.myField;
850   //Main part of code
851   SALOME_MED::FIELDDOUBLE_ptr aFieldDouble = SALOME_MED::FIELDDOUBLE::_narrow(aMEDField);
852   if(!aFieldDouble->_is_nil()){
853     SALOME_MED::double_array_var anArray = aFieldDouble->getValue(SALOME_MED::MED_FULL_INTERLACE);
854     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::LoadField - There is FIELDDOUBLE = "<<anArray->length());
855     ::ImportField(anArray,aMesh,theField,theValForTime,theMeshOnEntity,aMeshOnEntity2);
856   }
857   SALOME_MED::FIELDINT_ptr aFieldInt = SALOME_MED::FIELDINT::_narrow(aMEDField);
858   if(!aFieldInt->_is_nil()){
859     SALOME_MED::long_array_var anArray = aFieldInt->getValue(SALOME_MED::MED_FULL_INTERLACE);
860     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::LoadField - There is FIELDINT = "<<anArray->length());
861     ::ImportField(anArray,aMesh,theField,theValForTime,theMeshOnEntity,aMeshOnEntity2);
862   }
863   return 1;
864 }