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