0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / IGESControl / IGESControl_Reader.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 //gka 06.01.99 S3767
15 //abv 10.04.99 S4136: eliminate using BRepAPI::Precision()
16
17 #include <BRepLib.hxx>
18 #include <IFSelect_CheckCounter.hxx>
19 #include <IFSelect_Functions.hxx>
20 #include <IGESControl_Controller.hxx>
21 #include <IGESControl_Reader.hxx>
22 #include <IGESData_FileProtocol.hxx>
23 #include <IGESData_IGESEntity.hxx>
24 #include <IGESData_IGESModel.hxx>
25 #include <IGESToBRep_Actor.hxx>
26 #include <Interface_Check.hxx>
27 #include <Interface_CheckIterator.hxx>
28 #include <Interface_InterfaceModel.hxx>
29 #include <Interface_Macros.hxx>
30 #include <Interface_ShareFlags.hxx>
31 #include <Interface_Static.hxx>
32 #include <Message_Messenger.hxx>
33 #include <Message_Msg.hxx>
34 #include <NCollection_DataMap.hxx>
35 #include <OSD_Timer.hxx>
36 #include <ShapeExtend_Explorer.hxx>
37 #include <ShapeFix_ShapeTolerance.hxx>
38 #include <TColStd_HSequenceOfInteger.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <Transfer_ActorOfTransientProcess.hxx>
41 #include <Transfer_Binder.hxx>
42 #include <Transfer_IteratorOfProcessForTransient.hxx>
43 #include <Transfer_TransferOutput.hxx>
44 #include <Transfer_TransientProcess.hxx>
45 #include <TransferBRep.hxx>
46 #include <XSControl_Controller.hxx>
47 #include <XSControl_TransferReader.hxx>
48 #include <XSControl_WorkSession.hxx>
49
50 #include <stdio.h>
51 // S3767 dce 18/01/1999
52 //Transfer_Iterator.hxx>
53 // add of stdio.h for NT compilation
54 //=======================================================================
55 //function : IGESControl_Reader
56 //purpose  : 
57 //=======================================================================
58 IGESControl_Reader::IGESControl_Reader ()
59 {
60   IGESControl_Controller::Init();
61   SetWS (new XSControl_WorkSession);
62   SetNorm("IGES");
63   Standard_Integer onlyvisible = Interface_Static::IVal("read.iges.onlyvisible");
64   theReadOnlyVisible = (onlyvisible == 1);
65 }
66
67
68 //=======================================================================
69 //function : IGESControl_Reader
70 //purpose  : 
71 //=======================================================================
72
73 IGESControl_Reader::IGESControl_Reader
74   (const Handle(XSControl_WorkSession)& WS, const Standard_Boolean scratch)
75 {
76   IGESControl_Controller::Init();
77   SetWS (WS,scratch);
78   SetNorm ("IGES");
79   Standard_Integer onlyvisible = Interface_Static::IVal("read.iges.onlyvisible");
80   theReadOnlyVisible = (onlyvisible == 1);
81  }
82
83
84 //=======================================================================
85 //function : IGESModel
86 //purpose  : 
87 //=======================================================================
88
89 Handle(IGESData_IGESModel) IGESControl_Reader::IGESModel () const
90 {
91   return Handle(IGESData_IGESModel)::DownCast(Model());
92 }
93
94
95
96 //=======================================================================
97 //function : NbRootsForTransfer
98 //purpose  : 
99 //=======================================================================
100
101 Standard_Integer  IGESControl_Reader::NbRootsForTransfer()
102 {
103   if (therootsta) return theroots.Length();
104   therootsta = Standard_True;
105   
106   Handle(IGESData_IGESModel) model = IGESModel(); 
107   if (model.IsNull()) return 0;
108   
109   Handle(XSControl_WorkSession) session = WS();
110   Handle(Interface_Protocol) protocol = session->Protocol();
111   Handle(XSControl_Controller) controller = session->NormAdaptor();
112   Handle(Transfer_ActorOfTransientProcess) actor = controller->ActorRead(model);
113   
114   Interface_ShareFlags SH (model,protocol);
115    
116   // sln 11.06.2002 OCC448
117   Interface_Static::SetIVal("read.iges.onlyvisible",theReadOnlyVisible);
118   
119   Standard_Integer nb = model->NbEntities();
120   for (Standard_Integer i = 1; i <= nb; i ++) {
121     Handle(IGESData_IGESEntity) ent = model->Entity(i);
122     if ( SH.IsShared(ent) || ! actor->Recognize (ent) ) continue;
123     // on ajoute un traitement pour ne prendre que les entites visibles
124     if ( ! theReadOnlyVisible || ent->BlankStatus() == 0 ) {
125       theroots.Append(ent);
126     }
127   }
128   
129   return theroots.Length();
130 }
131
132 //  ####    Reliquat de methodes a reprendre    ####
133
134 //=======================================================================
135 // Function : PrintTransferInfo
136 // Purpose  : Print statistics information on transfer using MoniTool message management
137 // Created  : 18/01/98 DCE for S3767
138 // Modified : 
139 //=======================================================================
140
141 void  IGESControl_Reader::PrintTransferInfo
142   (const IFSelect_PrintFail failsonly, const IFSelect_PrintCount mode) const
143 {
144   Standard_Integer nbWarn = 0, nbFail= 0, nbEntities =0, nbRoots = 0, nbResults = 0;  
145   const Handle(Transfer_TransientProcess) &TP = WS()->TransferReader()->TransientProcess();
146   Handle(Message_Messenger) TF = TP->Messenger();
147   const Handle(Interface_InterfaceModel) &model = TP->Model();
148   if (! model.IsNull()) {
149     nbEntities = model->NbEntities();
150     nbRoots = TP->NbRoots();
151     //nbResults = TP->NbMapped();
152     Transfer_IteratorOfProcessForTransient iterTrans = TP->RootResult(Standard_True);
153     NCollection_DataMap<TCollection_AsciiString, Standard_Integer> aMapCountResult;
154     NCollection_DataMap<TCollection_AsciiString, Standard_Integer> aMapCountMapping;
155     for (iterTrans.Start(); iterTrans.More() ; iterTrans.Next() ) {
156       nbResults++;
157       // Init for dicoCountResult for IFSelect_ResultCount
158       if ( mode == IFSelect_ResultCount ) {
159         char mess[300];
160         const Handle(Transfer_Binder) aBinder = iterTrans.Value();
161         sprintf(mess,"\t%s",aBinder->ResultTypeName());
162         if (aMapCountResult.IsBound(mess))
163           aMapCountResult.ChangeFind(mess)++;
164         else
165           aMapCountResult.Bind(mess,1);
166       }
167       // Init for dicoCountMapping for IFSelect_Mapping
168       else if ( mode == IFSelect_Mapping ) {
169         char mess[300];
170         const Handle(Transfer_Binder) aBinder = iterTrans.Value();
171         DeclareAndCast(IGESData_IGESEntity,igesEnt,iterTrans.Starting());
172
173         sprintf(mess,"%d\t%d\t%s\t%s", igesEnt->TypeNumber(), igesEnt->FormNumber(),
174         "%d", aBinder->ResultTypeName());
175         //std::cout << mess << std::endl;
176         if (aMapCountMapping.IsBound(mess))
177           aMapCountMapping.ChangeFind(mess)++;
178         else
179           aMapCountMapping.Bind(mess, 1);
180       }
181     }
182
183     Interface_CheckIterator checkIterator = TP->CheckList(Standard_False);
184     NCollection_DataMap<TCollection_AsciiString, Standard_Integer> aMapCount;
185     NCollection_DataMap<TCollection_AsciiString, Handle(TColStd_HSequenceOfInteger)> aMapList;
186     // Init the dicoCount dicoList and nbWarn ,nb Fail.
187     for(checkIterator.Start(); checkIterator.More(); checkIterator.Next() ) {
188       char mess[300];
189       const Handle(Interface_Check) aCheck = checkIterator.Value(); 
190       Handle(Standard_Transient) ent = model->Value(checkIterator.Number());
191       DeclareAndCast(IGESData_IGESEntity,igesEnt,ent);
192       Standard_Integer type = igesEnt->TypeNumber(), form = igesEnt->FormNumber();
193       Standard_Integer nw = aCheck->NbWarnings(), nf = aCheck->NbFails(), i;
194       for(i = 1; (failsonly==IFSelect_FailAndWarn) && (i<= nw); i++) {
195         sprintf(mess,"\t W\t%d\t%d\t%s",type,form,aCheck->CWarning(i));
196         if (aMapCount.IsBound(mess))
197           aMapCount.ChangeFind(mess)++;
198         else
199           aMapCount.Bind(mess, 1);
200
201         Handle(TColStd_HSequenceOfInteger) alist;
202         if (aMapList.IsBound(mess))
203           alist = aMapList.ChangeFind(mess);
204         else {
205           alist = new TColStd_HSequenceOfInteger();
206           aMapList.Bind(mess, alist);
207         }
208         alist->Append(model->Number(igesEnt)*2-1);
209       }
210       for(i = 1; i<= nf; i++) {
211         sprintf(mess,"\t F\t%d\t%d\t%s",type,form,aCheck->CFail(i));
212         // TF << mess << std::endl;
213         if (aMapCount.IsBound(mess))
214           aMapCount.ChangeFind(mess)++;
215         else
216           aMapCount.Bind(mess, 1);
217         Handle(TColStd_HSequenceOfInteger) alist;
218         if (aMapList.IsBound(mess))
219           alist = aMapList.ChangeFind(mess);
220         else {
221           alist = new TColStd_HSequenceOfInteger();
222           aMapList.Bind(mess, alist);
223         }
224         alist->Append(model->Number(igesEnt)*2-1);
225       }
226       nbWarn += nw;
227       nbFail += nf;
228     }
229     Message_Msg msg3000("IGES_3000");  // *************************
230     TF->Send (msg3000, Message_Info); //smh#14
231     
232     switch (mode) {
233     case IFSelect_GeneralInfo : {
234       Message_Msg msg3005("IGES_3005");TF->Send(msg3005, Message_Info);
235       Message_Msg msg3010("IGES_3010");msg3010.Arg(nbEntities);TF->Send(msg3010, Message_Info);
236       Message_Msg msg3011("IGES_3011");msg3011.Arg(nbRoots);TF->Send(msg3011, Message_Info);      
237       Message_Msg msg3015("IGES_3015");msg3015.Arg(nbResults);TF->Send(msg3015, Message_Info);
238       Message_Msg msg3020("IGES_3020");msg3020.Arg(nbWarn);TF->Send(msg3020, Message_Info);
239       Message_Msg msg3025("IGES_3025");msg3025.Arg(nbFail);TF->Send(msg3025, Message_Info);
240       break;
241     }
242     case IFSelect_CountByItem : 
243     case IFSelect_ListByItem : {
244       Message_Msg msg3030("IGES_3030");
245       TF->Send(msg3030, Message_Info);
246       NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator aMapCountIter(aMapCount);
247       NCollection_DataMap<TCollection_AsciiString, Handle(TColStd_HSequenceOfInteger)>::Iterator aMapListIter(aMapList);
248       for(; aMapCountIter.More() && aMapListIter.More();
249             aMapCountIter.Next(), aMapListIter.Next()) {
250         Message_Messenger::StreamBuffer aSender = TF->SendInfo();
251         aSender << aMapCountIter.Value() << aMapCountIter.Key() << std::endl;
252         if (mode == IFSelect_ListByItem) {
253           Handle(TColStd_HSequenceOfInteger) entityList = aMapListIter.Value();
254           Standard_Integer length = entityList->Length();
255           Message_Msg msg3035("IGES_3035");
256           TF->Send(msg3035, Message_Info);
257           char line[80];
258           sprintf(line, "\t\t\t");
259           aSender << line;
260           Standard_Integer nbInLine = 0;
261           for (Standard_Integer i = 1; i <= length; i++) {
262             // IDT_Out << (entityList->Value(i)) << " ";
263             sprintf(line, "\t %d", entityList->Value(i));
264             aSender << line;
265             if (++nbInLine == 6) {
266               nbInLine = 0;
267               sprintf(line, "\n\t\t\t");
268               aSender << line;
269             }
270           }
271           aSender << std::endl;
272         }
273       }
274       break;
275     }
276     case IFSelect_ResultCount : { 
277       Message_Msg msg3040("IGES_3040");TF->Send(msg3040, Message_Info);
278       Message_Msg msg3011("IGES_3011");msg3011.Arg(nbRoots);TF->Send(msg3011, Message_Info);      
279       Message_Msg msg3015("IGES_3015");msg3015.Arg(nbResults);TF->Send(msg3015, Message_Info);
280       Message_Msg msg3045("IGES_3045");TF->Send(msg3045, Message_Info);
281
282       NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator aMapIter(aMapCountResult);
283       for (; aMapIter.More(); aMapIter.Next())
284       {
285         TF->SendInfo() << aMapIter.Key() << aMapIter.Value() << std::endl;
286       }
287       break;
288     }
289     case IFSelect_Mapping : { 
290       Message_Msg msg3040("IGES_3050");TF->Send(msg3040, Message_Info);
291       Message_Msg msg3011("IGES_3011");msg3011.Arg(nbRoots);TF->Send(msg3011, Message_Info);      
292       Message_Msg msg3015("IGES_3015");msg3015.Arg(nbResults);TF->Send(msg3015, Message_Info);
293       Message_Msg msg3045("IGES_3055");TF->Send(msg3045, Message_Info);
294       // Add failed entities in dicoCountMapping
295       if (nbRoots!=nbResults) {
296         for (Standard_Integer i = 1; i <= nbRoots; i++) {
297           DeclareAndCast(IGESData_IGESEntity, root, TP->Root(i));
298           if (!TP->IsBound(root)) {
299             char mess[300];
300
301             sprintf(mess, "%d\t%d \t%s\t%s", root->TypeNumber(), root->FormNumber(),
302               "%d", "Failed");
303             //std::cout << mess << std::endl;
304             if (aMapCountMapping.IsBound(mess))
305               aMapCountMapping.ChangeFind(mess)++;
306             else
307               aMapCountMapping.Bind(mess, 1);
308           }
309         }
310       }
311       NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator aMapCountIter(aMapCountMapping);
312       for(; aMapCountIter.More(); aMapCountIter.Next()) {
313         char mess[80];
314         sprintf(mess, aMapCountIter.Key().ToCString(), aMapCountIter.Value());
315         TF->SendInfo() << mess << std::endl; //dicoCountIter.Value() << dicoCountIter.Name() << std::endl;
316       }
317       break;
318     }
319     default: break;
320     }
321   }
322 }