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