]> SALOME platform Git repositories - modules/visu.git/blob - src/CONVERTOR/VISU_MedConvertor.cxx
Salome HOME
a0434a30cd6c652ee092178ee109bccc9343be28
[modules/visu.git] / src / CONVERTOR / VISU_MedConvertor.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 //  File   : VISU_MedConvertor.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27
28 #include "VISU_MedConvertor.hxx"
29 #include "VISU_Convertor.hxx"
30 #include "VISU_ConvertorUtils.hxx"
31
32 #include "MED_Factory.hxx"
33 #include "MED_Algorithm.hxx"
34 #include "MED_Utilities.hxx"
35
36 #include <vtkCellType.h>
37
38 #define _EDF_NODE_IDS_
39
40 using namespace std;
41 using namespace MED;
42 using namespace VISU;
43
44 #ifdef _DEBUG_
45 static int MYDEBUG = 0;
46 #else
47 static int MYDEBUG = 0;
48 #endif
49
50
51 namespace{
52
53   using namespace MED;
54   using namespace VISU;
55   
56   int MEDGeom2NbNodes(MED::EGeometrieElement theMEDGeomType)
57   { 
58     return theMEDGeomType % 100;
59   }
60
61   int MEDGeomToVTK(MED::EGeometrieElement theMEDGeomType)
62   { 
63     switch(theMEDGeomType){
64     case ePOINT1: return VTK_VERTEX;
65     case eSEG2: return VTK_LINE;
66     case eSEG3: return VTK_LINE;
67     case eTRIA3: return VTK_TRIANGLE;
68     case eTRIA6: return VTK_TRIANGLE;
69     case eQUAD4: return VTK_QUAD;
70     case eQUAD8: return VTK_QUAD;
71     case eTETRA4: return VTK_TETRA;
72     case eTETRA10: return VTK_TETRA;
73     case eHEXA8: return VTK_HEXAHEDRON;
74     case eHEXA20: return VTK_HEXAHEDRON;
75     case ePENTA6: return VTK_WEDGE;
76     case ePENTA15: return VTK_WEDGE;
77     case ePYRA5: return VTK_PYRAMID;
78     case ePYRA13: return VTK_PYRAMID;
79     case ePOLYGONE: return VTK_POLYGON;
80 //     case ePOLYEDRE: return VTK_POLYEDRE;
81     }
82     return -1;
83   }
84
85   int VTKGeom2NbNodes(int theVTKGeomType)
86   { 
87     switch(theVTKGeomType){
88     case VTK_VERTEX: return 1;
89     case VTK_LINE: return 2;
90     case VTK_TRIANGLE: return 3;
91     case VTK_QUAD: return 4;
92     case VTK_TETRA: return 4;
93     case VTK_HEXAHEDRON: return 8;
94     case VTK_WEDGE: return 6;
95     case VTK_PYRAMID: return 5;
96     }
97     return -1;
98   }
99
100   MED::EGeometrieElement VTKGeomToMED(int theVTKGeomType)
101   { 
102     switch(theVTKGeomType){
103     case VTK_VERTEX: return ePOINT1;
104     case VTK_LINE: return eSEG2;
105     case VTK_TRIANGLE: return eTRIA3;
106     case VTK_QUAD: return eQUAD4;
107     case VTK_TETRA: return eTETRA4;
108     case VTK_HEXAHEDRON: return eHEXA8;
109     case VTK_WEDGE: return ePENTA6;
110     case VTK_PYRAMID: return ePYRA5;
111     case VTK_POLYGON: return ePOLYGONE;
112     }
113     return EGeometrieElement(-1);
114   }
115
116   TEntity MEDEntityToVTK(MED::EEntiteMaillage theMEDEntity)
117   {
118     switch(theMEDEntity){
119     case eNOEUD: return NODE_ENTITY;
120     case eARETE: return EDGE_ENTITY;
121     case eFACE: return FACE_ENTITY;
122     case eMAILLE: return CELL_ENTITY;
123     }
124     return TEntity(-1);
125   }
126
127   MED::EEntiteMaillage VTKEntityToMED(TEntity theVTKEntity)
128   {
129     switch(theVTKEntity){
130     case NODE_ENTITY: return eNOEUD;
131     case EDGE_ENTITY: return eARETE;
132     case FACE_ENTITY: return eFACE;
133     case CELL_ENTITY: return eMAILLE;
134     }
135     return MED::EEntiteMaillage(-1);
136   }
137
138 }
139
140 extern "C"
141 VISU_Convertor* CreateConvertor(const string& theFileName)
142 {
143   return new VISU_MedConvertor(theFileName);
144 }
145
146 VISU_MedConvertor::VISU_MedConvertor(const string& theFileName)  {
147   myFileInfo.setFile(QString(theFileName.c_str()));
148   myName = myFileInfo.baseName().latin1();
149 }
150
151 VISU_Convertor* VISU_MedConvertor::Build()  {
152   MED::PWrapper aMed = MED::CrWrapper(myFileInfo.absFilePath().latin1());
153   MED::TInt aNbMeshes = aMed->GetNbMeshes();
154
155   MSG(MYDEBUG,"VISU_MedConvertor::Build()");
156   INITMSG(MYDEBUG,"GetNbMeshes() = "<<aNbMeshes<<"\n");
157
158   for(MED::TInt iMesh = 1; iMesh <= aNbMeshes; iMesh++){
159     try{
160       MED::PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh);
161       
162       MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
163       
164       MED::TElemGroup anElemGroup = GetElemsByEntity(aMed,aMeshInfo,aEntityInfo);
165
166       // Find PNodeInfo
167       MED::TElemGroup::const_iterator anElemGroupIter = anElemGroup.find(MED::eNOEUD);
168       if(anElemGroupIter == anElemGroup.end())
169         continue;
170
171       const MED::TElemMap& anElemMap = anElemGroupIter->second;
172       MED::TElemMap::const_iterator anElemMapIter = anElemMap.find(MED::ePOINT1);
173       if(anElemMapIter == anElemMap.end())
174         continue;
175
176       MED::PNodeInfo aNodeInfo(anElemMapIter->second);
177
178       // creating TMesh structure and TMeshOnEntityMap
179       typedef std::map<TInt,TInt> TFamilyCounterMap;
180       TFamilyCounterMap aFamilyNbCellsCounterMap, aFamilyCellsSizeCounterMap;
181       TFamilyCounterMap aFamilyNbPolygonesCounterMap, aFamilyPolygonesSizeCounterMap;
182
183       MED::TInt aDim = aMeshInfo->GetDim();
184       std::string aMeshName = aMeshInfo->GetName();
185     
186       VISU::PMEDMesh aMesh = myMeshMap[aMeshName](new VISU::TMEDMesh());
187       aMesh->myDim = aDim;
188       aMesh->myName = aMeshName;
189       aMesh->myNbPoints = aNodeInfo->GetNbElem();
190       aMesh->myMeshInfo = aMeshInfo;
191       aMesh->myEntityInfo = aEntityInfo;
192
193       INITMSG(MYDEBUG,"aMeshName = '"<<aMeshName<<
194               "'; myNbPoints = "<<aMesh->myNbPoints<<
195               "; aDim = "<<aDim<<"\n");
196       
197       BEGMSG(MYDEBUG,"aEntityInfo.size() = "<<aEntityInfo.size()<<"\n");
198
199       VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
200       anElemGroupIter = anElemGroup.begin();
201       for(; anElemGroupIter != anElemGroup.end(); anElemGroupIter++){
202         const MED::EEntiteMaillage& aMEntity = anElemGroupIter->first;
203         const MED::TElemMap& anElemMap = anElemGroupIter->second;
204
205         VISU::TEntity aVEntity = MEDEntityToVTK(aMEntity);
206         VISU::PMEDMeshOnEntity aMeshOnEntity = 
207           aMeshOnEntityMap[aVEntity](new VISU::TMEDMeshOnEntity());
208         aMeshOnEntity->myEntity = aVEntity;
209         aMeshOnEntity->myMeshName = aMeshName;
210         aMeshOnEntity->myGeom = aEntityInfo[aMEntity];
211
212         INITMSG(MYDEBUG,"aMEntity = "<<aMEntity<<"; aVEntity = "<<aVEntity<<"\n");
213
214         if(aMEntity == MED::eNOEUD){
215           aMeshOnEntity->myNbCells = aMesh->myNbPoints;
216           aMeshOnEntity->myCellsSize = 2*aMesh->myNbPoints;
217           
218           for(MED::TInt iElem = 0; iElem < aMesh->myNbPoints; iElem++){
219             MED::TInt aFamId = aNodeInfo->GetFamNum(iElem);
220             if(aFamId != 0){
221               aFamilyNbCellsCounterMap[aFamId] += 1;
222               aFamilyCellsSizeCounterMap[aFamId] += 2;
223             }
224           }
225
226           INITMSG(MYDEBUG,"myNbCells = "<<aMeshOnEntity->myNbCells<<
227                   "; myCellsSize = "<<aMeshOnEntity->myCellsSize<<"\n");;
228
229         }else{
230           aMeshOnEntity->myNbCells = 0;
231           aMeshOnEntity->myCellsSize = 0;
232           anElemMapIter = anElemMap.begin();
233           for(; anElemMapIter != anElemMap.end(); anElemMapIter++){
234             const MED::EGeometrieElement& aGeom = anElemMapIter->first;
235             MED::PElemInfo anElemInfo = anElemMapIter->second;
236             
237             switch(aGeom){
238             case MED::ePOLYGONE: {
239               MED::PPolygoneInfo aPolygoneInfo(anElemInfo);
240               MED::TInt aNbElem = aPolygoneInfo->GetNbElem();
241               MED::TElemNum aConn  = aPolygoneInfo->GetConnectivite();
242               MED::TElemNum aIndex = aPolygoneInfo->GetIndex();
243               MED::TInt aNbConn  = aConn.size();
244             
245               aMeshOnEntity->myNbCells += aNbElem;
246               
247               for (int ii = 0; ii<aNbElem ; ii++){
248                 int aNbConnii = aPolygoneInfo->GetNbConn(ii);
249                 aMeshOnEntity->myCellsSize += aNbConnii;
250               }
251
252               INITMSG(MYDEBUG,"aGeom = "<<aGeom<<"; aNbElem = "<<aNbElem<<
253                       "; myNbPolygones = "<<aNbElem<<
254                       "; nbConn= "<<aNbConn<<"\n");
255               
256               for(MED::TInt iElem = 0; iElem < aNbElem; iElem++){
257                 MED::TInt aFamId = aPolygoneInfo->GetFamNum(iElem);
258                 if(aFamId != 0){
259                   aFamilyNbCellsCounterMap[aFamId] += 1;
260                   aFamilyCellsSizeCounterMap[aFamId] += aPolygoneInfo->GetNbConn(iElem) + 1;
261                 }
262               }
263               break;
264             }
265             default: {
266               int aVNbNodes = VTKGeom2NbNodes(MEDGeomToVTK(aGeom));
267               MED::PCellInfo aCellInfo(anElemInfo);
268               MED::TInt aNbElem = aCellInfo->GetNbElem();
269               aMeshOnEntity->myNbCells += aNbElem;
270               aMeshOnEntity->myCellsSize += aNbElem*(aVNbNodes+1);
271               INITMSG(MYDEBUG,"aGeom = "<<aGeom<<"; aNbElem = "<<aNbElem<<
272                       "; myNbCells = "<<aMeshOnEntity->myNbCells<<
273                       "; myCellsSize = "<<aMeshOnEntity->myCellsSize<<"\n");
274             
275               for(MED::TInt iElem = 0; iElem < aNbElem; iElem++){
276                 MED::TInt aFamId = aCellInfo->GetFamNum(iElem);
277                 if(aFamId != 0){
278                   aFamilyNbCellsCounterMap[aFamId] += 1;
279                   aFamilyCellsSizeCounterMap[aFamId] += aVNbNodes + 1;
280                 }
281               }
282             }}
283           }
284         }
285       }
286
287       BEGMSG(MYDEBUG,"TFamilyByEntity:\n");
288
289       MED::TFamilyGroup aFamilyGroup = GetFamilies(aMed,aMeshInfo);
290
291       MED::TGroupInfo aGroupInfo = GetFamiliesByGroup(aFamilyGroup);
292
293       MED::TFamilyByEntity aFamilyByEntity = GetFamiliesByEntity(aMed,anElemGroup,aFamilyGroup);
294
295       MED::TFamilyByEntity::const_iterator aFamilyByEntityIter = aFamilyByEntity.begin();
296       for(; aFamilyByEntityIter != aFamilyByEntity.end(); aFamilyByEntityIter++){
297         const MED::EEntiteMaillage& aMEntity = aFamilyByEntityIter->first;
298         const MED::TFamilyGroup& aFamilyGroup = aFamilyByEntityIter->second;
299         
300         VISU::TEntity aVEntity = MEDEntityToVTK(aMEntity);
301         VISU::PMEDMeshOnEntity aMeshOnEntity = aMesh->myMeshOnEntityMap[aVEntity];
302         VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
303       
304         if(aFamilyGroup.empty())
305           continue;
306         
307         INITMSG(MYDEBUG,"aMEntity = "<<aMEntity<<"; aVEntity = "<<aVEntity<<"\n");
308         MED::TFamilyGroup::const_iterator aFamilyGroupIter = aFamilyGroup.begin();
309         for(; aFamilyGroupIter != aFamilyGroup.end(); aFamilyGroupIter++){
310           MED::PFamilyInfo aFamilyInfo = *aFamilyGroupIter;
311           if(aFamilyInfo->GetId() == 0) 
312             continue;
313           
314           std::string aFamilyName = aFamilyInfo->GetName();
315           VISU::PMEDFamily aFamily = aFamilyMap[aFamilyName](new TMEDFamily());
316           
317           aFamily->myId = aFamilyInfo->GetId();
318           aFamily->myName = aFamilyInfo->GetName();
319           aFamily->myEntity = aVEntity;
320           aFamily->myNbCells = aFamilyNbCellsCounterMap[aFamily->myId];
321           aFamily->myCellsSize = aFamilyCellsSizeCounterMap[aFamily->myId];
322           
323           INITMSG(MYDEBUG,"aFamilyName = '"<<aFamily->myName<<
324                   "'; myId = "<<aFamily->myId<<"; "<<
325                   "; aNbGroup = "<<aFamilyInfo->GetNbGroup()<<
326                   "; myEntity = "<<aFamily->myEntity<<
327                   "; myNbCells = "<<aFamily->myNbCells<<
328                   "; myCellsSize = "<<aFamily->myCellsSize<<"\n");
329           
330           VISU::TBindGroups& aBindGroups = aFamily->myGroups;
331           MED::TInt aNbGroup = aFamilyInfo->GetNbGroup();
332           for(MED::TInt i = 0; i < aNbGroup; i++){
333             std::string aGroupName = aFamilyInfo->GetGroupName(i);
334             aBindGroups.insert(aGroupName);
335             INITMSG(MYDEBUG,"aGroupName = '"<<aGroupName<<"'\n");
336           }
337         }
338       }
339       
340       BEGMSG(MYDEBUG,"VISU::TGroup:\n");
341
342       VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
343       MED::TGroupInfo::const_iterator aGroupInfoIter = aGroupInfo.begin();
344       for(; aGroupInfoIter != aGroupInfo.end(); aGroupInfoIter++){
345         const std::string& aGroupName = aGroupInfoIter->first;
346         const MED::TFamilyGroup& aFamilyGroup = aGroupInfoIter->second;
347         VISU::PMEDGroup aGroup(new TMEDGroup());
348         aGroup->myName = aGroupName;
349         aGroup->myMeshName = aMesh->myName;
350         
351         INITMSG(MYDEBUG,"aGroup->myName = '"<<aGroup->myName<<"'\n");
352         
353         MED::TFamilyGroup::const_iterator aFamilyIter = aFamilyGroup.begin();
354         for(; aFamilyIter != aFamilyGroup.end(); aFamilyIter++){
355           MED::PFamilyInfo aFamilyInfo = *aFamilyIter;
356           std::string aFamilyName = aFamilyInfo->GetName();
357           
358           VISU::TEntity aVEntity = TEntity(-1);
359           VISU::PFamily aFamily;
360           
361           // Find aVisuEntity
362           const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
363           VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityIter = aMeshOnEntityMap.begin();
364           for(; aMeshOnEntityIter != aMeshOnEntityMap.end(); aMeshOnEntityIter++){
365             const VISU::PMeshOnEntity& aMeshOnEntity = aMeshOnEntityIter->second;
366             const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
367             VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
368             for (; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
369               const std::string& aName = aFamilyMapIter->first;
370               aFamily = aFamilyMapIter->second;
371               if(aName == aFamilyName){
372                 aVEntity = aFamily->myEntity;
373                 goto exit_lable;
374               }
375             }
376           }
377         exit_lable:
378           if(aFamily && aVEntity >= 0){
379             aGroup->myFamilyAndEntitySet.insert(TFamilyAndEntity(aFamilyName,aVEntity));
380             INITMSG(MYDEBUG,"aFamilyName = '"<<aFamilyName<<"'; '"<<aFamily->myName<<"'; aVEntity = "<<aVEntity<<"\n");
381             
382             aGroup->myNbCells += aFamily->myNbCells;
383             aGroup->myCellsSize += aFamily->myCellsSize;
384           }
385         }
386         if(!aGroup->myFamilyAndEntitySet.empty() && aGroup->myNbCells > 0){
387           BEGMSG(MYDEBUG,"myNbCells = "<<aGroup->myNbCells<<
388                  "; myCellsSize = "<<aGroup->myCellsSize<<"\n\n");
389           aGroupMap.insert(VISU::TGroupMap::value_type(aGroupName,aGroup));
390         }
391       }
392       
393       MED::TInt aNbFields = aMed->GetNbFields(); 
394       BEGMSG(MYDEBUG,"VISU::TField aNbFields = "<<aNbFields<<"\n");
395       for(MED::TInt iField = 1; iField <= aNbFields; iField++){
396         MED::PFieldInfo aFieldInfo = aMed->GetPFieldInfo(aMeshInfo,iField);
397         MED::TInt aNbComp = aFieldInfo->GetNbComp();
398         std::string aFieldName = aFieldInfo->GetName();
399         
400         MED::TGeom aTGeom;
401         MED::EEntiteMaillage aMEntity;
402         MED::TInt aNbTimeStamps = aMed->GetNbTimeStamps(aFieldInfo,aEntityInfo,aMEntity,aTGeom);
403         VISU::TEntity aVEntity = MEDEntityToVTK(aMEntity);
404         VISU::PMeshOnEntity aMeshOnEntity = aMesh->myMeshOnEntityMap[aVEntity];
405         VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
406         VISU::PMEDField aField = aFieldMap[aFieldName](new TMEDField());
407         aField->myId = iField;
408         aField->myNbComp = aNbComp;
409         aField->myEntity = aVEntity;
410         aField->myName = aFieldName;
411         aField->myMeshName = aMeshName;
412         aField->myDataSize = aMeshOnEntity->myNbCells * aNbComp;
413         aField->myCompNames.resize(aNbComp);
414         aField->myUnitNames.resize(aNbComp);
415
416         INITMSG(MYDEBUG,"myName = '"<<aField->myName<<
417                 "'; myId = "<<aField->myId<<
418                 "; myEntity = "<<aField->myEntity<<
419                 "; myDataSize = "<<aField->myDataSize<<
420                 "; myNbComp = "<<aField->myNbComp<<"\n");
421         
422         for(MED::TInt iComp = 0; iComp < aNbComp; iComp++){
423           aField->myCompNames[iComp] = aFieldInfo->GetCompName(iComp);
424           aField->myUnitNames[iComp] = aFieldInfo->GetUnitName(iComp);
425         }
426         
427         for(MED::TInt iTimeStamp = 1; iTimeStamp <= aNbTimeStamps; iTimeStamp++){
428           MED::TErr anErr;
429           MED::PTimeStampInfo aTimeStamp = aMed->GetPTimeStampInfo(aFieldInfo,
430                                                                    aMEntity,
431                                                                    aTGeom,
432                                                                    iTimeStamp,
433                                                                    &anErr);
434           if(anErr < 0)
435             continue;
436           
437           MED::PTimeStampVal aTimeStampVal = aMed->GetPTimeStampVal(aTimeStamp,&anErr);
438           
439           MED::TFloat aDt = aTimeStamp->GetDt();
440           std::string anUnitDt = aTimeStamp->GetUnitDt();
441           VISU::TValField& aValField = aField->myValField;
442           VISU::PMEDValForTime aValForTime = aValField[iTimeStamp](new TMEDValForTime());
443           aValForTime->myId = iTimeStamp;
444           aValForTime->myFieldName = aField->myName;
445           aValForTime->myEntity = aField->myEntity;
446           aValForTime->myMeshName = aField->myMeshName;
447           aValForTime->myNbComp = aField->myNbComp;
448           aValForTime->myTime = VISU::TTime(aDt,anUnitDt);
449           INITMSG(MYDEBUG,"aDt = "<<aDt<<", "<<anUnitDt<<"\n");
450         }
451       }
452     }catch (std::exception& exc){
453       MSG(MYDEBUG,"Follow exception was occured in:\n"<<exc.what());
454     }catch(...){
455       EXCEPTION(runtime_error,"Unknown exception !!!");
456     }
457   }
458   return this; 
459 }
460
461 int VISU_MedConvertor::LoadMeshOnEntity(VISU::PMeshOnEntityImpl theMeshOnEntity, 
462                                         const string& theFamilyName)
463 {
464   MED::PWrapper aMed = MED::CrWrapper(myFileInfo.absFilePath().latin1());
465   const std::string& aMeshName = theMeshOnEntity->myMeshName;
466   const VISU::TEntity& anEntity = theMeshOnEntity->myEntity;
467   VISU::PMeshImpl aMesh = myMeshMap[aMeshName];
468   int isPointsUpdated;
469   if(anEntity == VISU::NODE_ENTITY) 
470     isPointsUpdated = LoadPoints(aMed,aMesh,theFamilyName);
471   else
472     isPointsUpdated = LoadPoints(aMed,aMesh);
473   int isCellsOnEntityUpdated = LoadCellsOnEntity(aMed,aMesh,theMeshOnEntity,theFamilyName);
474
475   return (isPointsUpdated || isCellsOnEntityUpdated);
476 }
477
478 int VISU_MedConvertor::LoadMeshOnGroup(VISU::PMeshImpl theMesh, 
479                                        const VISU::TFamilyAndEntitySet& theFamilyAndEntitySet)
480 {
481   MED::PWrapper aMed = MED::CrWrapper(myFileInfo.absFilePath().latin1());
482   int isPointsUpdated = 0, isCellsOnEntityUpdated = 0;
483   VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = theFamilyAndEntitySet.begin();
484   for(; aFamilyAndEntitySetIter != theFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
485     const std::string& aFamilyName = aFamilyAndEntitySetIter->first;
486     const VISU::TEntity& anEntity = aFamilyAndEntitySetIter->second;
487     const VISU::PMEDMeshOnEntity aMeshOnEntity = theMesh->myMeshOnEntityMap[anEntity];
488     if(anEntity == VISU::NODE_ENTITY){
489       isPointsUpdated += LoadPoints(aMed,theMesh,aFamilyName);
490       isCellsOnEntityUpdated += LoadCellsOnEntity(aMed,theMesh,aMeshOnEntity);
491     }else{
492       isPointsUpdated += LoadPoints(aMed,theMesh);
493       isCellsOnEntityUpdated += LoadCellsOnEntity(aMed,theMesh,aMeshOnEntity,aFamilyName);
494     }
495   }
496
497   return (isPointsUpdated || isCellsOnEntityUpdated);
498 }
499
500 int VISU_MedConvertor::LoadFieldOnMesh(VISU::PMeshImpl theMesh, 
501                                        VISU::PMeshOnEntityImpl theMeshOnEntity, 
502                                        VISU::PFieldImpl theField, 
503                                        VISU::PValForTimeImpl theValForTime)
504 {
505   MED::PWrapper aMed = MED::CrWrapper(myFileInfo.absFilePath().latin1());
506   int isPointsUpdated = LoadPoints(aMed,theMesh);
507   int isCellsOnEntityUpdated = LoadCellsOnEntity(aMed,theMesh,theMeshOnEntity);
508   int isFieldUpdated = LoadField(aMed,theMesh,theMeshOnEntity,theField,theValForTime);
509   
510   return (isPointsUpdated || isCellsOnEntityUpdated || isFieldUpdated);
511 }
512
513
514 int 
515 VISU_MedConvertor::LoadPoints(const MED::PWrapper& theMed,
516                               VISU::PMEDMesh theMesh, 
517                               const string& theFamilyName) 
518 {
519   try{
520     //Check on existing family
521     VISU::PMEDMeshOnEntity aMeshOnEntity = theMesh->myMeshOnEntityMap[VISU::NODE_ENTITY];
522     aMeshOnEntity->myEntity = VISU::NODE_ENTITY;
523     aMeshOnEntity->myMeshName = theMesh->myName;
524     VISU::PFamilyImpl aFamily = GetFamily(aMeshOnEntity,theFamilyName);
525     //Check on loading already done
526     bool isPointsLoaded = !theMesh->myPointsCoord.empty();
527     if(isPointsLoaded) 
528       if(!aFamily) 
529         return 0;
530       else if(!aFamily->mySubMesh.empty()) 
531         return 0;
532
533     INITMSG(MYDEBUG,"LoadPoints - isPointsLoaded = "<<isPointsLoaded<<"; theFamilyName = '"<<theFamilyName<<"'\n");
534     
535     //Main part of code
536     MED::PNodeInfo aNodeInfo = theMed->GetPNodeInfo(theMesh->myMeshInfo);
537     MED::TInt aNbElem = aNodeInfo->GetNbElem();
538
539     if(!isPointsLoaded){
540       VISU::TMeshImpl::TPointsDim&  aPointsDim = theMesh->myPointsDim;
541       aPointsDim.resize(theMesh->myDim);
542       for(int iDim = 0; iDim < theMesh->myDim; iDim++)
543         aPointsDim[iDim] = aNodeInfo->GetCoordName(iDim);
544       
545       VISU::TMeshImpl::TPointsCoord& aPointsCoord = theMesh->myPointsCoord;
546       aPointsCoord.resize(aNbElem*theMesh->myDim);
547       for(int iElem = 0; iElem < aNbElem; iElem++) 
548         for(int iDim = 0, iElem2Dim = iElem*theMesh->myDim; iDim < theMesh->myDim; iDim++, iElem2Dim++)
549           aPointsCoord[iElem2Dim] = aNodeInfo->GetNodeCoord(iElem,iDim);
550       
551       VISU::TMeshOnEntityImpl::TConnForCellType& aConnForCellType = aMeshOnEntity->myCellsConn[VTK_VERTEX];
552       aConnForCellType.resize(aNbElem);
553       for (int iElem = 0; iElem < aNbElem; iElem++)
554         aConnForCellType[iElem] = TMeshOnEntityImpl::TConnect(1,iElem);
555     }
556     if(aFamily && aNbElem > 0){
557       VISU::TFamilyImpl::TSubMeshOnCellType& aSubMeshOnCellType = aFamily->mySubMesh[VTK_VERTEX];
558       for (int iElem = 0; iElem < aNbElem; iElem++) 
559         if(aNodeInfo->GetFamNum(iElem) == aFamily->myId)
560           aSubMeshOnCellType.insert(iElem);
561     }
562     return 1;
563   }catch(std::exception& exc){
564     MSG(MYDEBUG,"Follow exception was occured :\n"<<exc.what());
565     theMesh->myPointsCoord.clear();
566     throw;
567   }catch(...){
568     theMesh->myPointsCoord.clear();
569     EXCEPTION(runtime_error,"Unknown exception !!!");
570   }
571   return 0;
572 }
573
574
575 int 
576 VISU_MedConvertor::LoadCellsOnEntity(const MED::PWrapper& theMed, 
577                                      VISU::PMEDMesh theMesh,
578                                      VISU::PMEDMeshOnEntity theMeshOnEntity,
579                                      const string& theFamilyName)
580 {
581   try{
582     //Check on existing family
583     VISU::PFamilyImpl aFamily = GetFamily(theMeshOnEntity,theFamilyName);
584     //Check on loading already done
585     bool isCellsLoaded = !theMeshOnEntity->myCellsConn.empty();
586     if(isCellsLoaded)
587       if(!aFamily) 
588         return 0;
589       else if(!aFamily->mySubMesh.empty()) 
590         return 0;
591
592     INITMSG(MYDEBUG,"LoadCellsOnEntity - theFamilyName = '"<<theFamilyName<<"'\n");
593     BEGMSG(MYDEBUG,"LoadCellsOnEntity - isCellsLoaded = "<<isCellsLoaded<<"; isFamilyPresent = "<<bool(aFamily)<<endl);
594
595     const VISU::TEntity& aVEntity = theMeshOnEntity->myEntity;
596     const MED::EEntiteMaillage& aMEntity = VTKEntityToMED(aVEntity);
597
598     MED::PMeshInfo& aMeshInfo = theMesh->myMeshInfo;
599     MED::PNodeInfo aNodeInfo = theMed->GetPNodeInfo(aMeshInfo);
600     MED::TInt aNbPoints = aNodeInfo->GetNbElem();
601
602     std::map<TInt,TInt> aNodeIdMap;
603 #ifdef _EDF_NODE_IDS_
604     MED::EBooleen anIsNodeNum = MED::eFAUX;
605 #else
606     MED::EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
607     if(anIsNodeNum){
608       for(MED::TInt i = 0; i < aNbPoints; i++){
609         aNodeIdMap[aNodeInfo->GetElemNum(i)-1] = i;
610       }
611     }
612 #endif
613
614     const MED::TGeom& aTGeom = theMeshOnEntity->myGeom;
615     MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
616     VISU::TMeshOnEntityImpl::TCellsConn& aCellsConn = theMeshOnEntity->myCellsConn;
617
618     for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
619       const MED::EGeometrieElement& aGeom = anTGeomIter->first;
620       int aVTKGeomType = MEDGeomToVTK(aGeom);
621       switch(aGeom){
622       case MED::ePOLYGONE: {
623         MED::PPolygoneInfo aPolygoneInfo = theMed->GetPPolygoneInfo(aMeshInfo,aMEntity,aGeom);
624         MED::TInt aNbElem = aPolygoneInfo->GetNbElem();
625         
626         if(!isCellsLoaded){
627           VISU::TMeshOnEntityImpl::TConnForCellType& aConnForPolygoneType = aCellsConn[aVTKGeomType];
628           aConnForPolygoneType.resize(aNbElem);
629           
630           int aMNbNodes = aPolygoneInfo->GetConnDim();
631           
632           vector<MED::TInt> aConnect(aMNbNodes);
633           vector<MED::TInt> aIndex = aPolygoneInfo->GetIndex();
634           
635           for(int iElem = 0; iElem < aNbElem; iElem++){
636             VISU::TMeshOnEntityImpl::TConnect& anArray = aConnForPolygoneType[iElem];
637             int aNbConn = aPolygoneInfo->GetNbConn(iElem);
638             
639             anArray.resize(aNbConn);
640             
641             aConnect = aPolygoneInfo->GetConnectivite();
642             
643             for(int i = 0 ; i < aNbConn; i++){
644               anArray[i] = aConnect[aIndex[iElem]-1+i]-1;
645             }
646           }
647         }
648         if(aFamily){
649           VISU::TFamilyImpl::TSubMeshOnCellType& aSubMeshOnCellType = aFamily->mySubMesh[aVTKGeomType];
650           for(int iElem = 0; iElem < aNbElem; iElem++) 
651             if(aPolygoneInfo->GetFamNum(iElem) == aFamily->myId)
652               aSubMeshOnCellType.insert(iElem);
653         }
654         break;
655       }
656       default: {
657         int aVNbNodes = VTKGeom2NbNodes(aVTKGeomType);
658         
659         MED::PCellInfo aCellInfo = theMed->GetPCellInfo(aMeshInfo,aMEntity,aGeom);
660         MED::TInt aNbElem = aCellInfo->GetNbElem();
661         
662         if(!isCellsLoaded){
663           VISU::TMeshOnEntityImpl::TConnForCellType& aConnForCellType = aCellsConn[aVTKGeomType];
664           aConnForCellType.resize(aNbElem);
665           
666           int aMNbNodes = MEDGeom2NbNodes(aGeom);
667           vector<MED::TInt> aConnect(aMNbNodes);
668           
669           for(int iElem = 0; iElem < aNbElem; iElem++) {
670             TMeshOnEntityImpl::TConnect& anArray = aConnForCellType[iElem];
671             anArray.resize(aVNbNodes);
672             
673             if(anIsNodeNum){
674               for(int i = 0; i < aMNbNodes; i++){
675                 aConnect[i] = aNodeIdMap[aCellInfo->GetConn(iElem,i)-1];
676               }
677             }else{
678               for(int i = 0; i < aMNbNodes; i++){
679                 aConnect[i] = aCellInfo->GetConn(iElem,i)-1;
680               }
681             }
682             
683             switch(aGeom){
684             case MED::eTETRA4:
685             case MED::eTETRA10:
686               anArray[0] = aConnect[0];
687               anArray[1] = aConnect[1];
688               anArray[2] = aConnect[3];  
689               anArray[3] = aConnect[2];  
690               break;
691             case MED::ePYRA5:
692             case MED::ePYRA13:
693               anArray[0] = aConnect[0];
694               anArray[1] = aConnect[3];  
695               anArray[2] = aConnect[2];
696               anArray[3] = aConnect[1];  
697               anArray[4] = aConnect[4];
698               break;
699             default:
700               for(int iNode = 0; iNode < aVNbNodes; iNode++) 
701                 anArray[iNode] = aConnect[iNode];
702             }
703             for(int iNode = 0; iNode < aVNbNodes; iNode++) 
704               if(anArray[iNode] < 0 || aNbPoints <= anArray[iNode])
705                 EXCEPTION(runtime_error,"ImportCells >> aNbPoints("<<aNbPoints<<") "<<
706                           "<= anArray["<<iElem<<"]"<<
707                           "["<<iNode<<"]"<<
708                           "("<<anArray[iNode]<<") < 0");
709           }
710         }
711         //Filling aFamily SubMesh
712         if(aFamily){
713           VISU::TFamilyImpl::TSubMeshOnCellType& aSubMeshOnCellType = aFamily->mySubMesh[aVTKGeomType];
714           for(int iElem = 0; iElem < aNbElem; iElem++) 
715             if(aCellInfo->GetFamNum(iElem) == aFamily->myId)
716               aSubMeshOnCellType.insert(iElem);
717         }
718       }}
719     }
720     return 1;
721   }catch(std::exception& exc){
722     theMeshOnEntity->myCellsConn.clear();
723     throw;
724   }catch(...){
725     theMeshOnEntity->myCellsConn.clear();
726     EXCEPTION(runtime_error,"Unknown exception !!!");
727   }
728   return 0;
729 }
730
731
732 int 
733 VISU_MedConvertor::LoadField(const MED::PWrapper& theMed,
734                              VISU::PMEDMesh theMesh,
735                              VISU::PMEDMeshOnEntity theMeshOnEntity,
736                              VISU::PMEDField theField, 
737                              VISU::PMEDValForTime theValForTime)
738 {
739   //Check on loading already done
740   if(!theValForTime->myValForCells.empty()) return 0;
741
742   //Main part of code
743   MED::PMeshInfo aMeshInfo = theMesh->myMeshInfo;
744   MED::TInt anId = theField->myId;
745   MED::PFieldInfo aFieldInfo = theMed->GetPFieldInfo(aMeshInfo,anId);
746
747   MED::TGeom aTGeom;
748   MED::EEntiteMaillage aMEntity;
749   const MED::TEntityInfo& anEntityInfo = theMesh->myEntityInfo;
750   theMed->GetNbTimeStamps(aFieldInfo,anEntityInfo,aMEntity,aTGeom);
751
752   anId = theValForTime->myId;
753   MED::PTimeStampInfo aTimeStampInfo = 
754     theMed->GetPTimeStampInfo(aFieldInfo,aMEntity,aTGeom,anId);
755
756   MED::PTimeStampVal aTimeStampVal = theMed->GetPTimeStampVal(aTimeStampInfo);
757
758   MED::TInt aNbGauss = aTimeStampInfo->GetNbGauss();
759   MED::TInt aNbComp = theField->myNbComp;
760
761   const MED::TGeom& anEntityTGeom = theMeshOnEntity->myGeom;
762   MED::TGeom::const_iterator aTGeomIter = anEntityTGeom.begin();
763   for(; aTGeomIter != anEntityTGeom.end(); aTGeomIter++){
764     const MED::EGeometrieElement& aGeom = aTGeomIter->first;
765     const MED::TInt& aNbElem = aTGeomIter->second;
766
767     INITMSG(MYDEBUG,"LoadField - aGeom = "<<aGeom<<"; aNbElem = '"<<aNbElem<<endl);
768
769     if(aTGeom.find(aGeom) == aTGeom.end()){
770       theField->myDataSize -= aNbElem*theField->myNbComp;
771       theField->myIsTrimmed = true;
772     }else{
773       int aVTKGeomType = MEDGeomToVTK(aGeom);
774       VISU::TValForTimeImpl::TValForCellsWithType& anArray = theValForTime->myValForCells[aVTKGeomType];
775       anArray.resize(aNbComp*aNbElem);
776       for(MED::TInt iElem = 0, anId = 0; iElem < aNbElem; iElem++){
777         for(MED::TInt iComp = 0; iComp < aNbComp; iComp++, anId++){
778           for(MED::TInt iGauss = 0; iGauss < aNbGauss; iGauss++){
779             anArray[anId] += aTimeStampVal->GetVal(aGeom,iElem,iComp,iGauss);
780           }
781           anArray[anId] /= aNbGauss;
782         }
783       }
784     }
785   }
786   return 1; 
787 }