0032969: Coding - get rid of unused headers [IMeshData to PLib]
[occt.git] / src / MoniTool / MoniTool_CaseData.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Geom2d_CartesianPoint.hxx>
16 #include <Geom2d_Curve.hxx>
17 #include <Geom_CartesianPoint.hxx>
18 #include <gp_XY.hxx>
19 #include <gp_XYZ.hxx>
20 #include <Message_Msg.hxx>
21 #include <MoniTool_CaseData.hxx>
22 #include <OSD_Timer.hxx>
23 #include <Standard_Failure.hxx>
24 #include <Standard_Transient.hxx>
25 #include <Standard_Type.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TCollection_HAsciiString.hxx>
28 #include <TopoDS_HShape.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <NCollection_DataMap.hxx>
31
32 IMPLEMENT_STANDARD_RTTIEXT(MoniTool_CaseData,Standard_Transient)
33
34 static NCollection_DataMap<TCollection_AsciiString, Standard_Integer> defch;
35 static NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)> defms;
36 static Standard_Boolean stachr = Standard_False;
37
38 //static OSD_Timer chrono;
39 // because merdouille link dynamique & perf, ne creer le static qu au 1er usage
40 static OSD_Timer& chrono() {
41   static OSD_Timer chr;
42   return chr;
43 }
44
45
46     MoniTool_CaseData::MoniTool_CaseData
47   (const Standard_CString caseid, const Standard_CString name)
48     : thesubst (0) , thecase (caseid) , thename (name)  
49       {  thecheck = DefCheck(caseid);  }
50
51     void  MoniTool_CaseData::SetCaseId (const Standard_CString caseid)
52       {  thecase.Clear();  thecase.AssignCat (caseid);  thecheck = DefCheck(caseid);  thesubst = 0;  }
53
54     void  MoniTool_CaseData::SetName   (const Standard_CString name)
55       {  thename.Clear();  thename.AssignCat (name);  thesubst = 0;  }
56
57     Standard_CString  MoniTool_CaseData::CaseId () const
58       {  return thecase.ToCString();  }
59
60     Standard_CString  MoniTool_CaseData::Name   () const
61       {  return thename.ToCString();  }
62
63
64     Standard_Boolean  MoniTool_CaseData::IsCheck   () const
65       {  return  (thecheck > 0);  }
66
67     Standard_Boolean  MoniTool_CaseData::IsWarning () const
68       {  return (thecheck == 1);  }
69
70     Standard_Boolean  MoniTool_CaseData::IsFail    () const
71       {  return (thecheck == 2);  }
72
73     void  MoniTool_CaseData::ResetCheck ()
74       {  thecheck = 0;  }
75
76     void  MoniTool_CaseData::SetWarning ()
77       {  thecheck = 1;  }
78
79     void  MoniTool_CaseData::SetFail    ()
80       {  thecheck = 2;  }
81
82 //  ####    DATA    ####
83
84     void  MoniTool_CaseData::SetChange  ()
85       {  thesubst = -1;   }
86
87     void  MoniTool_CaseData::SetReplace (const Standard_Integer num)
88       {  thesubst = num;  }
89
90     void  MoniTool_CaseData::AddData
91   (const Handle(Standard_Transient)& val,
92    const Standard_Integer kind, const Standard_CString name)
93 {
94   TCollection_AsciiString aname(name);
95   Standard_Integer subs = thesubst;
96
97 //  SetChange (calculer la position d apres Name)
98   if (thesubst < 0) {
99     if (name[0] != '\0') subs = NameNum (name);
100   }
101 //  SetChange / SetReplace
102   if (subs > 0 && subs <= thedata.Length()) {
103     thedata.SetValue (subs,val);
104     thekind.SetValue (subs,kind);
105     if (aname.Length() > 0) thednam.SetValue (subs,aname);
106 //  Ajout Normal
107   } else {
108     thedata.Append (val);
109     thekind.Append (kind);
110     thednam.Append (aname);
111   }
112   thesubst = 0;
113 }
114
115     void  MoniTool_CaseData::AddRaised (const Handle(Standard_Failure)& theException, const Standard_CString name)
116       {  AddData ( theException,1,name);  }
117
118     void  MoniTool_CaseData::AddShape
119   (const TopoDS_Shape& sh, const Standard_CString name)
120       {  AddData ( new TopoDS_HShape(sh), 4,name);  }
121
122     void  MoniTool_CaseData::AddXYZ
123   (const gp_XYZ& aXYZ, const Standard_CString name)
124       {  AddData ( new Geom_CartesianPoint (aXYZ), 5,name);  }
125
126     void  MoniTool_CaseData::AddXY
127   (const gp_XY&  aXY, const Standard_CString name)
128       {  AddData ( new Geom2d_CartesianPoint (aXY), 6,name);  }
129
130     void  MoniTool_CaseData::AddReal
131   (const Standard_Real val, const Standard_CString name)
132       {  AddData ( new Geom2d_CartesianPoint (val,0.), 8,name);  }
133
134     void  MoniTool_CaseData::AddReals
135  (const Standard_Real v1, const Standard_Real v2, const Standard_CString name)
136       {  AddData ( new Geom2d_CartesianPoint (v1,v2), 7,name);  }
137
138     void  MoniTool_CaseData::AddCPU
139   (const Standard_Real lastCPU, const Standard_Real curCPU,
140    const Standard_CString name)
141 {
142   Standard_Real cpu = curCPU;
143   if (cpu == 0.) {
144     Standard_Real sec;  Standard_Integer i1,i2;
145     chrono().Show (sec,i1,i2,cpu);
146   }
147   cpu = cpu - lastCPU;
148   AddData ( new Geom2d_CartesianPoint (cpu,0.), 9,name);
149 }
150
151     Standard_Real  MoniTool_CaseData::GetCPU () const
152 {
153   if (!stachr) { chrono().Start(); stachr = Standard_True; }
154   Standard_Real sec,cpu;  Standard_Integer i1,i2;
155   chrono().Show (sec,i1,i2,cpu);
156   return cpu;
157 }
158
159     Standard_Boolean  MoniTool_CaseData::LargeCPU
160   (const Standard_Real maxCPU,
161    const Standard_Real lastCPU, const Standard_Real curCPU) const
162 {
163   Standard_Real cpu = curCPU;
164   if (cpu == 0.) {
165     Standard_Real sec;  Standard_Integer i1,i2;
166     chrono().Show (sec,i1,i2,cpu);
167   }
168   cpu = cpu - lastCPU;
169   return (cpu >= maxCPU);
170 }
171
172     void  MoniTool_CaseData::AddGeom
173   (const Handle(Standard_Transient)& val, const Standard_CString name)
174       {  AddData (val,3,name);  }
175
176     void  MoniTool_CaseData::AddEntity
177   (const Handle(Standard_Transient)& val, const Standard_CString name)
178       {  AddData (val,2,name);  }
179
180     void  MoniTool_CaseData::AddText
181   (const Standard_CString text, const Standard_CString name)
182       {  AddData (new TCollection_HAsciiString(text),10,name);  }
183
184     void  MoniTool_CaseData::AddInteger
185   (const Standard_Integer val, const Standard_CString name)
186 {
187   Standard_Real rval = val;
188   AddData ( new Geom2d_CartesianPoint (rval,0.), 11,name);
189 }
190
191     void  MoniTool_CaseData::AddAny
192   (const Handle(Standard_Transient)& val, const Standard_CString name)
193       {  AddData (val,0,name);  }
194
195
196     void  MoniTool_CaseData::RemoveData (const Standard_Integer num)
197 {
198   if (num < 1 || num > thedata.Length()) return;
199   thedata.Remove(num);  thekind.Remove(num);  thednam.Remove(num);
200 }
201
202 //    ####    INTERROGATIONS    ####
203
204     Standard_Integer  MoniTool_CaseData::NbData () const
205       {  return thedata.Length();  }
206
207     Handle(Standard_Transient)  MoniTool_CaseData::Data
208   (const Standard_Integer nd) const
209 {
210   Handle(Standard_Transient) val;
211   if (nd < 1 || nd > thedata.Length()) return val;
212   return thedata (nd);
213 }
214
215 Standard_Boolean  MoniTool_CaseData::GetData
216   (const Standard_Integer nd, const Handle(Standard_Type)& type,
217    Handle(Standard_Transient)& val) const
218 {
219   if (type.IsNull()) return Standard_False;
220   if (nd < 1 || nd > thedata.Length()) return Standard_False;
221   Handle(Standard_Transient) v = thedata (nd);
222   if (v.IsNull()) return Standard_False;
223   if (!v->IsKind(type)) return Standard_False;
224   val = v;
225   return Standard_True;
226 }
227
228     Standard_Integer  MoniTool_CaseData::Kind
229   (const Standard_Integer nd) const
230 {
231   if (nd < 1 || nd > thekind.Length()) return 0;
232   return thekind (nd);
233 }
234
235
236 static  const TCollection_AsciiString&  nulname ()
237 {
238   static TCollection_AsciiString nuln;
239   return nuln;
240 }
241
242     const TCollection_AsciiString&  MoniTool_CaseData::Name
243   (const Standard_Integer nd) const
244 {
245   if (nd < 1 || nd > thednam.Length()) return nulname();
246   return thednam(nd);
247 }
248
249 static Standard_Integer NameKind (const Standard_CString name)
250 {
251   char n0 = name[0];
252   if (n0 == 'A' && name[1] == 'N' && name[2] == 'Y' && name[3] == '\0') return 0;
253   if (n0 == 'E') {
254     if (name[1] == 'X' && name[2] == '\0') return 1;
255     if (name[1] == 'N' && name[2] == '\0') return 2;
256     return 0;
257   }
258   if (n0 == 'G' && name[1] == '\0') return 3;
259   if (n0 == 'S' && name[1] == 'H' && name[2] == '\0') return 4;
260   if (n0 == 'X' && name[1] == 'Y') {
261     if (name[2] == 'Z' && name[3] == '\0') return 5;
262     if (name[2] == '\0') return 6;
263   }
264   if (n0 == 'U' && name[1] == 'V' && name[2] == '\0') return 6;
265   if (n0 == 'R') {
266     if (name[1] == '\0') return 8;
267     if (name[1] == 'R' && name[2] == '\0') return 7;
268   }
269   if (n0 == 'C' && name[1] == 'P' && name[2] == 'U' && name[3] == '\0') return 9;
270   if (n0 == 'T' && name[1] == '\0') return 10;
271   if (n0 == 'I' && name[1] == '\0') return 11;
272
273   return 0;
274 }
275
276 static Standard_Integer NameRank (const Standard_CString name)
277 {
278   for (Standard_Integer i = 0; name[i] != '\0'; i ++) {
279     if (name[i] == ':' && name[i+1] != '\0') return atoi(&name[i+1]);
280   }
281   return 1;
282 }
283
284     Standard_Integer  MoniTool_CaseData::NameNum
285   (const Standard_CString name) const
286 {
287   if (!name || name[0] == '\0') return 0;
288   Standard_Integer nd, nn = 0, nb = NbData();
289   for (nd = 1; nd <= nb; nd ++) {
290     if (thednam(nd).IsEqual(name)) return nd;
291   }
292
293   Standard_Integer kind = NameKind (name);
294   if (kind < 0) return 0;
295   Standard_Integer num  = NameRank (name);
296
297   for (nd = 1; nd <= nb; nd ++) {
298     if (thekind(nd) == kind) {
299       nn ++;
300       if (nn == num) return nd;
301     }
302   }
303   return 0;
304 }
305
306
307 //  ####    RETOUR DES VALEURS    ####
308
309     TopoDS_Shape  MoniTool_CaseData::Shape
310   (const Standard_Integer nd) const
311 {
312   TopoDS_Shape sh;
313   Handle(TopoDS_HShape) hs = Handle(TopoDS_HShape)::DownCast (Data(nd));
314   if (!hs.IsNull()) sh = hs->Shape();
315   return sh;
316 }
317
318     Standard_Boolean  MoniTool_CaseData::XYZ
319   (const Standard_Integer nd, gp_XYZ& val) const
320 {
321   Handle(Geom_CartesianPoint) p = Handle(Geom_CartesianPoint)::DownCast(Data(nd));
322   if (p.IsNull()) return Standard_False;
323   val = p->Pnt().XYZ();
324   return Standard_True;
325 }
326
327     Standard_Boolean   MoniTool_CaseData::XY
328   (const Standard_Integer nd, gp_XY& val) const
329 {
330   Handle(Geom2d_CartesianPoint) p = Handle(Geom2d_CartesianPoint)::DownCast(Data(nd));
331   if (p.IsNull()) return Standard_False;
332   val = p->Pnt2d().XY();
333   return Standard_True;
334 }
335
336     Standard_Boolean   MoniTool_CaseData::Reals
337   (const Standard_Integer nd,
338    Standard_Real& v1, Standard_Real& v2) const
339 {
340   Handle(Geom2d_CartesianPoint) p = Handle(Geom2d_CartesianPoint)::DownCast(Data(nd));
341   if (p.IsNull()) return Standard_False;
342   v1 = p->X();  v2 = p->Y();
343   return Standard_True;
344 }
345
346
347     Standard_Boolean   MoniTool_CaseData::Real
348   (const Standard_Integer nd,
349    Standard_Real& val) const
350 {
351   Handle(Geom2d_CartesianPoint) p = Handle(Geom2d_CartesianPoint)::DownCast(Data(nd));
352   if (p.IsNull()) return Standard_False;
353   val = p->X();
354   return Standard_True;
355 }
356
357     Standard_Boolean   MoniTool_CaseData::Text
358   (const Standard_Integer nd,
359    Standard_CString& text) const
360 {
361   Handle(TCollection_HAsciiString) t = Handle(TCollection_HAsciiString)::DownCast(Data(nd));
362   if (t.IsNull()) return Standard_False;
363   text = t->ToCString();
364   return Standard_True;
365 }
366
367     Standard_Boolean   MoniTool_CaseData::Integer
368   (const Standard_Integer nd,
369    Standard_Integer& val) const
370 {
371   Handle(Geom2d_CartesianPoint) p = Handle(Geom2d_CartesianPoint)::DownCast(Data(nd));
372 //  if (p.IsNull()) return Standard_False;
373   if (thekind(nd) != 11) return Standard_False;
374   Standard_Real rval = p->X();
375   val = (Standard_Integer)rval;
376   return Standard_True;
377 }
378
379
380 //  ####    MESSAGES ET DEFINITIONS    ####
381
382 Message_Msg  MoniTool_CaseData::Msg () const
383 {
384   Standard_CString defm = DefMsg (thecase.ToCString());
385
386 //  A REPRENDRE COMPLETEMENT !  Il faut analyser defm = mescode + variables
387   Message_Msg mess;
388   mess.Set (defm);
389
390   return mess;
391 }
392
393
394     void  MoniTool_CaseData::SetDefWarning (const Standard_CString acode)
395       {  defch.Bind(acode,1);  }
396
397     void  MoniTool_CaseData::SetDefFail (const Standard_CString acode)
398       {  defch.Bind(acode,2);  }
399
400     Standard_Integer  MoniTool_CaseData::DefCheck (const Standard_CString acode)
401 {
402   Standard_Integer val;
403   if (!defch.Find(acode, val))
404     val = 0;
405   return val;
406 }
407
408
409     void  MoniTool_CaseData::SetDefMsg
410   (const Standard_CString casecode, const Standard_CString mesdef)
411 {
412   Handle(TCollection_HAsciiString) str = new TCollection_HAsciiString (mesdef);
413   defms.Bind(casecode,str);
414 }
415
416     Standard_CString MoniTool_CaseData::DefMsg (const Standard_CString casecode)
417 {
418   Handle(Standard_Transient) aTStr;
419   if (!defms.Find(casecode, aTStr)) return "";
420   Handle(TCollection_HAsciiString) str = Handle(TCollection_HAsciiString)::DownCast(aTStr);
421   if (str.IsNull()) return "";
422   return str->ToCString();
423 }