2 #include "DriverMED_W_SMESHDS_Mesh.h"
3 #include "DriverMED_W_SMDS_Mesh.h"
5 #include "SMDS_MeshElement.hxx"
6 #include "SMDS_MeshNode.hxx"
7 #include "SMDS_MeshFacesIterator.hxx"
8 #include "SMDS_MeshEdgesIterator.hxx"
9 #include "SMDS_MeshNodesIterator.hxx"
10 #include "SMDS_MeshVolumesIterator.hxx"
11 #include "SMESHDS_SubMesh.hxx"
12 #include "SMDS_MapIteratorOfExtendedMap.hxx"
13 #include "SMDS_MapOfMeshElement.hxx"
14 #include "SMDS_Position.hxx"
17 #include <TopExp_Explorer.hxx>
19 #include <TopoDS_Iterator.hxx>
20 #include <TopoDS_Compound.hxx>
21 #include <TopoDS_CompSolid.hxx>
22 #include <TopoDS_Solid.hxx>
23 #include <TopoDS_Shell.hxx>
24 #include <TopoDS_Face.hxx>
25 #include <TopoDS_Wire.hxx>
26 #include <TopoDS_Edge.hxx>
27 #include <TopoDS_Vertex.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_MapOfShape.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
36 #include "utilities.h"
38 DriverMED_W_SMESHDS_Mesh::DriverMED_W_SMESHDS_Mesh() {
42 DriverMED_W_SMESHDS_Mesh::~DriverMED_W_SMESHDS_Mesh() {
46 void DriverMED_W_SMESHDS_Mesh::SetMesh(Handle(SMDS_Mesh)& aMesh) {
50 void DriverMED_W_SMESHDS_Mesh::SetFile(string aFile) {
54 void DriverMED_W_SMESHDS_Mesh::SetFileId(med_idt aFileId) {
58 void DriverMED_W_SMESHDS_Mesh::SetMeshId(int aMeshId) {
62 void DriverMED_W_SMESHDS_Mesh::Write() {
64 string myClass = string("SMDS_Mesh");
65 string myExtension = string("MED");
67 DriverMED_W_SMDS_Mesh* myWriter = new DriverMED_W_SMDS_Mesh;
69 myWriter->SetMesh(myMesh);
70 // myWriter->SetFile(myFile);
71 myWriter->SetMeshId(myMeshId);
72 myWriter->SetFileId(myFileId);
79 void DriverMED_W_SMESHDS_Mesh::Add() {
86 /* nombre d'objets MED */
87 char nom_universel[MED_TAILLE_LNOM+1];
88 med_int long_fichier_en_tete;
89 char *fichier_en_tete;
92 med_int nmaa,mdim,nnoe;
93 med_int nmai[MED_NBR_GEOMETRIE_MAILLE],nfac[MED_NBR_GEOMETRIE_FACE];
94 med_int nare[MED_NBR_GEOMETRIE_ARETE];
96 char nommaa[MED_TAILLE_NOM+1];
99 // PN : Initilialisation de nomcoo et unicoo pour lisibilite du maillage
100 char nomcoo[3*MED_TAILLE_PNOM+1]="x y z ";
101 char unicoo[3*MED_TAILLE_PNOM+1]="m m m ";
106 med_booleen inonoe,inunoe;
107 med_mode_switch mode_coo;
108 char str[MED_TAILLE_PNOM+1];
114 med_int elem_id,myId;
115 med_int *connectivite;
119 med_booleen inoele, inuele;
120 med_connectivite typ_con;
121 med_geometrie_element typgeo;
122 med_geometrie_element typmai[MED_NBR_GEOMETRIE_MAILLE] = {MED_POINT1,MED_SEG2,
125 MED_QUAD8,MED_TETRA4,
126 MED_TETRA10,MED_HEXA8,
127 MED_HEXA20,MED_PENTA6,
128 MED_PENTA15,MED_PYRA5,
130 med_int desmai[MED_NBR_GEOMETRIE_MAILLE] = {0,2,3,3,3,4,4,4,4,6,6,5,5,5,5};
131 med_int nmailles[MED_NBR_GEOMETRIE_MAILLE];
132 char nommai[MED_NBR_GEOMETRIE_MAILLE] [MED_TAILLE_NOM+1] = {"MED_POINT1",
147 med_geometrie_element typfac[MED_NBR_GEOMETRIE_FACE] = {MED_TRIA3,MED_TRIA6,
148 MED_QUAD4,MED_QUAD8};
149 med_int desfac[MED_NBR_GEOMETRIE_FACE] = {3,3,4,4};
150 med_int nfaces[MED_NBR_GEOMETRIE_FACE];
151 char nomfac[MED_NBR_GEOMETRIE_FACE][MED_TAILLE_NOM+1] = {"MED_TRIA3","MED_TRIA6",
152 "MED_QUAD4","MED_QUAD8"};
153 med_geometrie_element typare[MED_NBR_GEOMETRIE_ARETE] = {MED_SEG2,MED_SEG3};
154 med_int desare[MED_NBR_GEOMETRIE_ARETE] = {2,3};
155 med_int naretes[MED_NBR_GEOMETRIE_ARETE];
156 char nomare[MED_NBR_GEOMETRIE_ARETE] [MED_TAILLE_NOM+1] = {"MED_SEG2","MED_SEG3"};
159 mode_coo = MED_FULL_INTERLACE;
162 //---- provisoire : switch pour ecrire les familles de mailles
163 int besoinfamilledemaille=1;
164 //---- provisoire : switch pour ecrire les familles de mailles
166 /****************************************************************************
167 * OUVERTURE DU FICHIER EN ECRITURE *
168 ****************************************************************************/
169 char* file2Read = (char*)myFile.c_str();
171 MESSAGE ( " file2Read " << file2Read )
173 myFileId = MEDouvrir(file2Read,MED_REMP);
176 fprintf(stderr,">> ERREUR : ouverture du fichier %s \n",file2Read);
180 /****************************************************************************
181 * NOMBRES D'OBJETS MED *
182 ****************************************************************************/
183 fprintf(stdout,"\n(****************************)\n");
184 fprintf(stdout,"(* INFORMATIONS GENERALES : *)\n");
185 fprintf(stdout,"(****************************)\n");
187 /* calcul de la dimension */
189 double epsilon=0.00001;
198 SMDS_MeshNodesIterator myItNodes(myMesh);
200 for (;myItNodes.More();myItNodes.Next()) {
201 const Handle(SMDS_MeshElement)& elem = myItNodes.Value();
202 const Handle(SMDS_MeshNode)& node = myMesh->GetNode(1,elem);
204 nodeRefX = fabs(node->X());
205 nodeRefY = fabs(node->Y());
206 nodeRefZ = fabs(node->Z());
214 if ( (fabs(fabs(node->X()) - nodeRefX) > epsilon ) && dimX )
216 if ( (fabs(fabs(node->Y()) - nodeRefY) > epsilon ) && dimY )
218 if ( (fabs(fabs(node->Z()) - nodeRefZ) > epsilon ) && dimZ )
221 if ( !dimX && !dimY && !dimZ ) {
229 if ( dimX && dimY && dimZ )
234 else if (( dimY && !dimZ ) || ( !dimY && dimZ ) )
236 } else if ( !dimY ) {
239 else if (( dimX && !dimZ ) || ( !dimX && dimZ ) )
241 } else if ( !dimZ ) {
244 else if (( dimY && !dimX ) || ( !dimY && dimX ) )
249 MESSAGE ( " mdim " << mdim );
251 /* creation du maillage */
253 sprintf(nommaa,"Mesh %d",numero);
255 ret = MEDmaaCr(myFileId,nommaa,mdim);
260 /* Combien de noeuds ? */
261 nnoe = myMesh->NbNodes();
263 /* Combien de mailles, faces ou aretes ? */
264 for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
267 Standard_Integer nb_of_nodes, nb_of_faces, nb_of_edges;
268 vector<int> elem_Id[MED_NBR_GEOMETRIE_MAILLE];
270 SMDS_MeshEdgesIterator itEdges(myMesh);
271 nb_of_edges = myMesh->NbEdges();
272 for (;itEdges.More();itEdges.Next()) {
273 const Handle(SMDS_MeshElement)& elem = itEdges.Value();
275 nb_of_nodes = elem->NbNodes();
277 switch (nb_of_nodes) {
279 elem_Id[1].push_back(elem->GetID());
284 elem_Id[2].push_back(elem->GetID());
292 SMDS_MeshFacesIterator itFaces(myMesh);
293 nb_of_faces = myMesh->NbFaces();
294 for (;itFaces.More();itFaces.Next()) {
295 const Handle(SMDS_MeshElement)& elem = itFaces.Value();
297 nb_of_nodes = elem->NbNodes();
299 switch (nb_of_nodes) {
301 elem_Id[3].push_back(elem->GetID());
306 elem_Id[5].push_back(elem->GetID());
311 elem_Id[4].push_back(elem->GetID());
319 SMDS_MeshVolumesIterator itVolumes(myMesh);
320 for (;itVolumes.More();itVolumes.Next()) {
321 const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
323 nb_of_nodes = elem->NbNodes();
324 switch (nb_of_nodes) {
326 elem_Id[9].push_back(elem->GetID());
335 /****************************************************************************
336 * ECRITURE DES NOEUDS *
337 ****************************************************************************/
338 fprintf(stdout,"\n(************************)\n");
339 fprintf(stdout,"(* NOEUDS DU MAILLAGE : *)\n");
340 fprintf(stdout,"(************************)\n");
342 /* Allocations memoires */
343 /* table des coordonnees
344 profil : (dimension * nombre de noeuds ) */
345 coo = (med_float*) malloc(sizeof(med_float)*nnoe*mdim);
346 /* table des numeros, des numeros de familles des noeuds
347 profil : (nombre de noeuds) */
348 numnoe = (med_int*) malloc(sizeof(med_int)*nnoe);
349 nufano = (med_int*) malloc(sizeof(med_int)*nnoe);
350 /* table des noms des noeuds
351 profil : (nnoe*MED_TAILLE_PNOM+1) */
354 /* PN pour aster, il faut une famille 0 pour les noeuds et une autre pour les elements*/
355 /* PN : Creation de la famille 0 */
356 char * nomfam="FAMILLE_0";
365 ret = MEDfamCr(myFileId,nommaa,nomfam,numfam, &attide,&attval,attdes,natt,gro,ngro);
369 /* PN : FIN Creation de la famille 0 */
371 map < int, int > mapNoeud;
372 typedef pair<set<int>::iterator, bool> IsFamily;
379 int verifienbnoeuds = 0;
382 SMDS_MeshNodesIterator itNodes(myMesh);
383 for (;itNodes.More();itNodes.Next())
385 const Handle(SMDS_MeshElement)& elem = itNodes.Value();
386 const Handle(SMDS_MeshNode)& node = myMesh->GetNode(1,elem);
390 coo[i*3+1]=node->Y();
391 coo[i*3+2]=node->Z();
395 coo[i*2+1]=node->Z();
399 coo[i*2+1]=node->Z();
403 coo[i*2+1]=node->Y();
406 mapNoeud[node->GetID()] = i+1;
408 // renvoie 0 pour les noeuds internes du volume
409 int numfamille=node->GetPosition()->GetShapeId();
410 nufano[i]=numfamille;
415 //SCRUTE(coo[i*3+1]);
416 //SCRUTE(coo[i*3+2]);
417 if ( nufano[i] != 0 )
419 IsFamily deja = FamilySet.insert(nufano[i]); // insert if new, or gives existant
420 if (deja.second) // actually inserted
422 char famille[MED_TAILLE_NOM+1];
423 sprintf(famille,"F%d",nufano[i]);
424 // CreateFamily(strdup(nommaa),strdup(famille),nufano[i],attvalabs++);
426 CreateFamily(strdup(nommaa),strdup(famille),nufano[i],numfamille);
427 //MESSAGE("---famille-noeud--- "<<nbFamillesNoeud<<" "<<nufano[i]);
435 ret = MEDnoeudsEcr(myFileId,nommaa,mdim,coo,mode_coo,MED_CART,
436 nomcoo,unicoo,nomnoe,MED_FAUX,rien,MED_FAUX,
437 nufano,nnoe,MED_REMP);
439 MESSAGE("--- Creation de " << verifienbnoeuds << " noeuds");
440 ASSERT(verifienbnoeuds == nnoe);
441 MESSAGE("--- Creation de " << nbFamillesNoeud << " familles de noeuds");
443 /* liberation memoire */
448 /****************************************************************************
449 * ECRITURE DES ELEMENTS *
450 ****************************************************************************/
451 fprintf(stdout,"\n(**************************)\n");
452 fprintf(stdout,"(* ELEMENTS DU MAILLAGE : *)\n");
453 fprintf(stdout,"(**************************)\n");
455 /* Ecriture des connectivites, noms, numeros des mailles */
459 int nbFamillesElts =0;
460 Handle(SMESHDS_Mesh) mySMESHDSMesh = Handle(SMESHDS_Mesh)::DownCast(myMesh);
461 TopTools_IndexedMapOfShape myIndexToShape;
462 TopExp::MapShapes(mySMESHDSMesh->ShapeToMesh(),myIndexToShape);
464 map < int, int > mapFamille;
466 if (besoinfamilledemaille == 1)
469 for (t =1; t <= myIndexToShape.Extent(); t++)
471 const TopoDS_Shape S = myIndexToShape(t);
472 if (mySMESHDSMesh->HasMeshElements(S))
474 //MESSAGE ("********* Traitement de la Famille "<<-t);
476 Handle(SMESHDS_SubMesh) SM = mySMESHDSMesh->MeshElements(S);
477 const TColStd_ListOfInteger& indElt = SM->GetIDElements();
478 TColStd_ListIteratorOfListOfInteger ite(indElt);
481 for (; ite.More(); ite.Next())
483 int eltId = ite.Value();
484 mapFamille [eltId] = - t;
490 char famille[MED_TAILLE_NOM+1];
491 sprintf(famille,"E%d",t);
492 CreateFamily(strdup(nommaa),strdup(famille),-t,attvalabs++);
499 for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
501 if (nmailles[i] > 0 && ret == 0)
503 MESSAGE ( " Start "<<typmai[i]);
505 /* dimension de la maille */
506 edim = typmai[i] / 100;
508 if (mdim == 2 || mdim == 3)
509 if (edim == 1) nsup = 1;
511 if (edim == 2) nsup = 1;
514 taille = nsup+typmai[i]%100;
517 /* allocation memoire */
518 connectivite = (med_int*)malloc(sizeof(med_int)* taille*nmailles[i]);
519 nomele = (char*)malloc(sizeof(char)*MED_TAILLE_PNOM* nmailles[i]+1);
520 numele = (med_int*)malloc(sizeof(med_int)* nmailles[i]);
521 nufael = (med_int*)malloc(sizeof(med_int)* nmailles[i]);
523 nbNodes=typmai[i]%100;
525 for (j=0;j<nmailles[i];j++)
527 myId = elem_Id[i][j];
528 const Handle(SMDS_MeshElement)& elem = myMesh->FindElement(myId);
529 //*(numele+j) = myId;
530 *(numele+j) = indice ++;
532 for (k=0; k<nbNodes; k++)
534 *(connectivite+j*taille+k) = mapNoeud[elem->GetConnection(k+1)];
535 //SCRUTE(*(connectivite+j*taille+k));
537 if (nsup) *(connectivite+j*taille + nbNodes) = 0;
539 if (besoinfamilledemaille == 1)
541 if (mapFamille.find(myId)!=mapFamille.end())
543 nufael[j]=mapFamille[myId];
560 /* ecriture des données */
563 ret=MEDelementsEcr( myFileId,nommaa,mdim,connectivite,mode_coo,nomele,MED_FAUX,numele,
564 MED_VRAI,nufael,nmailles[i],MED_MAILLE,typmai[i],typ_con,MED_REMP);
568 if (ret < 0) MESSAGE(">> ERREUR : ecriture des mailles \n");
570 /* liberation memoire */
574 MESSAGE ( " End "<<typmai[i]);
577 MESSAGE("--- Creation de " << nbFamillesElts << " familles d elements");
581 /****************************************************************************
582 * FERMETURE DU FICHIER *
583 ****************************************************************************/
585 ret = MEDfermer(myFileId);
588 fprintf(stderr,">> ERREUR : erreur a la fermeture du fichier %s\n",file2Read);
589 MESSAGE ("fichier ferme");
594 void DriverMED_W_SMESHDS_Mesh::CreateFamily(char* nommaa, char* famille, int i,med_int k) {
600 char attdes[MED_TAILLE_DESC+1];
601 char gro[MED_TAILLE_LNOM+1];
602 char fam2[MED_TAILLE_LNOM+1];
606 strcpy(fam2,famille);
608 med_int * attide=new med_int[1];
609 med_int * attval=new med_int[1];
614 //MESSAGE("-------- Creation de la Famille : "<< famille << "numero " << i << " --------------");
615 med_int ret = MEDfamCr(myFileId, nommaa, fam2, i, attide, attval, attdes, natt, gro, ngro);