1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 //cky 16.01.99 Remove couts.
15 //rln 28.12.98 CCI60005
17 #include <Standard_Stream.hxx>
19 #include <IGESControl_Writer.ixx>
20 #include <IGESControl_Controller.hxx>
21 #include <IGESSelect_WorkLibrary.hxx>
22 #include <BRepToIGES_BREntity.hxx>
23 #include <BRepToIGESBRep_Entity.hxx>
24 #include <Geom_Curve.hxx>
25 #include <Geom_Surface.hxx>
26 #include <GeomToIGES_GeomCurve.hxx>
27 #include <GeomToIGES_GeomSurface.hxx>
28 #include <IGESData_IGESWriter.hxx>
29 #include <IGESData_Protocol.hxx>
30 #include <XSControl_TransferWriter.hxx>
32 #include <Interface_Macros.hxx>
33 #include <Interface_Static.hxx>
34 #include <ShapeAnalysis_ShapeTolerance.hxx>
37 #include <Bnd_Box.hxx>
38 #include <BRepBndLib.hxx>
39 #include <GeomAdaptor_Curve.hxx>
40 #include <GeomAdaptor_Surface.hxx>
41 #include <BndLib_Add3dCurve.hxx>
42 #include <BndLib_AddSurface.hxx>
44 #include <XSAlgo_AlgoContainer.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <Message_ProgressIndicator.hxx>
48 #include <OSD_OpenFile.hxx>
49 #include <Geom_Geometry.hxx>
51 IGESControl_Writer::IGESControl_Writer ()
52 : theTP (new Transfer_FinderProcess(10000)) ,
53 thest (Standard_False)
55 // faudrait aussi (?) prendre les parametres par defaut ... ?
56 IGESControl_Controller::Init();
57 thedit.Init(IGESSelect_WorkLibrary::DefineProtocol());
58 thedit.SetUnitName(Interface_Static::CVal ("write.iges.unit"));
60 thecr = Interface_Static::IVal ("write.iges.brep.mode");
61 themod = thedit.Model();
64 IGESControl_Writer::IGESControl_Writer
65 (const Standard_CString unit, const Standard_Integer modecr)
66 : theTP (new Transfer_FinderProcess(10000)) ,
67 thecr (modecr) , thest (Standard_False)
69 // faudrait aussi (?) prendre les parametres par defaut ... ?
70 IGESControl_Controller::Init();
71 thedit.Init(IGESSelect_WorkLibrary::DefineProtocol());
72 thedit.SetUnitName(unit);
74 themod = thedit.Model();
77 IGESControl_Writer::IGESControl_Writer
78 (const Handle(IGESData_IGESModel)& model, const Standard_Integer modecr)
79 : theTP (new Transfer_FinderProcess(10000)) ,
81 thedit (model,IGESSelect_WorkLibrary::DefineProtocol()) ,
82 thecr (modecr) , thest (Standard_False) { }
84 Handle(IGESData_IGESModel) IGESControl_Writer::Model () const
89 Handle(Transfer_FinderProcess) IGESControl_Writer::TransferProcess () const
94 void IGESControl_Writer::SetTransferProcess
95 (const Handle(Transfer_FinderProcess)& TP)
100 Standard_Boolean IGESControl_Writer::AddShape (const TopoDS_Shape& theShape)
102 if (theShape.IsNull()) return Standard_False;
104 // for progress indication
105 Handle(Message_ProgressIndicator) progress = theTP->GetProgress();
106 if ( ! progress.IsNull() ) {
107 Standard_Integer nbfaces=0;
108 for( TopExp_Explorer exp(theShape,TopAbs_FACE); exp.More(); exp.Next() )
110 progress->SetScale ( "Faces", 0, nbfaces, 1 );
113 XSAlgo::AlgoContainer()->PrepareForTransfer();
115 // modified by NIZHNY-EAP Tue Aug 29 11:16:54 2000 ___BEGIN___
116 Handle(Standard_Transient) info;
117 Standard_Real Tol = Interface_Static::RVal("write.precision.val");
118 Standard_Real maxTol = Interface_Static::RVal("read.maxprecision.val");
119 TopoDS_Shape Shape = XSAlgo::AlgoContainer()->ProcessShape( theShape, Tol, maxTol,
120 "write.iges.resource.name",
121 "write.iges.sequence", info,
123 // modified by NIZHNY-EAP Tue Aug 29 11:17:01 2000 ___END___
124 Handle(IGESData_IGESEntity) ent;
125 BRepToIGES_BREntity B0; B0.SetTransferProcess(theTP); B0.SetModel(themod);
126 BRepToIGESBRep_Entity B1; B1.SetTransferProcess(theTP); B1.SetModel(themod);
127 if (thecr) ent = B1.TransferShape(Shape);
128 else ent = B0.TransferShape(Shape);
131 return Standard_False;
132 // modified by NIZHNY-EAP Tue Aug 29 11:37:18 2000 ___BEGIN___
133 XSAlgo::AlgoContainer()->MergeTransferInfo(theTP, info);
134 // modified by NIZHNY-EAP Tue Aug 29 11:37:25 2000 ___END___
136 //22.10.98 gka BUC60080
138 Standard_Integer oldnb = themod->NbEntities();
139 Standard_Boolean aent = AddEntity (ent);
140 Standard_Integer newnb = themod->NbEntities();
142 Standard_Real oldtol = themod->GlobalSection().Resolution(), newtol;
144 Standard_Integer tolmod = Interface_Static::IVal("write.precision.mode");
146 newtol = Interface_Static::RVal("write.precision.val");
148 ShapeAnalysis_ShapeTolerance stu;
149 Standard_Real Tolv = stu.Tolerance (Shape, tolmod, TopAbs_VERTEX);
150 Standard_Real Tole = stu.Tolerance (Shape, tolmod, TopAbs_EDGE);
152 if (tolmod == 0 ) { //Average
153 Standard_Real Tol1 = (Tolv + Tole) / 2;
154 newtol = (oldtol * oldnb + Tol1 * (newnb - oldnb)) / newnb;
156 else if (tolmod < 0) {//Least
157 newtol = Min (Tolv, Tole);
158 if (oldnb > 0) newtol = Min (oldtol, newtol);
161 newtol = Max (Tolv, Tole);
162 if (oldnb > 0) newtol = Max (oldtol, newtol);
166 IGESData_GlobalSection gs = themod->GlobalSection();
167 gs.SetResolution (newtol / gs.UnitValue());//rln 28.12.98 CCI60005
169 //#34 22.10.98 rln BUC60081
171 BRepBndLib::Add (Shape, box);
172 if (!(box.IsVoid() || box.IsOpenXmax() || box.IsOpenYmax() || box.IsOpenZmax() || box.IsOpenXmin() || box.IsOpenYmin() || box.IsOpenZmin())){
173 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
174 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
175 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
176 aYmax / gs.UnitValue(),
177 aZmax / gs.UnitValue()));
178 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
179 aYmin / gs.UnitValue(),
180 aZmin / gs.UnitValue()));
183 themod->SetGlobalSection(gs);
188 Standard_Boolean IGESControl_Writer::AddGeom (const Handle(Standard_Transient)& geom)
190 if (geom.IsNull() || !geom->IsKind (STANDARD_TYPE (Geom_Geometry)))
191 return Standard_False;
192 DeclareAndCast(Geom_Curve,Curve,geom);
193 DeclareAndCast(Geom_Surface,Surf,geom);
194 Handle(IGESData_IGESEntity) ent;
196 // On reconnait : Curve et Surface de Geom
197 // quid de Point; Geom2d ?
199 // GeomToIGES_GeomPoint GP;
200 GeomToIGES_GeomCurve GC; GC.SetModel(themod);
201 GeomToIGES_GeomSurface GS; GS.SetModel(themod);
203 //#34 22.10.98 rln BUC60081
204 IGESData_GlobalSection gs = themod->GlobalSection();
207 if (!Curve.IsNull()) {
208 ent = GC.TransferCurve(Curve,Curve->FirstParameter(),Curve->LastParameter());
209 BndLib_Add3dCurve::Add (GeomAdaptor_Curve (Curve), 0, box); }
210 else if (!Surf.IsNull()) {
211 Standard_Real U1,U2,V1,V2;
212 Surf->Bounds(U1,U2,V1,V2);
213 ent = GS.TransferSurface(Surf,U1,U2,V1,V2);
214 BndLib_AddSurface::Add (GeomAdaptor_Surface (Surf), 0, box);
217 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
218 box.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
219 gs.MaxMaxCoords (gp_XYZ (aXmax / gs.UnitValue(),
220 aYmax / gs.UnitValue(),
221 aZmax / gs.UnitValue()));
222 gs.MaxMaxCoords (gp_XYZ (aXmin / gs.UnitValue(),
223 aYmin / gs.UnitValue(),
224 aZmin / gs.UnitValue()));
225 themod->SetGlobalSection(gs);
226 return AddEntity (ent);
229 Standard_Boolean IGESControl_Writer::AddEntity (const Handle(IGESData_IGESEntity)& ent)
231 if (ent.IsNull()) return Standard_False;
232 themod->AddWithRefs(ent,IGESSelect_WorkLibrary::DefineProtocol());
233 thest = Standard_False;
234 return Standard_True;
237 void IGESControl_Writer::ComputeModel ()
240 thedit.ComputeStatus();
241 thedit.AutoCorrectModel();
242 thest = Standard_True;
245 Standard_Boolean IGESControl_Writer::Write
246 (Standard_OStream& S, const Standard_Boolean fnes)
248 if (!S) return Standard_False;
250 Standard_Integer nbEnt = themod->NbEntities();
252 cout<<" IGES Write : "<<nbEnt<<" ent.s"<< flush;
255 return Standard_False;
256 IGESData_IGESWriter IW (themod);
257 // ne pas oublier le mode fnes ... a transmettre a IW
258 IW.SendModel (IGESSelect_WorkLibrary::DefineProtocol());
260 cout<<" ... ecriture ..."<<flush;
262 if (fnes) IW.WriteMode() = 10;
263 Standard_Boolean status = IW.Print(S);
265 cout<<" ... fichier ecrit ..."<<endl;
270 Standard_Boolean IGESControl_Writer::Write
271 (const Standard_CString file, const Standard_Boolean fnes)
274 OSD_OpenStream(fout,file,ios::out);
275 if (!fout) return Standard_False;
277 cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<<file<<endl;
279 Standard_Boolean res = Write (fout,fnes);
283 res = fout.good() && res && !errno;
288 void IGESControl_Writer::PrintStatsTransfer
289 (const Standard_Integer what, const Standard_Integer mode) const
291 XSControl_TransferWriter::PrintStatsProcess (theTP,what,mode);