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