Salome HOME
Updated copyright comment
[modules/hexablock.git] / src / HEXABLOCK / HexDumpStudy.cxx
1
2 // C++ : Dump python
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 #include "HexDumpStudy.hxx"
23 #include "HexWitness.hxx"
24 #include "HexEltBase.hxx"
25 #include "HexDocument.hxx"
26
27 BEGIN_NAMESPACE_HEXA
28
29 enum {EL_REAL=EL_NONE};
30
31 static cpchar 
32     tab_name [EL_MAXI] = {"TReals", "TNodes", "TEdges", "TQuads", "THexas", 
33                       "TxxxA", "TxxxB",  "TxxxC", "TxxxD", "TxxxE", "TxxxF" };
34
35 // =================================================== Constructeur
36 DumpStudy::DumpStudy ()
37 {
38    map_name [NULL] = "None";
39    is_open   = NOT DumpActif;
40    nbr_nulls = 0;
41    fic_dump  = stdout;
42    witness   = NULL;
43
44    for (int nc=0 ; nc<EL_MAXI ; nc++) tab_count [nc] = 0;
45    if (is_open)
46       return;
47
48    fic_dump = fopen ("tmp_dump.py", "w");
49    if (fic_dump==NULL)
50        fic_dump = stdout;
51
52    std::string buff;
53    cpchar when = get_time (buff);
54
55    fprintf (fic_dump, "\n");
56    fprintf (fic_dump, "# Dump generated by HEXABLOCK at %s\n", when);
57    fprintf (fic_dump, "\n");
58    fprintf (fic_dump, "import hexablock\n");
59    fprintf (fic_dump, "\n");
60
61 }
62 // =================================================== start (EltBase*)
63 bool DumpStudy::start (EltBase* obj, cpchar method)
64 {
65    if (is_open || obj==NULL)
66       return false;
67
68    cpchar name = findName (obj);
69    return start (name, method);
70 }
71 // =================================================== start (name)
72 bool DumpStudy::start (cpchar obj, cpchar method, bool raz)
73 {
74    if (is_open || obj==NULL)
75       return false;
76
77    tab_declar.clear ();
78
79    right_part  = obj;
80    right_part += ".";
81    right_part += method;
82    right_part += " (";
83
84    nbr_args = 0;
85    is_open  = true;
86    if (raz) 
87       witness->raz();
88    return is_open;
89 }
90 // =================================================== operator << (int)
91 DumpStudy& DumpStudy::operator << (int val)
92 {
93    if (NOT is_open)
94       return *this; 
95
96    char     valeur [20];
97    sprintf (valeur, "%d", val);
98
99    addArgument (valeur);
100    return *this;
101 }
102 // =================================================== operator << (double)
103 DumpStudy& DumpStudy::operator << (double val)
104 {
105    if (NOT is_open)
106       return *this; 
107
108    char valeur [20];
109    sprintf (valeur, "%g", val);
110
111    addArgument (valeur);
112    return *this;
113 }
114 // =================================================== operator << (cpchar)
115 DumpStudy& DumpStudy::operator << (cpchar val)
116 {
117    if (NOT is_open)
118       return *this; 
119
120    std::string valeur ("'");
121    valeur += val;
122    valeur += "'";
123
124    addArgument (valeur);
125    return *this;
126 }
127 // =================================================== operator << (elt)
128 DumpStudy& DumpStudy::operator << (EltBase* elt)
129 {
130    if (NOT is_open)
131       return *this; 
132
133    cpchar name = findName (elt);
134    addArgument (name);
135    return *this;
136 }
137 // =================================================== operator << (Quads)
138 DumpStudy& DumpStudy::operator << (Quads& tab)
139 {
140    if (NOT is_open)
141       return *this; 
142
143    TabElts&  tabelt = (TabElts&) tab;
144    addArgVector (EL_QUAD, tabelt) ;
145    return *this; 
146 }
147 // =================================================== operator << (Edges)
148 DumpStudy& DumpStudy::operator << (Edges& tab)
149 {
150    if (NOT is_open)
151       return *this; 
152
153    TabElts&  tabelt = (TabElts&) tab;
154    addArgVector (EL_EDGE, tabelt) ;
155    return *this; 
156 }
157 // =================================================== operator << (Hexas)
158 DumpStudy& DumpStudy::operator << (Hexas& tab)
159 {
160    if (NOT is_open)
161       return *this; 
162
163    TabElts&  tabelt = (TabElts&) tab;
164    addArgVector (EL_HEXA, tabelt) ;
165    return *this; 
166 }
167 // =================================================== operator << (Reals)
168 DumpStudy& DumpStudy::operator << (RealVector& tab)
169 {
170    if (NOT is_open)
171       return *this; 
172
173    char name [20], valeur [30];
174
175    sprintf (name, "%s%d", tab_name[EL_REAL], ++tab_count[EL_REAL]);
176    addVector (name);
177
178    int lg = tab.size ();
179    for (int nv=0 ; nv<lg ; nv++)
180        { 
181        sprintf   (valeur, "%g", tab[nv]);
182        majVector (valeur);
183        } 
184
185    closeVector ();
186    return *this; 
187 }
188 // =================================================== close + return
189 void DumpStudy::close (bool reactive, EltBase* result)
190 {
191    if (reactive)
192       is_open = false;
193    else
194       return;
195
196    char name [32];
197    if (result==NULL)
198       {
199       nbr_nulls ++;
200       sprintf (name, "null%03d", nbr_nulls); 
201       }
202    else
203       {
204       map_name [result] = result->makeVarName (name);
205       }
206
207    declareVectors ();
208    right_part += ")";
209    fprintf (fic_dump, "%s = %s\n", name, right_part.c_str());
210 }
211 // =================================================== close + return int
212 void DumpStudy::close (bool reactive, int result)
213 {
214    static int nbr_values = 0;
215    if (reactive)
216       is_open = false;
217    else
218       return;
219
220    char name [32];
221    nbr_values ++;
222    sprintf (name, "rep%03d", nbr_values); 
223
224    declareVectors ();
225    right_part += ")";
226    fprintf (fic_dump, "%s = %s\n", name, right_part.c_str());
227 }
228 // =================================================== close + return double
229 void DumpStudy::close (bool reactive, double result)
230 {
231    static int nbr_values = 0;
232    if (reactive)
233       is_open = false;
234    else
235       return;
236
237    char name [32];
238    nbr_values ++;
239    sprintf (name, "val%03d", nbr_values); 
240
241    declareVectors ();
242    right_part += ")";
243    fprintf (fic_dump, "%s = %s\n", name, right_part.c_str());
244 }
245 // =================================================== close 
246 void DumpStudy::close (bool reactive)
247 {
248    if (reactive)
249       is_open = false;
250    else
251       return;
252
253    declareVectors ();
254    right_part += ")";
255    fprintf (fic_dump, "%s\n", right_part.c_str());
256 }
257 // =================================================== lock
258 bool DumpStudy::lock ()
259 {
260    if (is_open)
261       return false;
262
263    is_open = true;
264    return is_open;
265 }
266 // =================================================== restore 
267 void DumpStudy::restore (bool reactive)
268 {
269    if (reactive)
270       is_open = false;
271 }
272 // =================================================== getBegin 
273 void DumpStudy::getBegin (std::string& begin)
274 {
275    begin  = right_part;
276    begin += ")";
277 }
278 // -------------------------------------------------------------
279 // ------------ Private
280 // ------------ For members only
281 // -------------------------------------------------------------
282 // =================================================== addArgument
283 void DumpStudy::addArgument (cpchar arg)
284 {
285    nbr_args ++;
286    if (nbr_args>1)
287        right_part += ", ";
288
289    right_part += arg;
290 }
291 // =================================================== addArgVector
292 void DumpStudy::addArgVector (EnumElt type, TabElts& table)
293 {
294    char name [20];
295    sprintf (name, "%s%d", tab_name[type], ++tab_count[type]);
296
297    addVector (name);
298
299    int lg = table.size ();
300
301    for (int nv=0 ; nv<lg ; nv++)
302        { 
303        EltBase* elt = table[nv];
304        cpchar nom = findName (elt);
305        majVector (nom);
306        } 
307
308    closeVector ();
309 }
310 // =================================================== declareVectors
311 void DumpStudy::declareVectors ()
312 {
313    int lg = tab_declar.size();
314    if (lg==0)
315       return;
316
317    for (int nd=0 ; nd<lg ; nd++)
318        {
319        fprintf (fic_dump, "%s\n", tab_declar[nd].c_str());
320        }
321 }
322 // =================================================== addVector
323 void DumpStudy::addVector (cpchar name)
324 {
325    addArgument (name);
326
327    nbr_values   = 0;
328    curr_vector  = name;
329    curr_vector += " = ";
330 }
331 // =================================================== majVector
332 void DumpStudy::majVector (cpchar value)
333 {
334    if (nbr_values == 0)
335        curr_vector += "[";
336    else if ((nbr_values MODULO 7) == 0)
337        curr_vector += ",\n           ";
338    else
339        curr_vector += ", ";
340
341    curr_vector += value;
342    nbr_values ++;
343 }
344 // =================================================== closeVector
345 void DumpStudy::closeVector ()
346 {
347    curr_vector += "]";
348    tab_declar.push_back (curr_vector);
349 }
350 // =================================================== findName
351 cpchar DumpStudy::findName (EltBase* elt)
352 {
353    cpchar name = "Unknown";
354    std::map <EltBase*, std::string> :: iterator iter = map_name.find (elt);
355    if (iter != map_name.end())
356       name = iter->second.c_str();
357    else
358       name = "Unknown";
359
360    return name;
361 }
362 END_NAMESPACE_HEXA