0025367: IGES and BRep persistence - support unicode file names on Windows
[occt.git] / src / StepSelect / StepSelect_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.
b311480e 13
7fd59977 14#include <StepSelect_WorkLibrary.ixx>
15
16#include <sys/stat.h>
17#include <errno.h>
18
19#include <StepData_Protocol.hxx>
20#include <StepData_StepModel.hxx>
21#include <StepFile_Read.hxx>
22#include <StepData_StepWriter.hxx>
23#include <Interface_CheckIterator.hxx>
24
25#include <StepSelect_FileModifier.hxx>
26
27#include <StepData_UndefinedEntity.hxx>
28#include <StepData_StepDumper.hxx>
29
30#include <TCollection_HAsciiString.hxx>
31#include <TColStd_HSequenceOfInteger.hxx>
32#include <IFSelect_GeneralModifier.hxx>
33#include <Interface_ParamType.hxx>
34#include <Interface_ReportEntity.hxx>
35#include <Interface_UndefinedContent.hxx>
36
37#include <Message.hxx>
38#include <Message_Messenger.hxx>
39#include <Interface_Macros.hxx>
40#include <Interface_Check.hxx>
94708556 41#include <OSD_OpenFile.hxx>
7fd59977 42
43StepSelect_WorkLibrary::StepSelect_WorkLibrary
44 (const Standard_Boolean copymode)
45 : thecopymode (copymode) , thelabmode (0)
46{
47 SetDumpLevels (1,2);
48 SetDumpHelp (0,"#id + Step Type");
49 SetDumpHelp (1,"Entity as in file");
50 SetDumpHelp (2,"Entity + shareds (level 1) as in file");
51}
52// rq : les init sont faits par ailleurs, pas de souci a se faire
53
54
55void StepSelect_WorkLibrary::SetDumpLabel (const Standard_Integer mode)
56{
57 thelabmode = mode;
58}
59
60
61Standard_Integer StepSelect_WorkLibrary::ReadFile
62 (const Standard_CString name,
63 Handle(Interface_InterfaceModel)& model,
64 const Handle(Interface_Protocol)& protocol) const
65{
66 long status = 1;
67 DeclareAndCast(StepData_Protocol,stepro,protocol);
68 if (stepro.IsNull()) return 1;
69 Handle(StepData_StepModel) stepmodel = new StepData_StepModel;
70 model = stepmodel;
71 StepFile_ReadTrace (0);
72 char *pName=(char *)name;
73 status = StepFile_Read (pName,stepmodel,stepro);
74 return status;
75}
76
77
78Standard_Boolean StepSelect_WorkLibrary::WriteFile
79 (IFSelect_ContextWrite& ctx) const
80{
81// Preparation
82 Handle(Message_Messenger) sout = Message::DefaultMessenger();
83 DeclareAndCast(StepData_StepModel,stepmodel,ctx.Model());
84 DeclareAndCast(StepData_Protocol,stepro,ctx.Protocol());
85 if (stepmodel.IsNull() || stepro.IsNull()) return Standard_False;
86
87 ofstream fout;
94708556 88 OSD_OpenStream(fout,ctx.FileName(),ios::out|ios::trunc);
7fd59977 89
90 if (!fout || !fout.rdbuf()->is_open()) {
91 ctx.CCheck(0)->AddFail("Step File could not be created");
92 sout<<" Step File could not be created : " << ctx.FileName() << endl; return 0;
93 }
94 sout << " Step File Name : "<<ctx.FileName();
95 StepData_StepWriter SW(stepmodel);
96 sout<<"("<<stepmodel->NbEntities()<<" ents) ";
97
98// File Modifiers
99 Standard_Integer nbmod = ctx.NbModifiers();
100 for (Standard_Integer numod = 1; numod <= nbmod; numod ++) {
101 ctx.SetModifier (numod);
102 DeclareAndCast(StepSelect_FileModifier,filemod,ctx.FileModifier());
103 if (!filemod.IsNull()) filemod->Perform(ctx,SW);
104// (impressions de mise au point)
105 sout << " .. FileMod." << numod << filemod->Label();
106 if (ctx.IsForAll()) sout << " (all model)";
107 else sout << " (" << ctx.NbEntities() << " entities)";
108// sout << flush;
109 }
110
111// Envoi
112 SW.SendModel(stepro);
113 Interface_CheckIterator chl = SW.CheckList();
114 for (chl.Start(); chl.More(); chl.Next())
115 ctx.CCheck(chl.Number())->GetMessages(chl.Value());
116 sout<<" Write ";
117 Standard_Boolean isGood = SW.Print(fout);
118 sout<<" Done"<<endl;
119
120 errno = 0;
121 fout.close();
122 isGood = fout.good() && isGood && !errno;
123 if(errno)
124 sout << strerror(errno) << endl;
125 return isGood;
126}
127
128
129Standard_Boolean StepSelect_WorkLibrary::CopyModel
130 (const Handle(Interface_InterfaceModel)& original,
131 const Handle(Interface_InterfaceModel)& newmodel,
132 const Interface_EntityIterator& list,
133 Interface_CopyTool& TC) const
134{
135 if (thecopymode) return
136 IFSelect_WorkLibrary::CopyModel (original,newmodel,list,TC);
137 return thecopymode;
138}
139
140
141void StepSelect_WorkLibrary::DumpEntity
142 (const Handle(Interface_InterfaceModel)& model,
143 const Handle(Interface_Protocol)& protocol,
144 const Handle(Standard_Transient)& entity,
145 const Handle(Message_Messenger)& S, const Standard_Integer level) const
146{
147 Standard_Integer nument = model->Number(entity);
148 if (nument <= 0 || nument > model->NbEntities()) return;
149 Standard_Boolean iserr = model->IsRedefinedContent(nument);
150 Handle(Standard_Transient) ent, con; ent = entity;
151 S<<" --- (STEP) Entity "; model->Print(entity,S);
152 if (iserr) con = model->ReportEntity(nument)->Content();
153 if (entity.IsNull()) { S<<" Null"<<endl; return; }
154
155// On attaque le dump : d abord cas de l Erreur
156 S << " Type cdl : " << entity->DynamicType()->Name() << endl;
157 if (iserr)
158 S<<" *** NOT WELL LOADED : CONTENT FROM FILE ***"<<endl;
159 else if (model->IsUnknownEntity(nument))
160 S<<" *** UNKNOWN TYPE ***"<<endl;
161
162 StepData_StepDumper dump(GetCasted(StepData_StepModel,model),
163 GetCasted(StepData_Protocol,protocol),thelabmode);
164 dump.Dump(S,ent,level);
165}