Salome HOME
Copyright update: 2016
[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     //cout << " SetNodeInfo(aNodeInfo)" << endl;
533     MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbNodes);
534     myMed->SetNodeInfo(aNodeInfo);
535     aNodeInfo.reset(); // free memory used for arrays
536
537
538     // Storing SMDS elements to the MED file for the MED mesh
539     //-------------------------------------------------------
540     // Write one element type at once in order to minimize memory usage (PAL19276)
541
542     const SMDS_MeshInfo& nbElemInfo = myMesh->GetMeshInfo();
543
544     // poly elements are not supported by med-2.1
545     bool polyTypesSupported = ( myMed->CrPolygoneInfo(aMeshInfo,eMAILLE,ePOLYGONE,0,0).get() != 0 );
546     TInt nbPolygonNodes = 0, nbPolyhedronNodes = 0, nbPolyhedronFaces = 0;
547
548     // nodes on VERTEXes where 0D elements are absent
549     std::vector<const SMDS_MeshElement*> nodesOf0D;
550     std::vector< SMDS_ElemIteratorPtr > iterVec;
551     SMDS_ElemIteratorPtr iterVecIter;
552     if ( myAddODOnVertices && getNodesOfMissing0DOnVert( myMesh, nodesOf0D ))
553     {
554       iterVec.resize(2);
555       iterVec[0] = myMesh->elementsIterator( SMDSAbs_0DElement );
556       iterVec[1] = SMDS_ElemIteratorPtr
557         ( new SMDS_ElementVectorIterator( nodesOf0D.begin(), nodesOf0D.end() ));
558
559       typedef SMDS_IteratorOnIterators
560         < const SMDS_MeshElement *, std::vector< SMDS_ElemIteratorPtr > > TItIterator;
561       iterVecIter = SMDS_ElemIteratorPtr( new TItIterator( iterVec ));
562     }
563
564     // collect info on all geom types
565
566     list< TElemTypeData > aTElemTypeDatas;
567
568     EEntiteMaillage anEntity = eMAILLE;
569 #ifdef _ELEMENTS_BY_DIM_
570     anEntity = eNOEUD_ELEMENT;
571 #endif
572     aTElemTypeDatas.push_back(TElemTypeData(anEntity,
573                                             ePOINT1,
574                                             nbElemInfo.Nb0DElements() + nodesOf0D.size(),
575                                             SMDSAbs_0DElement));
576 #ifdef _ELEMENTS_BY_DIM_
577     anEntity = eSTRUCT_ELEMENT;
578 #endif
579     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
580                                              eBALL,
581                                              nbElemInfo.NbBalls(),
582                                              SMDSAbs_Ball));
583 #ifdef _ELEMENTS_BY_DIM_
584     anEntity = eARETE;
585 #endif
586     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
587                                              eSEG2,
588                                              nbElemInfo.NbEdges( ORDER_LINEAR ),
589                                              SMDSAbs_Edge));
590     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
591                                              eSEG3,
592                                              nbElemInfo.NbEdges( ORDER_QUADRATIC ),
593                                              SMDSAbs_Edge));
594 #ifdef _ELEMENTS_BY_DIM_
595     anEntity = eFACE;
596 #endif
597     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
598                                              eTRIA3,
599                                              nbElemInfo.NbTriangles( ORDER_LINEAR ),
600                                              SMDSAbs_Face));
601     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
602                                              eTRIA6,
603                                              nbElemInfo.NbTriangles( ORDER_QUADRATIC ) -
604                                              nbElemInfo.NbBiQuadTriangles(),
605                                              SMDSAbs_Face));
606     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
607                                              eTRIA7,
608                                              nbElemInfo.NbBiQuadTriangles(),
609                                              SMDSAbs_Face));
610     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
611                                              eQUAD4,
612                                              nbElemInfo.NbQuadrangles( ORDER_LINEAR ),
613                                              SMDSAbs_Face));
614     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
615                                              eQUAD8,
616                                              nbElemInfo.NbQuadrangles( ORDER_QUADRATIC ) -
617                                              nbElemInfo.NbBiQuadQuadrangles(),
618                                              SMDSAbs_Face));
619     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
620                                              eQUAD9,
621                                              nbElemInfo.NbBiQuadQuadrangles(),
622                                              SMDSAbs_Face));
623     if ( polyTypesSupported ) {
624       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
625                                                ePOLYGONE,
626                                                nbElemInfo.NbPolygons( ORDER_LINEAR ),
627                                                SMDSAbs_Face));
628       // we need one more loop on poly elements to count nb of their nodes
629       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
630                                                ePOLYGONE,
631                                                nbElemInfo.NbPolygons( ORDER_LINEAR ),
632                                                SMDSAbs_Face));
633       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
634                                                ePOLYGON2,
635                                                nbElemInfo.NbPolygons( ORDER_QUADRATIC ),
636                                                SMDSAbs_Face));
637       // we need one more loop on QUAD poly elements to count nb of their nodes
638       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
639                                                ePOLYGON2,
640                                                nbElemInfo.NbPolygons( ORDER_QUADRATIC ),
641                                                SMDSAbs_Face));
642     }
643 #ifdef _ELEMENTS_BY_DIM_
644     anEntity = eMAILLE;
645 #endif
646     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
647                                              eTETRA4,
648                                              nbElemInfo.NbTetras( ORDER_LINEAR ),
649                                              SMDSAbs_Volume));
650     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
651                                              eTETRA10,
652                                              nbElemInfo.NbTetras( ORDER_QUADRATIC ),
653                                              SMDSAbs_Volume));
654     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
655                                              ePYRA5,
656                                              nbElemInfo.NbPyramids( ORDER_LINEAR ),
657                                              SMDSAbs_Volume));
658     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
659                                              ePYRA13,
660                                              nbElemInfo.NbPyramids( ORDER_QUADRATIC ),
661                                              SMDSAbs_Volume));
662     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
663                                              ePENTA6,
664                                              nbElemInfo.NbPrisms( ORDER_LINEAR ),
665                                              SMDSAbs_Volume));
666     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
667                                              ePENTA15,
668                                              nbElemInfo.NbPrisms( ORDER_QUADRATIC ),
669                                              SMDSAbs_Volume));
670     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
671                                              eHEXA8,
672                                              nbElemInfo.NbHexas( ORDER_LINEAR ),
673                                              SMDSAbs_Volume));
674     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
675                                              eHEXA20,
676                                              nbElemInfo.NbHexas( ORDER_QUADRATIC )-
677                                              nbElemInfo.NbTriQuadHexas(),
678                                              SMDSAbs_Volume));
679     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
680                                              eHEXA27,
681                                              nbElemInfo.NbTriQuadHexas(),
682                                              SMDSAbs_Volume));
683     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
684                                              eOCTA12,
685                                              nbElemInfo.NbHexPrisms(),
686                                              SMDSAbs_Volume));
687     if ( polyTypesSupported ) {
688       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
689                                                ePOLYEDRE,
690                                                nbElemInfo.NbPolyhedrons(),
691                                                SMDSAbs_Volume));
692       // we need one more loop on poly elements to count nb of their nodes
693       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
694                                                ePOLYEDRE,
695                                                nbElemInfo.NbPolyhedrons(),
696                                                SMDSAbs_Volume));
697     }
698
699     vector< bool > isElemFamMapBuilt( SMDSAbs_NbElementTypes, false );
700
701     // loop on all geom types of elements
702
703     list< TElemTypeData >::iterator aElemTypeData = aTElemTypeDatas.begin();
704     for ( ; aElemTypeData != aTElemTypeDatas.end(); ++aElemTypeData )
705     {
706       if ( aElemTypeData->_nbElems == 0 )
707         continue;
708
709       int defaultFamilyId = 0;
710       switch ( aElemTypeData->_smdsType ) {
711       case SMDSAbs_0DElement: defaultFamilyId = my0DElementsDefaultFamilyId; break;
712       case SMDSAbs_Ball:      defaultFamilyId = myBallsDefaultFamilyId;      break;
713       case SMDSAbs_Edge:      defaultFamilyId = myEdgesDefaultFamilyId;      break;
714       case SMDSAbs_Face:      defaultFamilyId = myFacesDefaultFamilyId;      break;
715       case SMDSAbs_Volume:    defaultFamilyId = myVolumesDefaultFamilyId;    break;
716       default:
717         continue;
718       }
719
720       // iterator on elements of a current type
721       SMDS_ElemIteratorPtr elemIterator;
722       int iElem = 0;
723
724       // Treat POLYGONs
725       // ---------------
726       if ( aElemTypeData->_geomType == ePOLYGONE ||
727            aElemTypeData->_geomType == ePOLYGON2 )
728       {
729         if ( aElemTypeData->_geomType == ePOLYGONE )
730           elemIterator = myMesh->elementEntityIterator( SMDSEntity_Polygon );
731         else
732           elemIterator = myMesh->elementEntityIterator( SMDSEntity_Quad_Polygon );
733
734         if ( nbPolygonNodes == 0 ) {
735           // Count nb of nodes
736           while ( elemIterator->more() ) {
737             const SMDS_MeshElement* anElem = elemIterator->next();
738             nbPolygonNodes += anElem->NbNodes();
739             if ( ++iElem == aElemTypeData->_nbElems )
740               break;
741           }
742         }
743         else {
744           // Store in med file
745           PPolygoneInfo aPolygoneInfo = myMed->CrPolygoneInfo(aMeshInfo,
746                                                               aElemTypeData->_entity,
747                                                               aElemTypeData->_geomType,
748                                                               aElemTypeData->_nbElems,
749                                                               nbPolygonNodes,
750                                                               theConnMode, theIsElemNum,
751                                                               theIsElemNames);
752           TElemNum & index = *(aPolygoneInfo->myIndex.get());
753           index[0] = 1;
754
755           while ( elemIterator->more() )
756           {
757             const SMDS_MeshElement* anElem = elemIterator->next();
758             // index
759             TInt aNbNodes = anElem->NbNodes();
760             index[ iElem+1 ] = index[ iElem ] + aNbNodes;
761
762             // connectivity
763             TConnSlice aTConnSlice = aPolygoneInfo->GetConnSlice( iElem );
764             for(TInt iNode = 0; iNode < aNbNodes; iNode++) {
765               const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
766 #ifdef _EDF_NODE_IDS_
767               aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
768 #else
769               aTConnSlice[ iNode ] = aNode->GetID();
770 #endif
771             }
772             // element number
773             aPolygoneInfo->SetElemNum( iElem, anElem->GetID() );
774
775             // family number
776             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
777             aPolygoneInfo->SetFamNum( iElem, famNum );
778
779             if ( ++iElem == aPolygoneInfo->GetNbElem() )
780               break;
781           }
782           myMed->SetPolygoneInfo(aPolygoneInfo);
783
784           nbPolygonNodes = 0; // to treat next polygon type
785         }
786       }
787
788       // Treat POLYEDREs
789       // ----------------
790       else if (aElemTypeData->_geomType == ePOLYEDRE )
791       {
792         elemIterator = myMesh->elementGeomIterator( SMDSGeom_POLYHEDRA );
793         
794         if ( nbPolyhedronNodes == 0 ) {
795           // Count nb of nodes
796           while ( elemIterator->more() ) {
797             const SMDS_MeshElement*  anElem = elemIterator->next();
798             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
799             if ( !aPolyedre ) continue;
800             nbPolyhedronNodes += aPolyedre->NbNodes();
801             nbPolyhedronFaces += aPolyedre->NbFaces();
802             if ( ++iElem == aElemTypeData->_nbElems )
803               break;
804           }
805         }
806         else {
807           // Store in med file
808           PPolyedreInfo aPolyhInfo = myMed->CrPolyedreInfo(aMeshInfo,
809                                                            aElemTypeData->_entity,
810                                                            aElemTypeData->_geomType,
811                                                            aElemTypeData->_nbElems,
812                                                            nbPolyhedronFaces+1,
813                                                            nbPolyhedronNodes,
814                                                            theConnMode,
815                                                            theIsElemNum,
816                                                            theIsElemNames);
817           TElemNum & index = *(aPolyhInfo->myIndex.get());
818           TElemNum & faces = *(aPolyhInfo->myFaces.get());
819           TElemNum & conn  = *(aPolyhInfo->myConn.get());
820           index[0] = 1;
821           faces[0] = 1;
822
823           TInt iFace = 0, iNode = 0;
824           while ( elemIterator->more() )
825           {
826             const SMDS_MeshElement*  anElem = elemIterator->next();
827             const SMDS_VtkVolume *aPolyedre = dynamic_cast<const SMDS_VtkVolume*>(anElem);
828             if ( !aPolyedre ) continue;
829             // index
830             TInt aNbFaces = aPolyedre->NbFaces();
831             index[ iElem+1 ] = index[ iElem ] + aNbFaces;
832
833             // face index
834             for (TInt f = 1; f <= aNbFaces; ++f, ++iFace ) {
835               int aNbFaceNodes = aPolyedre->NbFaceNodes( f );
836               faces[ iFace+1 ] = faces[ iFace ] + aNbFaceNodes;
837             }
838             // connectivity
839             SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
840             while ( nodeIt->more() ) {
841               const SMDS_MeshElement* aNode = nodeIt->next();
842 #ifdef _EDF_NODE_IDS_
843               conn[ iNode ] = aNodeIdMap[aNode->GetID()];
844 #else
845               conn[ iNode ] = aNode->GetID();
846 #endif
847               ++iNode;
848             }
849             // element number
850             aPolyhInfo->SetElemNum( iElem, anElem->GetID() );
851
852             // family number
853             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
854             aPolyhInfo->SetFamNum( iElem, famNum );
855
856             if ( ++iElem == aPolyhInfo->GetNbElem() )
857               break;
858           }
859           myMed->SetPolyedreInfo(aPolyhInfo);
860         }
861       } // if (aElemTypeData->_geomType == ePOLYEDRE )
862
863       // Treat BALLs
864       // ----------------
865       else if (aElemTypeData->_geomType == eBALL )
866       {
867         // allocate data arrays
868         PBallInfo aBallInfo = myMed->CrBallInfo( aMeshInfo, aElemTypeData->_nbElems );
869
870         // build map of family numbers for this type
871         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
872         {
873           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
874           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
875         }
876
877         elemIterator = myMesh->elementsIterator( SMDSAbs_Ball );
878         while ( elemIterator->more() )
879         {
880           const SMDS_MeshElement* anElem = elemIterator->next();
881           // connectivity
882           const SMDS_MeshElement* aNode = anElem->GetNode( 0 );
883 #ifdef _EDF_NODE_IDS_
884           (*aBallInfo->myConn)[ iElem ] = aNodeIdMap[aNode->GetID()];
885 #else
886           (*aBallInfo->myConn)[ iElem ] = aNode->GetID();
887 #endif
888           // element number
889           aBallInfo->SetElemNum( iElem, anElem->GetID() );
890
891           // diameter
892           aBallInfo->myDiameters[ iElem ] =
893             static_cast<const SMDS_BallElement*>( anElem )->GetDiameter();
894
895           // family number
896           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
897           aBallInfo->SetFamNum( iElem, famNum );
898           ++iElem;
899         }
900         // store data in a file
901         myMed->SetBallInfo(aBallInfo);
902       }
903
904       else
905       {
906         // Treat standard types
907         // ---------------------
908
909         // allocate data arrays
910         PCellInfo aCellInfo = myMed->CrCellInfo( aMeshInfo,
911                                                  aElemTypeData->_entity,
912                                                  aElemTypeData->_geomType,
913                                                  aElemTypeData->_nbElems,
914                                                  theConnMode,
915                                                  theIsElemNum,
916                                                  theIsElemNames);
917         // build map of family numbers for this type
918         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
919         {
920           //cout << " fillElemFamilyMap()" << endl;
921           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
922           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
923         }
924
925         TInt aNbNodes = MED::GetNbNodes(aElemTypeData->_geomType);
926         elemIterator = myMesh->elementsIterator( aElemTypeData->_smdsType );
927         if ( aElemTypeData->_smdsType == SMDSAbs_0DElement && ! nodesOf0D.empty() )
928           elemIterator = iterVecIter;
929         while ( elemIterator->more() )
930         {
931           const SMDS_MeshElement* anElem = elemIterator->next();
932           if ( anElem->NbNodes() != aNbNodes || anElem->IsPoly() )
933             continue; // other geometry
934
935           // connectivity
936           TConnSlice aTConnSlice = aCellInfo->GetConnSlice( iElem );
937           for (TInt iNode = 0; iNode < aNbNodes; iNode++) {
938             const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
939 #ifdef _EDF_NODE_IDS_
940             aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
941 #else
942             aTConnSlice[ iNode ] = aNode->GetID();
943 #endif
944           }
945           // element number
946           aCellInfo->SetElemNum( iElem, anElem->GetID() );
947
948           // family number
949           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
950           aCellInfo->SetFamNum( iElem, famNum );
951
952           if ( ++iElem == aCellInfo->GetNbElem() )
953             break;
954         }
955         // store data in a file
956         myMed->SetCellInfo(aCellInfo);
957       }
958
959     } // loop on geom types
960
961
962   }
963   catch(const std::exception& exc) {
964     INFOS("The following exception was caught:\n\t"<<exc.what());
965     throw;
966   }
967   catch(...) {
968     INFOS("Unknown exception was caught !!!");
969     throw;
970   }
971
972   myMeshId = -1;
973   myGroups.clear();
974   mySubMeshes.clear();
975   return aResult;
976 }
977
978 //================================================================================
979 /*!
980  * \brief Returns nodes on VERTEXes where 0D elements are absent
981  */
982 //================================================================================
983
984 bool DriverMED_W_SMESHDS_Mesh::
985 getNodesOfMissing0DOnVert(SMESHDS_Mesh*                         meshDS,
986                           std::vector<const SMDS_MeshElement*>& nodes)
987 {
988   nodes.clear();
989   for ( int i = 1; i <= meshDS->MaxShapeIndex(); ++i )
990   {
991     if ( meshDS->IndexToShape( i ).ShapeType() != TopAbs_VERTEX )
992       continue;
993     if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(i) ) {
994       SMDS_NodeIteratorPtr nIt= sm->GetNodes();
995       while (nIt->more())
996       {
997         const SMDS_MeshNode* n = nIt->next();
998         if ( n->NbInverseElements( SMDSAbs_0DElement ) == 0 )
999           nodes.push_back( n );
1000       }
1001     }
1002   }
1003   return !nodes.empty();
1004 }