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_W_SMESHDS_Mesh.cxx
28 #include "DriverMED_W_SMESHDS_Mesh.h"
29 #include "DriverMED_W_SMDS_Mesh.h"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMDS_MeshFacesIterator.hxx"
34 #include "SMDS_MeshEdgesIterator.hxx"
35 #include "SMDS_MeshNodesIterator.hxx"
36 #include "SMDS_MeshVolumesIterator.hxx"
37 #include "SMESHDS_SubMesh.hxx"
38 #include "SMDS_MapIteratorOfExtendedMap.hxx"
39 #include "SMDS_MapOfMeshElement.hxx"
40 #include "SMDS_Position.hxx"
43 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Iterator.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_CompSolid.hxx>
48 #include <TopoDS_Solid.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Wire.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopTools_MapOfShape.hxx>
56 #include <TColStd_ListIteratorOfListOfInteger.hxx>
62 #include "utilities.h"
64 DriverMED_W_SMESHDS_Mesh::DriverMED_W_SMESHDS_Mesh() {
68 DriverMED_W_SMESHDS_Mesh::~DriverMED_W_SMESHDS_Mesh() {
72 void DriverMED_W_SMESHDS_Mesh::SetMesh(Handle(SMDS_Mesh)& aMesh) {
76 void DriverMED_W_SMESHDS_Mesh::SetFile(string aFile) {
80 void DriverMED_W_SMESHDS_Mesh::SetFileId(med_idt aFileId) {
84 void DriverMED_W_SMESHDS_Mesh::SetMeshId(int aMeshId) {
88 void DriverMED_W_SMESHDS_Mesh::Write() {
90 string myClass = string("SMDS_Mesh");
91 string myExtension = string("MED");
93 DriverMED_W_SMDS_Mesh* myWriter = new DriverMED_W_SMDS_Mesh;
95 myWriter->SetMesh(myMesh);
96 // myWriter->SetFile(myFile);
97 myWriter->SetMeshId(myMeshId);
98 myWriter->SetFileId(myFileId);
105 void DriverMED_W_SMESHDS_Mesh::Add() {
112 /* nombre d'objets MED */
113 char nom_universel[MED_TAILLE_LNOM+1];
114 med_int long_fichier_en_tete;
115 char *fichier_en_tete;
116 char version_hdf[10];
117 char version_med[10];
118 med_int nmaa,mdim,nnoe;
119 med_int nmai[MED_NBR_GEOMETRIE_MAILLE],nfac[MED_NBR_GEOMETRIE_FACE];
120 med_int nare[MED_NBR_GEOMETRIE_ARETE];
121 /* nom du maillage */
122 char nommaa[MED_TAILLE_NOM+1];
125 // PN : Initilialisation de nomcoo et unicoo pour lisibilite du maillage
126 char nomcoo[3*MED_TAILLE_PNOM+1]="x y z ";
127 char unicoo[3*MED_TAILLE_PNOM+1]="m m m ";
132 med_booleen inonoe,inunoe;
133 med_mode_switch mode_coo;
134 char str[MED_TAILLE_PNOM+1];
140 med_int elem_id,myId;
141 med_int *connectivite;
145 med_booleen inoele, inuele;
146 med_connectivite typ_con;
147 med_geometrie_element typgeo;
148 med_geometrie_element typmai[MED_NBR_GEOMETRIE_MAILLE] = {MED_POINT1,MED_SEG2,
151 MED_QUAD8,MED_TETRA4,
152 MED_TETRA10,MED_HEXA8,
153 MED_HEXA20,MED_PENTA6,
154 MED_PENTA15,MED_PYRA5,
156 med_int desmai[MED_NBR_GEOMETRIE_MAILLE] = {0,2,3,3,3,4,4,4,4,6,6,5,5,5,5};
157 med_int nmailles[MED_NBR_GEOMETRIE_MAILLE];
158 char nommai[MED_NBR_GEOMETRIE_MAILLE] [MED_TAILLE_NOM+1] = {"MED_POINT1",
173 med_geometrie_element typfac[MED_NBR_GEOMETRIE_FACE] = {MED_TRIA3,MED_TRIA6,
174 MED_QUAD4,MED_QUAD8};
175 med_int desfac[MED_NBR_GEOMETRIE_FACE] = {3,3,4,4};
176 med_int nfaces[MED_NBR_GEOMETRIE_FACE];
177 char nomfac[MED_NBR_GEOMETRIE_FACE][MED_TAILLE_NOM+1] = {"MED_TRIA3","MED_TRIA6",
178 "MED_QUAD4","MED_QUAD8"};
179 med_geometrie_element typare[MED_NBR_GEOMETRIE_ARETE] = {MED_SEG2,MED_SEG3};
180 med_int desare[MED_NBR_GEOMETRIE_ARETE] = {2,3};
181 med_int naretes[MED_NBR_GEOMETRIE_ARETE];
182 char nomare[MED_NBR_GEOMETRIE_ARETE] [MED_TAILLE_NOM+1] = {"MED_SEG2","MED_SEG3"};
185 mode_coo = MED_FULL_INTERLACE;
188 //---- provisoire : switch pour ecrire les familles de mailles
189 int besoinfamilledemaille=1;
190 //---- provisoire : switch pour ecrire les familles de mailles
192 /****************************************************************************
193 * OUVERTURE DU FICHIER EN ECRITURE *
194 ****************************************************************************/
195 char* file2Read = (char*)myFile.c_str();
197 MESSAGE ( " file2Read " << file2Read )
199 myFileId = MEDouvrir(file2Read,MED_REMP);
202 fprintf(stderr,">> ERREUR : ouverture du fichier %s \n",file2Read);
206 /****************************************************************************
207 * NOMBRES D'OBJETS MED *
208 ****************************************************************************/
209 MESSAGE("(****************************)");
210 MESSAGE("(* INFORMATIONS GENERALES : *)");
211 MESSAGE("(****************************)");
213 /* calcul de la dimension */
215 double epsilon=0.00001;
224 SMDS_MeshNodesIterator myItNodes(myMesh);
226 for (;myItNodes.More();myItNodes.Next()) {
227 const Handle(SMDS_MeshElement)& elem = myItNodes.Value();
228 const Handle(SMDS_MeshNode)& node = myMesh->GetNode(1,elem);
230 nodeRefX = fabs(node->X());
231 nodeRefY = fabs(node->Y());
232 nodeRefZ = fabs(node->Z());
240 if ( (fabs(fabs(node->X()) - nodeRefX) > epsilon ) && dimX )
242 if ( (fabs(fabs(node->Y()) - nodeRefY) > epsilon ) && dimY )
244 if ( (fabs(fabs(node->Z()) - nodeRefZ) > epsilon ) && dimZ )
247 if ( !dimX && !dimY && !dimZ ) {
255 if ( dimX && dimY && dimZ )
260 else if (( dimY && !dimZ ) || ( !dimY && dimZ ) )
262 } else if ( !dimY ) {
265 else if (( dimX && !dimZ ) || ( !dimX && dimZ ) )
267 } else if ( !dimZ ) {
270 else if (( dimY && !dimX ) || ( !dimY && dimX ) )
275 MESSAGE ( " mdim " << mdim );
277 /* creation du maillage */
279 sprintf(nommaa,"Mesh %d",numero);
281 ret = MEDmaaCr(myFileId,nommaa,mdim);
286 /* Combien de noeuds ? */
287 nnoe = myMesh->NbNodes();
289 /* Combien de mailles, faces ou aretes ? */
290 for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
293 Standard_Integer nb_of_nodes, nb_of_faces, nb_of_edges;
294 vector<int> elem_Id[MED_NBR_GEOMETRIE_MAILLE];
296 SMDS_MeshEdgesIterator itEdges(myMesh);
297 nb_of_edges = myMesh->NbEdges();
298 for (;itEdges.More();itEdges.Next()) {
299 const Handle(SMDS_MeshElement)& elem = itEdges.Value();
301 nb_of_nodes = elem->NbNodes();
303 switch (nb_of_nodes) {
305 elem_Id[1].push_back(elem->GetID());
310 elem_Id[2].push_back(elem->GetID());
318 SMDS_MeshFacesIterator itFaces(myMesh);
319 nb_of_faces = myMesh->NbFaces();
320 for (;itFaces.More();itFaces.Next()) {
321 const Handle(SMDS_MeshElement)& elem = itFaces.Value();
323 nb_of_nodes = elem->NbNodes();
325 switch (nb_of_nodes) {
327 elem_Id[3].push_back(elem->GetID());
332 elem_Id[5].push_back(elem->GetID());
337 elem_Id[4].push_back(elem->GetID());
345 SMDS_MeshVolumesIterator itVolumes(myMesh);
346 for (;itVolumes.More();itVolumes.Next()) {
347 const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
349 nb_of_nodes = elem->NbNodes();
350 switch (nb_of_nodes) {
352 elem_Id[9].push_back(elem->GetID());
361 /****************************************************************************
362 * ECRITURE DES NOEUDS *
363 ****************************************************************************/
364 MESSAGE("(************************)");
365 MESSAGE("(* NOEUDS DU MAILLAGE : *)");
366 MESSAGE("(************************)");
368 /* Allocations memoires */
369 /* table des coordonnees
370 profil : (dimension * nombre de noeuds ) */
371 coo = (med_float*) malloc(sizeof(med_float)*nnoe*mdim);
372 /* table des numeros, des numeros de familles des noeuds
373 profil : (nombre de noeuds) */
374 numnoe = (med_int*) malloc(sizeof(med_int)*nnoe);
375 nufano = (med_int*) malloc(sizeof(med_int)*nnoe);
376 /* table des noms des noeuds
377 profil : (nnoe*MED_TAILLE_PNOM+1) */
380 /* PN pour aster, il faut une famille 0 pour les noeuds et une autre pour les elements*/
381 /* PN : Creation de la famille 0 */
382 char * nomfam="FAMILLE_0";
391 ret = MEDfamCr(myFileId,nommaa,nomfam,numfam, &attide,&attval,attdes,natt,gro,ngro);
395 /* PN : FIN Creation de la famille 0 */
397 map < int, int > mapNoeud;
398 typedef pair<set<int>::iterator, bool> IsFamily;
405 int verifienbnoeuds = 0;
408 SMDS_MeshNodesIterator itNodes(myMesh);
409 for (;itNodes.More();itNodes.Next())
411 const Handle(SMDS_MeshElement)& elem = itNodes.Value();
412 const Handle(SMDS_MeshNode)& node = myMesh->GetNode(1,elem);
416 coo[i*3+1]=node->Y();
417 coo[i*3+2]=node->Z();
421 coo[i*2+1]=node->Z();
425 coo[i*2+1]=node->Z();
429 coo[i*2+1]=node->Y();
434 coo[i*2+1]=node->Z();
438 coo[i*2+1]=node->Z();
442 coo[i*2+1]=node->Y();
445 mapNoeud[node->GetID()] = i+1;
447 // renvoie 0 pour les noeuds internes du volume
448 int numfamille=node->GetPosition()->GetShapeId();
449 nufano[i]=numfamille;
454 //SCRUTE(coo[i*3+1]);
455 //SCRUTE(coo[i*3+2]);
456 if ( nufano[i] != 0 )
458 IsFamily deja = FamilySet.insert(nufano[i]); // insert if new, or gives existant
459 if (deja.second) // actually inserted
461 char famille[MED_TAILLE_NOM+1];
462 sprintf(famille,"F%d",nufano[i]);
463 // CreateFamily(strdup(nommaa),strdup(famille),nufano[i],attvalabs++);
465 CreateFamily(strdup(nommaa),strdup(famille),nufano[i],numfamille);
466 //MESSAGE("---famille-noeud--- "<<nbFamillesNoeud<<" "<<nufano[i]);
474 ret = MEDnoeudsEcr(myFileId,nommaa,mdim,coo,mode_coo,MED_CART,
475 nomcoo,unicoo,nomnoe,MED_FAUX,rien,MED_FAUX,
476 nufano,nnoe,MED_REMP);
478 MESSAGE("--- Creation de " << verifienbnoeuds << " noeuds");
479 ASSERT(verifienbnoeuds == nnoe);
480 MESSAGE("--- Creation de " << nbFamillesNoeud << " familles de noeuds");
482 /* liberation memoire */
487 /****************************************************************************
488 * ECRITURE DES ELEMENTS *
489 ****************************************************************************/
490 MESSAGE("(**************************)");
491 MESSAGE("(* ELEMENTS DU MAILLAGE : *)");
492 MESSAGE("(**************************)");
494 /* Ecriture des connectivites, noms, numeros des mailles */
498 int nbFamillesElts =0;
499 Handle(SMESHDS_Mesh) mySMESHDSMesh = Handle(SMESHDS_Mesh)::DownCast(myMesh);
500 TopTools_IndexedMapOfShape myIndexToShape;
501 TopExp::MapShapes(mySMESHDSMesh->ShapeToMesh(),myIndexToShape);
503 map < int, int > mapFamille;
505 if (besoinfamilledemaille == 1)
508 for (t =1; t <= myIndexToShape.Extent(); t++)
510 const TopoDS_Shape S = myIndexToShape(t);
511 if (mySMESHDSMesh->HasMeshElements(S))
513 //MESSAGE ("********* Traitement de la Famille "<<-t);
515 Handle(SMESHDS_SubMesh) SM = mySMESHDSMesh->MeshElements(S);
516 const TColStd_ListOfInteger& indElt = SM->GetIDElements();
517 TColStd_ListIteratorOfListOfInteger ite(indElt);
520 for (; ite.More(); ite.Next())
522 int eltId = ite.Value();
523 mapFamille [eltId] = - t;
529 char famille[MED_TAILLE_NOM+1];
530 sprintf(famille,"E%d",t);
531 CreateFamily(strdup(nommaa),strdup(famille),-t,attvalabs++);
538 for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
540 if (nmailles[i] > 0 && ret == 0)
542 MESSAGE ( " Start "<<typmai[i]);
544 /* dimension de la maille */
545 edim = typmai[i] / 100;
547 if (mdim == 2 || mdim == 3)
548 if (edim == 1) nsup = 1;
550 if (edim == 2) nsup = 1;
553 taille = nsup+typmai[i]%100;
556 /* allocation memoire */
557 connectivite = (med_int*)malloc(sizeof(med_int)* taille*nmailles[i]);
558 nomele = (char*)malloc(sizeof(char)*MED_TAILLE_PNOM* nmailles[i]+1);
559 numele = (med_int*)malloc(sizeof(med_int)* nmailles[i]);
560 nufael = (med_int*)malloc(sizeof(med_int)* nmailles[i]);
562 nbNodes=typmai[i]%100;
564 for (j=0;j<nmailles[i];j++)
566 myId = elem_Id[i][j];
567 const Handle(SMDS_MeshElement)& elem = myMesh->FindElement(myId);
568 //*(numele+j) = myId;
569 *(numele+j) = indice ++;
571 for (k=0; k<nbNodes; k++)
573 *(connectivite+j*taille+k) = mapNoeud[elem->GetConnection(k+1)];
574 //SCRUTE(*(connectivite+j*taille+k));
576 if (nsup) *(connectivite+j*taille + nbNodes) = 0;
578 if (besoinfamilledemaille == 1)
580 if (mapFamille.find(myId)!=mapFamille.end())
582 nufael[j]=mapFamille[myId];
599 /* ecriture des données */
602 ret=MEDelementsEcr( myFileId,nommaa,mdim,connectivite,mode_coo,nomele,MED_FAUX,numele,
603 MED_VRAI,nufael,nmailles[i],MED_MAILLE,typmai[i],typ_con,MED_REMP);
607 if (ret < 0) MESSAGE(">> ERREUR : ecriture des mailles \n");
609 /* liberation memoire */
613 MESSAGE ( " End "<<typmai[i]);
616 MESSAGE("--- Creation de " << nbFamillesElts << " familles d elements");
620 /****************************************************************************
621 * FERMETURE DU FICHIER *
622 ****************************************************************************/
624 ret = MEDfermer(myFileId);
627 fprintf(stderr,">> ERREUR : erreur a la fermeture du fichier %s\n",file2Read);
628 MESSAGE ("fichier ferme");
633 void DriverMED_W_SMESHDS_Mesh::CreateFamily(char* nommaa, char* famille, int i,med_int k) {
639 char attdes[MED_TAILLE_DESC+1];
640 char gro[MED_TAILLE_LNOM+1];
641 char fam2[MED_TAILLE_LNOM+1];
645 strcpy(fam2,famille);
647 med_int * attide=new med_int[1];
648 med_int * attval=new med_int[1];
653 //MESSAGE("-------- Creation de la Famille : "<< famille << "numero " << i << " --------------");
654 med_int ret = MEDfamCr(myFileId, nommaa, fam2, i, attide, attval, attdes, natt, gro, ngro);