Salome HOME
Merge br_enable_import_mesh. Enable import mesh and save/load SMESH study.
[modules/smesh.git] / src / DriverUNV / DriverUNV_R_SMDS_Mesh.cxx
1 using namespace std;
2 #include "DriverUNV_R_SMDS_Mesh.h"
3
4 #include "utilities.h"
5
6
7 extern "C"
8 {
9
10 /**
11  * Factory function which will be called by SMESHDriver
12  */
13 void * SMESH_createUNVMeshReader()
14 {
15         return new DriverUNV_R_SMDS_Mesh();
16 }
17
18 }
19
20 DriverUNV_R_SMDS_Mesh::DriverUNV_R_SMDS_Mesh()
21 {
22         ;
23 }
24
25 DriverUNV_R_SMDS_Mesh::~DriverUNV_R_SMDS_Mesh()
26 {
27         ;
28 }
29
30 void DriverUNV_R_SMDS_Mesh::SetMesh(SMDS_Mesh * aMesh)
31 {
32         myMesh = aMesh;
33 }
34
35 void DriverUNV_R_SMDS_Mesh::SetFile(string aFile)
36 {
37         myFile = aFile;
38 }
39
40 void DriverUNV_R_SMDS_Mesh::SetFileId(FILE * aFileId)
41 {
42         myFileId = aFileId;
43 }
44
45 void DriverUNV_R_SMDS_Mesh::SetMeshId(int aMeshId)
46 {
47         myMeshId = aMeshId;
48 }
49
50 void DriverUNV_R_SMDS_Mesh::Add()
51 {
52         ;
53 }
54
55 void DriverUNV_R_SMDS_Mesh::Read()
56 {
57
58         int cell = 0, node = 0, n1, n2, n3, n4, n_nodes, nodes[6], blockId, i;
59         char *s1, *s2, *s3;
60         string str1, str2, str3;
61         int i1 = 0;
62         bool ok, found_block2411, found_block2412;
63
64   /****************************************************************************
65   *                      OUVERTURE DU FICHIER EN LECTURE                      *
66   ****************************************************************************/
67         char *file2Read = (char *)myFile.c_str();
68         myFileId = fopen(file2Read, "r");
69         if (myFileId < 0)
70         {
71                 fprintf(stderr, ">> ERREUR : ouverture du fichier %s \n", file2Read);
72                 exit(EXIT_FAILURE);
73         }
74
75         s1 = (char *)malloc(sizeof(char) * 100);
76         s2 = (char *)malloc(sizeof(char) * 100);
77         s3 = (char *)malloc(sizeof(char) * 100);
78
79         found_block2411 = false;
80         found_block2412 = false;
81
82         do
83         {
84
85                 while (i1 == -1)
86                 {
87                         fscanf(myFileId, "%d\n", &blockId);
88                         switch (blockId)
89                         {
90                         case 2411:
91                         {
92                                 MESSAGE("BlockId " << blockId);
93                                 fscanf(myFileId, "%d", &node);
94                                 //MESSAGE("Node "<<node);
95                                 while (node != -1)
96                                 {
97                                         fscanf(myFileId, "%d %d %d\n", &n1, &n2, &n3);
98                                         fscanf(myFileId, "%s %s %s\n", s1, s2, s3);
99                                         str1 = string(s1);
100                                         str2 = string(s2);
101                                         str3 = string(s3);
102                                         if (str1.find("D") != string::npos)
103                                                 str1.replace(str1.find("D"), 1, "E");
104                                         if (str2.find("D") != string::npos)
105                                                 str2.replace(str2.find("D"), 1, "E");
106                                         if (str3.find("D") != string::npos)
107                                                 str3.replace(str3.find("D"), 1, "E");
108                                         ok = myMesh->AddNodeWithID(atof(str1.c_str()),
109                                                 atof(str2.c_str()), atof(str3.c_str()), node);
110                                         fscanf(myFileId, "%d", &node);
111                                 }
112                                 i1 = 0;
113                                 found_block2411 = true;
114                                 break;
115                         }
116                         case 2412:
117                         {
118                                 MESSAGE("BlockId " << blockId);
119                                 fscanf(myFileId, "%d", &cell);
120                                 //MESSAGE("Cell "<<cell);
121                                 while (cell != -1)
122                                 {
123                                         fscanf(myFileId, "%d %d %d %d %d\n", &n1, &n2, &n3, &n4,
124                                                 &n_nodes);
125
126                                         if ((n1 == 71) || (n1 == 72) || (n1 == 74) || (n1 == 91) ||
127                                                 (n1 == 92))
128                                         {                       //203
129                                                 if (n_nodes == 3)
130                                                 {
131                                                         for (i = 1; i <= n_nodes; i++)
132                                                                 fscanf(myFileId, "%d", &nodes[i - 1]);
133                                                         ok = myMesh->AddFaceWithID(nodes[0], nodes[1],
134                                                                 nodes[2], cell);
135                                                 }
136                                                 else if (n_nodes == 6)
137                                                 {               //206
138                                                         for (i = 1; i <= n_nodes; i++)
139                                                                 fscanf(myFileId, "%d", &nodes[i - 1]);
140                                                         ok = myMesh->AddFaceWithID(nodes[0], nodes[2],
141                                                                 nodes[4], cell);
142                                                 }
143                                         }
144
145                                         else if ((n1 == 11) || (n1 == 21) || (n1 == 24) ||
146                                                 (n1 == 25))
147                                         {                       //103
148                                                 fgets(s2, 100, myFileId);
149                                                 if (n_nodes == 3)
150                                                 {
151                                                         for (i = 1; i <= n_nodes; i++)
152                                                                 fscanf(myFileId, "%d", &nodes[i - 1]);
153                                                         ok = myMesh->AddEdgeWithID(nodes[0], nodes[1],
154                                                                 cell);
155                                                         //MESSAGE("in 103 "<<cell);
156                                                 }
157                                                 else if (n_nodes == 2)
158                                                 {               //102
159                                                         for (i = 1; i <= n_nodes; i++)
160                                                                 fscanf(myFileId, "%d", &nodes[i - 1]);
161                                                         ok = myMesh->AddEdgeWithID(nodes[0], nodes[1],
162                                                                 cell);
163                                                         //MESSAGE("in 102 "<<cell);
164                                                 }
165                                         }
166
167                                         fscanf(myFileId, "\n");
168                                         fscanf(myFileId, "%d", &cell);
169                                 }
170
171                                 i1 = 0;
172                                 found_block2412 = true;
173                                 break;
174                         }
175                         case -1:
176                         {
177                                 break;
178                         }
179                         default:
180                                 MESSAGE("BlockId " << blockId);
181                                 i1 = 0;
182                                 break;
183                         }
184                 }
185
186                 fscanf(myFileId, "%s\n", s1);
187                 i1 = atoi(s1);
188
189         }
190         while ((!feof(myFileId)) && ((!found_block2411) || (!found_block2412)));
191
192   /****************************************************************************
193   *                      FERMETURE DU FICHIER                      *
194   ****************************************************************************/
195         free(s1);
196         free(s2);
197         free(s3);
198         fclose(myFileId);
199
200 }