1b7d969e9799b724780978a555371bf40cc3a73f
[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 IMPLEMENT_STANDARD_RTTIEXT(StepSelect_WorkLibrary,IFSelect_WorkLibrary)
46
47 StepSelect_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
59 void  StepSelect_WorkLibrary::SetDumpLabel (const Standard_Integer mode)
60 {
61   thelabmode = mode;
62 }
63
64
65 Standard_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
82 Standard_Boolean  StepSelect_WorkLibrary::WriteFile
83   (IFSelect_ContextWrite& ctx) const
84 {
85 //  Preparation
86   Message_Messenger::StreamBuffer sout = Message::SendInfo();
87   DeclareAndCast(StepData_StepModel,stepmodel,ctx.Model());
88   DeclareAndCast(StepData_Protocol,stepro,ctx.Protocol());
89   if (stepmodel.IsNull() || stepro.IsNull()) return Standard_False;
90
91   std::ofstream fout;
92   OSD_OpenStream(fout,ctx.FileName(),std::ios::out|std::ios::trunc);
93
94   if (!fout || !fout.is_open()) {
95     ctx.CCheck(0)->AddFail("Step File could not be created");
96     sout<<" Step File could not be created : " << ctx.FileName() << std::endl; return 0;
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)";
112 //    sout << std::flush;
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);                 
122   sout<<" Done"<<std::endl;
123       
124   errno = 0;
125   fout.close();
126   isGood = fout.good() && isGood && !errno;
127   if(errno)
128     sout << strerror(errno) << std::endl;
129   return isGood;  
130 }
131
132
133 Standard_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
145 void  StepSelect_WorkLibrary::DumpEntity
146   (const Handle(Interface_InterfaceModel)& model,
147    const Handle(Interface_Protocol)& protocol,
148    const Handle(Standard_Transient)& entity,
149    Standard_OStream& S, const Standard_Integer level) const
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;
155   S <<" --- (STEP) Entity ";  model->Print(entity, S);
156   if (iserr) con = model->ReportEntity(nument)->Content();
157   if (entity.IsNull()) {  S <<" Null"<<std::endl; return;  }
158
159 //  On attaque le dump : d abord cas de l Erreur
160   S << " Type cdl : " << entity->DynamicType()->Name() << std::endl;
161   if (iserr)
162     S <<" ***  NOT WELL LOADED : CONTENT FROM FILE  ***"<<std::endl;
163   else if (model->IsUnknownEntity(nument))
164     S <<" ***  UNKNOWN TYPE  ***"<<std::endl;
165
166   StepData_StepDumper dump(GetCasted(StepData_StepModel,model),
167                            GetCasted(StepData_Protocol,protocol),thelabmode);
168   dump.Dump(S,ent,level);
169 }