0031501: Foundation Classes, Message_Printer - remove theToPutEndl argument
[occt.git] / src / TransferBRep / TransferBRep.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 <BRep_Builder.hxx>
16 #include <BRep_Tool.hxx>
17 #include <BRepLib.hxx>
18 #include <Interface_Check.hxx>
19 #include <Interface_CheckIterator.hxx>
20 #include <Interface_InterfaceModel.hxx>
21 #include <Interface_Macros.hxx>
22 #include <Message_Msg.hxx>
23 #include <Message_Printer.hxx>
24 #include <Standard_ErrorHandler.hxx>
25 #include <Standard_Failure.hxx>
26 #include <Standard_Transient.hxx>
27 #include <TopExp_Explorer.hxx>
28 #include <TopoDS.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <TopoDS_HShape.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <Transfer_Binder.hxx>
33 #include <Transfer_FinderProcess.hxx>
34 #include <Transfer_IteratorOfProcessForTransient.hxx>
35 #include <Transfer_SimpleBinderOfTransient.hxx>
36 #include <Transfer_TransientProcess.hxx>
37 #include <TransferBRep.hxx>
38 #include <TransferBRep_BinderOfShape.hxx>
39 #include <TransferBRep_ShapeBinder.hxx>
40 #include <TransferBRep_ShapeListBinder.hxx>
41 #include <TransferBRep_ShapeMapper.hxx>
42 #include <TransferBRep_TransferResultInfo.hxx>
43
44 #include <stdio.h>
45 //#include <TransferBRep_Analyzer.hxx>
46 static void  ShapeAppend
47   (const Handle(Transfer_Binder)& binder,
48    const Handle(TopTools_HSequenceOfShape)& shapes)
49 {
50   if (binder.IsNull()) return;
51   if (binder->IsKind(STANDARD_TYPE(TransferBRep_BinderOfShape))) {
52     DeclareAndCast(TransferBRep_BinderOfShape,shbind,binder);
53     if (shbind->HasResult()) shapes->Append (shbind->Result());
54   }
55   else if (binder->IsKind(STANDARD_TYPE(TransferBRep_ShapeListBinder))) {
56     DeclareAndCast(TransferBRep_ShapeListBinder,slbind,binder);
57     Standard_Integer i,nb = slbind->NbShapes();
58     for (i = 1; i <= nb; i ++) shapes->Append (slbind->Shape(i));
59   }
60   else if (binder->IsKind(STANDARD_TYPE(Transfer_SimpleBinderOfTransient))) {
61     DeclareAndCast(Transfer_SimpleBinderOfTransient,trbind,binder);
62     DeclareAndCast(TopoDS_HShape,hs,trbind->Result());
63     if (!hs.IsNull()) shapes->Append (hs->Shape());
64   }
65   Handle(Transfer_Binder) nextr = binder->NextResult();
66   if (!nextr.IsNull()) ShapeAppend (nextr,shapes);
67 }
68
69
70     TopoDS_Shape  TransferBRep::ShapeResult
71   (const Handle(Transfer_Binder)& binder)
72 {
73   TopoDS_Shape shape;
74   Handle(Transfer_Binder) bnd = binder;
75   while (!bnd.IsNull()) {
76     DeclareAndCast(TransferBRep_BinderOfShape,shb,bnd);
77     if (!shb.IsNull()) return shb->Result();
78     DeclareAndCast(Transfer_SimpleBinderOfTransient,hsb,bnd);
79     if (!hsb.IsNull()) {
80       Handle(TopoDS_HShape) hsp = GetCasted(TopoDS_HShape,hsb->Result());
81       if (!hsp.IsNull()) return hsp->Shape();
82     }
83     bnd = bnd->NextResult();
84   }
85   return shape;
86 }
87
88     TopoDS_Shape  TransferBRep::ShapeResult
89   (const Handle(Transfer_TransientProcess)& TP,
90    const Handle(Standard_Transient)& ent)
91 {
92   TopoDS_Shape shape;
93   Handle(Transfer_Binder) binder = TP->Find(ent);
94   if (binder.IsNull())  binder = GetCasted (Transfer_Binder,ent);
95   if (!binder.IsNull()) return TransferBRep::ShapeResult (binder);
96   DeclareAndCast(TopoDS_HShape,hsp,ent);
97   if (!hsp.IsNull()) return hsp->Shape();
98   return shape;
99 }
100
101
102     void  TransferBRep::SetShapeResult
103   (const Handle(Transfer_TransientProcess)& TP,
104    const Handle(Standard_Transient)& ent, const TopoDS_Shape& result)
105 {
106   if (result.IsNull() || ent.IsNull() || TP.IsNull()) return;
107   TP->Bind (ent,new TransferBRep_ShapeBinder(result));
108 }
109
110
111     Handle(TopTools_HSequenceOfShape)  TransferBRep::Shapes
112   (const Handle(Transfer_TransientProcess)& TP, const Standard_Boolean roots)
113 {
114   Handle(TopTools_HSequenceOfShape) shapes;
115   if (TP.IsNull()) return shapes;
116   shapes = new TopTools_HSequenceOfShape();
117
118   Transfer_IteratorOfProcessForTransient list = 
119     (roots ? TP->RootResult() : TP->CompleteResult());
120
121   for (list.Start(); list.More(); list.Next()) {
122     Handle(Transfer_Binder) binder = list.Value();
123     ShapeAppend (binder,shapes);
124   }
125   return shapes;
126 }
127
128     Handle(TopTools_HSequenceOfShape)  TransferBRep::Shapes
129   (const Handle(Transfer_TransientProcess)& TP,
130    const Handle(TColStd_HSequenceOfTransient)& list)
131 {
132   Handle(TopTools_HSequenceOfShape) shapes;
133   if (TP.IsNull() && list.IsNull()) return shapes;
134   shapes = new TopTools_HSequenceOfShape();
135
136   Standard_Integer ie, ne = list->Length();
137   for (ie = 1; ie <= ne; ie ++) {
138     Handle(Transfer_Binder) binder = TP->Find(list->Value(ie));
139     ShapeAppend (binder,shapes);
140   }
141
142   return shapes;
143 }
144
145
146     TopAbs_Orientation  TransferBRep::ShapeState
147   (const Handle(Transfer_FinderProcess)& FP, const TopoDS_Shape& shape)
148 {
149   if (FP.IsNull() || shape.IsNull()) return TopAbs_EXTERNAL;
150   Handle(TransferBRep_ShapeMapper) sm = new TransferBRep_ShapeMapper(shape);
151   Standard_Integer index = FP->MapIndex (sm);
152   if (index == 0) return TopAbs_EXTERNAL;
153   sm = Handle(TransferBRep_ShapeMapper)::DownCast(FP->Mapped(index));
154   if (sm.IsNull()) return TopAbs_EXTERNAL;
155   const TopoDS_Shape& mapped = sm->Value();
156 //  l egalite est assumee, on ne teste que l orientation
157   if (mapped.Orientation() != shape.Orientation()) return TopAbs_REVERSED;
158   return TopAbs_FORWARD;
159 }
160
161     Handle(Transfer_Binder)  TransferBRep::ResultFromShape
162   (const Handle(Transfer_FinderProcess)& FP, const TopoDS_Shape& shape)
163 {
164   Handle(Transfer_Binder) res;
165   if (FP.IsNull() || shape.IsNull()) return res;
166   Handle(TransferBRep_ShapeMapper) sm = new TransferBRep_ShapeMapper(shape);
167   return FP->Find (sm);
168 }
169
170     Handle(Standard_Transient)  TransferBRep::TransientFromShape
171   (const Handle(Transfer_FinderProcess)& FP, const TopoDS_Shape& shape)
172 {
173   Handle(Standard_Transient) res;
174   if (FP.IsNull() || shape.IsNull()) return res;
175   Handle(TransferBRep_ShapeMapper) sm = new TransferBRep_ShapeMapper(shape);
176   return FP->FindTransient (sm);
177 }
178
179
180     void  TransferBRep::SetTransientFromShape
181   (const Handle(Transfer_FinderProcess)& FP,
182    const TopoDS_Shape& shape, const Handle(Standard_Transient)& result)
183 {
184   if (FP.IsNull() || shape.IsNull()) return;
185   Handle(TransferBRep_ShapeMapper) sm = new TransferBRep_ShapeMapper(shape);
186   FP->BindTransient (sm,result);
187 }
188
189     Handle(TransferBRep_ShapeMapper)  TransferBRep::ShapeMapper
190   (const Handle(Transfer_FinderProcess)& FP,
191    const TopoDS_Shape& shape)
192 {
193   Handle(TransferBRep_ShapeMapper) mapper = new TransferBRep_ShapeMapper(shape);
194   Standard_Integer index = FP->MapIndex (mapper);
195   if (index == 0) return mapper;
196   return Handle(TransferBRep_ShapeMapper)::DownCast(FP->Mapped(index));
197 }
198
199 // Functions to collect transfer result information
200
201 //=======================================================================
202 //function : FillInfo
203 //purpose  : 
204 //=======================================================================
205
206 static void FillInfo (const Handle(Transfer_Binder)& Binder,
207                       const Handle(Interface_Check)& Check,
208                       const Handle(TransferBRep_TransferResultInfo)& Info)
209 {
210   Standard_Integer R = 0, RW = 0, RF = 0, RWF = 0, NR = 0, NRW = 0, NRF = 0, NRWF = 0;
211   if (Binder->HasResult())
212     if (Check->HasWarnings() && Check->HasFailed()) RWF++;
213     else if (Check->HasWarnings()) RW++;
214     else if (Check->HasFailed()) RF++;
215     else R++;
216   else 
217     if (Check->HasWarnings() && Check->HasFailed()) NRWF++;
218     else if (Check->HasWarnings()) NRW++;
219     else if (Check->HasFailed()) NRF++;
220     else NR++;
221   Info->Result()   += R;  Info->ResultWarning()   += RW;  Info->ResultFail()   += RF;  Info->ResultWarningFail()   += RWF;
222   Info->NoResult() += NR; Info->NoResultWarning() += NRW; Info->NoResultFail() += NRF; Info->NoResultWarningFail() += NRWF;
223 }     
224              
225 //=======================================================================
226 //function : TransferResultInfo
227 //purpose  : 
228 //=======================================================================
229
230  void TransferBRep::TransferResultInfo (const Handle(Transfer_TransientProcess)& TP,
231                                         const Handle(TColStd_HSequenceOfTransient)& EntityTypes,
232                                         Handle(TransferBRep_HSequenceOfTransferResultInfo)& InfoSeq)
233 {
234   // create output Sequence in accordance with required ShapeTypes
235   InfoSeq = new TransferBRep_HSequenceOfTransferResultInfo;
236   if (TP.IsNull() || EntityTypes.IsNull()) return;
237   Standard_Integer SeqLen = EntityTypes->Length();
238   Standard_Integer i; // svv Jan11 2000 : porting on DEC
239   for (i = 1; i <= SeqLen; i++) {
240     InfoSeq->Append (new TransferBRep_TransferResultInfo);
241   }
242   
243   // fill Sequence
244   Standard_Integer NbMapped = TP->NbMapped();
245   for (i = 1; i <= NbMapped; i++) {
246     Handle(Standard_Transient) Entity = TP->Mapped (i);
247
248     Handle(Transfer_Binder) Binder = TP->Find (Entity);
249     if (Binder.IsNull()) continue;
250     const Handle(Interface_Check) Check = Binder->Check ();
251     
252     // find appropriate element in the Sequence
253     for (Standard_Integer index = 1; index <= SeqLen; index++) {
254       if (Entity->IsKind (EntityTypes->Value(index)->DynamicType())) {
255         Handle(TransferBRep_TransferResultInfo) Info = InfoSeq->Value (index);
256         // fill element
257         FillInfo (Binder, Check, Info);
258       }
259     }
260   }
261 }
262
263 //=======================================================================
264 //function : TransferResultInfo
265 //purpose  : 
266 //=======================================================================
267
268  void TransferBRep::TransferResultInfo (const Handle(Transfer_FinderProcess)& FP,
269                                         const Handle(TColStd_HSequenceOfInteger)& ShapeTypes,
270                                         Handle(TransferBRep_HSequenceOfTransferResultInfo)& InfoSeq)
271 {
272   // create output Sequence in accordance with required ShapeTypes
273   InfoSeq = new TransferBRep_HSequenceOfTransferResultInfo;
274   if (FP.IsNull() || ShapeTypes.IsNull()) return;
275   Standard_Integer SeqLen = ShapeTypes->Length();
276   Standard_Integer i; // svv Jan11 2000 : porting on DEC
277   for (i = 1; i <= SeqLen; i++) {
278     InfoSeq->Append (new TransferBRep_TransferResultInfo);
279   }
280   
281   // fill Sequence
282   Standard_Integer NbMapped = FP->NbMapped();
283   for (i = 1; i <= NbMapped; i++) {
284     Handle(TransferBRep_ShapeMapper) Mapper = Handle(TransferBRep_ShapeMapper)::DownCast (FP->Mapped (i));
285     Handle(Transfer_Binder) Binder = FP->Find (Mapper);
286     if (Binder.IsNull()) continue;
287     const Handle(Interface_Check) Check = Binder->Check ();
288     
289     TopoDS_Shape S = Mapper->Value();
290     TopAbs_ShapeEnum ShapeType = S.ShapeType();
291     
292     // find appropriate element in the Sequence
293     for (Standard_Integer index = 1; index <= SeqLen; index++) {
294 //JR/Hp :
295       TopAbs_ShapeEnum CurrentType = (TopAbs_ShapeEnum)ShapeTypes->Value (index);
296 //      TopAbs_ShapeEnum CurrentType = (TopAbs_ShapeEnum)ShapeTypes->Value (index);
297       if (CurrentType == ShapeType || CurrentType == TopAbs_SHAPE) {
298         Handle(TransferBRep_TransferResultInfo) Info = InfoSeq->Value (index);
299         // fill element
300         FillInfo (Binder, Check, Info);
301       }
302     }
303   }
304 }
305
306 //  ########  CHECK LOURD  ########
307
308 // # # # # # #    Enchainement General du CHECK LOURD    # # # # # #
309
310 /*
311 Interface_CheckIterator TransferBRep::BRepCheck
312   (const TopoDS_Shape& shape, const Standard_Integer lev)
313 {
314   Interface_CheckIterator result;
315   TransferBRep_Analyzer ana;
316   ana.Check (shape,lev);
317   return ana.CheckResult ();
318 }
319 */
320
321 //  ###  conversion resultat -> starting
322
323     Interface_CheckIterator  TransferBRep::ResultCheckList
324   (const Interface_CheckIterator& chl,
325    const Handle(Transfer_FinderProcess)& FP,
326    const Handle(Interface_InterfaceModel)& model)
327 {
328   Interface_CheckIterator  nchl;
329   if (FP.IsNull() || model.IsNull()) return nchl;
330   nchl.SetModel(model);
331   for (chl.Start(); chl.More(); chl.Next()) {
332     Standard_Integer num = 0;
333     Handle(Interface_Check) ach = chl.Value();
334     if (ach->NbFails() + ach->NbWarnings() == 0) continue;
335     DeclareAndCast(Transfer_Finder,starting,ach->Entity());
336     Handle(Standard_Transient) ent;
337     if (!starting.IsNull()) ent = FP->FindTransient(starting);
338     if (!ent.IsNull()) {
339       ach->SetEntity(ent);
340       num = model->Number(ent);
341     }
342     nchl.Add (ach,num);
343   }
344   return nchl;
345 }
346
347     Handle(TColStd_HSequenceOfTransient)  TransferBRep::Checked
348   (const Interface_CheckIterator& chl, const Standard_Boolean alsoshapes)
349 {
350   Handle(TColStd_HSequenceOfTransient) ls = new TColStd_HSequenceOfTransient();
351   for (chl.Start(); chl.More(); chl.Next()) {
352     const Handle(Interface_Check) ach = chl.Value();
353     if (ach->NbFails() + ach->NbWarnings() == 0) continue;
354     Handle(Standard_Transient) ent = ach->Entity();
355     if (ent.IsNull()) continue;
356     if (!alsoshapes) {
357       if (ent->IsKind(STANDARD_TYPE(TransferBRep_BinderOfShape)) ||
358           ent->IsKind(STANDARD_TYPE(TopoDS_HShape)) ||
359           ent->IsKind(STANDARD_TYPE(TransferBRep_ShapeMapper)) ) continue;
360     }
361     ls->Append(ent);
362   }
363   return ls;
364 }
365
366     Handle(TopTools_HSequenceOfShape)  TransferBRep::CheckedShapes
367   (const Interface_CheckIterator& chl)
368 {
369   Handle(TopTools_HSequenceOfShape) ls = new TopTools_HSequenceOfShape();
370   for (chl.Start(); chl.More(); chl.Next()) {
371     const Handle(Interface_Check) ach = chl.Value();
372     if (ach->NbFails() + ach->NbWarnings() == 0) continue;
373     Handle(Standard_Transient) ent = ach->Entity();
374     if (ent.IsNull()) continue;
375     DeclareAndCast(TopoDS_HShape,hs,ent);
376     DeclareAndCast(TransferBRep_BinderOfShape,sb,ent);
377     DeclareAndCast(TransferBRep_ShapeMapper,sm,ent);
378     if (!hs.IsNull()) ls->Append (hs->Shape());
379     if (!sb.IsNull()) ls->Append (sb->Result());
380     if (!sm.IsNull()) ls->Append (sm->Value());
381   }
382   return ls;
383 }
384
385     Interface_CheckIterator  TransferBRep::CheckObject
386   (const Interface_CheckIterator& chl, const Handle(Standard_Transient)& obj)
387 {
388   TopoDS_Shape S;
389   DeclareAndCast(TopoDS_HShape,hs,obj);
390   DeclareAndCast(TransferBRep_BinderOfShape,sb,obj);
391   DeclareAndCast(TransferBRep_ShapeMapper,sm,obj);
392   if (!hs.IsNull()) S = hs->Shape();
393   if (!sb.IsNull()) S = sb->Result();
394   if (!sm.IsNull()) S = sm->Value();
395   Interface_CheckIterator nchl;
396
397   for (chl.Start(); chl.More(); chl.Next()) {
398     const Handle(Interface_Check) ach = chl.Value();
399     if (ach->NbFails() + ach->NbWarnings() == 0) continue;
400     Handle(Standard_Transient) ent = ach->Entity();
401     if (ent.IsNull()) continue;
402     if (S.IsNull()) {
403       if (ent == obj) {
404         Handle(Interface_Check) bch(ach);  bch->SetEntity(ent);
405         nchl.Add (bch,0);
406       }
407     } else {
408       TopoDS_Shape sh;
409       DeclareAndCast(TopoDS_HShape,hsh,ent);
410       DeclareAndCast(TransferBRep_BinderOfShape,sbs,ent);
411       DeclareAndCast(TransferBRep_ShapeMapper,smp,ent);
412       if (!hsh.IsNull()) sh = hsh->Shape();
413       if (!sbs.IsNull()) sh = sbs->Result();
414       if (!smp.IsNull()) sh = smp->Value();
415       if (sh == S) {
416         Handle(Interface_Check) bch(ach);  bch->SetEntity(ent);
417         nchl.Add (bch,0);
418       }
419     }
420   }
421   return nchl;
422 }
423
424 //=======================================================================
425 //function : PrintResultInfo
426 //purpose  : 
427 //=======================================================================
428
429 void TransferBRep::PrintResultInfo(const Handle(Message_Printer)& Printer,
430                                    const Message_Msg& Header,
431                                    const Handle(TransferBRep_TransferResultInfo)& ResultInfo,
432                                    const Standard_Boolean printEmpty)
433 {
434   Standard_Integer R, RW, RF, RWF, NR, NRW, NRF, NRWF;
435   R    = ResultInfo->Result();
436   RW   = ResultInfo->ResultWarning();
437   RF   = ResultInfo->ResultFail();
438   RWF  = ResultInfo->ResultWarningFail();
439   NR   = ResultInfo->NoResult();
440   NRW  = ResultInfo->NoResultWarning();
441   NRF  = ResultInfo->NoResultFail();
442   NRWF = ResultInfo->NoResultWarningFail();
443   
444   Message_Msg aLocalHeader = Header;
445   Printer->Send (aLocalHeader, Message_Info);
446   
447   Message_Msg EPMSG30 ("Result.Print.MSG30"); //    Result: %d
448   EPMSG30.Arg (R);
449   Printer->Send (EPMSG30, Message_Info);
450   if(printEmpty || (RW > 0 )) {
451     Message_Msg EPMSG32 ("Result.Print.MSG32"); //    Result + Warning(s): %d
452     EPMSG32.Arg (RW);
453     Printer->Send (EPMSG32, Message_Info);
454   }
455   if(printEmpty || (RF > 0 )) {
456     Message_Msg EPMSG34 ("Result.Print.MSG34"); //    Result + Fail(s): %d
457     EPMSG34.Arg (RF);
458     Printer->Send (EPMSG34, Message_Info);
459   }
460   if(printEmpty || (RWF > 0)) {
461     Message_Msg EPMSG36 ("Result.Print.MSG36"); //    Result + Warning(s) + Fail(s): %d
462     EPMSG36.Arg (RWF);
463     Printer->Send (EPMSG36, Message_Info);
464   }
465   Message_Msg EPMSG38 ("Result.Print.MSG38"); //    TOTAL Result: %d
466   EPMSG38.Arg (R + RW + RF + RWF);
467   Printer->Send (EPMSG38, Message_Info);
468   if(printEmpty || (NR > 0)) {
469     Message_Msg EPMSG40 ("Result.Print.MSG40"); //    No Result: %d
470     EPMSG40.Arg (NR);
471     Printer->Send (EPMSG40, Message_Info);
472   }
473   if(printEmpty || (NRW > 0)) {
474     Message_Msg EPMSG42 ("Result.Print.MSG42"); //    No Result + Warning(s): %d
475     EPMSG42.Arg (NRW);
476     Printer->Send (EPMSG42, Message_Info);
477   }
478   if(printEmpty || (NRF > 0)) {
479     Message_Msg EPMSG44 ("Result.Print.MSG44"); //    No Result + Fail(s): %d
480     EPMSG44.Arg (NRF);
481     Printer->Send (EPMSG44, Message_Info);
482   }
483   if(printEmpty || (NRWF > 0)) {
484     Message_Msg EPMSG46 ("Result.Print.MSG46"); //    No Result + Warning(s) + Fail(s): %d
485     EPMSG46.Arg (NRWF);
486     Printer->Send (EPMSG46, Message_Info);
487   }
488   
489   Message_Msg EPMSG48 ("Result.Print.MSG48"); //    TOTAL No Result: %d
490   EPMSG48.Arg (NR + NRW + NRF + NRWF);
491   Printer->Send (EPMSG48, Message_Info);
492
493 }