0022731: Error on attempt to write in IGES 5.3 the attached brep model
[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);
572db63c
G
113
114 if(ent.IsNull())
115 return Standard_False;
7fd59977 116// modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
117 XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
118// modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
119
120 //22.10.98 gka BUC60080
121
122 Standard_Integer oldnb = themod->NbEntities();
123 Standard_Boolean aent = AddEntity (ent);
124 Standard_Integer newnb = themod->NbEntities();
125
126 Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
127
128 Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
129 if (tolmod == 2)
130 newtol = Interface_Static::RVal("write.precision.val");
131 else {
132 ShapeAnalysis_ShapeTolerance stu;
133 Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
134 Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE);
135
136 if (tolmod == 0 ) { //Average
137 Standard_Real Tol1 = (Tolv + Tole) / 2;
138 newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
139 }
140 else if (tolmod < 0) {//Least
141 newtol = Min (Tolv, Tole);
142 if (oldnb > 0) newtol = Min (oldtol, newtol);
143 }
144 else { //Greatest
145 newtol = Max (Tolv, Tole);
146 if (oldnb > 0) newtol = Max (oldtol, newtol);
147 }
148 }
149
150 IGESData_GlobalSection gs = themod->GlobalSection();
151 gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
152
153 //#34 22.10.98 rln BUC60081
154 Bnd_Box box;
155 BRepBndLib::Add (Shape, box);
156 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
157 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
158 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
159 aYmax / gs.UnitValue(),
160 aZmax / gs.UnitValue()));
161 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
162 aYmin / gs.UnitValue(),
163 aZmin / gs.UnitValue()));
164
165 themod->SetGlobalSection(gs);
166
167 return aent;
168}
169
170Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
171{
172 if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
173 return Standard_False;
174 DeclareAndCast(Geom_Curve,Curve,geom);
175 DeclareAndCast(Geom_Surface,Surf,geom);
176 Handle(IGESData_IGESEntity) ent;
177
178// On reconnait : Curve et Surface de Geom
179// quid de Point; Geom2d ?
180
181// GeomToIGES_GeomPoint GP;
182 GeomToIGES_GeomCurve GC; GC.SetModel(themod);
183 GeomToIGES_GeomSurface GS; GS.SetModel(themod);
184
185 //#34 22.10.98 rln BUC60081
186 IGESData_GlobalSection gs = themod->GlobalSection();
187 Bnd_Box box;
188
189 if (!Curve.IsNull()) {
190 ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
191 BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
192 else if (!Surf.IsNull()) {
193 Standard_Real U1,U2,V1,V2;
194 Surf->Bounds(U1,U2,V1,V2);
195 ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
196 BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
197 }
198
199 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
200 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
201 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
202 aYmax / gs.UnitValue(),
203 aZmax / gs.UnitValue()));
204 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
205 aYmin / gs.UnitValue(),
206 aZmin / gs.UnitValue()));
207 themod->SetGlobalSection(gs);
208 return AddEntity (ent);
209}
210
211Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
212{
213 if (ent.IsNull()) return Standard_False;
214 themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
215 thest = Standard_False;
216 return Standard_True;
217}
218
219void IGESControl_Writer::ComputeModel ()
220{
221 if (thest) return;
222 thedit.ComputeStatus();
223 thedit.AutoCorrectModel();
224 thest = Standard_True;
225}
226
227Standard_Boolean IGESControl_Writer::Write
228 (Standard_OStream& S, const Standard_Boolean fnes)
229{
230 if (!S) return Standard_False;
231 ComputeModel();
572db63c 232 Standard_Integer nbEnt = themod->NbEntities();
7fd59977 233#ifdef DEBUG
572db63c 234 cout<<" IGES Write : "<<nbEnt<<" ent.s"<< flush;
7fd59977 235#endif
572db63c
G
236 if(!nbEnt)
237 return Standard_False;
7fd59977 238 IGESData_IGESWriter IW (themod);
239// ne pas oublier le mode fnes ... a transmettre a IW
240 IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
241#ifdef DEBUG
242 cout<<" ... ecriture ..."<<flush;
243#endif
244 if (fnes) IW.WriteMode() = 10;
245 Standard_Boolean status = IW.Print(S);
246#ifdef DEBUG
247 cout<<" ... fichier ecrit ..."<<endl;
248#endif
249 return status;
250}
251
252Standard_Boolean IGESControl_Writer::Write
253 (const Standard_CString file, const Standard_Boolean fnes)
254{
255 ofstream fout(file,ios::out);
256 if (!fout) return Standard_False;
257#ifdef DEBUG
258 cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
259#endif
260 Standard_Boolean res = Write (fout,fnes);
261
262 errno = 0;
263 fout.close();
264 res = fout.good() && res && !errno;
265
266 return res;
267}
268
269void IGESControl_Writer::PrintStatsTransfer
270 (const Standard_Integer what, const Standard_Integer mode) const
271{
272 XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);
273}