ca16bd22fcd3433eb4910eab1664b481524fccf3
[occt.git] / src / StepFile / StepFile_Read.cxx
1 // Created on: 1991-08-30
2 // Created by: Christian CAILLET
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
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
28
29 #include <stdio.h>
30 #include "recfile.ph"
31 #include "stepread.ph"
32 extern "C" void recfile_modeprint (int mode);  // controle trace recfile
33           // recfile_modeprint est declare a part
34
35 #include <Interface_ParamType.hxx>
36 #include <Interface_Protocol.hxx>
37 #include <Interface_Check.hxx>
38 #include <StepData_Protocol.hxx>
39
40 #include <StepData_StepReaderData.hxx>
41 #include <StepData_StepReaderTool.hxx>
42 #include <StepFile_Read.hxx>
43
44 #include <Standard_ErrorHandler.hxx>
45 #include <Standard_Failure.hxx>
46 #include <Interface_InterfaceError.hxx>
47
48 #include <Message_Messenger.hxx>
49 #include <Message.hxx>
50
51 #ifdef OCCT_DEBUG
52 #define CHRONOMESURE
53 #ifdef CHRONOMESURE
54 # include <OSD_Timer.hxx>
55 #endif
56 #endif
57
58 //  ##  ##  ##  ##    ON SAURA AU MOINS TRAITER UndefinedEntity  ##  ##  ##  ##
59
60 static Handle(Interface_Check) checkread = new Interface_Check;
61 static Standard_Integer modepr = 1;
62
63 void StepFile_ReadTrace (const Standard_Integer mode)
64 {
65   modepr = mode;   // recfile_modeprint est rappele a chaque lecture de fichier
66 }
67
68
69 static Standard_Integer StepFile_Read
70  (char* nomfic,
71   const Handle(StepData_StepModel)& stepmodel,
72   const Handle(StepData_Protocol)& protocol,
73   const Handle(StepData_FileRecognizer)& recoheader,
74   const Handle(StepData_FileRecognizer)& recodata);
75
76
77 Standard_Integer StepFile_Read
78  (char* nomfic,
79   const Handle(StepData_StepModel)& stepmodel,
80   const Handle(StepData_FileRecognizer)& recoheader,
81   const Handle(StepData_FileRecognizer)& recodata)
82 {
83   return StepFile_Read
84     (nomfic,stepmodel,
85      Handle(StepData_Protocol)::DownCast(Interface_Protocol::Active()),
86      recoheader,recodata);
87 }
88
89 Standard_Integer StepFile_Read
90  (char* nomfic,
91   const Handle(StepData_StepModel)& stepmodel,
92   const Handle(StepData_FileRecognizer)& recoheader,
93   const Handle(StepData_Protocol)& protocol)
94 {
95   Handle(StepData_FileRecognizer) nulreco;
96   return StepFile_Read (nomfic,stepmodel,protocol,recoheader,nulreco);
97 }
98
99 Standard_Integer StepFile_Read
100  (char* nomfic,
101   const Handle(StepData_StepModel)& stepmodel,
102   const Handle(StepData_Protocol)& protocol)
103 {
104   Handle(StepData_FileRecognizer) nulreco;
105   return StepFile_Read (nomfic,stepmodel,protocol,nulreco,nulreco);
106 }
107
108 //  ##  ##  ##  ##  ##  ##    Corps de la Routine    ##  ##  ##  ##  ##  ##
109
110 static Interface_ParamType LesTypes[10];   // passage types (recstep/Interface)
111
112 Standard_Integer StepFile_Read
113  (char* nomfic,
114   const Handle(StepData_StepModel)& stepmodel,
115   const Handle(StepData_Protocol)& protocol,
116   const Handle(StepData_FileRecognizer)& recoheader,
117   const Handle(StepData_FileRecognizer)& recodata)
118
119 {
120   Handle(Message_Messenger) sout = Message::DefaultMessenger();
121   char *ficnom = nomfic ;  // because const (non reconnu par C)
122
123   checkread->Clear();
124   recfile_modeprint ( (modepr > 0 ? modepr-1 : 0) );
125   FILE* newin = stepread_setinput(ficnom);
126   if (!newin) return -1;
127 #ifdef CHRONOMESURE
128   Standard_Integer n ; 
129   OSD_Timer c ; 
130   c.Reset () ; 
131   c.Start();
132   sout << "      ...    Step File Reading : " << ficnom << "" << Message_EndLine;  
133 #endif
134
135   try {
136     OCC_CATCH_SIGNALS
137     if (stepread () != 0) {  lir_file_fin(3);  stepread_endinput (newin,ficnom);  return 1;  }
138   }
139   catch (Standard_Failure const& anException) {
140 #ifdef OCCT_DEBUG
141     sout << " ...  Exception Raised while reading Step File : " << ficnom << ":\n" << Message_EndLine;
142     sout << anException.GetMessageString();  
143     sout << "    ..." << Message_EndLine;
144 #endif
145     (void)anException;
146     lir_file_fin(3);  
147     stepread_endinput (newin,ficnom);  
148     return 1;
149   }
150   // Continue reading of file despite of possible fails
151   //if (checkread->HasFailed()) {  lir_file_fin(3);  stepread_endinput (newin,ficnom);  return 1;  }
152 #ifdef CHRONOMESURE
153   sout << "      ...    STEP File   Read    ... " << Message_EndLine;  
154   c.Show(); 
155 #endif
156
157
158 //  Creation du StepReaderData
159
160   LesTypes[rec_argNondef]    = Interface_ParamVoid ;
161   LesTypes[rec_argSub]       = Interface_ParamSub ;
162   LesTypes[rec_argIdent]     = Interface_ParamIdent ;
163   LesTypes[rec_argInteger]   = Interface_ParamInteger ;
164   LesTypes[rec_argFloat]     = Interface_ParamReal ;
165   LesTypes[rec_argEnum]      = Interface_ParamEnum ;
166   LesTypes[rec_argBinary]    = Interface_ParamBinary ;
167   LesTypes[rec_argText]      = Interface_ParamText ;
168   LesTypes[rec_argHexa]      = Interface_ParamHexa ;
169   LesTypes[rec_argMisc]      = Interface_ParamMisc ;
170
171   Standard_Integer nbhead, nbrec, nbpar;
172   lir_file_nbr (&nbhead,&nbrec,&nbpar);  // renvoi par lex/yacc
173   Handle(StepData_StepReaderData) undirec =
174     new StepData_StepReaderData(nbhead,nbrec,nbpar);  // creation tableau de records
175
176   for ( Standard_Integer nr = 1; nr <= nbrec; nr ++) {
177     int nbarg; char* ident; char* typrec = 0;
178     lir_file_rec (&ident, &typrec, &nbarg);
179     undirec->SetRecord (nr, ident, typrec, nbarg);
180
181     if (nbarg>0) {
182       int typa; char* val;
183       Interface_ParamType newtype;
184       while(lir_file_arg (&typa, &val) == 1) {
185         newtype = LesTypes[typa] ;
186         undirec->AddStepParam (nr, val, newtype);
187       }
188     }
189     undirec->InitParams(nr);
190     lir_file_finrec();
191   }
192   lir_file_fin(1);
193 //  on a undirec pret pour la suite
194
195 #ifdef CHRONOMESURE
196   sout << "      ... Step File loaded  ... " << Message_EndLine; 
197   c.Show();
198   sout << "   "<< undirec->NbRecords () <<
199       " records (entities,sub-lists,scopes), "<< nbpar << " parameters\n" << Message_EndLine;
200 #endif
201
202 //   Analyse : par StepReaderTool
203
204   StepData_StepReaderTool readtool (undirec,protocol);
205   readtool.SetErrorHandle (Standard_True);
206
207   readtool.PrepareHeader(recoheader);  // Header. reco nul -> pour Protocol
208   readtool.Prepare(recodata);          // Data.   reco nul -> pour Protocol
209
210 #ifdef CHRONOMESURE
211   sout << "      ... Parameters prepared ... "; 
212   c.Show(); 
213 #endif
214
215   readtool.LoadModel(stepmodel);
216   if (stepmodel->Protocol().IsNull()) stepmodel->SetProtocol (protocol);
217   lir_file_fin(2);
218   
219   readtool.Clear();
220   undirec.Nullify();
221 #ifdef CHRONOMESURE
222   sout << "      ...   Objets analysed  ... " << Message_EndLine; 
223   c.Show();
224   n = stepmodel->NbEntities() ;
225   sout << "  STEP Loading done : " << n << " Entities" << Message_EndLine;
226 #endif
227   
228   stepread_endinput (newin,ficnom);  return 0 ;
229 }
230
231 void StepFile_Interrupt (char* mess)
232 {
233 #ifdef OCCT_DEBUG
234   Handle(Message_Messenger) sout = Message::DefaultMessenger();
235   sout << "    ****    StepFile Error : " << mess << "    ****" << Message_EndLine;
236 #endif
237   checkread->AddFail(mess);
238 }