0023822: SIGSEGV 'segmentation violation' during writing IGES
[occt.git] / src / IGESControl / IGESControl_Writer.cxx
CommitLineData
b311480e 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
7fd59977 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
52IGESControl_Writer::IGESControl_Writer ()
53 : theTP (new Transfer_FinderProcess(10000)) ,
54 thedit (IGESSelect_WorkLibrary::DefineProtocol()) ,
06f3d6bc 55 thest (Standard_False)
7fd59977 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();
06f3d6bc 61 thecr = Interface_Static::IVal ("write.iges.brep.mode");
7fd59977 62 themod = thedit.Model();
63}
64
65IGESControl_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
78IGESControl_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
85Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
86{
87 return themod;
88}
89
90Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
91{
92 return theTP;
93}
94
95void IGESControl_Writer::SetTransferProcess
96 (const Handle(Transfer_FinderProcess)& TP)
97{
98 theTP = TP;
99}
100
101Standard_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",
b485ee79
KD
122 "write.iges.sequence", info,
123 progress );
7fd59977 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);
572db63c
G
130
131 if(ent.IsNull())
132 return Standard_False;
7fd59977 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);
6db61714 173 if (!(box.IsVoid() || box.IsOpenXmax() || box.IsOpenYmax() || box.IsOpenZmax() || box.IsOpenXmin() || box.IsOpenYmin() || box.IsOpenZmin())){
174 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
175 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
176 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
177 aYmax / gs.UnitValue(),
178 aZmax / gs.UnitValue()));
179 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
180 aYmin / gs.UnitValue(),
181 aZmin / gs.UnitValue()));
182 }
7fd59977 183
184 themod->SetGlobalSection(gs);
185
186 return aent;
187}
188
189Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
190{
191 if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
192 return Standard_False;
193 DeclareAndCast(Geom_Curve,Curve,geom);
194 DeclareAndCast(Geom_Surface,Surf,geom);
195 Handle(IGESData_IGESEntity) ent;
196
197// On reconnait : Curve et Surface de Geom
198// quid de Point; Geom2d ?
199
200// GeomToIGES_GeomPoint GP;
201 GeomToIGES_GeomCurve GC; GC.SetModel(themod);
202 GeomToIGES_GeomSurface GS; GS.SetModel(themod);
203
204 //#34 22.10.98 rln BUC60081
205 IGESData_GlobalSection gs = themod->GlobalSection();
206 Bnd_Box box;
207
208 if (!Curve.IsNull()) {
209 ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
210 BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
211 else if (!Surf.IsNull()) {
212 Standard_Real U1,U2,V1,V2;
213 Surf->Bounds(U1,U2,V1,V2);
214 ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
215 BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
216 }
217
218 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
219 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
220 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
221 aYmax / gs.UnitValue(),
222 aZmax / gs.UnitValue()));
223 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
224 aYmin / gs.UnitValue(),
225 aZmin / gs.UnitValue()));
226 themod->SetGlobalSection(gs);
227 return AddEntity (ent);
228}
229
230Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
231{
232 if (ent.IsNull()) return Standard_False;
233 themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
234 thest = Standard_False;
235 return Standard_True;
236}
237
238void IGESControl_Writer::ComputeModel ()
239{
240 if (thest) return;
241 thedit.ComputeStatus();
242 thedit.AutoCorrectModel();
243 thest = Standard_True;
244}
245
246Standard_Boolean IGESControl_Writer::Write
247 (Standard_OStream& S, const Standard_Boolean fnes)
248{
249 if (!S) return Standard_False;
250 ComputeModel();
572db63c 251 Standard_Integer nbEnt = themod->NbEntities();
7fd59977 252#ifdef DEBUG
572db63c 253 cout<<" IGES Write : "<<nbEnt<<" ent.s"<< flush;
7fd59977 254#endif
572db63c
G
255 if(!nbEnt)
256 return Standard_False;
7fd59977 257 IGESData_IGESWriter IW (themod);
258// ne pas oublier le mode fnes ... a transmettre a IW
259 IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
260#ifdef DEBUG
261 cout<<" ... ecriture ..."<<flush;
262#endif
263 if (fnes) IW.WriteMode() = 10;
264 Standard_Boolean status = IW.Print(S);
265#ifdef DEBUG
266 cout<<" ... fichier ecrit ..."<<endl;
267#endif
268 return status;
269}
270
271Standard_Boolean IGESControl_Writer::Write
272 (const Standard_CString file, const Standard_Boolean fnes)
273{
274 ofstream fout(file,ios::out);
275 if (!fout) return Standard_False;
276#ifdef 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
288void IGESControl_Writer::PrintStatsTransfer
289 (const Standard_Integer what, const Standard_Integer mode) const
290{
291 XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);
292}