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 aFamily->SetId( aFamId );
188 myFamilies[aFamId] = aFamily;
192 // Reading MED nodes to the corresponding SMDS structure
193 //------------------------------------------------------
194 PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
196 TCoordHelperPtr aCoordHelperPtr;
198 TInt aMeshDimension = aMeshInfo->GetDim();
199 bool anIsDimPresent[3] = {false, false, false};
200 for(TInt iDim = 0; iDim < aMeshDimension; iDim++){
201 string aDimName = aNodeInfo->GetCoordName(iDim);
202 if(aDimName == "x" || aDimName == "X")
203 anIsDimPresent[eX] = true;
204 else if(aDimName == "y" || aDimName == "Y")
205 anIsDimPresent[eY] = true;
206 else if(aDimName == "z" || aDimName == "Z")
207 anIsDimPresent[eZ] = true;
209 switch(aMeshDimension){
211 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYZGetCoord));
214 if(anIsDimPresent[eY] && anIsDimPresent[eZ])
215 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYZGetCoord));
216 else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
217 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXZGetCoord));
219 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYGetCoord));
222 if(anIsDimPresent[eY])
223 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYGetCoord));
224 else if(anIsDimPresent[eZ])
225 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aZGetCoord));
227 aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXGetCoord));
232 EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
233 TInt aNbElems = aNodeInfo->GetNbElem();
234 if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
235 DriverMED_FamilyPtr aFamily;
236 for(TInt iElem = 0; iElem < aNbElems; iElem++){
237 double aCoords[3] = {0.0, 0.0, 0.0};
238 for(TInt iDim = 0; iDim < 3; iDim++)
239 aCoords[iDim] = aCoordHelperPtr->GetCoord(iElem,iDim);
240 const SMDS_MeshNode* aNode;
242 aNode = myMesh->AddNodeWithID
243 (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
245 aNode = myMesh->AddNode
246 (aCoords[0],aCoords[1],aCoords[2]);
248 //cout<<aNode->GetID()<<": "<<aNode->X()<<", "<<aNode->Y()<<", "<<aNode->Z()<<endl;
250 // Save reference to this node from its family
251 TInt aFamNum = aNodeInfo->GetFamNum(iElem);
252 if ( checkFamilyID ( aFamily, aFamNum ))
254 aFamily->AddElement(aNode);
255 aFamily->SetType(SMDSAbs_Node);
259 // Reading pre information about all MED cells
260 //--------------------------------------------
261 bool takeNumbers = true; // initially we trust the numbers from file
262 MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
263 MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
264 for(; anEntityIter != aEntityInfo.end(); anEntityIter++){
265 const EEntiteMaillage& anEntity = anEntityIter->first;
266 if(anEntity == eNOEUD) continue;
267 // Reading MED cells to the corresponding SMDS structure
268 //------------------------------------------------------
269 const MED::TGeom& aTGeom = anEntityIter->second;
270 MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
271 for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
272 const EGeometrieElement& aGeom = anTGeomIter->first;
273 if(aGeom == ePOINT1) continue;
274 PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
275 EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
276 TInt aNbElems = aCellInfo->GetNbElem();
277 if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
278 if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
280 for(int iElem = 0; iElem < aNbElems; iElem++){
313 vector<TInt> aNodeIds(aNbNodes);
314 bool anIsValidConnect = false;
317 #ifdef _EDF_NODE_IDS_
319 for(int i = 0; i < aNbNodes; i++){
320 aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
323 for(int i = 0; i < aNbNodes; i++){
324 aNodeIds[i] = aCellInfo->GetConn(iElem,i);
328 for(int i = 0; i < aNbNodes; i++){
329 aNodeIds[i] = aCellInfo->GetConn(iElem,i);
332 anIsValidConnect = true;
333 }catch(const std::exception& exc){
334 //INFOS("Follow exception was cought:\n\t"<<exc.what());
337 //INFOS("Unknown exception was cought !!!");
341 if(!anIsValidConnect)
344 bool isRenum = false;
345 SMDS_MeshElement* anElement = NULL;
346 TInt aFamNum = aCellInfo->GetFamNum(iElem);
348 //MESSAGE("Try to create element # " << iElem << " with id = "
349 // << aCellInfo->GetElemNum(iElem));
354 anElement = myMesh->AddEdgeWithID(aNodeIds[0],
356 aCellInfo->GetElemNum(iElem));
358 anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
359 FindNode(myMesh,aNodeIds[1]));
360 isRenum = anIsElemNum;
367 anElement = myMesh->AddFaceWithID(aNodeIds[0],
370 aCellInfo->GetElemNum(iElem));
372 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
373 FindNode(myMesh,aNodeIds[1]),
374 FindNode(myMesh,aNodeIds[2]));
375 isRenum = anIsElemNum;
381 // There is some differnce between SMDS and MED
383 anElement = myMesh->AddFaceWithID(aNodeIds[0],
387 aCellInfo->GetElemNum(iElem));
389 anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
390 FindNode(myMesh,aNodeIds[1]),
391 FindNode(myMesh,aNodeIds[2]),
392 FindNode(myMesh,aNodeIds[3]));
393 isRenum = anIsElemNum;
400 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
404 aCellInfo->GetElemNum(iElem));
406 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
407 FindNode(myMesh,aNodeIds[1]),
408 FindNode(myMesh,aNodeIds[2]),
409 FindNode(myMesh,aNodeIds[3]));
410 isRenum = anIsElemNum;
416 // There is some differnce between SMDS and MED
418 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
423 aCellInfo->GetElemNum(iElem));
425 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
426 FindNode(myMesh,aNodeIds[1]),
427 FindNode(myMesh,aNodeIds[2]),
428 FindNode(myMesh,aNodeIds[3]),
429 FindNode(myMesh,aNodeIds[4]));
430 isRenum = anIsElemNum;
437 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
443 aCellInfo->GetElemNum(iElem));
445 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
446 FindNode(myMesh,aNodeIds[1]),
447 FindNode(myMesh,aNodeIds[2]),
448 FindNode(myMesh,aNodeIds[3]),
449 FindNode(myMesh,aNodeIds[4]),
450 FindNode(myMesh,aNodeIds[5]));
451 isRenum = anIsElemNum;
458 anElement = myMesh->AddVolumeWithID(aNodeIds[0],
466 aCellInfo->GetElemNum(iElem));
468 anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
469 FindNode(myMesh,aNodeIds[1]),
470 FindNode(myMesh,aNodeIds[2]),
471 FindNode(myMesh,aNodeIds[3]),
472 FindNode(myMesh,aNodeIds[4]),
473 FindNode(myMesh,aNodeIds[5]),
474 FindNode(myMesh,aNodeIds[6]),
475 FindNode(myMesh,aNodeIds[7]));
476 isRenum = anIsElemNum;
480 }catch(const std::exception& exc){
481 //INFOS("Follow exception was cought:\n\t"<<exc.what());
484 //INFOS("Unknown exception was cought !!!");
489 aResult = DRS_WARN_SKIP_ELEM;
495 if (aResult < DRS_WARN_RENUMBER)
496 aResult = DRS_WARN_RENUMBER;
498 if ( checkFamilyID ( aFamily, aFamNum ))
500 // Save reference to this element from its family
501 aFamily->AddElement(anElement);
502 aFamily->SetType(anElement->GetType());
511 }catch(const std::exception& exc){
512 INFOS("Follow exception was cought:\n\t"<<exc.what());
515 INFOS("Unknown exception was cought !!!");
518 if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
522 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
524 list<string> aMeshNames;
527 if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
529 PWrapper aMed = CrWrapper(myFile);
531 if (TInt aNbMeshes = aMed->GetNbMeshes()) {
532 for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
533 // Reading the MED mesh
534 //---------------------
535 PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
536 aMeshNames.push_back(aMeshInfo->GetName());
539 }catch(const std::exception& exc){
540 INFOS("Follow exception was cought:\n\t"<<exc.what());
541 theStatus = DRS_FAIL;
543 INFOS("Unknown exception was cought !!!");
544 theStatus = DRS_FAIL;
550 list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
552 list<TNameAndType> aResult;
553 set<TNameAndType> aResGroupNames;
555 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
556 for (; aFamsIter != myFamilies.end(); aFamsIter++)
558 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
559 const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
560 set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
561 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
563 TNameAndType aNameAndType = make_pair( *aGrNamesIter, aFamily->GetType() );
564 // Check, if this is a Group or SubMesh name
565 //if (aName.substr(0, 5) == string("Group")) {
566 if ( aResGroupNames.insert( aNameAndType ).second ) {
567 aResult.push_back( aNameAndType );
576 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
578 string aGroupName (theGroup->GetStoreName());
579 if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
581 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
582 for (; aFamsIter != myFamilies.end(); aFamsIter++)
584 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
585 if (aFamily->GetType() == theGroup->GetType() && aFamily->MemberOf(aGroupName))
587 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
588 set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
589 const SMDS_MeshElement * element = 0;
590 for (; anElemsIter != anElements.end(); anElemsIter++)
592 element = *anElemsIter;
593 theGroup->SMDSGroup().Add(element);
596 theGroup->SetType( element->GetType() );
601 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
604 char submeshGrpName[ 30 ];
605 sprintf( submeshGrpName, "SubMesh %d", theId );
606 string aName (submeshGrpName);
607 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
608 for (; aFamsIter != myFamilies.end(); aFamsIter++)
610 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
611 if (aFamily->MemberOf(aName))
613 const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
614 set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
615 if (aFamily->GetType() == SMDSAbs_Node)
617 for (; anElemsIter != anElements.end(); anElemsIter++)
619 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
620 theSubMesh->AddNode(node);
625 for (; anElemsIter != anElements.end(); anElemsIter++)
627 theSubMesh->AddElement(*anElemsIter);
634 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
636 map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
637 for (; aFamsIter != myFamilies.end(); aFamsIter++)
639 DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
640 MED::TStringSet aGroupNames = aFamily->GetGroupNames();
641 set<string>::iterator aGrNamesIter = aGroupNames.begin();
642 for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
644 string aName = *aGrNamesIter;
645 // Check, if this is a Group or SubMesh name
646 if (aName.substr(0, 7) == string("SubMesh"))
648 int Id = atoi(string(aName).substr(7).c_str());
649 set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
650 set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
651 if (aFamily->GetType() == SMDSAbs_Node)
653 for (; anElemsIter != anElements.end(); anElemsIter++)
655 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
656 ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
657 // find out a shape type
658 TopoDS_Shape aShape = myMesh->IndexToShape( Id );
659 int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
660 switch ( aShapeType ) {
662 myMesh->SetNodeOnFace(node, Id); break;
664 myMesh->SetNodeOnEdge(node, Id); break;
666 myMesh->SetNodeOnVertex(node, Id); break;
668 myMesh->SetNodeInVolume(node, Id);
674 for (; anElemsIter != anElements.end(); anElemsIter++)
676 myMesh->SetMeshElementOnShape(*anElemsIter, Id);
684 * \brief Ensure aFamily to have required ID
685 * \param aFamily - a family to check and update
686 * \param anID - an ID aFamily should have
687 * \retval bool - true if successful
689 bool DriverMED_R_SMESHDS_Mesh::checkFamilyID(DriverMED_FamilyPtr & aFamily, int anID) const
691 if ( !aFamily || aFamily->GetId() != anID ) {
692 map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
693 if ( i_fam == myFamilies.end() )
695 aFamily = i_fam->second;
697 return ( aFamily->GetId() == anID );