Salome HOME
Debug visu.py; add visu_split_views.py as example of view parameters management
[modules/visu.git] / src / VISU_I / VISU_CorbaMedConvertor.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VISU_CorbaMedConvertor.cxx
25 //  Author : Alexey PETROV
26 //  Module : VISU
27 //  $Header$
28 //  Copyright (C) 2003  CEA/DEN, EDF R&D
29
30 #include "VISU_CorbaMedConvertor.hxx"
31
32 #include <vtkCellType.h>
33
34 #include <boost/tuple/tuple.hpp>
35
36 using namespace std;
37 using namespace VISU;
38
39 #define USER_INTERLACE MED_FULL_INTERLACE
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 extern "C" {
48   VISU_Convertor* 
49   CreateMEDConvertor(SALOMEDS::SObject_ptr theMedSObject) 
50   {
51     return new VISU_MEDConvertor(theMedSObject);
52   }
53
54   VISU_Convertor* 
55   CreateMEDFieldConvertor(SALOME_MED::FIELD_ptr theField) 
56   {
57     return new VISU_MEDFieldConvertor(theField);
58   }
59 }
60
61 namespace{
62   using namespace SALOME_MED;
63   
64   const int MED_NBR_GEOMETRIE_MAILLE = 15;
65   
66   medGeometryElement 
67   CELLGEOM[MED_NBR_GEOMETRIE_MAILLE] = {
68     MED_POINT1,
69     MED_SEG2,
70     MED_SEG3,
71     MED_TRIA3,
72     MED_QUAD4,
73     MED_TRIA6,
74     MED_QUAD8,
75     MED_TETRA4,
76     MED_PYRA5,
77     MED_PENTA6,
78     MED_HEXA8,
79     MED_TETRA10,
80     MED_PYRA13,
81     MED_PENTA15,
82     MED_HEXA20
83   };
84   
85   const int MED_NBR_GEOMETRIE_FACE = 4;
86   
87   medGeometryElement
88   FACEGEOM[MED_NBR_GEOMETRIE_FACE] = {
89     MED_TRIA3,
90     MED_QUAD4,
91     MED_TRIA6,
92     MED_QUAD8
93   };
94   
95   const int MED_NBR_GEOMETRIE_ARETE = 2;
96   
97   medGeometryElement
98   EDGEGEOM[MED_NBR_GEOMETRIE_ARETE] = {
99     MED_SEG2,
100     MED_SEG3
101   };
102   
103   const int MED_NBR_GEOMETRIE_NODE = 1;
104   
105   medGeometryElement
106   NODEGEOM[MED_NBR_GEOMETRIE_NODE] = {
107     MED_POINT1,
108   };
109   
110   int GetEntity2Geom(const VISU::TEntity& theEntity, medGeometryElement*& theVector)
111   {
112     switch(theEntity){
113     case CELL_ENTITY: theVector = CELLGEOM; return MED_NBR_GEOMETRIE_MAILLE; break;
114     case FACE_ENTITY: theVector = FACEGEOM; return MED_NBR_GEOMETRIE_FACE; break;
115     case EDGE_ENTITY: theVector = EDGEGEOM; return MED_NBR_GEOMETRIE_ARETE; break;
116     case NODE_ENTITY: theVector = NODEGEOM; return MED_NBR_GEOMETRIE_NODE; break;
117     }
118     return -1;
119   }
120   
121   int MEDGeom2NbNodes(int theMEDGeomType)
122   { 
123     switch(theMEDGeomType){
124     case MED_NONE: return 0;
125     case MED_POINT1: return 1;
126     case MED_SEG2: return 2;
127     case MED_SEG3: return 3;
128     case MED_TRIA3: return 3;
129     case MED_TRIA6: return 6;
130     case MED_QUAD4: return 4;
131     case MED_QUAD8: return 8;
132     case MED_TETRA4: return 4;
133     case MED_TETRA10: return 10;
134     case MED_HEXA8: return 8;
135     case MED_HEXA20: return 20;
136     case MED_PENTA6: return 6;
137     case MED_PENTA15: return 15;
138     case MED_PYRA5: return 5;
139     case MED_PYRA13: return 13;
140     }
141     return -1;
142   }
143   
144   int MEDGeomToVTK(medGeometryElement theMEDGeomType)
145   { 
146     switch(theMEDGeomType){
147     case MED_NONE: return VTK_EMPTY_CELL;
148     case MED_POINT1: return VTK_VERTEX;
149     case MED_SEG2: return VTK_LINE;
150     case MED_SEG3: return VTK_LINE;
151     case MED_TRIA3: return VTK_TRIANGLE;
152     case MED_TRIA6: return VTK_TRIANGLE;
153     case MED_QUAD4: return VTK_QUAD;
154     case MED_QUAD8: return VTK_QUAD;
155     case MED_TETRA4: return VTK_TETRA;
156     case MED_TETRA10: return VTK_TETRA;
157     case MED_HEXA8: return VTK_HEXAHEDRON;
158     case MED_HEXA20: return VTK_HEXAHEDRON;
159     case MED_PENTA6: return VTK_WEDGE;
160     case MED_PENTA15: return VTK_WEDGE;
161     case MED_PYRA5: return VTK_PYRAMID;
162     case MED_PYRA13: return VTK_PYRAMID;
163     }
164     return -1;
165   }
166   
167   int VTKGeom2NbNodes(int theVTKGeomType)
168   { 
169     switch(theVTKGeomType){
170     case VTK_VERTEX: return 1;
171     case VTK_LINE: return 2;
172     case VTK_TRIANGLE: return 3;
173     case VTK_QUAD: return 4;
174     case VTK_TETRA: return 4;
175     case VTK_HEXAHEDRON: return 8;
176     case VTK_WEDGE: return 6;
177     case VTK_PYRAMID: return 5;
178     }
179     return -1;
180   }
181   
182   medGeometryElement VTKGeomToMED(int theVTKGeomType)
183   { 
184     switch(theVTKGeomType){
185     case VTK_VERTEX: return MED_POINT1;
186     case VTK_LINE: return MED_SEG2;
187     case VTK_TRIANGLE: return MED_TRIA3;
188     case VTK_QUAD: return MED_QUAD4;
189     case VTK_TETRA: return MED_TETRA4;
190     case VTK_HEXAHEDRON: return MED_HEXA8;
191     case VTK_WEDGE: return MED_PENTA6;
192     case VTK_PYRAMID: return MED_PYRA5;
193     }
194     return medGeometryElement(-1);
195   }
196   
197   VISU::TEntity MEDEntityToVTK(medEntityMesh theMEDEntity)
198   {
199     switch(theMEDEntity){
200     case MED_NODE: return NODE_ENTITY;
201     case MED_EDGE: return EDGE_ENTITY;
202     case MED_FACE: return FACE_ENTITY;
203     case MED_CELL: return CELL_ENTITY;
204     }
205     return VISU::TEntity(-1);
206   }
207   
208   medEntityMesh VTKEntityToMED(VISU::TEntity theVTKEntity)
209   {
210     switch(theVTKEntity){
211     case NODE_ENTITY: return MED_NODE;
212     case EDGE_ENTITY: return MED_EDGE;
213     case FACE_ENTITY: return MED_FACE;
214     case CELL_ENTITY: return MED_CELL;
215     }
216     return medEntityMesh(-1);
217   }
218   
219   string GetSObjectName(SALOMEDS::SObject_ptr aSObject){
220     SALOMEDS::GenericAttribute_var anAttr;
221     if (aSObject->FindAttribute(anAttr,"AttributeName")) {
222       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
223       CORBA::String_var aString = aName->Value();
224       return aString.in();
225     }
226     return "";
227   }
228   
229   void 
230   GetCellsSize(vtkIdType& theNbCells, vtkIdType& theCellsSize,
231                SALOME_MED::MESH_ptr theMEDMesh,
232                const VISU::TEntity& theVEntity)
233   {
234     medGeometryElement* aGeomElems;
235     theNbCells = theCellsSize = 0;
236     int iGeomEnd = GetEntity2Geom(theVEntity,aGeomElems);
237     const medEntityMesh& aMEntity = VTKEntityToMED(theVEntity);
238     if(MYDEBUG) MESSAGE("GetCellsSize - theVEntity = "<<theVEntity);
239       for(int iGeom = 0; iGeom < iGeomEnd; iGeom++){
240         medGeometryElement aMEDGeom = aGeomElems[iGeom];
241         int iNumElemEnd = theMEDMesh->getNumberOfElements(aMEntity,aMEDGeom);
242         if(iNumElemEnd > 0){
243           if(MYDEBUG) MESSAGE("GetCellsSize - iNumElemEnd = "<<iNumElemEnd);
244           theCellsSize += iNumElemEnd*(MEDGeom2NbNodes(aMEDGeom) + 1);
245           theNbCells += iNumElemEnd;
246         }
247       }
248   }
249   
250   
251   void 
252   GetCellsSize(vtkIdType& theNbCells, vtkIdType& theCellsSize,
253                SALOME_MED::FAMILY_ptr theMEDFamily)
254   {
255     medGeometryElement_array_var aGeom = theMEDFamily->getTypes();
256     int iGeomEnd = aGeom->length();
257     theNbCells = theCellsSize = 0;
258     if(MYDEBUG) MESSAGE("GetCellsSize - iGeomEnd = "<<iGeomEnd);
259     for(int iGeom = 0; iGeom < iGeomEnd; iGeom++) {
260       medGeometryElement aMEDGeom = aGeom[iGeom];
261       long_array_var aCellNumForType = theMEDFamily->getNumber(aMEDGeom);
262       int iNumElemEnd = aCellNumForType->length();
263       if(iNumElemEnd > 0){
264         if(MYDEBUG) MESSAGE("GetCellsSize - iNumElemEnd = "<<iNumElemEnd);
265         theNbCells += iNumElemEnd;
266         theCellsSize += iNumElemEnd*(MEDGeom2NbNodes(aMEDGeom) + 1);
267       }
268     }
269   }
270   
271   
272   void
273   GetCellsSize(VISU::PCMesh theMesh, 
274                SALOME_MED::MESH_ptr theMEDMesh, 
275                const VISU::TEntity& theEntity)
276   {
277     TMeshOnEntityMap& aMeshOnEntityMap = theMesh->myMeshOnEntityMap;
278     VISU::PCMeshOnEntity aMeshOnEntity = aMeshOnEntityMap[theEntity];
279     if(theEntity == NODE_ENTITY){
280       theMesh->myNbPoints = theMEDMesh->getNumberOfNodes();
281       aMeshOnEntity->myNbCells = theMesh->myNbPoints;
282       aMeshOnEntity->myCellsSize = 2*theMesh->myNbPoints;
283       vtkIdType aNbCells, aCellsSize;
284       GetCellsSize(aNbCells,aCellsSize,theMEDMesh,CELL_ENTITY);
285       if(aNbCells > 0){
286         
287         TMeshOnEntityMap::iterator aIter = aMeshOnEntityMap.find(CELL_ENTITY);
288         if (aIter != aMeshOnEntityMap.end()){
289           VISU::PCMeshOnEntity aMeshOnCells = aIter->second;
290         
291           aMeshOnCells->myEntity = VISU::CELL_ENTITY;
292           aMeshOnCells->myMeshName = theMesh->myName;
293           aMeshOnCells->myNbCells = aNbCells;
294           aMeshOnCells->myCellsSize = aCellsSize;
295         }
296       }
297     }else{
298       GetCellsSize(aMeshOnEntity->myNbCells,aMeshOnEntity->myCellsSize,theMEDMesh,theEntity);
299     }
300   }
301   
302 }
303
304
305 VISU_Convertor* 
306 VISU_MEDFieldConvertor::Build()
307 {
308   if(myField->_is_nil()) 
309     throw std::runtime_error("VISU_MEDFieldConvertor::Build >> myField->_is_nil() !!!");
310   
311   SALOME_MED::SUPPORT_var aMEDSupport = myField->getSupport();
312   if(aMEDSupport->_is_nil()) 
313     throw std::runtime_error("VISU_MEDFieldConvertor::Build >> aMEDSupport->_is_nil() !!!");
314
315   SALOME_MED::medEntityMesh aMEntity = aMEDSupport->getEntity();
316   TEntity aVEntity = MEDEntityToVTK(aMEntity);
317   SALOME_MED::MESH_var aMEDMesh = aMEDSupport->getMesh();
318   if(aMEDMesh->_is_nil()) 
319     throw std::runtime_error("VISU_MEDFieldConvertor::Build >> aMEDMesh->_is_nil() !!!");
320
321   CORBA::String_var aMeshName = aMEDMesh->getName();
322   CORBA::String_var aFieldName = myField->getName();
323
324   PCMesh aMesh = myMeshMap[aMeshName.in()](new TCMesh());
325   aMesh->myDim = aMEDMesh->getSpaceDimension();
326   aMesh->myPointsDim.resize(aMesh->myDim);
327   aMesh->myName = aMeshName.in();
328   aMesh->myMesh = aMEDMesh;
329
330   if(MYDEBUG) MESSAGE("VISU_MEDFieldConvertor::Build - aMeshName = "<<aMeshName<<"; myDim = "<<aMesh->myDim);
331
332   TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
333   PCMeshOnEntity aMeshOnEntity = aMeshOnEntityMap[aVEntity](new TCMeshOnEntity());
334   aMeshOnEntity->myEntity = aVEntity;
335   aMeshOnEntity->myMeshName = aMeshName.in();
336   aMeshOnEntity->mySupport = aMEDSupport;
337
338   if(aVEntity == NODE_ENTITY){
339     PCMeshOnEntity aMeshOnEntity2 = aMeshOnEntityMap[CELL_ENTITY](new TCMeshOnEntity());
340     *aMeshOnEntity2 = *aMeshOnEntity;
341     aMeshOnEntity->myEntity = CELL_ENTITY;
342     GetCellsSize(aMesh,aMEDMesh,CELL_ENTITY);
343   }else{
344     PCMeshOnEntity aMeshOnEntity2 = aMeshOnEntityMap[NODE_ENTITY](new TCMeshOnEntity());
345     *aMeshOnEntity2 = *aMeshOnEntity;
346     aMeshOnEntity->myEntity = NODE_ENTITY;
347     GetCellsSize(aMesh,aMEDMesh,NODE_ENTITY);
348   }
349   GetCellsSize(aMesh,aMEDMesh,aVEntity);
350
351   TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
352   PCField aField = aFieldMap[aFieldName.in()](new TCField());
353   aField->myId = myField->getOrderNumber();
354   aField->myName = aFieldName.in();
355   aField->myEntity = aVEntity;
356   aField->myMeshName = aMeshName.in();
357   aField->myNbComp = myField->getNumberOfComponents();
358   aField->myDataSize = aMeshOnEntity->myNbCells * aField->myNbComp;
359   aField->myCompNames.resize(aField->myNbComp);
360   aField->myUnitNames.resize(aField->myNbComp);
361
362   if(MYDEBUG) MESSAGE("VISU_MEDFieldConvertor::Build - aMeshName = "<<aMeshName<<"; myDim = "<<aMesh->myDim);
363
364   TValField& aValField = aField->myValField;
365   int anId = myField->getIterationNumber();
366   PCValForTime aValForTime = aValField[anId](new TCValForTime());
367   aValForTime->myId = anId;
368   CORBA::Double aDT = myField->getTime();
369   aValForTime->myTime = TTime(aDT,"");
370   aValForTime->myField = myField;
371
372   if(MYDEBUG) 
373     MESSAGE("VISU_MEDFieldConvertor::Build - aFieldName = '"<<aFieldName<<
374             "'; myId = "<<anId<<"; myTime = "<<aDT);
375
376   return this;
377 }
378
379
380 VISU_Convertor* 
381 VISU_MEDConvertor::Build() 
382 {
383   if(mySObject->_is_nil()) 
384     throw std::runtime_error("VISU_MEDConvertor::Build >> mySObject->_is_nil() !!!");
385   SALOMEDS::Study_var aStudy = mySObject->GetStudy();
386
387   CORBA::Object_var aMedObject = VISU::SObjectToObject(mySObject);
388   if(!CORBA::is_nil(aMedObject)){
389     SALOME_MED::MED_var aMED = SALOME_MED::MED::_narrow(aMedObject);
390     return Build(aMED);
391   }
392
393   SALOMEDS::ChildIterator_var aTimeStampIterator = aStudy->NewChildIterator(mySObject);
394   return Build(aTimeStampIterator);
395 }
396
397 namespace{
398
399   using namespace boost;
400
401   struct TSObjectByName{
402     std::string myName;
403     typedef tuple<SALOMEDS::SObject_var> TRet;
404
405     TSObjectByName(const std::string& theName):
406       myName(theName)
407     {}
408
409     TRet operator()(SALOMEDS::SObject_ptr theSObj, bool& theIsSuccess)
410     {
411       SALOMEDS::GenericAttribute_var anAttr;
412       if(theSObj->FindAttribute(anAttr,"AttributeName")){
413         SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
414         CORBA::String_var aValue = aName->Value();
415         theIsSuccess = (myName == aValue.in());
416         if(theIsSuccess)
417           return TRet(SALOMEDS::SObject::_duplicate(theSObj));
418       }
419       return TRet();
420     }
421
422   };
423
424   struct TMeshByName{
425     std::string myName;
426     typedef tuple<SALOME_MED::MESH_var,SALOMEDS::SObject_var> TRet;
427
428     TMeshByName(const std::string& theName):
429       myName(theName)
430     {}
431
432     TRet operator()(SALOMEDS::SObject_ptr theSObj, bool& theIsSuccess)
433     {
434       CORBA::Object_var anObj = VISU::SObjectToObject(theSObj);
435       if(!CORBA::is_nil(anObj)){
436         SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(anObj);
437         if(!CORBA::is_nil(aMesh)){
438           CORBA::String_var aName = aMesh->getName();
439           theIsSuccess = (myName == aName.in());
440           if(theIsSuccess)
441             return TRet(aMesh,SALOMEDS::SObject::_duplicate(theSObj));
442         }
443       }
444       return TRet();
445     }
446   };
447
448   template<typename TFun>
449   typename TFun::TRet
450   Find(SALOMEDS::SObject_ptr theStartSObj, 
451        SALOMEDS::Study_ptr theStudy,
452        TFun theFun,
453        bool& theIsSuccess,
454        bool theIsAllLevels = true)
455   {
456     SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator(theStartSObj);
457     anIter->InitEx(theIsAllLevels);
458     for(; anIter->More(); anIter->Next()){
459       SALOMEDS::SObject_var aSObj = anIter->Value();
460       typename TFun::TRet aRet = theFun(aSObj,theIsSuccess);
461       if(theIsSuccess)
462         return aRet;
463     }
464     return typename TFun::TRet();
465   }
466
467 }
468
469 VISU_Convertor* 
470 VISU_MEDConvertor::Build(SALOME_MED::MED_ptr theMED)
471 {
472   if(CORBA::is_nil(theMED)) 
473     return NULL;
474
475   CORBA::Long aNbMeshes = theMED->getNumberOfMeshes();
476   SALOME_MED::string_array_var aMeshNames = theMED->getMeshNames();
477   if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aNbMeshes = "<<aNbMeshes);
478
479   SALOMEDS::Study_var aStudy = mySObject->GetStudy();
480   SALOMEDS::SObject_var aMedCompSObj = mySObject->GetFather();
481
482   bool anIsSuccess = false;
483   TSObjectByName::TRet aSObjectByNameRet = 
484     Find(aMedCompSObj,aStudy,TSObjectByName("MEDMESH"),anIsSuccess);
485   if(MYDEBUG) 
486     MESSAGE("VISU_MEDConvertor::Build - Find ('"<<"MEDMESH"<<"') = "<<anIsSuccess);
487   if(anIsSuccess){
488     SALOMEDS::SObject_var aMeshesSObj = boost::get<0>(aSObjectByNameRet);
489     for(int iMesh = 0; iMesh < aNbMeshes; iMesh++){
490       anIsSuccess = false;
491       CORBA::String_var aMeshName = aMeshNames[iMesh];
492       TMeshByName::TRet aMeshByNameRet = 
493         Find(aMeshesSObj,aStudy,TMeshByName(aMeshName.in()),anIsSuccess);
494       if(MYDEBUG) 
495         MESSAGE("VISU_MEDConvertor::Build - Find aMeshName('"<<aMeshName.in()<<"') = "<<anIsSuccess);
496       if(!anIsSuccess)
497         continue;
498
499       PCMesh aMesh = myMeshMap[aMeshName.in()](new TCMesh());
500       SALOME_MED::MESH_var aMEDMesh = boost::get<0>(aMeshByNameRet);
501       aMesh->myDim = aMEDMesh->getSpaceDimension();
502       aMesh->myName = aMeshName.in();
503       aMesh->myPointsDim.resize(aMesh->myDim);
504       aMesh->myMesh = aMEDMesh;
505
506       if(MYDEBUG) 
507         MESSAGE("VISU_MEDConvertor::Build - aMeshName = "<<aMeshName<<"; myDim = "<<aMesh->myDim);
508
509       std::string aName = aMeshName.in();
510       std::replace(aName.begin(),aName.end(),' ','_');
511
512       anIsSuccess = false;
513       std::ostringstream aStream;
514       aStream<<"MEDSUPPORTS_OF_"<<aName;
515       std::string aSupportsName(aStream.str());
516       TSObjectByName::TRet aSObjectByNameRet = 
517         Find(aMeshesSObj,aStudy,TSObjectByName(aSupportsName.c_str()),anIsSuccess);
518       if(MYDEBUG) 
519         MESSAGE("VISU_MEDConvertor::Build - Find aSupportsName('"<<aSupportsName<<"') = "<<anIsSuccess);
520       if(!anIsSuccess)
521         continue;
522
523       TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
524       SALOMEDS::SObject_var aSupportsSObj = boost::get<0>(aSObjectByNameRet);
525       SALOMEDS::ChildIterator_var aSupportIterator = aStudy->NewChildIterator(aSupportsSObj);
526
527       // Fill all MeshOnEntity
528       aSupportIterator->InitEx(true);
529       for(; aSupportIterator->More(); aSupportIterator->Next()){
530         SALOMEDS::SObject_var aSupportSObj = aSupportIterator->Value();
531         
532         CORBA::Object_var aMedSupport = VISU::SObjectToObject(aSupportSObj);
533         if(CORBA::is_nil(aMedSupport)) 
534           continue;
535         
536         SALOME_MED::SUPPORT_var aMEDSupport = SALOME_MED::SUPPORT::_narrow(aMedSupport); 
537         if(aMEDSupport->_is_nil()) 
538           continue;
539         
540         SALOME_MED::MESH_var aMeshOnSupport = aMEDSupport->getMesh();
541         SALOME_MED::medEntityMesh aMEntity = aMEDSupport->getEntity();
542         VISU::TEntity aVEntity = MEDEntityToVTK(aMEntity);
543         CORBA::String_var aSupportName = aMEDSupport->getName();
544         
545         if(aMEDSupport->isOnAllElements() && strcmp(aSupportName.in(),"SupportOnAll_MED_") > 0){
546           if(MYDEBUG) 
547             MESSAGE("VISU_MEDConvertor::Build - Support isOnAllElements = '"<<aSupportName<<
548                     "' aVEntity = "<<aVEntity);
549           int aNbCells, aCellsSize;
550           //Check, if there is any data on the support?
551           if(aVEntity == NODE_ENTITY){
552             aMesh->myNbPoints = aMeshOnSupport->getNumberOfNodes();
553             aNbCells = aMesh->myNbPoints;
554             aCellsSize = 2*aMesh->myNbPoints;
555           }else
556             GetCellsSize(aNbCells,aCellsSize,aMeshOnSupport,aVEntity);
557           
558           if(aNbCells > 0){
559             PCMeshOnEntity aMeshOnEntity = aMeshOnEntityMap[aVEntity](new TCMeshOnEntity());
560             aMeshOnEntity->myMeshName = aMeshName.in();
561             aMeshOnEntity->myEntity = aVEntity;
562             aMeshOnEntity->myNbCells = aNbCells;
563             aMeshOnEntity->myCellsSize = aCellsSize;
564             aMeshOnEntity->mySupport = aMEDSupport;
565           }
566         }
567       }
568
569       // Fill all Family
570       aSupportIterator->InitEx(true);
571       for(; aSupportIterator->More(); aSupportIterator->Next()){
572         SALOMEDS::SObject_var aSupportSObj = aSupportIterator->Value();
573         
574         CORBA::Object_var aMedSupport = VISU::SObjectToObject(aSupportSObj);
575         if(CORBA::is_nil(aMedSupport)) 
576           continue;
577         
578         SALOME_MED::SUPPORT_var aMEDSupport = SALOME_MED::SUPPORT::_narrow(aMedSupport); 
579         if(aMEDSupport->_is_nil()) 
580           continue;
581         
582         SALOME_MED::MESH_var aMeshOnSupport = aMEDSupport->getMesh();
583         SALOME_MED::medEntityMesh aMEntity = aMEDSupport->getEntity();
584         VISU::TEntity aVEntity = MEDEntityToVTK(aMEntity);
585         CORBA::String_var aSupportName = aMEDSupport->getName();
586         
587         SALOME_MED::FAMILY_var aMEDFamily = SALOME_MED::FAMILY::_narrow(aMedSupport);
588         if(!aMEDFamily->_is_nil()) {
589           TMeshOnEntityMap::iterator aMeshOnEntityMapIter = aMeshOnEntityMap.find(aVEntity);
590           if(aMeshOnEntityMapIter == aMeshOnEntityMap.end())
591             continue;
592           PCMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
593
594           int aNbCells = aMeshOnEntity->myNbCells, aCellsSize = aMeshOnEntity->myCellsSize;
595           CORBA::Boolean anIsOnAllElements = aMEDSupport->isOnAllElements();
596           if(!anIsOnAllElements)
597             GetCellsSize(aNbCells,aCellsSize,aMEDFamily);
598
599           if(MYDEBUG) 
600             MESSAGE("VISU_MEDConvertor::Build - aFamily = '"<<aSupportName<<
601                     "'; anIsOnAllElements = "<<anIsOnAllElements<<
602                     "; aVEntity = "<<aVEntity<<
603                     "; aNbCells = "<<aNbCells);
604
605           if(aNbCells > 0){
606             TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
607             PCFamily aFamily = aFamilyMap[aSupportName.in()](new TCFamily());
608             aFamily->myName = aSupportName.in();
609             aFamily->myEntity = aVEntity;
610             aFamily->myNbCells = aNbCells;
611             aFamily->myCellsSize = aCellsSize;
612             aFamily->myId = aMEDFamily->getIdentifier();
613             aFamily->myFamily = aMEDFamily;
614           }
615         }
616       }
617         
618       // Fill all Groups
619       aSupportIterator->InitEx(true);
620       for(; aSupportIterator->More(); aSupportIterator->Next()){
621         SALOMEDS::SObject_var aSupportSObj = aSupportIterator->Value();
622         
623         CORBA::Object_var aMedSupport = VISU::SObjectToObject(aSupportSObj);
624         if(CORBA::is_nil(aMedSupport)) 
625           continue;
626         
627         SALOME_MED::SUPPORT_var aMEDSupport = SALOME_MED::SUPPORT::_narrow(aMedSupport); 
628         if(aMEDSupport->_is_nil()) 
629           continue;
630         
631         SALOME_MED::MESH_var aMeshOnSupport = aMEDSupport->getMesh();
632         SALOME_MED::medEntityMesh aMEntity = aMEDSupport->getEntity();
633         VISU::TEntity aVEntity = MEDEntityToVTK(aMEntity);
634         CORBA::String_var aSupportName = aMEDSupport->getName();
635         
636         SALOME_MED::GROUP_var aMEDGroup = SALOME_MED::GROUP::_narrow(aMedSupport);
637         if(!aMEDGroup->_is_nil()){
638           CORBA::Boolean anIsOnAllElements = aMEDSupport->isOnAllElements();
639
640           if(MYDEBUG) 
641             MESSAGE("VISU_MEDConvertor::Build - aGroup = '"<<aSupportName<<
642                     "'; anIsOnAllElements = "<<anIsOnAllElements<<
643                     "; aVEntity = "<<aVEntity);
644
645           PCGroup aGroup(new TCGroup());
646           aGroup->myGroup = aMEDGroup;
647           aGroup->myName = aSupportName.in();
648           aGroup->myMeshName = aMeshName.in();
649           VISU::TFamilyAndEntitySet& aFamilyAndEntitySet = aGroup->myFamilyAndEntitySet;
650           
651           SALOME_MED::Family_array_var aFamilies = aMEDGroup->getFamilies();
652           int iFamilyEnd = aFamilies->length();
653           for(int iFamaily = 0; iFamaily < iFamilyEnd; iFamaily++){
654             SALOME_MED::FAMILY_var aMEDFamily = aFamilies[iFamaily];
655             CORBA::String_var aFamilyName = aMEDFamily->getName();
656             PFamily aFamily = FindFamily(aMesh,aFamilyName.in());
657             if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aGroup - aFamilyName = '"<<aFamilyName.in()<<"' = "<<bool(aFamily));
658             if(aFamily){
659               TFamilyAndEntity aFamilyAndEntity(aFamilyName.in(),aFamily->myEntity);
660               aFamilyAndEntitySet.insert(aFamilyAndEntity);
661               
662               aGroup->myNbCells += aFamily->myNbCells;
663               aGroup->myCellsSize += aFamily->myCellsSize;
664               
665               VISU::TBindGroups& aBindGroups = aFamily->myGroups;
666               aBindGroups.insert(aSupportName.in());
667             }
668           }
669           
670           if(!aGroup->myFamilyAndEntitySet.empty()){
671             TGroupMap& aGroupMap = aMesh->myGroupMap;
672             aGroupMap[aSupportName.in()] = aGroup;
673           }
674
675         }
676       }
677     }
678   }
679
680   anIsSuccess = false;
681   aSObjectByNameRet = Find(aMedCompSObj,aStudy,TSObjectByName("MEDFIELD"),anIsSuccess);
682   if(anIsSuccess){
683     SALOMEDS::SObject_var aFieldsSObj = boost::get<0>(aSObjectByNameRet);
684     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - MEDFIELD found.");
685     SALOMEDS::ChildIterator_var aFieldIterator = aStudy->NewChildIterator(aFieldsSObj);
686     for(int iField = 0; aFieldIterator->More(); aFieldIterator->Next(), iField++){
687       SALOMEDS::SObject_var aFieldSObj = aFieldIterator->Value();
688       if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aFieldName = '"<<GetSObjectName(aFieldSObj)<<"'");
689       SALOMEDS::ChildIterator_var aTimeStampIterator = aStudy->NewChildIterator(aFieldSObj);
690       for(; aTimeStampIterator->More(); aTimeStampIterator->Next()){
691         SALOMEDS::SObject_var aTimeStampSObj = aTimeStampIterator->Value();
692         if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aTimeStampSObj = '"<<GetSObjectName(aTimeStampSObj)<<"'");
693         CORBA::Object_var aMedField = VISU::SObjectToObject(aTimeStampSObj);
694         if(CORBA::is_nil(aMedField)) 
695           continue;
696
697         SALOME_MED::FIELD_var aMEDField = SALOME_MED::FIELD::_narrow(aMedField);
698         if(aMEDField->_is_nil()) 
699           continue;
700
701         SALOME_MED::SUPPORT_var aMEDSupport = aMEDField->getSupport();
702         if(aMEDSupport->_is_nil()) 
703           continue;
704
705         SALOME_MED::medEntityMesh aMEntity = aMEDSupport->getEntity();
706         VISU::TEntity anEntity = MEDEntityToVTK(aMEntity);
707         SALOME_MED::MESH_var aMEDMesh = aMEDSupport->getMesh();
708         if(aMEDMesh->_is_nil()) 
709           continue;
710
711         CORBA::String_var aMeshName = aMEDMesh->getName();
712         CORBA::String_var aFieldName = aMEDField->getName();
713         
714         TMeshMap::iterator aMeshMapIter = myMeshMap.find(aMeshName.in());
715         if(aMeshMapIter == myMeshMap.end())
716           continue;
717
718         PCMesh aMesh = aMeshMapIter->second;
719         TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
720         TMeshOnEntityMap::iterator aMeshOnEntityMapIter = aMeshOnEntityMap.find(anEntity);
721         if(aMeshOnEntityMapIter == aMeshOnEntityMap.end())
722           continue;
723
724         PCMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
725         TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
726         TFieldMap::iterator aFieldMapIter = aFieldMap.find(aFieldName.in());
727         PCField aField;
728         if(aFieldMapIter == aFieldMap.end()){
729           aField = aFieldMap[aFieldName.in()](new TCField());
730           aField->myId = iField;
731           aField->myName = aFieldName.in();
732           aField->myEntity = anEntity;
733           aField->myMeshName = aMeshName.in();
734           aField->myNbComp = aMEDField->getNumberOfComponents();
735           aField->myDataSize = aMeshOnEntity->myNbCells * aField->myNbComp;
736           if(MYDEBUG) 
737             MESSAGE("VISU_MEDConvertor::Build - aMeshOnEntity->myNbCells = "<<aMeshOnEntity->myNbCells);
738           aField->myCompNames.resize(aField->myNbComp);
739           aField->myUnitNames.resize(aField->myNbComp);
740         }else
741           aField = aFieldMapIter->second;
742
743         TValField& aValField = aField->myValField;
744         int anId = aMEDField->getIterationNumber();
745         PCValForTime aValForTime = aValField[anId](new TCValForTime());
746         aValForTime->myId = anId;
747         CORBA::Double aDT = aMEDField->getTime();
748         aValForTime->myTime = TTime(aDT,"");
749         aValForTime->myField = aMEDField;
750         if(MYDEBUG) 
751           MESSAGE("VISU_MEDConvertor::Build - aMeshName = '"<<aMeshName<<
752                   "'; myEntity = "<<anEntity<<"; myTime = "<<aDT);
753       }      
754     }
755   }
756   return this; 
757 }
758  
759
760 VISU_Convertor* 
761 VISU_MEDConvertor::Build(SALOMEDS::ChildIterator_ptr theTimeStampIterator)
762 {
763   if(theTimeStampIterator->_is_nil()) return NULL;
764   for(; theTimeStampIterator->More(); theTimeStampIterator->Next()){
765     SALOMEDS::SObject_var aTimeStampSObj = theTimeStampIterator->Value();
766     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aTimeStampSObj = '"<<GetSObjectName(aTimeStampSObj)<<"'");
767
768     CORBA::Object_var aMedField = VISU::SObjectToObject(aTimeStampSObj);
769     if(CORBA::is_nil(aMedField)) 
770       continue;
771
772     SALOME_MED::FIELD_var aMEDField = SALOME_MED::FIELD::_narrow(aMedField);
773     if(aMEDField->_is_nil()) 
774       continue;
775
776     SALOME_MED::SUPPORT_var aMEDSupport = aMEDField->getSupport();
777     if(aMEDSupport->_is_nil()) 
778       continue;
779
780     SALOME_MED::medEntityMesh aMEntity = aMEDSupport->getEntity();
781     TEntity aVEntity = MEDEntityToVTK(aMEntity);
782     SALOME_MED::MESH_var aMEDMesh = aMEDSupport->getMesh();
783     if(aMEDMesh->_is_nil()) continue;
784     CORBA::String_var aMeshName = aMEDMesh->getName();
785     CORBA::String_var aFieldName = aMEDField->getName();
786
787     PCMesh aMesh = myMeshMap[aMeshName.in()](new TCMesh());
788     aMesh->myDim = aMEDMesh->getSpaceDimension();
789     aMesh->myPointsDim.resize(aMesh->myDim);
790     aMesh->myName = aMeshName.in();
791     aMesh->myNbPoints = aMEDMesh->getNumberOfNodes();
792     aMesh->myMesh = aMEDMesh;
793     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::Build - aMeshName = '"<<aMeshName<<"'; myDim = "<<aMesh->myDim);
794
795     TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
796     PCMeshOnEntity aMeshOnEntity = aMeshOnEntityMap[aVEntity](new TCMeshOnEntity());
797     aMeshOnEntity->myEntity = aVEntity;
798     aMeshOnEntity->myMeshName = aMeshName.in();
799     aMeshOnEntity->mySupport = aMEDSupport;
800     if(aVEntity == NODE_ENTITY){
801       PCMeshOnEntity aMeshOnEntity2 = aMeshOnEntityMap[CELL_ENTITY](new TCMeshOnEntity());
802       *aMeshOnEntity2 = *aMeshOnEntity;
803       aMeshOnEntity->myEntity = CELL_ENTITY;
804       GetCellsSize(aMesh,aMEDMesh,CELL_ENTITY);
805     }else{
806       PCMeshOnEntity aMeshOnEntity2 = aMeshOnEntityMap[NODE_ENTITY](new TCMeshOnEntity());
807       *aMeshOnEntity2 = *aMeshOnEntity;
808       aMeshOnEntity->myEntity = NODE_ENTITY;
809       GetCellsSize(aMesh,aMEDMesh,NODE_ENTITY);
810     }
811     GetCellsSize(aMesh,aMEDMesh,aVEntity);
812     TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
813     TFieldMap::iterator aFieldMapIter = aFieldMap.find(aFieldName.in());
814     PCField aField;
815     if(aFieldMapIter == aFieldMap.end()){
816       aField = aFieldMap[aFieldName.in()](new TCField());
817       CORBA::Short iField = mySObject->Tag();
818       aField->myId = iField;
819       aField->myName = aFieldName.in();
820       aField->myEntity = aVEntity;
821       aField->myMeshName = aMeshName.in();
822       aField->myNbComp = aMEDField->getNumberOfComponents();
823       aField->myDataSize = aMeshOnEntity->myNbCells * aField->myNbComp;
824       if(MYDEBUG) 
825         MESSAGE("VISU_MEDConvertor::Build - aMeshOnEntity->myNbCells = "<<aMeshOnEntity->myNbCells);
826       aField->myCompNames.resize(aField->myNbComp);
827       aField->myUnitNames.resize(aField->myNbComp);
828     }
829     TValField& aValField = aField->myValField;
830     int anId = aMEDField->getIterationNumber();
831     PCValForTime aValForTime = aValField[anId](new TCValForTime());
832     aValForTime->myId = anId;
833     CORBA::Double aDT = aMEDField->getTime();
834     aValForTime->myTime = TTime(aDT,"");
835     aValForTime->myField = aMEDField;
836     if(MYDEBUG) 
837       MESSAGE("VISU_MEDConvertor::Build - aMeshName = '"<<aMeshName<<
838               "'; myEntity = "<<aVEntity<<"; myTime = "<<aDT);
839   }
840   return this; 
841 }
842
843
844 int
845 VISU_MEDConvertor::LoadMeshOnEntity(VISU::PMeshOnEntityImpl theMeshOnEntity, 
846                                     const string& theFamilyName)
847 {
848   //Main part of code
849   const string& aMeshName = theMeshOnEntity->myMeshName;
850   const TEntity& aVEntity = theMeshOnEntity->myEntity;
851   PCMesh aMesh = myMeshMap[aMeshName];
852   int isPointsUpdated;
853   if(aVEntity == NODE_ENTITY) 
854     isPointsUpdated = LoadPoints(aMesh,theFamilyName);
855   else 
856     isPointsUpdated = LoadPoints(aMesh);
857   int isCellsOnEntityUpdated = LoadCellsOnEntity(aMesh,theMeshOnEntity,theFamilyName);
858
859   return (isPointsUpdated || isCellsOnEntityUpdated);
860 }
861  
862  
863 int 
864 VISU_MEDConvertor::LoadMeshOnGroup(VISU::PMeshImpl theMesh, 
865                                    const VISU::TFamilyAndEntitySet& theFamilyAndEntitySet)
866 {
867   //Main part of code
868   int isPointsUpdated = 0;
869   int isCellsOnEntityUpdated = 0;
870   VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = theFamilyAndEntitySet.begin();
871   for(; aFamilyAndEntitySetIter != theFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
872     const string& aFamilyName = aFamilyAndEntitySetIter->first;
873     const VISU::TEntity& aVEntity = aFamilyAndEntitySetIter->second;
874     PCMeshOnEntity aMeshOnEntity = theMesh->myMeshOnEntityMap[aVEntity];
875     if(aVEntity == VISU::NODE_ENTITY){
876       isPointsUpdated += LoadPoints(theMesh,aFamilyName);
877       isCellsOnEntityUpdated += LoadCellsOnEntity(theMesh,aMeshOnEntity);
878     }else{
879       isPointsUpdated += LoadPoints(theMesh);
880       isCellsOnEntityUpdated += LoadCellsOnEntity(theMesh,aMeshOnEntity,aFamilyName);
881     }
882   }
883
884   return (isPointsUpdated || isCellsOnEntityUpdated);
885 }
886
887
888 int 
889 VISU_MEDConvertor::LoadFieldOnMesh(VISU::PMeshImpl theMesh, 
890                                    VISU::PMeshOnEntityImpl theMeshOnEntity, 
891                                    VISU::PFieldImpl theField, 
892                                    VISU::PValForTimeImpl theValForTime)
893 {
894   //Main part of code
895   int isPointsUpdated = LoadPoints(theMesh);
896   int isCellsOnEntityUpdated = LoadCellsOnEntity(theMesh,theMeshOnEntity);
897   int isFieldUpdated = LoadField(theMesh,theMeshOnEntity,theField,theValForTime);
898   
899   return (isPointsUpdated || isCellsOnEntityUpdated || isFieldUpdated);
900 }
901
902 int 
903 VISU_MEDConvertor::LoadPoints(VISU::PCMesh theMesh, 
904                               const string& theFamilyName)
905 {
906   //Check on existing family
907   PCMeshOnEntity aMeshOnEntity = theMesh->myMeshOnEntityMap[VISU::NODE_ENTITY];
908   PCFamily aFamily = GetFamily(aMeshOnEntity,theFamilyName);
909   //Check on loading already done
910   bool isPointsLoaded = !theMesh->myPointsCoord.empty();
911   if(isPointsLoaded) 
912     if(!aFamily) 
913       return 0;
914     else if(!aFamily->mySubMesh.empty()) 
915       return 0;
916
917   if(MYDEBUG) 
918     MESSAGE("LoadPoints - isPointsLoaded = "<<isPointsLoaded<<"; theFamilyName = '"<<theFamilyName<<"'");
919
920   SALOME_MED::MESH_var& aMedMesh = theMesh->myMesh;
921   int iNumElemEnd = aMedMesh->getNumberOfNodes();
922   TMeshImpl::TPointsCoord& aPointsCoord = theMesh->myPointsCoord;
923
924   if(MYDEBUG) MESSAGE("LoadPoints - iNumElemEnd = "<<iNumElemEnd);
925
926   if (iNumElemEnd <= 0) 
927     throw std::runtime_error("LoadPoints >> There is no points in the mesh !!!");
928
929   aPointsCoord.resize(theMesh->myDim*iNumElemEnd,0.0);
930   SALOME_MED::double_array_var coord = aMedMesh->getCoordinates(SALOME_MED::MED_FULL_INTERLACE);
931   if(!isPointsLoaded){
932     for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) 
933       for(int iDim = 0, iNumElem2Dim = iNumElem*theMesh->myDim; iDim < theMesh->myDim; iDim++, iNumElem2Dim++)
934         aPointsCoord[iNumElem2Dim] = coord[iNumElem2Dim];
935
936     if(MYDEBUG) MESSAGE("LoadPoints - Filling aMeshOnEntity with type NODE_ENTITY");
937
938     TMeshOnEntityImpl::TConnForCellType& aConnForCellType = aMeshOnEntity->myCellsConn[VTK_VERTEX];
939     aConnForCellType.resize(iNumElemEnd);
940     for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++)
941       aConnForCellType[iNumElem] = TMeshOnEntityImpl::TConnect(1,iNumElem);
942   }
943   if(aFamily){
944     if(MYDEBUG) MESSAGE("LoadPoints - Filling aFamily SubMesh");
945
946     SALOME_MED::FAMILY_var aMedFamily = aFamily->myFamily;
947     CORBA::Boolean anIsOnAllElements = aMedFamily->isOnAllElements();
948     TFamilyImpl::TSubMeshOnCellType& aSubMeshOnCellType = aFamily->mySubMesh[VTK_VERTEX];
949
950     if(!anIsOnAllElements){
951       SALOME_MED::medGeometryElement_array_var aGeom = aMedFamily->getTypes();
952       SALOME_MED::long_array_var aCellNumForType = aMedFamily->getNumber(aGeom[0]);
953       int iNumElemEndTmp = iNumElemEnd;
954       iNumElemEnd = aCellNumForType->length();
955       for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) {
956         int tmp = aCellNumForType[iNumElem]-1;
957         if(0 > tmp || tmp >= iNumElemEndTmp) {
958           static QString aString;
959           aString.sprintf("LoadPoints >> iNumElemEndTmp(%d) <= aCellNumForType[%d]=%d < 0 !!!",iNumElemEnd,iNumElem,tmp);
960           throw std::runtime_error(aString.latin1());
961         }
962         aSubMeshOnCellType.insert(tmp);
963       }
964     }else{
965       for(int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++){
966         aSubMeshOnCellType.insert(iNumElem);
967       }
968     }
969   }
970   return 1;
971 }
972
973
974 int 
975 VISU_MEDConvertor::LoadCellsOnEntity(VISU::PCMesh theMesh,
976                                      VISU::PCMeshOnEntity theMeshOnEntity, 
977                                      const string& theFamilyName)
978 {
979   //Check on existing family
980   PCFamily aFamily = GetFamily(theMeshOnEntity,theFamilyName);
981   //Check on loading already done
982   bool isCellsLoaded = !theMeshOnEntity->myCellsConn.empty();
983   if(isCellsLoaded) 
984     if(!aFamily) 
985       return 0;
986     else if(!aFamily->mySubMesh.empty()) 
987       return 0;
988
989   SALOME_MED::SUPPORT_var& aMedSupport = theMeshOnEntity->mySupport;
990   SALOME_MED::MESH_var aMedMesh = aMedSupport->getMesh();
991   if(MYDEBUG) {
992     MESSAGE("LoadCellsOnEntity - theFamilyName = '"<<theFamilyName<<"'");
993     MESSAGE("LoadCellsOnEntity - isCellsLoaded = "<<isCellsLoaded<<"; isFamilyPresent = "<<bool(aFamily));
994   }
995
996   //Main part of code
997   SALOME_MED::medGeometryElement* aGeomElems;
998   const TEntity& aVEntity = theMeshOnEntity->myEntity;
999   int iGeomEnd = GetEntity2Geom(aVEntity,aGeomElems);
1000   const SALOME_MED::medEntityMesh& aMEntity = VTKEntityToMED(aVEntity);
1001   int aNbPoints = theMesh->myPointsCoord.size()/theMesh->myDim;
1002   if(!isCellsLoaded){
1003     for(int iGeom = 0, aCounter = 0; iGeom < iGeomEnd; iGeom++){
1004       SALOME_MED::medGeometryElement aGeom = aGeomElems[iGeom];
1005       int aMNbNodes = MEDGeom2NbNodes(aGeom);
1006       int aVGeom = MEDGeomToVTK(aGeom);
1007       int aVNbNodes = VTKGeom2NbNodes(aVGeom);
1008       int iNumElemEnd = aMedMesh->getNumberOfElements(aMEntity,aGeom);
1009       if (iNumElemEnd > 0) {
1010         SALOME_MED::long_array_var conn = 
1011           aMedMesh->getConnectivity(SALOME_MED::MED_FULL_INTERLACE,SALOME_MED::MED_NODAL,aMEntity,aGeom);
1012         TMeshOnEntityImpl::TConnForCellType& aConnForCellType = theMeshOnEntity->myCellsConn[aVGeom];
1013         //APO - aConnForCellType.resize(iNumElemEnd);
1014         vector<int> aConnect(aMNbNodes);
1015         int aNbConnForElem = conn->length()/iNumElemEnd;
1016         if(MYDEBUG) MESSAGE("LoadCellsOnEntity - aGeom = "<<aGeom<<
1017                             "; iNumElemEnd = "<<iNumElemEnd<<
1018                             "; aNbConnForElem = "<<aNbConnForElem);
1019         for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) {
1020           VISU::TMeshOnEntityImpl::TConnect anArray(aVNbNodes);
1021           for (int k = 0, kj = iNumElem*aNbConnForElem; k < aMNbNodes; k++) {
1022             aConnect[k] = conn[kj+k] - 1;
1023           }
1024           switch(aGeom){
1025           case SALOME_MED::MED_TETRA4 :
1026           case SALOME_MED::MED_TETRA10 :
1027             anArray[0] = aConnect[0];
1028             anArray[1] = aConnect[1];
1029             anArray[2] = aConnect[3];  
1030             anArray[3] = aConnect[2];  
1031             break;
1032           case SALOME_MED::MED_PYRA5 :
1033           case SALOME_MED::MED_PYRA13 :
1034             anArray[0] = aConnect[0];
1035             anArray[1] = aConnect[3];  
1036             anArray[2] = aConnect[2];
1037             anArray[3] = aConnect[1];  
1038             anArray[4] = aConnect[4];
1039             break;
1040           default:
1041             for (int k = 0; k < aVNbNodes; k++) 
1042               anArray[k] = aConnect[k];
1043           }
1044           for (int k = 0; k < aVNbNodes; k++) 
1045             if(anArray[k] < 0 || aNbPoints <= anArray[k]){
1046               static QString aString;
1047               aString.sprintf("ImportCells >> aNbPoints(%d) <= anArray[%d][%d]=%d < 0 !!!",aNbPoints,iNumElem,k,anArray[k]);
1048               throw std::runtime_error(aString.latin1());
1049             }
1050           aConnForCellType.push_back(anArray);
1051         }
1052         //Workaround for MED Component data structure
1053         int aSize = aConnForCellType.size();
1054         theMeshOnEntity->myCellsFirstIndex[aGeom] = TCMeshOnEntity::TIndexAndSize(aCounter,aSize);
1055         aCounter += aSize;
1056       }
1057     }
1058   }
1059   //Filling aFamily SubMesh
1060   if(aFamily){
1061     SALOME_MED::FAMILY_var aMedFamily = aFamily->myFamily;
1062     CORBA::Boolean anIsOnAllElements = aMedFamily->isOnAllElements();
1063     if(!anIsOnAllElements){
1064       SALOME_MED::medGeometryElement_array_var aGeoms = aMedFamily->getTypes();
1065       iGeomEnd = aGeoms->length();
1066       if(MYDEBUG) MESSAGE("LoadCellsOnEntity - iGeomEnd = "<<iGeomEnd);
1067       for (int iGeom = 0; iGeom < iGeomEnd; iGeom++) {
1068         SALOME_MED::medGeometryElement aGeom = aGeoms[iGeom];
1069         SALOME_MED::long_array_var aCellNumForType = aMedFamily->getNumber(aGeom);
1070         int aVGeom = MEDGeomToVTK(aGeom);
1071         TFamilyImpl::TSubMeshOnCellType& aSubMeshOnCellType = aFamily->mySubMesh[aVGeom]; 
1072         int iNumElemEndTmp = theMeshOnEntity->myCellsConn[aVGeom].size();
1073         int iNumElemEnd = aCellNumForType->length();
1074         int aCounter = theMeshOnEntity->myCellsFirstIndex[aGeom].first;
1075         if(MYDEBUG) 
1076           MESSAGE("LoadCellsOnEntity - aGeom = "<<aGeom<<
1077                   "; iNumElemEnd = "<<iNumElemEnd<<
1078                   "; aCounter = "<<aCounter);
1079         for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) {
1080           int tmp = aCellNumForType[iNumElem]-aCounter-1;
1081           if(0 > tmp || tmp >= iNumElemEndTmp) {
1082             static QString aString;
1083             aString.sprintf("LoadCellsOnEntity >> iNumElemEndTmp(%d) <= aCellNumForType[%d]=%d < 0 !!!",iNumElemEndTmp,iNumElem,tmp);
1084             throw std::runtime_error(aString.latin1());
1085           }
1086           aSubMeshOnCellType.insert(tmp);
1087         }
1088       }
1089     }else{
1090       const TMeshOnEntityImpl::TCellsConn& aCellsConn = theMeshOnEntity->myCellsConn;
1091       TMeshOnEntityImpl::TCellsConn::const_iterator aCellsConnIter = aCellsConn.begin();
1092       for(; aCellsConnIter != aCellsConn.end(); aCellsConnIter++){
1093         int aVGeom = aCellsConnIter->first;
1094         const TMeshOnEntityImpl::TConnForCellType& aConnForCellType = aCellsConnIter->second;
1095         TFamilyImpl::TSubMeshOnCellType& aSubMeshOnCellType = aFamily->mySubMesh[aVGeom];
1096         int iNumElemEnd = aConnForCellType.size();
1097         for(int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++)
1098           aSubMeshOnCellType.insert(iNumElem);
1099       }
1100     }
1101   }
1102   return 1;
1103 }
1104
1105 template<class TArray> 
1106 int 
1107 ImportField(TArray& theArray, 
1108             VISU::PCMesh theMesh,
1109             VISU::PCField theField,
1110             VISU::PCValForTime theValForTime,
1111             VISU::PCMeshOnEntity theMeshOnEntity)
1112 {
1113   if(theField->myEntity == NODE_ENTITY){
1114     TValForTimeImpl::TValForCellsWithType& aValForCellsWithType = 
1115       theValForTime->myValForCells[VTK_VERTEX];
1116     int iNumElemEnd = theMesh->myPointsCoord.size()/theMesh->myDim*theField->myNbComp;
1117     if(MYDEBUG) MESSAGE("ImportField - iNumElemEnd = "<<iNumElemEnd);
1118     aValForCellsWithType.resize(iNumElemEnd);
1119     for (int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++) 
1120       aValForCellsWithType[iNumElem] = theArray[iNumElem];
1121   }else{
1122     SALOME_MED::medGeometryElement* aGeomElems;
1123     const TEntity& aVEntity = theField->myEntity;
1124     int iGeomEnd = GetEntity2Geom(aVEntity,aGeomElems);
1125     for(int iGeom = 0; iGeom < iGeomEnd; iGeom++){
1126       SALOME_MED::medGeometryElement aGeom = aGeomElems[iGeom];
1127       int aVGeom = MEDGeomToVTK(aGeom);
1128       const TCMeshOnEntity::TCellsFirstIndex& aCellsFirstIndex = theMeshOnEntity->myCellsFirstIndex;
1129       TCMeshOnEntity::TCellsFirstIndex::const_iterator aCellsFirstIndexIter = aCellsFirstIndex.find(aGeom);
1130       if(aCellsFirstIndexIter != aCellsFirstIndex.end()){
1131         const TCMeshOnEntity::TIndexAndSize& aIndexAndSize = aCellsFirstIndexIter->second;
1132         int iNumElemEnd = aIndexAndSize.second;
1133         if(MYDEBUG) 
1134           MESSAGE("ImportField - aGeom = "<<aGeom<<
1135                   "; aIndexAndSize = {"<<aIndexAndSize.first<<
1136                   ","<<aIndexAndSize.second<<"}");
1137         TValForTimeImpl::TValForCellsWithType& aValForCellsWithType = theValForTime->myValForCells[aVGeom];
1138         aValForCellsWithType.resize(iNumElemEnd*theField->myNbComp);
1139         for(int iNumElem = 0; iNumElem < iNumElemEnd; iNumElem++)
1140           for(int k = 0, kj = iNumElem*theField->myNbComp; k < theField->myNbComp; k++)
1141             aValForCellsWithType[kj+k] = theArray[aIndexAndSize.first*theField->myNbComp+kj+k];
1142       }
1143     }
1144   }
1145   return 1;
1146 }
1147
1148 int
1149 VISU_MEDConvertor::LoadField(VISU::PCMesh theMesh,
1150                              VISU::PCMeshOnEntity theMeshOnEntity,
1151                              VISU::PField theField, 
1152                              VISU::PCValForTime theValForTime)
1153 {
1154   //Check on loading already done
1155   if(!theValForTime->myValForCells.empty()) 
1156     return 0;
1157  
1158   SALOME_MED::FIELD_var aMEDField = theValForTime->myField;
1159   SALOME_MED::FIELDDOUBLE_ptr aFieldDouble = SALOME_MED::FIELDDOUBLE::_narrow(aMEDField);
1160   if(!aFieldDouble->_is_nil()){
1161     SALOME_MED::double_array_var anArray = aFieldDouble->getValue(SALOME_MED::MED_FULL_INTERLACE);
1162     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::LoadField - There is FIELDDOUBLE = "<<anArray->length());
1163     ::ImportField(anArray,theMesh,theField,theValForTime,theMeshOnEntity);
1164   }
1165   SALOME_MED::FIELDINT_ptr aFieldInt = SALOME_MED::FIELDINT::_narrow(aMEDField);
1166   if(!aFieldInt->_is_nil()){
1167     SALOME_MED::long_array_var anArray = aFieldInt->getValue(SALOME_MED::MED_FULL_INTERLACE);
1168     if(MYDEBUG) MESSAGE("VISU_MEDConvertor::LoadField - There is FIELDINT = "<<anArray->length());
1169     ::ImportField(anArray,theMesh,theField,theValForTime,theMeshOnEntity);
1170   }
1171   return 1;
1172 }