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 <Interface_Check.hxx>
16 #include <Interface_InterfaceModel.hxx>
17 #include <Interface_Macros.hxx>
18 #include <Message.hxx>
19 #include <Message_Messenger.hxx>
20 #include <Standard_ErrorHandler.hxx>
21 #include <Standard_Failure.hxx>
22 #include <Standard_Transient.hxx>
23 #include <StepData_FileRecognizer.hxx>
24 #include <StepData_Protocol.hxx>
25 #include <StepData_ReadWriteModule.hxx>
26 #include <StepData_StepModel.hxx>
27 #include <StepData_StepReaderData.hxx>
28 #include <StepData_StepReaderTool.hxx>
29 #include <StepData_UndefinedEntity.hxx>
30 #include <TCollection_AsciiString.hxx>
31 
32 //=======================================================================
33 //function : StepData_StepReaderTool
34 //purpose  :
35 //=======================================================================
StepData_StepReaderTool(const Handle (StepData_StepReaderData)& reader,const Handle (StepData_Protocol)& protocol)36 StepData_StepReaderTool::StepData_StepReaderTool
37   (const Handle(StepData_StepReaderData)& reader,
38    const Handle(StepData_Protocol)& protocol)
39 :  theglib(protocol) , therlib(protocol)
40 {
41   SetData(reader,protocol);
42 }
43 
44 
45 //=======================================================================
46 //function : Recognize
47 //purpose  :
48 //=======================================================================
49 
Recognize(const Standard_Integer num,Handle (Interface_Check)& ach,Handle (Standard_Transient)& ent)50 Standard_Boolean StepData_StepReaderTool::Recognize(const Standard_Integer num,
51                                                     Handle(Interface_Check)& ach,
52                                                     Handle(Standard_Transient)& ent)
53 {
54 //  Handle(Standard_Transient) bid;  // pas exploite
55 //  return thereco->Evaluate(thetypes.Value(num),bid);
56 
57 //  Recognizer : C est lui qui assure la Reconnaissance (-> Liste limitative)
58   if (!thereco.IsNull()) {
59     DeclareAndCast(StepData_StepReaderData,stepdat,Data());
60     return thereco->Evaluate(stepdat->RecordType(num),ent);
61   }
62 
63 //  Pas de Recognizer : Reconnaissance par la librairie
64   return RecognizeByLib (num,theglib,therlib,ach,ent);
65 }
66 
67 
68 //  ....         Methodes de preparations propres a StepReaderTool         ....
69 
70 
71 //=======================================================================
72 //function : Prepare
73 //purpose  :
74 //=======================================================================
75 
Prepare(const Handle (StepData_FileRecognizer)& reco,const Standard_Boolean optim)76 void StepData_StepReaderTool::Prepare
77   (const Handle(StepData_FileRecognizer)& reco, const Standard_Boolean optim)
78 {
79   thereco = reco;
80   Prepare(optim);
81 }
82 
83 
84 //=======================================================================
85 //function : Prepare
86 //purpose  :
87 //=======================================================================
88 
Prepare(const Standard_Boolean optim)89 void StepData_StepReaderTool::Prepare (const Standard_Boolean optim)
90 {
91 //   SetEntityNumbers a ete mis du cote de ReaderData, because beaucoup acces
92   Standard_Boolean erh = ErrorHandle();
93   DeclareAndCast(StepData_StepReaderData,stepdat,Data());
94   if (erh) {
95     try {
96       OCC_CATCH_SIGNALS
97       stepdat->SetEntityNumbers(optim);
98       SetEntities();
99     }
100     catch(Standard_Failure const& anException) {
101       Message_Messenger::StreamBuffer sout = Message::SendInfo();
102       sout << " Exception Raised during Preparation :\n";
103       sout << anException.GetMessageString();
104       sout << "\n Now, trying to continue, but with presomption of failure\n";
105     }
106   }
107   else {
108     stepdat->SetEntityNumbers(optim);
109     SetEntities();
110   }
111 }
112 
113 
114 // ....            Gestion du Header : Preparation, lecture            .... //
115 
116 
117 //=======================================================================
118 //function : PrepareHeader
119 //purpose  :
120 //=======================================================================
121 
PrepareHeader(const Handle (StepData_FileRecognizer)& reco)122 void StepData_StepReaderTool::PrepareHeader
123   (const Handle(StepData_FileRecognizer)& reco)
124 {
125   Standard_Integer i = 0;
126 
127 // Reconnaissance des types
128   DeclareAndCast(StepData_StepReaderData,stepdat,Data());
129   while ( (i = stepdat->FindNextHeaderRecord(i)) != 0) {
130     Handle(Standard_Transient) ent;
131 //  On a donne un Recognizer : il fixe une liste limitative de types reconnus
132     if (!reco.IsNull()) {
133       if (!reco->Evaluate(stepdat->RecordType(i),ent)) {
134 	ent = Protocol()->UnknownEntity();
135       }
136     } else {
137 //  Pas de Recognizer : Reconnaissance par la librairie
138       Handle(Interface_Check) ach = new Interface_Check;    // faudrait le lister ... ?
139       RecognizeByLib (i,theglib,therlib,ach,ent);
140     }
141     if (ent.IsNull()) ent = Protocol()->UnknownEntity();
142     stepdat->BindEntity(i,ent);
143   }
144 
145 //  Reste la Resolution des references : ne concerne que les sous-listes
146 //  Assuree par ReaderData
147   stepdat->PrepareHeader();
148 }
149 
150 
151 // ....   Methodes pour la lecture du Modele (apres preparation)   .... //
152 
153 
154 //=======================================================================
155 //function : BeginRead
156 //purpose  :
157 //=======================================================================
158 
BeginRead(const Handle (Interface_InterfaceModel)& amodel)159 void StepData_StepReaderTool::BeginRead
160   (const Handle(Interface_InterfaceModel)& amodel)
161 {
162   Message_Messenger::StreamBuffer sout = Message::SendTrace();
163   DeclareAndCast(StepData_StepModel,model,amodel);
164   DeclareAndCast(StepData_StepReaderData,stepdat,Data());
165 
166   model->ClearHeader();
167   model->SetGlobalCheck(stepdat->GlobalCheck());
168   Standard_Integer i = 0;
169   while ( (i = stepdat->FindNextHeaderRecord(i)) != 0) {
170     Handle(Standard_Transient) ent = stepdat->BoundEntity(i);
171     Handle(Interface_Check) ach = new Interface_Check(ent);
172     AnalyseRecord(i,ent,ach);
173     if (ent->IsKind(STANDARD_TYPE(StepData_UndefinedEntity))) {
174       TCollection_AsciiString mess("Header Entity not Recognized, StepType: ");
175       mess.AssignCat(stepdat->RecordType(i));
176       ach->AddWarning(mess.ToCString());
177     }
178     if (ach->HasFailed() || ach->HasWarnings()) {
179       Handle(Interface_Check) mch = model->GlobalCheck();
180       mch->GetMessages(ach); model->SetGlobalCheck(mch);
181     }
182     model->AddHeaderEntity(ent);
183     if (ach->HasWarnings()) {
184       Handle(Interface_Check) mch = model->GlobalCheck();
185       Standard_Integer nbmess = ach->NbWarnings();
186       sout<<nbmess<<" Warnings on Reading Header Entity N0."<<i<<":";
187       if (!ent.IsNull()) sout << ent->DynamicType()->Name() << std::endl;
188       for (Standard_Integer nf = 1; nf <= nbmess; nf++)
189       {
190         sout << ach->CWarning(nf) << "\n";
191       }
192     }
193     if (ach->HasFailed()) {
194       Handle(Interface_Check) mch = model->GlobalCheck();
195       Standard_Integer nbmess = ach->NbFails();
196       sout << " Fails on Reading Header Entity N0." << i << ":";
197       if (!ent.IsNull()) sout << ent->DynamicType()->Name() << std::endl;
198       for (Standard_Integer nf = 1; nf <= nbmess; nf++)
199       {
200         sout << ach->CFail(nf) << "\n";
201       }
202     }
203   }
204 }
205 
206 
207 //=======================================================================
208 //function : AnalyseRecord
209 //purpose  :
210 //=======================================================================
211 
AnalyseRecord(const Standard_Integer num,const Handle (Standard_Transient)& anent,Handle (Interface_Check)& acheck)212 Standard_Boolean StepData_StepReaderTool::AnalyseRecord
213   (const Standard_Integer num,
214    const Handle(Standard_Transient)& anent,
215    Handle(Interface_Check)& acheck)
216 {
217   DeclareAndCast(StepData_StepReaderData,stepdat,Data());
218   Handle(Interface_ReaderModule) imodule;
219   Standard_Integer CN;
220   if (therlib.Select(anent,imodule,CN))
221   {
222     Handle(StepData_ReadWriteModule) module =
223       Handle(StepData_ReadWriteModule)::DownCast (imodule);
224     module->ReadStep(CN,stepdat,num,acheck,anent);
225   }
226   else {
227 //  Pas trouve : tenter UndefinedEntity de StepData
228     DeclareAndCast(StepData_UndefinedEntity,und,anent);
229     if (und.IsNull()) acheck->AddFail
230       ("# Entity neither Recognized nor set as UndefinedEntity from StepData #");
231     else und->ReadRecord(stepdat,num,acheck);
232   }
233   return (!acheck->HasFailed());
234 }
235 
236 
237 //=======================================================================
238 //function : EndRead
239 //purpose  :
240 //=======================================================================
241 
EndRead(const Handle (Interface_InterfaceModel)& amodel)242 void StepData_StepReaderTool::EndRead
243   (const Handle(Interface_InterfaceModel)& amodel)
244 {
245   DeclareAndCast(StepData_StepReaderData,stepdat,Data());
246   DeclareAndCast(StepData_StepModel,stepmodel,amodel);
247   if (stepmodel.IsNull()) return;
248   Standard_Integer i = 0;
249   while ( (i = stepdat->FindNextRecord(i)) != 0) {
250     stepmodel->SetIdentLabel(stepdat->BoundEntity(i),stepdat->RecordIdent(i));
251   }
252 }
253