Salome HOME
sources v1.2
[modules/smesh.git] / src / DriverUNV / DriverUNV_R_SMDS_Mesh.cxx
1 //  SMESH DriverUNV : driver to read and write 'unv' files
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : DriverUNV_R_SMDS_Mesh.cxx
25 //  Module : SMESH
26
27 using namespace std;
28 #include "DriverUNV_R_SMDS_Mesh.h"
29
30 #include "utilities.h"
31
32 DriverUNV_R_SMDS_Mesh::DriverUNV_R_SMDS_Mesh() {
33 ;
34 }
35
36 DriverUNV_R_SMDS_Mesh::~DriverUNV_R_SMDS_Mesh() {
37 ;
38 }
39
40 void DriverUNV_R_SMDS_Mesh::SetMesh(Handle(SMDS_Mesh)& aMesh) {
41   myMesh = aMesh;
42 }
43
44 void DriverUNV_R_SMDS_Mesh::SetFile(string aFile) {
45   myFile = aFile;
46 }
47
48 void DriverUNV_R_SMDS_Mesh::SetFileId(FILE* aFileId) {
49   myFileId = aFileId;
50 }
51
52 void DriverUNV_R_SMDS_Mesh::SetMeshId(int aMeshId) {
53   myMeshId = aMeshId;
54 }
55
56 void DriverUNV_R_SMDS_Mesh::Add() {
57   ;
58 }
59
60 void DriverUNV_R_SMDS_Mesh::Read() {
61
62   int cell=0,node=0,n1,n2,n3,n4,n_nodes,nodes[6],blockId,i;
63   char *s1,*s2,*s3;
64   string str1,str2,str3;
65   int i1=0;
66   bool ok, found_block2411, found_block2412;
67
68   /****************************************************************************
69   *                      OUVERTURE DU FICHIER EN LECTURE                      *
70   ****************************************************************************/
71   char* file2Read = (char*)myFile.c_str();
72   myFileId = fopen(file2Read,"r");
73   if (myFileId < 0)
74     {
75       fprintf(stderr,">> ERREUR : ouverture du fichier %s \n",file2Read);
76       exit(EXIT_FAILURE);
77     }
78
79   s1 = (char*) malloc(sizeof(char)*100);
80   s2 = (char*) malloc(sizeof(char)*100);
81   s3 = (char*) malloc(sizeof(char)*100);
82
83   found_block2411 = false;
84   found_block2412 = false;
85
86   do {
87
88     while (i1==-1) {
89       fscanf(myFileId,"%d\n",&blockId);
90       switch (blockId) {
91       case 2411 : {
92       MESSAGE("BlockId "<<blockId);
93         fscanf(myFileId,"%d",&node);
94         //MESSAGE("Node "<<node);
95         while (node!=-1) {
96           fscanf(myFileId,"%d %d %d\n",&n1,&n2,&n3);
97           fscanf(myFileId,"%s %s %s\n",s1,s2,s3);
98           str1=string(s1);
99           str2=string(s2);
100           str3=string(s3);
101           if (str1.find("D")!=string::npos) str1.replace(str1.find("D"),1,"E");
102           if (str2.find("D")!=string::npos) str2.replace(str2.find("D"),1,"E");
103           if (str3.find("D")!=string::npos) str3.replace(str3.find("D"),1,"E");
104           ok = myMesh->AddNodeWithID(atof(str1.c_str()),atof(str2.c_str()),atof(str3.c_str()),node);
105           fscanf(myFileId,"%d",&node);
106         }
107         i1=0;
108         found_block2411 = true;
109         break;
110       } 
111       case 2412 : {
112       MESSAGE("BlockId "<<blockId);
113         fscanf(myFileId,"%d",&cell);
114         //MESSAGE("Cell "<<cell);
115         while (cell!=-1) {
116           fscanf(myFileId,"%d %d %d %d %d\n",&n1,&n2,&n3,&n4,&n_nodes);
117
118           if ((n1==71)||(n1==72)||(n1==74)||(n1==91)||(n1==92)) {//203
119             if (n_nodes==3) {
120               for (i=1;i<=n_nodes;i++)
121                 fscanf(myFileId,"%d",&nodes[i-1]);
122               ok = myMesh->AddFaceWithID(nodes[0],nodes[1],nodes[2],cell);
123             }
124             else if (n_nodes==6) {//206
125               for (i=1;i<=n_nodes;i++)
126                 fscanf(myFileId,"%d",&nodes[i-1]);
127               ok = myMesh->AddFaceWithID(nodes[0],nodes[2],nodes[4],cell);
128             }
129           }
130
131           else if ((n1==11)||(n1==21)||(n1==24)||(n1==25)) {//103
132             fgets(s2,100,myFileId);
133             if (n_nodes==3) {
134               for (i=1;i<=n_nodes;i++)
135                 fscanf(myFileId,"%d",&nodes[i-1]);
136               ok = myMesh->AddEdgeWithID(nodes[0],nodes[1],cell);
137               //MESSAGE("in 103 "<<cell);
138             }
139             else if (n_nodes==2) {//102
140               for (i=1;i<=n_nodes;i++)
141                 fscanf(myFileId,"%d",&nodes[i-1]);
142               ok = myMesh->AddEdgeWithID(nodes[0],nodes[1],cell);
143               //MESSAGE("in 102 "<<cell);
144             }
145           }
146
147           fscanf(myFileId,"\n");
148           fscanf(myFileId,"%d",&cell);
149         }
150
151         i1=0;
152         found_block2412 = true;
153         break;
154       }
155       case -1 : {
156         break;
157       }
158       default:
159         MESSAGE("BlockId "<<blockId);
160         i1=0;
161         break;
162       }
163     }
164   
165     fscanf(myFileId,"%s\n",s1);
166     i1 = atoi(s1);
167
168   }
169   while ((!feof(myFileId))&&((!found_block2411)||(!found_block2412)));
170
171   /****************************************************************************
172   *                      FERMETURE DU FICHIER                      *
173   ****************************************************************************/
174   free(s1);
175   free(s2);
176   free(s3);
177   fclose(myFileId);
178
179 }