0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / IGESControl / IGESControl_Writer.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 //cky 16.01.99 Remove couts.
15 //rln 28.12.98 CCI60005
16
17 #include <Standard_Stream.hxx>
18
19 #include <IGESControl_Writer.ixx>
20 #include <IGESControl_Controller.hxx>
21 #include <IGESSelect_WorkLibrary.hxx>
22 #include <BRepToIGES_BREntity.hxx>
23 #include <BRepToIGESBRep_Entity.hxx>
24 #include <Geom_Curve.hxx>
25 #include <Geom_Surface.hxx>
26 #include <GeomToIGES_GeomCurve.hxx>
27 #include <GeomToIGES_GeomSurface.hxx>
28 #include <IGESData_IGESWriter.hxx>
29 #include <XSControl_TransferWriter.hxx>
30
31 #include <Interface_Macros.hxx>
32 #include <Interface_Static.hxx>
33 #include <ShapeAnalysis_ShapeTolerance.hxx>
34
35 #include <gp_XYZ.hxx>
36 #include <Bnd_Box.hxx>
37 #include <BRepBndLib.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <GeomAdaptor_Surface.hxx>
40 #include <BndLib_Add3dCurve.hxx>
41 #include <BndLib_AddSurface.hxx>
42 #include <XSAlgo.hxx>
43 #include <XSAlgo_AlgoContainer.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <Message_ProgressIndicator.hxx>
46 #include <errno.h>
47
48 IGESControl_Writer::IGESControl_Writer ()
49     :  theTP (new Transfer_FinderProcess(10000)) ,
50        thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
51        thest (Standard_False)
52 {
53 //  faudrait aussi (?) prendre les parametres par defaut ... ?
54   IGESControl_Controller::Init();
55   thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
56   thedit.ApplyUnit(); 
57   thecr = Interface_Static::IVal ("write.iges.brep.mode");
58   themod = thedit.Model();
59 }
60
61 IGESControl_Writer::IGESControl_Writer
62   (const Standard_CString unit, const Standard_Integer modecr)
63     :  theTP (new Transfer_FinderProcess(10000)) ,
64        thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
65        thecr (modecr) , thest (Standard_False)
66 {
67 //  faudrait aussi (?) prendre les parametres par defaut ... ?
68   IGESControl_Controller::Init();
69   thedit.SetUnitName(unit);
70   thedit.ApplyUnit();
71   themod = thedit.Model();
72 }
73
74 IGESControl_Writer::IGESControl_Writer
75   (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
76     :  theTP (new Transfer_FinderProcess(10000)) ,
77        themod (model) , 
78        thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
79        thecr (modecr) , thest (Standard_False)     {  }
80
81 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
82 {
83   return themod;
84 }
85
86 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
87 {
88   return theTP;
89 }
90
91 void IGESControl_Writer::SetTransferProcess
92   (const Handle(Transfer_FinderProcess)& TP)
93 {
94   theTP = TP;
95 }
96
97 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
98 {
99   if (theShape.IsNull()) return Standard_False;
100   
101   // for progress indication
102   Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
103   if ( ! progress.IsNull() ) { 
104     Standard_Integer nbfaces=0;
105     for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
106       nbfaces++;
107     progress->SetScale ( "Faces", 0, nbfaces, 1 );
108   }
109   
110   XSAlgo::AlgoContainer()->PrepareForTransfer();
111   
112   //  modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
113   Handle(Standard_Transient) info;
114   Standard_Real Tol = Interface_Static::RVal("write.precision.val");
115   Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
116   TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol, 
117                                                               "write.iges.resource.name", 
118                                                               "write.iges.sequence", info,
119                                                               progress );
120   //  modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
121   Handle(IGESData_IGESEntity) ent; 
122   BRepToIGES_BREntity   B0;  B0.SetTransferProcess(theTP); B0.SetModel(themod);
123   BRepToIGESBRep_Entity B1;  B1.SetTransferProcess(theTP); B1.SetModel(themod);
124   if (thecr) ent = B1.TransferShape(Shape);
125   else       ent = B0.TransferShape(Shape);
126
127   if(ent.IsNull())
128     return Standard_False;
129 //  modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
130   XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
131 //  modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
132   
133   //22.10.98 gka BUC60080
134
135   Standard_Integer oldnb = themod->NbEntities();
136   Standard_Boolean aent = AddEntity (ent);
137   Standard_Integer newnb = themod->NbEntities();
138
139   Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
140   
141   Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
142   if (tolmod == 2)
143     newtol = Interface_Static::RVal("write.precision.val");
144   else {
145     ShapeAnalysis_ShapeTolerance stu; 
146     Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
147     Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE); 
148
149     if (tolmod == 0 ) {   //Average
150       Standard_Real Tol1 = (Tolv + Tole) / 2;
151       newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
152     }
153     else if (tolmod < 0) {//Least
154       newtol = Min (Tolv, Tole);
155       if (oldnb > 0) newtol = Min (oldtol, newtol);
156     }
157     else {                //Greatest
158       newtol = Max (Tolv, Tole);
159       if (oldnb > 0) newtol = Max (oldtol, newtol);
160     }
161   }
162   
163   IGESData_GlobalSection gs = themod->GlobalSection();
164   gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
165
166   //#34 22.10.98 rln BUC60081
167   Bnd_Box box;
168   BRepBndLib::Add (Shape, box);
169   if (!(box.IsVoid() || box.IsOpenXmax() || box.IsOpenYmax() || box.IsOpenZmax() || box.IsOpenXmin() || box.IsOpenYmin() || box.IsOpenZmin())){
170     Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
171     box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
172     gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
173                              aYmax / gs.UnitValue(),
174                              aZmax / gs.UnitValue()));
175     gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
176                              aYmin / gs.UnitValue(),
177                              aZmin / gs.UnitValue()));
178   }
179
180   themod->SetGlobalSection(gs);
181
182   return aent;
183 }
184
185 Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
186 {
187   if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
188     return Standard_False;
189   DeclareAndCast(Geom_Curve,Curve,geom);
190   DeclareAndCast(Geom_Surface,Surf,geom);
191   Handle(IGESData_IGESEntity) ent; 
192
193 //  On reconnait : Curve et Surface de Geom
194 //   quid de Point; Geom2d ?
195
196 //  GeomToIGES_GeomPoint GP;
197   GeomToIGES_GeomCurve GC;    GC.SetModel(themod);
198   GeomToIGES_GeomSurface GS;  GS.SetModel(themod);
199
200   //#34 22.10.98 rln BUC60081
201   IGESData_GlobalSection gs = themod->GlobalSection();
202   Bnd_Box box;
203
204   if (!Curve.IsNull()) {
205     ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
206     BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
207   else if (!Surf.IsNull()) {
208     Standard_Real U1,U2,V1,V2;
209     Surf->Bounds(U1,U2,V1,V2);
210     ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
211     BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
212   }
213
214   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
215   box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
216   gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
217                            aYmax / gs.UnitValue(),
218                            aZmax / gs.UnitValue()));
219   gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
220                            aYmin / gs.UnitValue(),
221                            aZmin / gs.UnitValue()));
222   themod->SetGlobalSection(gs);
223   return AddEntity (ent);
224 }
225
226 Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
227 {
228   if (ent.IsNull()) return Standard_False;
229   themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
230   thest = Standard_False;
231   return Standard_True;
232 }
233
234 void IGESControl_Writer::ComputeModel ()
235 {
236   if (thest) return;
237   thedit.ComputeStatus();
238   thedit.AutoCorrectModel();
239   thest = Standard_True;
240 }
241
242 Standard_Boolean IGESControl_Writer::Write
243   (Standard_OStream& S, const Standard_Boolean fnes)
244 {
245   if (!S) return Standard_False;
246   ComputeModel();
247   Standard_Integer nbEnt = themod->NbEntities();
248 #ifdef IGESCONTROL_DEB
249   cout<<" IGES Write : "<<nbEnt<<" ent.s"<< flush;
250 #endif
251   if(!nbEnt)
252     return Standard_False;
253   IGESData_IGESWriter IW (themod);
254 //  ne pas oublier le mode fnes ... a transmettre a IW
255   IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
256 #ifdef IGESCONTROL_DEB
257   cout<<" ...  ecriture  ..."<<flush;
258 #endif
259   if (fnes) IW.WriteMode() = 10;
260   Standard_Boolean status = IW.Print(S);
261 #ifdef IGESCONTROL_DEB
262   cout<<" ...  fichier ecrit  ..."<<endl;
263 #endif
264   return status;
265 }
266
267 Standard_Boolean IGESControl_Writer::Write
268   (const Standard_CString file, const Standard_Boolean fnes)
269 {
270   ofstream fout(file,ios::out);
271   if (!fout) return Standard_False;
272 #ifdef IGESCONTROL_DEB
273   cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
274 #endif
275   Standard_Boolean res = Write (fout,fnes);
276
277   errno = 0;
278   fout.close();
279   res = fout.good() && res && !errno;
280
281   return res;
282 }
283
284 void IGESControl_Writer::PrintStatsTransfer
285   (const Standard_Integer what, const Standard_Integer mode) const
286 {
287   XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);
288 }