0027342: STEP - support C++ streams for import / export
[occt.git] / src / StepFile / StepFile_Read.cxx
CommitLineData
b311480e 1// Created on: 1991-08-30
2// Created by: Christian CAILLET
3// Copyright (c) 1991-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17// StepFile_Read
18
19// routine assurant l enchainement des operations de lecture d un fichier
20// STEP dans un StepModel, en fonction d une cle de reconnaissance
21// Retour de la fonction :
22// 0 si OK (le StepModel a ete charge)
23// -1 si abandon car fichier pas pu etre ouvert
24// 1 si erreur en cours de lecture
25
26// Compilation conditionnelle : concerne les mesures de performances
27
7fd59977 28#include <stdio.h>
68922bcc 29#include <iostream>
7fd59977 30#include "recfile.ph"
31#include "stepread.ph"
7fd59977 32
33#include <Interface_ParamType.hxx>
34#include <Interface_Protocol.hxx>
35#include <Interface_Check.hxx>
36#include <StepData_Protocol.hxx>
37
38#include <StepData_StepReaderData.hxx>
39#include <StepData_StepReaderTool.hxx>
40#include <StepFile_Read.hxx>
41
42#include <Standard_ErrorHandler.hxx>
43#include <Standard_Failure.hxx>
44#include <Interface_InterfaceError.hxx>
45
46#include <Message_Messenger.hxx>
47#include <Message.hxx>
48
68922bcc 49#include <OSD_OpenFile.hxx>
50#include <OSD_Timer.hxx>
51
0797d9d3 52#ifdef OCCT_DEBUG
7fd59977 53#define CHRONOMESURE
0797d9d3 54#endif
7fd59977 55
56// ## ## ## ## ON SAURA AU MOINS TRAITER UndefinedEntity ## ## ## ##
57
68922bcc 58extern "C" void recfile_modeprint (int mode); // controle trace recfile
59
7fd59977 60static Handle(Interface_Check) checkread = new Interface_Check;
61static Standard_Integer modepr = 1;
62
63void StepFile_ReadTrace (const Standard_Integer mode)
64{
65 modepr = mode; // recfile_modeprint est rappele a chaque lecture de fichier
66}
67
7fd59977 68// ## ## ## ## ## ## Corps de la Routine ## ## ## ## ## ##
69
70static Interface_ParamType LesTypes[10]; // passage types (recstep/Interface)
71
68922bcc 72static Standard_Integer StepFile_Read (const char* theName,
73 std::istream* theIStream,
74 const Handle(StepData_StepModel)& stepmodel,
75 const Handle(StepData_Protocol)& protocol,
76 const Handle(StepData_FileRecognizer)& recoheader,
77 const Handle(StepData_FileRecognizer)& recodata)
7fd59977 78{
7fd59977 79 checkread->Clear();
80 recfile_modeprint ( (modepr > 0 ? modepr-1 : 0) );
68922bcc 81
82 // if stream is not provided, open file stream here
83 std::istream *aStreamPtr = theIStream;
84 std::ifstream aFileStream;
85 if (!aStreamPtr) {
86 OSD_OpenStream(aFileStream, theName, std::ios_base::in | std::ios_base::binary);
87 aStreamPtr = &aFileStream;
88 }
89
90 if (aStreamPtr->bad())
91 {
92 return -1;
93 }
94
7fd59977 95#ifdef CHRONOMESURE
68922bcc 96 OSD_Timer c;
97 c.Reset();
7fd59977 98 c.Start();
68922bcc 99 Message::SendInfo() << " ... Step File Reading : '" << theName << "'";
7fd59977 100#endif
101
102 try {
103 OCC_CATCH_SIGNALS
68922bcc 104 if (stepread(*aStreamPtr) != 0) {
105 lir_file_fin(3);
106 return 1;
107 }
7fd59977 108 }
9775fa61 109 catch (Standard_Failure const& anException) {
0797d9d3 110#ifdef OCCT_DEBUG
68922bcc 111 Message::SendAlarm() << " ... Exception Raised while reading Step File : '" << theName << "':\n"
112 << anException.GetMessageString()
113 << " ...";
0797d9d3 114#endif
9775fa61 115 (void)anException;
7fd59977 116 lir_file_fin(3);
7fd59977 117 return 1;
118 }
68922bcc 119
120// Continue reading of file despite of possible fails
121// if (checkread->HasFailed()) { lir_file_fin(3); stepread_endinput(newifstream, ficnom); return 1; }
7fd59977 122#ifdef CHRONOMESURE
68922bcc 123 {
124 Message_Messenger::StreamBuffer sout = Message::SendInfo();
125 sout << " ... STEP File Read ...\n";
126 c.Show (sout);
127 }
7fd59977 128#endif
129
7fd59977 130// Creation du StepReaderData
131
132 LesTypes[rec_argNondef] = Interface_ParamVoid ;
133 LesTypes[rec_argSub] = Interface_ParamSub ;
134 LesTypes[rec_argIdent] = Interface_ParamIdent ;
135 LesTypes[rec_argInteger] = Interface_ParamInteger ;
136 LesTypes[rec_argFloat] = Interface_ParamReal ;
137 LesTypes[rec_argEnum] = Interface_ParamEnum ;
138 LesTypes[rec_argBinary] = Interface_ParamBinary ;
139 LesTypes[rec_argText] = Interface_ParamText ;
140 LesTypes[rec_argHexa] = Interface_ParamHexa ;
141 LesTypes[rec_argMisc] = Interface_ParamMisc ;
142
143 Standard_Integer nbhead, nbrec, nbpar;
144 lir_file_nbr (&nbhead,&nbrec,&nbpar); // renvoi par lex/yacc
145 Handle(StepData_StepReaderData) undirec =
146 new StepData_StepReaderData(nbhead,nbrec,nbpar); // creation tableau de records
147
148 for ( Standard_Integer nr = 1; nr <= nbrec; nr ++) {
c7854818 149 int nbarg; char* ident; char* typrec = 0;
7fd59977 150 lir_file_rec (&ident, &typrec, &nbarg);
151 undirec->SetRecord (nr, ident, typrec, nbarg);
152
153 if (nbarg>0) {
154 int typa; char* val;
155 Interface_ParamType newtype;
156 while(lir_file_arg (&typa, &val) == 1) {
157 newtype = LesTypes[typa] ;
158 undirec->AddStepParam (nr, val, newtype);
159 }
160 }
161 undirec->InitParams(nr);
162 lir_file_finrec();
163 }
164 lir_file_fin(1);
165// on a undirec pret pour la suite
166
167#ifdef CHRONOMESURE
68922bcc 168 {
169 Message_Messenger::StreamBuffer sout = Message::SendInfo();
170 sout << " ... Step File loaded ...\n";
171 c.Show (sout);
172 sout << " "<< undirec->NbRecords() << " records (entities,sub-lists,scopes), "<< nbpar << " parameters";
173 }
7fd59977 174#endif
175
176// Analyse : par StepReaderTool
177
178 StepData_StepReaderTool readtool (undirec,protocol);
179 readtool.SetErrorHandle (Standard_True);
180
181 readtool.PrepareHeader(recoheader); // Header. reco nul -> pour Protocol
182 readtool.Prepare(recodata); // Data. reco nul -> pour Protocol
bc650d41 183
7fd59977 184#ifdef CHRONOMESURE
68922bcc 185 {
186 Message_Messenger::StreamBuffer sout = Message::SendInfo();
187 sout << " ... Parameters prepared ...\n";
188 c.Show (sout);
189 }
7fd59977 190#endif
191
192 readtool.LoadModel(stepmodel);
193 if (stepmodel->Protocol().IsNull()) stepmodel->SetProtocol (protocol);
194 lir_file_fin(2);
bc650d41
G
195
196 readtool.Clear();
197 undirec.Nullify();
7fd59977 198#ifdef CHRONOMESURE
68922bcc 199 {
200 Message_Messenger::StreamBuffer sout = Message::SendInfo();
201 sout << " ... Objets analysed ...\n";
202 c.Show (sout);
203 Standard_Integer n = stepmodel->NbEntities();
204 sout << " STEP Loading done : " << n << " Entities";
205 }
7fd59977 206#endif
68922bcc 207 return 0;
208}
209
210Standard_Integer StepFile_Read(const char* theName,
211 std::istream* theIStream,
212 const Handle(StepData_StepModel)& stepmodel,
213 const Handle(StepData_Protocol)& protocol)
214{
215 Handle(StepData_FileRecognizer) nulreco;
216 return StepFile_Read (theName,theIStream,stepmodel,protocol,nulreco,nulreco);
7fd59977 217}
218
219void StepFile_Interrupt (char* mess)
220{
0797d9d3 221#ifdef OCCT_DEBUG
0ebe5b0a 222 Message_Messenger::StreamBuffer sout = Message::SendInfo();
223 sout << " **** StepFile Error : " << mess << " ****" << std::endl;
0797d9d3 224#endif
7fd59977 225 checkread->AddFail(mess);
226}