1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH DriverMED : driver to read and write 'med' files
24 // File : DriverMED_W_SMESHDS_Mesh.cxx
28 #include "DriverMED_W_SMESHDS_Mesh.h"
30 #include "DriverMED_Family.h"
31 #include "MED_Factory.hxx"
32 #include "MED_Utilities.hxx"
33 #include "SMDS_IteratorOnIterators.hxx"
34 #include "SMDS_MeshNode.hxx"
35 #include "SMDS_SetIterator.hxx"
36 #include "SMESHDS_Mesh.hxx"
37 #include "MED_Common.hxx"
38 #include "MED_TFile.hxx"
42 #include <BRep_Tool.hxx>
43 #include <TopExp_Explorer.hxx>
46 #include <utilities.h>
49 #define _EDF_NODE_IDS_
50 //#define _ELEMENTS_BY_DIM_
56 //================================================================================
60 //================================================================================
62 DriverMED_W_SMESHDS_Mesh::DriverMED_W_SMESHDS_Mesh():
63 myAllSubMeshes (false),
64 myDoGroupOfNodes (false),
65 myDoGroupOfEdges (false),
66 myDoGroupOfFaces (false),
67 myDoGroupOfVolumes (false),
68 myDoGroupOf0DElems(false),
69 myDoGroupOfBalls(false),
70 myAutoDimension(false),
71 myAddODOnVertices(false),
72 myDoAllInGroups(false),
78 //================================================================================
80 * \brief Set a file name and a version
81 * \param [in] theFileName - output file name
82 * \param [in] theVersion - desired MED file version == major * 10 + minor
84 //================================================================================
86 void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName, int theVersion)
88 myVersion = theVersion;
89 Driver_SMESHDS_Mesh::SetFile(theFileName);
92 //================================================================================
94 * MED version is either the latest available, or with an inferior minor,
95 * to ensure backward compatibility on writing med files.
97 //================================================================================
99 string DriverMED_W_SMESHDS_Mesh::GetVersionString(int theMinor, int theNbDigits)
101 TInt majeur, mineur, release;
102 majeur=MED_MAJOR_NUM;
103 mineur=MED_MINOR_NUM;
104 release=MED_RELEASE_NUM;
105 TInt imposedMineur = mineur;
108 imposedMineur = mineur;
109 else if (theMinor > MED_MINOR_NUM)
110 imposedMineur = mineur;
112 imposedMineur = theMinor;
115 if ( theNbDigits > 0 )
117 if ( theNbDigits > 1 )
118 name << "." << imposedMineur;
119 if ( theNbDigits > 2 )
120 name << "." << release;
124 void DriverMED_W_SMESHDS_Mesh::AddGroup(SMESHDS_GroupBase* theGroup)
126 myGroups.push_back(theGroup);
129 void DriverMED_W_SMESHDS_Mesh::AddAllSubMeshes()
131 myAllSubMeshes = true;
134 void DriverMED_W_SMESHDS_Mesh::AddSubMesh(SMESHDS_SubMesh* theSubMesh, int /*theID*/)
136 mySubMeshes.push_back( theSubMesh );
139 void DriverMED_W_SMESHDS_Mesh::AddGroupOfNodes()
141 myDoGroupOfNodes = true;
144 void DriverMED_W_SMESHDS_Mesh::AddGroupOfEdges()
146 myDoGroupOfEdges = true;
149 void DriverMED_W_SMESHDS_Mesh::AddGroupOfFaces()
151 myDoGroupOfFaces = true;
154 void DriverMED_W_SMESHDS_Mesh::AddGroupOfVolumes()
156 myDoGroupOfVolumes = true;
159 void DriverMED_W_SMESHDS_Mesh::AddGroupOf0DElems()
161 myDoGroupOf0DElems = true;
164 void DriverMED_W_SMESHDS_Mesh::AddGroupOfBalls()
166 myDoGroupOfBalls = true;
169 //================================================================================
171 * \brief Set up a flag to add all elements not belonging to any group to
172 * some auxiliary group. This is needed for SMESH -> SAUVE -> SMESH conversion,
173 * which since PAL0023285 reads only SAUVE elements belonging to any group,
174 * and hence can lose some elements. That auxiliary group is ignored while
175 * reading a MED file.
177 //================================================================================
179 void DriverMED_W_SMESHDS_Mesh::AddAllToGroup()
181 myDoAllInGroups = true;
187 //---------------------------------------------
189 * \brief Retrieving node coordinates utilities
191 //---------------------------------------------
193 typedef double (SMDS_MeshNode::* TGetCoord)() const;
194 typedef const char* TName;
195 typedef const char* TUnit;
197 // name length in a mesh must be equal to 16 :
204 TUnit aUnit[3] = {M,M,M};
207 TGetCoord aXYZGetCoord[3] = {
212 TName aXYZName[3] = {X,Y,Z};
215 TGetCoord aXYGetCoord[2] = {
219 TName aXYName[2] = {X,Y};
221 TGetCoord aYZGetCoord[2] = {
225 TName aYZName[2] = {Y,Z};
227 TGetCoord aXZGetCoord[2] = {
231 TName aXZName[2] = {X,Z};
234 TGetCoord aXGetCoord[1] = {
237 TName aXName[1] = {X};
239 TGetCoord aYGetCoord[1] = {
242 TName aYName[1] = {Y};
244 TGetCoord aZGetCoord[1] = {
247 TName aZName[1] = {Z};
251 SMDS_NodeIteratorPtr myNodeIter;
252 const SMDS_MeshNode* myCurrentNode;
253 TGetCoord* myGetCoord;
257 TCoordHelper(const SMDS_NodeIteratorPtr& theNodeIter,
258 TGetCoord* theGetCoord,
260 TUnit* theUnit = aUnit):
261 myNodeIter(theNodeIter),
262 myGetCoord(theGetCoord),
266 virtual ~TCoordHelper(){}
268 return myNodeIter->more() &&
269 (myCurrentNode = myNodeIter->next());
271 const SMDS_MeshNode* GetNode(){
272 return myCurrentNode;
274 MED::TIDVector::value_type GetID(){
275 return myCurrentNode->GetID();
277 MED::TFloatVector::value_type GetCoord(TInt theCoodId){
278 return (myCurrentNode->*myGetCoord[theCoodId])();
280 MED::TStringVector::value_type GetName(TInt theDimId){
281 return myName[theDimId];
283 MED::TStringVector::value_type GetUnit(TInt theDimId){
284 return myUnit[theDimId];
287 typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
289 //-------------------------------------------------------
291 * \brief Structure describing element type
293 //-------------------------------------------------------
296 EEntiteMaillage _entity;
297 EGeometrieElement _geomType;
299 SMDSAbs_ElementType _smdsType;
301 TElemTypeData (EEntiteMaillage entity, EGeometrieElement geom, TInt nb, SMDSAbs_ElementType type)
302 : _entity(entity), _geomType(geom), _nbElems( nb ), _smdsType( type ) {}
306 typedef NCollection_DataMap< Standard_Address, int > TElemFamilyMap;
308 //================================================================================
310 * \brief Fills element to famaly ID map for element type.
311 * Removes all families of anElemType
313 //================================================================================
315 void fillElemFamilyMap( TElemFamilyMap & anElemFamMap,
316 list<DriverMED_FamilyPtr> & aFamilies,
317 const SMDSAbs_ElementType anElemType)
319 anElemFamMap.Clear();
320 list<DriverMED_FamilyPtr>::iterator aFamsIter = aFamilies.begin();
321 while ( aFamsIter != aFamilies.end() )
323 if ((*aFamsIter)->GetType() != anElemType) {
327 int aFamId = (*aFamsIter)->GetId();
328 const ElementsSet& anElems = (*aFamsIter)->GetElements();
329 ElementsSet::const_iterator anElemsIter = anElems.begin();
330 for (; anElemsIter != anElems.end(); anElemsIter++)
332 anElemFamMap.Bind( (Standard_Address)*anElemsIter, aFamId );
334 // remove a family from the list
335 aFamilies.erase( aFamsIter++ );
340 //================================================================================
342 * \brief For an element, return family ID found in the map or a default one
344 //================================================================================
346 int getFamilyId( const TElemFamilyMap & anElemFamMap,
347 const SMDS_MeshElement* anElement,
348 const int aDefaultFamilyId)
350 if ( anElemFamMap.IsBound( (Standard_Address) anElement ))
351 return anElemFamMap( (Standard_Address) anElement );
353 return aDefaultFamilyId;
356 //================================================================================
358 * \brief Returns iterator on sub-meshes
360 //================================================================================
362 SMESHDS_SubMeshIteratorPtr getIterator( std::vector<SMESHDS_SubMesh*>& mySubMeshes )
364 return SMESHDS_SubMeshIteratorPtr
365 ( new SMDS_SetIterator
366 < const SMESHDS_SubMesh*, std::vector< SMESHDS_SubMesh* >::iterator >( mySubMeshes.begin(),
367 mySubMeshes.end() ));
371 //================================================================================
373 * \brief Write my mesh to a file
375 //================================================================================
377 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::Perform()
379 MED::PWrapper myMed = CrWrapperW(myFile, myVersion);
380 return this->PerformInternal<MED::PWrapper>(myMed);
383 //================================================================================
385 * \brief Write my mesh to a MEDCoupling DS
387 //================================================================================
389 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh_Mem::Perform()
393 Driver_Mesh::Status status = Driver_Mesh::DRS_OK;
394 bool isClosed(false);
395 {// let braces to flush (call of MED::PWrapper myMed destructor)
396 TMemFile *tfileInst = new TMemFile(&ptr,&sz,&isClosed);// this new will be destroyed by destructor of myMed
397 MED::PWrapper myMed = CrWrapperW(myFile, -1, tfileInst);
398 status = this->PerformInternal<MED::PWrapper>(myMed);
401 EXCEPTION(std::runtime_error, "TFTMemFile destructor : on destruction file has not been closed properly -> chunk of memory data may be invalid !");
402 _data = MEDCoupling::DataArrayByte::New();
403 _data->useArray(reinterpret_cast<char *>(ptr),true,MEDCoupling::DeallocType::C_DEALLOC,sz,1);
405 THROW_SALOME_EXCEPTION("DriverMED_W_SMESHDS_Mesh_Mem::Perform - MED memory file id is supposed to be closed !");
409 //================================================================================
411 * \brief Write my mesh
413 //================================================================================
415 template<class LowLevelWriter>
416 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::PerformInternal(LowLevelWriter myMed)
418 Status aResult = DRS_OK;
420 //MESSAGE("Perform - myFile : "<<myFile);
422 if ( Driver_Mesh::IsMeshTooLarge< TInt >( myMesh, /*checkIDs =*/ true ))
423 return DRS_TOO_LARGE_MESH;
425 // Creating the MED mesh for corresponding SMDS structure
426 //-------------------------------------------------------
428 if (myMeshId != -1) {
429 ostringstream aMeshNameStr;
430 aMeshNameStr<<myMeshId;
431 aMeshName = aMeshNameStr.str();
433 aMeshName = myMeshName;
436 // Mesh dimension definition
438 TInt aMeshDimension = 0;
439 if ( myMesh->NbEdges() > 0 )
441 if ( myMesh->NbFaces() > 0 )
443 if ( myMesh->NbVolumes() > 0 )
446 TInt aSpaceDimension = 3;
447 TCoordHelperPtr aCoordHelperPtr;
449 bool anIsXDimension = false;
450 bool anIsYDimension = false;
451 bool anIsZDimension = false;
452 if ( myAutoDimension && aMeshDimension < 3 )
454 SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
455 double aBounds[6] = {0.,0.,0.,0.,0.,0.};
456 if(aNodesIter->more()){
457 const SMDS_MeshNode* aNode = aNodesIter->next();
458 aBounds[0] = aBounds[1] = aNode->X();
459 aBounds[2] = aBounds[3] = aNode->Y();
460 aBounds[4] = aBounds[5] = aNode->Z();
462 while(aNodesIter->more()){
463 const SMDS_MeshNode* aNode = aNodesIter->next();
464 aBounds[0] = min(aBounds[0],aNode->X());
465 aBounds[1] = max(aBounds[1],aNode->X());
467 aBounds[2] = min(aBounds[2],aNode->Y());
468 aBounds[3] = max(aBounds[3],aNode->Y());
470 aBounds[4] = min(aBounds[4],aNode->Z());
471 aBounds[5] = max(aBounds[5],aNode->Z());
475 TopoDS_Shape mainShape = myMesh->ShapeToMesh();
476 bool hasShapeToMesh = ( myMesh->SubMeshIndices().size() > 1 );
477 if ( !mainShape.IsNull() && hasShapeToMesh )
479 // define EPS by max tolerance of the mainShape (IPAL53097)
480 TopExp_Explorer subShape;
481 for ( subShape.Init( mainShape, TopAbs_FACE ); subShape.More(); subShape.Next() ) {
482 EPS = Max( EPS, BRep_Tool::Tolerance( TopoDS::Face( subShape.Current() )));
484 for ( subShape.Init( mainShape, TopAbs_EDGE ); subShape.More(); subShape.Next() ) {
485 EPS = Max( EPS, BRep_Tool::Tolerance( TopoDS::Edge( subShape.Current() )));
487 for ( subShape.Init( mainShape, TopAbs_VERTEX ); subShape.More(); subShape.Next() ) {
488 EPS = Max( EPS, BRep_Tool::Tolerance( TopoDS::Vertex( subShape.Current() )));
492 anIsXDimension = (aBounds[1] - aBounds[0]) + abs(aBounds[1]) + abs(aBounds[0]) > EPS;
493 anIsYDimension = (aBounds[3] - aBounds[2]) + abs(aBounds[3]) + abs(aBounds[2]) > EPS;
494 anIsZDimension = (aBounds[5] - aBounds[4]) + abs(aBounds[5]) + abs(aBounds[4]) > EPS;
495 if ( myZTolerance > 0 && anIsZDimension )
496 anIsZDimension = (aBounds[5] > myZTolerance || aBounds[4] < -myZTolerance );
497 aSpaceDimension = Max( aMeshDimension, anIsXDimension + anIsYDimension + anIsZDimension );
498 if ( !aSpaceDimension )
500 // PAL16857(SMESH not conform to the MED convention):
501 if ( aSpaceDimension == 2 && anIsZDimension ) // 2D only if mesh is in XOY plane
503 // PAL18941(a saved study with a mesh belong Z is opened and the mesh is belong X)
504 if ( aSpaceDimension == 1 && !anIsXDimension ) {// 1D only if mesh is along OX
505 if ( anIsYDimension ) {
507 anIsXDimension = true;
514 SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
515 switch ( aSpaceDimension ) {
517 aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYZGetCoord,aXYZName));
520 if(anIsXDimension && anIsYDimension)
521 aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYGetCoord,aXYName));
522 if(anIsYDimension && anIsZDimension)
523 aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYZGetCoord,aYZName));
524 if(anIsXDimension && anIsZDimension)
525 aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXZGetCoord,aXZName));
529 aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXGetCoord,aXName));
531 aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYGetCoord,aYName));
533 aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aZGetCoord,aZName));
538 PMeshInfo aMeshInfo = myMed->CrMeshInfo(aMeshDimension,aSpaceDimension,aMeshName);
539 //MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
540 myMed->SetMeshInfo(aMeshInfo);
542 // Storing SMDS groups and sub-meshes as med families
543 //----------------------------------------------------
544 int myNodesDefaultFamilyId = 0;
545 int my0DElementsDefaultFamilyId = 0;
546 int myBallsDefaultFamilyId = 0;
547 int myEdgesDefaultFamilyId = 0;
548 int myFacesDefaultFamilyId = 0;
549 int myVolumesDefaultFamilyId = 0;
550 smIdType nbNodes = myMesh->NbNodes();
551 smIdType nb0DElements = myMesh->Nb0DElements();
552 smIdType nbBalls = myMesh->NbBalls();
553 smIdType nbEdges = myMesh->NbEdges();
554 smIdType nbFaces = myMesh->NbFaces();
555 smIdType nbVolumes = myMesh->NbVolumes();
556 if (myDoGroupOfNodes) myNodesDefaultFamilyId = REST_NODES_FAMILY;
557 if (myDoGroupOfEdges) myEdgesDefaultFamilyId = REST_EDGES_FAMILY;
558 if (myDoGroupOfFaces) myFacesDefaultFamilyId = REST_FACES_FAMILY;
559 if (myDoGroupOfVolumes) myVolumesDefaultFamilyId = REST_VOLUMES_FAMILY;
560 if (myDoGroupOf0DElems) my0DElementsDefaultFamilyId = REST_0DELEM_FAMILY;
561 if (myDoGroupOfBalls) myBallsDefaultFamilyId = REST_BALL_FAMILY;
562 if (myDoAllInGroups )
564 if (!myDoGroupOfEdges) myEdgesDefaultFamilyId = NIG_EDGES_FAMILY ;
565 if (!myDoGroupOfFaces) myFacesDefaultFamilyId = NIG_FACES_FAMILY ;
566 if (!myDoGroupOfVolumes) myVolumesDefaultFamilyId = NIG_VOLS_FAMILY ;
567 if (!myDoGroupOf0DElems) my0DElementsDefaultFamilyId = NIG_0DELEM_FAMILY ;
568 if (!myDoGroupOfBalls) myBallsDefaultFamilyId = NIG_BALL_FAMILY ;
571 //MESSAGE("Perform - aFamilyInfo");
572 list<DriverMED_FamilyPtr> aFamilies;
573 if (myAllSubMeshes) {
574 aFamilies = DriverMED_Family::MakeFamilies
575 (myMesh->SubMeshes(), myGroups,
576 myDoGroupOfNodes && nbNodes,
577 myDoGroupOfEdges && nbEdges,
578 myDoGroupOfFaces && nbFaces,
579 myDoGroupOfVolumes && nbVolumes,
580 myDoGroupOf0DElems && nb0DElements,
581 myDoGroupOfBalls && nbBalls,
585 aFamilies = DriverMED_Family::MakeFamilies
586 (getIterator( mySubMeshes ), myGroups,
587 myDoGroupOfNodes && nbNodes,
588 myDoGroupOfEdges && nbEdges,
589 myDoGroupOfFaces && nbFaces,
590 myDoGroupOfVolumes && nbVolumes,
591 myDoGroupOf0DElems && nb0DElements,
592 myDoGroupOfBalls && nbBalls,
595 list<DriverMED_FamilyPtr>::iterator aFamsIter;
596 for (aFamsIter = aFamilies.begin(); aFamsIter != aFamilies.end(); aFamsIter++)
598 PFamilyInfo aFamilyInfo = (*aFamsIter)->GetFamilyInfo<LowLevelWriter>(myMed,aMeshInfo);
599 myMed->SetFamilyInfo(aFamilyInfo);
602 // Storing SMDS nodes to the MED file for the MED mesh
603 //----------------------------------------------------
604 #ifdef _EDF_NODE_IDS_
605 typedef map<TInt,TInt> TNodeIdMap;
606 TNodeIdMap aNodeIdMap;
608 const EModeSwitch theMode = eFULL_INTERLACE;
609 const ERepere theSystem = eCART;
610 const EBooleen theIsElemNum = mySaveNumbers ? eVRAI : eFAUX;
611 const EBooleen theIsElemNames = eFAUX;
612 const EConnectivite theConnMode = eNOD;
614 TInt aNbNodes = FromSmIdType<TInt>( myMesh->NbNodes() );
615 PNodeInfo aNodeInfo = myMed->CrNodeInfo(aMeshInfo, aNbNodes,
616 theMode, theSystem, theIsElemNum, theIsElemNames);
618 // find family numbers for nodes
619 TElemFamilyMap anElemFamMap;
620 fillElemFamilyMap( anElemFamMap, aFamilies, SMDSAbs_Node );
622 for (TInt iNode = 0; aCoordHelperPtr->Next(); iNode++)
625 TCoordSlice aTCoordSlice = aNodeInfo->GetCoordSlice( iNode );
626 for(TInt iCoord = 0; iCoord < aSpaceDimension; iCoord++){
627 aTCoordSlice[iCoord] = aCoordHelperPtr->GetCoord(iCoord);
629 if ( aSpaceDimension == 3 &&
630 -myZTolerance < aTCoordSlice[2] && aTCoordSlice[2] < myZTolerance )
631 aTCoordSlice[2] = 0.;
634 TInt aNodeID = FromSmIdType<TInt>( aCoordHelperPtr->GetID() );
635 aNodeInfo->SetElemNum( iNode, aNodeID );
636 #ifdef _EDF_NODE_IDS_
637 aNodeIdMap.insert( aNodeIdMap.end(), make_pair( aNodeID, iNode+1 ));
640 const SMDS_MeshNode* aNode = aCoordHelperPtr->GetNode();
641 int famNum = getFamilyId( anElemFamMap, aNode, myNodesDefaultFamilyId );
642 aNodeInfo->SetFamNum( iNode, famNum );
644 anElemFamMap.Clear();
646 // coordinate names and units
647 for (TInt iCoord = 0; iCoord < aSpaceDimension; iCoord++) {
648 aNodeInfo->SetCoordName( iCoord, aCoordHelperPtr->GetName(iCoord));
649 aNodeInfo->SetCoordUnit( iCoord, aCoordHelperPtr->GetUnit(iCoord));
652 //MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbNodes);
653 myMed->SetNodeInfo(aNodeInfo);
654 aNodeInfo.reset(); // free memory used for arrays
657 // Storing SMDS elements to the MED file for the MED mesh
658 //-------------------------------------------------------
659 // Write one element type at once in order to minimize memory usage (PAL19276)
661 const SMDS_MeshInfo& nbElemInfo = myMesh->GetMeshInfo();
663 // poly elements are not supported by med-2.1
664 bool polyTypesSupported = ( myMed->CrPolygoneInfo(aMeshInfo,eMAILLE,ePOLYGONE,0,0).get() != 0 );
665 TInt nbPolygonNodes = 0, nbPolyhedronNodes = 0, nbPolyhedronFaces = 0;
667 // nodes on VERTEXes where 0D elements are absent
668 std::vector<const SMDS_MeshElement*> nodesOf0D;
669 std::vector< SMDS_ElemIteratorPtr > iterVec;
670 SMDS_ElemIteratorPtr iterVecIter;
671 if ( myAddODOnVertices && getNodesOfMissing0DOnVert( myMesh, nodesOf0D ))
674 iterVec[0] = myMesh->elementsIterator( SMDSAbs_0DElement );
675 iterVec[1] = SMDS_ElemIteratorPtr
676 ( new SMDS_ElementVectorIterator( nodesOf0D.begin(), nodesOf0D.end() ));
678 typedef SMDS_IteratorOnIterators
679 < const SMDS_MeshElement *, std::vector< SMDS_ElemIteratorPtr > > TItIterator;
680 iterVecIter = SMDS_ElemIteratorPtr( new TItIterator( iterVec ));
683 // collect info on all geom types
685 list< TElemTypeData > aTElemTypeDatas;
687 EEntiteMaillage anEntity = eMAILLE;
688 #ifdef _ELEMENTS_BY_DIM_
689 anEntity = eNOEUD_ELEMENT;
691 aTElemTypeDatas.push_back(TElemTypeData(anEntity,
693 nbElemInfo.Nb0DElements() + nodesOf0D.size(),
695 #ifdef _ELEMENTS_BY_DIM_
696 anEntity = eSTRUCT_ELEMENT;
698 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
700 FromSmIdType<TInt>(nbElemInfo.NbBalls()),
702 #ifdef _ELEMENTS_BY_DIM_
705 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
707 FromSmIdType<TInt>(nbElemInfo.NbEdges( ORDER_LINEAR )),
709 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
711 FromSmIdType<TInt>(nbElemInfo.NbEdges( ORDER_QUADRATIC )),
713 #ifdef _ELEMENTS_BY_DIM_
716 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
718 FromSmIdType<TInt>(nbElemInfo.NbTriangles( ORDER_LINEAR )),
720 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
722 FromSmIdType<TInt>(nbElemInfo.NbTriangles( ORDER_QUADRATIC ) -
723 nbElemInfo.NbBiQuadTriangles()),
725 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
727 FromSmIdType<TInt>(nbElemInfo.NbBiQuadTriangles()),
729 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
731 FromSmIdType<TInt>(nbElemInfo.NbQuadrangles( ORDER_LINEAR )),
733 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
735 FromSmIdType<TInt>(nbElemInfo.NbQuadrangles( ORDER_QUADRATIC ) -
736 nbElemInfo.NbBiQuadQuadrangles()),
738 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
740 FromSmIdType<TInt>(nbElemInfo.NbBiQuadQuadrangles()),
742 if ( polyTypesSupported ) {
743 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
745 FromSmIdType<TInt>(nbElemInfo.NbPolygons( ORDER_LINEAR )),
747 // we need one more loop on poly elements to count nb of their nodes
748 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
750 FromSmIdType<TInt>(nbElemInfo.NbPolygons( ORDER_LINEAR )),
752 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
754 FromSmIdType<TInt>(nbElemInfo.NbPolygons( ORDER_QUADRATIC )),
756 // we need one more loop on QUAD poly elements to count nb of their nodes
757 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
759 FromSmIdType<TInt>(nbElemInfo.NbPolygons( ORDER_QUADRATIC )),
762 #ifdef _ELEMENTS_BY_DIM_
765 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
767 FromSmIdType<TInt>(nbElemInfo.NbTetras( ORDER_LINEAR )),
769 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
771 FromSmIdType<TInt>(nbElemInfo.NbTetras( ORDER_QUADRATIC )),
773 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
775 FromSmIdType<TInt>(nbElemInfo.NbPyramids( ORDER_LINEAR )),
777 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
779 FromSmIdType<TInt>(nbElemInfo.NbPyramids( ORDER_QUADRATIC )),
781 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
783 FromSmIdType<TInt>(nbElemInfo.NbPrisms( ORDER_LINEAR )),
785 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
787 FromSmIdType<TInt>(nbElemInfo.NbQuadPrisms()),
789 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
791 FromSmIdType<TInt>(nbElemInfo.NbBiQuadPrisms()),
793 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
795 FromSmIdType<TInt>(nbElemInfo.NbHexas( ORDER_LINEAR )),
797 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
799 FromSmIdType<TInt>(nbElemInfo.NbHexas( ORDER_QUADRATIC )-
800 nbElemInfo.NbTriQuadHexas()),
802 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
804 FromSmIdType<TInt>(nbElemInfo.NbTriQuadHexas()),
806 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
808 FromSmIdType<TInt>(nbElemInfo.NbHexPrisms()),
810 if ( polyTypesSupported ) {
811 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
813 FromSmIdType<TInt>(nbElemInfo.NbPolyhedrons()),
815 // we need one more loop on poly elements to count nb of their nodes
816 aTElemTypeDatas.push_back( TElemTypeData(anEntity,
818 FromSmIdType<TInt>(nbElemInfo.NbPolyhedrons()),
822 vector< bool > isElemFamMapBuilt( SMDSAbs_NbElementTypes, false );
824 // loop on all geom types of elements
826 list< TElemTypeData >::iterator aElemTypeData = aTElemTypeDatas.begin();
827 for ( ; aElemTypeData != aTElemTypeDatas.end(); ++aElemTypeData )
829 if ( aElemTypeData->_nbElems == 0 )
832 int defaultFamilyId = 0;
833 switch ( aElemTypeData->_smdsType ) {
834 case SMDSAbs_0DElement: defaultFamilyId = my0DElementsDefaultFamilyId; break;
835 case SMDSAbs_Ball: defaultFamilyId = myBallsDefaultFamilyId; break;
836 case SMDSAbs_Edge: defaultFamilyId = myEdgesDefaultFamilyId; break;
837 case SMDSAbs_Face: defaultFamilyId = myFacesDefaultFamilyId; break;
838 case SMDSAbs_Volume: defaultFamilyId = myVolumesDefaultFamilyId; break;
843 // build map of family numbers for this type
844 if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
846 fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
847 isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
850 // iterator on elements of a current type
851 SMDS_ElemIteratorPtr elemIterator;
856 if ( aElemTypeData->_geomType == ePOLYGONE ||
857 aElemTypeData->_geomType == ePOLYGON2 )
859 if ( aElemTypeData->_geomType == ePOLYGONE )
860 elemIterator = myMesh->elementEntityIterator( SMDSEntity_Polygon );
862 elemIterator = myMesh->elementEntityIterator( SMDSEntity_Quad_Polygon );
864 if ( nbPolygonNodes == 0 ) {
866 while ( elemIterator->more() ) {
867 const SMDS_MeshElement* anElem = elemIterator->next();
868 nbPolygonNodes += anElem->NbNodes();
869 if ( ++iElem == aElemTypeData->_nbElems )
875 PPolygoneInfo aPolygoneInfo = myMed->CrPolygoneInfo(aMeshInfo,
876 aElemTypeData->_entity,
877 aElemTypeData->_geomType,
878 aElemTypeData->_nbElems,
880 theConnMode, theIsElemNum,
882 TElemNum & index = *(aPolygoneInfo->myIndex.get());
885 while ( elemIterator->more() )
887 const SMDS_MeshElement* anElem = elemIterator->next();
889 TInt aNbNodes = anElem->NbNodes();
890 index[ iElem+1 ] = index[ iElem ] + aNbNodes;
893 TConnSlice aTConnSlice = aPolygoneInfo->GetConnSlice( iElem );
894 for(TInt iNode = 0; iNode < aNbNodes; iNode++) {
895 const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
896 #ifdef _EDF_NODE_IDS_
897 aTConnSlice[ iNode ] = aNodeIdMap[FromSmIdType<TInt>(aNode->GetID())];
899 aTConnSlice[ iNode ] = aNode->GetID();
903 aPolygoneInfo->SetElemNum( iElem, FromSmIdType<TInt>(anElem->GetID()) );
906 int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
907 aPolygoneInfo->SetFamNum( iElem, famNum );
909 if ( ++iElem == aPolygoneInfo->GetNbElem() )
912 myMed->SetPolygoneInfo(aPolygoneInfo);
914 nbPolygonNodes = 0; // to treat next polygon type
920 else if ( aElemTypeData->_geomType == ePOLYEDRE )
922 elemIterator = myMesh->elementGeomIterator( SMDSGeom_POLYHEDRA );
924 if ( nbPolyhedronNodes == 0 ) {
926 while ( elemIterator->more() ) {
927 const SMDS_MeshElement* anElem = elemIterator->next();
928 nbPolyhedronNodes += anElem->NbNodes();
929 nbPolyhedronFaces += anElem->NbFaces();
930 if ( ++iElem == aElemTypeData->_nbElems )
936 PPolyedreInfo aPolyhInfo = myMed->CrPolyedreInfo(aMeshInfo,
937 aElemTypeData->_entity,
938 aElemTypeData->_geomType,
939 aElemTypeData->_nbElems,
945 TElemNum & index = *(aPolyhInfo->myIndex.get());
946 TElemNum & faces = *(aPolyhInfo->myFaces.get());
947 TElemNum & conn = *(aPolyhInfo->myConn.get());
951 TInt iFace = 0, iNode = 0;
952 while ( elemIterator->more() )
954 const SMDS_MeshElement* anElem = elemIterator->next();
955 const SMDS_MeshVolume *aPolyedre = myMesh->DownCast< SMDS_MeshVolume >( anElem );
956 if ( !aPolyedre ) continue;
958 TInt aNbFaces = aPolyedre->NbFaces();
959 index[ iElem+1 ] = index[ iElem ] + aNbFaces;
962 for (TInt f = 1; f <= aNbFaces; ++f, ++iFace ) {
963 int aNbFaceNodes = aPolyedre->NbFaceNodes( f );
964 faces[ iFace+1 ] = faces[ iFace ] + aNbFaceNodes;
967 SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
968 while ( nodeIt->more() ) {
969 const SMDS_MeshElement* aNode = nodeIt->next();
970 #ifdef _EDF_NODE_IDS_
971 conn[ iNode ] = aNodeIdMap[FromSmIdType<TInt>(aNode->GetID())];
973 conn[ iNode ] = aNode->GetID();
978 aPolyhInfo->SetElemNum( iElem, FromSmIdType<TInt>(anElem->GetID()) );
981 int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
982 aPolyhInfo->SetFamNum( iElem, famNum );
984 if ( ++iElem == aPolyhInfo->GetNbElem() )
987 myMed->SetPolyedreInfo(aPolyhInfo);
989 } // if (aElemTypeData->_geomType == ePOLYEDRE )
993 else if (aElemTypeData->_geomType == eBALL )
995 // allocate data arrays
996 PBallInfo aBallInfo = myMed->CrBallInfo( aMeshInfo, aElemTypeData->_nbElems );
998 elemIterator = myMesh->elementsIterator( SMDSAbs_Ball );
999 while ( elemIterator->more() )
1001 const SMDS_MeshElement* anElem = elemIterator->next();
1003 const SMDS_MeshElement* aNode = anElem->GetNode( 0 );
1004 #ifdef _EDF_NODE_IDS_
1005 (*aBallInfo->myConn)[ iElem ] = aNodeIdMap[FromSmIdType<TInt>(aNode->GetID())];
1007 (*aBallInfo->myConn)[ iElem ] = aNode->GetID();
1010 aBallInfo->SetElemNum( iElem, FromSmIdType<TInt>(anElem->GetID()) );
1013 aBallInfo->myDiameters[ iElem ] =
1014 static_cast<const SMDS_BallElement*>( anElem )->GetDiameter();
1017 int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
1018 aBallInfo->SetFamNum( iElem, famNum );
1021 // store data in a file
1022 myMed->SetBallInfo(aBallInfo);
1027 // Treat standard types
1028 // ---------------------
1029 // allocate data arrays
1030 PCellInfo aCellInfo = myMed->CrCellInfo( aMeshInfo,
1031 aElemTypeData->_entity,
1032 aElemTypeData->_geomType,
1033 aElemTypeData->_nbElems,
1038 TInt aNbNodes = MED::GetNbNodes(aElemTypeData->_geomType);
1040 elemIterator = myMesh->elementsIterator( aElemTypeData->_smdsType );
1041 if ( aElemTypeData->_smdsType == SMDSAbs_0DElement && ! nodesOf0D.empty() )
1042 elemIterator = iterVecIter;
1044 while ( elemIterator->more() )
1046 const SMDS_MeshElement* anElem = elemIterator->next();
1047 if ( anElem->NbNodes() != aNbNodes || anElem->IsPoly() )
1048 continue; // other geometry
1051 TConnSlice aTConnSlice = aCellInfo->GetConnSlice( iElem );
1052 for (TInt iNode = 0; iNode < aNbNodes; iNode++) {
1053 const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
1054 #ifdef _EDF_NODE_IDS_
1055 aTConnSlice[ iNode ] = aNodeIdMap[FromSmIdType<TInt>(aNode->GetID())];
1057 aTConnSlice[ iNode ] = aNode->GetID();
1061 aCellInfo->SetElemNum( iElem, FromSmIdType<TInt>(anElem->GetID()) );
1064 int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
1065 aCellInfo->SetFamNum( iElem, famNum );
1067 if ( ++iElem == aCellInfo->GetNbElem() )
1070 // fix numbers of added SMDSAbs_0DElement
1071 if ( aElemTypeData->_smdsType == SMDSAbs_0DElement && ! nodesOf0D.empty() )
1073 iElem = myMesh->Nb0DElements();
1074 TInt elem0DNum = FromSmIdType<TInt>( myMesh->MaxElementID() + 1 );
1075 for ( size_t i = 0; i < nodesOf0D.size(); ++i )
1076 aCellInfo->SetElemNum( iElem++, elem0DNum++);
1079 // store data in a file
1080 myMed->SetCellInfo(aCellInfo);
1082 } // loop on geom types
1084 catch(const std::exception& exc) {
1085 INFOS("The following exception was caught:\n\t"<<exc.what());
1089 INFOS("Unknown exception was caught !!!");
1095 mySubMeshes.clear();
1099 //================================================================================
1101 * \brief Returns nodes on VERTEXes where 0D elements are absent
1103 //================================================================================
1105 bool DriverMED_W_SMESHDS_Mesh::
1106 getNodesOfMissing0DOnVert(SMESHDS_Mesh* meshDS,
1107 std::vector<const SMDS_MeshElement*>& nodes)
1110 for ( int i = 1; i <= meshDS->MaxShapeIndex(); ++i )
1112 if ( meshDS->IndexToShape( i ).ShapeType() != TopAbs_VERTEX )
1114 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(i) ) {
1115 SMDS_NodeIteratorPtr nIt= sm->GetNodes();
1118 const SMDS_MeshNode* n = nIt->next();
1119 if ( n->NbInverseElements( SMDSAbs_0DElement ) == 0 )
1120 nodes.push_back( n );
1124 return !nodes.empty();