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