0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / StepSelect / StepSelect_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
15 #include <IFSelect_ContextWrite.hxx>
16 #include <IFSelect_GeneralModifier.hxx>
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>
23 #include <Interface_ParamType.hxx>
24 #include <Interface_Protocol.hxx>
25 #include <Interface_ReportEntity.hxx>
26 #include <Interface_UndefinedContent.hxx>
27 #include <Message.hxx>
28 #include <Message_Messenger.hxx>
29 #include <OSD_OpenFile.hxx>
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>
42
43 #include <errno.h>
44 #include <sys/stat.h>
45 StepSelect_WorkLibrary::StepSelect_WorkLibrary
46   (const Standard_Boolean copymode)
47   : thecopymode (copymode) , thelabmode (0)
48 {
49   SetDumpLevels (1,2);
50   SetDumpHelp (0,"#id + Step Type");
51   SetDumpHelp (1,"Entity as in file");
52   SetDumpHelp (2,"Entity + shareds (level 1) as in file");
53 }
54 // rq : les init sont faits par ailleurs, pas de souci a se faire
55
56
57 void  StepSelect_WorkLibrary::SetDumpLabel (const Standard_Integer mode)
58 {
59   thelabmode = mode;
60 }
61
62
63 Standard_Integer  StepSelect_WorkLibrary::ReadFile
64   (const Standard_CString name,
65    Handle(Interface_InterfaceModel)& model,
66    const Handle(Interface_Protocol)& protocol) const
67 {
68   long status = 1;
69   DeclareAndCast(StepData_Protocol,stepro,protocol);
70   if (stepro.IsNull()) return 1;
71   Handle(StepData_StepModel) stepmodel  = new StepData_StepModel;
72   model  = stepmodel;
73   StepFile_ReadTrace (0);
74   char *pName=(char *)name;
75   status = StepFile_Read (pName,stepmodel,stepro);
76   return status;
77 }
78
79
80 Standard_Boolean  StepSelect_WorkLibrary::WriteFile
81   (IFSelect_ContextWrite& ctx) const
82 {
83 //  Preparation
84   Handle(Message_Messenger) sout = Message::DefaultMessenger();
85   DeclareAndCast(StepData_StepModel,stepmodel,ctx.Model());
86   DeclareAndCast(StepData_Protocol,stepro,ctx.Protocol());
87   if (stepmodel.IsNull() || stepro.IsNull()) return Standard_False;
88
89   ofstream fout;
90   OSD_OpenStream(fout,ctx.FileName(),ios::out|ios::trunc);
91
92   if (!fout || !fout.rdbuf()->is_open()) {
93     ctx.CCheck(0)->AddFail("Step File could not be created");
94     sout<<" Step File could not be created : " << ctx.FileName() << endl; return 0;
95   }
96   sout << " Step File Name : "<<ctx.FileName();
97   StepData_StepWriter SW(stepmodel);
98   sout<<"("<<stepmodel->NbEntities()<<" ents) ";
99
100 //  File Modifiers
101   Standard_Integer nbmod = ctx.NbModifiers();
102   for (Standard_Integer numod = 1; numod <= nbmod; numod ++) {
103     ctx.SetModifier (numod);
104     DeclareAndCast(StepSelect_FileModifier,filemod,ctx.FileModifier());
105     if (!filemod.IsNull()) filemod->Perform(ctx,SW);
106 //   (impressions de mise au point)
107     sout << " .. FileMod." << numod << filemod->Label();
108     if (ctx.IsForAll()) sout << " (all model)";
109     else  sout << " (" << ctx.NbEntities() << " entities)";
110 //    sout << flush;
111   }
112
113 //  Envoi
114   SW.SendModel(stepro);
115   Interface_CheckIterator chl = SW.CheckList();
116   for (chl.Start(); chl.More(); chl.Next())
117     ctx.CCheck(chl.Number())->GetMessages(chl.Value());
118   sout<<" Write ";
119   Standard_Boolean isGood = SW.Print(fout);                 
120   sout<<" Done"<<endl;
121       
122   errno = 0;
123   fout.close();
124   isGood = fout.good() && isGood && !errno;
125   if(errno)
126     sout << strerror(errno) << endl;
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,
147    const Handle(Message_Messenger)& S, const Standard_Integer level) const
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;
153   S<<" --- (STEP) Entity ";  model->Print(entity,S);
154   if (iserr) con = model->ReportEntity(nument)->Content();
155   if (entity.IsNull()) {  S<<" Null"<<endl; return;  }
156
157 //  On attaque le dump : d abord cas de l Erreur
158   S << " Type cdl : " << entity->DynamicType()->Name() << endl;
159   if (iserr)
160     S<<" ***  NOT WELL LOADED : CONTENT FROM FILE  ***"<<endl;
161   else if (model->IsUnknownEntity(nument))
162     S<<" ***  UNKNOWN TYPE  ***"<<endl;
163
164   StepData_StepDumper dump(GetCasted(StepData_StepModel,model),
165                            GetCasted(StepData_Protocol,protocol),thelabmode);
166   dump.Dump(S,ent,level);
167 }