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