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