Salome HOME
Updated copyright comment
[modules/hexablock.git] / src / HEXABLOCK / test_brep.cxx
1
2 // C++ : Extraction des brep d'un fichier XML 
3
4 // Copyright (C) 2009-2024  CEA, EDF
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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23
24 #include "HexXmlTree.hxx"
25
26 using namespace Hex;
27
28 void parse_brep (XmlTree& xml);
29
30 // ======================================================== test_brep
31 int test_brep (int nbargs, cpchar tabargs[])
32 {
33
34    XmlTree xml("");
35    string filename = "restore";
36    if (nbargs>1)
37       filename = tabargs [1];
38
39    static const int NbExt = 3;
40    static cpchar t_ext [NbExt] = { ".xml", ".XML", ".Xml" };
41    size_t ici   = 0;
42    bool   noext = true;
43    for (int nx = 0; nx < NbExt && noext ; nx++)
44        {
45        ici   = filename.rfind (t_ext[nx]);
46        noext = ici < 0 || ici > filename.size();
47        }
48
49    if (noext)
50       filename += ".xml"; 
51
52    int ier = xml.parseFile (filename);
53    if (ier!=HOK) 
54       return ier;
55
56    parse_brep (xml);
57    return HOK;
58 }
59 // ======================================================== get_names
60 void get_names (const string& chaine, vector<string>& table)
61 {
62    table.clear ();
63    int    lg    = chaine.size();
64    string mot   = "";
65    bool encours = false;
66
67    for (int nc=0 ; nc<lg ; nc++)
68        {
69        char car  = chaine[nc];
70        if (isalnum (car))
71           {
72           mot += car;
73           encours = true;
74           }
75        else if (encours)
76           {
77           table.push_back (mot);
78           encours = false;
79           mot     = "";
80           }
81        }
82
83    if (encours)
84       table.push_back (mot);
85 }
86 // ======================================================== save_file_brep
87 void save_file_brep (string& nom, string& brep)
88 {
89    string filename = nom + ".brep";
90    FILE*  fic      = fopen (filename.c_str(), "w"); 
91
92    if (fic==NULL)
93       {
94       cout << " *** Echec a l'ouverture du fichier " << filename << endl;
95       return;
96       }
97
98    cout << " +++ Ouverture du fichier " << filename << endl;
99    fprintf (fic, "%s\n", brep.c_str());
100    fclose  (fic);
101    
102 }
103 // ======================================================== parse_brep
104 void parse_brep (XmlTree& xml)
105 {
106    // xml.dump ();
107
108    XmlTree* rubrique = xml.findChild ("ListVertices");
109    int nbrelts       = rubrique->getNbrChildren ();
110
111    string nom, brep, filename;
112    XmlTree* node = NULL;
113    vector <string> tname;
114
115    for (int nro=0 ; nro < nbrelts ; nro++)
116        {
117        node = rubrique->getChild (nro);
118        nom  = node->findValue ("id");
119        brep = node->findValue ("shape");
120        if (NOT brep.empty ())
121            save_file_brep (nom, brep);
122        }
123
124    rubrique = xml.findChild ("ListEdges");
125    nbrelts  = rubrique->getNbrChildren ();
126
127    for (int nro=0 ; nro < nbrelts ; nro++)
128        {
129        node = rubrique->getChild (nro);
130        const string& type = node->getName();
131        if (type=="Edge")
132           {
133           nom      = node->findValue ("id");
134           const  string& vertices = node->findValue ("vertices");
135           get_names (vertices, tname);
136           }
137        else if (type=="Shape")
138           {
139           brep  = node->findValue ("brep");
140           save_file_brep (nom, brep);
141           }
142        }
143
144    rubrique = xml.findChild ("ListQuads");
145    nbrelts  = rubrique->getNbrChildren ();
146
147    for (int nro=0 ; nro < nbrelts ; nro++)
148        {
149        node = rubrique->getChild (nro);
150        const string& type = node->getName();
151        if (type=="Quad")
152           {
153           nom   = node->findValue ("id");
154           const string& edges = node->findValue ("edges");
155           get_names (edges, tname);
156           }
157        else if (type=="Shape")
158           {
159           brep = node->findValue ("brep");
160           save_file_brep (nom, brep);
161           }
162        }
163 }