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