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