Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Interface / Interface_CheckIterator.cxx
1 #include <Interface_CheckIterator.ixx> 
2 #include <Standard_NoSuchObject.hxx>
3 #include <TCollection_HAsciiString.hxx>
4
5
6 static const Handle(Interface_Check)& nulcheck ()
7 {
8   static Handle(Interface_Check) nulch = new Interface_Check;
9   return nulch;
10 }
11
12
13 //=======================================================================
14 //function : Interface_CheckIterator
15 //purpose  : 
16 //=======================================================================
17
18 Interface_CheckIterator::Interface_CheckIterator ()
19 {
20   Clear();
21 }
22
23
24 //=======================================================================
25 //function : Interface_CheckIterator
26 //purpose  : 
27 //=======================================================================
28
29 Interface_CheckIterator::Interface_CheckIterator(const Standard_CString name)
30      : thename (name)
31 {
32   Clear();
33 }
34
35
36 //=======================================================================
37 //function : SetName
38 //purpose  : 
39 //=======================================================================
40
41 void Interface_CheckIterator::SetName (const Standard_CString name)
42 {
43   thename.Clear();
44   if (name[0] != '\0') thename.AssignCat(name);
45 }
46
47
48 //=======================================================================
49 //function : Name
50 //purpose  : 
51 //=======================================================================
52
53 Standard_CString Interface_CheckIterator::Name () const
54 {
55   return thename.ToCString();
56 }
57
58
59 //=======================================================================
60 //function : SetModel
61 //purpose  : 
62 //=======================================================================
63
64 void Interface_CheckIterator::SetModel(const Handle(Interface_InterfaceModel)& model)
65 {
66   themod = model;
67 }
68
69
70 //=======================================================================
71 //function : Model
72 //purpose  : 
73 //=======================================================================
74
75 Handle(Interface_InterfaceModel) Interface_CheckIterator::Model() const
76 {
77   return themod;
78 }
79
80
81 //=======================================================================
82 //function : Clear
83 //purpose  : 
84 //=======================================================================
85
86 void Interface_CheckIterator::Clear ()
87 {
88   thelist = new Interface_HSequenceOfCheck();
89   thenums = new TColStd_HSequenceOfInteger();
90   thecurr = new Interface_IntVal;
91   thecurr->CValue() = 1;
92 }
93
94
95 //=======================================================================
96 //function : Merge
97 //purpose  : 
98 //=======================================================================
99
100 void Interface_CheckIterator::Merge (Interface_CheckIterator& other)
101 {
102   themod = other.Model();
103   for (other.Start(); other.More(); other.Next())
104     Add (other.Value(),other.Number());
105 }
106
107
108 //=======================================================================
109 //function : Add
110 //purpose  : 
111 //=======================================================================
112
113 void Interface_CheckIterator::Add(const Handle(Interface_Check)& ach,
114                                   const Standard_Integer num)
115 {
116   //  Add <meme num que le dernier> -> cumul des Checks
117   if (ach->NbWarnings() + ach->NbFails() == 0) return;
118   Standard_Integer nm = num;
119   if (num <= 0 && ach->HasEntity()) {
120     if (!themod.IsNull()) {
121       nm = themod->Number (ach->Entity());
122       if (nm <= 0) nm = -1;
123     }
124     else nm = -1;
125   }
126   if (nm >= 0 && nm <= - (thecurr->Value()) ) {
127     Standard_Integer i , numpos = 0 , nb = thelist->Length();
128     for (i = nb; i > 0; i --)
129       if (thenums->Value(i) == nm) {  numpos = i; break;  }
130     if (numpos > 0 && nm >= 0) {
131       Handle(Interface_Check) lch = thelist->ChangeValue(numpos);
132       lch->GetMessages (ach);
133     }
134     //  Cas normal : on ajoute en fin de liste
135     else  {  thelist->Append(ach);  thenums->Append(nm);  }
136   }
137   //  Pas encore vu passe : inutile de chercher
138   else  {  thelist->Append(ach);  thenums->Append(nm);  thecurr->CValue() = -nm;  }
139 }
140
141
142 //=======================================================================
143 //function : Check
144 //purpose  : 
145 //=======================================================================
146
147 const Handle(Interface_Check)& Interface_CheckIterator::Check
148        (const Standard_Integer num) const
149 {
150   Standard_Integer i, nb = thelist->Length();
151   for (i = 1; i <= nb; i ++) {
152     if (num == thenums->Value(i)) return thelist->Value(i);
153   }
154   return nulcheck();
155 }
156
157
158 //=======================================================================
159 //function : Check
160 //purpose  : 
161 //=======================================================================
162
163 const Handle(Interface_Check)& Interface_CheckIterator::Check
164   (const Handle(Standard_Transient)& ent) const
165 {
166   Standard_Integer num = -1;
167   if (!themod.IsNull()) num = themod->Number(ent);
168   if (num > 0) return Check(num);
169
170   Standard_Integer i, nb = thelist->Length();
171   for (i = 1; i <= nb; i ++) {
172     if (ent == thelist->Value(i)->Entity()) return thelist->Value(i);
173   }
174   return nulcheck();
175 }
176
177
178 //=======================================================================
179 //function : CCheck
180 //purpose  : 
181 //=======================================================================
182
183 Handle(Interface_Check)& Interface_CheckIterator::CCheck
184        (const Standard_Integer num)
185 {
186   Standard_Integer i, nb = thenums->Length();
187   for (i = 1; i <= nb; i ++) {
188     if (num == thenums->Value(i)) return thelist->ChangeValue(i);
189   }
190   Handle(Interface_Check) ach = new Interface_Check;
191   thelist->Append(ach);  thenums->Append(num);
192   return thelist->ChangeValue (thelist->Length());
193 }
194
195
196 //=======================================================================
197 //function : CCheck
198 //purpose  : 
199 //=======================================================================
200
201 Handle(Interface_Check)& Interface_CheckIterator::CCheck
202        (const Handle(Standard_Transient)& ent)
203 {
204   Standard_Integer num = -1;
205   if (!themod.IsNull()) num = themod->Number(ent);
206   if (num > 0) return CCheck(num);
207
208   Standard_Integer i, nb = thelist->Length();
209   for (i = 1; i <= nb; i ++) {
210     if (ent == thelist->Value(i)->Entity()) return thelist->ChangeValue(i);
211   }
212
213   Handle(Interface_Check) ach = new Interface_Check;
214   thelist->Append(ach);  thenums->Append(num);
215   return thelist->ChangeValue (thelist->Length());
216 }
217
218
219 //=======================================================================
220 //function : IsEmpty
221 //purpose  : 
222 //=======================================================================
223
224 Standard_Boolean Interface_CheckIterator::IsEmpty
225   (const Standard_Boolean failsonly) const
226 {
227   if (thelist->IsEmpty()) return Standard_True;
228   if (!failsonly) return Standard_False;
229   Standard_Integer i, nb = thelist->Length();
230   for (i = 1; i <= nb; i ++) {
231     if (thelist->Value(i)->HasFailed()) return Standard_False;
232   }
233   return Standard_True;
234 }
235
236
237 //=======================================================================
238 //function : Status
239 //purpose  : 
240 //=======================================================================
241
242 Interface_CheckStatus Interface_CheckIterator::Status () const
243 {
244   Interface_CheckStatus stat = Interface_CheckOK;
245   Standard_Integer i, nb = thelist->Length();
246   for (i = 1; i <= nb; i ++) {
247     const Handle(Interface_Check) ach = thelist->Value(i);
248     if (ach->HasFailed()) return Interface_CheckFail;
249     if (ach->NbWarnings() > 0) stat = Interface_CheckWarning;
250   }
251   return stat;
252 }
253
254
255 //=======================================================================
256 //function : Complies
257 //purpose  : 
258 //=======================================================================
259
260 Standard_Boolean Interface_CheckIterator::Complies
261   (const Interface_CheckStatus stat) const
262 {
263   Standard_Boolean res = Standard_False;
264   if (stat == Interface_CheckNoFail) res = Standard_True;
265   Standard_Integer i, nb = thelist->Length();
266   for (i = 1; i <= nb; i ++) {
267     const Handle(Interface_Check) ach = thelist->Value(i);
268     Standard_Integer nbf = ach->NbFails(), nbw = ach->NbWarnings();
269     switch (stat) {
270     case Interface_CheckOK      : if (nbf + nbw > 0) return Standard_False; break;
271     case Interface_CheckWarning : if (nbf > 0) return Standard_False;
272                                   if (nbw > 0) res  = Standard_True;  break;
273     case Interface_CheckFail    : if (nbf > 0) return Standard_True;  break;
274     case Interface_CheckAny     : return Standard_True;
275     case Interface_CheckMessage : if (nbf + nbw > 0) return Standard_True;  break;
276     case Interface_CheckNoFail  : if (nbf > 0) return Standard_False;  break;
277     default : break;
278     }
279   }
280   return res;
281 }
282
283
284 //=======================================================================
285 //function : Extract
286 //purpose  : 
287 //=======================================================================
288
289 Interface_CheckIterator Interface_CheckIterator::Extract
290   (const Interface_CheckStatus stat) const
291 {
292   Interface_CheckIterator res;
293   res.SetModel (themod);  res.SetName (thename.ToCString());
294   Standard_Integer i, nb = thelist->Length();
295   for (i = 1; i <= nb; i ++) {
296     const Handle(Interface_Check) ach = thelist->Value(i);
297     Standard_Integer nbf = ach->NbFails(), nbw = ach->NbWarnings();
298     Standard_Boolean prend = Standard_False;
299     switch (stat) {
300     case Interface_CheckOK      : prend = (nbf + nbw == 0);       break;
301     case Interface_CheckWarning : prend = (nbf == 0 && nbw > 0);  break;
302     case Interface_CheckFail    : prend = (nbf >  0);             break;
303     case Interface_CheckAny     : prend = Standard_True;          break;
304     case Interface_CheckMessage : prend = (nbf + nbw >  0);       break;
305     case Interface_CheckNoFail  : prend = (nbf == 0);             break;
306     default : break;
307     }
308     if (prend) res.Add (ach,thenums->Value(i));
309   }
310   return res;
311 }
312
313
314 //=======================================================================
315 //function : Extract
316 //purpose  : 
317 //=======================================================================
318
319 Interface_CheckIterator Interface_CheckIterator::Extract
320   (const Standard_CString mess,
321    const Standard_Integer incl, const Interface_CheckStatus stat) const
322 {
323   Handle(TCollection_HAsciiString) str = new TCollection_HAsciiString (mess);
324   Interface_CheckIterator res;
325   res.SetModel (themod);  res.SetName (thename.ToCString());
326   Standard_Integer i, nb = thelist->Length();
327   for (i = 1; i <= nb; i ++) {
328     const Handle(Interface_Check) ach = thelist->Value(i);
329     if (ach->Complies(str,incl,stat)) res.Add (ach,thenums->Value(i));
330   }
331   return res;
332 }
333
334
335 //=======================================================================
336 //function : Remove
337 //purpose  : 
338 //=======================================================================
339
340 Standard_Boolean Interface_CheckIterator::Remove(const Standard_CString mess,
341                                                  const Standard_Integer incl,
342                                                  const Interface_CheckStatus stat)
343 {
344   Handle(TCollection_HAsciiString) str = new TCollection_HAsciiString (mess);
345   Standard_Boolean res = Standard_False;
346   Standard_Integer i, nb = thelist->Length();
347   for (i = 1; i <= nb; i ++) {
348     Handle(Interface_Check) ach = thelist->ChangeValue(i);
349     if (ach->Remove (str,incl,stat)) res = Standard_True;
350   }
351   return res;
352 }
353
354
355 //=======================================================================
356 //function : Checkeds
357 //purpose  : 
358 //=======================================================================
359
360 Handle(TColStd_HSequenceOfTransient) Interface_CheckIterator::Checkeds
361   (const Standard_Boolean failsonly, const Standard_Boolean global) const
362 {
363   Handle(TColStd_HSequenceOfTransient) list;
364   if (themod.IsNull()) return list;
365   list = new TColStd_HSequenceOfTransient();
366   Standard_Integer num, i, nb = thelist->Length();
367   for (i = 1; i <= nb; i ++) {
368     const Handle(Interface_Check) chk = thelist->Value(i);
369     if (failsonly && !chk->HasFailed()) continue;
370     if (chk->NbWarnings() == 0) continue;
371     num = thenums->Value(i);
372     if (num == 0 && global) list->Append (themod);
373     else if (num > 0) list->Append (themod->Value(num));
374   }
375   return list;
376 }
377
378
379 //=======================================================================
380 //function : Start
381 //purpose  : 
382 //=======================================================================
383
384 void Interface_CheckIterator::Start () const
385 {
386   thecurr->CValue() = 1;
387 }
388
389
390 //=======================================================================
391 //function : More
392 //purpose  : 
393 //=======================================================================
394
395 Standard_Boolean Interface_CheckIterator::More () const
396 {
397   if (thecurr->Value() < 0) thecurr->CValue() = 1;
398   return (thecurr->Value() <= thelist->Length());
399 }
400
401
402 //=======================================================================
403 //function : Next
404 //purpose  : 
405 //=======================================================================
406
407 void Interface_CheckIterator::Next () const
408 {
409   if (thecurr->Value() < 0) thecurr->CValue() = 1;
410   thecurr->CValue() ++;
411 }
412
413
414 //=======================================================================
415 //function : Value
416 //purpose  : 
417 //=======================================================================
418
419 const Handle(Interface_Check)& Interface_CheckIterator::Value () const 
420 {
421   if (thecurr->Value() > thelist->Length()) Standard_NoSuchObject::Raise
422     ("Interface Check Iterator : Value");
423   return thelist->Value(thecurr->Value());
424 }
425
426
427 //=======================================================================
428 //function : Number
429 //purpose  : 
430 //=======================================================================
431
432 Standard_Integer Interface_CheckIterator::Number () const 
433 {
434   if (thecurr->Value() > thenums->Length()) Standard_NoSuchObject::Raise
435     ("Interface Check Iterator : Value");
436   return thenums->Value(thecurr->Value());
437 }
438
439
440 //=======================================================================
441 //function : Print
442 //purpose  : 
443 //=======================================================================
444
445 void Interface_CheckIterator::Print(const Handle(Message_Messenger)& S,
446                                     const Standard_Boolean failsonly,
447                                     const Standard_Integer final) const
448 {
449   Print (S,themod,failsonly,final);
450 }
451
452
453 //=======================================================================
454 //function : Print
455 //purpose  : 
456 //=======================================================================
457
458 void Interface_CheckIterator::Print(const Handle(Message_Messenger)& S,
459                                     const Handle(Interface_InterfaceModel)& model,
460                                     const Standard_Boolean failsonly,
461                                     const Standard_Integer /*final*/) const
462 {
463   Standard_Boolean titre = Standard_False;
464   /*Standard_CString mesnum;
465     Standard_CString mesnum0 = ":";
466     Standard_CString mesnum1 = " (original):";
467     Standard_CString mesnum2 = " (computed):";    */
468   Standard_Integer i, nbch = 0, nb = thelist->Length();//,j; svv #2
469   Standard_Boolean yamod = !model.IsNull();
470   for (i = 1; i <= nb; i ++) {
471     const Handle(Interface_Check) ach = thelist->Value(i);
472     Standard_Integer nbw = 0, nbf = ach->NbFails();
473     if (!failsonly)  nbw = ach->NbWarnings();
474     if (nbf + nbw == 0) continue;
475     Handle(Standard_Transient) ent = ach->Entity();
476     Standard_Integer nm0 = thenums->Value(i);
477     Standard_Boolean entnul = ent.IsNull();
478     Standard_Integer num = nm0;
479     if (nm0 <= 0 && !entnul && yamod) num = model->Number(ent);
480     if (nm0 <= 0 && entnul) num = -1;    // Global
481 //  mesnum = mesnum0;
482 //    if (yamod) mesnum = (nm0 > 0 ? mesnum1 : mesnum2);
483
484     if (!titre)        S<<" **  " << Name() << "  **"<<endl;
485     titre = Standard_True;
486     S<<"Check:"; if(nb > 9 && i < 10) S<<" "; if (nb > 99 && i < 100) S<<" ";
487     S<<i;
488     if      (num <  0) S<<" -- Global Check"<<endl;
489     else if (num == 0) S<<" -- Entity n0 ??:";
490     else {
491       if (yamod) { S<<" -- Entity (n0:id) "; model->Print (ent,S); }
492       else       S<<" -- Entity n0 "<<num;
493 //      S<<" -- Entity n0 "<<num<<mesnum;
494 //      if (yamod) model->PrintLabel(ent,S);
495     }
496     if      (num >= 0 &&  entnul) S<<" (unknown Type)"<<endl;
497     else if (num >= 0 && !entnul) {
498       if (yamod) S<<"   Type:"<<model->TypeName(ent)<<endl;
499       else       S<<"   Type:"<<ent->DynamicType()->Name()<<endl;
500     }
501
502     nbch ++;
503     ach->Print (S, (failsonly ? 1 : 3));
504   }
505 //  if (nbch > 0)  S<<" ----  Checks : "<<nbch<<"  ----"<<endl;
506 }
507
508
509 //=======================================================================
510 //function : Destroy
511 //purpose  : 
512 //=======================================================================
513
514 void Interface_CheckIterator::Destroy ()
515 {
516   thecurr.Nullify();
517 }    // redevient standard