0023024: Update headers of OCCT files
[occt.git] / src / IGESControl / IGESControl_Writer.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 //cky 16.01.99 Remove couts.
19 //rln 28.12.98 CCI60005
20
21 #include <Standard_Stream.hxx>
22
23 #include <IGESControl_Writer.ixx>
24 #include <IGESControl_Controller.hxx>
25 #include <IGESSelect_WorkLibrary.hxx>
26 #include <BRepToIGES_BREntity.hxx>
27 #include <BRepToIGESBRep_Entity.hxx>
28 #include <Geom_Curve.hxx>
29 #include <Geom_Surface.hxx>
30 #include <GeomToIGES_GeomCurve.hxx>
31 #include <GeomToIGES_GeomSurface.hxx>
32 #include <IGESData_IGESWriter.hxx>
33 #include <XSControl_TransferWriter.hxx>
34
35 #include <Interface_Macros.hxx>
36 #include <Interface_Static.hxx>
37 #include <ShapeAnalysis_ShapeTolerance.hxx>
38
39 #include <gp_XYZ.hxx>
40 #include <Bnd_Box.hxx>
41 #include <BRepBndLib.hxx>
42 #include <GeomAdaptor_Curve.hxx>
43 #include <GeomAdaptor_Surface.hxx>
44 #include <BndLib_Add3dCurve.hxx>
45 #include <BndLib_AddSurface.hxx>
46 #include <XSAlgo.hxx>
47 #include <XSAlgo_AlgoContainer.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <Message_ProgressIndicator.hxx>
50 #include <errno.h>
51
52 IGESControl_Writer::IGESControl_Writer ()
53     :  theTP (new Transfer_FinderProcess(10000)) ,
54        thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
55        thest (Standard_False)
56 {
57 //  faudrait aussi (?) prendre les parametres par defaut ... ?
58   IGESControl_Controller::Init();
59   thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
60   thedit.ApplyUnit(); 
61   thecr = Interface_Static::IVal ("write.iges.brep.mode");
62   themod = thedit.Model();
63 }
64
65 IGESControl_Writer::IGESControl_Writer
66   (const Standard_CString unit, const Standard_Integer modecr)
67     :  theTP (new Transfer_FinderProcess(10000)) ,
68        thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
69        thecr (modecr) , thest (Standard_False)
70 {
71 //  faudrait aussi (?) prendre les parametres par defaut ... ?
72   IGESControl_Controller::Init();
73   thedit.SetUnitName(unit);
74   thedit.ApplyUnit();
75   themod = thedit.Model();
76 }
77
78 IGESControl_Writer::IGESControl_Writer
79   (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
80     :  theTP (new Transfer_FinderProcess(10000)) ,
81        themod (model) , 
82        thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
83        thecr (modecr) , thest (Standard_False)     {  }
84
85 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
86 {
87   return themod;
88 }
89
90 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
91 {
92   return theTP;
93 }
94
95 void IGESControl_Writer::SetTransferProcess
96   (const Handle(Transfer_FinderProcess)& TP)
97 {
98   theTP = TP;
99 }
100
101 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
102 {
103   if (theShape.IsNull()) return Standard_False;
104   
105   // for progress indication
106   Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
107   if ( ! progress.IsNull() ) { 
108     Standard_Integer nbfaces=0;
109     for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
110       nbfaces++;
111     progress->SetScale ( "Faces", 0, nbfaces, 1 );
112   }
113   
114   XSAlgo::AlgoContainer()->PrepareForTransfer();
115   
116   //  modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
117   Handle(Standard_Transient) info;
118   Standard_Real Tol = Interface_Static::RVal("write.precision.val");
119   Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
120   TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol, 
121                                                               "write.iges.resource.name", 
122                                                               "write.iges.sequence", info,
123                                                               progress );
124   //  modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
125   Handle(IGESData_IGESEntity) ent; 
126   BRepToIGES_BREntity   B0;  B0.SetTransferProcess(theTP); B0.SetModel(themod);
127   BRepToIGESBRep_Entity B1;  B1.SetTransferProcess(theTP); B1.SetModel(themod);
128   if (thecr) ent = B1.TransferShape(Shape);
129   else       ent = B0.TransferShape(Shape);
130
131   if(ent.IsNull())
132     return Standard_False;
133 //  modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
134   XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
135 //  modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
136   
137   //22.10.98 gka BUC60080
138
139   Standard_Integer oldnb = themod->NbEntities();
140   Standard_Boolean aent = AddEntity (ent);
141   Standard_Integer newnb = themod->NbEntities();
142
143   Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
144   
145   Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
146   if (tolmod == 2)
147     newtol = Interface_Static::RVal("write.precision.val");
148   else {
149     ShapeAnalysis_ShapeTolerance stu; 
150     Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
151     Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE); 
152
153     if (tolmod == 0 ) {   //Average
154       Standard_Real Tol1 = (Tolv + Tole) / 2;
155       newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
156     }
157     else if (tolmod < 0) {//Least
158       newtol = Min (Tolv, Tole);
159       if (oldnb > 0) newtol = Min (oldtol, newtol);
160     }
161     else {                //Greatest
162       newtol = Max (Tolv, Tole);
163       if (oldnb > 0) newtol = Max (oldtol, newtol);
164     }
165   }
166   
167   IGESData_GlobalSection gs = themod->GlobalSection();
168   gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
169
170   //#34 22.10.98 rln BUC60081
171   Bnd_Box box;
172   BRepBndLib::Add (Shape, box);
173   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
174   box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
175   gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
176                            aYmax / gs.UnitValue(),
177                            aZmax / gs.UnitValue()));
178   gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
179                            aYmin / gs.UnitValue(),
180                            aZmin / gs.UnitValue()));
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 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 DEBUG
259   cout<<" ...  ecriture  ..."<<flush;
260 #endif
261   if (fnes) IW.WriteMode() = 10;
262   Standard_Boolean status = IW.Print(S);
263 #ifdef 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(file,ios::out);
273   if (!fout) return Standard_False;
274 #ifdef DEBUG
275   cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
276 #endif
277   Standard_Boolean res = Write (fout,fnes);
278
279   errno = 0;
280   fout.close();
281   res = fout.good() && res && !errno;
282
283   return res;
284 }
285
286 void IGESControl_Writer::PrintStatsTransfer
287   (const Standard_Integer what, const Standard_Integer mode) const
288 {
289   XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);
290 }