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