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 MED::TVector<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());
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 MED::TVector<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 )) {
296 // Save reference to this element from its family
297 aFamily->AddElement(anElement);
298 aFamily->SetType(anElement->GetType());
305 PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
306 EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
307 TInt aNbElems = aCellInfo->GetNbElem();
308 if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
309 if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
311 for(int iElem = 0; iElem < aNbElems; iElem++){
344 TNodeIds aNodeIds(aNbNodes);
345 bool anIsValidConnect = false;
346 TCConnSlice aConnSlice = aCellInfo->GetConnSlice(iElem);
348 #ifdef _EDF_NODE_IDS_
350 for(int iNode = 0; iNode < aNbNodes; iNode++)
351 aNodeIds[iNode] = aNodeInfo->GetElemNum(aConnSlice[iNode] - 1);
353 for(int iNode = 0; iNode < aNbNodes; iNode++)
354 aNodeIds[iNode] = aConnSlice[iNode];
356 for(int iNode = 0; iNode < aNbNodes; iNode++)
357 aNodeIds[iNode] = aConnSlice[iNode];
359 anIsValidConnect = true;
360 }catch(const std::exception& exc){
361 //INFOS("Follow exception was cought:\n\t"<<exc.what());
364 //INFOS("Unknown exception was cought !!!");
368 if(!anIsValidConnect)
371 bool isRenum = false;
372 SMDS_MeshElement* anElement = NULL;
373 TInt aFamNum = aCellInfo->GetFamNum(iElem);
375 //MESSAGE("Try to create element # " << iElem << " with id = "
376 // << aCellInfo->GetElemNum(iElem));
381 anElement = myMesh->AddEdgeWithID(aNodeIds[0],
383 aCellInfo->GetElemNum(iElem));
385 anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
386 FindNode(myMesh,aNodeIds[1]));
387 isRenum = anIsElemNum;
394 anElement = myMesh->AddFaceWithID(aNodeIds[0],
397 aCellInfo->GetElemNum(iElem));
399 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
400 FindNode(myMesh,aNodeIds[1]),
401 FindNode(myMesh,aNodeIds[2]));
402 isRenum = anIsElemNum;
408 // There is some differnce between SMDS and MED
410 anElement = myMesh->AddFaceWithID(aNodeIds[0],
414 aCellInfo->GetElemNum(iElem));
416 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
417 FindNode(myMesh,aNodeIds[1]),
418 FindNode(myMesh,aNodeIds[2]),
419 FindNode(myMesh,aNodeIds[3]));
420 isRenum = anIsElemNum;
427 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
431 aCellInfo->GetElemNum(iElem));
433 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
434 FindNode(myMesh,aNodeIds[1]),
435 FindNode(myMesh,aNodeIds[2]),
436 FindNode(myMesh,aNodeIds[3]));
437 isRenum = anIsElemNum;
443 // There is some differnce between SMDS and MED
445 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
450 aCellInfo->GetElemNum(iElem));
452 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
453 FindNode(myMesh,aNodeIds[1]),
454 FindNode(myMesh,aNodeIds[2]),
455 FindNode(myMesh,aNodeIds[3]),
456 FindNode(myMesh,aNodeIds[4]));
457 isRenum = anIsElemNum;
464 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
470 aCellInfo->GetElemNum(iElem));
472 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
473 FindNode(myMesh,aNodeIds[1]),
474 FindNode(myMesh,aNodeIds[2]),
475 FindNode(myMesh,aNodeIds[3]),
476 FindNode(myMesh,aNodeIds[4]),
477 FindNode(myMesh,aNodeIds[5]));
478 isRenum = anIsElemNum;
485 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
493 aCellInfo->GetElemNum(iElem));
495 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
496 FindNode(myMesh,aNodeIds[1]),
497 FindNode(myMesh,aNodeIds[2]),
498 FindNode(myMesh,aNodeIds[3]),
499 FindNode(myMesh,aNodeIds[4]),
500 FindNode(myMesh,aNodeIds[5]),
501 FindNode(myMesh,aNodeIds[6]),
502 FindNode(myMesh,aNodeIds[7]));
503 isRenum = anIsElemNum;
507 }catch(const std::exception& exc){
508 //INFOS("Follow exception was cought:\n\t"<<exc.what());
511 //INFOS("Unknown exception was cought !!!");
516 aResult = DRS_WARN_SKIP_ELEM;
522 if (aResult < DRS_WARN_RENUMBER)
523 aResult = DRS_WARN_RENUMBER;
525 if ( checkFamilyID ( aFamily, aFamNum )) {
526 // Save reference to this element from its family
527 myFamilies[aFamNum]->AddElement(anElement);
528 myFamilies[aFamNum]->SetType(anElement->GetType());
537 }catch(const std::exception& exc){
538 INFOS("Follow exception was cought:\n\t"<<exc.what());
541 INFOS("Unknown exception was cought !!!");
544 if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
548 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
550 list<string> aMeshNames;
553 if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
555 PWrapper aMed = CrWrapper(myFile);
557 if (TInt aNbMeshes = aMed->GetNbMeshes()) {
558 for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
559 // Reading the MED mesh
560 //---------------------
561 PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
562 aMeshNames.push_back(aMeshInfo->GetName());
565 }catch(const std::exception& exc){
566 INFOS("Follow exception was cought:\n\t"<<exc.what());
567 theStatus = DRS_FAIL;
569 INFOS("Unknown exception was cought !!!");
570 theStatus = DRS_FAIL;
576 list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
578 list<TNameAndType> aResult;
579 set<TNameAndType> aResGroupNames;
581 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
582 for (; aFamsIter != myFamilies.end(); aFamsIter++)
584 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
585 const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
586 set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
587 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
589 TNameAndType aNameAndType = make_pair( *aGrNamesIter, aFamily->GetType() );
590 // Check, if this is a Group or SubMesh name
591 //if (aName.substr(0, 5) == string("Group")) {
592 if ( aResGroupNames.insert( aNameAndType ).second ) {
593 aResult.push_back( aNameAndType );
602 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
604 string aGroupName (theGroup->GetStoreName());
605 if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
607 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
608 for (; aFamsIter != myFamilies.end(); aFamsIter++)
610 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
611 if (aFamily->GetType() == theGroup->GetType() && aFamily->MemberOf(aGroupName))
613 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
614 set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
615 const SMDS_MeshElement * element = 0;
616 for (; anElemsIter != anElements.end(); anElemsIter++)
618 element = *anElemsIter;
619 theGroup->SMDSGroup().Add(element);
622 theGroup->SetType( element->GetType() );
627 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
630 char submeshGrpName[ 30 ];
631 sprintf( submeshGrpName, "SubMesh %d", theId );
632 string aName (submeshGrpName);
633 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
634 for (; aFamsIter != myFamilies.end(); aFamsIter++)
636 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
637 if (aFamily->MemberOf(aName))
639 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
640 set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
641 if (aFamily->GetType() == SMDSAbs_Node)
643 for (; anElemsIter != anElements.end(); anElemsIter++)
645 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
646 theSubMesh->AddNode(node);
651 for (; anElemsIter != anElements.end(); anElemsIter++)
653 theSubMesh->AddElement(*anElemsIter);
660 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
662 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
663 for (; aFamsIter != myFamilies.end(); aFamsIter++)
665 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
666 MED::TStringSet aGroupNames = aFamily->GetGroupNames();
667 set<string>::iterator aGrNamesIter = aGroupNames.begin();
668 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
670 string aName = *aGrNamesIter;
671 // Check, if this is a Group or SubMesh name
672 if (aName.substr(0, 7) == string("SubMesh"))
674 int Id = atoi(string(aName).substr(7).c_str());
675 set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
676 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
677 if (aFamily->GetType() == SMDSAbs_Node)
679 for (; anElemsIter != anElements.end(); anElemsIter++)
681 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
682 ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
683 // find out a shape type
684 TopoDS_Shape aShape = myMesh->IndexToShape( Id );
685 int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
686 switch ( aShapeType ) {
688 myMesh->SetNodeOnFace(node, Id); break;
690 myMesh->SetNodeOnEdge(node, Id); break;
692 myMesh->SetNodeOnVertex(node, Id); break;
694 myMesh->SetNodeInVolume(node, Id);
700 for (; anElemsIter != anElements.end(); anElemsIter++)
702 myMesh->SetMeshElementOnShape(*anElemsIter, Id);
710 * \brief Ensure aFamily to have required ID
711 * \param aFamily - a family to check and update
712 * \param anID - an ID aFamily should have
713 * \retval bool - true if successful
715 bool DriverMED_R_SMESHDS_Mesh::checkFamilyID(DriverMED_FamilyPtr & aFamily, int anID) const
717 if ( !aFamily || aFamily->GetId() != anID ) {
718 map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
719 if ( i_fam == myFamilies.end() )
721 aFamily = i_fam->second;
723 return ( aFamily->GetId() == anID );