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