0024428: Implementation of LGPL license
[occt.git] / src / IGESSelect / IGESSelect_WorkLibrary.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
6 // under the terms of the GNU Lesser General Public 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 #include <Standard_Stream.hxx>
15
16 #include <IGESSelect_WorkLibrary.ixx>
17 #include <Standard_ErrorHandler.hxx>
18 #include <Message_Messenger.hxx>
19 #include <Message.hxx>
20
21 #include <IGESSolid.hxx>
22 #include <IGESAppli.hxx>
23 #include <IGESDefs.hxx>
24
25 #include <IGESData_IGESWriter.hxx>
26 #include <IGESData_IGESModel.hxx>
27 #include <IGESData_Protocol.hxx>
28 #include <IGESData_FileProtocol.hxx>
29 #include <IGESFile_Read.hxx>
30 #include <IGESSelect_Dumper.hxx>
31 #include <IFSelect_GeneralModifier.hxx>
32 #include <TColStd_HSequenceOfInteger.hxx>
33 #include <IGESSelect_FileModifier.hxx>
34
35 #include <Interface_ReportEntity.hxx>
36 #include <IGESData_IGESEntity.hxx>
37 #include <IGESData_IGESDumper.hxx>
38
39 #include <Interface_Check.hxx>
40
41 #include <Interface_Macros.hxx>
42 #include <errno.h>
43
44 static int deja = 0;
45 static  Handle(IGESData_FileProtocol) IGESProto;
46
47
48      IGESSelect_WorkLibrary::IGESSelect_WorkLibrary
49   (const Standard_Boolean modefnes)
50   : themodefnes (modefnes)
51 {
52   IGESSolid::Init();
53   IGESAppli::Init();
54   IGESDefs::Init();
55
56   if (!deja) {
57     Handle(IGESSelect_Dumper) sesdump = new IGESSelect_Dumper;  // ainsi,cestfait
58     deja = 1;
59   }
60   SetDumpLevels (4,6);
61   SetDumpHelp (0,"Only DNum");
62   SetDumpHelp (1,"DNum, IGES Type & Form");
63   SetDumpHelp (2,"Main Directory Informations");
64   SetDumpHelp (3,"Complete Directory Part");
65   SetDumpHelp (4,"Directory + Fields (except list contents)");
66   SetDumpHelp (5,"Complete (with list contents)");
67   SetDumpHelp (6,"Complete + Transformed data");
68 }
69
70     Standard_Integer  IGESSelect_WorkLibrary::ReadFile
71   (const Standard_CString name,
72    Handle(Interface_InterfaceModel)& model,
73    const Handle(Interface_Protocol)& protocol) const
74 {
75   Handle(Message_Messenger) sout = Message::DefaultMessenger();
76   Handle(IGESData_IGESModel) igesmod = new IGESData_IGESModel;
77   DeclareAndCast(IGESData_Protocol,prot,protocol);
78
79   char* pname=(char*) name;
80   Standard_Integer status = IGESFile_Read (pname,igesmod,prot);
81
82   if (status < 0) sout<<"File not found : "<<name<<endl;
83   if (status > 0) sout<<"Error when reading file : "<<name<<endl;
84   if (status == 0) model = igesmod;
85   else             model.Nullify();
86   return status;
87 }
88
89
90     Standard_Boolean  IGESSelect_WorkLibrary::WriteFile
91   (IFSelect_ContextWrite& ctx) const
92 {
93   Handle(Message_Messenger) sout = Message::DefaultMessenger();
94 //  Preparation
95   DeclareAndCast(IGESData_IGESModel,igesmod,ctx.Model());
96   DeclareAndCast(IGESData_Protocol,prot,ctx.Protocol());
97
98   if (igesmod.IsNull() || prot.IsNull()) return Standard_False;
99   ofstream fout;
100   fout.rdbuf()->open(ctx.FileName(),ios::out );
101   if (!fout) {
102     ctx.CCheck(0)->AddFail("IGES File could not be created");
103     sout<<" - IGES File could not be created : " << ctx.FileName() << endl; return 0;
104   }
105   sout<<" IGES File Name : "<<ctx.FileName();
106   IGESData_IGESWriter VW(igesmod);  
107   sout<<"("<<igesmod->NbEntities()<<" ents) ";
108
109 //  File Modifiers
110   Standard_Integer nbmod = ctx.NbModifiers();
111   for (Standard_Integer numod = 1; numod <= nbmod; numod ++) {
112     ctx.SetModifier (numod);
113     DeclareAndCast(IGESSelect_FileModifier,filemod,ctx.FileModifier());
114     if (!filemod.IsNull()) filemod->Perform(ctx,VW);
115 //   (impressions de mise au point)
116     sout << " .. FileMod." << numod <<" "<< filemod->Label();
117     if (ctx.IsForAll()) sout << " (all model)";
118     else  sout << " (" << ctx.NbEntities() << " entities)";
119 //    sout << flush;
120   }
121
122 //  Envoi
123   VW.SendModel(prot);            
124   sout<<" Write ";
125   if (themodefnes) VW.WriteMode() = 10;
126   Standard_Boolean status = VW.Print(fout);                sout<<" Done"<<endl;
127
128   errno = 0;
129   fout.close();
130   status = fout.good() && status && !errno;
131   if(errno)
132     sout << strerror(errno) << endl;
133
134   return status;
135 }
136
137     Handle(IGESData_Protocol)  IGESSelect_WorkLibrary::DefineProtocol ()
138 {
139   if (!IGESProto.IsNull()) return IGESProto;
140   Handle(IGESData_Protocol)     IGESProto1 = IGESSolid::Protocol();
141   Handle(IGESData_Protocol)     IGESProto2 = IGESAppli::Protocol();
142 //  Handle(IGESData_FileProtocol) IGESProto  = new IGESData_FileProtocol;
143   IGESProto  = new IGESData_FileProtocol;
144   IGESProto->Add(IGESProto1);
145   IGESProto->Add(IGESProto2);
146   return IGESProto;
147 }
148
149
150     void  IGESSelect_WorkLibrary::DumpEntity
151   (const Handle(Interface_InterfaceModel)& model, 
152    const Handle(Interface_Protocol)& protocol,
153    const Handle(Standard_Transient)& entity,
154    const Handle(Message_Messenger)& S, const Standard_Integer level) const
155 {
156   DeclareAndCast(IGESData_IGESModel,igesmod,model);
157   DeclareAndCast(IGESData_Protocol,igespro,protocol);
158   DeclareAndCast(IGESData_IGESEntity,igesent,entity);
159   if (igesmod.IsNull() || igespro.IsNull() || igesent.IsNull()) return;
160   Standard_Integer num = igesmod->Number(igesent);
161   if (num == 0) return;
162
163   S<<" --- Entity "<<num;
164   Standard_Boolean iserr = model->IsRedefinedContent(num);
165   Handle(Standard_Transient) con;
166   if (iserr) con = model->ReportEntity(num)->Content();
167   if (entity.IsNull()) { S<<" Null"<<endl; return ;  }
168
169 //  On attaque le dump : d abord cas de l Erreur
170   if (iserr) {
171     S << " ERRONEOUS, Content, Type cdl : ";
172     if (!con.IsNull()) S << con->DynamicType()->Name();
173     else S << "(undefined)" << endl;
174     igesent = GetCasted(IGESData_IGESEntity,con);
175     con.Nullify();
176     Handle(Interface_Check) check = model->ReportEntity(num)->Check();
177     Interface_CheckIterator chlist;
178     chlist.Add (check,num);
179     chlist.Print (S,igesmod,Standard_False);
180     if (igesent.IsNull()) return;
181   }
182   else S << " Type cdl : " << igesent->DynamicType()->Name();
183
184   IGESData_IGESDumper dump(igesmod,igespro);
185   try {
186     OCC_CATCH_SIGNALS
187     dump.Dump(igesent,S,level,(level-1)/3);
188   }
189   catch (Standard_Failure) {
190     S << " **  Dump Interrupt **" << endl;
191   }
192 }