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 "MEDA_Wrapper.hxx"
37 #include "MED_Utilities.hxx"
41 #define _EDF_NODE_IDS_
43 void DriverMED_R_SMESHDS_Mesh::SetMeshName(string theMeshName)
45 myMeshName = theMeshName;
48 static const SMDS_MeshNode*
49 FindNode(const SMDS_Mesh* theMesh, med_int theId){
50 const SMDS_MeshNode* aNode = theMesh->FindNode(theId);
51 if(aNode) return aNode;
52 EXCEPTION(runtime_error,"SMDS_Mesh::FindNode - cannot find a SMDS_MeshNode for ID = "<<theId);
56 enum ECoordName{eX, eY, eZ, eNone};
57 typedef med_float (*TGetCoord)(MEDA::PNodeInfo&, med_int);
59 template<ECoordName TheCoordId>
60 med_float GetCoord(MEDA::PNodeInfo& thePNodeInfo, med_int theElemId){
61 return thePNodeInfo->GetNodeCoord(theElemId,TheCoordId);
65 med_float GetCoord<eNone>(MEDA::PNodeInfo& thePNodeInfo, med_int theElemId){
70 static TGetCoord aXYZGetCoord[3] = {
77 static TGetCoord aXYGetCoord[3] = {
83 static TGetCoord aYZGetCoord[3] = {
89 static TGetCoord aXZGetCoord[3] = {
96 static TGetCoord aXGetCoord[3] = {
102 static TGetCoord aYGetCoord[3] = {
108 static TGetCoord aZGetCoord[3] = {
116 MEDA::PNodeInfo myPNodeInfo;
117 TGetCoord* myGetCoord;
119 TCoordHelper(const MEDA::PNodeInfo& thePNodeInfo,
120 TGetCoord* theGetCoord):
121 myPNodeInfo(thePNodeInfo),
122 myGetCoord(theGetCoord)
124 virtual ~TCoordHelper(){}
125 med_float GetCoord(med_int theElemId, med_int theCoodId){
126 return (*myGetCoord[theCoodId])(myPNodeInfo,theElemId);
129 typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
132 Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
134 Status aResult = DRS_FAIL;
136 using namespace MEDA;
139 MESSAGE("Perform - myFile : "<<myFile);
140 TWrapper aMed(myFile);
143 if(med_int aNbMeshes = aMed.GetNbMeshes()){
144 for(int iMesh = 0; iMesh < aNbMeshes; iMesh++){
145 // Reading the MED mesh
146 //---------------------
147 PMeshInfo aMeshInfo = aMed.GetMeshInfo(iMesh);
149 if (myMeshId != -1) {
150 ostringstream aMeshNameStr;
151 aMeshNameStr<<myMeshId;
152 aMeshName = aMeshNameStr.str();
154 aMeshName = myMeshName;
156 MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
157 if(aMeshName != aMeshInfo->GetName()) continue;
159 med_int aMeshDim = aMeshInfo->GetDim();
161 // Reading MED families to the temporary structure
162 //------------------------------------------------
163 med_int aNbFams = aMed.GetNbFamilies(aMeshInfo);
164 MESSAGE("Read " << aNbFams << " families");
165 for (med_int iFam = 0; iFam < aNbFams; iFam++) {
166 PFamilyInfo aFamilyInfo = aMed.GetFamilyInfo(aMeshInfo, iFam);
167 med_int aFamId = aFamilyInfo->GetId();
168 MESSAGE("Family " << aFamId << " :");
170 DriverMED_FamilyPtr aFamily (new DriverMED_Family);
172 med_int aNbGrp = aFamilyInfo->GetNbGroup();
173 MESSAGE("belong to " << aNbGrp << " groups");
174 for (med_int iGr = 0; iGr < aNbGrp; iGr++) {
175 string aGroupName = aFamilyInfo->GetGroupName(iGr);
177 aFamily->AddGroupName(aGroupName);
179 myFamilies[aFamId] = aFamily;
182 // Reading MED nodes to the corresponding SMDS structure
183 //------------------------------------------------------
184 PNodeInfo aNodeInfo = aMed.GetNodeInfo(aMeshInfo);
186 TCoordHelperPtr aCoordHelperPtr;
188 med_int aMeshDimension = aMeshInfo->GetDim();
189 bool anIsDimPresent[3] = {false, false, false};
190 for(med_int iDim = 0; iDim < aMeshDimension; iDim++){
191 string aDimName = aNodeInfo->GetCoordName(iDim);
192 if(aDimName == "x" || aDimName == "X")
193 anIsDimPresent[eX] = true;
194 else if(aDimName == "y" || aDimName == "Y")
195 anIsDimPresent[eY] = true;
196 else if(aDimName == "z" || aDimName == "Z")
197 anIsDimPresent[eZ] = true;
199 switch(aMeshDimension){
201 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYZGetCoord));
204 if(anIsDimPresent[eY] && anIsDimPresent[eZ])
205 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYZGetCoord));
206 else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
207 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXZGetCoord));
209 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYGetCoord));
212 if(anIsDimPresent[eY])
213 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYGetCoord));
214 else if(anIsDimPresent[eZ])
215 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aZGetCoord));
217 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXGetCoord));
222 med_booleen anIsNodeNum = aNodeInfo->IsElemNum();
223 med_int aNbElems = aNodeInfo->GetNbElem();
224 MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
225 for(med_int iElem = 0; iElem < aNbElems; iElem++){
226 double aCoords[3] = {0.0, 0.0, 0.0};
227 for(med_int iDim = 0; iDim < 3; iDim++)
228 aCoords[iDim] = aCoordHelperPtr->GetCoord(iElem,iDim);
229 const SMDS_MeshNode* aNode;
231 aNode = myMesh->AddNodeWithID
232 (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
234 aNode = myMesh->AddNode
235 (aCoords[0],aCoords[1],aCoords[2]);
237 //cout<<aNode->GetID()<<": "<<aNode->X()<<", "<<aNode->Y()<<", "<<aNode->Z()<<endl;
239 // Save reference to this node from its family
240 med_int aFamNum = aNodeInfo->GetFamNum(iElem);
241 if (myFamilies.find(aFamNum) != myFamilies.end())
243 myFamilies[aFamNum]->AddElement(aNode);
244 myFamilies[aFamNum]->SetType(SMDSAbs_Node);
248 // Reading pre information about all MED cells
249 //--------------------------------------------
250 bool takeNumbers = true; // initially we trust the numbers from file
251 MED::TEntityInfo aEntityInfo = aMed.GetEntityInfo(aMeshInfo);
252 MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
253 for(; anEntityIter != aEntityInfo.end(); anEntityIter++){
254 const med_entite_maillage& anEntity = anEntityIter->first;
255 if(anEntity == MED_NOEUD) continue;
256 // Reading MED cells to the corresponding SMDS structure
257 //------------------------------------------------------
258 const MED::TGeom& aTGeom = anEntityIter->second;
259 MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
260 for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
261 const med_geometrie_element& aGeom = anTGeomIter->first;
262 if(aGeom == MED_POINT1) continue;
263 PCellInfo aCellInfo = aMed.GetCellInfo(aMeshInfo,anEntity,aGeom);
264 med_booleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : MED_FAUX;
265 med_int aNbElems = aCellInfo->GetNbElem();
266 MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
267 MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
269 for(int iElem = 0; iElem < aNbElems; iElem++){
270 med_int aNbNodes = -1;
302 vector<med_int> aNodeIds(aNbNodes);
303 #ifdef _EDF_NODE_IDS_
305 for(int i = 0; i < aNbNodes; i++){
306 aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
309 for(int i = 0; i < aNbNodes; i++){
310 aNodeIds[i] = aCellInfo->GetConn(iElem,i);
314 for(int i = 0; i < aNbNodes; i++){
315 aNodeIds[i] = aCellInfo->GetConn(iElem,i);
319 bool isRenum = false;
320 SMDS_MeshElement* anElement = NULL;
321 med_int aFamNum = aCellInfo->GetFamNum(iElem);
327 anElement = myMesh->AddEdgeWithID(aNodeIds[0],
329 aCellInfo->GetElemNum(iElem));
331 anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
332 FindNode(myMesh,aNodeIds[1]));
333 isRenum = anIsElemNum;
340 anElement = myMesh->AddFaceWithID(aNodeIds[0],
343 aCellInfo->GetElemNum(iElem));
345 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
346 FindNode(myMesh,aNodeIds[1]),
347 FindNode(myMesh,aNodeIds[2]));
348 isRenum = anIsElemNum;
354 // There is some differnce between SMDS and MED
356 anElement = myMesh->AddFaceWithID(aNodeIds[0],
360 aCellInfo->GetElemNum(iElem));
362 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
363 FindNode(myMesh,aNodeIds[1]),
364 FindNode(myMesh,aNodeIds[2]),
365 FindNode(myMesh,aNodeIds[3]));
366 isRenum = anIsElemNum;
373 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
377 aCellInfo->GetElemNum(iElem));
379 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
380 FindNode(myMesh,aNodeIds[1]),
381 FindNode(myMesh,aNodeIds[2]),
382 FindNode(myMesh,aNodeIds[3]));
383 isRenum = anIsElemNum;
389 // There is some differnce between SMDS and MED
391 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
396 aCellInfo->GetElemNum(iElem));
398 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
399 FindNode(myMesh,aNodeIds[1]),
400 FindNode(myMesh,aNodeIds[2]),
401 FindNode(myMesh,aNodeIds[3]),
402 FindNode(myMesh,aNodeIds[4]));
403 isRenum = anIsElemNum;
410 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
416 aCellInfo->GetElemNum(iElem));
418 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
419 FindNode(myMesh,aNodeIds[1]),
420 FindNode(myMesh,aNodeIds[2]),
421 FindNode(myMesh,aNodeIds[3]),
422 FindNode(myMesh,aNodeIds[4]),
423 FindNode(myMesh,aNodeIds[5]));
424 isRenum = anIsElemNum;
431 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
439 aCellInfo->GetElemNum(iElem));
441 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
442 FindNode(myMesh,aNodeIds[1]),
443 FindNode(myMesh,aNodeIds[2]),
444 FindNode(myMesh,aNodeIds[3]),
445 FindNode(myMesh,aNodeIds[4]),
446 FindNode(myMesh,aNodeIds[5]),
447 FindNode(myMesh,aNodeIds[6]),
448 FindNode(myMesh,aNodeIds[7]));
449 isRenum = anIsElemNum;
453 }catch(const std::exception& exc){
454 //INFOS("Follow exception was cought:\n\t"<<exc.what());
457 //INFOS("Unknown exception was cought !!!");
462 aResult = DRS_WARN_SKIP_ELEM;
466 anIsElemNum = MED_FAUX;
468 if (aResult < DRS_WARN_RENUMBER)
469 aResult = DRS_WARN_RENUMBER;
471 if (myFamilies.find(aFamNum) != myFamilies.end()) {
472 // Save reference to this element from its family
473 myFamilies[aFamNum]->AddElement(anElement);
474 myFamilies[aFamNum]->SetType(anElement->GetType());
483 }catch(const std::exception& exc){
484 INFOS("Follow exception was cought:\n\t"<<exc.what());
487 INFOS("Unknown exception was cought !!!");
490 MESSAGE("Perform - aResult status = "<<aResult);
494 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
496 list<string> aMeshNames;
499 using namespace MEDA;
501 MESSAGE("GetMeshNames - myFile : " << myFile);
503 TWrapper aMed (myFile);
505 if (med_int aNbMeshes = aMed.GetNbMeshes()) {
506 for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
507 // Reading the MED mesh
508 //---------------------
509 PMeshInfo aMeshInfo = aMed.GetMeshInfo(iMesh);
510 aMeshNames.push_back(aMeshInfo->GetName());
513 }catch(const std::exception& exc){
514 INFOS("Follow exception was cought:\n\t"<<exc.what());
515 theStatus = DRS_FAIL;
517 INFOS("Unknown exception was cought !!!");
518 theStatus = DRS_FAIL;
524 list<string> DriverMED_R_SMESHDS_Mesh::GetGroupNames()
526 list<string> aResult;
527 set<string> aResGroupNames;
529 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
530 for (; aFamsIter != myFamilies.end(); aFamsIter++)
532 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
533 const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
534 set<string>::iterator aGrNamesIter = aGroupNames.begin();
535 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
537 string aName = *aGrNamesIter;
538 // Check, if this is a Group or SubMesh name
539 //if (aName.substr(0, 5) == string("Group")) {
540 if (aResGroupNames.find(aName) == aResGroupNames.end()) {
541 aResGroupNames.insert(aName);
542 aResult.push_back(aName);
551 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
553 string aGroupName (theGroup->GetStoreName());
554 MESSAGE("Get Group " << aGroupName);
556 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
557 for (; aFamsIter != myFamilies.end(); aFamsIter++)
559 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
560 if (aFamily->MemberOf(aGroupName))
562 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
563 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
564 const SMDS_MeshElement * element = 0;
565 for (; anElemsIter != anElements.end(); anElemsIter++)
567 element = *anElemsIter;
568 theGroup->SMDSGroup().Add(element);
571 theGroup->SetType( element->GetType() );
576 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
579 char submeshGrpName[ 30 ];
580 sprintf( submeshGrpName, "SubMesh %d", theId );
581 string aName (submeshGrpName);
582 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
583 for (; aFamsIter != myFamilies.end(); aFamsIter++)
585 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
586 if (aFamily->MemberOf(aName))
588 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
589 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
590 if (aFamily->GetType() == SMDSAbs_Node)
592 for (; anElemsIter != anElements.end(); anElemsIter++)
594 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
595 theSubMesh->AddNode(node);
600 for (; anElemsIter != anElements.end(); anElemsIter++)
602 theSubMesh->AddElement(*anElemsIter);
609 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
611 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
612 for (; aFamsIter != myFamilies.end(); aFamsIter++)
614 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
615 MED::TStringSet aGroupNames = aFamily->GetGroupNames();
616 set<string>::iterator aGrNamesIter = aGroupNames.begin();
617 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
619 string aName = *aGrNamesIter;
620 // Check, if this is a Group or SubMesh name
621 if (aName.substr(0, 7) == string("SubMesh"))
623 int Id = atoi(string(aName).substr(7).c_str());
624 set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
625 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
626 if (aFamily->GetType() == SMDSAbs_Node)
628 for (; anElemsIter != anElements.end(); anElemsIter++)
630 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
631 ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
632 // find out a shape type
633 TopoDS_Shape aShape = myMesh->IndexToShape( Id );
634 int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
635 switch ( aShapeType ) {
637 myMesh->SetNodeOnFace(node, Id); break;
639 myMesh->SetNodeOnEdge(node, Id); break;
641 myMesh->SetNodeOnVertex(node, Id); break;
643 myMesh->SetNodeInVolume(node, Id);
649 for (; anElemsIter != anElements.end(); anElemsIter++)
651 myMesh->SetMeshElementOnShape(*anElemsIter, Id);