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.
14 #include <StepData_StepReaderTool.ixx>
15 #include <Standard_ErrorHandler.hxx>
16 #include <Standard_Failure.hxx>
17 #include <StepData_StepModel.hxx>
18 #include <Standard_Transient.hxx>
19 #include <StepData_ReadWriteModule.hxx>
20 #include <StepData_UndefinedEntity.hxx>
22 #include <Message_Messenger.hxx>
23 #include <Message.hxx>
24 #include <Interface_Macros.hxx>
25 #include <TCollection_AsciiString.hxx>
28 //=======================================================================
29 //function : StepData_StepReaderTool
31 //=======================================================================
33 StepData_StepReaderTool::StepData_StepReaderTool
34 (const Handle(StepData_StepReaderData)& reader,
35 const Handle(StepData_Protocol)& protocol)
36 : theglib(protocol) , therlib(protocol)
38 SetData(reader,protocol);
42 //=======================================================================
43 //function : Recognize
45 //=======================================================================
47 Standard_Boolean StepData_StepReaderTool::Recognize(const Standard_Integer num,
48 Handle(Interface_Check)& ach,
49 Handle(Standard_Transient)& ent)
51 // Handle(Standard_Transient) bid; // pas exploite
52 // return thereco->Evaluate(thetypes.Value(num),bid);
54 // Recognizer : C est lui qui assure la Reconnaissance (-> Liste limitative)
55 if (!thereco.IsNull()) {
56 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
57 return thereco->Evaluate(stepdat->RecordType(num),ent);
60 // Pas de Recognizer : Reconnaissance par la librairie
61 return RecognizeByLib (num,theglib,therlib,ach,ent);
65 // .... Methodes de preparations propres a StepReaderTool ....
68 //=======================================================================
71 //=======================================================================
73 void StepData_StepReaderTool::Prepare
74 (const Handle(StepData_FileRecognizer)& reco, const Standard_Boolean optim)
81 //=======================================================================
84 //=======================================================================
86 void StepData_StepReaderTool::Prepare (const Standard_Boolean optim)
88 // SetEntityNumbers a ete mis du cote de ReaderData, because beaucoup acces
89 Standard_Boolean erh = ErrorHandle();
90 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
94 stepdat->SetEntityNumbers(optim);
97 catch(Standard_Failure) {
98 Handle(Message_Messenger) sout = Message::DefaultMessenger();
99 sout << " Exception Raised during Preparation :\n";
100 sout << Standard_Failure::Caught()->GetMessageString();
101 sout << "\n Now, trying to continue, but with presomption of failure\n";
105 stepdat->SetEntityNumbers(optim);
111 // .... Gestion du Header : Preparation, lecture .... //
114 //=======================================================================
115 //function : PrepareHeader
117 //=======================================================================
119 void StepData_StepReaderTool::PrepareHeader
120 (const Handle(StepData_FileRecognizer)& reco)
122 Standard_Integer i = 0;
124 // Reconnaissance des types
125 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
126 while ( (i = stepdat->FindNextHeaderRecord(i)) != 0) {
127 Handle(Standard_Transient) ent;
128 // On a donne un Recognizer : il fixe une liste limitative de types reconnus
129 if (!reco.IsNull()) {
130 if (!reco->Evaluate(stepdat->RecordType(i),ent)) {
131 ent = Protocol()->UnknownEntity();
134 // Pas de Recognizer : Reconnaissance par la librairie
135 Handle(Interface_Check) ach = new Interface_Check; // faudrait le lister ... ?
136 RecognizeByLib (i,theglib,therlib,ach,ent);
138 if (ent.IsNull()) ent = Protocol()->UnknownEntity();
139 stepdat->BindEntity(i,ent);
142 // Reste la Resolution des references : ne concerne que les sous-listes
143 // Assuree par ReaderData
144 stepdat->PrepareHeader();
148 // .... Methodes pour la lecture du Modele (apres preparation) .... //
151 //=======================================================================
152 //function : BeginRead
154 //=======================================================================
156 void StepData_StepReaderTool::BeginRead
157 (const Handle(Interface_InterfaceModel)& amodel)
159 Handle(Message_Messenger) sout = Message::DefaultMessenger();
160 DeclareAndCast(StepData_StepModel,model,amodel);
161 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
163 model->ClearHeader();
164 model->SetGlobalCheck(stepdat->GlobalCheck());
165 Standard_Integer i = 0;
166 while ( (i = stepdat->FindNextHeaderRecord(i)) != 0) {
167 Handle(Standard_Transient) ent = stepdat->BoundEntity(i);
168 Handle(Interface_Check) ach = new Interface_Check(ent);
169 AnalyseRecord(i,ent,ach);
170 if (ent->IsKind(STANDARD_TYPE(StepData_UndefinedEntity))) {
171 TCollection_AsciiString mess("Header Entity not Recognized, StepType: ");
172 mess.AssignCat(stepdat->RecordType(i));
173 ach->AddWarning(mess.ToCString());
175 if (ach->HasFailed() || ach->HasWarnings()) {
176 Handle(Interface_Check) mch = model->GlobalCheck();
177 mch->GetMessages(ach); model->SetGlobalCheck(mch);
179 model->AddHeaderEntity(ent);
180 if (ach->HasWarnings()) {
181 Handle(Interface_Check) mch = model->GlobalCheck();
182 Standard_Integer nbmess = ach->NbWarnings();
183 sout<<nbmess<<" Warnings on Reading Header Entity N0."<<i<<":";
184 if (!ent.IsNull()) sout << ent->DynamicType()->Name() << endl;
185 for (Standard_Integer nf = 1; nf <= nbmess; nf ++)
186 sout << ach->CWarning(nf) << "\n";
188 if (ach->HasFailed()) {
189 Handle(Interface_Check) mch = model->GlobalCheck();
190 Standard_Integer nbmess = ach->NbFails();
191 sout << " Errors on Reading Header Entity N0."<<i<<":";
192 if (!ent.IsNull()) sout << ent->DynamicType()->Name() << endl;
193 for (Standard_Integer nf = 1; nf <= nbmess; nf ++)
194 sout << ach->CFail(nf) << "\n";
200 //=======================================================================
201 //function : AnalyseRecord
203 //=======================================================================
205 Standard_Boolean StepData_StepReaderTool::AnalyseRecord
206 (const Standard_Integer num,
207 const Handle(Standard_Transient)& anent,
208 Handle(Interface_Check)& acheck)
210 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
211 Handle(StepData_ReadWriteModule) module; Standard_Integer CN;
212 if (therlib.Select(anent,module,CN))
213 module->ReadStep(CN,stepdat,num,acheck,anent);
215 // Pas trouve : tenter UndefinedEntity de StepData
216 DeclareAndCast(StepData_UndefinedEntity,und,anent);
217 if (und.IsNull()) acheck->AddFail
218 ("# Entity neither Recognized nor set as UndefinedEntity from StepData #");
219 else und->ReadRecord(stepdat,num,acheck);
221 return (!acheck->HasFailed());
225 //=======================================================================
228 //=======================================================================
230 void StepData_StepReaderTool::EndRead
231 (const Handle(Interface_InterfaceModel)& amodel)
233 DeclareAndCast(StepData_StepReaderData,stepdat,Data());
234 DeclareAndCast(StepData_StepModel,stepmodel,amodel);
235 if (stepmodel.IsNull()) return;
236 Standard_Integer i = 0;
237 while ( (i = stepdat->FindNextRecord(i)) != 0) {
238 stepmodel->SetIdentLabel(stepdat->BoundEntity(i),stepdat->RecordIdent(i));