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