Salome HOME
db201a99e1fe2ce5349fb56c47e1bd2f8dee5fde
[modules/hexablock.git] / src / HEXABLOCK / HexDocument_Xml.cxx
1
2 // C++ : Classe Document : methodes internes
3
4 #include "HexDocument.hxx"
5
6 #include "HexEltBase.hxx"
7 #include "HexVertex.hxx"
8 #include "HexEdge.hxx"
9 #include "HexQuad.hxx"
10 #include "HexHexa.hxx"
11
12 #include "HexElements.hxx"
13
14 #include "HexVector.hxx"
15 #include "HexCylinder.hxx"
16 #include "HexPipe.hxx"
17 #include "HexMatrix.hxx"
18 #include "HexCloner.hxx"
19 #include "HexPropagation.hxx"
20 #include "HexLaw.hxx"
21
22 #include "HexXmlWriter.hxx"
23 #include "HexXmlTree.hxx"
24
25 BEGIN_NAMESPACE_HEXA
26
27 // ======================================================== get_coords
28 int get_coords (const string& chaine, double& x, double& y, double& z)
29 {
30    // int nv = sscanf (chaine.c_str (), "%lg %lg %lg", &x, &y, &z);
31    cpchar buffer = chaine.c_str ();
32    int nv = sscanf (buffer, "%lg %lg %lg", &x, &y, &z);
33    if (nv!=3) return HERR;
34    return HOK;
35 }
36 // ======================================================== get_values
37 int get_values (const string& chaine, int size, int table[])
38 {
39    int lg  = chaine.size();
40    int nv  = 0;
41    int val = 0;
42    bool encours = false;
43
44    for (int nc=0 ; nc<lg ; nc++)
45        {
46        char car  = chaine[nc];
47        if (car >= '0' && car <= '9')
48           {
49           val = 10* val + car - '0';
50           encours = true;
51           }
52        else if (encours)
53           {
54           table [nv] = val;
55           encours = false;
56           val     = 0;
57           nv++;
58           if (nv >= size) 
59              return nv;
60           }
61        }
62
63    if (encours)
64       table [nv] = val;
65    return nv;
66 }
67 // ======================================================== get_int
68 int get_int (const string& chaine)
69 {
70    int val [2] = {0, 0};
71    get_values (chaine, 1, val);
72    return val[0];
73 }
74 // ======================================================== loadXml
75 int Document::loadXml ()
76 {
77    XmlTree xml("");
78    xml.parseFile (doc_name + ".xml");
79    xml.dump ();
80
81    vector <Vertex*> t_vertex;
82    vector <Edge*>   t_edge;
83    vector <Quad*>   t_quad;
84    vector <Hexa*>   t_hexa;
85    int table [10];
86
87    XmlTree* rubrique = xml.findChild ("ListVertices");
88    int nbrelts       = rubrique->getNbrChildren ();
89    t_vertex.resize (nbrelts);
90
91    for (int nro=0 ; nro < nbrelts ; nro++)
92        {
93        XmlTree* node = rubrique->getChild (nro);
94        double px, py, pz;
95        const  string& nom    = node->findValue ("id");
96        const  string& coords = node->findValue ("coord");
97        get_coords (coords, px, py, pz);
98
99        int nver = get_int (nom);
100        t_vertex [nver] = addVertex (px, py, pz);
101        Display  (nver);
102        }
103
104
105    rubrique = xml.findChild ("ListEdges");
106    nbrelts  = rubrique->getNbrChildren ();
107    t_edge.resize (nbrelts);
108
109    for (int nro=0 ; nro < nbrelts ; nro++)
110        {
111        XmlTree* node = rubrique->getChild (nro);
112        const  string& nom      = node->findValue ("id");
113        const  string& vertices = node->findValue ("vertices");
114        get_values (vertices, V_TWO, table);
115
116        int ned = get_int (nom);
117        t_edge [ned] = new Edge (t_vertex [table[0]], t_vertex [table[1]]);
118        Display  (ned);
119        }
120
121    rubrique = xml.findChild ("ListQuads");
122    nbrelts  = rubrique->getNbrChildren ();
123    t_quad.resize (nbrelts);
124
125    for (int nro=0 ; nro < nbrelts ; nro++)
126        {
127        XmlTree* node = rubrique->getChild (nro);
128        const string& nom   = node->findValue ("id");
129        const string& edges = node->findValue ("edges");
130        get_values (edges, QUAD4, table);
131
132        int nquad = get_int (nom);
133        t_quad [nquad] = new Quad (t_edge [table[0]], t_edge [table[1]],
134                                   t_edge [table[2]], t_edge [table[3]]);
135        Display  (nquad);
136        }
137
138    rubrique = xml.findChild ("ListHexas");
139    nbrelts  = rubrique->getNbrChildren ();
140    t_hexa.resize (nbrelts);
141
142    for (int nro=0 ; nro < nbrelts ; nro++)
143        {
144        XmlTree* node = rubrique->getChild (nro);
145        const  string& nom   = node->findValue ("id");
146        const  string& quads = node->findValue ("quads");
147        get_values (quads, HQ_MAXI, table);
148
149        int nhexa      = get_int (nom);
150        t_hexa [nhexa] = new Hexa (t_quad [table[0]], t_quad [table[1]],
151                                   t_quad [table[2]], t_quad [table[3]],
152                                   t_quad [table[4]], t_quad [table[5]]);
153        Display  (nhexa);
154        }
155
156
157    return HOK;
158 }
159 // ======================================================== renumeroter
160 void Document::renumeroter ()
161 {
162    doc_modified = true;
163                                        // -- 1) Raz numerotation precedente
164    markAll (NO_COUNTED);
165 }
166 // ======================================================== saveFile
167 int Document::saveFile ()
168 {
169                                        // -- 1) Raz numerotation precedente
170    renumeroter ();
171    if (maj_propagation)
172        majPropagation ();
173
174    doc_modified = false;
175    XmlWriter xml;
176    xml.openXml  (doc_name);
177    xml.openMark ("Document");
178    xml.addAttribute ("name", doc_name);
179    xml.endMark ();
180
181    cpchar balise [] = {"ListXXX", 
182           "ListVertices", "ListEdges", "ListQuads", "ListHexas", "ListXXXX" };
183
184    for (int type=EL_VERTEX ; type <= EL_HEXA ; type++)
185        {
186        xml.addMark (balise [type]);
187        for (EltBase* elt = doc_first_elt[type]->next (); elt!=NULL;
188                      elt = elt->next())
189            {
190            if (elt !=NULL && elt->isHere())
191               elt->saveXml (xml);
192            }
193        xml.closeMark (true);
194        }
195
196    xml.addMark ("ListDicretizationLaws");
197    for (int nro=0 ; nro<nbr_laws ; nro++)
198        doc_laws [nro]->saveXml (xml);
199    xml.closeMark (true);
200
201    xml.addMark ("ListPropagations");
202    for (int nro=0 ; nro<nbr_propagations ; nro++)
203        doc_propagation[nro]->saveXml (xml);
204    xml.closeMark ();
205
206    xml.closeMark ();
207    xml.closeXml  ();
208    return  HOK;
209 }
210 // ======================================================== markAll
211 void Document::markAll (int marque, int type)
212 {
213    int debut = EL_VERTEX;
214    int fin   = EL_HEXA;
215    if (type>=0 && type<EL_MAXI)
216       debut = fin = type;
217
218    for (int type=debut ; type <= fin ; type++)
219        {
220        for (EltBase* elt = doc_first_elt[type]->next (); elt!=NULL;
221                      elt = elt->next())
222            elt->setMark (marque);
223        }
224 }
225 // ====================================================== saveVtk
226 int Document::saveVtk (cpchar nomfic)
227 {
228                                            // -- 1) Raz numerotation precedente
229    markAll (NO_COUNTED, EL_VERTEX);
230
231    int nbnodes = 0;
232    int nbcells = 0;
233                                            // -- 2) Comptage
234    for (EltBase* elt = doc_first_elt[EL_HEXA]->next (); elt!=NULL;
235                  elt = elt->next())
236        {
237        Hexa* cell = static_cast <Hexa*> (elt);
238        if (cell!=NULL && cell->isHere())
239           {
240           nbcells ++;
241           nbnodes += cell->countNodes ();
242           }
243        }
244
245    pfile    vtk = fopen (nomfic, "w");
246    fprintf (vtk, "# vtk DataFile Version 3.1\n");
247    fprintf (vtk, "%s \n", nomfic);
248    fprintf (vtk, "ASCII\n");
249    fprintf (vtk, "DATASET UNSTRUCTURED_GRID\n");
250    fprintf (vtk, "POINTS %d float\n", nbnodes);
251
252                                            // -- 2) Les noeuds
253    int nronode = 0;
254    for (EltBase* elt = doc_first_elt[EL_HEXA]->next (); elt!=NULL;
255                  elt = elt->next())
256        {
257        Hexa* cell = static_cast <Hexa*> (elt);
258        if (cell!=NULL && cell->isHere())
259           cell->printNodes (vtk, nronode);
260        }
261                                            // -- 2) Les hexas
262
263    fprintf (vtk, "CELLS %d %d\n", nbcells, nbcells*(HV_MAXI+1));
264
265    for (EltBase* elt = doc_first_elt[EL_HEXA]->next (); elt!=NULL;
266                  elt = elt->next())
267        {
268        Hexa* cell = static_cast <Hexa*> (elt);
269        if (cell!=NULL && cell->isHere())
270           cell->printHexa (vtk);
271        }
272
273    fprintf (vtk, "CELL_TYPES %d\n", nbcells);
274    for (int nro=0 ; nro<nbcells ; nro++)
275        fprintf (vtk, "%d\n", HE_MAXI);
276
277    fprintf (vtk, "POINT_DATA %d \n", nbnodes);
278    fprintf (vtk, "SCALARS A float\n");
279    fprintf (vtk, "LOOKUP_TABLE default\n");
280
281    for (EltBase* elt = doc_first_elt[EL_HEXA]->next (); elt!=NULL;
282                  elt = elt->next())
283        {
284        Hexa* cell = static_cast <Hexa*> (elt);
285        if (cell!=NULL && cell->isHere())
286           cell->colorNodes (vtk);
287        }
288
289    fclose (vtk);
290    return HOK;
291 }
292 // ====================================================== purge
293 void Document::purge ()
294 {
295    purge_elements = true;
296                        // ------------------- Raz marques
297    markAll (NO_USED);
298
299                        // ------------------- Marquage elements utilises
300    for (EltBase* elt = doc_first_elt[EL_HEXA]->next (); elt!=NULL;
301                  elt = elt->next())
302        {
303        Hexa* cell = static_cast <Hexa*> (elt);
304        if (cell!=NULL && cell->isHere())
305           cell->markElements (IS_USED);
306        }
307                        // ------------------- Elimination elements inutilises
308    for (int type=EL_VERTEX ; type <= EL_QUAD ; type++)
309        {
310        for (EltBase* elt = doc_first_elt[type]->next (); elt!=NULL;
311                      elt = elt->next())
312            {
313            if (elt->getMark  () == NO_USED)
314                elt->suppress ();
315            }
316        }
317                        // ------------------- Sortie elements inutilises
318
319    EltBase* trash = doc_last_elt [EL_REMOVED];
320
321    for (int type=EL_VERTEX ; type <= EL_HEXA ; type++)
322        {
323        doc_nbr_elt [type] = 0;
324        EltBase* last = doc_first_elt [type];
325        for (EltBase* elt = last->next (); elt!=NULL; elt = last->next())
326            {
327            if (elt->isHere  ())
328                {
329                doc_nbr_elt  [type] ++;
330                last = elt;
331                }
332            else
333                {
334                last  -> setNext (elt -> next());
335                trash -> setNext (elt);
336                trash = elt;
337                trash -> setNext (NULL);
338                }
339            }
340        doc_last_elt [type] = last;
341        }
342
343    doc_last_elt [EL_REMOVED] = trash;
344    update ();
345 }
346 // ======================================================== majReferences
347 void Document::majReferences ()
348 {
349    maj_connection = false;
350
351    for (int type=EL_VERTEX ; type <= EL_QUAD ; type++)
352        {
353        for (EltBase* elt = doc_first_elt[type]->next (); elt!=NULL;
354                      elt = elt->next())
355            {
356            elt->razReferences ();
357            }
358        }
359
360    for (int type=EL_EDGE ; type <= EL_HEXA ; type++)
361        {
362        for (EltBase* elt = doc_first_elt[type]->next (); elt!=NULL;
363                      elt = elt->next())
364            {
365            if (elt->isHere ())
366                elt->majReferences ();
367            }
368        }
369 }
370 // ======================================================== dump
371 void Document::dump ()
372 {
373    cpchar nom_type [] = { "Elments non classes", 
374           "Sommets", "Aretes", "Faces", "Hexaedres", "Elements detruits" };
375
376    for (int type=EL_VERTEX ; type <= EL_HEXA ; type++)
377        {
378        printf ("\n");
379        printf (" ++++ Liste des %s\n", nom_type[type]);
380        printf ("\n");
381
382        for (EltBase* elt = doc_first_elt[type]->next (); elt!=NULL;
383                      elt = elt->next())
384            {
385            elt->dump ();
386            }
387        }
388
389    printf (" ++++ End od dump\n");
390 }
391 // ======================================================== putError
392 void Document::putError (cpchar mess, cpchar info1, cpchar info2)
393 {
394      nbr_errors ++;
395      printf (" ********************************************************** \n");
396      printf (" ****  HexaBlocks Error nro %d :\n", nbr_errors);
397      printf (" **** ");
398      printf (mess, info1, info2);
399      printf ("\n");
400      printf (" **** \n");
401      printf (" ********************************************************** \n");
402 }
403
404 END_NAMESPACE_HEXA