0032961: Coding - get rid of unused headers [IGESAppli to IGESToBRep]
[occt.git] / src / IGESFile / IGESFile_Read.cxx
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 // dce 21.01.99 : move of general message to IGESToBRep_Reader
15
16 #include <stdio.h>
17 // declarations des programmes C de base :
18 #include <IGESData_IGESReaderData.hxx>
19 #include <IGESData_IGESReaderTool.hxx>
20 #include <IGESData_GeneralModule.hxx>
21 #include <Interface_Check.hxx>
22
23 //  Pour traiter les exceptions :
24 #include <Standard_ErrorHandler.hxx>
25 #include <Standard_Failure.hxx>
26
27 // definition de base, a inclure pour utiliser
28 #include <IGESFile_Read.hxx>
29
30 #include "igesread.h"
31
32 //#define VERIFPRINT
33
34 // MGE 16/06/98
35 // To use Msg class
36 #include <Message_Msg.hxx>
37
38 // decoupage interne pour faciliter les recuperations d erreurs
39 static Standard_Integer recupne,recupnp;  // pour affichage en cas de pepin
40 static Handle(Interface_Check)& checkread()
41 {
42   static Handle(Interface_Check) chrd = new Interface_Check;
43   return chrd;
44 }
45
46 static void IGESFile_ReadHeader  (const Handle(IGESData_IGESReaderData)& IR);
47 static void IGESFile_ReadContent (const Handle(IGESData_IGESReaderData)& IR);
48 void IGESFile_Check (int mode,Message_Msg& amsg);
49 // void IGESFile_Check2 (int mode,char * code, int num, char * str);
50 // void IGESFile_Check3 (int mode,char * code);
51
52 //  Correspondance entre types igesread et types Interface_ParamFile ...
53 static Interface_ParamType LesTypes[10];
54
55
56 //  Nouvelle maniere : Protocol suffit
57
58 Standard_Integer IGESFile_Read
59   (char* nomfic,
60    const Handle(IGESData_IGESModel)& amodel,
61    const Handle(IGESData_Protocol)& protocol)
62 {
63   Handle(IGESData_FileRecognizer) nulreco;
64   return IGESFile_Read(nomfic,amodel,protocol,nulreco,Standard_False);
65 }
66
67 Standard_Integer IGESFile_ReadFNES
68   (char* nomfic,
69    const Handle(IGESData_IGESModel)& amodel,
70    const Handle(IGESData_Protocol)& protocol)
71 {
72   Handle(IGESData_FileRecognizer) nulreco;
73   return IGESFile_Read(nomfic,amodel,protocol,nulreco,Standard_True);
74 }
75
76
77 //  Ancienne maniere : avec Recognizer
78
79 Standard_Integer IGESFile_Read
80   (char* nomfic,
81    const Handle(IGESData_IGESModel)& amodel,
82    const Handle(IGESData_Protocol)& protocol,
83    const Handle(IGESData_FileRecognizer)& reco,
84    const Standard_Boolean modefnes)
85 {
86   //====================================
87   Message_Msg Msg1  = Message_Msg("XSTEP_1");
88   Message_Msg Msg15 = Message_Msg("XSTEP_15");
89   //====================================
90
91   char* ficnom = nomfic; // ficnom ?
92   int lesect[6];
93   
94   // Sending of message : Beginning of the reading
95   IGESFile_Check(2, Msg1);
96
97   checkread()->Clear();
98   int result = igesread(ficnom,lesect,modefnes);
99
100   if (result != 0) return result;
101
102 //  Chargement des resultats dans un IGESReader
103
104   LesTypes[ArgVide] = Interface_ParamVoid;
105   LesTypes[ArgQuid] = Interface_ParamMisc;
106   LesTypes[ArgChar] = Interface_ParamText;
107   LesTypes[ArgInt]  = Interface_ParamInteger;
108   LesTypes[ArgSign] = Interface_ParamInteger;
109   LesTypes[ArgReal] = Interface_ParamReal;
110   LesTypes[ArgExp ] = Interface_ParamMisc;  // exposant pas termine
111   LesTypes[ArgRexp] = Interface_ParamReal;       // exposant complet
112   LesTypes[ArgMexp] = Interface_ParamEnum;       // exposant mais pas de point
113
114
115   int nbparts, nbparams;
116   iges_stats(&nbparts,&nbparams);    // et fait les Initialisations necessaires
117   Handle(IGESData_IGESReaderData) IR =
118 //    new IGESData_IGESReaderData (nbparts, nbparams);
119     new IGESData_IGESReaderData((lesect[3]+1)/2, nbparams);
120   {
121    {
122     try {
123       OCC_CATCH_SIGNALS
124       IGESFile_ReadHeader(IR);
125     }    // fin essai 1 (global)
126     catch (Standard_Failure const&) {
127       // Sending of message : Internal error during the header reading 
128       Message_Msg Msg11 = Message_Msg("XSTEP_11");
129       IGESFile_Check (1,Msg11);
130     }
131    }
132
133    {
134     try {
135       OCC_CATCH_SIGNALS
136       if (nbparts > 0) IGESFile_ReadContent(IR);
137
138   // Sending of message : Loaded data  
139     }    // fin essai 2 (entites)
140     catch (Standard_Failure const&) {
141       // Sending of message : Internal error during the content reading 
142       if (recupnp == 0) {
143         Message_Msg Msg13 = Message_Msg("XSTEP_13");
144         Msg13.Arg(recupne);
145         IGESFile_Check(1,Msg13);
146       }
147       else {
148         Message_Msg Msg14 = Message_Msg("XSTEP_14");
149         Msg14.Arg(recupne);
150         Msg14.Arg(recupnp);
151         IGESFile_Check(1, Msg14);
152       }
153     }
154    }
155   }
156   
157   Standard_Integer nbr = IR->NbRecords();
158   // Sending of message : Number of total loaded entities 
159   Msg15.Arg(nbr);
160   IGESFile_Check(2, Msg15);
161   iges_finfile(1);
162   IGESData_IGESReaderTool IT (IR,protocol);
163   IT.Prepare(reco); 
164   IT.SetErrorHandle(Standard_True);
165
166   // Sending of message : Loading of Model : Beginning 
167   IT.LoadModel(amodel);
168   if (amodel->Protocol().IsNull()) amodel->SetProtocol (protocol);
169   iges_finfile(2);
170
171   //  A present, le check
172   // Nb warning in global section.
173   Standard_Integer nbWarn = checkread()->NbWarnings(), nbFail = checkread()->NbFails();
174   const Handle(Interface_Check)& oldglob = amodel->GlobalCheck();
175   if (nbWarn + nbFail > 0) {
176     checkread()->GetMessages (oldglob);
177     amodel->SetGlobalCheck (checkread());
178   }
179
180   checkread()->Trace(0,1);
181  
182   return 0;
183 }
184
185
186 // Decoupage interne
187
188  void IGESFile_ReadHeader  (const Handle(IGESData_IGESReaderData)& IR)
189 {
190   Standard_Integer l=0; //szv#4:S4163:12Mar99 i,j,k not needed
191   char* parval;
192   int typarg;
193   //  d abord les start lines (commentaires)
194   //szv#4:S4163:12Mar99 optimized
195 /*
196   while ( (j = iges_lirparam(&typarg,&parval)) != 0) {
197     k = -1;
198     for (Standard_Integer j = 72; j >= 0; j --) {
199       if (parval[j] > 32) {  k = j;  break;  }
200     }
201     parval[k+1] = '\0';
202     if (k >= 0 || l > 0) IR->AddStartLine (parval);
203     l ++;
204   }
205   //  puis la Global Section
206   iges_setglobal();
207   while ( (i = iges_lirparam(&typarg,&parval)) != 0) {
208     IR->AddGlobal(LesTypes[typarg],parval);
209   }
210 */
211   while (iges_lirparam(&typarg,&parval) != 0) {
212     Standard_Integer j; // svv Jan11 2000 : porting on DEC
213     for (j = 72; j >= 0; j--)
214       if (parval[j] > 32) break;
215     parval[j+1] = '\0';
216     if (j >= 0 || l > 0) IR->AddStartLine (parval);
217     l++;
218   }
219   //  puis la Global Section
220   iges_setglobal();
221   while (iges_lirparam(&typarg,&parval) != 0) IR->AddGlobal(LesTypes[typarg],parval);
222   IR->SetGlobalSection();
223 }
224
225  void IGESFile_ReadContent (const Handle(IGESData_IGESReaderData)& IR)
226 {
227   char *res1, *res2, *nom, *num; char* parval;
228   int *v; int typarg;
229   int nbparam;
230     
231
232   Standard_Integer nn=0;
233   int ns; //szv#4:S4163:12Mar99 i unused
234   while ( (ns = iges_lirpart(&v,&res1,&res2,&nom,&num,&nbparam)) != 0) {
235     nn++;
236     recupnp = 0;
237     recupne = (ns+1)/2;  // numero entite
238 //    if(recupne > IR->NbEntities()) {
239 //      iges_nextpart();
240 //      continue;
241 //    }
242     IR->SetDirPart(recupne,
243                    v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],
244                    v[11],v[12],v[13],v[14],v[15],v[16],res1,res2,nom,num);
245     while (iges_lirparam(&typarg,&parval) != 0) { //szv#4:S4163:12Mar99 `i=` not needed
246       recupnp ++;
247       if (typarg == ArgInt || typarg == ArgSign) {
248         Standard_Integer nument = atoi(parval);
249         if (nument < 0) nument = -nument;
250         if (nument & 1) nument = (nument+1)/2;
251         else nument = 0;
252         IR->AddParam(recupne,parval,LesTypes[typarg],nument);
253       }
254       else IR->AddParam(recupne,parval,LesTypes[typarg]);
255     }
256     IR->InitParams(recupne);
257     iges_nextpart();
258   }
259 }
260
261
262 void IGESFile_Check (int mode,Message_Msg& amsg)
263 {
264   // MGE 20/07/98
265   switch (mode)
266    {
267     case 0 : checkread()->SendFail (amsg); break;
268     case 1 : checkread()->SendWarning (amsg); break;
269     case 2 : checkread()->SendMsg (amsg);break;
270     default : checkread()->SendMsg (amsg); 
271    }
272   //checkread().Trace(3,-1);
273 }
274
275 void IGESFile_Check2 (int mode,char * code, int num, char * str)
276 {
277   // MGE 20/07/98
278   Message_Msg amsg (code);
279   amsg.Arg(num);
280   amsg.Arg(str);
281
282   switch (mode)
283    {
284     case 0 : checkread()->SendFail (amsg); break;
285     case 1 : checkread()->SendWarning (amsg); break;
286     case 2 : checkread()->SendMsg (amsg); break;
287     default : checkread()->SendMsg (amsg); 
288    }
289   //checkread().Trace(3,-1);
290 }
291
292
293 void IGESFile_Check3 (int mode,char * code)
294 {
295   // MGE 20/07/98
296   Message_Msg amsg (code);
297   switch (mode)
298    {
299     case 0 : checkread()->SendFail (amsg); break;
300     case 1 : checkread()->SendWarning (amsg); break;
301     case 2 : checkread()->SendMsg (amsg); break;
302     default : checkread()->SendMsg (amsg);
303    }
304   //checkread().Trace(3,-1);
305 }