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