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_Utilities.hxx"
42 static int MYDEBUG = 0;
44 static int MYDEBUG = 0;
47 #define _EDF_NODE_IDS_
51 void DriverMED_R_SMESHDS_Mesh::SetMeshName(string theMeshName)
53 myMeshName = theMeshName;
56 static const SMDS_MeshNode*
57 FindNode(const SMDS_Mesh* theMesh, TInt theId){
58 const SMDS_MeshNode* aNode = theMesh->FindNode(theId);
59 if(aNode) return aNode;
60 EXCEPTION(runtime_error,"SMDS_Mesh::FindNode - cannot find a SMDS_MeshNode for ID = "<<theId);
64 enum ECoordName{eX, eY, eZ, eNone};
65 typedef TFloat (*TGetCoord)(MED::PNodeInfo&, TInt);
67 template<ECoordName TheCoordId>
68 TFloat GetCoord(MED::PNodeInfo& thePNodeInfo, TInt theElemId){
69 return thePNodeInfo->GetNodeCoord(theElemId,TheCoordId);
73 TFloat GetCoord<eNone>(MED::PNodeInfo& thePNodeInfo, TInt theElemId){
78 static TGetCoord aXYZGetCoord[3] = {
85 static TGetCoord aXYGetCoord[3] = {
91 static TGetCoord aYZGetCoord[3] = {
97 static TGetCoord aXZGetCoord[3] = {
104 static TGetCoord aXGetCoord[3] = {
110 static TGetCoord aYGetCoord[3] = {
116 static TGetCoord aZGetCoord[3] = {
124 MED::PNodeInfo myPNodeInfo;
125 TGetCoord* myGetCoord;
127 TCoordHelper(const MED::PNodeInfo& thePNodeInfo,
128 TGetCoord* theGetCoord):
129 myPNodeInfo(thePNodeInfo),
130 myGetCoord(theGetCoord)
132 virtual ~TCoordHelper(){}
133 TFloat GetCoord(TInt theElemId, TInt theCoodId){
134 return (*myGetCoord[theCoodId])(myPNodeInfo,theElemId);
137 typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
140 Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
142 Status aResult = DRS_FAIL;
145 if(MYDEBUG) MESSAGE("Perform - myFile : "<<myFile);
146 PWrapper aMed = CrWrapper(myFile);
149 if(TInt aNbMeshes = aMed->GetNbMeshes()){
150 for(int iMesh = 0; iMesh < aNbMeshes; iMesh++){
151 // Reading the MED mesh
152 //---------------------
153 PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
155 if (myMeshId != -1) {
156 ostringstream aMeshNameStr;
157 aMeshNameStr<<myMeshId;
158 aMeshName = aMeshNameStr.str();
160 aMeshName = myMeshName;
162 if(MYDEBUG) MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
163 if(aMeshName != aMeshInfo->GetName()) continue;
165 //TInt aMeshDim = aMeshInfo->GetDim();
167 // Reading MED families to the temporary structure
168 //------------------------------------------------
170 TInt aNbFams = aMed->GetNbFamilies(aMeshInfo);
171 if(MYDEBUG) MESSAGE("Read " << aNbFams << " families");
172 for (TInt iFam = 0; iFam < aNbFams; iFam++) {
173 PFamilyInfo aFamilyInfo = aMed->GetPFamilyInfo(aMeshInfo,iFam+1,&anErr);
175 TInt aFamId = aFamilyInfo->GetId();
176 if(MYDEBUG) MESSAGE("Family " << aFamId << " :");
178 DriverMED_FamilyPtr aFamily (new DriverMED_Family);
180 TInt aNbGrp = aFamilyInfo->GetNbGroup();
181 if(MYDEBUG) MESSAGE("belong to " << aNbGrp << " groups");
182 for (TInt iGr = 0; iGr < aNbGrp; iGr++) {
183 string aGroupName = aFamilyInfo->GetGroupName(iGr);
184 if(MYDEBUG) MESSAGE(aGroupName);
185 aFamily->AddGroupName(aGroupName);
187 myFamilies[aFamId] = aFamily;
191 // Reading MED nodes to the corresponding SMDS structure
192 //------------------------------------------------------
193 PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
195 TCoordHelperPtr aCoordHelperPtr;
197 TInt aMeshDimension = aMeshInfo->GetDim();
198 bool anIsDimPresent[3] = {false, false, false};
199 for(TInt iDim = 0; iDim < aMeshDimension; iDim++){
200 string aDimName = aNodeInfo->GetCoordName(iDim);
201 if(aDimName == "x" || aDimName == "X")
202 anIsDimPresent[eX] = true;
203 else if(aDimName == "y" || aDimName == "Y")
204 anIsDimPresent[eY] = true;
205 else if(aDimName == "z" || aDimName == "Z")
206 anIsDimPresent[eZ] = true;
208 switch(aMeshDimension){
210 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYZGetCoord));
213 if(anIsDimPresent[eY] && anIsDimPresent[eZ])
214 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYZGetCoord));
215 else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
216 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXZGetCoord));
218 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYGetCoord));
221 if(anIsDimPresent[eY])
222 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYGetCoord));
223 else if(anIsDimPresent[eZ])
224 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aZGetCoord));
226 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXGetCoord));
231 EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
232 TInt aNbElems = aNodeInfo->GetNbElem();
233 if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
234 for(TInt iElem = 0; iElem < aNbElems; iElem++){
235 double aCoords[3] = {0.0, 0.0, 0.0};
236 for(TInt iDim = 0; iDim < 3; iDim++)
237 aCoords[iDim] = aCoordHelperPtr->GetCoord(iElem,iDim);
238 const SMDS_MeshNode* aNode;
240 aNode = myMesh->AddNodeWithID
241 (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
243 aNode = myMesh->AddNode
244 (aCoords[0],aCoords[1],aCoords[2]);
246 //cout<<aNode->GetID()<<": "<<aNode->X()<<", "<<aNode->Y()<<", "<<aNode->Z()<<endl;
248 // Save reference to this node from its family
249 TInt aFamNum = aNodeInfo->GetFamNum(iElem);
250 if (myFamilies.find(aFamNum) != myFamilies.end())
252 myFamilies[aFamNum]->AddElement(aNode);
253 myFamilies[aFamNum]->SetType(SMDSAbs_Node);
257 // Reading pre information about all MED cells
258 //--------------------------------------------
259 bool takeNumbers = true; // initially we trust the numbers from file
260 MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
261 MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
262 for(; anEntityIter != aEntityInfo.end(); anEntityIter++){
263 const EEntiteMaillage& anEntity = anEntityIter->first;
264 if(anEntity == eNOEUD) continue;
265 // Reading MED cells to the corresponding SMDS structure
266 //------------------------------------------------------
267 const MED::TGeom& aTGeom = anEntityIter->second;
268 MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
269 for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
270 const EGeometrieElement& aGeom = anTGeomIter->first;
271 if(aGeom == ePOINT1) continue;
272 PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
273 EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
274 TInt aNbElems = aCellInfo->GetNbElem();
275 if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
276 if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
278 for(int iElem = 0; iElem < aNbElems; iElem++){
311 vector<TInt> aNodeIds(aNbNodes);
312 #ifdef _EDF_NODE_IDS_
314 for(int i = 0; i < aNbNodes; i++){
315 aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
318 for(int i = 0; i < aNbNodes; i++){
319 aNodeIds[i] = aCellInfo->GetConn(iElem,i);
323 for(int i = 0; i < aNbNodes; i++){
324 aNodeIds[i] = aCellInfo->GetConn(iElem,i);
328 bool isRenum = false;
329 SMDS_MeshElement* anElement = NULL;
330 TInt aFamNum = aCellInfo->GetFamNum(iElem);
336 anElement = myMesh->AddEdgeWithID(aNodeIds[0],
338 aCellInfo->GetElemNum(iElem));
340 anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
341 FindNode(myMesh,aNodeIds[1]));
342 isRenum = anIsElemNum;
349 anElement = myMesh->AddFaceWithID(aNodeIds[0],
352 aCellInfo->GetElemNum(iElem));
354 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
355 FindNode(myMesh,aNodeIds[1]),
356 FindNode(myMesh,aNodeIds[2]));
357 isRenum = anIsElemNum;
363 // There is some differnce between SMDS and MED
365 anElement = myMesh->AddFaceWithID(aNodeIds[0],
369 aCellInfo->GetElemNum(iElem));
371 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
372 FindNode(myMesh,aNodeIds[1]),
373 FindNode(myMesh,aNodeIds[2]),
374 FindNode(myMesh,aNodeIds[3]));
375 isRenum = anIsElemNum;
382 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
386 aCellInfo->GetElemNum(iElem));
388 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
389 FindNode(myMesh,aNodeIds[1]),
390 FindNode(myMesh,aNodeIds[2]),
391 FindNode(myMesh,aNodeIds[3]));
392 isRenum = anIsElemNum;
398 // There is some differnce between SMDS and MED
400 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
405 aCellInfo->GetElemNum(iElem));
407 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
408 FindNode(myMesh,aNodeIds[1]),
409 FindNode(myMesh,aNodeIds[2]),
410 FindNode(myMesh,aNodeIds[3]),
411 FindNode(myMesh,aNodeIds[4]));
412 isRenum = anIsElemNum;
419 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
425 aCellInfo->GetElemNum(iElem));
427 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
428 FindNode(myMesh,aNodeIds[1]),
429 FindNode(myMesh,aNodeIds[2]),
430 FindNode(myMesh,aNodeIds[3]),
431 FindNode(myMesh,aNodeIds[4]),
432 FindNode(myMesh,aNodeIds[5]));
433 isRenum = anIsElemNum;
440 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
448 aCellInfo->GetElemNum(iElem));
450 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
451 FindNode(myMesh,aNodeIds[1]),
452 FindNode(myMesh,aNodeIds[2]),
453 FindNode(myMesh,aNodeIds[3]),
454 FindNode(myMesh,aNodeIds[4]),
455 FindNode(myMesh,aNodeIds[5]),
456 FindNode(myMesh,aNodeIds[6]),
457 FindNode(myMesh,aNodeIds[7]));
458 isRenum = anIsElemNum;
462 }catch(const std::exception& exc){
463 //INFOS("Follow exception was cought:\n\t"<<exc.what());
466 //INFOS("Unknown exception was cought !!!");
471 aResult = DRS_WARN_SKIP_ELEM;
477 if (aResult < DRS_WARN_RENUMBER)
478 aResult = DRS_WARN_RENUMBER;
480 if (myFamilies.find(aFamNum) != myFamilies.end()) {
481 // Save reference to this element from its family
482 myFamilies[aFamNum]->AddElement(anElement);
483 myFamilies[aFamNum]->SetType(anElement->GetType());
492 }catch(const std::exception& exc){
493 INFOS("Follow exception was cought:\n\t"<<exc.what());
496 INFOS("Unknown exception was cought !!!");
499 if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
503 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
505 list<string> aMeshNames;
508 if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
510 PWrapper aMed = CrWrapper(myFile);
512 if (TInt aNbMeshes = aMed->GetNbMeshes()) {
513 for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
514 // Reading the MED mesh
515 //---------------------
516 PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
517 aMeshNames.push_back(aMeshInfo->GetName());
520 }catch(const std::exception& exc){
521 INFOS("Follow exception was cought:\n\t"<<exc.what());
522 theStatus = DRS_FAIL;
524 INFOS("Unknown exception was cought !!!");
525 theStatus = DRS_FAIL;
531 list<string> DriverMED_R_SMESHDS_Mesh::GetGroupNames()
533 list<string> aResult;
534 set<string> aResGroupNames;
536 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
537 for (; aFamsIter != myFamilies.end(); aFamsIter++)
539 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
540 const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
541 set<string>::iterator aGrNamesIter = aGroupNames.begin();
542 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
544 string aName = *aGrNamesIter;
545 // Check, if this is a Group or SubMesh name
546 //if (aName.substr(0, 5) == string("Group")) {
547 if (aResGroupNames.find(aName) == aResGroupNames.end()) {
548 aResGroupNames.insert(aName);
549 aResult.push_back(aName);
558 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
560 string aGroupName (theGroup->GetStoreName());
561 if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
563 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
564 for (; aFamsIter != myFamilies.end(); aFamsIter++)
566 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
567 if (aFamily->MemberOf(aGroupName))
569 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
570 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
571 const SMDS_MeshElement * element = 0;
572 for (; anElemsIter != anElements.end(); anElemsIter++)
574 element = *anElemsIter;
575 theGroup->SMDSGroup().Add(element);
578 theGroup->SetType( element->GetType() );
583 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
586 char submeshGrpName[ 30 ];
587 sprintf( submeshGrpName, "SubMesh %d", theId );
588 string aName (submeshGrpName);
589 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
590 for (; aFamsIter != myFamilies.end(); aFamsIter++)
592 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
593 if (aFamily->MemberOf(aName))
595 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
596 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
597 if (aFamily->GetType() == SMDSAbs_Node)
599 for (; anElemsIter != anElements.end(); anElemsIter++)
601 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
602 theSubMesh->AddNode(node);
607 for (; anElemsIter != anElements.end(); anElemsIter++)
609 theSubMesh->AddElement(*anElemsIter);
616 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
618 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
619 for (; aFamsIter != myFamilies.end(); aFamsIter++)
621 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
622 MED::TStringSet aGroupNames = aFamily->GetGroupNames();
623 set<string>::iterator aGrNamesIter = aGroupNames.begin();
624 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
626 string aName = *aGrNamesIter;
627 // Check, if this is a Group or SubMesh name
628 if (aName.substr(0, 7) == string("SubMesh"))
630 int Id = atoi(string(aName).substr(7).c_str());
631 set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
632 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
633 if (aFamily->GetType() == SMDSAbs_Node)
635 for (; anElemsIter != anElements.end(); anElemsIter++)
637 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
638 ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
639 // find out a shape type
640 TopoDS_Shape aShape = myMesh->IndexToShape( Id );
641 int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
642 switch ( aShapeType ) {
644 myMesh->SetNodeOnFace(node, Id); break;
646 myMesh->SetNodeOnEdge(node, Id); break;
648 myMesh->SetNodeOnVertex(node, Id); break;
650 myMesh->SetNodeInVolume(node, Id);
656 for (; anElemsIter != anElements.end(); anElemsIter++)
658 myMesh->SetMeshElementOnShape(*anElemsIter, Id);