1 // SMESH DriverMED : driver to read and write 'med' files
3 // Copyright (C) 2003 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.
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : DriverMED_R_SMESHDS_Mesh.cxx
27 #include "DriverMED_R_SMESHDS_Mesh.h"
28 #include "DriverMED_R_SMDS_Mesh.h"
29 #include "SMESHDS_Mesh.hxx"
30 #include "utilities.h"
32 #include "DriverMED_Family.h"
34 #include "SMESHDS_Group.hxx"
36 #include "MED_Factory.hxx"
37 #include "MED_CoordUtils.hxx"
38 #include "MED_Utilities.hxx"
43 static int MYDEBUG = 0;
45 static int MYDEBUG = 0;
48 #define _EDF_NODE_IDS_
53 DriverMED_R_SMESHDS_Mesh
54 ::SetMeshName(string theMeshName)
56 myMeshName = theMeshName;
59 static const SMDS_MeshNode*
60 FindNode(const SMDS_Mesh* theMesh, TInt theId){
61 const SMDS_MeshNode* aNode = theMesh->FindNode(theId);
62 if(aNode) return aNode;
63 EXCEPTION(runtime_error,"SMDS_Mesh::FindNode - cannot find a SMDS_MeshNode for ID = "<<theId);
68 DriverMED_R_SMESHDS_Mesh
71 Status aResult = DRS_FAIL;
74 if(MYDEBUG) MESSAGE("Perform - myFile : "<<myFile);
75 PWrapper aMed = CrWrapper(myFile);
78 if(TInt aNbMeshes = aMed->GetNbMeshes()){
79 for(int iMesh = 0; iMesh < aNbMeshes; iMesh++){
80 // Reading the MED mesh
81 //---------------------
82 PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
85 ostringstream aMeshNameStr;
86 aMeshNameStr<<myMeshId;
87 aMeshName = aMeshNameStr.str();
89 aMeshName = myMeshName;
91 if(MYDEBUG) MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
92 if(aMeshName != aMeshInfo->GetName()) continue;
94 //TInt aMeshDim = aMeshInfo->GetDim();
96 // Reading MED families to the temporary structure
97 //------------------------------------------------
99 TInt aNbFams = aMed->GetNbFamilies(aMeshInfo);
100 if(MYDEBUG) MESSAGE("Read " << aNbFams << " families");
101 for (TInt iFam = 0; iFam < aNbFams; iFam++) {
102 PFamilyInfo aFamilyInfo = aMed->GetPFamilyInfo(aMeshInfo,iFam+1,&anErr);
104 TInt aFamId = aFamilyInfo->GetId();
105 if(MYDEBUG) MESSAGE("Family " << aFamId << " :");
107 DriverMED_FamilyPtr aFamily (new DriverMED_Family);
109 TInt aNbGrp = aFamilyInfo->GetNbGroup();
110 if(MYDEBUG) MESSAGE("belong to " << aNbGrp << " groups");
111 for (TInt iGr = 0; iGr < aNbGrp; iGr++) {
112 string aGroupName = aFamilyInfo->GetGroupName(iGr);
113 if(MYDEBUG) MESSAGE(aGroupName);
114 aFamily->AddGroupName(aGroupName);
116 aFamily->SetId( aFamId );
117 myFamilies[aFamId] = aFamily;
121 // Reading MED nodes to the corresponding SMDS structure
122 //------------------------------------------------------
123 PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
125 PCoordHelper aCoordHelper = GetCoordHelper(aNodeInfo);
127 EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
128 TInt aNbElems = aNodeInfo->GetNbElem();
129 if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
130 DriverMED_FamilyPtr aFamily;
131 for(TInt iElem = 0; iElem < aNbElems; iElem++){
132 TCCoordSlice aCoordSlice = aNodeInfo->GetCoordSlice(iElem);
133 double aCoords[3] = {0.0, 0.0, 0.0};
134 for(TInt iDim = 0; iDim < 3; iDim++)
135 aCoords[iDim] = aCoordHelper->GetCoord(aCoordSlice,iDim);
136 const SMDS_MeshNode* aNode;
138 aNode = myMesh->AddNodeWithID
139 (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
141 aNode = myMesh->AddNode
142 (aCoords[0],aCoords[1],aCoords[2]);
144 //cout<<aNode->GetID()<<": "<<aNode->X()<<", "<<aNode->Y()<<", "<<aNode->Z()<<endl;
146 // Save reference to this node from its family
147 TInt aFamNum = aNodeInfo->GetFamNum(iElem);
148 if ( checkFamilyID ( aFamily, aFamNum ))
150 aFamily->AddElement(aNode);
151 aFamily->SetType(SMDSAbs_Node);
155 // Reading pre information about all MED cells
156 //--------------------------------------------
157 typedef std::vector<int> TNodeIds;
158 bool takeNumbers = true; // initially we trust the numbers from file
159 MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
160 MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
161 for(; anEntityIter != aEntityInfo.end(); anEntityIter++){
162 const EEntiteMaillage& anEntity = anEntityIter->first;
163 if(anEntity == eNOEUD) continue;
164 // Reading MED cells to the corresponding SMDS structure
165 //------------------------------------------------------
166 const MED::TGeom2Size& aGeom2Size = anEntityIter->second;
167 MED::TGeom2Size::const_iterator aGeom2SizeIter = aGeom2Size.begin();
168 for(; aGeom2SizeIter != aGeom2Size.end(); aGeom2SizeIter++){
169 const EGeometrieElement& aGeom = aGeom2SizeIter->first;
175 PPolygoneInfo aPolygoneInfo = aMed->GetPPolygoneInfo(aMeshInfo,anEntity,aGeom);
176 EBooleen anIsElemNum = takeNumbers ? aPolygoneInfo->IsElemNum() : eFAUX;
178 TInt aNbElem = aPolygoneInfo->GetNbElem();
179 for(TInt iElem = 0; iElem < aNbElem; iElem++){
180 MED::TCConnSlice aConnSlice = aPolygoneInfo->GetConnSlice(iElem);
181 TInt aNbConn = aPolygoneInfo->GetNbConn(iElem);
182 TNodeIds aNodeIds(aNbConn);
183 #ifdef _EDF_NODE_IDS_
185 for(TInt iConn = 0; iConn < aNbConn; iConn++)
186 aNodeIds[iConn] = aNodeInfo->GetElemNum(aConnSlice[iConn] - 1);
188 for(TInt iConn = 0; iConn < aNbConn; iConn++)
189 aNodeIds[iConn] = aConnSlice[iConn];
191 for(TInt iConn = 0; iConn < aNbConn; iConn++)
192 aNodeIds[iConn] = aConnSlice[iConn];
194 bool isRenum = false;
195 SMDS_MeshElement* anElement = NULL;
196 TInt aFamNum = aPolygoneInfo->GetFamNum(iElem);
200 TInt anElemId = aPolygoneInfo->GetElemNum(iElem);
201 anElement = myMesh->AddPolygonalFaceWithID(aNodeIds,anElemId);
204 std::vector<const SMDS_MeshNode*> aNodes(aNbConn);
205 for(TInt iConn = 0; iConn < aNbConn; iConn++)
206 aNodes[iConn] = FindNode(myMesh,aNodeIds[iConn]);
207 anElement = myMesh->AddPolygonalFace(aNodes);
208 isRenum = anIsElemNum;
210 }catch(const std::exception& exc){
217 aResult = DRS_WARN_SKIP_ELEM;
222 if(aResult < DRS_WARN_RENUMBER)
223 aResult = DRS_WARN_RENUMBER;
225 if ( checkFamilyID ( aFamily, aFamNum ))
227 // Save reference to this element from its family
228 aFamily->AddElement(anElement);
229 aFamily->SetType(anElement->GetType());
232 } // for (TInt iPG = 0; iPG < nbPolygons; iPG++)
236 PPolyedreInfo aPolyedreInfo = aMed->GetPPolyedreInfo(aMeshInfo,anEntity,aGeom);
237 EBooleen anIsElemNum = takeNumbers ? aPolyedreInfo->IsElemNum() : eFAUX;
239 TInt aNbElem = aPolyedreInfo->GetNbElem();
240 for(TInt iElem = 0; iElem < aNbElem; iElem++){
241 MED::TCConnSliceArr aConnSliceArr = aPolyedreInfo->GetConnSliceArr(iElem);
242 TInt aNbFaces = aConnSliceArr.size();
243 typedef std::vector<int> TQuantities;
244 TQuantities aQuantities(aNbFaces);
245 TInt aNbNodes = aPolyedreInfo->GetNbNodes(iElem);
246 TNodeIds aNodeIds(aNbNodes);
247 for(TInt iFace = 0, iNode = 0; iFace < aNbFaces; iFace++){
248 MED::TCConnSlice aConnSlice = aConnSliceArr[iFace];
249 TInt aNbConn = aConnSlice.size();
250 aQuantities[iFace] = aNbConn;
251 #ifdef _EDF_NODE_IDS_
253 for(TInt iConn = 0; iConn < aNbConn; iConn++)
254 aNodeIds[iNode++] = aNodeInfo->GetElemNum(aConnSlice[iConn] - 1);
256 for(TInt iConn = 0; iConn < aNbConn; iConn++)
257 aNodeIds[iNode++] = aConnSlice[iConn];
259 for(TInt iConn = 0; iConn < aNbConn; iConn++)
260 aNodeIds[iNode++] = aConnSlice[iConn];
264 bool isRenum = false;
265 SMDS_MeshElement* anElement = NULL;
266 TInt aFamNum = aPolyedreInfo->GetFamNum(iElem);
270 TInt anElemId = aPolyedreInfo->GetElemNum(iElem);
271 anElement = myMesh->AddPolyhedralVolumeWithID(aNodeIds,aQuantities,anElemId);
274 std::vector<const SMDS_MeshNode*> aNodes(aNbNodes);
275 for(TInt iConn = 0; iConn < aNbNodes; iConn++)
276 aNodes[iConn] = FindNode(myMesh,aNodeIds[iConn]);
277 anElement = myMesh->AddPolyhedralVolume(aNodes,aQuantities);
278 isRenum = anIsElemNum;
280 }catch(const std::exception& exc){
287 aResult = DRS_WARN_SKIP_ELEM;
292 if (aResult < DRS_WARN_RENUMBER)
293 aResult = DRS_WARN_RENUMBER;
295 if ( checkFamilyID ( aFamily, aFamNum ))
297 // Save reference to this element from its family
298 aFamily->AddElement(anElement);
299 aFamily->SetType(anElement->GetType());
302 } // for (int iPE = 0; iPE < nbPolyedres; iPE++)
306 PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
307 EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
308 TInt aNbElems = aCellInfo->GetNbElem();
309 if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
310 if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
312 for(int iElem = 0; iElem < aNbElems; iElem++){
345 TNodeIds aNodeIds(aNbNodes);
346 bool anIsValidConnect = false;
347 TCConnSlice aConnSlice = aCellInfo->GetConnSlice(iElem);
349 #ifdef _EDF_NODE_IDS_
351 for(int iNode = 0; iNode < aNbNodes; iNode++)
352 aNodeIds[iNode] = aNodeInfo->GetElemNum(aConnSlice[iNode] - 1);
354 for(int iNode = 0; iNode < aNbNodes; iNode++)
355 aNodeIds[iNode] = aConnSlice[iNode];
357 for(int iNode = 0; iNode < aNbNodes; iNode++)
358 aNodeIds[iNode] = aConnSlice[iNode];
360 anIsValidConnect = true;
361 }catch(const std::exception& exc){
362 //INFOS("Follow exception was cought:\n\t"<<exc.what());
365 //INFOS("Unknown exception was cought !!!");
369 if(!anIsValidConnect)
372 bool isRenum = false;
373 SMDS_MeshElement* anElement = NULL;
374 TInt aFamNum = aCellInfo->GetFamNum(iElem);
376 //MESSAGE("Try to create element # " << iElem << " with id = "
377 // << aCellInfo->GetElemNum(iElem));
382 anElement = myMesh->AddEdgeWithID(aNodeIds[0],
384 aCellInfo->GetElemNum(iElem));
386 anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
387 FindNode(myMesh,aNodeIds[1]));
388 isRenum = anIsElemNum;
395 anElement = myMesh->AddFaceWithID(aNodeIds[0],
398 aCellInfo->GetElemNum(iElem));
400 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
401 FindNode(myMesh,aNodeIds[1]),
402 FindNode(myMesh,aNodeIds[2]));
403 isRenum = anIsElemNum;
409 // There is some differnce between SMDS and MED
411 anElement = myMesh->AddFaceWithID(aNodeIds[0],
415 aCellInfo->GetElemNum(iElem));
417 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
418 FindNode(myMesh,aNodeIds[1]),
419 FindNode(myMesh,aNodeIds[2]),
420 FindNode(myMesh,aNodeIds[3]));
421 isRenum = anIsElemNum;
428 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
432 aCellInfo->GetElemNum(iElem));
434 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
435 FindNode(myMesh,aNodeIds[1]),
436 FindNode(myMesh,aNodeIds[2]),
437 FindNode(myMesh,aNodeIds[3]));
438 isRenum = anIsElemNum;
444 // There is some differnce between SMDS and MED
446 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
451 aCellInfo->GetElemNum(iElem));
453 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
454 FindNode(myMesh,aNodeIds[1]),
455 FindNode(myMesh,aNodeIds[2]),
456 FindNode(myMesh,aNodeIds[3]),
457 FindNode(myMesh,aNodeIds[4]));
458 isRenum = anIsElemNum;
465 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
471 aCellInfo->GetElemNum(iElem));
473 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
474 FindNode(myMesh,aNodeIds[1]),
475 FindNode(myMesh,aNodeIds[2]),
476 FindNode(myMesh,aNodeIds[3]),
477 FindNode(myMesh,aNodeIds[4]),
478 FindNode(myMesh,aNodeIds[5]));
479 isRenum = anIsElemNum;
486 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
494 aCellInfo->GetElemNum(iElem));
496 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
497 FindNode(myMesh,aNodeIds[1]),
498 FindNode(myMesh,aNodeIds[2]),
499 FindNode(myMesh,aNodeIds[3]),
500 FindNode(myMesh,aNodeIds[4]),
501 FindNode(myMesh,aNodeIds[5]),
502 FindNode(myMesh,aNodeIds[6]),
503 FindNode(myMesh,aNodeIds[7]));
504 isRenum = anIsElemNum;
508 }catch(const std::exception& exc){
509 //INFOS("Follow exception was cought:\n\t"<<exc.what());
512 //INFOS("Unknown exception was cought !!!");
517 aResult = DRS_WARN_SKIP_ELEM;
523 if (aResult < DRS_WARN_RENUMBER)
524 aResult = DRS_WARN_RENUMBER;
526 if ( checkFamilyID ( aFamily, aFamNum )) {
527 // Save reference to this element from its family
528 myFamilies[aFamNum]->AddElement(anElement);
529 myFamilies[aFamNum]->SetType(anElement->GetType());
538 }catch(const std::exception& exc){
539 INFOS("Follow exception was cought:\n\t"<<exc.what());
542 INFOS("Unknown exception was cought !!!");
545 if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
549 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
551 list<string> aMeshNames;
554 if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
556 PWrapper aMed = CrWrapper(myFile);
558 if (TInt aNbMeshes = aMed->GetNbMeshes()) {
559 for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
560 // Reading the MED mesh
561 //---------------------
562 PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
563 aMeshNames.push_back(aMeshInfo->GetName());
566 }catch(const std::exception& exc){
567 INFOS("Follow exception was cought:\n\t"<<exc.what());
568 theStatus = DRS_FAIL;
570 INFOS("Unknown exception was cought !!!");
571 theStatus = DRS_FAIL;
577 list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
579 list<TNameAndType> aResult;
580 set<TNameAndType> aResGroupNames;
582 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
583 for (; aFamsIter != myFamilies.end(); aFamsIter++)
585 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
586 const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
587 set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
588 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
590 TNameAndType aNameAndType = make_pair( *aGrNamesIter, aFamily->GetType() );
591 // Check, if this is a Group or SubMesh name
592 //if (aName.substr(0, 5) == string("Group")) {
593 if ( aResGroupNames.insert( aNameAndType ).second ) {
594 aResult.push_back( aNameAndType );
603 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
605 string aGroupName (theGroup->GetStoreName());
606 if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
608 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
609 for (; aFamsIter != myFamilies.end(); aFamsIter++)
611 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
612 if (aFamily->GetType() == theGroup->GetType() && aFamily->MemberOf(aGroupName))
614 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
615 set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
616 const SMDS_MeshElement * element = 0;
617 for (; anElemsIter != anElements.end(); anElemsIter++)
619 element = *anElemsIter;
620 theGroup->SMDSGroup().Add(element);
623 theGroup->SetType( element->GetType() );
628 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
631 char submeshGrpName[ 30 ];
632 sprintf( submeshGrpName, "SubMesh %d", theId );
633 string aName (submeshGrpName);
634 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
635 for (; aFamsIter != myFamilies.end(); aFamsIter++)
637 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
638 if (aFamily->MemberOf(aName))
640 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
641 set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
642 if (aFamily->GetType() == SMDSAbs_Node)
644 for (; anElemsIter != anElements.end(); anElemsIter++)
646 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
647 theSubMesh->AddNode(node);
652 for (; anElemsIter != anElements.end(); anElemsIter++)
654 theSubMesh->AddElement(*anElemsIter);
661 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
663 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
664 for (; aFamsIter != myFamilies.end(); aFamsIter++)
666 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
667 MED::TStringSet aGroupNames = aFamily->GetGroupNames();
668 set<string>::iterator aGrNamesIter = aGroupNames.begin();
669 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
671 string aName = *aGrNamesIter;
672 // Check, if this is a Group or SubMesh name
673 if (aName.substr(0, 7) == string("SubMesh"))
675 int Id = atoi(string(aName).substr(7).c_str());
676 set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
677 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
678 if (aFamily->GetType() == SMDSAbs_Node)
680 for (; anElemsIter != anElements.end(); anElemsIter++)
682 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
683 ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
684 // find out a shape type
685 TopoDS_Shape aShape = myMesh->IndexToShape( Id );
686 int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
687 switch ( aShapeType ) {
689 myMesh->SetNodeOnFace(node, Id); break;
691 myMesh->SetNodeOnEdge(node, Id); break;
693 myMesh->SetNodeOnVertex(node, Id); break;
695 myMesh->SetNodeInVolume(node, Id);
701 for (; anElemsIter != anElements.end(); anElemsIter++)
703 myMesh->SetMeshElementOnShape(*anElemsIter, Id);
711 * \brief Ensure aFamily to have required ID
712 * \param aFamily - a family to check and update
713 * \param anID - an ID aFamily should have
714 * \retval bool - true if successful
716 bool DriverMED_R_SMESHDS_Mesh::checkFamilyID(DriverMED_FamilyPtr & aFamily, int anID) const
718 if ( !aFamily || aFamily->GetId() != anID ) {
719 map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
720 if ( i_fam == myFamilies.end() )
722 aFamily = i_fam->second;
724 return ( aFamily->GetId() == anID );