Salome HOME
0021803: EDF 2351 : Available versions of MED in TUI function ExportMED aren't consis...
[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.NbPrisms( ORDER_QUADRATIC ),
681                                              SMDSAbs_Volume));
682     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
683                                              eHEXA8,
684                                              nbElemInfo.NbHexas( ORDER_LINEAR ),
685                                              SMDSAbs_Volume));
686     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
687                                              eHEXA20,
688                                              nbElemInfo.NbHexas( ORDER_QUADRATIC )-
689                                              nbElemInfo.NbTriQuadHexas(),
690                                              SMDSAbs_Volume));
691     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
692                                              eHEXA27,
693                                              nbElemInfo.NbTriQuadHexas(),
694                                              SMDSAbs_Volume));
695     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
696                                              eOCTA12,
697                                              nbElemInfo.NbHexPrisms(),
698                                              SMDSAbs_Volume));
699     if ( polyTypesSupported ) {
700       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
701                                                ePOLYEDRE,
702                                                nbElemInfo.NbPolyhedrons(),
703                                                SMDSAbs_Volume));
704       // we need one more loop on poly elements to count nb of their nodes
705       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
706                                                ePOLYEDRE,
707                                                nbElemInfo.NbPolyhedrons(),
708                                                SMDSAbs_Volume));
709     }
710
711     vector< bool > isElemFamMapBuilt( SMDSAbs_NbElementTypes, false );
712
713     // loop on all geom types of elements
714
715     list< TElemTypeData >::iterator aElemTypeData = aTElemTypeDatas.begin();
716     for ( ; aElemTypeData != aTElemTypeDatas.end(); ++aElemTypeData )
717     {
718       if ( aElemTypeData->_nbElems == 0 )
719         continue;
720
721       int defaultFamilyId = 0;
722       switch ( aElemTypeData->_smdsType ) {
723       case SMDSAbs_0DElement: defaultFamilyId = my0DElementsDefaultFamilyId; break;
724       case SMDSAbs_Ball:      defaultFamilyId = myBallsDefaultFamilyId;      break;
725       case SMDSAbs_Edge:      defaultFamilyId = myEdgesDefaultFamilyId;      break;
726       case SMDSAbs_Face:      defaultFamilyId = myFacesDefaultFamilyId;      break;
727       case SMDSAbs_Volume:    defaultFamilyId = myVolumesDefaultFamilyId;    break;
728       default:
729         continue;
730       }
731
732       // iterator on elements of a current type
733       SMDS_ElemIteratorPtr elemIterator;
734       int iElem = 0;
735
736       // Treat POLYGONs
737       // ---------------
738       if ( aElemTypeData->_geomType == ePOLYGONE ||
739            aElemTypeData->_geomType == ePOLYGON2 )
740       {
741         if ( aElemTypeData->_geomType == ePOLYGONE )
742           elemIterator = myMesh->elementEntityIterator( SMDSEntity_Polygon );
743         else
744           elemIterator = myMesh->elementEntityIterator( SMDSEntity_Quad_Polygon );
745
746         if ( nbPolygonNodes == 0 ) {
747           // Count nb of nodes
748           while ( elemIterator->more() ) {
749             const SMDS_MeshElement* anElem = elemIterator->next();
750             nbPolygonNodes += anElem->NbNodes();
751             if ( ++iElem == aElemTypeData->_nbElems )
752               break;
753           }
754         }
755         else {
756           // Store in med file
757           PPolygoneInfo aPolygoneInfo = myMed->CrPolygoneInfo(aMeshInfo,
758                                                               aElemTypeData->_entity,
759                                                               aElemTypeData->_geomType,
760                                                               aElemTypeData->_nbElems,
761                                                               nbPolygonNodes,
762                                                               theConnMode, theIsElemNum,
763                                                               theIsElemNames);
764           TElemNum & index = *(aPolygoneInfo->myIndex.get());
765           index[0] = 1;
766
767           while ( elemIterator->more() )
768           {
769             const SMDS_MeshElement* anElem = elemIterator->next();
770             // index
771             TInt aNbNodes = anElem->NbNodes();
772             index[ iElem+1 ] = index[ iElem ] + aNbNodes;
773
774             // connectivity
775             TConnSlice aTConnSlice = aPolygoneInfo->GetConnSlice( iElem );
776             for(TInt iNode = 0; iNode < aNbNodes; iNode++) {
777               const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
778 #ifdef _EDF_NODE_IDS_
779               aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
780 #else
781               aTConnSlice[ iNode ] = aNode->GetID();
782 #endif
783             }
784             // element number
785             aPolygoneInfo->SetElemNum( iElem, anElem->GetID() );
786
787             // family number
788             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
789             aPolygoneInfo->SetFamNum( iElem, famNum );
790
791             if ( ++iElem == aPolygoneInfo->GetNbElem() )
792               break;
793           }
794           myMed->SetPolygoneInfo(aPolygoneInfo);
795
796           nbPolygonNodes = 0; // to treat next polygon type
797         }
798       }
799
800       // Treat POLYEDREs
801       // ----------------
802       else if (aElemTypeData->_geomType == ePOLYEDRE )
803       {
804         elemIterator = myMesh->elementGeomIterator( SMDSGeom_POLYHEDRA );
805         
806         if ( nbPolyhedronNodes == 0 ) {
807           // Count nb of nodes
808           while ( elemIterator->more() ) {
809             const SMDS_MeshElement*  anElem = elemIterator->next();
810             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
811             if ( !aPolyedre ) continue;
812             nbPolyhedronNodes += aPolyedre->NbNodes();
813             nbPolyhedronFaces += aPolyedre->NbFaces();
814             if ( ++iElem == aElemTypeData->_nbElems )
815               break;
816           }
817         }
818         else {
819           // Store in med file
820           PPolyedreInfo aPolyhInfo = myMed->CrPolyedreInfo(aMeshInfo,
821                                                            aElemTypeData->_entity,
822                                                            aElemTypeData->_geomType,
823                                                            aElemTypeData->_nbElems,
824                                                            nbPolyhedronFaces+1,
825                                                            nbPolyhedronNodes,
826                                                            theConnMode,
827                                                            theIsElemNum,
828                                                            theIsElemNames);
829           TElemNum & index = *(aPolyhInfo->myIndex.get());
830           TElemNum & faces = *(aPolyhInfo->myFaces.get());
831           TElemNum & conn  = *(aPolyhInfo->myConn.get());
832           index[0] = 1;
833           faces[0] = 1;
834
835           TInt iFace = 0, iNode = 0;
836           while ( elemIterator->more() )
837           {
838             const SMDS_MeshElement*  anElem = elemIterator->next();
839             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
840             if ( !aPolyedre ) continue;
841             // index
842             TInt aNbFaces = aPolyedre->NbFaces();
843             index[ iElem+1 ] = index[ iElem ] + aNbFaces;
844
845             // face index
846             for (TInt f = 1; f <= aNbFaces; ++f, ++iFace ) {
847               int aNbFaceNodes = aPolyedre->NbFaceNodes( f );
848               faces[ iFace+1 ] = faces[ iFace ] + aNbFaceNodes;
849             }
850             // connectivity
851             SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
852             while ( nodeIt->more() ) {
853               const SMDS_MeshElement* aNode = nodeIt->next();
854 #ifdef _EDF_NODE_IDS_
855               conn[ iNode ] = aNodeIdMap[aNode->GetID()];
856 #else
857               conn[ iNode ] = aNode->GetID();
858 #endif
859               ++iNode;
860             }
861             // element number
862             aPolyhInfo->SetElemNum( iElem, anElem->GetID() );
863
864             // family number
865             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
866             aPolyhInfo->SetFamNum( iElem, famNum );
867
868             if ( ++iElem == aPolyhInfo->GetNbElem() )
869               break;
870           }
871           myMed->SetPolyedreInfo(aPolyhInfo);
872         }
873       } // if (aElemTypeData->_geomType == ePOLYEDRE )
874
875       // Treat BALLs
876       // ----------------
877       else if (aElemTypeData->_geomType == eBALL )
878       {
879         // allocate data arrays
880         PBallInfo aBallInfo = myMed->CrBallInfo( aMeshInfo, aElemTypeData->_nbElems );
881
882         // build map of family numbers for this type
883         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
884         {
885           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
886           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
887         }
888
889         elemIterator = myMesh->elementsIterator( SMDSAbs_Ball );
890         while ( elemIterator->more() )
891         {
892           const SMDS_MeshElement* anElem = elemIterator->next();
893           // connectivity
894           const SMDS_MeshElement* aNode = anElem->GetNode( 0 );
895 #ifdef _EDF_NODE_IDS_
896           (*aBallInfo->myConn)[ iElem ] = aNodeIdMap[aNode->GetID()];
897 #else
898           (*aBallInfo->myConn)[ iElem ] = aNode->GetID();
899 #endif
900           // element number
901           aBallInfo->SetElemNum( iElem, anElem->GetID() );
902
903           // diameter
904           aBallInfo->myDiameters[ iElem ] =
905             static_cast<const SMDS_BallElement*>( anElem )->GetDiameter();
906
907           // family number
908           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
909           aBallInfo->SetFamNum( iElem, famNum );
910           ++iElem;
911         }
912         // store data in a file
913         myMed->SetBallInfo(aBallInfo);
914       }
915
916       else
917       {
918         // Treat standard types
919         // ---------------------
920
921         // allocate data arrays
922         PCellInfo aCellInfo = myMed->CrCellInfo( aMeshInfo,
923                                                  aElemTypeData->_entity,
924                                                  aElemTypeData->_geomType,
925                                                  aElemTypeData->_nbElems,
926                                                  theConnMode,
927                                                  theIsElemNum,
928                                                  theIsElemNames);
929         // build map of family numbers for this type
930         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
931         {
932           //cout << " fillElemFamilyMap()" << endl;
933           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
934           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
935         }
936
937         TInt aNbNodes = MED::GetNbNodes(aElemTypeData->_geomType);
938         elemIterator = myMesh->elementsIterator( aElemTypeData->_smdsType );
939         if ( aElemTypeData->_smdsType == SMDSAbs_0DElement && ! nodesOf0D.empty() )
940           elemIterator = iterVecIter;
941         while ( elemIterator->more() )
942         {
943           const SMDS_MeshElement* anElem = elemIterator->next();
944           if ( anElem->NbNodes() != aNbNodes || anElem->IsPoly() )
945             continue; // other geometry
946
947           // connectivity
948           TConnSlice aTConnSlice = aCellInfo->GetConnSlice( iElem );
949           for (TInt iNode = 0; iNode < aNbNodes; iNode++) {
950             const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
951 #ifdef _EDF_NODE_IDS_
952             aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
953 #else
954             aTConnSlice[ iNode ] = aNode->GetID();
955 #endif
956           }
957           // element number
958           aCellInfo->SetElemNum( iElem, anElem->GetID() );
959
960           // family number
961           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
962           aCellInfo->SetFamNum( iElem, famNum );
963
964           if ( ++iElem == aCellInfo->GetNbElem() )
965             break;
966         }
967         // store data in a file
968         myMed->SetCellInfo(aCellInfo);
969       }
970
971     } // loop on geom types
972
973
974   }
975   catch(const std::exception& exc) {
976     INFOS("The following exception was caught:\n\t"<<exc.what());
977     throw;
978   }
979   catch(...) {
980     INFOS("Unknown exception was caught !!!");
981     throw;
982   }
983
984   myMeshId = -1;
985   myGroups.clear();
986   mySubMeshes.clear();
987   return aResult;
988 }
989
990 //================================================================================
991 /*!
992  * \brief Returns nodes on VERTEXes where 0D elements are absent
993  */
994 //================================================================================
995
996 bool DriverMED_W_SMESHDS_Mesh::
997 getNodesOfMissing0DOnVert(SMESHDS_Mesh*                         meshDS,
998                           std::vector<const SMDS_MeshElement*>& nodes)
999 {
1000   nodes.clear();
1001   for ( int i = 1; i <= meshDS->MaxShapeIndex(); ++i )
1002   {
1003     if ( meshDS->IndexToShape( i ).ShapeType() != TopAbs_VERTEX )
1004       continue;
1005     if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(i) ) {
1006       SMDS_NodeIteratorPtr nIt= sm->GetNodes();
1007       while (nIt->more())
1008       {
1009         const SMDS_MeshNode* n = nIt->next();
1010         if ( n->NbInverseElements( SMDSAbs_0DElement ) == 0 )
1011           nodes.push_back( n );
1012       }
1013     }
1014   }
1015   return !nodes.empty();
1016 }