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