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