1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
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>
32 //=======================================================================
33 //function : StepData_StepReaderTool
35 //=======================================================================
36 StepData_StepReaderTool::StepData_StepReaderTool
37 (const Handle(StepData_StepReaderData)& reader,
38 const Handle(StepData_Protocol)& protocol)
39 : theglib(protocol) , therlib(protocol)
41 SetData(reader,protocol);
45 //=======================================================================
46 //function : Recognize
48 //=======================================================================
50 Standard_Boolean StepData_StepReaderTool::Recognize(const Standard_Integer num,
51 Handle(Interface_Check)& ach,
52 Handle(Standard_Transient)& ent)
54 // Handle(Standard_Transient) bid; // pas exploite
55 // return thereco->Evaluate(thetypes.Value(num),bid);
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);
63 // Pas de Recognizer : Reconnaissance par la librairie
64 return RecognizeByLib (num,theglib,therlib,ach,ent);
68 // .... Methodes de preparations propres a StepReaderTool ....
71 //=======================================================================
74 //=======================================================================
76 void StepData_StepReaderTool::Prepare
77 (const Handle(StepData_FileRecognizer)& reco, const Standard_Boolean optim)
84 //=======================================================================
87 //=======================================================================
89 void StepData_StepReaderTool::Prepare (const Standard_Boolean optim)
91 // SetEntityNumbers a ete mis du cote de ReaderData, because beaucoup acces
92 Standard_Boolean erh = ErrorHandle();
93 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
97 stepdat->SetEntityNumbers(optim);
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";
108 stepdat->SetEntityNumbers(optim);
114 // .... Gestion du Header : Preparation, lecture .... //
117 //=======================================================================
118 //function : PrepareHeader
120 //=======================================================================
122 void StepData_StepReaderTool::PrepareHeader
123 (const Handle(StepData_FileRecognizer)& reco)
125 Standard_Integer i = 0;
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();
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);
141 if (ent.IsNull()) ent = Protocol()->UnknownEntity();
142 stepdat->BindEntity(i,ent);
145 // Reste la Resolution des references : ne concerne que les sous-listes
146 // Assuree par ReaderData
147 stepdat->PrepareHeader();
151 // .... Methodes pour la lecture du Modele (apres preparation) .... //
154 //=======================================================================
155 //function : BeginRead
157 //=======================================================================
159 void StepData_StepReaderTool::BeginRead
160 (const Handle(Interface_InterfaceModel)& amodel)
162 Message_Messenger::StreamBuffer sout = Message::SendInfo();
163 DeclareAndCast(StepData_StepModel,model,amodel);
164 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
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());
178 if (ach->HasFailed() || ach->HasWarnings()) {
179 Handle(Interface_Check) mch = model->GlobalCheck();
180 mch->GetMessages(ach); model->SetGlobalCheck(mch);
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 sout << ach->CWarning(nf) << "\n";
191 if (ach->HasFailed()) {
192 Handle(Interface_Check) mch = model->GlobalCheck();
193 Standard_Integer nbmess = ach->NbFails();
194 sout << " Errors on Reading Header Entity N0."<<i<<":";
195 if (!ent.IsNull()) sout << ent->DynamicType()->Name() << std::endl;
196 for (Standard_Integer nf = 1; nf <= nbmess; nf ++)
197 sout << ach->CFail(nf) << "\n";
203 //=======================================================================
204 //function : AnalyseRecord
206 //=======================================================================
208 Standard_Boolean StepData_StepReaderTool::AnalyseRecord
209 (const Standard_Integer num,
210 const Handle(Standard_Transient)& anent,
211 Handle(Interface_Check)& acheck)
213 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
214 Handle(Interface_ReaderModule) imodule;
216 if (therlib.Select(anent,imodule,CN))
218 Handle(StepData_ReadWriteModule) module =
219 Handle(StepData_ReadWriteModule)::DownCast (imodule);
220 module->ReadStep(CN,stepdat,num,acheck,anent);
223 // Pas trouve : tenter UndefinedEntity de StepData
224 DeclareAndCast(StepData_UndefinedEntity,und,anent);
225 if (und.IsNull()) acheck->AddFail
226 ("# Entity neither Recognized nor set as UndefinedEntity from StepData #");
227 else und->ReadRecord(stepdat,num,acheck);
229 return (!acheck->HasFailed());
233 //=======================================================================
236 //=======================================================================
238 void StepData_StepReaderTool::EndRead
239 (const Handle(Interface_InterfaceModel)& amodel)
241 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
242 DeclareAndCast(StepData_StepModel,stepmodel,amodel);
243 if (stepmodel.IsNull()) return;
244 Standard_Integer i = 0;
245 while ( (i = stepdat->FindNextRecord(i)) != 0) {
246 stepmodel->SetIdentLabel(stepdat->BoundEntity(i),stepdat->RecordIdent(i));