Salome HOME
Copyrights update 2015.
[modules/smesh.git] / src / DriverMED / DriverMED_W_SMESHDS_Mesh.cxx
1 // Copyright (C) 2007-2015  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.push_back( 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   /*!
298    * \brief Returns iterator on sub-meshes
299    */
300   //================================================================================
301
302   SMESHDS_SubMeshIteratorPtr getIterator( std::vector<SMESHDS_SubMesh*>& mySubMeshes )
303   {
304     return SMESHDS_SubMeshIteratorPtr
305       ( new SMDS_SetIterator
306         < const SMESHDS_SubMesh*, std::vector< SMESHDS_SubMesh* >::iterator >( mySubMeshes.begin(),
307                                                                                mySubMeshes.end() ));
308   }
309 }
310
311 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::Perform()
312 {
313   Status aResult = DRS_OK;
314   if (myMesh->hasConstructionEdges() || myMesh->hasConstructionFaces()) {
315     INFOS("SMDS_MESH with hasConstructionEdges() or hasConstructionFaces() do not supports!!!");
316     return DRS_FAIL;
317   }
318   try {
319     MESSAGE("Perform - myFile : "<<myFile);
320
321     // Creating the MED mesh for corresponding SMDS structure
322     //-------------------------------------------------------
323     string aMeshName;
324     if (myMeshId != -1) {
325       ostringstream aMeshNameStr;
326       aMeshNameStr<<myMeshId;
327       aMeshName = aMeshNameStr.str();
328     } else {
329       aMeshName = myMeshName;
330     }
331
332     // Mesh dimension definition
333     TInt aSpaceDimension = 3;
334     TCoordHelperPtr aCoordHelperPtr;
335     {
336       bool anIsXDimension = false;
337       bool anIsYDimension = false;
338       bool anIsZDimension = false;
339       if ( myAutoDimension )
340       {
341         SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
342         double aBounds[6];
343         if(aNodesIter->more()){
344           const SMDS_MeshNode* aNode = aNodesIter->next();
345           aBounds[0] = aBounds[1] = aNode->X();
346           aBounds[2] = aBounds[3] = aNode->Y();
347           aBounds[4] = aBounds[5] = aNode->Z();
348         }
349         while(aNodesIter->more()){
350           const SMDS_MeshNode* aNode = aNodesIter->next();
351           aBounds[0] = min(aBounds[0],aNode->X());
352           aBounds[1] = max(aBounds[1],aNode->X());
353           
354           aBounds[2] = min(aBounds[2],aNode->Y());
355           aBounds[3] = max(aBounds[3],aNode->Y());
356           
357           aBounds[4] = min(aBounds[4],aNode->Z());
358           aBounds[5] = max(aBounds[5],aNode->Z());
359         }
360
361         double EPS = 1.0E-7;
362         anIsXDimension = (aBounds[1] - aBounds[0]) + abs(aBounds[1]) + abs(aBounds[0]) > EPS;
363         anIsYDimension = (aBounds[3] - aBounds[2]) + abs(aBounds[3]) + abs(aBounds[2]) > EPS;
364         anIsZDimension = (aBounds[5] - aBounds[4]) + abs(aBounds[5]) + abs(aBounds[4]) > EPS;
365         aSpaceDimension = anIsXDimension + anIsYDimension + anIsZDimension;
366         if(!aSpaceDimension)
367           aSpaceDimension = 3;
368         // PAL16857(SMESH not conform to the MED convention):
369         if ( aSpaceDimension == 2 && anIsZDimension ) // 2D only if mesh is in XOY plane
370           aSpaceDimension = 3;
371         // PAL18941(a saved study with a mesh belong Z is opened and the mesh is belong X)
372         if ( aSpaceDimension == 1 && !anIsXDimension ) {// 1D only if mesh is along OX
373           if ( anIsYDimension ) {
374             aSpaceDimension = 2;
375             anIsXDimension = true;
376           } else {
377             aSpaceDimension = 3;
378           }
379         }
380       }
381
382       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator(/*idInceasingOrder=*/true);
383       switch(aSpaceDimension){
384       case 3:
385         aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYZGetCoord,aXYZName));
386         break;
387       case 2:
388         if(anIsXDimension && anIsYDimension)
389           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYGetCoord,aXYName));
390         if(anIsYDimension && anIsZDimension)
391           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYZGetCoord,aYZName));
392         if(anIsXDimension && anIsZDimension)
393           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXZGetCoord,aXZName));
394         break;
395       case 1:
396         if(anIsXDimension)
397           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXGetCoord,aXName));
398         if(anIsYDimension)
399           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYGetCoord,aYName));
400         if(anIsZDimension)
401           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aZGetCoord,aZName));
402         break;
403       }
404     }
405     TInt aMeshDimension = 0;
406     if ( myMesh->NbEdges() > 0 )
407       aMeshDimension = 1;
408     if ( myMesh->NbFaces() > 0 )
409       aMeshDimension = 2;
410     if ( myMesh->NbVolumes() > 0 )
411       aMeshDimension = 3;
412     
413     MED::PWrapper myMed = CrWrapper(myFile,myMedVersion);
414     PMeshInfo aMeshInfo = myMed->CrMeshInfo(aMeshDimension,aSpaceDimension,aMeshName);
415     MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
416     myMed->SetMeshInfo(aMeshInfo);
417
418     // Storing SMDS groups and sub-meshes as med families
419     //----------------------------------------------------
420     int myNodesDefaultFamilyId      = 0;
421     int my0DElementsDefaultFamilyId = 0;
422     int myBallsDefaultFamilyId      = 0;
423     int myEdgesDefaultFamilyId      = 0;
424     int myFacesDefaultFamilyId      = 0;
425     int myVolumesDefaultFamilyId    = 0;
426     int nbNodes      = myMesh->NbNodes();
427     int nb0DElements = myMesh->Nb0DElements();
428     int nbBalls      = myMesh->NbBalls();
429     int nbEdges      = myMesh->NbEdges();
430     int nbFaces      = myMesh->NbFaces();
431     int nbVolumes    = myMesh->NbVolumes();
432     if (myDoGroupOfNodes && nbNodes) myNodesDefaultFamilyId = REST_NODES_FAMILY;
433     if (myDoGroupOfEdges && nbEdges) myEdgesDefaultFamilyId = REST_EDGES_FAMILY;
434     if (myDoGroupOfFaces && nbFaces) myFacesDefaultFamilyId = REST_FACES_FAMILY;
435     if (myDoGroupOfVolumes && nbVolumes) myVolumesDefaultFamilyId = REST_VOLUMES_FAMILY;
436     if (myDoGroupOf0DElems && nb0DElements) my0DElementsDefaultFamilyId = REST_0DELEM_FAMILY;
437     if (myDoGroupOfBalls && nbBalls) myBallsDefaultFamilyId = REST_BALL_FAMILY;
438
439     MESSAGE("Perform - aFamilyInfo");
440     list<DriverMED_FamilyPtr> aFamilies;
441     if (myAllSubMeshes) {
442       aFamilies = DriverMED_Family::MakeFamilies
443         (myMesh->SubMeshes(), myGroups,
444          myDoGroupOfNodes   && nbNodes,
445          myDoGroupOfEdges   && nbEdges,
446          myDoGroupOfFaces   && nbFaces,
447          myDoGroupOfVolumes && nbVolumes,
448          myDoGroupOf0DElems && nb0DElements,
449          myDoGroupOfBalls   && nbBalls);
450     } else {
451       aFamilies = DriverMED_Family::MakeFamilies
452         (getIterator( mySubMeshes ), myGroups,
453          myDoGroupOfNodes   && nbNodes,
454          myDoGroupOfEdges   && nbEdges,
455          myDoGroupOfFaces   && nbFaces,
456          myDoGroupOfVolumes && nbVolumes,
457          myDoGroupOf0DElems && nb0DElements,
458          myDoGroupOfBalls   && nbBalls);
459     }
460     list<DriverMED_FamilyPtr>::iterator aFamsIter;
461     for (aFamsIter = aFamilies.begin(); aFamsIter != aFamilies.end(); aFamsIter++)
462     {
463       PFamilyInfo aFamilyInfo = (*aFamsIter)->GetFamilyInfo(myMed,aMeshInfo);
464       myMed->SetFamilyInfo(aFamilyInfo);
465     }
466
467     // Storing SMDS nodes to the MED file for the MED mesh
468     //----------------------------------------------------
469 #ifdef _EDF_NODE_IDS_
470     typedef map<TInt,TInt> TNodeIdMap;
471     TNodeIdMap aNodeIdMap;
472 #endif
473     const EModeSwitch   theMode        = eFULL_INTERLACE;
474     const ERepere       theSystem      = eCART;
475     const EBooleen      theIsElemNum   = eVRAI;
476     const EBooleen      theIsElemNames = eFAUX;
477     const EConnectivite theConnMode    = eNOD;
478
479     TInt aNbNodes = myMesh->NbNodes();
480     PNodeInfo aNodeInfo = myMed->CrNodeInfo(aMeshInfo, aNbNodes,
481                                             theMode, theSystem, theIsElemNum, theIsElemNames);
482
483     //cout << " fillElemFamilyMap( SMDSAbs_Node )" << endl;
484     // find family numbers for nodes
485     TElemFamilyMap anElemFamMap;
486     fillElemFamilyMap( anElemFamMap, aFamilies, SMDSAbs_Node );
487
488     for (TInt iNode = 0; aCoordHelperPtr->Next(); iNode++)
489     {
490       // coordinates
491       TCoordSlice aTCoordSlice = aNodeInfo->GetCoordSlice( iNode );
492       for(TInt iCoord = 0; iCoord < aSpaceDimension; iCoord++){
493         aTCoordSlice[iCoord] = aCoordHelperPtr->GetCoord(iCoord);
494       }
495       // node number
496       int aNodeID = aCoordHelperPtr->GetID();
497       aNodeInfo->SetElemNum( iNode, aNodeID );
498 #ifdef _EDF_NODE_IDS_
499       aNodeIdMap.insert( aNodeIdMap.end(), make_pair( aNodeID, iNode+1 ));
500 #endif
501       // family number
502       const SMDS_MeshNode* aNode = aCoordHelperPtr->GetNode();
503       int famNum = getFamilyId( anElemFamMap, aNode, myNodesDefaultFamilyId );
504       aNodeInfo->SetFamNum( iNode, famNum );
505     }
506     anElemFamMap.Clear();
507
508     // coordinate names and units
509     for (TInt iCoord = 0; iCoord < aSpaceDimension; iCoord++) {
510       aNodeInfo->SetCoordName( iCoord, aCoordHelperPtr->GetName(iCoord));
511       aNodeInfo->SetCoordUnit( iCoord, aCoordHelperPtr->GetUnit(iCoord));
512     }
513
514     //cout << " SetNodeInfo(aNodeInfo)" << endl;
515     MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbNodes);
516     myMed->SetNodeInfo(aNodeInfo);
517     aNodeInfo.reset(); // free memory used for arrays
518
519
520     // Storing SMDS elements to the MED file for the MED mesh
521     //-------------------------------------------------------
522     // Write one element type at once in order to minimize memory usage (PAL19276)
523
524     const SMDS_MeshInfo& nbElemInfo = myMesh->GetMeshInfo();
525
526     // poly elements are not supported by med-2.1
527     bool polyTypesSupported = myMed->CrPolygoneInfo(aMeshInfo,eMAILLE,ePOLYGONE,0,0);
528     TInt nbPolygonNodes = 0, nbPolyhedronNodes = 0, nbPolyhedronFaces = 0;
529
530     // nodes on VERTEXes where 0D elements are absent
531     std::vector<const SMDS_MeshElement*> nodesOf0D;
532     std::vector< SMDS_ElemIteratorPtr > iterVec;
533     SMDS_ElemIteratorPtr iterVecIter;
534     if ( myAddODOnVertices && getNodesOfMissing0DOnVert( myMesh, nodesOf0D ))
535     {
536       iterVec.resize(2);
537       iterVec[0] = myMesh->elementsIterator( SMDSAbs_0DElement );
538       iterVec[1] = SMDS_ElemIteratorPtr
539         ( new SMDS_ElementVectorIterator( nodesOf0D.begin(), nodesOf0D.end() ));
540
541       typedef SMDS_IteratorOnIterators
542         < const SMDS_MeshElement *, std::vector< SMDS_ElemIteratorPtr > > TItIterator;
543       iterVecIter = SMDS_ElemIteratorPtr( new TItIterator( iterVec ));
544     }
545
546     // collect info on all geom types
547
548     list< TElemTypeData > aTElemTypeDatas;
549
550     EEntiteMaillage anEntity = eMAILLE;
551 #ifdef _ELEMENTS_BY_DIM_
552     anEntity = eNOEUD_ELEMENT;
553 #endif
554     aTElemTypeDatas.push_back(TElemTypeData(anEntity,
555                                             ePOINT1,
556                                             nbElemInfo.Nb0DElements() + nodesOf0D.size(),
557                                             SMDSAbs_0DElement));
558 #ifdef _ELEMENTS_BY_DIM_
559     anEntity = eSTRUCT_ELEMENT;
560 #endif
561     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
562                                              eBALL,
563                                              nbElemInfo.NbBalls(),
564                                              SMDSAbs_Ball));
565 #ifdef _ELEMENTS_BY_DIM_
566     anEntity = eARETE;
567 #endif
568     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
569                                              eSEG2,
570                                              nbElemInfo.NbEdges( ORDER_LINEAR ),
571                                              SMDSAbs_Edge));
572     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
573                                              eSEG3,
574                                              nbElemInfo.NbEdges( ORDER_QUADRATIC ),
575                                              SMDSAbs_Edge));
576 #ifdef _ELEMENTS_BY_DIM_
577     anEntity = eFACE;
578 #endif
579     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
580                                              eTRIA3,
581                                              nbElemInfo.NbTriangles( ORDER_LINEAR ),
582                                              SMDSAbs_Face));
583     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
584                                              eTRIA6,
585                                              nbElemInfo.NbTriangles( ORDER_QUADRATIC ) -
586                                              nbElemInfo.NbBiQuadTriangles(),
587                                              SMDSAbs_Face));
588     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
589                                              eTRIA7,
590                                              nbElemInfo.NbBiQuadTriangles(),
591                                              SMDSAbs_Face));
592     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
593                                              eQUAD4,
594                                              nbElemInfo.NbQuadrangles( ORDER_LINEAR ),
595                                              SMDSAbs_Face));
596     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
597                                              eQUAD8,
598                                              nbElemInfo.NbQuadrangles( ORDER_QUADRATIC ) -
599                                              nbElemInfo.NbBiQuadQuadrangles(),
600                                              SMDSAbs_Face));
601     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
602                                              eQUAD9,
603                                              nbElemInfo.NbBiQuadQuadrangles(),
604                                              SMDSAbs_Face));
605     if ( polyTypesSupported ) {
606       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
607                                                ePOLYGONE,
608                                                nbElemInfo.NbPolygons(),
609                                                SMDSAbs_Face));
610       // we need one more loop on poly elements to count nb of their nodes
611       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
612                                                ePOLYGONE,
613                                                nbElemInfo.NbPolygons(),
614                                                SMDSAbs_Face));
615     }
616 #ifdef _ELEMENTS_BY_DIM_
617     anEntity = eMAILLE;
618 #endif
619     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
620                                              eTETRA4,
621                                              nbElemInfo.NbTetras( ORDER_LINEAR ),
622                                              SMDSAbs_Volume));
623     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
624                                              eTETRA10,
625                                              nbElemInfo.NbTetras( ORDER_QUADRATIC ),
626                                              SMDSAbs_Volume));
627     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
628                                              ePYRA5,
629                                              nbElemInfo.NbPyramids( ORDER_LINEAR ),
630                                              SMDSAbs_Volume));
631     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
632                                              ePYRA13,
633                                              nbElemInfo.NbPyramids( ORDER_QUADRATIC ),
634                                              SMDSAbs_Volume));
635     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
636                                              ePENTA6,
637                                              nbElemInfo.NbPrisms( ORDER_LINEAR ),
638                                              SMDSAbs_Volume));
639     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
640                                              ePENTA15,
641                                              nbElemInfo.NbPrisms( ORDER_QUADRATIC ),
642                                              SMDSAbs_Volume));
643     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
644                                              eHEXA8,
645                                              nbElemInfo.NbHexas( ORDER_LINEAR ),
646                                              SMDSAbs_Volume));
647     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
648                                              eHEXA20,
649                                              nbElemInfo.NbHexas( ORDER_QUADRATIC )-
650                                              nbElemInfo.NbTriQuadHexas(),
651                                              SMDSAbs_Volume));
652     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
653                                              eHEXA27,
654                                              nbElemInfo.NbTriQuadHexas(),
655                                              SMDSAbs_Volume));
656     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
657                                              eOCTA12,
658                                              nbElemInfo.NbHexPrisms(),
659                                              SMDSAbs_Volume));
660     if ( polyTypesSupported ) {
661       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
662                                                ePOLYEDRE,
663                                                nbElemInfo.NbPolyhedrons(),
664                                                SMDSAbs_Volume));
665       // we need one more loop on poly elements to count nb of their nodes
666       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
667                                                ePOLYEDRE,
668                                                nbElemInfo.NbPolyhedrons(),
669                                                SMDSAbs_Volume));
670     }
671
672     vector< bool > isElemFamMapBuilt( SMDSAbs_NbElementTypes, false );
673
674     // loop on all geom types of elements
675
676     list< TElemTypeData >::iterator aElemTypeData = aTElemTypeDatas.begin();
677     for ( ; aElemTypeData != aTElemTypeDatas.end(); ++aElemTypeData )
678     {
679       if ( aElemTypeData->_nbElems == 0 )
680         continue;
681
682       int defaultFamilyId = 0;
683       switch ( aElemTypeData->_smdsType ) {
684       case SMDSAbs_0DElement:
685         defaultFamilyId = my0DElementsDefaultFamilyId;
686         break;
687       case SMDSAbs_Ball:
688         defaultFamilyId = myBallsDefaultFamilyId;
689         break;
690       case SMDSAbs_Edge:
691         defaultFamilyId = myEdgesDefaultFamilyId;
692         break;
693       case SMDSAbs_Face:
694         defaultFamilyId = myFacesDefaultFamilyId;
695         break;
696       case SMDSAbs_Volume:
697         defaultFamilyId = myVolumesDefaultFamilyId;
698         break;
699       default:
700         continue;
701       }
702
703       // iterator on elements of a current type
704       SMDS_ElemIteratorPtr elemIterator;
705       int iElem = 0;
706
707       // Treat POLYGONs
708       // ---------------
709       if ( aElemTypeData->_geomType == ePOLYGONE )
710       {
711         elemIterator = myMesh->elementGeomIterator( SMDSGeom_POLYGON );
712         if ( nbPolygonNodes == 0 ) {
713           // Count nb of nodes
714           while ( elemIterator->more() ) {
715             const SMDS_MeshElement* anElem = elemIterator->next();
716             nbPolygonNodes += anElem->NbNodes();
717             if ( ++iElem == aElemTypeData->_nbElems )
718               break;
719           }
720         }
721         else {
722           // Store in med file
723           PPolygoneInfo aPolygoneInfo = myMed->CrPolygoneInfo(aMeshInfo,
724                                                               aElemTypeData->_entity,
725                                                               aElemTypeData->_geomType,
726                                                               aElemTypeData->_nbElems,
727                                                               nbPolygonNodes,
728                                                               theConnMode, theIsElemNum,
729                                                               theIsElemNames);
730           TElemNum & index = *(aPolygoneInfo->myIndex.get());
731           index[0] = 1;
732
733           while ( elemIterator->more() )
734           {
735             const SMDS_MeshElement* anElem = elemIterator->next();
736             // index
737             TInt aNbNodes = anElem->NbNodes();
738             index[ iElem+1 ] = index[ iElem ] + aNbNodes;
739
740             // connectivity
741             TConnSlice aTConnSlice = aPolygoneInfo->GetConnSlice( iElem );
742             for(TInt iNode = 0; iNode < aNbNodes; iNode++) {
743               const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
744 #ifdef _EDF_NODE_IDS_
745               aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
746 #else
747               aTConnSlice[ iNode ] = aNode->GetID();
748 #endif
749             }
750             // element number
751             aPolygoneInfo->SetElemNum( iElem, anElem->GetID() );
752
753             // family number
754             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
755             aPolygoneInfo->SetFamNum( iElem, famNum );
756
757             if ( ++iElem == aPolygoneInfo->GetNbElem() )
758               break;
759           }
760           myMed->SetPolygoneInfo(aPolygoneInfo);
761         }
762
763       } 
764
765       // Treat POLYEDREs
766       // ----------------
767       else if (aElemTypeData->_geomType == ePOLYEDRE )
768       {
769         elemIterator = myMesh->elementGeomIterator( SMDSGeom_POLYHEDRA );
770         
771         if ( nbPolyhedronNodes == 0 ) {
772           // Count nb of nodes
773           while ( elemIterator->more() ) {
774             const SMDS_MeshElement* anElem = elemIterator->next();
775             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
776             if ( !aPolyedre ) continue;
777             nbPolyhedronNodes += aPolyedre->NbNodes();
778             nbPolyhedronFaces += aPolyedre->NbFaces();
779             if ( ++iElem == aElemTypeData->_nbElems )
780               break;
781           }
782         }
783         else {
784           // Store in med file
785           PPolyedreInfo aPolyhInfo = myMed->CrPolyedreInfo(aMeshInfo,
786                                                            aElemTypeData->_entity,
787                                                            aElemTypeData->_geomType,
788                                                            aElemTypeData->_nbElems,
789                                                            nbPolyhedronFaces+1,
790                                                            nbPolyhedronNodes,
791                                                            theConnMode,
792                                                            theIsElemNum,
793                                                            theIsElemNames);
794           TElemNum & index = *(aPolyhInfo->myIndex.get());
795           TElemNum & faces = *(aPolyhInfo->myFaces.get());
796           TElemNum & conn  = *(aPolyhInfo->myConn.get());
797           index[0] = 1;
798           faces[0] = 1;
799
800           TInt iFace = 0, iNode = 0;
801           while ( elemIterator->more() )
802           {
803             const SMDS_MeshElement* anElem = elemIterator->next();
804             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
805             if ( !aPolyedre ) continue;
806             // index
807             TInt aNbFaces = aPolyedre->NbFaces();
808             index[ iElem+1 ] = index[ iElem ] + aNbFaces;
809
810             // face index
811             for (TInt f = 1; f <= aNbFaces; ++f, ++iFace ) {
812               int aNbFaceNodes = aPolyedre->NbFaceNodes( f );
813               faces[ iFace+1 ] = faces[ iFace ] + aNbFaceNodes;
814             }
815             // connectivity
816             SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
817             while ( nodeIt->more() ) {
818               const SMDS_MeshElement* aNode = nodeIt->next();
819 #ifdef _EDF_NODE_IDS_
820               conn[ iNode ] = aNodeIdMap[aNode->GetID()];
821 #else
822               conn[ iNode ] = aNode->GetID();
823 #endif
824               ++iNode;
825             }
826             // element number
827             aPolyhInfo->SetElemNum( iElem, anElem->GetID() );
828
829             // family number
830             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
831             aPolyhInfo->SetFamNum( iElem, famNum );
832
833             if ( ++iElem == aPolyhInfo->GetNbElem() )
834               break;
835           }
836           myMed->SetPolyedreInfo(aPolyhInfo);
837         }
838       } // if (aElemTypeData->_geomType == ePOLYEDRE )
839
840       // Treat BALLs
841       // ----------------
842       else if (aElemTypeData->_geomType == eBALL )
843       {
844         // allocate data arrays
845         PBallInfo aBallInfo = myMed->CrBallInfo( aMeshInfo,
846                                                  aElemTypeData->_nbElems );
847
848         // build map of family numbers for this type
849         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
850         {
851           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
852           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
853         }
854
855         elemIterator = myMesh->elementsIterator( SMDSAbs_Ball );
856         while ( elemIterator->more() )
857         {
858           const SMDS_MeshElement* anElem = elemIterator->next();
859           // connectivity
860           const SMDS_MeshElement* aNode = anElem->GetNode( 0 );
861 #ifdef _EDF_NODE_IDS_
862           (*aBallInfo->myConn)[ iElem ] = aNodeIdMap[aNode->GetID()];
863 #else
864           (*aBallInfo->myConn)[ iElem ] = aNode->GetID();
865 #endif
866           // element number
867           aBallInfo->SetElemNum( iElem, anElem->GetID() );
868
869           // diameter
870           aBallInfo->myDiameters[ iElem ] =
871             static_cast<const SMDS_BallElement*>( anElem )->GetDiameter();
872
873           // family number
874           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
875           aBallInfo->SetFamNum( iElem, famNum );
876           ++iElem;
877         }
878         // store data in a file
879         myMed->SetBallInfo(aBallInfo);
880       }
881
882       else
883       {
884         // Treat standard types
885         // ---------------------
886
887         // allocate data arrays
888         PCellInfo aCellInfo = myMed->CrCellInfo( aMeshInfo,
889                                                  aElemTypeData->_entity,
890                                                  aElemTypeData->_geomType,
891                                                  aElemTypeData->_nbElems,
892                                                  theConnMode,
893                                                  theIsElemNum,
894                                                  theIsElemNames);
895         // build map of family numbers for this type
896         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
897         {
898           //cout << " fillElemFamilyMap()" << endl;
899           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
900           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
901         }
902
903         TInt aNbNodes = MED::GetNbNodes(aElemTypeData->_geomType);
904         elemIterator = myMesh->elementsIterator( aElemTypeData->_smdsType );
905         if ( aElemTypeData->_smdsType == SMDSAbs_0DElement && ! nodesOf0D.empty() )
906           elemIterator = iterVecIter;
907         while ( elemIterator->more() )
908         {
909           const SMDS_MeshElement* anElem = elemIterator->next();
910           if ( anElem->NbNodes() != aNbNodes || anElem->IsPoly() )
911             continue; // other geometry
912
913           // connectivity
914           TConnSlice aTConnSlice = aCellInfo->GetConnSlice( iElem );
915           for (TInt iNode = 0; iNode < aNbNodes; iNode++) {
916             const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
917 #ifdef _EDF_NODE_IDS_
918             aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
919 #else
920             aTConnSlice[ iNode ] = aNode->GetID();
921 #endif
922           }
923           // element number
924           aCellInfo->SetElemNum( iElem, anElem->GetID() );
925
926           // family number
927           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
928           aCellInfo->SetFamNum( iElem, famNum );
929
930           if ( ++iElem == aCellInfo->GetNbElem() )
931             break;
932         }
933         // store data in a file
934         myMed->SetCellInfo(aCellInfo);
935       }
936
937     } // loop on geom types
938
939
940   }
941   catch(const std::exception& exc) {
942     INFOS("The following exception was caught:\n\t"<<exc.what());
943     throw;
944   }
945   catch(...) {
946     INFOS("Unknown exception was caught !!!");
947     throw;
948   }
949
950   myMeshId = -1;
951   myGroups.clear();
952   mySubMeshes.clear();
953   return aResult;
954 }
955
956 //================================================================================
957 /*!
958  * \brief Returns nodes on VERTEXes where 0D elements are absent
959  */
960 //================================================================================
961
962 bool DriverMED_W_SMESHDS_Mesh::
963 getNodesOfMissing0DOnVert(SMESHDS_Mesh*                         meshDS,
964                           std::vector<const SMDS_MeshElement*>& nodes)
965 {
966   nodes.clear();
967   for ( int i = 1; i <= meshDS->MaxShapeIndex(); ++i )
968   {
969     if ( meshDS->IndexToShape( i ).ShapeType() != TopAbs_VERTEX )
970       continue;
971     if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(i) ) {
972       SMDS_NodeIteratorPtr nIt= sm->GetNodes();
973       while (nIt->more())
974       {
975         const SMDS_MeshNode* n = nIt->next();
976         if ( n->NbInverseElements( SMDSAbs_0DElement ) == 0 )
977           nodes.push_back( n );
978       }
979     }
980   }
981   return !nodes.empty();
982 }