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