Salome HOME
Merge branch V7_3_1_BR
[modules/smesh.git] / src / DriverMED / DriverMED_W_SMESHDS_Mesh.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH DriverMED : driver to read and write 'med' files
24 //  File   : DriverMED_W_SMESHDS_Mesh.cxx
25 //  Module : SMESH
26 //
27
28 #include "DriverMED_W_SMESHDS_Mesh.h"
29
30 #include "DriverMED_Family.h"
31 #include "MED_Factory.hxx"
32 #include "MED_Utilities.hxx"
33 #include "SMDS_IteratorOnIterators.hxx"
34 #include "SMDS_MeshElement.hxx"
35 #include "SMDS_MeshNode.hxx"
36 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
37 #include "SMDS_SetIterator.hxx"
38 #include "SMESHDS_Mesh.hxx"
39
40 #include <utilities.h>
41
42
43 #define _EDF_NODE_IDS_
44 //#define _ELEMENTS_BY_DIM_
45
46 using namespace std;
47 using namespace MED;
48
49
50 DriverMED_W_SMESHDS_Mesh::DriverMED_W_SMESHDS_Mesh():
51   myMedVersion(MED::eV2_2),
52   myAllSubMeshes (false),
53   myDoGroupOfNodes (false),
54   myDoGroupOfEdges (false),
55   myDoGroupOfFaces (false),
56   myDoGroupOfVolumes (false),
57   myDoGroupOf0DElems(false),
58   myDoGroupOfBalls(false),
59   myAutoDimension(true)
60 {}
61
62 void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName, 
63                                        MED::EVersion      theId)
64 {
65   Driver_SMESHDS_Mesh::SetFile(theFileName);
66   myMedVersion = theId;
67 }
68
69 void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName)
70 {
71   Driver_SMESHDS_Mesh::SetFile(theFileName);
72 }
73
74 string DriverMED_W_SMESHDS_Mesh::GetVersionString(const MED::EVersion theVersion, int theNbDigits)
75 {
76   TInt majeur, mineur, release;
77   majeur =  mineur = release = 0;
78 //   if ( theVersion == eV2_1 )
79 //     MED::GetVersionRelease<eV2_1>(majeur, mineur, release);
80 //   else
81     MED::GetVersionRelease<eV2_2>(majeur, mineur, release);
82   ostringstream name;
83   if ( theNbDigits > 0 )
84     name << majeur;
85   if ( theNbDigits > 1 )
86     name << "." << mineur;
87   if ( theNbDigits > 2 )
88     name << "." << release;
89   return name.str();
90 }
91
92 void DriverMED_W_SMESHDS_Mesh::AddGroup(SMESHDS_GroupBase* theGroup)
93 {
94   myGroups.push_back(theGroup);
95 }
96
97 void DriverMED_W_SMESHDS_Mesh::AddAllSubMeshes()
98 {
99   myAllSubMeshes = true;
100 }
101
102 void DriverMED_W_SMESHDS_Mesh::AddSubMesh(SMESHDS_SubMesh* theSubMesh, int theID)
103 {
104   mySubMeshes[theID] = theSubMesh;
105 }
106
107 void DriverMED_W_SMESHDS_Mesh::AddGroupOfNodes()
108 {
109   myDoGroupOfNodes = true;
110 }
111
112 void DriverMED_W_SMESHDS_Mesh::AddGroupOfEdges()
113 {
114   myDoGroupOfEdges = true;
115 }
116
117 void DriverMED_W_SMESHDS_Mesh::AddGroupOfFaces()
118 {
119   myDoGroupOfFaces = true;
120 }
121
122 void DriverMED_W_SMESHDS_Mesh::AddGroupOfVolumes()
123 {
124   myDoGroupOfVolumes = true;
125 }
126
127 namespace
128 {
129   typedef double (SMDS_MeshNode::* TGetCoord)() const;
130   typedef const char* TName;
131   typedef const char* TUnit;
132
133   // name length in a mesh must be equal to 16 :
134   //         1234567890123456
135   TName M = "m               ";
136   TName X = "x               ";
137   TName Y = "y               ";
138   TName Z = "z               ";
139
140   TUnit aUnit[3] = {M,M,M};
141
142   // 3 dim
143   TGetCoord aXYZGetCoord[3] = {
144     &SMDS_MeshNode::X, 
145     &SMDS_MeshNode::Y, 
146     &SMDS_MeshNode::Z
147   };
148   TName aXYZName[3] = {X,Y,Z};
149   
150   // 2 dim
151   TGetCoord aXYGetCoord[2] = {
152     &SMDS_MeshNode::X, 
153     &SMDS_MeshNode::Y
154   };
155   TName aXYName[2] = {X,Y};
156
157   TGetCoord aYZGetCoord[2] = {
158     &SMDS_MeshNode::Y, 
159     &SMDS_MeshNode::Z
160   };
161   TName aYZName[2] = {Y,Z};
162
163   TGetCoord aXZGetCoord[2] = {
164     &SMDS_MeshNode::X, 
165     &SMDS_MeshNode::Z
166   };
167   TName aXZName[2] = {X,Z};
168
169   // 1 dim
170   TGetCoord aXGetCoord[1] = {
171     &SMDS_MeshNode::X
172   };
173   TName aXName[1] = {X};
174
175   TGetCoord aYGetCoord[1] = {
176     &SMDS_MeshNode::Y
177   };
178   TName aYName[1] = {Y};
179
180   TGetCoord aZGetCoord[1] = {
181     &SMDS_MeshNode::Z
182   };
183   TName aZName[1] = {Z};
184
185
186   class TCoordHelper{
187     SMDS_NodeIteratorPtr myNodeIter;
188     const SMDS_MeshNode* myCurrentNode;
189     TGetCoord* myGetCoord;
190     TName* myName;
191     TUnit* myUnit;
192   public:
193     TCoordHelper(const SMDS_NodeIteratorPtr& theNodeIter,
194                  TGetCoord* theGetCoord,
195                  TName* theName,
196                  TUnit* theUnit = aUnit):
197       myNodeIter(theNodeIter),
198       myGetCoord(theGetCoord),
199       myName(theName),
200       myUnit(theUnit)
201     {}
202     virtual ~TCoordHelper(){}
203     bool Next(){ 
204       return myNodeIter->more() && 
205         (myCurrentNode = myNodeIter->next());
206     }
207     const SMDS_MeshNode* GetNode(){
208       return myCurrentNode;
209     }
210     MED::TIntVector::value_type GetID(){
211       return myCurrentNode->GetID();
212     }
213     MED::TFloatVector::value_type GetCoord(TInt theCoodId){
214       return (myCurrentNode->*myGetCoord[theCoodId])();
215     }
216     MED::TStringVector::value_type GetName(TInt theDimId){
217       return myName[theDimId];
218     }
219     MED::TStringVector::value_type GetUnit(TInt theDimId){
220       return myUnit[theDimId];
221     }
222   };
223   typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
224
225   //-------------------------------------------------------
226   /*!
227    * \brief Structure describing element type
228    */
229   //-------------------------------------------------------
230   struct TElemTypeData
231   {
232     EEntiteMaillage     _entity;
233     EGeometrieElement   _geomType;
234     TInt                _nbElems;
235     SMDSAbs_ElementType _smdsType;
236
237     TElemTypeData (EEntiteMaillage entity, EGeometrieElement geom, TInt nb, SMDSAbs_ElementType type)
238       : _entity(entity), _geomType(geom), _nbElems( nb ), _smdsType( type ) {}
239   };
240
241
242   typedef NCollection_DataMap< Standard_Address, int > TElemFamilyMap;
243
244   //================================================================================
245   /*!
246    * \brief Fills element to famaly ID map for element type.
247    * Removes all families of anElemType
248    */
249   //================================================================================
250
251   void fillElemFamilyMap( TElemFamilyMap &            anElemFamMap,
252                           list<DriverMED_FamilyPtr> & aFamilies,
253                           const SMDSAbs_ElementType   anElemType)
254   {
255     anElemFamMap.Clear();
256     //anElemFamMap.clear();
257     list<DriverMED_FamilyPtr>::iterator aFamsIter = aFamilies.begin();
258     while ( aFamsIter != aFamilies.end() )
259     {
260       if ((*aFamsIter)->GetType() != anElemType) {
261         aFamsIter++;
262       }
263       else {
264         int aFamId = (*aFamsIter)->GetId();
265         const set<const SMDS_MeshElement *>& anElems = (*aFamsIter)->GetElements();
266         set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElems.begin();
267         for (; anElemsIter != anElems.end(); anElemsIter++)
268         {
269           anElemFamMap.Bind( (Standard_Address)*anElemsIter, aFamId );
270           //anElemFamMap[*anElemsIter] = aFamId;
271         }
272         // remove a family from the list
273         aFamilies.erase( aFamsIter++ );
274       }
275     }
276   }
277
278   //================================================================================
279   /*!
280    * \brief For an element, return family ID found in the map or a default one
281    */
282   //================================================================================
283
284   int getFamilyId( const TElemFamilyMap &  anElemFamMap,
285                    const SMDS_MeshElement* anElement,
286                    const int               aDefaultFamilyId)
287   {
288     if ( anElemFamMap.IsBound( (Standard_Address) anElement ))
289       return anElemFamMap( (Standard_Address) anElement );
290 //     TElemFamilyMap::iterator elem_famNum = anElemFamMap.find( anElement );
291 //     if ( elem_famNum != anElemFamMap.end() )
292 //       return elem_famNum->second;
293     return aDefaultFamilyId;
294   }
295 }
296
297 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::Perform()
298 {
299   Status aResult = DRS_OK;
300   if (myMesh->hasConstructionEdges() || myMesh->hasConstructionFaces()) {
301     INFOS("SMDS_MESH with hasConstructionEdges() or hasConstructionFaces() do not supports!!!");
302     return DRS_FAIL;
303   }
304   try {
305     MESSAGE("Perform - myFile : "<<myFile);
306
307     // Creating the MED mesh for corresponding SMDS structure
308     //-------------------------------------------------------
309     string aMeshName;
310     if (myMeshId != -1) {
311       ostringstream aMeshNameStr;
312       aMeshNameStr<<myMeshId;
313       aMeshName = aMeshNameStr.str();
314     } else {
315       aMeshName = myMeshName;
316     }
317
318     // Mesh dimension definition
319     TInt aSpaceDimension = 3;
320     TCoordHelperPtr aCoordHelperPtr;
321     {
322       bool anIsXDimension = false;
323       bool anIsYDimension = false;
324       bool anIsZDimension = false;
325       if ( myAutoDimension )
326       {
327         SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
328         double aBounds[6];
329         if(aNodesIter->more()){
330           const SMDS_MeshNode* aNode = aNodesIter->next();
331           aBounds[0] = aBounds[1] = aNode->X();
332           aBounds[2] = aBounds[3] = aNode->Y();
333           aBounds[4] = aBounds[5] = aNode->Z();
334         }
335         while(aNodesIter->more()){
336           const SMDS_MeshNode* aNode = aNodesIter->next();
337           aBounds[0] = min(aBounds[0],aNode->X());
338           aBounds[1] = max(aBounds[1],aNode->X());
339           
340           aBounds[2] = min(aBounds[2],aNode->Y());
341           aBounds[3] = max(aBounds[3],aNode->Y());
342           
343           aBounds[4] = min(aBounds[4],aNode->Z());
344           aBounds[5] = max(aBounds[5],aNode->Z());
345         }
346
347         double EPS = 1.0E-7;
348         anIsXDimension = (aBounds[1] - aBounds[0]) + abs(aBounds[1]) + abs(aBounds[0]) > EPS;
349         anIsYDimension = (aBounds[3] - aBounds[2]) + abs(aBounds[3]) + abs(aBounds[2]) > EPS;
350         anIsZDimension = (aBounds[5] - aBounds[4]) + abs(aBounds[5]) + abs(aBounds[4]) > EPS;
351         aSpaceDimension = anIsXDimension + anIsYDimension + anIsZDimension;
352         if(!aSpaceDimension)
353           aSpaceDimension = 3;
354         // PAL16857(SMESH not conform to the MED convention):
355         if ( aSpaceDimension == 2 && anIsZDimension ) // 2D only if mesh is in XOY plane
356           aSpaceDimension = 3;
357         // PAL18941(a saved study with a mesh belong Z is opened and the mesh is belong X)
358         if ( aSpaceDimension == 1 && !anIsXDimension ) {// 1D only if mesh is along OX
359           if ( anIsYDimension ) {
360             aSpaceDimension = 2;
361             anIsXDimension = true;
362           } else {
363             aSpaceDimension = 3;
364           }
365         }
366       }
367
368       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator(/*idInceasingOrder=*/true);
369       switch(aSpaceDimension){
370       case 3:
371         aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYZGetCoord,aXYZName));
372         break;
373       case 2:
374         if(anIsXDimension && anIsYDimension)
375           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYGetCoord,aXYName));
376         if(anIsYDimension && anIsZDimension)
377           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYZGetCoord,aYZName));
378         if(anIsXDimension && anIsZDimension)
379           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXZGetCoord,aXZName));
380         break;
381       case 1:
382         if(anIsXDimension)
383           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXGetCoord,aXName));
384         if(anIsYDimension)
385           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYGetCoord,aYName));
386         if(anIsZDimension)
387           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aZGetCoord,aZName));
388         break;
389       }
390     }
391     TInt aMeshDimension = 0;
392     if ( myMesh->NbEdges() > 0 )
393       aMeshDimension = 1;
394     if ( myMesh->NbFaces() > 0 )
395       aMeshDimension = 2;
396     if ( myMesh->NbVolumes() > 0 )
397       aMeshDimension = 3;
398     
399     MED::PWrapper myMed = CrWrapper(myFile,myMedVersion);
400     PMeshInfo aMeshInfo = myMed->CrMeshInfo(aMeshDimension,aSpaceDimension,aMeshName);
401     MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
402     myMed->SetMeshInfo(aMeshInfo);
403
404     // Storing SMDS groups and sub-meshes as med families
405     //----------------------------------------------------
406     int myNodesDefaultFamilyId      = 0;
407     int my0DElementsDefaultFamilyId = 0;
408     int myBallsDefaultFamilyId      = 0;
409     int myEdgesDefaultFamilyId      = 0;
410     int myFacesDefaultFamilyId      = 0;
411     int myVolumesDefaultFamilyId    = 0;
412     int nbNodes      = myMesh->NbNodes();
413     int nb0DElements = myMesh->Nb0DElements();
414     int nbBalls      = myMesh->NbBalls();
415     int nbEdges      = myMesh->NbEdges();
416     int nbFaces      = myMesh->NbFaces();
417     int nbVolumes    = myMesh->NbVolumes();
418     if (myDoGroupOfNodes && nbNodes) myNodesDefaultFamilyId = REST_NODES_FAMILY;
419     if (myDoGroupOfEdges && nbEdges) myEdgesDefaultFamilyId = REST_EDGES_FAMILY;
420     if (myDoGroupOfFaces && nbFaces) myFacesDefaultFamilyId = REST_FACES_FAMILY;
421     if (myDoGroupOfVolumes && nbVolumes) myVolumesDefaultFamilyId = REST_VOLUMES_FAMILY;
422     if (myDoGroupOf0DElems && nb0DElements) my0DElementsDefaultFamilyId = REST_0DELEM_FAMILY;
423     if (myDoGroupOfBalls && nbBalls) myBallsDefaultFamilyId = REST_BALL_FAMILY;
424
425     MESSAGE("Perform - aFamilyInfo");
426     list<DriverMED_FamilyPtr> aFamilies;
427     if (myAllSubMeshes) {
428       aFamilies = DriverMED_Family::MakeFamilies
429         (myMesh->SubMeshes(), myGroups,
430          myDoGroupOfNodes   && nbNodes,
431          myDoGroupOfEdges   && nbEdges,
432          myDoGroupOfFaces   && nbFaces,
433          myDoGroupOfVolumes && nbVolumes,
434          myDoGroupOf0DElems && nb0DElements,
435          myDoGroupOfBalls   && nbBalls);
436     } else {
437       aFamilies = DriverMED_Family::MakeFamilies
438         (mySubMeshes, myGroups,
439          myDoGroupOfNodes   && nbNodes,
440          myDoGroupOfEdges   && nbEdges,
441          myDoGroupOfFaces   && nbFaces,
442          myDoGroupOfVolumes && nbVolumes,
443          myDoGroupOf0DElems && nb0DElements,
444          myDoGroupOfBalls   && nbBalls);
445     }
446     list<DriverMED_FamilyPtr>::iterator aFamsIter;
447     for (aFamsIter = aFamilies.begin(); aFamsIter != aFamilies.end(); aFamsIter++)
448     {
449       PFamilyInfo aFamilyInfo = (*aFamsIter)->GetFamilyInfo(myMed,aMeshInfo);
450       myMed->SetFamilyInfo(aFamilyInfo);
451     }
452
453     // Storing SMDS nodes to the MED file for the MED mesh
454     //----------------------------------------------------
455 #ifdef _EDF_NODE_IDS_
456     typedef map<TInt,TInt> TNodeIdMap;
457     TNodeIdMap aNodeIdMap;
458 #endif
459     const EModeSwitch   theMode        = eFULL_INTERLACE;
460     const ERepere       theSystem      = eCART;
461     const EBooleen      theIsElemNum   = eVRAI;
462     const EBooleen      theIsElemNames = eFAUX;
463     const EConnectivite theConnMode    = eNOD;
464
465     TInt aNbNodes = myMesh->NbNodes();
466     PNodeInfo aNodeInfo = myMed->CrNodeInfo(aMeshInfo, aNbNodes,
467                                             theMode, theSystem, theIsElemNum, theIsElemNames);
468
469     //cout << " fillElemFamilyMap( SMDSAbs_Node )" << endl;
470     // find family numbers for nodes
471     TElemFamilyMap anElemFamMap;
472     fillElemFamilyMap( anElemFamMap, aFamilies, SMDSAbs_Node );
473
474     for (TInt iNode = 0; aCoordHelperPtr->Next(); iNode++)
475     {
476       // coordinates
477       TCoordSlice aTCoordSlice = aNodeInfo->GetCoordSlice( iNode );
478       for(TInt iCoord = 0; iCoord < aSpaceDimension; iCoord++){
479         aTCoordSlice[iCoord] = aCoordHelperPtr->GetCoord(iCoord);
480       }
481       // node number
482       int aNodeID = aCoordHelperPtr->GetID();
483       aNodeInfo->SetElemNum( iNode, aNodeID );
484 #ifdef _EDF_NODE_IDS_
485       aNodeIdMap.insert( aNodeIdMap.end(), make_pair( aNodeID, iNode+1 ));
486 #endif
487       // family number
488       const SMDS_MeshNode* aNode = aCoordHelperPtr->GetNode();
489       int famNum = getFamilyId( anElemFamMap, aNode, myNodesDefaultFamilyId );
490       aNodeInfo->SetFamNum( iNode, famNum );
491     }
492     anElemFamMap.Clear();
493
494     // coordinate names and units
495     for (TInt iCoord = 0; iCoord < aSpaceDimension; iCoord++) {
496       aNodeInfo->SetCoordName( iCoord, aCoordHelperPtr->GetName(iCoord));
497       aNodeInfo->SetCoordUnit( iCoord, aCoordHelperPtr->GetUnit(iCoord));
498     }
499
500     //cout << " SetNodeInfo(aNodeInfo)" << endl;
501     MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbNodes);
502     myMed->SetNodeInfo(aNodeInfo);
503     aNodeInfo.reset(); // free memory used for arrays
504
505
506     // Storing SMDS elements to the MED file for the MED mesh
507     //-------------------------------------------------------
508     // Write one element type at once in order to minimize memory usage (PAL19276)
509
510     const SMDS_MeshInfo& nbElemInfo = myMesh->GetMeshInfo();
511
512     // poly elements are not supported by med-2.1
513     bool polyTypesSupported = myMed->CrPolygoneInfo(aMeshInfo,eMAILLE,ePOLYGONE,0,0);
514     TInt nbPolygonNodes = 0, nbPolyhedronNodes = 0, nbPolyhedronFaces = 0;
515
516     // nodes on VERTEXes where 0D elements are absent
517     std::vector<const SMDS_MeshElement*> nodesOf0D;
518     std::vector< SMDS_ElemIteratorPtr > iterVec;
519     SMDS_ElemIteratorPtr iterVecIter;
520     if ( myAddODOnVertices && getNodesOfMissing0DOnVert( myMesh, nodesOf0D ))
521     {
522       iterVec.resize(2);
523       iterVec[0] = myMesh->elementsIterator( SMDSAbs_0DElement );
524       iterVec[1] = SMDS_ElemIteratorPtr
525         ( new SMDS_ElementVectorIterator( nodesOf0D.begin(), nodesOf0D.end() ));
526
527       typedef SMDS_IteratorOnIterators
528         < const SMDS_MeshElement *, std::vector< SMDS_ElemIteratorPtr > > TItIterator;
529       iterVecIter = SMDS_ElemIteratorPtr( new TItIterator( iterVec ));
530     }
531
532     // collect info on all geom types
533
534     list< TElemTypeData > aTElemTypeDatas;
535
536     EEntiteMaillage anEntity = eMAILLE;
537 #ifdef _ELEMENTS_BY_DIM_
538     anEntity = eNOEUD_ELEMENT;
539 #endif
540     aTElemTypeDatas.push_back(TElemTypeData(anEntity,
541                                             ePOINT1,
542                                             nbElemInfo.Nb0DElements() + nodesOf0D.size(),
543                                             SMDSAbs_0DElement));
544 #ifdef _ELEMENTS_BY_DIM_
545     anEntity = eSTRUCT_ELEMENT;
546 #endif
547     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
548                                              eBALL,
549                                              nbElemInfo.NbBalls(),
550                                              SMDSAbs_Ball));
551 #ifdef _ELEMENTS_BY_DIM_
552     anEntity = eARETE;
553 #endif
554     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
555                                              eSEG2,
556                                              nbElemInfo.NbEdges( ORDER_LINEAR ),
557                                              SMDSAbs_Edge));
558     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
559                                              eSEG3,
560                                              nbElemInfo.NbEdges( ORDER_QUADRATIC ),
561                                              SMDSAbs_Edge));
562 #ifdef _ELEMENTS_BY_DIM_
563     anEntity = eFACE;
564 #endif
565     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
566                                              eTRIA3,
567                                              nbElemInfo.NbTriangles( ORDER_LINEAR ),
568                                              SMDSAbs_Face));
569     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
570                                              eTRIA6,
571                                              nbElemInfo.NbTriangles( ORDER_QUADRATIC ) -
572                                              nbElemInfo.NbBiQuadTriangles(),
573                                              SMDSAbs_Face));
574     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
575                                              eTRIA7,
576                                              nbElemInfo.NbBiQuadTriangles(),
577                                              SMDSAbs_Face));
578     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
579                                              eQUAD4,
580                                              nbElemInfo.NbQuadrangles( ORDER_LINEAR ),
581                                              SMDSAbs_Face));
582     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
583                                              eQUAD8,
584                                              nbElemInfo.NbQuadrangles( ORDER_QUADRATIC ) -
585                                              nbElemInfo.NbBiQuadQuadrangles(),
586                                              SMDSAbs_Face));
587     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
588                                              eQUAD9,
589                                              nbElemInfo.NbBiQuadQuadrangles(),
590                                              SMDSAbs_Face));
591     if ( polyTypesSupported ) {
592       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
593                                                ePOLYGONE,
594                                                nbElemInfo.NbPolygons(),
595                                                SMDSAbs_Face));
596       // we need one more loop on poly elements to count nb of their nodes
597       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
598                                                ePOLYGONE,
599                                                nbElemInfo.NbPolygons(),
600                                                SMDSAbs_Face));
601     }
602 #ifdef _ELEMENTS_BY_DIM_
603     anEntity = eMAILLE;
604 #endif
605     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
606                                              eTETRA4,
607                                              nbElemInfo.NbTetras( ORDER_LINEAR ),
608                                              SMDSAbs_Volume));
609     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
610                                              eTETRA10,
611                                              nbElemInfo.NbTetras( ORDER_QUADRATIC ),
612                                              SMDSAbs_Volume));
613     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
614                                              ePYRA5,
615                                              nbElemInfo.NbPyramids( ORDER_LINEAR ),
616                                              SMDSAbs_Volume));
617     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
618                                              ePYRA13,
619                                              nbElemInfo.NbPyramids( ORDER_QUADRATIC ),
620                                              SMDSAbs_Volume));
621     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
622                                              ePENTA6,
623                                              nbElemInfo.NbPrisms( ORDER_LINEAR ),
624                                              SMDSAbs_Volume));
625     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
626                                              ePENTA15,
627                                              nbElemInfo.NbPrisms( ORDER_QUADRATIC ),
628                                              SMDSAbs_Volume));
629     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
630                                              eHEXA8,
631                                              nbElemInfo.NbHexas( ORDER_LINEAR ),
632                                              SMDSAbs_Volume));
633     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
634                                              eHEXA20,
635                                              nbElemInfo.NbHexas( ORDER_QUADRATIC )-
636                                              nbElemInfo.NbTriQuadHexas(),
637                                              SMDSAbs_Volume));
638     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
639                                              eHEXA27,
640                                              nbElemInfo.NbTriQuadHexas(),
641                                              SMDSAbs_Volume));
642     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
643                                              eOCTA12,
644                                              nbElemInfo.NbHexPrisms(),
645                                              SMDSAbs_Volume));
646     if ( polyTypesSupported ) {
647       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
648                                                ePOLYEDRE,
649                                                nbElemInfo.NbPolyhedrons(),
650                                                SMDSAbs_Volume));
651       // we need one more loop on poly elements to count nb of their nodes
652       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
653                                                ePOLYEDRE,
654                                                nbElemInfo.NbPolyhedrons(),
655                                                SMDSAbs_Volume));
656     }
657
658     vector< bool > isElemFamMapBuilt( SMDSAbs_NbElementTypes, false );
659
660     // loop on all geom types of elements
661
662     list< TElemTypeData >::iterator aElemTypeData = aTElemTypeDatas.begin();
663     for ( ; aElemTypeData != aTElemTypeDatas.end(); ++aElemTypeData )
664     {
665       if ( aElemTypeData->_nbElems == 0 )
666         continue;
667
668       int defaultFamilyId = 0;
669       switch ( aElemTypeData->_smdsType ) {
670       case SMDSAbs_0DElement:
671         defaultFamilyId = my0DElementsDefaultFamilyId;
672         break;
673       case SMDSAbs_Ball:
674         defaultFamilyId = myBallsDefaultFamilyId;
675         break;
676       case SMDSAbs_Edge:
677         defaultFamilyId = myEdgesDefaultFamilyId;
678         break;
679       case SMDSAbs_Face:
680         defaultFamilyId = myFacesDefaultFamilyId;
681         break;
682       case SMDSAbs_Volume:
683         defaultFamilyId = myVolumesDefaultFamilyId;
684         break;
685       default:
686         continue;
687       }
688
689       // iterator on elements of a current type
690       SMDS_ElemIteratorPtr elemIterator;
691       int iElem = 0;
692
693       // Treat POLYGONs
694       // ---------------
695       if ( aElemTypeData->_geomType == ePOLYGONE )
696       {
697         elemIterator = myMesh->elementGeomIterator( SMDSGeom_POLYGON );
698         if ( nbPolygonNodes == 0 ) {
699           // Count nb of nodes
700           while ( elemIterator->more() ) {
701             const SMDS_MeshElement* anElem = elemIterator->next();
702             nbPolygonNodes += anElem->NbNodes();
703             if ( ++iElem == aElemTypeData->_nbElems )
704               break;
705           }
706         }
707         else {
708           // Store in med file
709           PPolygoneInfo aPolygoneInfo = myMed->CrPolygoneInfo(aMeshInfo,
710                                                               aElemTypeData->_entity,
711                                                               aElemTypeData->_geomType,
712                                                               aElemTypeData->_nbElems,
713                                                               nbPolygonNodes,
714                                                               theConnMode, theIsElemNum,
715                                                               theIsElemNames);
716           TElemNum & index = *(aPolygoneInfo->myIndex.get());
717           index[0] = 1;
718
719           while ( elemIterator->more() )
720           {
721             const SMDS_MeshElement* anElem = elemIterator->next();
722             // index
723             TInt aNbNodes = anElem->NbNodes();
724             index[ iElem+1 ] = index[ iElem ] + aNbNodes;
725
726             // connectivity
727             TConnSlice aTConnSlice = aPolygoneInfo->GetConnSlice( iElem );
728             for(TInt iNode = 0; iNode < aNbNodes; iNode++) {
729               const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
730 #ifdef _EDF_NODE_IDS_
731               aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
732 #else
733               aTConnSlice[ iNode ] = aNode->GetID();
734 #endif
735             }
736             // element number
737             aPolygoneInfo->SetElemNum( iElem, anElem->GetID() );
738
739             // family number
740             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
741             aPolygoneInfo->SetFamNum( iElem, famNum );
742
743             if ( ++iElem == aPolygoneInfo->GetNbElem() )
744               break;
745           }
746           myMed->SetPolygoneInfo(aPolygoneInfo);
747         }
748
749       } 
750
751       // Treat POLYEDREs
752       // ----------------
753       else if (aElemTypeData->_geomType == ePOLYEDRE )
754       {
755         elemIterator = myMesh->elementGeomIterator( SMDSGeom_POLYHEDRA );
756         
757         if ( nbPolyhedronNodes == 0 ) {
758           // Count nb of nodes
759           while ( elemIterator->more() ) {
760             const SMDS_MeshElement* anElem = elemIterator->next();
761             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
762             if ( !aPolyedre ) continue;
763             nbPolyhedronNodes += aPolyedre->NbNodes();
764             nbPolyhedronFaces += aPolyedre->NbFaces();
765             if ( ++iElem == aElemTypeData->_nbElems )
766               break;
767           }
768         }
769         else {
770           // Store in med file
771           PPolyedreInfo aPolyhInfo = myMed->CrPolyedreInfo(aMeshInfo,
772                                                            aElemTypeData->_entity,
773                                                            aElemTypeData->_geomType,
774                                                            aElemTypeData->_nbElems,
775                                                            nbPolyhedronFaces+1,
776                                                            nbPolyhedronNodes,
777                                                            theConnMode,
778                                                            theIsElemNum,
779                                                            theIsElemNames);
780           TElemNum & index = *(aPolyhInfo->myIndex.get());
781           TElemNum & faces = *(aPolyhInfo->myFaces.get());
782           TElemNum & conn  = *(aPolyhInfo->myConn.get());
783           index[0] = 1;
784           faces[0] = 1;
785
786           TInt iFace = 0, iNode = 0;
787           while ( elemIterator->more() )
788           {
789             const SMDS_MeshElement* anElem = elemIterator->next();
790             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
791             if ( !aPolyedre ) continue;
792             // index
793             TInt aNbFaces = aPolyedre->NbFaces();
794             index[ iElem+1 ] = index[ iElem ] + aNbFaces;
795
796             // face index
797             for (TInt f = 1; f <= aNbFaces; ++f, ++iFace ) {
798               int aNbFaceNodes = aPolyedre->NbFaceNodes( f );
799               faces[ iFace+1 ] = faces[ iFace ] + aNbFaceNodes;
800             }
801             // connectivity
802             SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
803             while ( nodeIt->more() ) {
804               const SMDS_MeshElement* aNode = nodeIt->next();
805 #ifdef _EDF_NODE_IDS_
806               conn[ iNode ] = aNodeIdMap[aNode->GetID()];
807 #else
808               conn[ iNode ] = aNode->GetID();
809 #endif
810               ++iNode;
811             }
812             // element number
813             aPolyhInfo->SetElemNum( iElem, anElem->GetID() );
814
815             // family number
816             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
817             aPolyhInfo->SetFamNum( iElem, famNum );
818
819             if ( ++iElem == aPolyhInfo->GetNbElem() )
820               break;
821           }
822           myMed->SetPolyedreInfo(aPolyhInfo);
823         }
824       } // if (aElemTypeData->_geomType == ePOLYEDRE )
825
826       // Treat BALLs
827       // ----------------
828       else if (aElemTypeData->_geomType == eBALL )
829       {
830         // allocate data arrays
831         PBallInfo aBallInfo = myMed->CrBallInfo( aMeshInfo,
832                                                  aElemTypeData->_nbElems );
833
834         // build map of family numbers for this type
835         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
836         {
837           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
838           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
839         }
840
841         elemIterator = myMesh->elementsIterator( SMDSAbs_Ball );
842         while ( elemIterator->more() )
843         {
844           const SMDS_MeshElement* anElem = elemIterator->next();
845           // connectivity
846           const SMDS_MeshElement* aNode = anElem->GetNode( 0 );
847 #ifdef _EDF_NODE_IDS_
848           (*aBallInfo->myConn)[ iElem ] = aNodeIdMap[aNode->GetID()];
849 #else
850           (*aBallInfo->myConn)[ iElem ] = aNode->GetID();
851 #endif
852           // element number
853           aBallInfo->SetElemNum( iElem, anElem->GetID() );
854
855           // diameter
856           aBallInfo->myDiameters[ iElem ] =
857             static_cast<const SMDS_BallElement*>( anElem )->GetDiameter();
858
859           // family number
860           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
861           aBallInfo->SetFamNum( iElem, famNum );
862           ++iElem;
863         }
864         // store data in a file
865         myMed->SetBallInfo(aBallInfo);
866       }
867
868       else
869       {
870         // Treat standard types
871         // ---------------------
872
873         // allocate data arrays
874         PCellInfo aCellInfo = myMed->CrCellInfo( aMeshInfo,
875                                                  aElemTypeData->_entity,
876                                                  aElemTypeData->_geomType,
877                                                  aElemTypeData->_nbElems,
878                                                  theConnMode,
879                                                  theIsElemNum,
880                                                  theIsElemNames);
881         // build map of family numbers for this type
882         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
883         {
884           //cout << " fillElemFamilyMap()" << endl;
885           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
886           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
887         }
888
889         TInt aNbNodes = MED::GetNbNodes(aElemTypeData->_geomType);
890         elemIterator = myMesh->elementsIterator( aElemTypeData->_smdsType );
891         if ( aElemTypeData->_smdsType == SMDSAbs_0DElement && ! nodesOf0D.empty() )
892           elemIterator = iterVecIter;
893         while ( elemIterator->more() )
894         {
895           const SMDS_MeshElement* anElem = elemIterator->next();
896           if ( anElem->NbNodes() != aNbNodes || anElem->IsPoly() )
897             continue; // other geometry
898
899           // connectivity
900           TConnSlice aTConnSlice = aCellInfo->GetConnSlice( iElem );
901           for (TInt iNode = 0; iNode < aNbNodes; iNode++) {
902             const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
903 #ifdef _EDF_NODE_IDS_
904             aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
905 #else
906             aTConnSlice[ iNode ] = aNode->GetID();
907 #endif
908           }
909           // element number
910           aCellInfo->SetElemNum( iElem, anElem->GetID() );
911
912           // family number
913           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
914           aCellInfo->SetFamNum( iElem, famNum );
915
916           if ( ++iElem == aCellInfo->GetNbElem() )
917             break;
918         }
919         // store data in a file
920         myMed->SetCellInfo(aCellInfo);
921       }
922
923     } // loop on geom types
924
925
926   }
927   catch(const std::exception& exc) {
928     INFOS("The following exception was caught:\n\t"<<exc.what());
929     throw;
930   }
931   catch(...) {
932     INFOS("Unknown exception was caught !!!");
933     throw;
934   }
935
936   myMeshId = -1;
937   myGroups.clear();
938   mySubMeshes.clear();
939   return aResult;
940 }
941
942 //================================================================================
943 /*!
944  * \brief Returns nodes on VERTEXes where 0D elements are absent
945  */
946 //================================================================================
947
948 bool DriverMED_W_SMESHDS_Mesh::
949 getNodesOfMissing0DOnVert(SMESHDS_Mesh*                         meshDS,
950                           std::vector<const SMDS_MeshElement*>& nodes)
951 {
952   nodes.clear();
953   for ( int i = 1; i <= meshDS->MaxShapeIndex(); ++i )
954   {
955     if ( meshDS->IndexToShape( i ).ShapeType() != TopAbs_VERTEX )
956       continue;
957     if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(i) ) {
958       SMDS_NodeIteratorPtr nIt= sm->GetNodes();
959       while (nIt->more())
960       {
961         const SMDS_MeshNode* n = nIt->next();
962         if ( n->NbInverseElements( SMDSAbs_0DElement ) == 0 )
963           nodes.push_back( n );
964       }
965     }
966   }
967   return !nodes.empty();
968 }