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