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