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